2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file graph_gui.cpp GUI that shows performance graphs. */
11 #include "graph_gui.h"
12 #include "window_gui.h"
13 #include "company_base.h"
14 #include "company_gui.h"
15 #include "economy_func.h"
16 #include "cargotype.h"
17 #include "strings_func.h"
18 #include "window_func.h"
19 #include "date_func.h"
21 #include "sortlist_type.h"
22 #include "core/geometry_func.hpp"
25 #include "widgets/graph_widget.h"
27 #include "table/strings.h"
28 #include "table/sprites.h"
31 #include "safeguards.h"
33 /* Bitmasks of company and cargo indices that shouldn't be drawn. */
34 static CompanyMask _legend_excluded_companies
;
35 static CargoTypes _legend_excluded_cargo
;
37 /* Apparently these don't play well with enums. */
38 static const OverflowSafeInt64
INVALID_DATAPOINT(INT64_MAX
); // Value used for a datapoint that shouldn't be drawn.
39 static const uint INVALID_DATAPOINT_POS
= UINT_MAX
; // Used to determine if the previous point was drawn.
45 struct GraphLegendWindow
: Window
{
46 GraphLegendWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
48 this->InitNested(window_number
);
50 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
51 if (!HasBit(_legend_excluded_companies
, c
)) this->LowerWidget(c
+ WID_GL_FIRST_COMPANY
);
53 this->OnInvalidateData(c
);
57 void DrawWidget(const Rect
&r
, int widget
) const override
59 if (!IsInsideMM(widget
, WID_GL_FIRST_COMPANY
, MAX_COMPANIES
+ WID_GL_FIRST_COMPANY
)) return;
61 CompanyID cid
= (CompanyID
)(widget
- WID_GL_FIRST_COMPANY
);
63 if (!Company::IsValidID(cid
)) return;
65 bool rtl
= _current_text_dir
== TD_RTL
;
67 Dimension d
= GetSpriteSize(SPR_COMPANY_ICON
);
68 DrawCompanyIcon(cid
, rtl
? r
.right
- d
.width
- 2 : r
.left
+ 2, r
.top
+ (r
.bottom
- r
.top
- d
.height
) / 2);
72 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 void OnClick(Point pt
, int widget
, int click_count
) override
77 if (!IsInsideMM(widget
, WID_GL_FIRST_COMPANY
, MAX_COMPANIES
+ WID_GL_FIRST_COMPANY
)) return;
79 ToggleBit(_legend_excluded_companies
, widget
- WID_GL_FIRST_COMPANY
);
80 this->ToggleWidgetLoweredState(widget
);
82 InvalidateWindowData(WC_INCOME_GRAPH
, 0);
83 InvalidateWindowData(WC_OPERATING_PROFIT
, 0);
84 InvalidateWindowData(WC_DELIVERED_CARGO
, 0);
85 InvalidateWindowData(WC_PERFORMANCE_HISTORY
, 0);
86 InvalidateWindowData(WC_COMPANY_VALUE
, 0);
90 * Some data on this window has become invalid.
91 * @param data Information about the changed data.
92 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
94 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
96 if (!gui_scope
) return;
97 if (Company::IsValidID(data
)) return;
99 SetBit(_legend_excluded_companies
, data
);
100 this->RaiseWidget(data
+ WID_GL_FIRST_COMPANY
);
105 * Construct a vertical list of buttons, one for each company.
106 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
107 * @return Panel with company buttons.
108 * @post \c *biggest_index contains the largest used index in the tree.
110 static NWidgetBase
*MakeNWidgetCompanyLines(int *biggest_index
)
112 NWidgetVertical
*vert
= new NWidgetVertical();
113 uint line_height
= max
<uint
>(GetSpriteSize(SPR_COMPANY_ICON
).height
, FONT_HEIGHT_NORMAL
) + WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
115 for (int widnum
= WID_GL_FIRST_COMPANY
; widnum
<= WID_GL_LAST_COMPANY
; widnum
++) {
116 NWidgetBackground
*panel
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, widnum
);
117 panel
->SetMinimalSize(246, line_height
);
118 panel
->SetFill(1, 0);
119 panel
->SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION_TOOLTIP
);
122 *biggest_index
= WID_GL_LAST_COMPANY
;
126 static const NWidgetPart _nested_graph_legend_widgets
[] = {
127 NWidget(NWID_HORIZONTAL
),
128 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
129 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_GRAPH_KEY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
130 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
131 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
133 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_GL_BACKGROUND
),
134 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
135 NWidget(NWID_HORIZONTAL
),
136 NWidget(NWID_SPACER
), SetMinimalSize(2, 0),
137 NWidgetFunction(MakeNWidgetCompanyLines
),
138 NWidget(NWID_SPACER
), SetMinimalSize(2, 0),
143 static WindowDesc
_graph_legend_desc(
144 WDP_AUTO
, "graph_legend", 0, 0,
145 WC_GRAPH_LEGEND
, WC_NONE
,
147 _nested_graph_legend_widgets
, lengthof(_nested_graph_legend_widgets
)
150 static void ShowGraphLegend()
152 AllocateWindowDescFront
<GraphLegendWindow
>(&_graph_legend_desc
, 0);
155 /** Contains the interval of a graph's data. */
156 struct ValuesInterval
{
157 OverflowSafeInt64 highest
; ///< Highest value of this interval. Must be zero or greater.
158 OverflowSafeInt64 lowest
; ///< Lowest value of this interval. Must be zero or less.
165 struct BaseGraphWindow
: Window
{
167 static const int GRAPH_MAX_DATASETS
= 64;
168 static const int GRAPH_AXIS_LINE_COLOUR
= PC_BLACK
;
169 static const int GRAPH_NUM_MONTHS
= 24; ///< Number of months displayed in the graph.
171 static const int MIN_GRAPH_NUM_LINES_Y
= 9; ///< Minimal number of horizontal lines to draw.
172 static const int MIN_GRID_PIXEL_SIZE
= 20; ///< Minimum distance between graph lines.
174 uint64 excluded_data
; ///< bitmask of the datasets that shouldn't be displayed.
178 static const TextColour graph_axis_label_colour
= TC_BLACK
; ///< colour of the graph axis label.
180 /* The starting month and year that values are plotted against. If month is
181 * 0xFF, use x_values_start and x_values_increment below instead. */
185 /* These values are used if the graph is being plotted against values
186 * rather than the dates specified by month and year. */
187 uint16 x_values_start
;
188 uint16 x_values_increment
;
191 StringID format_str_y_axis
;
192 byte colours
[GRAPH_MAX_DATASETS
];
193 OverflowSafeInt64 cost
[GRAPH_MAX_DATASETS
][GRAPH_NUM_MONTHS
]; ///< Stored costs for the last #GRAPH_NUM_MONTHS months
196 * Get the interval that contains the graph's data. Excluded data is ignored to show smaller values in
197 * better detail when disabling higher ones.
198 * @param num_hori_lines Number of horizontal lines to be drawn.
199 * @return Highest and lowest values of the graph (ignoring disabled data).
201 ValuesInterval
GetValuesInterval(int num_hori_lines
) const
203 assert(num_hori_lines
> 0);
205 ValuesInterval current_interval
;
206 current_interval
.highest
= INT64_MIN
;
207 current_interval
.lowest
= INT64_MAX
;
209 for (int i
= 0; i
< this->num_dataset
; i
++) {
210 if (HasBit(this->excluded_data
, i
)) continue;
211 for (int j
= 0; j
< this->num_on_x_axis
; j
++) {
212 OverflowSafeInt64 datapoint
= this->cost
[i
][j
];
214 if (datapoint
!= INVALID_DATAPOINT
) {
215 current_interval
.highest
= max(current_interval
.highest
, datapoint
);
216 current_interval
.lowest
= min(current_interval
.lowest
, datapoint
);
221 /* Prevent showing values too close to the graph limits. */
222 current_interval
.highest
= (11 * current_interval
.highest
) / 10;
223 current_interval
.lowest
= (11 * current_interval
.lowest
) / 10;
225 /* Always include zero in the shown range. */
226 double abs_lower
= (current_interval
.lowest
> 0) ? 0 : (double)abs(current_interval
.lowest
);
227 double abs_higher
= (current_interval
.highest
< 0) ? 0 : (double)current_interval
.highest
;
232 if (abs_lower
!= 0 || abs_higher
!= 0) {
233 /* The number of grids to reserve for the positive part is: */
234 num_pos_grids
= (int)floor(0.5 + num_hori_lines
* abs_higher
/ (abs_higher
+ abs_lower
));
236 /* If there are any positive or negative values, force that they have at least one grid. */
237 if (num_pos_grids
== 0 && abs_higher
!= 0) num_pos_grids
++;
238 if (num_pos_grids
== num_hori_lines
&& abs_lower
!= 0) num_pos_grids
--;
240 /* Get the required grid size for each side and use the maximum one. */
241 int64 grid_size_higher
= (abs_higher
> 0) ? ((int64
)abs_higher
+ num_pos_grids
- 1) / num_pos_grids
: 0;
242 int64 grid_size_lower
= (abs_lower
> 0) ? ((int64
)abs_lower
+ num_hori_lines
- num_pos_grids
- 1) / (num_hori_lines
- num_pos_grids
) : 0;
243 grid_size
= max(grid_size_higher
, grid_size_lower
);
245 /* If both values are zero, show an empty graph. */
246 num_pos_grids
= num_hori_lines
/ 2;
250 current_interval
.highest
= num_pos_grids
* grid_size
;
251 current_interval
.lowest
= -(num_hori_lines
- num_pos_grids
) * grid_size
;
252 return current_interval
;
256 * Get width for Y labels.
257 * @param current_interval Interval that contains all of the graph data.
258 * @param num_hori_lines Number of horizontal lines to be drawn.
260 uint
GetYLabelWidth(ValuesInterval current_interval
, int num_hori_lines
) const
262 /* draw text strings on the y axis */
263 int64 y_label
= current_interval
.highest
;
264 int64 y_label_separation
= (current_interval
.highest
- current_interval
.lowest
) / num_hori_lines
;
268 for (int i
= 0; i
< (num_hori_lines
+ 1); i
++) {
269 SetDParam(0, this->format_str_y_axis
);
270 SetDParam(1, y_label
);
271 Dimension d
= GetStringBoundingBox(STR_GRAPH_Y_LABEL
);
272 if (d
.width
> max_width
) max_width
= d
.width
;
274 y_label
-= y_label_separation
;
281 * Actually draw the graph.
282 * @param r the rectangle of the data field of the graph
284 void DrawGraph(Rect r
) const
286 uint x
, y
; ///< Reused whenever x and y coordinates are needed.
287 ValuesInterval interval
; ///< Interval that contains all of the graph data.
288 int x_axis_offset
; ///< Distance from the top of the graph to the x axis.
290 /* the colours and cost array of GraphDrawer must accommodate
291 * both values for cargo and companies. So if any are higher, quit */
292 assert_compile(GRAPH_MAX_DATASETS
>= (int)NUM_CARGO
&& GRAPH_MAX_DATASETS
>= (int)MAX_COMPANIES
);
293 assert(this->num_vert_lines
> 0);
295 byte grid_colour
= _colour_gradient
[COLOUR_GREY
][4];
297 /* Rect r will be adjusted to contain just the graph, with labels being
298 * placed outside the area. */
299 r
.top
+= 5 + GetCharacterHeight(FS_SMALL
) / 2;
300 r
.bottom
-= (this->month
== 0xFF ? 1 : 3) * GetCharacterHeight(FS_SMALL
) + 4;
304 /* Initial number of horizontal lines. */
305 int num_hori_lines
= 160 / MIN_GRID_PIXEL_SIZE
;
306 /* For the rest of the height, the number of horizontal lines will increase more slowly. */
307 int resize
= (r
.bottom
- r
.top
- 160) / (2 * MIN_GRID_PIXEL_SIZE
);
308 if (resize
> 0) num_hori_lines
+= resize
;
310 interval
= GetValuesInterval(num_hori_lines
);
312 int label_width
= GetYLabelWidth(interval
, num_hori_lines
);
314 r
.left
+= label_width
;
316 int x_sep
= (r
.right
- r
.left
) / this->num_vert_lines
;
317 int y_sep
= (r
.bottom
- r
.top
) / num_hori_lines
;
319 /* Redetermine right and bottom edge of graph to fit with the integer
320 * separation values. */
321 r
.right
= r
.left
+ x_sep
* this->num_vert_lines
;
322 r
.bottom
= r
.top
+ y_sep
* num_hori_lines
;
324 OverflowSafeInt64 interval_size
= interval
.highest
+ abs(interval
.lowest
);
325 /* Where to draw the X axis. Use floating point to avoid overflowing and results of zero. */
326 x_axis_offset
= (int)((r
.bottom
- r
.top
) * (double)interval
.highest
/ (double)interval_size
);
328 /* Draw the vertical grid lines. */
330 /* Don't draw the first line, as that's where the axis will be. */
333 for (int i
= 0; i
< this->num_vert_lines
; i
++) {
334 GfxFillRect(x
, r
.top
, x
, r
.bottom
, grid_colour
);
338 /* Draw the horizontal grid lines. */
341 for (int i
= 0; i
< (num_hori_lines
+ 1); i
++) {
342 GfxFillRect(r
.left
- 3, y
, r
.left
- 1, y
, GRAPH_AXIS_LINE_COLOUR
);
343 GfxFillRect(r
.left
, y
, r
.right
, y
, grid_colour
);
347 /* Draw the y axis. */
348 GfxFillRect(r
.left
, r
.top
, r
.left
, r
.bottom
, GRAPH_AXIS_LINE_COLOUR
);
350 /* Draw the x axis. */
351 y
= x_axis_offset
+ r
.top
;
352 GfxFillRect(r
.left
, y
, r
.right
, y
, GRAPH_AXIS_LINE_COLOUR
);
354 /* Find the largest value that will be drawn. */
355 if (this->num_on_x_axis
== 0) return;
357 assert(this->num_on_x_axis
> 0);
358 assert(this->num_dataset
> 0);
360 /* draw text strings on the y axis */
361 int64 y_label
= interval
.highest
;
362 int64 y_label_separation
= abs(interval
.highest
- interval
.lowest
) / num_hori_lines
;
364 y
= r
.top
- GetCharacterHeight(FS_SMALL
) / 2;
366 for (int i
= 0; i
< (num_hori_lines
+ 1); i
++) {
367 SetDParam(0, this->format_str_y_axis
);
368 SetDParam(1, y_label
);
369 DrawString(r
.left
- label_width
- 4, r
.left
- 4, y
, STR_GRAPH_Y_LABEL
, graph_axis_label_colour
, SA_RIGHT
);
371 y_label
-= y_label_separation
;
375 /* draw strings on the x axis */
376 if (this->month
!= 0xFF) {
379 byte month
= this->month
;
380 Year year
= this->year
;
381 for (int i
= 0; i
< this->num_on_x_axis
; i
++) {
382 SetDParam(0, month
+ STR_MONTH_ABBREV_JAN
);
383 SetDParam(1, month
+ STR_MONTH_ABBREV_JAN
+ 2);
385 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
);
395 /* Draw the label under the data point rather than on the grid line. */
398 uint16 label
= this->x_values_start
;
400 for (int i
= 0; i
< this->num_on_x_axis
; i
++) {
402 DrawString(x
+ 1, x
+ x_sep
- 1, y
, STR_GRAPH_Y_LABEL_NUMBER
, graph_axis_label_colour
, SA_HOR_CENTER
);
404 label
+= this->x_values_increment
;
409 /* draw lines and dots */
410 uint linewidth
= _settings_client
.gui
.graph_line_thickness
;
411 uint pointoffs1
= (linewidth
+ 1) / 2;
412 uint pointoffs2
= linewidth
+ 1 - pointoffs1
;
413 for (int i
= 0; i
< this->num_dataset
; i
++) {
414 if (!HasBit(this->excluded_data
, i
)) {
415 /* Centre the dot between the grid lines. */
416 x
= r
.left
+ (x_sep
/ 2);
418 byte colour
= this->colours
[i
];
419 uint prev_x
= INVALID_DATAPOINT_POS
;
420 uint prev_y
= INVALID_DATAPOINT_POS
;
422 for (int j
= 0; j
< this->num_on_x_axis
; j
++) {
423 OverflowSafeInt64 datapoint
= this->cost
[i
][j
];
425 if (datapoint
!= INVALID_DATAPOINT
) {
427 * Check whether we need to reduce the 'accuracy' of the
428 * datapoint value and the highest value to split overflows.
429 * And when 'drawing' 'one million' or 'one million and one'
430 * there is no significant difference, so the least
431 * significant bits can just be removed.
433 * If there are more bits needed than would fit in a 32 bits
434 * integer, so at about 31 bits because of the sign bit, the
435 * least significant bits are removed.
437 int mult_range
= FindLastBit(x_axis_offset
) + FindLastBit(abs(datapoint
));
438 int reduce_range
= max(mult_range
- 31, 0);
440 /* Handle negative values differently (don't shift sign) */
442 datapoint
= -(abs(datapoint
) >> reduce_range
);
444 datapoint
>>= reduce_range
;
446 y
= r
.top
+ x_axis_offset
- ((r
.bottom
- r
.top
) * datapoint
) / (interval_size
>> reduce_range
);
448 /* Draw the point. */
449 GfxFillRect(x
- pointoffs1
, y
- pointoffs1
, x
+ pointoffs2
, y
+ pointoffs2
, colour
);
451 /* Draw the line connected to the previous point. */
452 if (prev_x
!= INVALID_DATAPOINT_POS
) GfxDrawLine(prev_x
, prev_y
, x
, y
, colour
, linewidth
);
457 prev_x
= INVALID_DATAPOINT_POS
;
458 prev_y
= INVALID_DATAPOINT_POS
;
468 BaseGraphWindow(WindowDesc
*desc
, int widget
, StringID format_str_y_axis
) :
470 format_str_y_axis(format_str_y_axis
)
472 SetWindowDirty(WC_GRAPH_LEGEND
, 0);
473 this->num_vert_lines
= 24;
474 this->graph_widget
= widget
;
477 void InitializeWindow(WindowNumber number
)
479 /* Initialise the dataset */
480 this->UpdateStatistics(true);
482 this->InitNested(number
);
486 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
488 if (widget
!= this->graph_widget
) return;
490 uint x_label_width
= 0;
492 if (this->month
!= 0xFF) {
493 byte month
= this->month
;
494 Year year
= this->year
;
495 for (int i
= 0; i
< this->num_on_x_axis
; i
++) {
496 SetDParam(0, month
+ STR_MONTH_ABBREV_JAN
);
497 SetDParam(1, month
+ STR_MONTH_ABBREV_JAN
+ 2);
499 x_label_width
= max(x_label_width
, GetStringBoundingBox(month
== 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR
: STR_GRAPH_X_LABEL_MONTH
).width
);
508 /* Draw the label under the data point rather than on the grid line. */
509 SetDParamMaxValue(0, this->x_values_start
+ this->num_on_x_axis
* this->x_values_increment
, 0, FS_SMALL
);
510 x_label_width
= GetStringBoundingBox(STR_GRAPH_Y_LABEL_NUMBER
).width
;
513 SetDParam(0, this->format_str_y_axis
);
514 SetDParam(1, INT64_MAX
);
515 uint y_label_width
= GetStringBoundingBox(STR_GRAPH_Y_LABEL
).width
;
517 size
->width
= max
<uint
>(size
->width
, 5 + y_label_width
+ this->num_on_x_axis
* (x_label_width
+ 5) + 9);
518 size
->height
= max
<uint
>(size
->height
, 5 + (1 + MIN_GRAPH_NUM_LINES_Y
* 2 + (this->month
!= 0xFF ? 3 : 1)) * FONT_HEIGHT_SMALL
+ 4);
519 size
->height
= max
<uint
>(size
->height
, size
->width
/ 3);
522 void DrawWidget(const Rect
&r
, int widget
) const override
524 if (widget
!= this->graph_widget
) return;
529 virtual OverflowSafeInt64
GetGraphData(const Company
*c
, int j
)
531 return INVALID_DATAPOINT
;
534 void OnClick(Point pt
, int widget
, int click_count
) override
536 /* Clicked on legend? */
537 if (widget
== WID_CV_KEY_BUTTON
) ShowGraphLegend();
540 void OnGameTick() override
542 this->UpdateStatistics(false);
546 * Some data on this window has become invalid.
547 * @param data Information about the changed data.
548 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
550 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
552 if (!gui_scope
) return;
553 this->UpdateStatistics(true);
557 * Update the statistics.
558 * @param initialize Initialize the data structure.
560 void UpdateStatistics(bool initialize
)
562 CompanyMask excluded_companies
= _legend_excluded_companies
;
564 /* Exclude the companies which aren't valid */
565 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
566 if (!Company::IsValidID(c
)) SetBit(excluded_companies
, c
);
570 for (const Company
*c
: Company::Iterate()) {
571 nums
= min(this->num_vert_lines
, max(nums
, c
->num_valid_stat_ent
));
574 int mo
= (_cur_month
/ 3 - nums
) * 3;
581 if (!initialize
&& this->excluded_data
== excluded_companies
&& this->num_on_x_axis
== nums
&&
582 this->year
== yr
&& this->month
== mo
) {
583 /* There's no reason to get new stats */
587 this->excluded_data
= excluded_companies
;
588 this->num_on_x_axis
= nums
;
593 for (CompanyID k
= COMPANY_FIRST
; k
< MAX_COMPANIES
; k
++) {
594 const Company
*c
= Company::GetIfValid(k
);
596 this->colours
[numd
] = _colour_gradient
[c
->colour
][6];
597 for (int j
= this->num_on_x_axis
, i
= 0; --j
>= 0;) {
598 this->cost
[numd
][i
] = (j
>= c
->num_valid_stat_ent
) ? INVALID_DATAPOINT
: GetGraphData(c
, j
);
605 this->num_dataset
= numd
;
610 /********************/
611 /* OPERATING PROFIT */
612 /********************/
614 struct OperatingProfitGraphWindow
: BaseGraphWindow
{
615 OperatingProfitGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
616 BaseGraphWindow(desc
, WID_CV_GRAPH
, STR_JUST_CURRENCY_SHORT
)
618 this->InitializeWindow(window_number
);
621 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
623 return c
->old_economy
[j
].income
+ c
->old_economy
[j
].expenses
;
627 static const NWidgetPart _nested_operating_profit_widgets
[] = {
628 NWidget(NWID_HORIZONTAL
),
629 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
630 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_GRAPH_OPERATING_PROFIT_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
631 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
),
632 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
633 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
634 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
636 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_CV_BACKGROUND
),
637 NWidget(NWID_HORIZONTAL
),
638 NWidget(WWT_EMPTY
, COLOUR_GREY
, WID_CV_GRAPH
), SetMinimalSize(576, 160), SetFill(1, 1), SetResize(1, 1),
639 NWidget(NWID_VERTICAL
),
640 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
641 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
, WID_CV_RESIZE
),
647 static WindowDesc
_operating_profit_desc(
648 WDP_AUTO
, "graph_operating_profit", 0, 0,
649 WC_OPERATING_PROFIT
, WC_NONE
,
651 _nested_operating_profit_widgets
, lengthof(_nested_operating_profit_widgets
)
655 void ShowOperatingProfitGraph()
657 AllocateWindowDescFront
<OperatingProfitGraphWindow
>(&_operating_profit_desc
, 0);
665 struct IncomeGraphWindow
: BaseGraphWindow
{
666 IncomeGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
667 BaseGraphWindow(desc
, WID_CV_GRAPH
, STR_JUST_CURRENCY_SHORT
)
669 this->InitializeWindow(window_number
);
672 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
674 return c
->old_economy
[j
].income
;
678 static const NWidgetPart _nested_income_graph_widgets
[] = {
679 NWidget(NWID_HORIZONTAL
),
680 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
681 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_GRAPH_INCOME_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
682 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
),
683 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
684 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
685 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
687 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_CV_BACKGROUND
),
688 NWidget(NWID_HORIZONTAL
),
689 NWidget(WWT_EMPTY
, COLOUR_GREY
, WID_CV_GRAPH
), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
690 NWidget(NWID_VERTICAL
),
691 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
692 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
, WID_CV_RESIZE
),
698 static WindowDesc
_income_graph_desc(
699 WDP_AUTO
, "graph_income", 0, 0,
700 WC_INCOME_GRAPH
, WC_NONE
,
702 _nested_income_graph_widgets
, lengthof(_nested_income_graph_widgets
)
705 void ShowIncomeGraph()
707 AllocateWindowDescFront
<IncomeGraphWindow
>(&_income_graph_desc
, 0);
710 /*******************/
711 /* DELIVERED CARGO */
712 /*******************/
714 struct DeliveredCargoGraphWindow
: BaseGraphWindow
{
715 DeliveredCargoGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
716 BaseGraphWindow(desc
, WID_CV_GRAPH
, STR_JUST_COMMA
)
718 this->InitializeWindow(window_number
);
721 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
723 return c
->old_economy
[j
].delivered_cargo
.GetSum
<OverflowSafeInt64
>();
727 static const NWidgetPart _nested_delivered_cargo_graph_widgets
[] = {
728 NWidget(NWID_HORIZONTAL
),
729 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
730 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_GRAPH_CARGO_DELIVERED_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
731 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
),
732 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
733 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
734 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
736 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_CV_BACKGROUND
),
737 NWidget(NWID_HORIZONTAL
),
738 NWidget(WWT_EMPTY
, COLOUR_GREY
, WID_CV_GRAPH
), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
739 NWidget(NWID_VERTICAL
),
740 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
741 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
, WID_CV_RESIZE
),
747 static WindowDesc
_delivered_cargo_graph_desc(
748 WDP_AUTO
, "graph_delivered_cargo", 0, 0,
749 WC_DELIVERED_CARGO
, WC_NONE
,
751 _nested_delivered_cargo_graph_widgets
, lengthof(_nested_delivered_cargo_graph_widgets
)
754 void ShowDeliveredCargoGraph()
756 AllocateWindowDescFront
<DeliveredCargoGraphWindow
>(&_delivered_cargo_graph_desc
, 0);
759 /***********************/
760 /* PERFORMANCE HISTORY */
761 /***********************/
763 struct PerformanceHistoryGraphWindow
: BaseGraphWindow
{
764 PerformanceHistoryGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
765 BaseGraphWindow(desc
, WID_PHG_GRAPH
, STR_JUST_COMMA
)
767 this->InitializeWindow(window_number
);
770 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
772 return c
->old_economy
[j
].performance_history
;
775 void OnClick(Point pt
, int widget
, int click_count
) override
777 if (widget
== WID_PHG_DETAILED_PERFORMANCE
) ShowPerformanceRatingDetail();
778 this->BaseGraphWindow::OnClick(pt
, widget
, click_count
);
782 static const NWidgetPart _nested_performance_history_widgets
[] = {
783 NWidget(NWID_HORIZONTAL
),
784 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
785 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_GRAPH_COMPANY_PERFORMANCE_RATINGS_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
786 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
),
787 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
),
788 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
789 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
790 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
792 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_PHG_BACKGROUND
),
793 NWidget(NWID_HORIZONTAL
),
794 NWidget(WWT_EMPTY
, COLOUR_GREY
, WID_PHG_GRAPH
), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
795 NWidget(NWID_VERTICAL
),
796 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
797 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
, WID_PHG_RESIZE
),
803 static WindowDesc
_performance_history_desc(
804 WDP_AUTO
, "graph_performance", 0, 0,
805 WC_PERFORMANCE_HISTORY
, WC_NONE
,
807 _nested_performance_history_widgets
, lengthof(_nested_performance_history_widgets
)
810 void ShowPerformanceHistoryGraph()
812 AllocateWindowDescFront
<PerformanceHistoryGraphWindow
>(&_performance_history_desc
, 0);
819 struct CompanyValueGraphWindow
: BaseGraphWindow
{
820 CompanyValueGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
821 BaseGraphWindow(desc
, WID_CV_GRAPH
, STR_JUST_CURRENCY_SHORT
)
823 this->InitializeWindow(window_number
);
826 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
828 return c
->old_economy
[j
].company_value
;
832 static const NWidgetPart _nested_company_value_graph_widgets
[] = {
833 NWidget(NWID_HORIZONTAL
),
834 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
835 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_GRAPH_COMPANY_VALUES_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
836 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
),
837 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
838 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
839 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
841 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_CV_BACKGROUND
),
842 NWidget(NWID_HORIZONTAL
),
843 NWidget(WWT_EMPTY
, COLOUR_GREY
, WID_CV_GRAPH
), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
844 NWidget(NWID_VERTICAL
),
845 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
846 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
, WID_CV_RESIZE
),
852 static WindowDesc
_company_value_graph_desc(
853 WDP_AUTO
, "graph_company_value", 0, 0,
854 WC_COMPANY_VALUE
, WC_NONE
,
856 _nested_company_value_graph_widgets
, lengthof(_nested_company_value_graph_widgets
)
859 void ShowCompanyValueGraph()
861 AllocateWindowDescFront
<CompanyValueGraphWindow
>(&_company_value_graph_desc
, 0);
868 struct PaymentRatesGraphWindow
: BaseGraphWindow
{
869 uint line_height
; ///< Pixel height of each cargo type row.
870 Scrollbar
*vscroll
; ///< Cargo list scrollbar.
872 PaymentRatesGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
873 BaseGraphWindow(desc
, WID_CPR_GRAPH
, STR_JUST_CURRENCY_SHORT
)
875 this->num_on_x_axis
= 20;
876 this->num_vert_lines
= 20;
878 this->x_values_start
= 10;
879 this->x_values_increment
= 10;
881 this->CreateNestedTree();
882 this->vscroll
= this->GetScrollbar(WID_CPR_MATRIX_SCROLLBAR
);
883 this->vscroll
->SetCount(_sorted_standard_cargo_specs_size
);
885 /* Initialise the dataset */
886 this->OnHundredthTick();
888 this->FinishInitNested(window_number
);
891 void UpdateExcludedData()
893 this->excluded_data
= 0;
897 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
898 if (HasBit(_legend_excluded_cargo
, cs
->Index())) SetBit(this->excluded_data
, i
);
903 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
905 if (widget
!= WID_CPR_MATRIX
) {
906 BaseGraphWindow::UpdateWidgetSize(widget
, size
, padding
, fill
, resize
);
911 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
912 SetDParam(0, cs
->name
);
913 Dimension d
= GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO
);
914 d
.width
+= 14; // colour field
915 d
.width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
916 d
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
917 *size
= maxdim(d
, *size
);
920 this->line_height
= size
->height
;
921 size
->height
= this->line_height
* 11; /* Default number of cargo types in most climates. */
923 resize
->height
= this->line_height
;
926 void DrawWidget(const Rect
&r
, int widget
) const override
928 if (widget
!= WID_CPR_MATRIX
) {
929 BaseGraphWindow::DrawWidget(r
, widget
);
933 bool rtl
= _current_text_dir
== TD_RTL
;
935 int x
= r
.left
+ WD_FRAMERECT_LEFT
;
938 int pos
= this->vscroll
->GetPosition();
939 int max
= pos
+ this->vscroll
->GetCapacity();
942 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
943 if (pos
-- > 0) continue;
944 if (--max
< 0) break;
946 bool lowered
= !HasBit(_legend_excluded_cargo
, cs
->Index());
948 /* Redraw box if lowered */
949 if (lowered
) DrawFrameRect(r
.left
, y
, r
.right
, y
+ this->line_height
- 1, COLOUR_ORANGE
, lowered
? FR_LOWERED
: FR_NONE
);
951 byte clk_dif
= lowered
? 1 : 0;
952 int rect_x
= clk_dif
+ (rtl
? r
.right
- 12 : r
.left
+ WD_FRAMERECT_LEFT
);
954 GfxFillRect(rect_x
, y
+ clk_dif
, rect_x
+ 8, y
+ 5 + clk_dif
, PC_BLACK
);
955 GfxFillRect(rect_x
+ 1, y
+ 1 + clk_dif
, rect_x
+ 7, y
+ 4 + clk_dif
, cs
->legend_colour
);
956 SetDParam(0, cs
->name
);
957 DrawString(rtl
? r
.left
: x
+ 14 + clk_dif
, (rtl
? r
.right
- 14 + clk_dif
: r
.right
), y
+ clk_dif
, STR_GRAPH_CARGO_PAYMENT_CARGO
);
959 y
+= this->line_height
;
963 void OnClick(Point pt
, int widget
, int click_count
) override
966 case WID_CPR_ENABLE_CARGOES
:
967 /* Remove all cargoes from the excluded lists. */
968 _legend_excluded_cargo
= 0;
969 this->excluded_data
= 0;
973 case WID_CPR_DISABLE_CARGOES
: {
974 /* Add all cargoes to the excluded lists. */
977 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
978 SetBit(_legend_excluded_cargo
, cs
->Index());
979 SetBit(this->excluded_data
, i
);
986 case WID_CPR_MATRIX
: {
987 uint row
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_CPR_MATRIX
, 0, this->line_height
);
988 if (row
>= this->vscroll
->GetCount()) return;
991 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
992 if (row
-- > 0) continue;
994 ToggleBit(_legend_excluded_cargo
, cs
->Index());
995 this->UpdateExcludedData();
1004 void OnResize() override
1006 this->vscroll
->SetCapacityFromWidget(this, WID_CPR_MATRIX
);
1009 void OnGameTick() override
1011 /* Override default OnGameTick */
1015 * Some data on this window has become invalid.
1016 * @param data Information about the changed data.
1017 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1019 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1021 if (!gui_scope
) return;
1022 this->OnHundredthTick();
1025 void OnHundredthTick() override
1027 this->UpdateExcludedData();
1030 const CargoSpec
*cs
;
1031 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
1032 this->colours
[i
] = cs
->legend_colour
;
1033 for (uint j
= 0; j
!= 20; j
++) {
1034 this->cost
[i
][j
] = GetTransportedGoodsIncome(10, 20, j
* 4 + 4, cs
->Index());
1038 this->num_dataset
= i
;
1042 static const NWidgetPart _nested_cargo_payment_rates_widgets
[] = {
1043 NWidget(NWID_HORIZONTAL
),
1044 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
1045 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1046 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
1047 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
1048 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
1050 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_CPR_BACKGROUND
), SetMinimalSize(568, 128),
1051 NWidget(NWID_HORIZONTAL
),
1052 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1053 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
),
1054 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1056 NWidget(NWID_HORIZONTAL
),
1057 NWidget(WWT_EMPTY
, COLOUR_GREY
, WID_CPR_GRAPH
), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
1058 NWidget(NWID_VERTICAL
),
1059 NWidget(NWID_SPACER
), SetMinimalSize(0, 24), SetFill(0, 1),
1060 NWidget(WWT_PUSHTXTBTN
, COLOUR_ORANGE
, WID_CPR_ENABLE_CARGOES
), SetDataTip(STR_GRAPH_CARGO_ENABLE_ALL
, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL
), SetFill(1, 0),
1061 NWidget(WWT_PUSHTXTBTN
, COLOUR_ORANGE
, WID_CPR_DISABLE_CARGOES
), SetDataTip(STR_GRAPH_CARGO_DISABLE_ALL
, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL
), SetFill(1, 0),
1062 NWidget(NWID_SPACER
), SetMinimalSize(0, 4),
1063 NWidget(NWID_HORIZONTAL
),
1064 NWidget(WWT_MATRIX
, COLOUR_ORANGE
, WID_CPR_MATRIX
), SetResize(0, 2), SetMatrixDataTip(1, 0, STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO
), SetScrollbar(WID_CPR_MATRIX_SCROLLBAR
),
1065 NWidget(NWID_VSCROLLBAR
, COLOUR_ORANGE
, WID_CPR_MATRIX_SCROLLBAR
),
1067 NWidget(NWID_SPACER
), SetMinimalSize(0, 24), SetFill(0, 1),
1069 NWidget(NWID_SPACER
), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
1071 NWidget(NWID_HORIZONTAL
),
1072 NWidget(NWID_SPACER
), SetMinimalSize(WD_RESIZEBOX_WIDTH
, 0), SetFill(1, 0), SetResize(1, 0),
1073 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
),
1074 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1075 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
, WID_CPR_RESIZE
),
1080 static WindowDesc
_cargo_payment_rates_desc(
1081 WDP_AUTO
, "graph_cargo_payment_rates", 0, 0,
1082 WC_PAYMENT_RATES
, WC_NONE
,
1084 _nested_cargo_payment_rates_widgets
, lengthof(_nested_cargo_payment_rates_widgets
)
1088 void ShowCargoPaymentRates()
1090 AllocateWindowDescFront
<PaymentRatesGraphWindow
>(&_cargo_payment_rates_desc
, 0);
1093 /************************/
1094 /* COMPANY LEAGUE TABLE */
1095 /************************/
1097 static const StringID _performance_titles
[] = {
1098 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER
,
1099 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER
,
1100 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER
,
1101 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER
,
1102 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR
,
1103 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR
,
1104 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR
,
1105 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR
,
1106 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR
,
1107 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR
,
1108 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE
,
1109 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE
,
1110 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN
,
1111 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN
,
1112 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT
,
1113 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON
,
1116 static inline StringID
GetPerformanceTitleFromValue(uint value
)
1118 return _performance_titles
[minu(value
, 1000) >> 6];
1121 class CompanyLeagueWindow
: public Window
{
1123 GUIList
<const Company
*> companies
;
1124 uint ordinal_width
; ///< The width of the ordinal number
1125 uint text_width
; ///< The width of the actual text
1126 uint icon_width
; ///< The width of the company icon
1127 int line_height
; ///< Height of the text lines
1130 * (Re)Build the company league list
1132 void BuildCompanyList()
1134 if (!this->companies
.NeedRebuild()) return;
1136 this->companies
.clear();
1138 for (const Company
*c
: Company::Iterate()) {
1139 this->companies
.push_back(c
);
1142 this->companies
.shrink_to_fit();
1143 this->companies
.RebuildDone();
1146 /** Sort the company league by performance history */
1147 static bool PerformanceSorter(const Company
* const &c1
, const Company
* const &c2
)
1149 return c2
->old_economy
[0].performance_history
< c1
->old_economy
[0].performance_history
;
1153 CompanyLeagueWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
1155 this->InitNested(window_number
);
1156 this->companies
.ForceRebuild();
1157 this->companies
.NeedResort();
1160 void OnPaint() override
1162 this->BuildCompanyList();
1163 this->companies
.Sort(&PerformanceSorter
);
1165 this->DrawWidgets();
1168 void DrawWidget(const Rect
&r
, int widget
) const override
1170 if (widget
!= WID_CL_BACKGROUND
) return;
1172 int icon_y_offset
= 1 + (FONT_HEIGHT_NORMAL
- this->line_height
) / 2;
1173 uint y
= r
.top
+ WD_FRAMERECT_TOP
- icon_y_offset
;
1175 bool rtl
= _current_text_dir
== TD_RTL
;
1176 uint ordinal_left
= rtl
? r
.right
- WD_FRAMERECT_LEFT
- this->ordinal_width
: r
.left
+ WD_FRAMERECT_LEFT
;
1177 uint ordinal_right
= rtl
? r
.right
- WD_FRAMERECT_LEFT
: r
.left
+ WD_FRAMERECT_LEFT
+ this->ordinal_width
;
1178 uint icon_left
= r
.left
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
+ (rtl
? this->text_width
: this->ordinal_width
);
1179 uint text_left
= rtl
? r
.left
+ WD_FRAMERECT_LEFT
: r
.right
- WD_FRAMERECT_LEFT
- this->text_width
;
1180 uint text_right
= rtl
? r
.left
+ WD_FRAMERECT_LEFT
+ this->text_width
: r
.right
- WD_FRAMERECT_LEFT
;
1182 for (uint i
= 0; i
!= this->companies
.size(); i
++) {
1183 const Company
*c
= this->companies
[i
];
1184 DrawString(ordinal_left
, ordinal_right
, y
, i
+ STR_ORDINAL_NUMBER_1ST
, i
== 0 ? TC_WHITE
: TC_YELLOW
);
1186 DrawCompanyIcon(c
->index
, icon_left
, y
+ icon_y_offset
);
1188 SetDParam(0, c
->index
);
1189 SetDParam(1, c
->index
);
1190 SetDParam(2, GetPerformanceTitleFromValue(c
->old_economy
[0].performance_history
));
1191 DrawString(text_left
, text_right
, y
, STR_COMPANY_LEAGUE_COMPANY_NAME
);
1192 y
+= this->line_height
;
1196 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
1198 if (widget
!= WID_CL_BACKGROUND
) return;
1200 this->ordinal_width
= 0;
1201 for (uint i
= 0; i
< MAX_COMPANIES
; i
++) {
1202 this->ordinal_width
= max(this->ordinal_width
, GetStringBoundingBox(STR_ORDINAL_NUMBER_1ST
+ i
).width
);
1204 this->ordinal_width
+= 5; // Keep some extra spacing
1206 uint widest_width
= 0;
1207 uint widest_title
= 0;
1208 for (uint i
= 0; i
< lengthof(_performance_titles
); i
++) {
1209 uint width
= GetStringBoundingBox(_performance_titles
[i
]).width
;
1210 if (width
> widest_width
) {
1212 widest_width
= width
;
1216 Dimension d
= GetSpriteSize(SPR_COMPANY_ICON
);
1217 this->icon_width
= d
.width
+ 2;
1218 this->line_height
= max
<int>(d
.height
+ 2, FONT_HEIGHT_NORMAL
);
1220 for (const Company
*c
: Company::Iterate()) {
1221 SetDParam(0, c
->index
);
1222 SetDParam(1, c
->index
);
1223 SetDParam(2, _performance_titles
[widest_title
]);
1224 widest_width
= max(widest_width
, GetStringBoundingBox(STR_COMPANY_LEAGUE_COMPANY_NAME
).width
);
1227 this->text_width
= widest_width
+ 30; // Keep some extra spacing
1229 size
->width
= WD_FRAMERECT_LEFT
+ this->ordinal_width
+ WD_FRAMERECT_RIGHT
+ this->icon_width
+ WD_FRAMERECT_LEFT
+ this->text_width
+ WD_FRAMERECT_RIGHT
;
1230 size
->height
= WD_FRAMERECT_TOP
+ this->line_height
* MAX_COMPANIES
+ WD_FRAMERECT_BOTTOM
;
1234 void OnGameTick() override
1236 if (this->companies
.NeedResort()) {
1242 * Some data on this window has become invalid.
1243 * @param data Information about the changed data.
1244 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1246 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1249 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
1250 this->companies
.ForceRebuild();
1252 this->companies
.ForceResort();
1257 static const NWidgetPart _nested_company_league_widgets
[] = {
1258 NWidget(NWID_HORIZONTAL
),
1259 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
1260 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_COMPANY_LEAGUE_TABLE_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1261 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
1262 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
1264 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_CL_BACKGROUND
), SetMinimalSize(400, 0), SetMinimalTextLines(15, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
),
1267 static WindowDesc
_company_league_desc(
1268 WDP_AUTO
, "league", 0, 0,
1269 WC_COMPANY_LEAGUE
, WC_NONE
,
1271 _nested_company_league_widgets
, lengthof(_nested_company_league_widgets
)
1274 void ShowCompanyLeagueTable()
1276 AllocateWindowDescFront
<CompanyLeagueWindow
>(&_company_league_desc
, 0);
1279 /*****************************/
1280 /* PERFORMANCE RATING DETAIL */
1281 /*****************************/
1283 struct PerformanceRatingDetailWindow
: Window
{
1284 static CompanyID company
;
1287 PerformanceRatingDetailWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
1289 this->UpdateCompanyStats();
1291 this->InitNested(window_number
);
1292 this->OnInvalidateData(INVALID_COMPANY
);
1295 void UpdateCompanyStats()
1297 /* Update all company stats with the current data
1298 * (this is because _score_info is not saved to a savegame) */
1299 for (Company
*c
: Company::Iterate()) {
1300 UpdateCompanyRatingAndValue(c
, false);
1303 this->timeout
= DAY_TICKS
* 5;
1306 uint score_info_left
;
1307 uint score_info_right
;
1312 uint score_detail_left
;
1313 uint score_detail_right
;
1315 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
1318 case WID_PRD_SCORE_FIRST
:
1319 this->bar_height
= FONT_HEIGHT_NORMAL
+ 4;
1320 size
->height
= this->bar_height
+ 2 * WD_MATRIX_TOP
;
1322 uint score_info_width
= 0;
1323 for (uint i
= SCORE_BEGIN
; i
< SCORE_END
; i
++) {
1324 score_info_width
= max(score_info_width
, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES
+ i
).width
);
1326 SetDParamMaxValue(0, 1000);
1327 score_info_width
+= GetStringBoundingBox(STR_BLACK_COMMA
).width
+ WD_FRAMERECT_LEFT
;
1329 SetDParamMaxValue(0, 100);
1330 this->bar_width
= GetStringBoundingBox(STR_PERFORMANCE_DETAIL_PERCENT
).width
+ 20; // Wide bars!
1332 /* At this number we are roughly at the max; it can become wider,
1333 * but then you need at 1000 times more money. At that time you're
1334 * not that interested anymore in the last few digits anyway.
1335 * The 500 is because 999 999 500 to 999 999 999 are rounded to
1336 * 1 000 M, and not 999 999 k. Use negative numbers to account for
1337 * the negative income/amount of money etc. as well. */
1338 int max
= -(999999999 - 500);
1340 /* Scale max for the display currency. Prior to rendering the value
1341 * is converted into the display currency, which may cause it to
1342 * raise significantly. We need to compensate for that since {{CURRCOMPACT}}
1343 * is used, which can produce quite short renderings of very large
1344 * values. Otherwise the calculated width could be too narrow.
1345 * Note that it doesn't work if there was a currency with an exchange
1346 * rate greater than max.
1347 * When the currency rate is more than 1000, the 999 999 k becomes at
1348 * least 999 999 M which roughly is equally long. Furthermore if the
1349 * exchange rate is that high, 999 999 k is usually not enough anymore
1350 * to show the different currency numbers. */
1351 if (_currency
->rate
< 1000) max
/= _currency
->rate
;
1354 uint score_detail_width
= GetStringBoundingBox(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY
).width
;
1356 size
->width
= 7 + score_info_width
+ 5 + this->bar_width
+ 5 + score_detail_width
+ 7;
1358 uint right
= size
->width
- 7;
1360 bool rtl
= _current_text_dir
== TD_RTL
;
1361 this->score_info_left
= rtl
? right
- score_info_width
: left
;
1362 this->score_info_right
= rtl
? right
: left
+ score_info_width
;
1364 this->score_detail_left
= rtl
? left
: right
- score_detail_width
;
1365 this->score_detail_right
= rtl
? left
+ score_detail_width
: right
;
1367 this->bar_left
= left
+ (rtl
? score_detail_width
: score_info_width
) + 5;
1368 this->bar_right
= this->bar_left
+ this->bar_width
;
1373 void DrawWidget(const Rect
&r
, int widget
) const override
1375 /* No need to draw when there's nothing to draw */
1376 if (this->company
== INVALID_COMPANY
) return;
1378 if (IsInsideMM(widget
, WID_PRD_COMPANY_FIRST
, WID_PRD_COMPANY_LAST
+ 1)) {
1379 if (this->IsWidgetDisabled(widget
)) return;
1380 CompanyID cid
= (CompanyID
)(widget
- WID_PRD_COMPANY_FIRST
);
1381 int offset
= (cid
== this->company
) ? 1 : 0;
1382 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
);
1383 DrawCompanyIcon(cid
, (r
.left
+ r
.right
- sprite_size
.width
) / 2 + offset
, (r
.top
+ r
.bottom
- sprite_size
.height
) / 2 + offset
);
1387 if (!IsInsideMM(widget
, WID_PRD_SCORE_FIRST
, WID_PRD_SCORE_LAST
+ 1)) return;
1389 ScoreID score_type
= (ScoreID
)(widget
- WID_PRD_SCORE_FIRST
);
1391 /* The colours used to show how the progress is going */
1392 int colour_done
= _colour_gradient
[COLOUR_GREEN
][4];
1393 int colour_notdone
= _colour_gradient
[COLOUR_RED
][4];
1395 /* Draw all the score parts */
1396 int64 val
= _score_part
[company
][score_type
];
1397 int64 needed
= _score_info
[score_type
].needed
;
1398 int score
= _score_info
[score_type
].score
;
1400 /* SCORE_TOTAL has his own rules ;) */
1401 if (score_type
== SCORE_TOTAL
) {
1402 for (ScoreID i
= SCORE_BEGIN
; i
< SCORE_END
; i
++) score
+= _score_info
[i
].score
;
1406 uint bar_top
= r
.top
+ WD_MATRIX_TOP
;
1407 uint text_top
= bar_top
+ 2;
1409 DrawString(this->score_info_left
, this->score_info_right
, text_top
, STR_PERFORMANCE_DETAIL_VEHICLES
+ score_type
);
1411 /* Draw the score */
1412 SetDParam(0, score
);
1413 DrawString(this->score_info_left
, this->score_info_right
, text_top
, STR_BLACK_COMMA
, TC_FROMSTRING
, SA_RIGHT
);
1415 /* Calculate the %-bar */
1416 uint x
= Clamp
<int64
>(val
, 0, needed
) * this->bar_width
/ needed
;
1417 bool rtl
= _current_text_dir
== TD_RTL
;
1419 x
= this->bar_right
- x
;
1421 x
= this->bar_left
+ x
;
1425 if (x
!= this->bar_left
) GfxFillRect(this->bar_left
, bar_top
, x
, bar_top
+ this->bar_height
, rtl
? colour_notdone
: colour_done
);
1426 if (x
!= this->bar_right
) GfxFillRect(x
, bar_top
, this->bar_right
, bar_top
+ this->bar_height
, rtl
? colour_done
: colour_notdone
);
1429 SetDParam(0, Clamp
<int64
>(val
, 0, needed
) * 100 / needed
);
1430 DrawString(this->bar_left
, this->bar_right
, text_top
, STR_PERFORMANCE_DETAIL_PERCENT
, TC_FROMSTRING
, SA_HOR_CENTER
);
1432 /* SCORE_LOAN is inversed */
1433 if (score_type
== SCORE_LOAN
) val
= needed
- val
;
1435 /* Draw the amount we have against what is needed
1436 * For some of them it is in currency format */
1438 SetDParam(1, needed
);
1439 switch (score_type
) {
1440 case SCORE_MIN_PROFIT
:
1441 case SCORE_MIN_INCOME
:
1442 case SCORE_MAX_INCOME
:
1445 DrawString(this->score_detail_left
, this->score_detail_right
, text_top
, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY
);
1448 DrawString(this->score_detail_left
, this->score_detail_right
, text_top
, STR_PERFORMANCE_DETAIL_AMOUNT_INT
);
1452 void OnClick(Point pt
, int widget
, int click_count
) override
1454 /* Check which button is clicked */
1455 if (IsInsideMM(widget
, WID_PRD_COMPANY_FIRST
, WID_PRD_COMPANY_LAST
+ 1)) {
1456 /* Is it no on disable? */
1457 if (!this->IsWidgetDisabled(widget
)) {
1458 this->RaiseWidget(this->company
+ WID_PRD_COMPANY_FIRST
);
1459 this->company
= (CompanyID
)(widget
- WID_PRD_COMPANY_FIRST
);
1460 this->LowerWidget(this->company
+ WID_PRD_COMPANY_FIRST
);
1466 void OnGameTick() override
1468 /* Update the company score every 5 days */
1469 if (--this->timeout
== 0) {
1470 this->UpdateCompanyStats();
1476 * Some data on this window has become invalid.
1477 * @param data the company ID of the company that is going to be removed
1478 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1480 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1482 if (!gui_scope
) return;
1483 /* Disable the companies who are not active */
1484 for (CompanyID i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
1485 this->SetWidgetDisabledState(i
+ WID_PRD_COMPANY_FIRST
, !Company::IsValidID(i
));
1488 /* Check if the currently selected company is still active. */
1489 if (this->company
!= INVALID_COMPANY
&& !Company::IsValidID(this->company
)) {
1490 /* Raise the widget for the previous selection. */
1491 this->RaiseWidget(this->company
+ WID_PRD_COMPANY_FIRST
);
1492 this->company
= INVALID_COMPANY
;
1495 if (this->company
== INVALID_COMPANY
) {
1496 for (const Company
*c
: Company::Iterate()) {
1497 this->company
= c
->index
;
1502 /* Make sure the widget is lowered */
1503 this->LowerWidget(this->company
+ WID_PRD_COMPANY_FIRST
);
1507 CompanyID
PerformanceRatingDetailWindow::company
= INVALID_COMPANY
;
1510 * Make a vertical list of panels for outputting score details.
1511 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
1512 * @return Panel with performance details.
1513 * @post \c *biggest_index contains the largest used index in the tree.
1515 static NWidgetBase
*MakePerformanceDetailPanels(int *biggest_index
)
1517 const StringID performance_tips
[] = {
1518 STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP
,
1519 STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP
,
1520 STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP
,
1521 STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP
,
1522 STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP
,
1523 STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP
,
1524 STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP
,
1525 STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP
,
1526 STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP
,
1527 STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP
,
1530 assert_compile(lengthof(performance_tips
) == SCORE_END
- SCORE_BEGIN
);
1532 NWidgetVertical
*vert
= new NWidgetVertical(NC_EQUALSIZE
);
1533 for (int widnum
= WID_PRD_SCORE_FIRST
; widnum
<= WID_PRD_SCORE_LAST
; widnum
++) {
1534 NWidgetBackground
*panel
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, widnum
);
1535 panel
->SetFill(1, 1);
1536 panel
->SetDataTip(0x0, performance_tips
[widnum
- WID_PRD_SCORE_FIRST
]);
1539 *biggest_index
= WID_PRD_SCORE_LAST
;
1543 /** Make a number of rows with buttons for each company for the performance rating detail window. */
1544 NWidgetBase
*MakeCompanyButtonRowsGraphGUI(int *biggest_index
)
1546 return MakeCompanyButtonRows(biggest_index
, WID_PRD_COMPANY_FIRST
, WID_PRD_COMPANY_LAST
, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP
);
1549 static const NWidgetPart _nested_performance_rating_detail_widgets
[] = {
1550 NWidget(NWID_HORIZONTAL
),
1551 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
1552 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_PERFORMANCE_DETAIL
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1553 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
1554 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
1556 NWidget(WWT_PANEL
, COLOUR_GREY
),
1557 NWidgetFunction(MakeCompanyButtonRowsGraphGUI
), SetPadding(0, 1, 1, 2),
1559 NWidgetFunction(MakePerformanceDetailPanels
),
1562 static WindowDesc
_performance_rating_detail_desc(
1563 WDP_AUTO
, "league_details", 0, 0,
1564 WC_PERFORMANCE_DETAIL
, WC_NONE
,
1566 _nested_performance_rating_detail_widgets
, lengthof(_nested_performance_rating_detail_widgets
)
1569 void ShowPerformanceRatingDetail()
1571 AllocateWindowDescFront
<PerformanceRatingDetailWindow
>(&_performance_rating_detail_desc
, 0);
1574 void InitializeGraphGui()
1576 _legend_excluded_companies
= 0;
1577 _legend_excluded_cargo
= 0;