OpenTTD: src/graph_gui.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

graph_gui.cpp
Go to the documentation of this file.
1 /* $Id: graph_gui.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "stdafx.h"
13 #include "graph_gui.h"
14 #include "window_gui.h"
15 #include "company_base.h"
16 #include "company_gui.h"
17 #include "economy_func.h"
18 #include "cargotype.h"
19 #include "strings_func.h"
20 #include "window_func.h"
21 #include "date_func.h"
22 #include "gfx_func.h"
23 #include "sortlist_type.h"
24 #include "core/geometry_func.hpp"
25 #include "currency.h"
26
27 #include "widgets/graph_widget.h"
28
29 #include "table/strings.h"
30 #include "table/sprites.h"
31 #include <math.h>
32
33 #include "safeguards.h"
34
35 /* Bitmasks of company and cargo indices that shouldn't be drawn. */
36 static uint _legend_excluded_companies;
37 static uint _legend_excluded_cargo;
38
39 /* Apparently these don't play well with enums. */
40 static const OverflowSafeInt64 INVALID_DATAPOINT(INT64_MAX); // Value used for a datapoint that shouldn't be drawn.
41 static const uint INVALID_DATAPOINT_POS = UINT_MAX; // Used to determine if the previous point was drawn.
42
43 /****************/
44 /* GRAPH LEGEND */
45 /****************/
46
47 struct GraphLegendWindow : Window {
48 GraphLegendWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
49 {
50 this->InitNested(window_number);
51
52 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
53 if (!HasBit(_legend_excluded_companies, c)) this->LowerWidget(c + WID_GL_FIRST_COMPANY);
54
55 this->OnInvalidateData(c);
56 }
57 }
58
59 virtual void DrawWidget(const Rect &r, int widget) const
60 {
61 if (!IsInsideMM(widget, WID_GL_FIRST_COMPANY, MAX_COMPANIES + WID_GL_FIRST_COMPANY)) return;
62
63 CompanyID cid = (CompanyID)(widget - WID_GL_FIRST_COMPANY);
64
65 if (!Company::IsValidID(cid)) return;
66
67 bool rtl = _current_text_dir == TD_RTL;
68
69 Dimension d = GetSpriteSize(SPR_COMPANY_ICON);
70 DrawCompanyIcon(cid, rtl ? r.right - d.width - 2 : r.left + 2, r.top + (r.bottom - r.top - d.height) / 2);
71
72 SetDParam(0, cid);
73 SetDParam(1, cid);
74 DrawString(r.left + (rtl ? (uint)WD_FRAMERECT_LEFT : (d.width + 4)), r.right - (rtl ? (d.width + 4) : (uint)WD_FRAMERECT_RIGHT), r.top + (r.bottom - r.top + 1 - FONT_HEIGHT_NORMAL) / 2, STR_COMPANY_NAME_COMPANY_NUM, HasBit(_legend_excluded_companies, cid) ? TC_BLACK : TC_WHITE);
75 }
76
77 virtual void OnClick(Point pt, int widget, int click_count)
78 {
79 if (!IsInsideMM(widget, WID_GL_FIRST_COMPANY, MAX_COMPANIES + WID_GL_FIRST_COMPANY)) return;
80
81 ToggleBit(_legend_excluded_companies, widget - WID_GL_FIRST_COMPANY);
82 this->ToggleWidgetLoweredState(widget);
83 this->SetDirty();
84 InvalidateWindowData(WC_INCOME_GRAPH, 0);
85 InvalidateWindowData(WC_OPERATING_PROFIT, 0);
86 InvalidateWindowData(WC_DELIVERED_CARGO, 0);
87 InvalidateWindowData(WC_PERFORMANCE_HISTORY, 0);
88 InvalidateWindowData(WC_COMPANY_VALUE, 0);
89 }
90
96 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
97 {
98 if (!gui_scope) return;
99 if (Company::IsValidID(data)) return;
100
101 SetBit(_legend_excluded_companies, data);
102 this->RaiseWidget(data + WID_GL_FIRST_COMPANY);
103 }
104 };
105
112 static NWidgetBase *MakeNWidgetCompanyLines(int *biggest_index)
113 {
114 NWidgetVertical *vert = new NWidgetVertical();
115 uint line_height = max<uint>(GetSpriteSize(SPR_COMPANY_ICON).height, FONT_HEIGHT_NORMAL) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
116
117 for (int widnum = WID_GL_FIRST_COMPANY; widnum <= WID_GL_LAST_COMPANY; widnum++) {
118 NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
119 panel->SetMinimalSize(246, line_height);
120 panel->SetFill(1, 0);
121 panel->SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION_TOOLTIP);
122 vert->Add(panel);
123 }
124 *biggest_index = WID_GL_LAST_COMPANY;
125 return vert;
126 }
127
128 static const NWidgetPart _nested_graph_legend_widgets[] = {
129 NWidget(NWID_HORIZONTAL),
130 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
131 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_KEY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
132 NWidget(WWT_SHADEBOX, COLOUR_GREY),
133 NWidget(WWT_STICKYBOX, COLOUR_GREY),
134 EndContainer(),
135 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_BACKGROUND),
136 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
137 NWidget(NWID_HORIZONTAL),
138 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
139 NWidgetFunction(MakeNWidgetCompanyLines),
140 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
141 EndContainer(),
142 EndContainer(),
143 };
144
145 static WindowDesc _graph_legend_desc(
146 WDP_AUTO, "graph_legend", 0, 0,
147 WC_GRAPH_LEGEND, WC_NONE,
148 0,
149 _nested_graph_legend_widgets, lengthof(_nested_graph_legend_widgets)
150 );
151
152 static void ShowGraphLegend()
153 {
154 AllocateWindowDescFront<GraphLegendWindow>(&_graph_legend_desc, 0);
155 }
156
158 struct ValuesInterval {
159 OverflowSafeInt64 highest;
160 OverflowSafeInt64 lowest;
161 };
162
163 /******************/
164 /* BASE OF GRAPHS */
165 /*****************/
166
167 struct BaseGraphWindow : Window {
168 protected:
169 static const int GRAPH_MAX_DATASETS = 32;
170 static const int GRAPH_AXIS_LINE_COLOUR = PC_BLACK;
171 static const int GRAPH_NUM_MONTHS = 24;
172
173 static const int MIN_GRAPH_NUM_LINES_Y = 9;
174 static const int MIN_GRID_PIXEL_SIZE = 20;
175
176 uint excluded_data;
177 byte num_dataset;
178 byte num_on_x_axis;
179 byte num_vert_lines;
180 static const TextColour graph_axis_label_colour = TC_BLACK;
181
182 /* The starting month and year that values are plotted against. If month is
183 * 0xFF, use x_values_start and x_values_increment below instead. */
184 byte month;
185 Year year;
186
187 /* These values are used if the graph is being plotted against values
188 * rather than the dates specified by month and year. */
189 uint16 x_values_start;
190 uint16 x_values_increment;
191
192 int graph_widget;
193 StringID format_str_y_axis;
194 byte colours[GRAPH_MAX_DATASETS];
195 OverflowSafeInt64 cost[GRAPH_MAX_DATASETS][GRAPH_NUM_MONTHS];
196
203 ValuesInterval GetValuesInterval(int num_hori_lines) const
204 {
205 assert(num_hori_lines > 0);
206
207 ValuesInterval current_interval;
208 current_interval.highest = INT64_MIN;
209 current_interval.lowest = INT64_MAX;
210
211 for (int i = 0; i < this->num_dataset; i++) {
212 if (HasBit(this->excluded_data, i)) continue;
213 for (int j = 0; j < this->num_on_x_axis; j++) {
214 OverflowSafeInt64 datapoint = this->cost[i][j];
215
216 if (datapoint != INVALID_DATAPOINT) {
217 current_interval.highest = max(current_interval.highest, datapoint);
218 current_interval.lowest = min(current_interval.lowest, datapoint);
219 }
220 }
221 }
222
223 /* Prevent showing values too close to the graph limits. */
224 current_interval.highest = (11 * current_interval.highest) / 10;
225 current_interval.lowest = (11 * current_interval.lowest) / 10;
226
227 /* Always include zero in the shown range. */
228 double abs_lower = (current_interval.lowest > 0) ? 0 : (double)abs(current_interval.lowest);
229 double abs_higher = (current_interval.highest < 0) ? 0 : (double)current_interval.highest;
230
231 int num_pos_grids;
232 int64 grid_size;
233
234 if (abs_lower != 0 || abs_higher != 0) {
235 /* The number of grids to reserve for the positive part is: */
236 num_pos_grids = (int)floor(0.5 + num_hori_lines * abs_higher / (abs_higher + abs_lower));
237
238 /* If there are any positive or negative values, force that they have at least one grid. */
239 if (num_pos_grids == 0 && abs_higher != 0) num_pos_grids++;
240 if (num_pos_grids == num_hori_lines && abs_lower != 0) num_pos_grids--;
241
242 /* Get the required grid size for each side and use the maximum one. */
243 int64 grid_size_higher = (abs_higher > 0) ? ((int64)abs_higher + num_pos_grids - 1) / num_pos_grids : 0;
244 int64 grid_size_lower = (abs_lower > 0) ? ((int64)abs_lower + num_hori_lines - num_pos_grids - 1) / (num_hori_lines - num_pos_grids) : 0;
245 grid_size = max(grid_size_higher, grid_size_lower);
246 } else {
247 /* If both values are zero, show an empty graph. */
248 num_pos_grids = num_hori_lines / 2;
249 grid_size = 1;
250 }
251
252 current_interval.highest = num_pos_grids * grid_size;
253 current_interval.lowest = -(num_hori_lines - num_pos_grids) * grid_size;
254 return current_interval;
255 }
256
262 uint GetYLabelWidth(ValuesInterval current_interval, int num_hori_lines) const
263 {
264 /* draw text strings on the y axis */
265 int64 y_label = current_interval.highest;
266 int64 y_label_separation = (current_interval.highest - current_interval.lowest) / num_hori_lines;
267
268 uint max_width = 0;
269
270 for (int i = 0; i < (num_hori_lines + 1); i++) {
271 SetDParam(0, this->format_str_y_axis);
272 SetDParam(1, y_label);
273 Dimension d = GetStringBoundingBox(STR_GRAPH_Y_LABEL);
274 if (d.width > max_width) max_width = d.width;
275
276 y_label -= y_label_separation;
277 }
278
279 return max_width;
280 }
281
286 void DrawGraph(Rect r) const
287 {
288 uint x, y;
289 ValuesInterval interval;
290 int x_axis_offset;
291
292 /* the colours and cost array of GraphDrawer must accommodate
293 * both values for cargo and companies. So if any are higher, quit */
294 assert_compile(GRAPH_MAX_DATASETS >= (int)NUM_CARGO && GRAPH_MAX_DATASETS >= (int)MAX_COMPANIES);
295 assert(this->num_vert_lines > 0);
296
297 byte grid_colour = _colour_gradient[COLOUR_GREY][4];
298
299 /* Rect r will be adjusted to contain just the graph, with labels being
300 * placed outside the area. */
301 r.top += 5 + GetCharacterHeight(FS_SMALL) / 2;
302 r.bottom -= (this->month == 0xFF ? 1 : 3) * GetCharacterHeight(FS_SMALL) + 4;
303 r.left += 9;
304 r.right -= 5;
305
306 /* Initial number of horizontal lines. */
307 int num_hori_lines = 160 / MIN_GRID_PIXEL_SIZE;
308 /* For the rest of the height, the number of horizontal lines will increase more slowly. */
309 int resize = (r.bottom - r.top - 160) / (2 * MIN_GRID_PIXEL_SIZE);
310 if (resize > 0) num_hori_lines += resize;
311
312 interval = GetValuesInterval(num_hori_lines);
313
314 int label_width = GetYLabelWidth(interval, num_hori_lines);
315
316 r.left += label_width;
317
318 int x_sep = (r.right - r.left) / this->num_vert_lines;
319 int y_sep = (r.bottom - r.top) / num_hori_lines;
320
321 /* Redetermine right and bottom edge of graph to fit with the integer
322 * separation values. */
323 r.right = r.left + x_sep * this->num_vert_lines;
324 r.bottom = r.top + y_sep * num_hori_lines;
325
326 OverflowSafeInt64 interval_size = interval.highest + abs(interval.lowest);
327 /* Where to draw the X axis. Use floating point to avoid overflowing and results of zero. */
328 x_axis_offset = (int)((r.bottom - r.top) * (double)interval.highest / (double)interval_size);
329
330 /* Draw the vertical grid lines. */
331
332 /* Don't draw the first line, as that's where the axis will be. */
333 x = r.left + x_sep;
334
335 for (int i = 0; i < this->num_vert_lines; i++) {
336 GfxFillRect(x, r.top, x, r.bottom, grid_colour);
337 x += x_sep;
338 }
339
340 /* Draw the horizontal grid lines. */
341 y = r.bottom;
342
343 for (int i = 0; i < (num_hori_lines + 1); i++) {
344 GfxFillRect(r.left - 3, y, r.left - 1, y, GRAPH_AXIS_LINE_COLOUR);
345 GfxFillRect(r.left, y, r.right, y, grid_colour);
346 y -= y_sep;
347 }
348
349 /* Draw the y axis. */
350 GfxFillRect(r.left, r.top, r.left, r.bottom, GRAPH_AXIS_LINE_COLOUR);
351
352 /* Draw the x axis. */
353 y = x_axis_offset + r.top;
354 GfxFillRect(r.left, y, r.right, y, GRAPH_AXIS_LINE_COLOUR);
355
356 /* Find the largest value that will be drawn. */
357 if (this->num_on_x_axis == 0) return;
358
359 assert(this->num_on_x_axis > 0);
360 assert(this->num_dataset > 0);
361
362 /* draw text strings on the y axis */
363 int64 y_label = interval.highest;
364 int64 y_label_separation = abs(interval.highest - interval.lowest) / num_hori_lines;
365
366 y = r.top - GetCharacterHeight(FS_SMALL) / 2;
367
368 for (int i = 0; i < (num_hori_lines + 1); i++) {
369 SetDParam(0, this->format_str_y_axis);
370 SetDParam(1, y_label);
371 DrawString(r.left - label_width - 4, r.left - 4, y, STR_GRAPH_Y_LABEL, graph_axis_label_colour, SA_RIGHT);
372
373 y_label -= y_label_separation;
374 y += y_sep;
375 }
376
377 /* draw strings on the x axis */
378 if (this->month != 0xFF) {
379 x = r.left;
380 y = r.bottom + 2;
381 byte month = this->month;
382 Year year = this->year;
383 for (int i = 0; i < this->num_on_x_axis; i++) {
384 SetDParam(0, month + STR_MONTH_ABBREV_JAN);
385 SetDParam(1, month + STR_MONTH_ABBREV_JAN + 2);
386 SetDParam(2, year);
387 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);
388
389 month += 3;
390 if (month >= 12) {
391 month = 0;
392 year++;
393 }
394 x += x_sep;
395 }
396 } else {
397 /* Draw the label under the data point rather than on the grid line. */
398 x = r.left;
399 y = r.bottom + 2;
400 uint16 label = this->x_values_start;
401
402 for (int i = 0; i < this->num_on_x_axis; i++) {
403 SetDParam(0, label);
404 DrawString(x + 1, x + x_sep - 1, y, STR_GRAPH_Y_LABEL_NUMBER, graph_axis_label_colour, SA_HOR_CENTER);
405
406 label += this->x_values_increment;
407 x += x_sep;
408 }
409 }
410
411 /* draw lines and dots */
412 uint linewidth = _settings_client.gui.graph_line_thickness;
413 uint pointoffs1 = (linewidth + 1) / 2;
414 uint pointoffs2 = linewidth + 1 - pointoffs1;
415 for (int i = 0; i < this->num_dataset; i++) {
416 if (!HasBit(this->excluded_data, i)) {
417 /* Centre the dot between the grid lines. */
418 x = r.left + (x_sep / 2);
419
420 byte colour = this->colours[i];
421 uint prev_x = INVALID_DATAPOINT_POS;
422 uint prev_y = INVALID_DATAPOINT_POS;
423
424 for (int j = 0; j < this->num_on_x_axis; j++) {
425 OverflowSafeInt64 datapoint = this->cost[i][j];
426
427 if (datapoint != INVALID_DATAPOINT) {
428 /*
429 * Check whether we need to reduce the 'accuracy' of the
430 * datapoint value and the highest value to split overflows.
431 * And when 'drawing' 'one million' or 'one million and one'
432 * there is no significant difference, so the least
433 * significant bits can just be removed.
434 *
435 * If there are more bits needed than would fit in a 32 bits
436 * integer, so at about 31 bits because of the sign bit, the
437 * least significant bits are removed.
438 */
439 int mult_range = FindLastBit(x_axis_offset) + FindLastBit(abs(datapoint));
440 int reduce_range = max(mult_range - 31, 0);
441
442 /* Handle negative values differently (don't shift sign) */
443 if (datapoint < 0) {
444 datapoint = -(abs(datapoint) >> reduce_range);
445 } else {
446 datapoint >>= reduce_range;
447 }
448 y = r.top + x_axis_offset - ((r.bottom - r.top) * datapoint) / (interval_size >> reduce_range);
449
450 /* Draw the point. */
451 GfxFillRect(x - pointoffs1, y - pointoffs1, x + pointoffs2, y + pointoffs2, colour);
452
453 /* Draw the line connected to the previous point. */
454 if (prev_x != INVALID_DATAPOINT_POS) GfxDrawLine(prev_x, prev_y, x, y, colour, linewidth);
455
456 prev_x = x;
457 prev_y = y;
458 } else {
459 prev_x = INVALID_DATAPOINT_POS;
460 prev_y = INVALID_DATAPOINT_POS;
461 }
462
463 x += x_sep;
464 }
465 }
466 }
467 }
468
469
470 BaseGraphWindow(WindowDesc *desc, int widget, StringID format_str_y_axis) :
471 Window(desc),
472 format_str_y_axis(format_str_y_axis)
473 {
474 SetWindowDirty(WC_GRAPH_LEGEND, 0);
475 this->num_vert_lines = 24;
476 this->graph_widget = widget;
477 }
478
479 void InitializeWindow(WindowNumber number)
480 {
481 /* Initialise the dataset */
482 this->UpdateStatistics(true);
483
484 this->InitNested(number);
485 }
486
487 public:
488 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
489 {
490 if (widget != this->graph_widget) return;
491
492 uint x_label_width = 0;
493
494 if (this->month != 0xFF) {
495 byte month = this->month;
496 Year year = this->year;
497 for (int i = 0; i < this->num_on_x_axis; i++) {
498 SetDParam(0, month + STR_MONTH_ABBREV_JAN);
499 SetDParam(1, month + STR_MONTH_ABBREV_JAN + 2);
500 SetDParam(2, year);
501 x_label_width = max(x_label_width, GetStringBoundingBox(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH).width);
502
503 month += 3;
504 if (month >= 12) {
505 month = 0;
506 year++;
507 }
508 }
509 } else {
510 /* Draw the label under the data point rather than on the grid line. */
511 SetDParamMaxValue(0, this->x_values_start + this->num_on_x_axis * this->x_values_increment, 0, FS_SMALL);
512 x_label_width = GetStringBoundingBox(STR_GRAPH_Y_LABEL_NUMBER).width;
513 }
514
515 SetDParam(0, this->format_str_y_axis);
516 SetDParam(1, INT64_MAX);
517 uint y_label_width = GetStringBoundingBox(STR_GRAPH_Y_LABEL).width;
518
519 size->width = max<uint>(size->width, 5 + y_label_width + this->num_on_x_axis * (x_label_width + 5) + 9);
520 size->height = max<uint>(size->height, 5 + (1 + MIN_GRAPH_NUM_LINES_Y * 2 + (this->month != 0xFF ? 3 : 1)) * FONT_HEIGHT_SMALL + 4);
521 size->height = max<uint>(size->height, size->width / 3);
522 }
523
524 virtual void DrawWidget(const Rect &r, int widget) const
525 {
526 if (widget != this->graph_widget) return;
527
528 DrawGraph(r);
529 }
530
531 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
532 {
533 return INVALID_DATAPOINT;
534 }
535
536 virtual void OnClick(Point pt, int widget, int click_count)
537 {
538 /* Clicked on legend? */
539 if (widget == WID_CV_KEY_BUTTON) ShowGraphLegend();
540 }
541
542 virtual void OnTick()
543 {
544 this->UpdateStatistics(false);
545 }
546
552 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
553 {
554 if (!gui_scope) return;
555 this->UpdateStatistics(true);
556 }
557
562 void UpdateStatistics(bool initialize)
563 {
564 uint excluded_companies = _legend_excluded_companies;
565
566 /* Exclude the companies which aren't valid */
567 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
568 if (!Company::IsValidID(c)) SetBit(excluded_companies, c);
569 }
570
571 byte nums = 0;
572 const Company *c;
573 FOR_ALL_COMPANIES(c) {
574 nums = min(this->num_vert_lines, max(nums, c->num_valid_stat_ent));
575 }
576
577 int mo = (_cur_month / 3 - nums) * 3;
578 int yr = _cur_year;
579 while (mo < 0) {
580 yr--;
581 mo += 12;
582 }
583
584 if (!initialize && this->excluded_data == excluded_companies && this->num_on_x_axis == nums &&
585 this->year == yr && this->month == mo) {
586 /* There's no reason to get new stats */
587 return;
588 }
589
590 this->excluded_data = excluded_companies;
591 this->num_on_x_axis = nums;
592 this->year = yr;
593 this->month = mo;
594
595 int numd = 0;
596 for (CompanyID k = COMPANY_FIRST; k < MAX_COMPANIES; k++) {
597 c = Company::GetIfValid(k);
598 if (c != NULL) {
599 this->colours[numd] = _colour_gradient[c->colour][6];
600 for (int j = this->num_on_x_axis, i = 0; --j >= 0;) {
601 this->cost[numd][i] = (j >= c->num_valid_stat_ent) ? INVALID_DATAPOINT : GetGraphData(c, j);
602 i++;
603 }
604 }
605 numd++;
606 }
607
608 this->num_dataset = numd;
609 }
610 };
611
612
613 /********************/
614 /* OPERATING PROFIT */
615 /********************/
616
617 struct OperatingProfitGraphWindow : BaseGraphWindow {
618 OperatingProfitGraphWindow(WindowDesc *desc, WindowNumber window_number) :
619 BaseGraphWindow(desc, WID_CV_GRAPH, STR_JUST_CURRENCY_SHORT)
620 {
621 this->InitializeWindow(window_number);
622 }
623
624 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
625 {
626 return c->old_economy[j].income + c->old_economy[j].expenses;
627 }
628 };
629
630 static const NWidgetPart _nested_operating_profit_widgets[] = {
631 NWidget(NWID_HORIZONTAL),
632 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
633 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_OPERATING_PROFIT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
634 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CV_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
635 NWidget(WWT_SHADEBOX, COLOUR_GREY),
636 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
637 NWidget(WWT_STICKYBOX, COLOUR_GREY),
638 EndContainer(),
639 NWidget(WWT_PANEL, COLOUR_GREY, WID_CV_BACKGROUND),
640 NWidget(NWID_HORIZONTAL),
641 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CV_GRAPH), SetMinimalSize(576, 160), SetFill(1, 1), SetResize(1, 1),
642 NWidget(NWID_VERTICAL),
643 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
644 NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CV_RESIZE),
645 EndContainer(),
646 EndContainer(),
647 EndContainer(),
648 };
649
650 static WindowDesc _operating_profit_desc(
651 WDP_AUTO, "graph_operating_profit", 0, 0,
652 WC_OPERATING_PROFIT, WC_NONE,
653 0,
654 _nested_operating_profit_widgets, lengthof(_nested_operating_profit_widgets)
655 );
656
657
658 void ShowOperatingProfitGraph()
659 {
660 AllocateWindowDescFront<OperatingProfitGraphWindow>(&_operating_profit_desc, 0);
661 }
662
663
664 /****************/
665 /* INCOME GRAPH */
666 /****************/
667
668 struct IncomeGraphWindow : BaseGraphWindow {
669 IncomeGraphWindow(WindowDesc *desc, WindowNumber window_number) :
670 BaseGraphWindow(desc, WID_CV_GRAPH, STR_JUST_CURRENCY_SHORT)
671 {
672 this->InitializeWindow(window_number);
673 }
674
675 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
676 {
677 return c->old_economy[j].income;
678 }
679 };
680
681 static const NWidgetPart _nested_income_graph_widgets[] = {
682 NWidget(NWID_HORIZONTAL),
683 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
684 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_INCOME_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
685 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CV_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
686 NWidget(WWT_SHADEBOX, COLOUR_GREY),
687 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
688 NWidget(WWT_STICKYBOX, COLOUR_GREY),
689 EndContainer(),
690 NWidget(WWT_PANEL, COLOUR_GREY, WID_CV_BACKGROUND),
691 NWidget(NWID_HORIZONTAL),
692 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CV_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
693 NWidget(NWID_VERTICAL),
694 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
695 NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CV_RESIZE),
696 EndContainer(),
697 EndContainer(),
698 EndContainer(),
699 };
700
701 static WindowDesc _income_graph_desc(
702 WDP_AUTO, "graph_income", 0, 0,
703 WC_INCOME_GRAPH, WC_NONE,
704 0,
705 _nested_income_graph_widgets, lengthof(_nested_income_graph_widgets)
706 );
707
708 void ShowIncomeGraph()
709 {
710 AllocateWindowDescFront<IncomeGraphWindow>(&_income_graph_desc, 0);
711 }
712
713 /*******************/
714 /* DELIVERED CARGO */
715 /*******************/
716
717 struct DeliveredCargoGraphWindow : BaseGraphWindow {
718 DeliveredCargoGraphWindow(WindowDesc *desc, WindowNumber window_number) :
719 BaseGraphWindow(desc, WID_CV_GRAPH, STR_JUST_COMMA)
720 {
721 this->InitializeWindow(window_number);
722 }
723
724 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
725 {
726 return c->old_economy[j].delivered_cargo.GetSum<OverflowSafeInt64>();
727 }
728 };
729
730 static const NWidgetPart _nested_delivered_cargo_graph_widgets[] = {
731 NWidget(NWID_HORIZONTAL),
732 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
733 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_CARGO_DELIVERED_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
734 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CV_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
735 NWidget(WWT_SHADEBOX, COLOUR_GREY),
736 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
737 NWidget(WWT_STICKYBOX, COLOUR_GREY),
738 EndContainer(),
739 NWidget(WWT_PANEL, COLOUR_GREY, WID_CV_BACKGROUND),
740 NWidget(NWID_HORIZONTAL),
741 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CV_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
742 NWidget(NWID_VERTICAL),
743 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
744 NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CV_RESIZE),
745 EndContainer(),
746 EndContainer(),
747 EndContainer(),
748 };
749
750 static WindowDesc _delivered_cargo_graph_desc(
751 WDP_AUTO, "graph_delivered_cargo", 0, 0,
752 WC_DELIVERED_CARGO, WC_NONE,
753 0,
754 _nested_delivered_cargo_graph_widgets, lengthof(_nested_delivered_cargo_graph_widgets)
755 );
756
757 void ShowDeliveredCargoGraph()
758 {
759 AllocateWindowDescFront<DeliveredCargoGraphWindow>(&_delivered_cargo_graph_desc, 0);
760 }
761
762 /***********************/
763 /* PERFORMANCE HISTORY */
764 /***********************/
765
766 struct PerformanceHistoryGraphWindow : BaseGraphWindow {
767 PerformanceHistoryGraphWindow(WindowDesc *desc, WindowNumber window_number) :
768 BaseGraphWindow(desc, WID_PHG_GRAPH, STR_JUST_COMMA)
769 {
770 this->InitializeWindow(window_number);
771 }
772
773 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
774 {
775 return c->old_economy[j].performance_history;
776 }
777
778 virtual void OnClick(Point pt, int widget, int click_count)
779 {
780 if (widget == WID_PHG_DETAILED_PERFORMANCE) ShowPerformanceRatingDetail();
781 this->BaseGraphWindow::OnClick(pt, widget, click_count);
782 }
783 };
784
785 static const NWidgetPart _nested_performance_history_widgets[] = {
786 NWidget(NWID_HORIZONTAL),
787 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
788 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_COMPANY_PERFORMANCE_RATINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
789 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_PHG_DETAILED_PERFORMANCE), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_PERFORMANCE_DETAIL_KEY, STR_GRAPH_PERFORMANCE_DETAIL_TOOLTIP),
790 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_PHG_KEY), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
791 NWidget(WWT_SHADEBOX, COLOUR_GREY),
792 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
793 NWidget(WWT_STICKYBOX, COLOUR_GREY),
794 EndContainer(),
795 NWidget(WWT_PANEL, COLOUR_GREY, WID_PHG_BACKGROUND),
796 NWidget(NWID_HORIZONTAL),
797 NWidget(WWT_EMPTY, COLOUR_GREY, WID_PHG_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
798 NWidget(NWID_VERTICAL),
799 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
800 NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_PHG_RESIZE),
801 EndContainer(),
802 EndContainer(),
803 EndContainer(),
804 };
805
806 static WindowDesc _performance_history_desc(
807 WDP_AUTO, "graph_performance", 0, 0,
808 WC_PERFORMANCE_HISTORY, WC_NONE,
809 0,
810 _nested_performance_history_widgets, lengthof(_nested_performance_history_widgets)
811 );
812
813 void ShowPerformanceHistoryGraph()
814 {
815 AllocateWindowDescFront<PerformanceHistoryGraphWindow>(&_performance_history_desc, 0);
816 }
817
818 /*****************/
819 /* COMPANY VALUE */
820 /*****************/
821
822 struct CompanyValueGraphWindow : BaseGraphWindow {
823 CompanyValueGraphWindow(WindowDesc *desc, WindowNumber window_number) :
824 BaseGraphWindow(desc, WID_CV_GRAPH, STR_JUST_CURRENCY_SHORT)
825 {
826 this->InitializeWindow(window_number);
827 }
828
829 virtual OverflowSafeInt64 GetGraphData(const Company *c, int j)
830 {
831 return c->old_economy[j].company_value;
832 }
833 };
834
835 static const NWidgetPart _nested_company_value_graph_widgets[] = {
836 NWidget(NWID_HORIZONTAL),
837 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
838 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_COMPANY_VALUES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
839 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CV_KEY_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
840 NWidget(WWT_SHADEBOX, COLOUR_GREY),
841 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
842 NWidget(WWT_STICKYBOX, COLOUR_GREY),
843 EndContainer(),
844 NWidget(WWT_PANEL, COLOUR_GREY, WID_CV_BACKGROUND),
845 NWidget(NWID_HORIZONTAL),
846 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CV_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
847 NWidget(NWID_VERTICAL),
848 NWidget(NWID_SPACER), SetFill(0, 1), SetResize(0, 1),
849 NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CV_RESIZE),
850 EndContainer(),
851 EndContainer(),
852 EndContainer(),
853 };
854
855 static WindowDesc _company_value_graph_desc(
856 WDP_AUTO, "graph_company_value", 0, 0,
857 WC_COMPANY_VALUE, WC_NONE,
858 0,
859 _nested_company_value_graph_widgets, lengthof(_nested_company_value_graph_widgets)
860 );
861
862 void ShowCompanyValueGraph()
863 {
864 AllocateWindowDescFront<CompanyValueGraphWindow>(&_company_value_graph_desc, 0);
865 }
866
867 /*****************/
868 /* PAYMENT RATES */
869 /*****************/
870
871 struct PaymentRatesGraphWindow : BaseGraphWindow {
872 bool first_init;
873 PaymentRatesGraphWindow(WindowDesc *desc, WindowNumber window_number) :
874 BaseGraphWindow(desc, WID_CPR_GRAPH, STR_JUST_CURRENCY_SHORT)
875 {
876 this->first_init = true;
877 this->num_on_x_axis = 20;
878 this->num_vert_lines = 20;
879 this->month = 0xFF;
880 this->x_values_start = 10;
881 this->x_values_increment = 10;
882
883 /* Initialise the dataset */
884 this->OnHundredthTick();
885
886 this->InitNested(window_number);
887
888 this->UpdateLoweredWidgets();
889 }
890
891 virtual void OnInit()
892 {
893 /* UpdateLoweredWidgets needs to be called after a language or NewGRF change, but it can't be called before
894 * InitNested is done. On the first init these functions are called in the correct order by the constructor. */
895 if (!this->first_init) {
896 /* Initialise the dataset */
897 this->OnHundredthTick();
898 this->UpdateLoweredWidgets();
899 }
900 this->first_init = false;
901 }
902
903 void UpdateExcludedData()
904 {
905 this->excluded_data = 0;
906
907 int i = 0;
908 const CargoSpec *cs;
909 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
910 if (HasBit(_legend_excluded_cargo, cs->Index())) SetBit(this->excluded_data, i);
911 i++;
912 }
913 }
914
915 void UpdateLoweredWidgets()
916 {
917 for (int i = 0; i < _sorted_standard_cargo_specs_size; i++) {
918 this->SetWidgetLoweredState(WID_CPR_CARGO_FIRST + i, !HasBit(this->excluded_data, i));
919 }
920 }
921
922 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
923 {
924 if (widget < WID_CPR_CARGO_FIRST) {
925 BaseGraphWindow::UpdateWidgetSize(widget, size, padding, fill, resize);
926 return;
927 }
928
929 const CargoSpec *cs = _sorted_cargo_specs[widget - WID_CPR_CARGO_FIRST];
930 SetDParam(0, cs->name);
931 Dimension d = GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO);
932 d.width += 14; // colour field
933 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
934 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
935 *size = maxdim(d, *size);
936 }
937
938 virtual void DrawWidget(const Rect &r, int widget) const
939 {
940 if (widget < WID_CPR_CARGO_FIRST) {
941 BaseGraphWindow::DrawWidget(r, widget);
942 return;
943 }
944
945 const CargoSpec *cs = _sorted_cargo_specs[widget - WID_CPR_CARGO_FIRST];
946 bool rtl = _current_text_dir == TD_RTL;
947
948 /* Since the buttons have no text, no images,
949 * both the text and the coloured box have to be manually painted.
950 * clk_dif will move one pixel down and one pixel to the right
951 * when the button is clicked */
952 byte clk_dif = this->IsWidgetLowered(widget) ? 1 : 0;
953 int x = r.left + WD_FRAMERECT_LEFT;
954 int y = r.top;
955
956 int rect_x = clk_dif + (rtl ? r.right - 12 : r.left + WD_FRAMERECT_LEFT);
957
958 GfxFillRect(rect_x, y + clk_dif, rect_x + 8, y + 5 + clk_dif, PC_BLACK);
959 GfxFillRect(rect_x + 1, y + 1 + clk_dif, rect_x + 7, y + 4 + clk_dif, cs->legend_colour);
960 SetDParam(0, cs->name);
961 DrawString(rtl ? r.left : x + 14 + clk_dif, (rtl ? r.right - 14 + clk_dif : r.right), y + clk_dif, STR_GRAPH_CARGO_PAYMENT_CARGO);
962 }
963
964 virtual void OnClick(Point pt, int widget, int click_count)
965 {
966 switch (widget) {
967 case WID_CPR_ENABLE_CARGOES:
968 /* Remove all cargoes from the excluded lists. */
969 _legend_excluded_cargo = 0;
970 this->excluded_data = 0;
971 this->UpdateLoweredWidgets();
972 this->SetDirty();
973 break;
974
975 case WID_CPR_DISABLE_CARGOES: {
976 /* Add all cargoes to the excluded lists. */
977 int i = 0;
978 const CargoSpec *cs;
979 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
980 SetBit(_legend_excluded_cargo, cs->Index());
981 SetBit(this->excluded_data, i);
982 i++;
983 }
984 this->UpdateLoweredWidgets();
985 this->SetDirty();
986 break;
987 }
988
989 default:
990 if (widget >= WID_CPR_CARGO_FIRST) {
991 int i = widget - WID_CPR_CARGO_FIRST;
992 ToggleBit(_legend_excluded_cargo, _sorted_cargo_specs[i]->Index());
993 this->ToggleWidgetLoweredState(widget);
994 this->UpdateExcludedData();
995 this->SetDirty();
996 }
997 break;
998 }
999 }
1000
1001 virtual void OnTick()
1002 {
1003 /* Override default OnTick */
1004 }
1005
1011 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1012 {
1013 if (!gui_scope) return;
1014 this->OnHundredthTick();
1015 }
1016
1017 virtual void OnHundredthTick()
1018 {
1019 this->UpdateExcludedData();
1020
1021 int i = 0;
1022 const CargoSpec *cs;
1023 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
1024 this->colours[i] = cs->legend_colour;
1025 for (uint j = 0; j != 20; j++) {
1026 this->cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->Index());
1027 }
1028 i++;
1029 }
1030 this->num_dataset = i;
1031 }
1032 };
1033
1035 static NWidgetBase *MakeCargoButtons(int *biggest_index)
1036 {
1037 NWidgetVertical *ver = new NWidgetVertical;
1038
1039 for (int i = 0; i < _sorted_standard_cargo_specs_size; i++) {
1040 NWidgetBackground *leaf = new NWidgetBackground(WWT_PANEL, COLOUR_ORANGE, WID_CPR_CARGO_FIRST + i, NULL);
1041 leaf->tool_tip = STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO;
1042 leaf->SetFill(1, 0);
1043 leaf->SetLowered(true);
1044 ver->Add(leaf);
1045 }
1046 *biggest_index = WID_CPR_CARGO_FIRST + _sorted_standard_cargo_specs_size - 1;
1047 return ver;
1048 }
1049
1050
1051 static const NWidgetPart _nested_cargo_payment_rates_widgets[] = {
1052 NWidget(NWID_HORIZONTAL),
1053 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1054 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1055 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1056 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1057 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1058 EndContainer(),
1059 NWidget(WWT_PANEL, COLOUR_GREY, WID_CPR_BACKGROUND), SetMinimalSize(568, 128),
1060 NWidget(NWID_HORIZONTAL),
1061 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1062 NWidget(WWT_TEXT, COLOUR_GREY, WID_CPR_HEADER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_TITLE, STR_NULL),
1063 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1064 EndContainer(),
1065 NWidget(NWID_HORIZONTAL),
1066 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CPR_GRAPH), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
1067 NWidget(NWID_VERTICAL),
1068 NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 0), SetResize(0, 1),
1069 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CPR_ENABLE_CARGOES), SetDataTip(STR_GRAPH_CARGO_ENABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL), SetFill(1, 0),
1070 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CPR_DISABLE_CARGOES), SetDataTip(STR_GRAPH_CARGO_DISABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL), SetFill(1, 0),
1071 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
1072 NWidgetFunction(MakeCargoButtons),
1073 NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1), SetResize(0, 1),
1074 EndContainer(),
1075 NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
1076 EndContainer(),
1077 NWidget(NWID_HORIZONTAL),
1078 NWidget(NWID_SPACER), SetMinimalSize(WD_RESIZEBOX_WIDTH, 0), SetFill(1, 0), SetResize(1, 0),
1079 NWidget(WWT_TEXT, COLOUR_GREY, WID_CPR_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_X_LABEL, STR_NULL),
1080 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1081 NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CPR_RESIZE),
1082 EndContainer(),
1083 EndContainer(),
1084 };
1085
1086 static WindowDesc _cargo_payment_rates_desc(
1087 WDP_AUTO, "graph_cargo_payment_rates", 0, 0,
1088 WC_PAYMENT_RATES, WC_NONE,
1089 0,
1090 _nested_cargo_payment_rates_widgets, lengthof(_nested_cargo_payment_rates_widgets)
1091 );
1092
1093
1094 void ShowCargoPaymentRates()
1095 {
1096 AllocateWindowDescFront<PaymentRatesGraphWindow>(&_cargo_payment_rates_desc, 0);
1097 }
1098
1099 /************************/
1100 /* COMPANY LEAGUE TABLE */
1101 /************************/
1102
1103 static const StringID _performance_titles[] = {
1104 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
1105 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
1106 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
1107 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
1108 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
1109 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
1110 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
1111 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
1112 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
1113 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
1114 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
1115 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
1116 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
1117 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
1118 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT,
1119 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON,
1120 };
1121
1122 static inline StringID GetPerformanceTitleFromValue(uint value)
1123 {
1124 return _performance_titles[minu(value, 1000) >> 6];
1125 }
1126
1127 class CompanyLeagueWindow : public Window {
1128 private:
1129 GUIList<const Company*> companies;
1130 uint ordinal_width;
1131 uint text_width;
1132 uint icon_width;
1133 int line_height;
1134
1138 void BuildCompanyList()
1139 {
1140 if (!this->companies.NeedRebuild()) return;
1141
1142 this->companies.Clear();
1143
1144 const Company *c;
1145 FOR_ALL_COMPANIES(c) {
1146 *this->companies.Append() = c;
1147 }
1148
1149 this->companies.Compact();
1150 this->companies.RebuildDone();
1151 }
1152
1154 static int CDECL PerformanceSorter(const Company * const *c1, const Company * const *c2)
1155 {
1156 return (*c2)->old_economy[0].performance_history - (*c1)->old_economy[0].performance_history;
1157 }
1158
1159 public:
1160 CompanyLeagueWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
1161 {
1162 this->InitNested(window_number);
1163 this->companies.ForceRebuild();
1164 this->companies.NeedResort();
1165 }
1166
1167 virtual void OnPaint()
1168 {
1169 this->BuildCompanyList();
1170 this->companies.Sort(&PerformanceSorter);
1171
1172 this->DrawWidgets();
1173 }
1174
1175 virtual void DrawWidget(const Rect &r, int widget) const
1176 {
1177 if (widget != WID_CL_BACKGROUND) return;
1178
1179 int icon_y_offset = 1 + (FONT_HEIGHT_NORMAL - this->line_height) / 2;
1180 uint y = r.top + WD_FRAMERECT_TOP - icon_y_offset;
1181
1182 bool rtl = _current_text_dir == TD_RTL;
1183 uint ordinal_left = rtl ? r.right - WD_FRAMERECT_LEFT - this->ordinal_width : r.left + WD_FRAMERECT_LEFT;
1184 uint ordinal_right = rtl ? r.right - WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->ordinal_width;
1185 uint icon_left = r.left + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT + (rtl ? this->text_width : this->ordinal_width);
1186 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - WD_FRAMERECT_LEFT - this->text_width;
1187 uint text_right = rtl ? r.left + WD_FRAMERECT_LEFT + this->text_width : r.right - WD_FRAMERECT_LEFT;
1188
1189 for (uint i = 0; i != this->companies.Length(); i++) {
1190 const Company *c = this->companies[i];
1191 DrawString(ordinal_left, ordinal_right, y, i + STR_ORDINAL_NUMBER_1ST, i == 0 ? TC_WHITE : TC_YELLOW);
1192
1193 DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
1194
1195 SetDParam(0, c->index);
1196 SetDParam(1, c->index);
1197 SetDParam(2, GetPerformanceTitleFromValue(c->old_economy[0].performance_history));
1198 DrawString(text_left, text_right, y, STR_COMPANY_LEAGUE_COMPANY_NAME);
1199 y += this->line_height;
1200 }
1201 }
1202
1203 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1204 {
1205 if (widget != WID_CL_BACKGROUND) return;
1206
1207 this->ordinal_width = 0;
1208 for (uint i = 0; i < MAX_COMPANIES; i++) {
1209 this->ordinal_width = max(this->ordinal_width, GetStringBoundingBox(STR_ORDINAL_NUMBER_1ST + i).width);
1210 }
1211 this->ordinal_width += 5; // Keep some extra spacing
1212
1213 uint widest_width = 0;
1214 uint widest_title = 0;
1215 for (uint i = 0; i < lengthof(_performance_titles); i++) {
1216 uint width = GetStringBoundingBox(_performance_titles[i]).width;
1217 if (width > widest_width) {
1218 widest_title = i;
1219 widest_width = width;
1220 }
1221 }
1222
1223 Dimension d = GetSpriteSize(SPR_COMPANY_ICON);
1224 this->icon_width = d.width + 2;
1225 this->line_height = max<int>(d.height + 2, FONT_HEIGHT_NORMAL);
1226
1227 const Company *c;
1228 FOR_ALL_COMPANIES(c) {
1229 SetDParam(0, c->index);
1230 SetDParam(1, c->index);
1231 SetDParam(2, _performance_titles[widest_title]);
1232 widest_width = max(widest_width, GetStringBoundingBox(STR_COMPANY_LEAGUE_COMPANY_NAME).width);
1233 }
1234
1235 this->text_width = widest_width + 30; // Keep some extra spacing
1236
1237 size->width = WD_FRAMERECT_LEFT + this->ordinal_width + WD_FRAMERECT_RIGHT + this->icon_width + WD_FRAMERECT_LEFT + this->text_width + WD_FRAMERECT_RIGHT;
1238 size->height = WD_FRAMERECT_TOP + this->line_height * MAX_COMPANIES + WD_FRAMERECT_BOTTOM;
1239 }
1240
1241
1242 virtual void OnTick()
1243 {
1244 if (this->companies.NeedResort()) {
1245 this->SetDirty();
1246 }
1247 }
1248
1254 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1255 {
1256 if (data == 0) {
1257 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
1258 this->companies.ForceRebuild();
1259 } else {
1260 this->companies.ForceResort();
1261 }
1262 }
1263 };
1264
1265 static const NWidgetPart _nested_company_league_widgets[] = {
1266 NWidget(NWID_HORIZONTAL),
1267 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1268 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_LEAGUE_TABLE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1269 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1270 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1271 EndContainer(),
1272 NWidget(WWT_PANEL, COLOUR_GREY, WID_CL_BACKGROUND), SetMinimalSize(400, 0), SetMinimalTextLines(15, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM),
1273 };
1274
1275 static WindowDesc _company_league_desc(
1276 WDP_AUTO, "league", 0, 0,
1277 WC_COMPANY_LEAGUE, WC_NONE,
1278 0,
1279 _nested_company_league_widgets, lengthof(_nested_company_league_widgets)
1280 );
1281
1282 void ShowCompanyLeagueTable()
1283 {
1284 AllocateWindowDescFront<CompanyLeagueWindow>(&_company_league_desc, 0);
1285 }
1286
1287 /*****************************/
1288 /* PERFORMANCE RATING DETAIL */
1289 /*****************************/
1290
1291 struct PerformanceRatingDetailWindow : Window {
1292 static CompanyID company;
1293 int timeout;
1294
1295 PerformanceRatingDetailWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
1296 {
1297 this->UpdateCompanyStats();
1298
1299 this->InitNested(window_number);
1300 this->OnInvalidateData(INVALID_COMPANY);
1301 }
1302
1303 void UpdateCompanyStats()
1304 {
1305 /* Update all company stats with the current data
1306 * (this is because _score_info is not saved to a savegame) */
1307 Company *c;
1308 FOR_ALL_COMPANIES(c) {
1309 UpdateCompanyRatingAndValue(c, false);
1310 }
1311
1312 this->timeout = DAY_TICKS * 5;
1313 }
1314
1315 uint score_info_left;
1316 uint score_info_right;
1317 uint bar_left;
1318 uint bar_right;
1319 uint bar_width;
1320 uint bar_height;
1321 uint score_detail_left;
1322 uint score_detail_right;
1323
1324 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1325 {
1326 switch (widget) {
1327 case WID_PRD_SCORE_FIRST:
1328 this->bar_height = FONT_HEIGHT_NORMAL + 4;
1329 size->height = this->bar_height + 2 * WD_MATRIX_TOP;
1330
1331 uint score_info_width = 0;
1332 for (uint i = SCORE_BEGIN; i < SCORE_END; i++) {
1333 score_info_width = max(score_info_width, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES + i).width);
1334 }
1335 SetDParamMaxValue(0, 1000);
1336 score_info_width += GetStringBoundingBox(STR_BLACK_COMMA).width + WD_FRAMERECT_LEFT;
1337
1338 SetDParamMaxValue(0, 100);
1339 this->bar_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_PERCENT).width + 20; // Wide bars!
1340
1341 /* At this number we are roughly at the max; it can become wider,
1342 * but then you need at 1000 times more money. At that time you're
1343 * not that interested anymore in the last few digits anyway.
1344 * The 500 is because 999 999 500 to 999 999 999 are rounded to
1345 * 1 000 M, and not 999 999 k. Use negative numbers to account for
1346 * the negative income/amount of money etc. as well. */
1347 int max = -(999999999 - 500);
1348
1349 /* Scale max for the display currency. Prior to rendering the value
1350 * is converted into the display currency, which may cause it to
1351 * raise significantly. We need to compensate for that since {{CURRCOMPACT}}
1352 * is used, which can produce quite short renderings of very large
1353 * values. Otherwise the calculated width could be too narrow.
1354 * Note that it doesn't work if there was a currency with an exchange
1355 * rate greater than max.
1356 * When the currency rate is more than 1000, the 999 999 k becomes at
1357 * least 999 999 M which roughly is equally long. Furthermore if the
1358 * exchange rate is that high, 999 999 k is usually not enough anymore
1359 * to show the different currency numbers. */
1360 if (_currency->rate < 1000) max /= _currency->rate;
1361 SetDParam(0, max);
1362 SetDParam(1, max);
1363 uint score_detail_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY).width;
1364
1365 size->width = 7 + score_info_width + 5 + this->bar_width + 5 + score_detail_width + 7;
1366 uint left = 7;
1367 uint right = size->width - 7;
1368
1369 bool rtl = _current_text_dir == TD_RTL;
1370 this->score_info_left = rtl ? right - score_info_width : left;
1371 this->score_info_right = rtl ? right : left + score_info_width;
1372
1373 this->score_detail_left = rtl ? left : right - score_detail_width;
1374 this->score_detail_right = rtl ? left + score_detail_width : right;
1375
1376 this->bar_left = left + (rtl ? score_detail_width : score_info_width) + 5;
1377 this->bar_right = this->bar_left + this->bar_width;
1378 break;
1379 }
1380 }
1381
1382 virtual void DrawWidget(const Rect &r, int widget) const
1383 {
1384 /* No need to draw when there's nothing to draw */
1385 if (this->company == INVALID_COMPANY) return;
1386
1387 if (IsInsideMM(widget, WID_PRD_COMPANY_FIRST, WID_PRD_COMPANY_LAST + 1)) {
1388 if (this->IsWidgetDisabled(widget)) return;
1389 CompanyID cid = (CompanyID)(widget - WID_PRD_COMPANY_FIRST);
1390 int offset = (cid == this->company) ? 1 : 0;
1391 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
1392 DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2 + offset, (r.top + r.bottom - sprite_size.height) / 2 + offset);
1393 return;
1394 }
1395
1396 if (!IsInsideMM(widget, WID_PRD_SCORE_FIRST, WID_PRD_SCORE_LAST + 1)) return;
1397
1398 ScoreID score_type = (ScoreID)(widget - WID_PRD_SCORE_FIRST);
1399
1400 /* The colours used to show how the progress is going */
1401 int colour_done = _colour_gradient[COLOUR_GREEN][4];
1402 int colour_notdone = _colour_gradient[COLOUR_RED][4];
1403
1404 /* Draw all the score parts */
1405 int val = _score_part[company][score_type];
1406 int needed = _score_info[score_type].needed;
1407 int score = _score_info[score_type].score;
1408
1409 /* SCORE_TOTAL has his own rules ;) */
1410 if (score_type == SCORE_TOTAL) {
1411 for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) score += _score_info[i].score;
1412 needed = SCORE_MAX;
1413 }
1414
1415 uint bar_top = r.top + WD_MATRIX_TOP;
1416 uint text_top = bar_top + 2;
1417
1418 DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
1419
1420 /* Draw the score */
1421 SetDParam(0, score);
1422 DrawString(this->score_info_left, this->score_info_right, text_top, STR_BLACK_COMMA, TC_FROMSTRING, SA_RIGHT);
1423
1424 /* Calculate the %-bar */
1425 uint x = Clamp(val, 0, needed) * this->bar_width / needed;
1426 bool rtl = _current_text_dir == TD_RTL;
1427 if (rtl) {
1428 x = this->bar_right - x;
1429 } else {
1430 x = this->bar_left + x;
1431 }
1432
1433 /* Draw the bar */
1434 if (x != this->bar_left) GfxFillRect(this->bar_left, bar_top, x, bar_top + this->bar_height, rtl ? colour_notdone : colour_done);
1435 if (x != this->bar_right) GfxFillRect(x, bar_top, this->bar_right, bar_top + this->bar_height, rtl ? colour_done : colour_notdone);
1436
1437 /* Draw it */
1438 SetDParam(0, Clamp(val, 0, needed) * 100 / needed);
1439 DrawString(this->bar_left, this->bar_right, text_top, STR_PERFORMANCE_DETAIL_PERCENT, TC_FROMSTRING, SA_HOR_CENTER);
1440
1441 /* SCORE_LOAN is inversed */
1442 if (score_type == SCORE_LOAN) val = needed - val;
1443
1444 /* Draw the amount we have against what is needed
1445 * For some of them it is in currency format */
1446 SetDParam(0, val);
1447 SetDParam(1, needed);
1448 switch (score_type) {
1449 case SCORE_MIN_PROFIT:
1450 case SCORE_MIN_INCOME:
1451 case SCORE_MAX_INCOME:
1452 case SCORE_MONEY:
1453 case SCORE_LOAN:
1454 DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY);
1455 break;
1456 default:
1457 DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_INT);
1458 }
1459 }
1460
1461 virtual void OnClick(Point pt, int widget, int click_count)
1462 {
1463 /* Check which button is clicked */
1464 if (IsInsideMM(widget, WID_PRD_COMPANY_FIRST, WID_PRD_COMPANY_LAST + 1)) {
1465 /* Is it no on disable? */
1466 if (!this->IsWidgetDisabled(widget)) {
1467 this->RaiseWidget(this->company + WID_PRD_COMPANY_FIRST);
1468 this->company = (CompanyID)(widget - WID_PRD_COMPANY_FIRST);
1469 this->LowerWidget(this->company + WID_PRD_COMPANY_FIRST);
1470 this->SetDirty();
1471 }
1472 }
1473 }
1474
1475 virtual void OnTick()
1476 {
1477 if (_pause_mode != PM_UNPAUSED) return;
1478
1479 /* Update the company score every 5 days */
1480 if (--this->timeout == 0) {
1481 this->UpdateCompanyStats();
1482 this->SetDirty();
1483 }
1484 }
1485
1491 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1492 {
1493 if (!gui_scope) return;
1494 /* Disable the companies who are not active */
1495 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1496 this->SetWidgetDisabledState(i + WID_PRD_COMPANY_FIRST, !Company::IsValidID(i));
1497 }
1498
1499 /* Check if the currently selected company is still active. */
1500 if (this->company != INVALID_COMPANY && !Company::IsValidID(this->company)) {
1501 /* Raise the widget for the previous selection. */
1502 this->RaiseWidget(this->company + WID_PRD_COMPANY_FIRST);
1503 this->company = INVALID_COMPANY;
1504 }
1505
1506 if (this->company == INVALID_COMPANY) {
1507 const Company *c;
1508 FOR_ALL_COMPANIES(c) {
1509 this->company = c->index;
1510 break;
1511 }
1512 }
1513
1514 /* Make sure the widget is lowered */
1515 this->LowerWidget(this->company + WID_PRD_COMPANY_FIRST);
1516 }
1517 };
1518
1519 CompanyID PerformanceRatingDetailWindow::company = INVALID_COMPANY;
1520
1527 static NWidgetBase *MakePerformanceDetailPanels(int *biggest_index)
1528 {
1529 const StringID performance_tips[] = {
1530 STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP,
1531 STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP,
1532 STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP,
1533 STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP,
1534 STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP,
1535 STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP,
1536 STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP,
1537 STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP,
1538 STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP,
1539 STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP,
1540 };
1541
1542 assert_compile(lengthof(performance_tips) == SCORE_END - SCORE_BEGIN);
1543
1544 NWidgetVertical *vert = new NWidgetVertical(NC_EQUALSIZE);
1545 for (int widnum = WID_PRD_SCORE_FIRST; widnum <= WID_PRD_SCORE_LAST; widnum++) {
1546 NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
1547 panel->SetFill(1, 1);
1548 panel->SetDataTip(0x0, performance_tips[widnum - WID_PRD_SCORE_FIRST]);
1549 vert->Add(panel);
1550 }
1551 *biggest_index = WID_PRD_SCORE_LAST;
1552 return vert;
1553 }
1554
1556 NWidgetBase *MakeCompanyButtonRowsGraphGUI(int *biggest_index)
1557 {
1558 return MakeCompanyButtonRows(biggest_index, WID_PRD_COMPANY_FIRST, WID_PRD_COMPANY_LAST, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP);
1559 }
1560
1561 static const NWidgetPart _nested_performance_rating_detail_widgets[] = {
1562 NWidget(NWID_HORIZONTAL),
1563 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1564 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PERFORMANCE_DETAIL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1565 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1566 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1567 EndContainer(),
1568 NWidget(WWT_PANEL, COLOUR_GREY),
1569 NWidgetFunction(MakeCompanyButtonRowsGraphGUI), SetPadding(0, 1, 1, 2),
1570 EndContainer(),
1571 NWidgetFunction(MakePerformanceDetailPanels),
1572 };
1573
1574 static WindowDesc _performance_rating_detail_desc(
1575 WDP_AUTO, "league_details", 0, 0,
1576 WC_PERFORMANCE_DETAIL, WC_NONE,
1577 0,
1578 _nested_performance_rating_detail_widgets, lengthof(_nested_performance_rating_detail_widgets)
1579 );
1580
1581 void ShowPerformanceRatingDetail()
1582 {
1583 AllocateWindowDescFront<PerformanceRatingDetailWindow>(&_performance_rating_detail_desc, 0);
1584 }