00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "gui.h"
00008 #include "window_gui.h"
00009 #include "company_base.h"
00010 #include "company_gui.h"
00011 #include "economy_func.h"
00012 #include "variables.h"
00013 #include "cargotype.h"
00014 #include "strings_func.h"
00015 #include "core/alloc_func.hpp"
00016 #include "window_func.h"
00017 #include "date_func.h"
00018 #include "gfx_func.h"
00019 #include "sortlist_type.h"
00020
00021 #include "table/strings.h"
00022 #include "table/sprites.h"
00023
00024
00025 static uint _legend_excluded_companies;
00026 static uint _legend_excluded_cargo;
00027
00028
00029 static const OverflowSafeInt64 INVALID_DATAPOINT(INT64_MAX);
00030 static const uint INVALID_DATAPOINT_POS = UINT_MAX;
00031
00032
00033
00034
00035
00036 struct GraphLegendWindow : Window {
00037 GraphLegendWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
00038 {
00039 for (uint i = 3; i < this->widget_count; i++) {
00040 if (!HasBit(_legend_excluded_companies, i - 3)) this->LowerWidget(i);
00041 }
00042
00043 this->FindWindowPlacementAndResize(desc);
00044 }
00045
00046 virtual void OnPaint()
00047 {
00048 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00049 if (IsValidCompanyID(c)) continue;
00050
00051 SetBit(_legend_excluded_companies, c);
00052 this->RaiseWidget(c + 3);
00053 }
00054
00055 this->DrawWidgets();
00056
00057 const Company *c;
00058 FOR_ALL_COMPANIES(c) {
00059 DrawCompanyIcon(c->index, 4, 18 + c->index * 12);
00060
00061 SetDParam(0, c->index);
00062 SetDParam(1, c->index);
00063 DrawString(21, 17 + c->index * 12, STR_7021, HasBit(_legend_excluded_companies, c->index) ? TC_BLACK : TC_WHITE);
00064 }
00065 }
00066
00067 virtual void OnClick(Point pt, int widget)
00068 {
00069 if (!IsInsideMM(widget, 3, 11)) return;
00070
00071 ToggleBit(_legend_excluded_companies, widget - 3);
00072 this->ToggleWidgetLoweredState(widget);
00073 this->SetDirty();
00074 InvalidateWindow(WC_INCOME_GRAPH, 0);
00075 InvalidateWindow(WC_OPERATING_PROFIT, 0);
00076 InvalidateWindow(WC_DELIVERED_CARGO, 0);
00077 InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
00078 InvalidateWindow(WC_COMPANY_VALUE, 0);
00079 }
00080 };
00081
00082 static const Widget _graph_legend_widgets[] = {
00083 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00084 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 249, 0, 13, STR_704E_KEY_TO_COMPANY_GRAPHS, STR_018C_WINDOW_TITLE_DRAG_THIS},
00085 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 249, 14, 113, 0x0, STR_NULL},
00086 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 2, 247, 16, 27, 0x0, STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
00087 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 2, 247, 28, 39, 0x0, STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
00088 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 2, 247, 40, 51, 0x0, STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
00089 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 2, 247, 52, 63, 0x0, STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
00090 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 2, 247, 64, 75, 0x0, STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
00091 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 2, 247, 76, 87, 0x0, STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
00092 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 2, 247, 88, 99, 0x0, STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
00093 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 2, 247, 100, 111, 0x0, STR_704F_CLICK_HERE_TO_TOGGLE_COMPANY},
00094 { WIDGETS_END},
00095 };
00096
00097 static const WindowDesc _graph_legend_desc = {
00098 WDP_AUTO, WDP_AUTO, 250, 114, 250, 114,
00099 WC_GRAPH_LEGEND, WC_NONE,
00100 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
00101 _graph_legend_widgets,
00102 };
00103
00104 static void ShowGraphLegend()
00105 {
00106 AllocateWindowDescFront<GraphLegendWindow>(&_graph_legend_desc, 0);
00107 }
00108
00109
00110
00111
00112
00113 struct BaseGraphWindow : Window {
00114 protected:
00115 enum {
00116 GRAPH_MAX_DATASETS = 32,
00117 GRAPH_AXIS_LINE_COLOUR = 215,
00118
00119 GRAPH_X_POSITION_BEGINNING = 44,
00120 GRAPH_X_POSITION_SEPARATION = 22,
00121
00122 GRAPH_NUM_LINES_Y = 9,
00123
00124
00125
00126 };
00127
00128 uint excluded_data;
00129 byte num_dataset;
00130 byte num_on_x_axis;
00131 bool has_negative_values;
00132 byte num_vert_lines;
00133 static const TextColour graph_axis_label_colour = TC_BLACK;
00134
00135
00136
00137 byte month;
00138 Year year;
00139
00140
00141
00142 uint16 x_values_start;
00143 uint16 x_values_increment;
00144
00145 int gd_left, gd_top;
00146 uint gd_height;
00147 StringID format_str_y_axis;
00148 byte colors[GRAPH_MAX_DATASETS];
00149 OverflowSafeInt64 cost[GRAPH_MAX_DATASETS][24];
00150
00151 void DrawGraph() const
00152 {
00153 uint x, y;
00154 OverflowSafeInt64 highest_value;
00155 int x_axis_offset;
00156
00157
00158
00159 assert(GRAPH_MAX_DATASETS >= (int)NUM_CARGO && GRAPH_MAX_DATASETS >= (int)MAX_COMPANIES);
00160 assert(this->num_vert_lines > 0);
00161
00162 byte grid_colour = _colour_gradient[COLOUR_GREY][4];
00163
00164
00165 int bottom = this->gd_top + this->gd_height - 1;
00166 int right = this->gd_left + GRAPH_X_POSITION_BEGINNING + this->num_vert_lines * GRAPH_X_POSITION_SEPARATION - 1;
00167
00168
00169
00170
00171 x = this->gd_left + GRAPH_X_POSITION_BEGINNING + GRAPH_X_POSITION_SEPARATION;
00172
00173 for (int i = 0; i < this->num_vert_lines; i++) {
00174 GfxFillRect(x, this->gd_top, x, bottom, grid_colour);
00175 x += GRAPH_X_POSITION_SEPARATION;
00176 }
00177
00178
00179 x = this->gd_left + GRAPH_X_POSITION_BEGINNING;
00180 y = this->gd_height + this->gd_top;
00181
00182 for (int i = 0; i < GRAPH_NUM_LINES_Y; i++) {
00183 GfxFillRect(x, y, right, y, grid_colour);
00184 y -= (this->gd_height / (GRAPH_NUM_LINES_Y - 1));
00185 }
00186
00187
00188 GfxFillRect(x, this->gd_top, x, bottom, GRAPH_AXIS_LINE_COLOUR);
00189
00190
00191 x_axis_offset = this->gd_height;
00192
00193
00194 if (this->has_negative_values) x_axis_offset /= 2;
00195
00196
00197 y = x_axis_offset + this->gd_top;
00198 GfxFillRect(x, y, right, y, GRAPH_AXIS_LINE_COLOUR);
00199
00200
00201 if (this->num_on_x_axis == 0)
00202 return;
00203
00204 assert(this->num_on_x_axis > 0);
00205 assert(this->num_dataset > 0);
00206
00207
00208
00209
00210
00211 highest_value = x_axis_offset * 2;
00212
00213 for (int i = 0; i < this->num_dataset; i++) {
00214 if (!HasBit(this->excluded_data, i)) {
00215 for (int j = 0; j < this->num_on_x_axis; j++) {
00216 OverflowSafeInt64 datapoint = this->cost[i][j];
00217
00218 if (datapoint != INVALID_DATAPOINT) {
00219
00220
00221
00222 highest_value = max(highest_value, abs(datapoint));
00223 }
00224 }
00225 }
00226 }
00227
00228
00229
00230 int round_val = highest_value % (GRAPH_NUM_LINES_Y - 1);
00231 if (round_val != 0) highest_value += (GRAPH_NUM_LINES_Y - 1 - round_val);
00232
00233
00234 int64 y_label = highest_value;
00235 int64 y_label_separation = highest_value / (GRAPH_NUM_LINES_Y - 1);
00236
00237
00238
00239 if (this->has_negative_values) y_label_separation *= 2;
00240
00241 x = this->gd_left + GRAPH_X_POSITION_BEGINNING + 1;
00242 y = this->gd_top - 3;
00243
00244 for (int i = 0; i < GRAPH_NUM_LINES_Y; i++) {
00245 SetDParam(0, this->format_str_y_axis);
00246 SetDParam(1, y_label);
00247 DrawStringRightAligned(x, y, STR_0170, graph_axis_label_colour);
00248
00249 y_label -= y_label_separation;
00250 y += (this->gd_height / (GRAPH_NUM_LINES_Y - 1));
00251 }
00252
00253
00254 if (this->month != 0xFF) {
00255 x = this->gd_left + GRAPH_X_POSITION_BEGINNING;
00256 y = this->gd_top + this->gd_height + 1;
00257 byte month = this->month;
00258 Year year = this->year;
00259 for (int i = 0; i < this->num_on_x_axis; i++) {
00260 SetDParam(0, month + STR_0162_JAN);
00261 SetDParam(1, month + STR_0162_JAN + 2);
00262 SetDParam(2, year);
00263 DrawString(x, y, month == 0 ? STR_016F : STR_016E, graph_axis_label_colour);
00264
00265 month += 3;
00266 if (month >= 12) {
00267 month = 0;
00268 year++;
00269 }
00270 x += GRAPH_X_POSITION_SEPARATION;
00271 }
00272 } else {
00273
00274 x = this->gd_left + GRAPH_X_POSITION_BEGINNING + (GRAPH_X_POSITION_SEPARATION / 2) + 1;
00275 y = this->gd_top + this->gd_height + 1;
00276 uint16 label = this->x_values_start;
00277
00278 for (int i = 0; i < this->num_on_x_axis; i++) {
00279 SetDParam(0, label);
00280 DrawStringCentered(x, y, STR_01CB, graph_axis_label_colour);
00281
00282 label += this->x_values_increment;
00283 x += GRAPH_X_POSITION_SEPARATION;
00284 }
00285 }
00286
00287
00288 for (int i = 0; i < this->num_dataset; i++) {
00289 if (!HasBit(this->excluded_data, i)) {
00290
00291 x = this->gd_left + GRAPH_X_POSITION_BEGINNING + (GRAPH_X_POSITION_SEPARATION / 2);
00292
00293 byte color = this->colors[i];
00294 uint prev_x = INVALID_DATAPOINT_POS;
00295 uint prev_y = INVALID_DATAPOINT_POS;
00296
00297 for (int j = 0; j < this->num_on_x_axis; j++) {
00298 OverflowSafeInt64 datapoint = this->cost[i][j];
00299
00300 if (datapoint != INVALID_DATAPOINT) {
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 int mult_range = FindLastBit(x_axis_offset) + FindLastBit(abs(datapoint));
00313 int reduce_range = max(mult_range - 31, 0);
00314
00315
00316 if (datapoint < 0) {
00317 datapoint = -(abs(datapoint) >> reduce_range);
00318 } else {
00319 datapoint >>= reduce_range;
00320 }
00321
00322 y = this->gd_top + x_axis_offset - (x_axis_offset * datapoint) / (highest_value >> reduce_range);
00323
00324
00325 GfxFillRect(x - 1, y - 1, x + 1, y + 1, color);
00326
00327
00328 if (prev_x != INVALID_DATAPOINT_POS) GfxDrawLine(prev_x, prev_y, x, y, color);
00329
00330 prev_x = x;
00331 prev_y = y;
00332 } else {
00333 prev_x = INVALID_DATAPOINT_POS;
00334 prev_y = INVALID_DATAPOINT_POS;
00335 }
00336
00337 x += GRAPH_X_POSITION_SEPARATION;
00338 }
00339 }
00340 }
00341 }
00342
00343
00344 BaseGraphWindow(const WindowDesc *desc, WindowNumber window_number, int left,
00345 int top, int height, bool has_negative_values, StringID format_str_y_axis) :
00346 Window(desc, window_number), has_negative_values(has_negative_values),
00347 gd_left(left), gd_top(top), gd_height(height), format_str_y_axis(format_str_y_axis)
00348 {
00349 InvalidateWindow(WC_GRAPH_LEGEND, 0);
00350 }
00351
00352 public:
00353 virtual void OnPaint()
00354 {
00355 this->DrawWidgets();
00356
00357 uint excluded_companies = _legend_excluded_companies;
00358
00359
00360 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00361 if (!IsValidCompanyID(c)) SetBit(excluded_companies, c);
00362 }
00363 this->excluded_data = excluded_companies;
00364 this->num_vert_lines = 24;
00365
00366 byte nums = 0;
00367 const Company *c;
00368 FOR_ALL_COMPANIES(c) {
00369 nums = max(nums, c->num_valid_stat_ent);
00370 }
00371 this->num_on_x_axis = min(nums, 24);
00372
00373 int mo = (_cur_month / 3 - nums) * 3;
00374 int yr = _cur_year;
00375 while (mo < 0) {
00376 yr--;
00377 mo += 12;
00378 }
00379
00380 this->year = yr;
00381 this->month = mo;
00382
00383 int numd = 0;
00384 for (CompanyID k = COMPANY_FIRST; k < MAX_COMPANIES; k++) {
00385 if (IsValidCompanyID(k)) {
00386 c = GetCompany(k);
00387 this->colors[numd] = _colour_gradient[c->colour][6];
00388 for (int j = this->num_on_x_axis, i = 0; --j >= 0;) {
00389 this->cost[numd][i] = (j >= c->num_valid_stat_ent) ? INVALID_DATAPOINT : GetGraphData(c, j);
00390 i++;
00391 }
00392 }
00393 numd++;
00394 }
00395
00396 this->num_dataset = numd;
00397
00398 this->DrawGraph();
00399 }
00400
00401 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00402 {
00403 return INVALID_DATAPOINT;
00404 }
00405
00406 virtual void OnClick(Point pt, int widget)
00407 {
00408
00409 if (widget == 2) ShowGraphLegend();
00410 }
00411 };
00412
00413
00414
00415
00416
00417 struct OperatingProfitGraphWindow : BaseGraphWindow {
00418 OperatingProfitGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00419 BaseGraphWindow(desc, window_number, 2, 18, 136, true, STR_CURRCOMPACT)
00420 {
00421 this->FindWindowPlacementAndResize(desc);
00422 }
00423
00424 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00425 {
00426 return c->old_economy[j].income + c->old_economy[j].expenses;
00427 }
00428 };
00429
00430 static const Widget _operating_profit_widgets[] = {
00431 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00432 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 525, 0, 13, STR_7025_OPERATING_PROFIT_GRAPH, STR_018C_WINDOW_TITLE_DRAG_THIS},
00433 { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_GREY, 526, 575, 0, 13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
00434 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 575, 14, 173, 0x0, STR_NULL},
00435 { WIDGETS_END},
00436 };
00437
00438 static const WindowDesc _operating_profit_desc = {
00439 WDP_AUTO, WDP_AUTO, 576, 174, 576, 174,
00440 WC_OPERATING_PROFIT, WC_NONE,
00441 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
00442 _operating_profit_widgets,
00443 };
00444
00445
00446 void ShowOperatingProfitGraph()
00447 {
00448 AllocateWindowDescFront<OperatingProfitGraphWindow>(&_operating_profit_desc, 0);
00449 }
00450
00451
00452
00453
00454
00455
00456 struct IncomeGraphWindow : BaseGraphWindow {
00457 IncomeGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00458 BaseGraphWindow(desc, window_number, 2, 18, 104, false, STR_CURRCOMPACT)
00459 {
00460 this->FindWindowPlacementAndResize(desc);
00461 }
00462
00463 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00464 {
00465 return c->old_economy[j].income;
00466 }
00467 };
00468
00469 static const Widget _income_graph_widgets[] = {
00470 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00471 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 525, 0, 13, STR_7022_INCOME_GRAPH, STR_018C_WINDOW_TITLE_DRAG_THIS},
00472 { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_GREY, 526, 575, 0, 13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
00473 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 575, 14, 141, 0x0, STR_NULL},
00474 { WIDGETS_END},
00475 };
00476
00477 static const WindowDesc _income_graph_desc = {
00478 WDP_AUTO, WDP_AUTO, 576, 142, 576, 142,
00479 WC_INCOME_GRAPH, WC_NONE,
00480 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
00481 _income_graph_widgets,
00482 };
00483
00484 void ShowIncomeGraph()
00485 {
00486 AllocateWindowDescFront<IncomeGraphWindow>(&_income_graph_desc, 0);
00487 }
00488
00489
00490
00491
00492
00493 struct DeliveredCargoGraphWindow : BaseGraphWindow {
00494 DeliveredCargoGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00495 BaseGraphWindow(desc, window_number, 2, 18, 104, false, STR_7024)
00496 {
00497 this->FindWindowPlacementAndResize(desc);
00498 }
00499
00500 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00501 {
00502 return c->old_economy[j].delivered_cargo;
00503 }
00504 };
00505
00506 static const Widget _delivered_cargo_graph_widgets[] = {
00507 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00508 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 525, 0, 13, STR_7050_UNITS_OF_CARGO_DELIVERED, STR_018C_WINDOW_TITLE_DRAG_THIS},
00509 { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_GREY, 526, 575, 0, 13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
00510 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 575, 14, 141, 0x0, STR_NULL},
00511 { WIDGETS_END},
00512 };
00513
00514 static const WindowDesc _delivered_cargo_graph_desc = {
00515 WDP_AUTO, WDP_AUTO, 576, 142, 576, 142,
00516 WC_DELIVERED_CARGO, WC_NONE,
00517 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
00518 _delivered_cargo_graph_widgets,
00519 };
00520
00521 void ShowDeliveredCargoGraph()
00522 {
00523 AllocateWindowDescFront<DeliveredCargoGraphWindow>(&_delivered_cargo_graph_desc, 0);
00524 }
00525
00526
00527
00528
00529
00530 struct PerformanceHistoryGraphWindow : BaseGraphWindow {
00531 PerformanceHistoryGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00532 BaseGraphWindow(desc, window_number, 2, 18, 200, false, STR_7024)
00533 {
00534 this->FindWindowPlacementAndResize(desc);
00535 }
00536
00537 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00538 {
00539 return c->old_economy[j].performance_history;
00540 }
00541
00542 virtual void OnClick(Point pt, int widget)
00543 {
00544 if (widget == 3) ShowPerformanceRatingDetail();
00545 this->BaseGraphWindow::OnClick(pt, widget);
00546 }
00547 };
00548
00549 static const Widget _performance_history_widgets[] = {
00550 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00551 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 475, 0, 13, STR_7051_COMPANY_PERFORMANCE_RATINGS, STR_018C_WINDOW_TITLE_DRAG_THIS},
00552 { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_GREY, 526, 575, 0, 13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
00553 { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_GREY, 476, 525, 0, 13, STR_PERFORMANCE_DETAIL_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
00554 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 575, 14, 237, 0x0, STR_NULL},
00555 { WIDGETS_END},
00556 };
00557
00558 static const WindowDesc _performance_history_desc = {
00559 WDP_AUTO, WDP_AUTO, 576, 238, 576, 238,
00560 WC_PERFORMANCE_HISTORY, WC_NONE,
00561 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
00562 _performance_history_widgets,
00563 };
00564
00565 void ShowPerformanceHistoryGraph()
00566 {
00567 AllocateWindowDescFront<PerformanceHistoryGraphWindow>(&_performance_history_desc, 0);
00568 }
00569
00570
00571
00572
00573
00574 struct CompanyValueGraphWindow : BaseGraphWindow {
00575 CompanyValueGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00576 BaseGraphWindow(desc, window_number, 2, 18, 200, false, STR_CURRCOMPACT)
00577 {
00578 this->FindWindowPlacementAndResize(desc);
00579 }
00580
00581 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
00582 {
00583 return c->old_economy[j].company_value;
00584 }
00585 };
00586
00587 static const Widget _company_value_graph_widgets[] = {
00588 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00589 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 525, 0, 13, STR_7052_COMPANY_VALUES, STR_018C_WINDOW_TITLE_DRAG_THIS},
00590 { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_GREY, 526, 575, 0, 13, STR_704C_KEY, STR_704D_SHOW_KEY_TO_GRAPHS},
00591 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 575, 14, 237, 0x0, STR_NULL},
00592 { WIDGETS_END},
00593 };
00594
00595 static const WindowDesc _company_value_graph_desc = {
00596 WDP_AUTO, WDP_AUTO, 576, 238, 576, 238,
00597 WC_COMPANY_VALUE, WC_NONE,
00598 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
00599 _company_value_graph_widgets,
00600 };
00601
00602 void ShowCompanyValueGraph()
00603 {
00604 AllocateWindowDescFront<CompanyValueGraphWindow>(&_company_value_graph_desc, 0);
00605 }
00606
00607
00608
00609
00610
00611 struct PaymentRatesGraphWindow : BaseGraphWindow {
00612 PaymentRatesGraphWindow(const WindowDesc *desc, WindowNumber window_number) :
00613 BaseGraphWindow(desc, window_number, 2, 24, 200, false, STR_CURRCOMPACT)
00614 {
00615 uint num_active = 0;
00616 for (CargoID c = 0; c < NUM_CARGO; c++) {
00617 if (GetCargo(c)->IsValid()) num_active++;
00618 }
00619
00620
00621 ResizeWindow(this, 0, max(num_active, 12U) * 8);
00622
00623
00624 this->widget_count += num_active;
00625 this->widget = ReallocT(this->widget, this->widget_count + 1);
00626 this->widget[this->widget_count].type = WWT_LAST;
00627
00628
00629 for (uint i = 0; i != num_active; i++) {
00630 Widget *wi = &this->widget[3 + i];
00631 wi->type = WWT_PANEL;
00632 wi->display_flags = RESIZE_NONE;
00633 wi->color = COLOUR_ORANGE;
00634 wi->left = 493;
00635 wi->right = 562;
00636 wi->top = 24 + i * 8;
00637 wi->bottom = wi->top + 7;
00638 wi->data = 0;
00639 wi->tooltips = STR_7064_TOGGLE_GRAPH_FOR_CARGO;
00640
00641 if (!HasBit(_legend_excluded_cargo, i)) this->LowerWidget(i + 3);
00642 }
00643
00644 this->SetDirty();
00645
00646 this->gd_height = this->height - 38;
00647 this->num_on_x_axis = 20;
00648 this->num_vert_lines = 20;
00649 this->month = 0xFF;
00650 this->x_values_start = 10;
00651 this->x_values_increment = 10;
00652
00653 this->FindWindowPlacementAndResize(desc);
00654 }
00655
00656 virtual void OnPaint()
00657 {
00658 this->DrawWidgets();
00659
00660 this->excluded_data = _legend_excluded_cargo;
00661
00662 int x = 495;
00663 int y = 24;
00664
00665 uint i = 0;
00666 for (CargoID c = 0; c < NUM_CARGO; c++) {
00667 const CargoSpec *cs = GetCargo(c);
00668 if (!cs->IsValid()) continue;
00669
00670
00671
00672
00673 if (i + 3 < this->widget_count) {
00674
00675
00676
00677
00678 byte clk_dif = this->IsWidgetLowered(i + 3) ? 1 : 0;
00679
00680 GfxFillRect(x + clk_dif, y + clk_dif, x + 8 + clk_dif, y + 5 + clk_dif, 0);
00681 GfxFillRect(x + 1 + clk_dif, y + 1 + clk_dif, x + 7 + clk_dif, y + 4 + clk_dif, cs->legend_colour);
00682 SetDParam(0, cs->name);
00683 DrawString(x + 14 + clk_dif, y + clk_dif, STR_7065, TC_FROMSTRING);
00684 y += 8;
00685 }
00686
00687 this->colors[i] = cs->legend_colour;
00688 for (uint j = 0; j != 20; j++) {
00689 this->cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, c);
00690 }
00691
00692 i++;
00693 }
00694 this->num_dataset = i;
00695
00696 this->DrawGraph();
00697
00698 DrawString(2 + 46, 24 + this->gd_height + 7, STR_7062_DAYS_IN_TRANSIT, TC_FROMSTRING);
00699 DrawString(2 + 84, 24 - 9, STR_7063_PAYMENT_FOR_DELIVERING, TC_FROMSTRING);
00700 }
00701
00702 virtual void OnClick(Point pt, int widget)
00703 {
00704 if (widget >= 3) {
00705 ToggleBit(_legend_excluded_cargo, widget - 3);
00706 this->ToggleWidgetLoweredState(widget);
00707 this->SetDirty();
00708 }
00709 }
00710 };
00711
00712 static const Widget _cargo_payment_rates_widgets[] = {
00713 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00714 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 567, 0, 13, STR_7061_CARGO_PAYMENT_RATES, STR_018C_WINDOW_TITLE_DRAG_THIS},
00715 { WWT_PANEL, RESIZE_BOTTOM, COLOUR_GREY, 0, 567, 14, 45, 0x0, STR_NULL},
00716 { WIDGETS_END},
00717 };
00718
00719 static const WindowDesc _cargo_payment_rates_desc = {
00720 WDP_AUTO, WDP_AUTO, 568, 46, 568, 46,
00721 WC_PAYMENT_RATES, WC_NONE,
00722 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
00723 _cargo_payment_rates_widgets,
00724 };
00725
00726
00727 void ShowCargoPaymentRates()
00728 {
00729 AllocateWindowDescFront<PaymentRatesGraphWindow>(&_cargo_payment_rates_desc, 0);
00730 }
00731
00732
00733
00734
00735
00736 static const StringID _performance_titles[] = {
00737 STR_7066_ENGINEER,
00738 STR_7066_ENGINEER,
00739 STR_7067_TRAFFIC_MANAGER,
00740 STR_7067_TRAFFIC_MANAGER,
00741 STR_7068_TRANSPORT_COORDINATOR,
00742 STR_7068_TRANSPORT_COORDINATOR,
00743 STR_7069_ROUTE_SUPERVISOR,
00744 STR_7069_ROUTE_SUPERVISOR,
00745 STR_706A_DIRECTOR,
00746 STR_706A_DIRECTOR,
00747 STR_706B_CHIEF_EXECUTIVE,
00748 STR_706B_CHIEF_EXECUTIVE,
00749 STR_706C_CHAIRMAN,
00750 STR_706C_CHAIRMAN,
00751 STR_706D_PRESIDENT,
00752 STR_706E_TYCOON,
00753 };
00754
00755 static inline StringID GetPerformanceTitleFromValue(uint value)
00756 {
00757 return _performance_titles[minu(value, 1000) >> 6];
00758 }
00759
00760 class CompanyLeagueWindow : public Window {
00761 private:
00762 GUIList<const Company*> companies;
00763
00767 void BuildCompanyList()
00768 {
00769 if (!this->companies.NeedRebuild()) return;
00770
00771 this->companies.Clear();
00772
00773 const Company *c;
00774 FOR_ALL_COMPANIES(c) {
00775 *this->companies.Append() = c;
00776 }
00777
00778 this->companies.Compact();
00779 this->companies.RebuildDone();
00780 }
00781
00783 static int CDECL PerformanceSorter(const Company* const *c1, const Company* const *c2)
00784 {
00785 return (*c2)->old_economy[1].performance_history - (*c1)->old_economy[1].performance_history;
00786 }
00787
00788 public:
00789 CompanyLeagueWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
00790 {
00791 this->companies.ForceRebuild();
00792 this->companies.NeedResort();
00793
00794 this->FindWindowPlacementAndResize(desc);
00795 }
00796
00797 virtual void OnPaint()
00798 {
00799 this->BuildCompanyList();
00800 this->companies.Sort(&PerformanceSorter);
00801
00802 this->DrawWidgets();
00803
00804 for (uint i = 0; i != this->companies.Length(); i++) {
00805 const Company *c = this->companies[i];
00806 SetDParam(0, i + STR_01AC_1ST);
00807 SetDParam(1, c->index);
00808 SetDParam(2, c->index);
00809 SetDParam(3, GetPerformanceTitleFromValue(c->old_economy[1].performance_history));
00810
00811 DrawString(2, 15 + i * 10, i == 0 ? STR_7054 : STR_7055, TC_FROMSTRING);
00812 DrawCompanyIcon(c->index, 27, 16 + i * 10);
00813 }
00814 }
00815
00816 virtual void OnTick()
00817 {
00818 if (this->companies.NeedResort()) {
00819 this->SetDirty();
00820 }
00821 }
00822
00823 virtual void OnInvalidateData(int data)
00824 {
00825 if (data == 0) {
00826 this->companies.ForceRebuild();
00827 } else {
00828 this->companies.ForceResort();
00829 }
00830 }
00831 };
00832
00833
00834 static const Widget _company_league_widgets[] = {
00835 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00836 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 387, 0, 13, STR_7053_COMPANY_LEAGUE_TABLE, STR_018C_WINDOW_TITLE_DRAG_THIS},
00837 { WWT_STICKYBOX, RESIZE_NONE, COLOUR_GREY, 388, 399, 0, 13, STR_NULL, STR_STICKY_BUTTON},
00838 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 399, 14, 96, 0x0, STR_NULL},
00839 { WIDGETS_END},
00840 };
00841
00842 static const WindowDesc _company_league_desc = {
00843 WDP_AUTO, WDP_AUTO, 400, 97, 400, 97,
00844 WC_COMPANY_LEAGUE, WC_NONE,
00845 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON,
00846 _company_league_widgets,
00847 };
00848
00849 void ShowCompanyLeagueTable()
00850 {
00851 AllocateWindowDescFront<CompanyLeagueWindow>(&_company_league_desc, 0);
00852 }
00853
00854
00855
00856
00857
00858 struct PerformanceRatingDetailWindow : Window {
00859 private:
00860 enum PerformanteRatingWidgets {
00861 PRW_COMPANY_FIRST = 13,
00862 PRW_COMPANY_LAST = 20,
00863 };
00864
00865 public:
00866 static CompanyID company;
00867 int timeout;
00868
00869 PerformanceRatingDetailWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
00870 {
00871
00872 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00873 this->SetWidgetDisabledState(i + PRW_COMPANY_FIRST, !IsValidCompanyID(i));
00874 }
00875
00876 this->UpdateCompanyStats();
00877
00878 if (company != INVALID_COMPANY) this->LowerWidget(company + PRW_COMPANY_FIRST);
00879
00880 this->FindWindowPlacementAndResize(desc);
00881 }
00882
00883 void UpdateCompanyStats()
00884 {
00885
00886
00887 Company *c;
00888 FOR_ALL_COMPANIES(c) {
00889 UpdateCompanyRatingAndValue(c, false);
00890 }
00891
00892 this->timeout = DAY_TICKS * 5;
00893
00894 }
00895
00896 virtual void OnPaint()
00897 {
00898 byte x;
00899 uint16 y = 14;
00900 int total_score = 0;
00901 int color_done, color_notdone;
00902
00903
00904 this->DrawWidgets();
00905
00906
00907 if (company == INVALID_COMPANY || !IsValidCompanyID(company)) {
00908 if (company != INVALID_COMPANY) {
00909
00910 this->RaiseWidget(company + PRW_COMPANY_FIRST);
00911 this->DisableWidget(company + PRW_COMPANY_FIRST);
00912 this->SetDirty();
00913
00914 company = INVALID_COMPANY;
00915 }
00916
00917 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00918 if (IsValidCompanyID(i)) {
00919
00920 this->LowerWidget(i + PRW_COMPANY_FIRST);
00921 this->SetDirty();
00922
00923 company = i;
00924 break;
00925 }
00926 }
00927 }
00928
00929
00930 if (company == INVALID_COMPANY) return;
00931
00932
00933 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00934 if (!IsValidCompanyID(i)) {
00935
00936 if (!this->IsWidgetDisabled(i + PRW_COMPANY_FIRST)) {
00937
00938 this->DisableWidget(i + PRW_COMPANY_FIRST);
00939
00940
00941 this->SetDirty();
00942 }
00943 continue;
00944 }
00945
00946
00947 if (this->IsWidgetDisabled(i + PRW_COMPANY_FIRST)) {
00948
00949 this->EnableWidget(i + PRW_COMPANY_FIRST);
00950
00951 this->SetDirty();
00952 }
00953
00954 x = (i == company) ? 1 : 0;
00955 DrawCompanyIcon(i, i * 37 + 13 + x, 16 + x);
00956 }
00957
00958
00959 color_done = _colour_gradient[COLOUR_GREEN][4];
00960 color_notdone = _colour_gradient[COLOUR_RED][4];
00961
00962
00963 for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) {
00964 int val = _score_part[company][i];
00965 int needed = _score_info[i].needed;
00966 int score = _score_info[i].score;
00967
00968 y += 20;
00969
00970 if (i == SCORE_TOTAL) {
00971 needed = total_score;
00972 score = SCORE_MAX;
00973 } else {
00974 total_score += score;
00975 }
00976
00977 DrawString(7, y, STR_PERFORMANCE_DETAIL_VEHICLES + i, TC_FROMSTRING);
00978
00979
00980 SetDParam(0, score);
00981 DrawStringRightAligned(107, y, SET_PERFORMANCE_DETAIL_INT, TC_FROMSTRING);
00982
00983
00984 x = Clamp(val, 0, needed) * 50 / needed;
00985
00986
00987 if (val < 0 && i == SCORE_LOAN) x = 0;
00988
00989
00990 if (x != 0) GfxFillRect(112, y - 2, 112 + x, y + 10, color_done);
00991 if (x != 50) GfxFillRect(112 + x, y - 2, 112 + 50, y + 10, color_notdone);
00992
00993
00994 x = Clamp(val, 0, needed) * 100 / needed;
00995
00996
00997 if (val < 0 && i == SCORE_LOAN) x = 0;
00998
00999
01000 SetDParam(0, x);
01001 DrawStringCentered(137, y, STR_PERFORMANCE_DETAIL_PERCENT, TC_FROMSTRING);
01002
01003
01004 if (i == SCORE_LOAN) val = needed - val;
01005
01006
01007
01008 SetDParam(0, val);
01009 SetDParam(1, needed);
01010 switch (i) {
01011 case SCORE_MIN_PROFIT:
01012 case SCORE_MIN_INCOME:
01013 case SCORE_MAX_INCOME:
01014 case SCORE_MONEY:
01015 case SCORE_LOAN:
01016 DrawString(167, y, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY, TC_FROMSTRING);
01017 break;
01018 default:
01019 DrawString(167, y, STR_PERFORMANCE_DETAIL_AMOUNT_INT, TC_FROMSTRING);
01020 }
01021 }
01022 }
01023
01024 virtual void OnClick(Point pt, int widget)
01025 {
01026
01027 if (IsInsideMM(widget, PRW_COMPANY_FIRST, PRW_COMPANY_LAST + 1)) {
01028
01029 if (!this->IsWidgetDisabled(widget)) {
01030 this->RaiseWidget(company + PRW_COMPANY_FIRST);
01031 company = (CompanyID)(widget - PRW_COMPANY_FIRST);
01032 this->LowerWidget(company + PRW_COMPANY_FIRST);
01033 this->SetDirty();
01034 }
01035 }
01036 }
01037
01038 virtual void OnTick()
01039 {
01040 if (_pause_game != 0) return;
01041
01042
01043 if (--this->timeout == 0) {
01044 this->UpdateCompanyStats();
01045 this->SetDirty();
01046 }
01047 }
01048 };
01049
01050 CompanyID PerformanceRatingDetailWindow::company = INVALID_COMPANY;
01051
01052
01053 static const Widget _performance_rating_detail_widgets[] = {
01054 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
01055 { WWT_CAPTION, RESIZE_NONE, COLOUR_GREY, 11, 298, 0, 13, STR_PERFORMANCE_DETAIL, STR_018C_WINDOW_TITLE_DRAG_THIS},
01056 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 298, 14, 27, 0x0, STR_NULL},
01057
01058 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 298, 28, 47, 0x0, STR_PERFORMANCE_DETAIL_VEHICLES_TIP},
01059 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 298, 48, 67, 0x0, STR_PERFORMANCE_DETAIL_STATIONS_TIP},
01060 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 298, 68, 87, 0x0, STR_PERFORMANCE_DETAIL_MIN_PROFIT_TIP},
01061 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 298, 88, 107, 0x0, STR_PERFORMANCE_DETAIL_MIN_INCOME_TIP},
01062 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 298, 108, 127, 0x0, STR_PERFORMANCE_DETAIL_MAX_INCOME_TIP},
01063 { WWT_PANEL, RESIZE_NONE, COLOUR_GREY, 0, 298, 128, 147,