00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "openttd.h"
00014 #include "graph_gui.h"
00015 #include "window_gui.h"
00016 #include "company_base.h"
00017 #include "company_gui.h"
00018 #include "economy_func.h"
00019 #include "cargotype.h"
00020 #include "strings_func.h"
00021 #include "window_func.h"
00022 #include "date_func.h"
00023 #include "gfx_func.h"
00024 #include "sortlist_type.h"
00025 #include "core/geometry_func.hpp"
00026 #include "math.h"
00027 #include "currency.h"
00028
00029 #include "table/strings.h"
00030 #include "table/sprites.h"
00031
00032
00033 static uint _legend_excluded_companies;
00034 static uint _legend_excluded_cargo;
00035
00036
00037 static const OverflowSafeInt64 INVALID_DATAPOINT(INT64_MAX);
00038 static const uint INVALID_DATAPOINT_POS = UINT_MAX;
00039
00040
00041
00042
00043
00045 enum GraphLegendWidgetNumbers {
00046 GLW_BACKGROUND,
00047
00048 GLW_FIRST_COMPANY,
00049 GLW_LAST_COMPANY = GLW_FIRST_COMPANY + MAX_COMPANIES - 1,
00050 };
00051
00052 struct GraphLegendWindow : Window {
00053 GraphLegendWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00054 {
00055 this->InitNested(desc, window_number);
00056
00057 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00058 if (!HasBit(_legend_excluded_companies, c)) this->LowerWidget(c + GLW_FIRST_COMPANY);
00059
00060 this->OnInvalidateData(c);
00061 }
00062 }
00063
00064 virtual void DrawWidget(const Rect &r, int widget) const
00065 {
00066 if (!IsInsideMM(widget, GLW_FIRST_COMPANY, MAX_COMPANIES + GLW_FIRST_COMPANY)) return;
00067
00068 CompanyID cid = (CompanyID)(widget - GLW_FIRST_COMPANY);
00069
00070 if (!Company::IsValidID(cid)) return;
00071
00072 bool rtl = _current_text_dir == TD_RTL;
00073
00074 DrawCompanyIcon(cid, rtl ? r.right - 16 : r.left + 2, r.top + 2 + (FONT_HEIGHT_NORMAL - 10) / 2);
00075
00076 SetDParam(0, cid);
00077 SetDParam(1, cid);
00078 DrawString(r.left + (rtl ? WD_FRAMERECT_LEFT : 19), r.right - (rtl ? 19 : WD_FRAMERECT_RIGHT), r.top + WD_FRAMERECT_TOP, STR_COMPANY_NAME_COMPANY_NUM, HasBit(_legend_excluded_companies, cid) ? TC_BLACK : TC_WHITE);
00079 }
00080
00081 virtual void OnClick(Point pt, int widget, int click_count)
00082 {
00083 if (!IsInsideMM(widget, GLW_FIRST_COMPANY, MAX_COMPANIES + GLW_FIRST_COMPANY)) return;
00084
00085 ToggleBit(_legend_excluded_companies, widget - GLW_FIRST_COMPANY);
00086 this->ToggleWidgetLoweredState(widget);
00087 this->SetDirty();
00088 InvalidateWindowData(WC_INCOME_GRAPH, 0);
00089 InvalidateWindowData(WC_OPERATING_PROFIT, 0);
00090 InvalidateWindowData(WC_DELIVERED_CARGO, 0);
00091 InvalidateWindowData(WC_PERFORMANCE_HISTORY, 0);
00092 InvalidateWindowData(WC_COMPANY_VALUE, 0);
00093 }
00094
00095 virtual void OnInvalidateData(int data)
00096 {
00097 if (Company::IsValidID(data)) return;
00098
00099 SetBit(_legend_excluded_companies, data);
00100 this->RaiseWidget(data + GLW_FIRST_COMPANY);
00101 }
00102 };
00103
00110 static NWidgetBase *MakeNWidgetCompanyLines(int *biggest_index)
00111 {
00112 NWidgetVertical *vert = new NWidgetVertical();
00113
00114 for (int widnum = GLW_FIRST_COMPANY; widnum <= GLW_LAST_COMPANY; widnum++) {
00115 NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
00116 panel->SetMinimalSize(246, FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
00117 panel->SetFill(1, 0);
00118 panel->SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION_TOOLTIP);
00119 vert->Add(panel);
00120 }
00121 *biggest_index = GLW_LAST_COMPANY;
00122 return vert;
00123 }
00124
00125 static const NWidgetPart _nested_graph_legend_widgets[] = {
00126 NWidget(NWID_HORIZONTAL),
00127 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00128 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_KEY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00129 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00130 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00131 EndContainer(),
00132 NWidget(WWT_PANEL, COLOUR_GREY, GLW_BACKGROUND),
00133 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00134 NWidget(NWID_HORIZONTAL),
00135 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00136 NWidgetFunction(MakeNWidgetCompanyLines),
00137 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00138 EndContainer(),
00139 EndContainer(),
00140 };
00141
00142 static const WindowDesc _graph_legend_desc(
00143 WDP_AUTO, 0, 0,
00144 WC_GRAPH_LEGEND, WC_NONE,
00145 0,
00146 _nested_graph_legend_widgets, lengthof(_nested_graph_legend_widgets)
00147 );
00148
00149 static void ShowGraphLegend()
00150 {
00151 AllocateWindowDescFront<GraphLegendWindow>(&_graph_legend_desc, 0);
00152 }
00153
00155 struct ValuesInterval {
00156 OverflowSafeInt64 highest;
00157 OverflowSafeInt64 lowest;
00158 };
00159
00160
00161
00162
00163
00165 enum CompanyValueWidgets {
00166 BGW_KEY_BUTTON,
00167 BGW_BACKGROUND,
00168 BGW_GRAPH,
00169 BGW_RESIZE,
00170 };
00171
00172 struct BaseGraphWindow : Window {
00173 protected:
00174 static const int GRAPH_MAX_DATASETS = 32;
00175 static const int GRAPH_AXIS_LINE_COLOUR = 215;
00176 static const int GRAPH_NUM_MONTHS = 24;
00177
00178 static const int MIN_GRAPH_NUM_LINES_Y = 9;
00179 static const int MIN_GRID_PIXEL_SIZE = 20;
00180
00181 uint excluded_data;
00182 byte num_dataset;
00183 byte num_on_x_axis;
00184 byte num_vert_lines;
00185 static const TextColour graph_axis_label_colour = TC_BLACK;
00186
00187
00188
00189 byte month;
00190 Year year;
00191
00192
00193
00194 uint16 x_values_start;
00195 uint16 x_values_increment;
00196
00197 int graph_widget;
00198 StringID format_str_y_axis;
00199 byte colours[GRAPH_MAX_DATASETS];
00200 OverflowSafeInt64 cost[GRAPH_MAX_DATASETS][GRAPH_NUM_MONTHS];
00201
00208 ValuesInterval GetValuesInterval(int num_hori_lines) const
00209 {
00210 ValuesInterval current_interval;
00211 current_interval.highest = INT64_MIN;
00212 current_interval.lowest = INT64_MAX;
00213
00214 for (int i = 0; i < this->num_dataset; i++) {
00215 if (HasBit(this->excluded_data, i)) continue;
00216 for (int j = 0; j < this->num_on_x_axis; j++) {
00217 OverflowSafeInt64 datapoint = this->cost[i][j];
00218
00219 if (datapoint != INVALID_DATAPOINT) {
00220 current_interval.highest = max(current_interval.highest, datapoint);
00221 current_interval.lowest = min(current_interval.lowest, datapoint);
00222 }
00223 }
00224 }
00225
00226
00227 current_interval.highest = (11 * current_interval.highest) / 10;
00228 current_interval.lowest = (11 * current_interval.lowest) / 10;
00229
00230
00231 double abs_lower = (current_interval.lowest > 0) ? 0 : (double)abs(current_interval.lowest);
00232 double abs_higher = (current_interval.highest < 0) ? 0 : (double)current_interval.highest;
00233
00234 int num_pos_grids;
00235 int64 grid_size;
00236
00237 if (abs_lower != 0 || abs_higher != 0) {
00238
00239 num_pos_grids = (int)floor(0.5 + num_hori_lines * abs_higher / (abs_higher + abs_lower));
00240
00241
00242 if (num_pos_grids == 0 && abs_higher != 0) num_pos_grids++;
00243 if (num_pos_grids == num_hori_lines && abs_lower != 0) num_pos_grids--;
00244
00245
00246 int64 grid_size_higher = (abs_higher > 0) ? ((int64)abs_higher + num_pos_grids - 1) / num_pos_grids : 0;
00247 int64 grid_size_lower = (abs_lower > 0) ? ((int64)abs_lower + num_hori_lines - num_pos_grids - 1) / (num_hori_lines - num_pos_grids) : 0;
00248 grid_size = max(grid_size_higher, grid_size_lower);
00249 } else {
00250
00251 num_pos_grids = num_hori_lines / 2;
00252 grid_size = 1;
00253 }
00254
00255 current_interval.highest = num_pos_grids * grid_size;
00256 current_interval.lowest = -(num_hori_lines - num_pos_grids) * grid_size;
00257 return current_interval;
00258 }
00259
00265 uint GetYLabelWidth(ValuesInterval current_interval, int num_hori_lines) const
00266 {
00267
00268 int64 y_label = current_interval.highest;
00269 int64 y_label_separation = (current_interval.highest - current_interval.lowest) / num_hori_lines;
00270
00271 uint max_width = 0;
00272
00273 for (int i = 0; i < (num_hori_lines + 1); i++) {
00274 SetDParam(0, this->format_str_y_axis);
00275 SetDParam(1, y_label);
00276 Dimension d = GetStringBoundingBox(STR_GRAPH_Y_LABEL);
00277 if (d.width > max_width) max_width = d.width;
00278
00279 y_label -= y_label_separation;
00280 }
00281
00282 return max_width;
00283 }
00284
00289 void DrawGraph(Rect r) const
00290 {
00291 uint x, y;
00292 ValuesInterval interval;
00293 int x_axis_offset;
00294
00295
00296
00297 assert_compile(GRAPH_MAX_DATASETS >= (int)NUM_CARGO && GRAPH_MAX_DATASETS >= (int)MAX_COMPANIES);
00298 assert(this->num_vert_lines > 0);
00299
00300 byte grid_colour = _colour_gradient[COLOUR_GREY][4];
00301
00302
00303
00304 r.top += 5 + GetCharacterHeight(FS_SMALL) / 2;
00305 r.bottom -= (this->month == 0xFF ? 1 : 3) * GetCharacterHeight(FS_SMALL) + 4;
00306 r.left += 9;
00307 r.right -= 5;
00308
00309
00310 int num_hori_lines = 160 / MIN_GRID_PIXEL_SIZE;
00311
00312 int resize = (r.bottom - r.top - 160) / (2 * MIN_GRID_PIXEL_SIZE);
00313 if (resize > 0) num_hori_lines += resize;
00314
00315 interval = GetValuesInterval(num_hori_lines);
00316
00317 int label_width = GetYLabelWidth(interval, num_hori_lines);
00318
00319 r.left += label_width;
00320
00321 int x_sep = (r.right - r.left) / this->num_vert_lines;
00322 int y_sep = (r.bottom - r.top) / num_hori_lines;
00323
00324
00325
00326 r.right = r.left + x_sep * this->num_vert_lines;
00327 r.bottom = r.top + y_sep * num_hori_lines;
00328
00329 OverflowSafeInt64 interval_size = interval.highest + abs(interval.lowest);
00330
00331 x_axis_offset = (int)((r.bottom - r.top) * (double)interval.highest / (double)interval_size);
00332
00333
00334
00335
00336 x = r.left + x_sep;
00337
00338 for (int i = 0; i < this->num_vert_lines; i++) {
00339 GfxFillRect(x, r.top, x, r.bottom, grid_colour);
00340 x += x_sep;
00341 }
00342
00343
00344 y = r.bottom;
00345
00346 for (int i = 0; i < (num_hori_lines + 1); i++) {
00347 GfxFillRect(r.left - 3, y, r.left - 1, y, GRAPH_AXIS_LINE_COLOUR);
00348 GfxFillRect(r.left, y, r.right, y, grid_colour);
00349 y -= y_sep;
00350 }
00351
00352
00353 GfxFillRect(r.left, r.top, r.left, r.bottom, GRAPH_AXIS_LINE_COLOUR);
00354
00355
00356 y = x_axis_offset + r.top;
00357 GfxFillRect(r.left, y, r.right, y, GRAPH_AXIS_LINE_COLOUR);
00358
00359
00360 if (this->num_on_x_axis == 0) return;
00361
00362 assert(this->num_on_x_axis > 0);
00363 assert(this->num_dataset > 0);
00364
00365
00366 int64 y_label = interval.highest;
00367 int64 y_label_separation = abs(interval.highest - interval.lowest) / num_hori_lines;
00368
00369 y = r.top - GetCharacterHeight(FS_SMALL) / 2;
00370
00371 for (int i = 0; i < (num_hori_lines + 1); i++) {
00372 SetDParam(0, this->format_str_y_axis);
00373 SetDParam(1, y_label);
00374 DrawString(r.left - label_width - 4, r.left - 4, y, STR_GRAPH_Y_LABEL, graph_axis_label_colour, SA_RIGHT);
00375
00376 y_label -= y_label_separation;
00377 y += y_sep;
00378 }
00379
00380
00381 if (this->month != 0xFF) {
00382 x = r.left;
00383 y = r.bottom + 2;
00384 byte month = this->month;
00385 Year year = this->year;
00386 for (int i = 0; i < this->num_on_x_axis; i++) {
00387 SetDParam(0, month + STR_MONTH_ABBREV_JAN);
00388 SetDParam(1, month + STR_MONTH_ABBREV_JAN + 2);
00389 SetDParam(2, year);
00390 DrawStringMultiLine(x, x + x_sep, y, this->height, month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH, graph_axis_label_colour);
00391
00392 month += 3;
00393 if (month >= 12) {
00394 month = 0;
00395 year++;
00396 }
00397 x += x_sep;
00398 }
00399 } else {
00400
00401 x = r.left;
00402 y = r.bottom + 2;
00403 uint16 label = this->x_values_start;
00404
00405 for (int i = 0; i < this->num_on_x_axis; i++) {
00406 SetDParam(0, label);
00407 DrawString(x + 1, x + x_sep - 1, y, STR_GRAPH_Y_LABEL_NUMBER, graph_axis_label_colour, SA_HOR_CENTER);
00408
00409 label += this->x_values_increment;
00410 x += x_sep;
00411 }
00412 }
00413
00414
00415 for (int i = 0; i < this->num_dataset; i++) {
00416 if (!HasBit(this->excluded_data, i)) {
00417
00418 x = r.left + (x_sep / 2);
00419
00420 byte colour = this->colours[i];
00421 uint prev_x = INVALID_DATAPOINT_POS;
00422 uint prev_y = INVALID_DATAPOINT_POS;
00423
00424 for (int j = 0; j < this->num_on_x_axis; j++) {
00425 OverflowSafeInt64 datapoint = this->cost[i][j];
00426
00427 if (datapoint != INVALID_DATAPOINT) {
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 int mult_range = FindLastBit(x_axis_offset) + FindLastBit(abs(datapoint));
00440 int reduce_range = max(mult_range - 31, 0);
00441
00442
00443 if (datapoint < 0) {
00444 datapoint = -(abs(datapoint) >> reduce_range);
00445 } else {
00446 datapoint >>= reduce_range;
00447 }
00448 y = r.top + x_axis_offset - ((r.bottom - r.top) * datapoint) / (interval_size >> reduce_range);
00449
00450
00451 GfxFillRect(x - 1, y - 1, x + 1, y + 1, colour);
00452
00453
00454 if (prev_x != INVALID_DATAPOINT_POS) GfxDrawLine(prev_x, prev_y, x, y, colour);
00455
00456 prev_x = x;
00457 prev_y = y;
00458 } else {
00459 prev_x = INVALID_DATAPOINT_POS;
00460 prev_y = INVALID_DATAPOINT_POS;
00461 }
00462
00463 x += x_sep;
00464 }
00465 }
00466 }
00467 }
00468
00469
00470 BaseGraphWindow(int widget, StringID format_str_y_axis) :
00471 Window(),
00472 format_str_y_axis(format_str_y_axis)
00473 {
00474 SetWindowDirty(WC_GRAPH_LEGEND, 0);
00475 this->num_vert_lines = 24;
00476 this->graph_widget = widget;
00477 }
00478
00479 void InitializeWindow(const WindowDesc *desc, WindowNumber number)
00480 {
00481
00482 this->UpdateStatistics(true);
00483
00484 this->InitNested(desc, number);
00485 }
00486
00487 public:
00488 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00489 {
00490 if (widget != this->graph_widget) return;
00491
00492 uint x_label_width = 0;
00493
00494 if (this->month != 0xFF) {
00495 byte month = this->month;
00496 Year year = this->year;
00497 for (int i = 0; i < this->num_on_x_axis; i++) {
00498 SetDParam(0, month + STR_MONTH_ABBREV_JAN);
00499 SetDParam(1, month + STR_MONTH_ABBREV_JAN + 2);
00500 SetDParam(2, year);
00501 x_label_width = max(x_label_width, GetStringBoundingBox(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH).width);
00502
00503 month += 3;
00504 if (month >= 12) {
00505 month = 0;
00506 year++;
00507 }
00508 }
00509 } else {
00510
00511 SetDParam(0, this->x_values_start + this->num_on_x_axis * this->x_values_increment);
00512 x_label_width = GetStringBoundingBox(STR_GRAPH_Y_LABEL_NUMBER).width;
00513 }
00514
00515 SetDParam(0, this->format_str_y_axis);
00516 SetDParam(1, INT64_MAX);
00517 uint y_label_width = GetStringBoundingBox(STR_GRAPH_Y_LABEL).width;
00518
00519 size->width = max<uint>(size->width, 5 + y_label_width + this->num_on_x_axis * (x_label_width + 5) + 9);
00520 size->height = max<uint>(size->height, 5 + (1 + MIN_GRAPH_NUM_LINES_Y * 2 + (this->month != 0xFF ? 3 : 1)) * FONT_HEIGHT_SMALL + 4);
00521 size->height = max<uint>(size->height, size->width / 3);
00522 }
00523
00524 virtual void DrawWidget(const Rect &r, int widget) const
00525 {
00526 if (widget != this->graph_widget) return;
00527
00528 DrawGraph(r);
00529 }
00530
00531 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00532 {
00533 return INVALID_DATAPOINT;
00534 }
00535
00536 virtual void OnClick(Point pt, int widget, int click_count)
00537 {
00538
00539 if (widget == BGW_KEY_BUTTON) ShowGraphLegend();
00540 }
00541
00542 virtual void OnTick()
00543 {
00544 this->UpdateStatistics(false);
00545 }
00546
00547 virtual void OnInvalidateData(int data)
00548 {
00549 this->UpdateStatistics(true);
00550 }
00551
00556 void UpdateStatistics(bool initialize)
00557 {
00558 uint excluded_companies = _legend_excluded_companies;
00559
00560
00561 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00562 if (!Company::IsValidID(c)) SetBit(excluded_companies, c);
00563 }
00564
00565 byte nums = 0;
00566 const Company *c;
00567 FOR_ALL_COMPANIES(c) {
00568 nums = min(this->num_vert_lines, max(nums, c->num_valid_stat_ent));
00569 }
00570
00571 int mo = (_cur_month / 3 - nums) * 3;
00572 int yr = _cur_year;
00573 while (mo < 0) {
00574 yr--;
00575 mo += 12;
00576 }
00577
00578 if (!initialize && this->excluded_data == excluded_companies && this->num_on_x_axis == nums &&
00579 this->year == yr && this->month == mo) {
00580
00581 return;
00582 }
00583
00584 this->excluded_data = excluded_companies;
00585 this->num_on_x_axis = nums;
00586 this->year = yr;
00587 this->month = mo;
00588
00589 int numd = 0;
00590 for (CompanyID k = COMPANY_FIRST; k < MAX_COMPANIES; k++) {
00591 c = Company::GetIfValid(k);
00592 if (c != NULL) {
00593 this->colours[numd] = _colour_gradient[c->colour][6];
00594 for (int j = this->num_on_x_axis, i = 0; --j >= 0;) {
00595 this->cost[numd][i] = (j >= c->num_valid_stat_ent) ? INVALID_DATAPOINT : GetGraphData(c, j);
00596 i++;
00597 }
00598 }
00599 numd++;
00600 }
00601
00602 this->num_dataset = numd;
00603 }
00604 };
00605
00606
00607
00608
00609
00610
00611 struct OperatingProfitGraphWindow : BaseGraphWindow {
00612 OperatingProfitGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00613 BaseGraphWindow(BGW_GRAPH, STR_JUST_CURRCOMPACT)
00614 {
00615 this->InitializeWindow(desc, window_number);
00616 }
00617
00618 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00619 {
00620 return c->old_economy[j].income + c->old_economy[j].expenses;
00621 }
00622 };
00623
00624 static const NWidgetPart _nested_operating_profit_widgets[] = {
00625 NWidget(NWID_HORIZONTAL),
00626 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00627 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_OPERATING_PROFIT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00628 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, BGW_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00629 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00630 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00631 EndContainer(),
00632 NWidget(WWT_PANEL, COLOUR_GREY, BGW_BACKGROUND),
00633 NWidget(NWID_HORIZONTAL),
00634 NWidget(WWT_EMPTY, COLOUR_GREY, BGW_GRAPH), SetMinimalSize(576, 160), SetFill(1, 1), SetResize(1, 1),
00635 NWidget(NWID_VERTICAL),
00636 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00637 NWidget(WWT_RESIZEBOX, COLOUR_GREY, BGW_RESIZE),
00638 EndContainer(),
00639 EndContainer(),
00640 EndContainer(),
00641 };
00642
00643 static const WindowDesc _operating_profit_desc(
00644 WDP_AUTO, 0, 0,
00645 WC_OPERATING_PROFIT, WC_NONE,
00646 WDF_UNCLICK_BUTTONS,
00647 _nested_operating_profit_widgets, lengthof(_nested_operating_profit_widgets)
00648 );
00649
00650
00651 void ShowOperatingProfitGraph()
00652 {
00653 AllocateWindowDescFront<OperatingProfitGraphWindow>(&_operating_profit_desc, 0);
00654 }
00655
00656
00657
00658
00659
00660
00661 struct IncomeGraphWindow : BaseGraphWindow {
00662 IncomeGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00663 BaseGraphWindow(BGW_GRAPH, STR_JUST_CURRCOMPACT)
00664 {
00665 this->InitializeWindow(desc, window_number);
00666 }
00667
00668 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00669 {
00670 return c->old_economy[j].income;
00671 }
00672 };
00673
00674 static const NWidgetPart _nested_income_graph_widgets[] = {
00675 NWidget(NWID_HORIZONTAL),
00676 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00677 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_INCOME_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00678 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, BGW_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00679 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00680 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00681 EndContainer(),
00682 NWidget(WWT_PANEL, COLOUR_GREY, BGW_BACKGROUND),
00683 NWidget(NWID_HORIZONTAL),
00684 NWidget(WWT_EMPTY, COLOUR_GREY, BGW_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
00685 NWidget(NWID_VERTICAL),
00686 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00687 NWidget(WWT_RESIZEBOX, COLOUR_GREY, BGW_RESIZE),
00688 EndContainer(),
00689 EndContainer(),
00690 EndContainer(),
00691 };
00692
00693
00694 static const WindowDesc _income_graph_desc(
00695 WDP_AUTO, 0, 0,
00696 WC_INCOME_GRAPH, WC_NONE,
00697 WDF_UNCLICK_BUTTONS,
00698 _nested_income_graph_widgets, lengthof(_nested_income_graph_widgets)
00699 );
00700
00701 void ShowIncomeGraph()
00702 {
00703 AllocateWindowDescFront<IncomeGraphWindow>(&_income_graph_desc, 0);
00704 }
00705
00706
00707
00708
00709
00710 struct DeliveredCargoGraphWindow : BaseGraphWindow {
00711 DeliveredCargoGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00712 BaseGraphWindow(BGW_GRAPH, STR_JUST_COMMA)
00713 {
00714 this->InitializeWindow(desc, window_number);
00715 }
00716
00717 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00718 {
00719 return c->old_economy[j].delivered_cargo;
00720 }
00721 };
00722
00723 static const NWidgetPart _nested_delivered_cargo_graph_widgets[] = {
00724 NWidget(NWID_HORIZONTAL),
00725 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00726 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_CARGO_DELIVERED_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00727 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, BGW_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00728 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00729 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00730 EndContainer(),
00731 NWidget(WWT_PANEL, COLOUR_GREY, BGW_BACKGROUND),
00732 NWidget(NWID_HORIZONTAL),
00733 NWidget(WWT_EMPTY, COLOUR_GREY, BGW_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
00734 NWidget(NWID_VERTICAL),
00735 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00736 NWidget(WWT_RESIZEBOX, COLOUR_GREY, BGW_RESIZE),
00737 EndContainer(),
00738 EndContainer(),
00739 EndContainer(),
00740 };
00741
00742 static const WindowDesc _delivered_cargo_graph_desc(
00743 WDP_AUTO, 0, 0,
00744 WC_DELIVERED_CARGO, WC_NONE,
00745 WDF_UNCLICK_BUTTONS,
00746 _nested_delivered_cargo_graph_widgets, lengthof(_nested_delivered_cargo_graph_widgets)
00747 );
00748
00749 void ShowDeliveredCargoGraph()
00750 {
00751 AllocateWindowDescFront<DeliveredCargoGraphWindow>(&_delivered_cargo_graph_desc, 0);
00752 }
00753
00754
00755
00756
00757
00759 enum PerformanceHistoryGraphWidgets {
00760 PHW_KEY,
00761 PHW_DETAILED_PERFORMANCE,
00762 PHW_BACKGROUND,
00763 PHW_GRAPH,
00764 PHW_RESIZE,
00765 };
00766
00767 struct PerformanceHistoryGraphWindow : BaseGraphWindow {
00768 PerformanceHistoryGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00769 BaseGraphWindow(PHW_GRAPH, STR_JUST_COMMA)
00770 {
00771 this->InitializeWindow(desc, window_number);
00772 }
00773
00774 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00775 {
00776 return c->old_economy[j].performance_history;
00777 }
00778
00779 virtual void OnClick(Point pt, int widget, int click_count)
00780 {
00781 if (widget == PHW_DETAILED_PERFORMANCE) ShowPerformanceRatingDetail();
00782 this->BaseGraphWindow::OnClick(pt, widget, click_count);
00783 }
00784 };
00785
00786 static const NWidgetPart _nested_performance_history_widgets[] = {
00787 NWidget(NWID_HORIZONTAL),
00788 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00789 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_COMPANY_PERFORMANCE_RATINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00790 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, PHW_DETAILED_PERFORMANCE), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_PERFORMANCE_DETAIL_KEY, STR_GRAPH_PERFORMANCE_DETAIL_TOOLTIP),
00791 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, PHW_KEY), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00792 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00793 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00794 EndContainer(),
00795 NWidget(WWT_PANEL, COLOUR_GREY, PHW_BACKGROUND),
00796 NWidget(NWID_HORIZONTAL),
00797 NWidget(WWT_EMPTY, COLOUR_GREY, PHW_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
00798 NWidget(NWID_VERTICAL),
00799 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00800 NWidget(WWT_RESIZEBOX, COLOUR_GREY, PHW_RESIZE),
00801 EndContainer(),
00802 EndContainer(),
00803 EndContainer(),
00804 };
00805
00806 static const WindowDesc _performance_history_desc(
00807 WDP_AUTO, 0, 0,
00808 WC_PERFORMANCE_HISTORY, WC_NONE,
00809 WDF_UNCLICK_BUTTONS,
00810 _nested_performance_history_widgets, lengthof(_nested_performance_history_widgets)
00811 );
00812
00813 void ShowPerformanceHistoryGraph()
00814 {
00815 AllocateWindowDescFront<PerformanceHistoryGraphWindow>(&_performance_history_desc, 0);
00816 }
00817
00818
00819
00820
00821
00822 struct CompanyValueGraphWindow : BaseGraphWindow {
00823 CompanyValueGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00824 BaseGraphWindow(BGW_GRAPH, STR_JUST_CURRCOMPACT)
00825 {
00826 this->InitializeWindow(desc, window_number);
00827 }
00828
00829 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00830 {
00831 return c->old_economy[j].company_value;
00832 }
00833 };
00834
00835 static const NWidgetPart _nested_company_value_graph_widgets[] = {
00836 NWidget(NWID_HORIZONTAL),
00837 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00838 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_COMPANY_VALUES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00839 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, BGW_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
00840 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00841 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00842 EndContainer(),
00843 NWidget(WWT_PANEL, COLOUR_GREY, BGW_BACKGROUND),
00844 NWidget(NWID_HORIZONTAL),
00845 NWidget(WWT_EMPTY, COLOUR_GREY, BGW_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
00846 NWidget(NWID_VERTICAL),
00847 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
00848 NWidget(WWT_RESIZEBOX, COLOUR_GREY, BGW_RESIZE),
00849 EndContainer(),
00850 EndContainer(),
00851 EndContainer(),
00852 };
00853
00854 static const WindowDesc _company_value_graph_desc(
00855 WDP_AUTO, 0, 0,
00856 WC_COMPANY_VALUE, WC_NONE,
00857 WDF_UNCLICK_BUTTONS,
00858 _nested_company_value_graph_widgets, lengthof(_nested_company_value_graph_widgets)
00859 );
00860
00861 void ShowCompanyValueGraph()
00862 {
00863 AllocateWindowDescFront<CompanyValueGraphWindow>(&_company_value_graph_desc, 0);
00864 }
00865
00866
00867
00868
00869
00871 enum CargoPaymentRatesWidgets {
00872 CPW_BACKGROUND,
00873 CPW_HEADER,
00874 CPW_GRAPH,
00875 CPW_RESIZE,
00876 CPW_FOOTER,
00877 CPW_ENABLE_CARGOS,
00878 CPW_DISABLE_CARGOS,
00879 CPW_CARGO_FIRST,
00880 };
00881
00882 struct PaymentRatesGraphWindow : BaseGraphWindow {
00883 bool first_init;
00884 PaymentRatesGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00885 BaseGraphWindow(CPW_GRAPH, STR_JUST_CURRCOMPACT)
00886 {
00887 this->first_init = true;
00888 this->num_on_x_axis = 20;
00889 this->num_vert_lines = 20;
00890 this->month = 0xFF;
00891 this->x_values_start = 10;
00892 this->x_values_increment = 10;
00893
00894
00895 this->OnHundredthTick();
00896
00897 this->InitNested(desc, window_number);
00898
00899 this->UpdateLoweredWidgets();
00900 }
00901
00902 virtual void OnInit()
00903 {
00904
00905
00906 if (!this->first_init) {
00907
00908 this->OnHundredthTick();
00909 this->UpdateLoweredWidgets();
00910 }
00911 this->first_init = false;
00912 }
00913
00914 void UpdateExcludedData()
00915 {
00916 this->excluded_data = 0;
00917
00918 int i = 0;
00919 const CargoSpec *cs;
00920 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
00921 if (HasBit(_legend_excluded_cargo, cs->Index())) SetBit(this->excluded_data, i);
00922 i++;
00923 }
00924 }
00925
00926 void UpdateLoweredWidgets()
00927 {
00928 for (int i = 0; i < _sorted_standard_cargo_specs_size; i++) {
00929 this->SetWidgetLoweredState(CPW_CARGO_FIRST + i, !HasBit(this->excluded_data, i));
00930 }
00931 }
00932
00933 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00934 {
00935 if (widget < CPW_CARGO_FIRST) {
00936 BaseGraphWindow::UpdateWidgetSize(widget, size, padding, fill, resize);
00937 return;
00938 }
00939
00940 const CargoSpec *cs = _sorted_cargo_specs[widget - CPW_CARGO_FIRST];
00941 SetDParam(0, cs->name);
00942 Dimension d = GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO);
00943 d.width += 14;
00944 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00945 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00946 *size = maxdim(d, *size);
00947 }
00948
00949 virtual void DrawWidget(const Rect &r, int widget) const
00950 {
00951 if (widget < CPW_CARGO_FIRST) {
00952 BaseGraphWindow::DrawWidget(r, widget);
00953 return;
00954 }
00955
00956 const CargoSpec *cs = _sorted_cargo_specs[widget - CPW_CARGO_FIRST];
00957 bool rtl = _current_text_dir == TD_RTL;
00958
00959
00960
00961
00962
00963 byte clk_dif = this->IsWidgetLowered(widget) ? 1 : 0;
00964 int x = r.left + WD_FRAMERECT_LEFT;
00965 int y = r.top;
00966
00967 int rect_x = clk_dif + (rtl ? r.right - 12 : r.left + WD_FRAMERECT_LEFT);
00968
00969 GfxFillRect(rect_x, y + clk_dif, rect_x + 8, y + 5 + clk_dif, 0);
00970 GfxFillRect(rect_x + 1, y + 1 + clk_dif, rect_x + 7, y + 4 + clk_dif, cs->legend_colour);
00971 SetDParam(0, cs->name);
00972 DrawString(rtl ? r.left : x + 14 + clk_dif, (rtl ? r.right - 14 + clk_dif : r.right), y + clk_dif, STR_GRAPH_CARGO_PAYMENT_CARGO);
00973 }
00974
00975 virtual void OnClick(Point pt, int widget, int click_count)
00976 {
00977 switch (widget) {
00978 case CPW_ENABLE_CARGOS:
00979
00980 _legend_excluded_cargo = 0;
00981 this->excluded_data = 0;
00982 this->UpdateLoweredWidgets();
00983 this->SetDirty();
00984 break;
00985
00986 case CPW_DISABLE_CARGOS: {
00987
00988 int i = 0;
00989 const CargoSpec *cs;
00990 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
00991 SetBit(_legend_excluded_cargo, cs->Index());
00992 SetBit(this->excluded_data, i);
00993 i++;
00994 }
00995 this->UpdateLoweredWidgets();
00996 this->SetDirty();
00997 break;
00998 }
00999
01000 default:
01001 if (widget >= CPW_CARGO_FIRST) {
01002 int i = widget - CPW_CARGO_FIRST;
01003 ToggleBit(_legend_excluded_cargo, _sorted_cargo_specs[i]->Index());
01004 this->ToggleWidgetLoweredState(widget);
01005 this->UpdateExcludedData();
01006 this->SetDirty();
01007 }
01008 break;
01009 }
01010 }
01011
01012 virtual void OnTick()
01013 {
01014
01015 }
01016
01017 virtual void OnInvalidateData(int data)
01018 {
01019 this->OnHundredthTick();
01020 }
01021
01022 virtual void OnHundredthTick()
01023 {
01024 this->UpdateExcludedData();
01025
01026 int i = 0;
01027 const CargoSpec *cs;
01028 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
01029 this->colours[i] = cs->legend_colour;
01030 for (uint j = 0; j != 20; j++) {
01031 this->cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->Index());
01032 }
01033 i++;
01034 }
01035 this->num_dataset = i;
01036 }
01037 };
01038
01040 static NWidgetBase *MakeCargoButtons(int *biggest_index)
01041 {
01042 NWidgetVertical *ver = new NWidgetVertical;
01043
01044 for (int i = 0; i < _sorted_standard_cargo_specs_size; i++) {
01045 NWidgetBackground *leaf = new NWidgetBackground(WWT_PANEL, COLOUR_ORANGE, CPW_CARGO_FIRST + i, NULL);
01046 leaf->tool_tip = STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO;
01047 leaf->SetFill(1, 0);
01048 leaf->SetLowered(true);
01049 ver->Add(leaf);
01050 }
01051 *biggest_index = CPW_CARGO_FIRST + _sorted_standard_cargo_specs_size - 1;
01052 return ver;
01053 }
01054
01055
01056 static const NWidgetPart _nested_cargo_payment_rates_widgets[] = {
01057 NWidget(NWID_HORIZONTAL),
01058 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01059 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01060 NWidget(WWT_SHADEBOX, COLOUR_GREY),
01061 NWidget(WWT_STICKYBOX, COLOUR_GREY),
01062 EndContainer(),
01063 NWidget(WWT_PANEL, COLOUR_GREY, CPW_BACKGROUND), SetMinimalSize(568, 128),
01064 NWidget(NWID_HORIZONTAL),
01065 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01066 NWidget(WWT_TEXT, COLOUR_GREY, CPW_HEADER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_TITLE, STR_NULL),
01067 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01068 EndContainer(),
01069 NWidget(NWID_HORIZONTAL),
01070 NWidget(WWT_EMPTY, COLOUR_GREY, CPW_GRAPH), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
01071 NWidget(NWID_VERTICAL),
01072 NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 0), SetResize(0, 1),
01073 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, CPW_ENABLE_CARGOS), SetDataTip(STR_GRAPH_CARGO_ENABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL), SetFill(1, 0),
01074 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, CPW_DISABLE_CARGOS), SetDataTip(STR_GRAPH_CARGO_DISABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL), SetFill(1, 0),
01075 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
01076 NWidgetFunction(MakeCargoButtons),
01077 NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1), SetResize(0, 1),
01078 EndContainer(),
01079 NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
01080 EndContainer(),
01081 NWidget(NWID_HORIZONTAL),
01082 NWidget(NWID_SPACER), SetMinimalSize(WD_RESIZEBOX_WIDTH, 0), SetFill(1, 0), SetResize(1, 0),
01083 NWidget(WWT_TEXT, COLOUR_GREY, CPW_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_X_LABEL, STR_NULL),
01084 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01085 NWidget(WWT_RESIZEBOX, COLOUR_GREY, CPW_RESIZE),
01086 EndContainer(),
01087 EndContainer(),
01088 };
01089
01090 static const WindowDesc _cargo_payment_rates_desc(
01091 WDP_AUTO, 0, 0,
01092 WC_PAYMENT_RATES, WC_NONE,
01093 WDF_UNCLICK_BUTTONS,
01094 _nested_cargo_payment_rates_widgets, lengthof(_nested_cargo_payment_rates_widgets)
01095 );
01096
01097
01098 void ShowCargoPaymentRates()
01099 {
01100 AllocateWindowDescFront<PaymentRatesGraphWindow>(&_cargo_payment_rates_desc, 0);
01101 }
01102
01103
01104
01105
01106
01108 enum CompanyLeagueWidgets {
01109 CLW_BACKGROUND,
01110 };
01111
01112 static const StringID _performance_titles[] = {
01113 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
01114 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
01115 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
01116 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
01117 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
01118 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
01119 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
01120 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
01121 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
01122 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
01123 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
01124 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
01125 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
01126 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
01127 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT,
01128 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON,
01129 };
01130
01131 static inline StringID GetPerformanceTitleFromValue(uint value)
01132 {
01133 return _performance_titles[minu(value, 1000) >> 6];
01134 }
01135
01136 class CompanyLeagueWindow : public Window {
01137 private:
01138 GUIList<const Company*> companies;
01139 uint ordinal_width;
01140 uint text_width;
01141
01145 void BuildCompanyList()
01146 {
01147 if (!this->companies.NeedRebuild()) return;
01148
01149 this->companies.Clear();
01150
01151 const Company *c;
01152 FOR_ALL_COMPANIES(c) {
01153 *this->companies.Append() = c;
01154 }
01155
01156 this->companies.Compact();
01157 this->companies.RebuildDone();
01158 }
01159
01161 static int CDECL PerformanceSorter(const Company * const *c1, const Company * const *c2)
01162 {
01163 return (*c2)->old_economy[0].performance_history - (*c1)->old_economy[0].performance_history;
01164 }
01165
01166 public:
01167 CompanyLeagueWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
01168 {
01169 this->InitNested(desc, window_number);
01170 this->companies.ForceRebuild();
01171 this->companies.NeedResort();
01172 }
01173
01174 virtual void OnPaint()
01175 {
01176 this->BuildCompanyList();
01177 this->companies.Sort(&PerformanceSorter);
01178
01179 this->DrawWidgets();
01180 }
01181
01182 virtual void DrawWidget(const Rect &r, int widget) const
01183 {
01184 if (widget != CLW_BACKGROUND) return;
01185
01186 uint y = r.top + WD_FRAMERECT_TOP;
01187 int icon_y_offset = 1 + (FONT_HEIGHT_NORMAL - 10) / 2;
01188
01189 bool rtl = _current_text_dir == TD_RTL;
01190 uint ordinal_left = rtl ? r.right - WD_FRAMERECT_LEFT - this->ordinal_width : r.left + WD_FRAMERECT_LEFT;
01191 uint ordinal_right = rtl ? r.right - WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->ordinal_width;
01192 uint icon_left = r.left + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT + (rtl ? this->text_width : this->ordinal_width);
01193 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - WD_FRAMERECT_LEFT - this->text_width;
01194 uint text_right = rtl ? r.left + WD_FRAMERECT_LEFT + this->text_width : r.right - WD_FRAMERECT_LEFT;
01195
01196 for (uint i = 0; i != this->companies.Length(); i++) {
01197 const Company *c = this->companies[i];
01198 DrawString(ordinal_left, ordinal_right, y, i + STR_ORDINAL_NUMBER_1ST, i == 0 ? TC_WHITE : TC_YELLOW);
01199
01200 DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
01201
01202 SetDParam(0, c->index);
01203 SetDParam(1, c->index);
01204 SetDParam(2, GetPerformanceTitleFromValue(c->old_economy[0].performance_history));
01205 DrawString(text_left, text_right, y, STR_COMPANY_LEAGUE_COMPANY_NAME);
01206 y += FONT_HEIGHT_NORMAL;
01207 }
01208 }
01209
01210 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01211 {
01212 if (widget != CLW_BACKGROUND) return;
01213
01214 this->ordinal_width = 0;
01215 for (uint i = 0; i < MAX_COMPANIES; i++) {
01216 this->ordinal_width = max(this->ordinal_width, GetStringBoundingBox(STR_ORDINAL_NUMBER_1ST + i).width);
01217 }
01218 this->ordinal_width += 5;
01219
01220 uint widest_width = 0;
01221 uint widest_title = 0;
01222 for (uint i = 0; i < lengthof(_performance_titles); i++) {
01223 uint width = GetStringBoundingBox(_performance_titles[i]).width;
01224 if (width > widest_width) {
01225 widest_title = i;
01226 widest_width = width;
01227 }
01228 }
01229
01230 const Company *c;
01231 FOR_ALL_COMPANIES(c) {
01232 SetDParam(0, c->index);
01233 SetDParam(1, c->index);
01234 SetDParam(2, _performance_titles[widest_title]);
01235 widest_width = max(widest_width, GetStringBoundingBox(STR_COMPANY_LEAGUE_COMPANY_NAME).width);
01236 }
01237
01238 this->text_width = widest_width + 30;
01239
01240 size->width = WD_FRAMERECT_LEFT + this->ordinal_width + WD_FRAMERECT_RIGHT + 16 + WD_FRAMERECT_LEFT + this->text_width + WD_FRAMERECT_RIGHT;
01241 }
01242
01243
01244 virtual void OnTick()
01245 {
01246 if (this->companies.NeedResort()) {
01247 this->SetDirty();
01248 }
01249 }
01250
01251 virtual void OnInvalidateData(int data)
01252 {
01253 if (data == 0) {
01254 this->companies.ForceRebuild();
01255 } else {
01256 this->companies.ForceResort();
01257 }
01258 }
01259 };
01260
01261 static const NWidgetPart _nested_company_league_widgets[] = {
01262 NWidget(NWID_HORIZONTAL),
01263 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01264 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_LEAGUE_TABLE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01265 NWidget(WWT_SHADEBOX, COLOUR_GREY),
01266 NWidget(WWT_STICKYBOX, COLOUR_GREY),
01267 EndContainer(),
01268 NWidget(WWT_PANEL, COLOUR_GREY, CLW_BACKGROUND), SetMinimalSize(400, 0), SetMinimalTextLines(15, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM),
01269 };
01270
01271 static const WindowDesc _company_league_desc(
01272 WDP_AUTO, 0, 0,
01273 WC_COMPANY_LEAGUE, WC_NONE,
01274 0,
01275 _nested_company_league_widgets, lengthof(_nested_company_league_widgets)
01276 );
01277
01278 void ShowCompanyLeagueTable()
01279 {
01280 AllocateWindowDescFront<CompanyLeagueWindow>(&_company_league_desc, 0);
01281 }
01282
01283
01284
01285
01286
01288 enum PerformanceRatingDetailsWidgets {
01289 PRW_SCORE_FIRST,
01290 PRW_SCORE_LAST = PRW_SCORE_FIRST + (SCORE_END - SCORE_BEGIN) - 1,
01291
01292 PRW_COMPANY_FIRST,
01293 PRW_COMPANY_LAST = PRW_COMPANY_FIRST + MAX_COMPANIES - 1,
01294 };
01295
01296 struct PerformanceRatingDetailWindow : Window {
01297 static CompanyID company;
01298 int timeout;
01299
01300 PerformanceRatingDetailWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
01301 {
01302 this->UpdateCompanyStats();
01303
01304 this->InitNested(desc, window_number);
01305 this->OnInvalidateData(INVALID_COMPANY);
01306 }
01307
01308 void UpdateCompanyStats()
01309 {
01310
01311
01312 Company *c;
01313 FOR_ALL_COMPANIES(c) {
01314 UpdateCompanyRatingAndValue(c, false);
01315 }
01316
01317 this->timeout = DAY_TICKS * 5;
01318 }
01319
01320 uint score_info_left;
01321 uint score_info_right;
01322 uint bar_left;
01323 uint bar_right;
01324 uint bar_width;
01325 uint bar_height;
01326 uint score_detail_left;
01327 uint score_detail_right;
01328
01329 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01330 {
01331 switch (widget) {
01332 case PRW_SCORE_FIRST:
01333 this->bar_height = FONT_HEIGHT_NORMAL + 4;
01334 size->height = this->bar_height + 2 * WD_MATRIX_TOP;
01335
01336 uint score_info_width = 0;
01337 for (uint i = SCORE_BEGIN; i < SCORE_END; i++) {
01338 score_info_width = max(score_info_width, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES + i).width);
01339 }
01340 SetDParam(0, 1000);
01341 score_info_width += GetStringBoundingBox(STR_BLACK_COMMA).width + WD_FRAMERECT_LEFT;
01342
01343 SetDParam(0, 100);
01344 this->bar_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_PERCENT).width + 20;
01345
01346
01347
01348
01349
01350
01351
01352 int max = -(999999999 - 500);
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365 if (_currency->rate < 1000) max /= _currency->rate;
01366 SetDParam(0, max);
01367 SetDParam(1, max);
01368 uint score_detail_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY).width;
01369
01370 size->width = 7 + score_info_width + 5 + this->bar_width + 5 + score_detail_width + 7;
01371 uint left = 7;
01372 uint right = size->width - 7;
01373
01374 bool rtl = _current_text_dir == TD_RTL;
01375 this->score_info_left = rtl ? right - score_info_width : left;
01376 this->score_info_right = rtl ? right : left + score_info_width;
01377
01378 this->score_detail_left = rtl ? left : right - score_detail_width;
01379 this->score_detail_right = rtl ? left + score_detail_width : right;
01380
01381 this->bar_left = left + (rtl ? score_detail_width : score_info_width) + 5;
01382 this->bar_right = this->bar_left + this->bar_width;
01383 break;
01384 }
01385 }
01386
01387 virtual void DrawWidget(const Rect &r, int widget) const
01388 {
01389
01390 if (this->company == INVALID_COMPANY) return;
01391
01392 if (IsInsideMM(widget, PRW_COMPANY_FIRST, PRW_COMPANY_LAST + 1)) {
01393 if (this->IsWidgetDisabled(widget)) return;
01394 CompanyID cid = (CompanyID)(widget - PRW_COMPANY_FIRST);
01395 int offset = (cid == this->company) ? 1 : 0;
01396 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
01397 DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2 + offset, (r.top + r.bottom - sprite_size.height) / 2 + offset);
01398 return;
01399 }
01400
01401 if (!IsInsideMM(widget, PRW_SCORE_FIRST, PRW_SCORE_LAST + 1)) return;
01402
01403 ScoreID score_type = (ScoreID)(widget - PRW_SCORE_FIRST);
01404
01405
01406 int colour_done = _colour_gradient[COLOUR_GREEN][4];
01407 int colour_notdone = _colour_gradient[COLOUR_RED][4];
01408
01409
01410 int val = _score_part[company][score_type];
01411 int needed = _score_info[score_type].needed;
01412 int score = _score_info[score_type].score;
01413
01414
01415 if (score_type == SCORE_TOTAL) {
01416 for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) score += _score_info[i].score;
01417 needed = SCORE_MAX;
01418 }
01419
01420 uint bar_top = r.top + WD_MATRIX_TOP;
01421 uint text_top = bar_top + 2;
01422
01423 DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
01424
01425
01426 SetDParam(0, score);
01427 DrawString(this->score_info_left, this->score_info_right, text_top, STR_BLACK_COMMA, TC_FROMSTRING, SA_RIGHT);
01428
01429
01430 uint x = Clamp(val, 0, needed) * this->bar_width / needed;
01431 bool rtl = _current_text_dir == TD_RTL;
01432 if (rtl) {
01433 x = this->bar_right - x;
01434 } else {
01435 x = this->bar_left + x;
01436 }
01437
01438
01439 if (x != this->bar_left) GfxFillRect(this->bar_left, bar_top, x, bar_top + this->bar_height, rtl ? colour_notdone : colour_done);
01440 if (x != this->bar_right) GfxFillRect(x, bar_top, this->bar_right, bar_top + this->bar_height, rtl ? colour_done : colour_notdone);
01441
01442
01443 SetDParam(0, Clamp(val, 0, needed) * 100 / needed);
01444 DrawString(this->bar_left, this->bar_right, text_top, STR_PERFORMANCE_DETAIL_PERCENT, TC_FROMSTRING, SA_HOR_CENTER);
01445
01446
01447 if (score_type == SCORE_LOAN) val = needed - val;
01448
01449
01450
01451 SetDParam(0, val);
01452 SetDParam(1, needed);
01453 switch (score_type) {
01454 case SCORE_MIN_PROFIT:
01455 case SCORE_MIN_INCOME:
01456 case SCORE_MAX_INCOME:
01457 case SCORE_MONEY:
01458 case SCORE_LOAN:
01459 DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY);
01460 break;
01461 default:
01462 DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_INT);
01463 }
01464 }
01465
01466 virtual void OnClick(Point pt, int widget, int click_count)
01467 {
01468
01469 if (IsInsideMM(widget, PRW_COMPANY_FIRST, PRW_COMPANY_LAST + 1)) {
01470
01471 if (!this->IsWidgetDisabled(widget)) {
01472 this->RaiseWidget(this->company + PRW_COMPANY_FIRST);
01473 this->company = (CompanyID)(widget - PRW_COMPANY_FIRST);
01474 this->LowerWidget(this->company + PRW_COMPANY_FIRST);
01475 this->SetDirty();
01476 }
01477 }
01478 }
01479
01480 virtual void OnTick()
01481 {
01482 if (_pause_mode != PM_UNPAUSED) return;
01483
01484
01485 if (--this->timeout == 0) {
01486 this->UpdateCompanyStats();
01487 this->SetDirty();
01488 }
01489 }
01490
01495 virtual void OnInvalidateData(int data)
01496 {
01497
01498 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
01499 this->SetWidgetDisabledState(i + PRW_COMPANY_FIRST, !Company::IsValidID(i));
01500 }
01501
01502
01503 if (this->company != INVALID_COMPANY && !Company::IsValidID(this->company)) {
01504
01505 this->RaiseWidget(this->company + PRW_COMPANY_FIRST);
01506 this->company = INVALID_COMPANY;
01507 }
01508
01509 if (this->company == INVALID_COMPANY) {
01510 const Company *c;
01511 FOR_ALL_COMPANIES(c) {
01512 this->company = c->index;
01513 break;
01514 }
01515 }
01516
01517
01518 this->LowerWidget(this->company + PRW_COMPANY_FIRST);
01519 }
01520 };
01521
01522 CompanyID PerformanceRatingDetailWindow::company = INVALID_COMPANY;
01523
01530 static NWidgetBase *MakePerformanceDetailPanels(int *biggest_index)
01531 {
01532 const StringID performance_tips[] = {
01533 STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP,
01534 STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP,
01535 STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP,
01536 STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP,
01537 STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP,
01538 STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP,
01539 STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP,
01540 STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP,
01541 STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP,
01542 STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP,
01543 };
01544
01545 assert_compile(lengthof(performance_tips) == SCORE_END - SCORE_BEGIN);
01546
01547 NWidgetVertical *vert = new NWidgetVertical(NC_EQUALSIZE);
01548 for (int widnum = PRW_SCORE_FIRST; widnum <= PRW_SCORE_LAST; widnum++) {
01549 NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
01550 panel->SetFill(1, 1);
01551 panel->SetDataTip(0x0, performance_tips[widnum - PRW_SCORE_FIRST]);
01552 vert->Add(panel);
01553 }
01554 *biggest_index = PRW_SCORE_LAST;
01555 return vert;
01556 }
01557
01559 NWidgetBase *MakeCompanyButtonRowsGraphGUI(int *biggest_index)
01560 {
01561 return MakeCompanyButtonRows(biggest_index, PRW_COMPANY_FIRST, PRW_COMPANY_LAST, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP);
01562 }
01563
01564 static const NWidgetPart _nested_performance_rating_detail_widgets[] = {
01565 NWidget(NWID_HORIZONTAL),
01566 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01567 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PERFORMANCE_DETAIL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01568 NWidget(WWT_SHADEBOX, COLOUR_GREY),
01569 NWidget(WWT_STICKYBOX, COLOUR_GREY),
01570 EndContainer(),
01571 NWidget(WWT_PANEL, COLOUR_GREY),
01572 NWidgetFunction(MakeCompanyButtonRowsGraphGUI), SetPadding(0, 1, 1, 2),
01573 EndContainer(),
01574 NWidgetFunction(MakePerformanceDetailPanels),
01575 };
01576
01577 static const WindowDesc _performance_rating_detail_desc(
01578 WDP_AUTO, 0, 0,
01579 WC_PERFORMANCE_DETAIL, WC_NONE,
01580 0,
01581 _nested_performance_rating_detail_widgets, lengthof(_nested_performance_rating_detail_widgets)
01582 );
01583
01584 void ShowPerformanceRatingDetail()
01585 {
01586 AllocateWindowDescFront<PerformanceRatingDetailWindow>(&_performance_rating_detail_desc, 0);
01587 }