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"
20 #include "core/geometry_func.hpp"
22 #include "timer/timer.h"
23 #include "timer/timer_window.h"
24 #include "timer/timer_game_tick.h"
25 #include "timer/timer_game_economy.h"
26 #include "zoom_func.h"
29 #include "widgets/graph_widget.h"
31 #include "table/strings.h"
32 #include "table/sprites.h"
34 #include "safeguards.h"
36 /* Bitmasks of company and cargo indices that shouldn't be drawn. */
37 static CompanyMask _legend_excluded_companies
;
38 static CargoTypes _legend_excluded_cargo_payment_rates
;
39 static CargoTypes _legend_excluded_cargo_production_history
;
41 /* Apparently these don't play well with enums. */
42 static const OverflowSafeInt64
INVALID_DATAPOINT(INT64_MAX
); // Value used for a datapoint that shouldn't be drawn.
43 static const uint INVALID_DATAPOINT_POS
= UINT_MAX
; // Used to determine if the previous point was drawn.
45 constexpr double INT64_MAX_IN_DOUBLE
= static_cast<double>(INT64_MAX
- 512); ///< The biggest double that when cast to int64_t still fits in a int64_t.
46 static_assert(static_cast<int64_t>(INT64_MAX_IN_DOUBLE
) < INT64_MAX
);
52 struct GraphLegendWindow
: Window
{
53 GraphLegendWindow(WindowDesc
&desc
, WindowNumber window_number
) : Window(desc
)
55 this->InitNested(window_number
);
57 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
58 if (!HasBit(_legend_excluded_companies
, c
)) this->LowerWidget(WID_GL_FIRST_COMPANY
+ c
);
60 this->OnInvalidateData(c
);
64 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
66 if (!IsInsideMM(widget
, WID_GL_FIRST_COMPANY
, WID_GL_FIRST_COMPANY
+ MAX_COMPANIES
)) return;
68 CompanyID cid
= (CompanyID
)(widget
- WID_GL_FIRST_COMPANY
);
70 if (!Company::IsValidID(cid
)) return;
72 bool rtl
= _current_text_dir
== TD_RTL
;
74 const Rect ir
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
75 Dimension d
= GetSpriteSize(SPR_COMPANY_ICON
);
76 DrawCompanyIcon(cid
, rtl
? ir
.right
- d
.width
: ir
.left
, CenterBounds(ir
.top
, ir
.bottom
, d
.height
));
78 const Rect tr
= ir
.Indent(d
.width
+ WidgetDimensions::scaled
.hsep_normal
, rtl
);
81 DrawString(tr
.left
, tr
.right
, CenterBounds(tr
.top
, tr
.bottom
, GetCharacterHeight(FS_NORMAL
)), STR_COMPANY_NAME_COMPANY_NUM
, HasBit(_legend_excluded_companies
, cid
) ? TC_BLACK
: TC_WHITE
);
84 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
86 if (!IsInsideMM(widget
, WID_GL_FIRST_COMPANY
, WID_GL_FIRST_COMPANY
+ MAX_COMPANIES
)) return;
88 ToggleBit(_legend_excluded_companies
, widget
- WID_GL_FIRST_COMPANY
);
89 this->ToggleWidgetLoweredState(widget
);
91 InvalidateWindowData(WC_INCOME_GRAPH
, 0);
92 InvalidateWindowData(WC_OPERATING_PROFIT
, 0);
93 InvalidateWindowData(WC_DELIVERED_CARGO
, 0);
94 InvalidateWindowData(WC_PERFORMANCE_HISTORY
, 0);
95 InvalidateWindowData(WC_COMPANY_VALUE
, 0);
99 * Some data on this window has become invalid.
100 * @param data Information about the changed data.
101 * @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.
103 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
105 if (!gui_scope
) return;
106 if (Company::IsValidID(data
)) return;
108 SetBit(_legend_excluded_companies
, data
);
109 this->RaiseWidget(data
+ WID_GL_FIRST_COMPANY
);
114 * Construct a vertical list of buttons, one for each company.
115 * @return Panel with company buttons.
117 static std::unique_ptr
<NWidgetBase
> MakeNWidgetCompanyLines()
119 auto vert
= std::make_unique
<NWidgetVertical
>(NC_EQUALSIZE
);
120 vert
->SetPadding(2, 2, 2, 2);
121 uint sprite_height
= GetSpriteSize(SPR_COMPANY_ICON
, nullptr, ZOOM_LVL_NORMAL
).height
;
123 for (WidgetID widnum
= WID_GL_FIRST_COMPANY
; widnum
<= WID_GL_LAST_COMPANY
; widnum
++) {
124 auto panel
= std::make_unique
<NWidgetBackground
>(WWT_PANEL
, COLOUR_BROWN
, widnum
);
125 panel
->SetMinimalSize(246, sprite_height
+ WidgetDimensions::unscaled
.framerect
.Vertical());
126 panel
->SetMinimalTextLines(1, WidgetDimensions::unscaled
.framerect
.Vertical(), FS_NORMAL
);
127 panel
->SetFill(1, 1);
128 panel
->SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION_TOOLTIP
);
129 vert
->Add(std::move(panel
));
134 static constexpr NWidgetPart _nested_graph_legend_widgets
[] = {
135 NWidget(NWID_HORIZONTAL
),
136 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
137 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_KEY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
138 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
139 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
141 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GL_BACKGROUND
),
142 NWidgetFunction(MakeNWidgetCompanyLines
),
146 static WindowDesc
_graph_legend_desc(
147 WDP_AUTO
, "graph_legend", 0, 0,
148 WC_GRAPH_LEGEND
, WC_NONE
,
150 _nested_graph_legend_widgets
153 static void ShowGraphLegend()
155 AllocateWindowDescFront
<GraphLegendWindow
>(_graph_legend_desc
, 0);
158 /** Contains the interval of a graph's data. */
159 struct ValuesInterval
{
160 OverflowSafeInt64 highest
; ///< Highest value of this interval. Must be zero or greater.
161 OverflowSafeInt64 lowest
; ///< Lowest value of this interval. Must be zero or less.
168 struct BaseGraphWindow
: Window
{
170 static const int GRAPH_MAX_DATASETS
= 64;
171 static const int GRAPH_BASE_COLOUR
= GREY_SCALE(2);
172 static const int GRAPH_GRID_COLOUR
= GREY_SCALE(3);
173 static const int GRAPH_AXIS_LINE_COLOUR
= GREY_SCALE(1);
174 static const int GRAPH_ZERO_LINE_COLOUR
= GREY_SCALE(8);
175 static const int GRAPH_YEAR_LINE_COLOUR
= GREY_SCALE(5);
176 static const int GRAPH_NUM_MONTHS
= 24; ///< Number of months displayed in the graph.
177 static const int PAYMENT_GRAPH_X_STEP_DAYS
= 10; ///< X-axis step label for cargo payment rates "Days in transit".
178 static const int PAYMENT_GRAPH_X_STEP_SECONDS
= 20; ///< X-axis step label for cargo payment rates "Seconds in transit".
179 static const int ECONOMY_QUARTER_MINUTES
= 3; ///< Minutes per economic quarter.
180 static const int ECONOMY_MONTH_MINUTES
= 1; ///< Minutes per economic month.
182 static const TextColour GRAPH_AXIS_LABEL_COLOUR
= TC_BLACK
; ///< colour of the graph axis label.
184 static const int MIN_GRAPH_NUM_LINES_Y
= 9; ///< Minimal number of horizontal lines to draw.
185 static const int MIN_GRID_PIXEL_SIZE
= 20; ///< Minimum distance between graph lines.
187 uint64_t excluded_data
; ///< bitmask of the datasets that shouldn't be displayed.
189 uint8_t num_on_x_axis
;
190 uint8_t num_vert_lines
;
192 /* The starting month and year that values are plotted against. */
193 TimerGameEconomy::Month month
;
194 TimerGameEconomy::Year year
;
195 uint8_t month_increment
; ///< month increment between vertical lines. must be divisor of 12.
197 bool draw_dates
= true; ///< Should we draw months and years on the time axis?
199 /* These values are used if the graph is being plotted against values
200 * rather than the dates specified by month and year. */
201 uint16_t x_values_start
;
202 int16_t x_values_increment
;
204 StringID format_str_y_axis
;
205 uint8_t colours
[GRAPH_MAX_DATASETS
];
206 OverflowSafeInt64 cost
[GRAPH_MAX_DATASETS
][GRAPH_NUM_MONTHS
]; ///< Stored costs for the last #GRAPH_NUM_MONTHS months
209 * Get the interval that contains the graph's data. Excluded data is ignored to show smaller values in
210 * better detail when disabling higher ones.
211 * @param num_hori_lines Number of horizontal lines to be drawn.
212 * @return Highest and lowest values of the graph (ignoring disabled data).
214 ValuesInterval
GetValuesInterval(int num_hori_lines
) const
216 assert(num_hori_lines
> 0);
218 ValuesInterval current_interval
;
219 current_interval
.highest
= INT64_MIN
;
220 current_interval
.lowest
= INT64_MAX
;
222 for (int i
= 0; i
< this->num_dataset
; i
++) {
223 if (HasBit(this->excluded_data
, i
)) continue;
224 for (int j
= 0; j
< this->num_on_x_axis
; j
++) {
225 OverflowSafeInt64 datapoint
= this->cost
[i
][j
];
227 if (datapoint
!= INVALID_DATAPOINT
) {
228 current_interval
.highest
= std::max(current_interval
.highest
, datapoint
);
229 current_interval
.lowest
= std::min(current_interval
.lowest
, datapoint
);
234 /* Always include zero in the shown range. */
235 double abs_lower
= (current_interval
.lowest
> 0) ? 0 : (double)abs(current_interval
.lowest
);
236 double abs_higher
= (current_interval
.highest
< 0) ? 0 : (double)current_interval
.highest
;
238 /* Prevent showing values too close to the graph limits. */
239 abs_higher
= (11.0 * abs_higher
) / 10.0;
240 abs_lower
= (11.0 * abs_lower
) / 10.0;
243 OverflowSafeInt64 grid_size
;
245 if (abs_lower
!= 0 || abs_higher
!= 0) {
246 /* The number of grids to reserve for the positive part is: */
247 num_pos_grids
= (int)floor(0.5 + num_hori_lines
* abs_higher
/ (abs_higher
+ abs_lower
));
249 /* If there are any positive or negative values, force that they have at least one grid. */
250 if (num_pos_grids
== 0 && abs_higher
!= 0) num_pos_grids
++;
251 if (num_pos_grids
== num_hori_lines
&& abs_lower
!= 0) num_pos_grids
--;
253 /* Get the required grid size for each side and use the maximum one. */
255 OverflowSafeInt64 grid_size_higher
= 0;
256 if (abs_higher
> 0) {
257 grid_size_higher
= abs_higher
> INT64_MAX_IN_DOUBLE
? INT64_MAX
: static_cast<int64_t>(abs_higher
);
258 grid_size_higher
= (grid_size_higher
+ num_pos_grids
- 1) / num_pos_grids
;
261 OverflowSafeInt64 grid_size_lower
= 0;
263 grid_size_lower
= abs_lower
> INT64_MAX_IN_DOUBLE
? INT64_MAX
: static_cast<int64_t>(abs_lower
);
264 grid_size_lower
= (grid_size_lower
+ num_hori_lines
- num_pos_grids
- 1) / (num_hori_lines
- num_pos_grids
);
267 grid_size
= std::max(grid_size_higher
, grid_size_lower
);
269 /* If both values are zero, show an empty graph. */
270 num_pos_grids
= num_hori_lines
/ 2;
274 current_interval
.highest
= num_pos_grids
* grid_size
;
275 current_interval
.lowest
= -(num_hori_lines
- num_pos_grids
) * grid_size
;
276 return current_interval
;
280 * Get width for Y labels.
281 * @param current_interval Interval that contains all of the graph data.
282 * @param num_hori_lines Number of horizontal lines to be drawn.
284 uint
GetYLabelWidth(ValuesInterval current_interval
, int num_hori_lines
) const
286 /* draw text strings on the y axis */
287 int64_t y_label
= current_interval
.highest
;
288 int64_t y_label_separation
= (current_interval
.highest
- current_interval
.lowest
) / num_hori_lines
;
292 for (int i
= 0; i
< (num_hori_lines
+ 1); i
++) {
293 SetDParam(0, this->format_str_y_axis
);
294 SetDParam(1, y_label
);
295 Dimension d
= GetStringBoundingBox(STR_GRAPH_Y_LABEL
);
296 if (d
.width
> max_width
) max_width
= d
.width
;
298 y_label
-= y_label_separation
;
305 * Actually draw the graph.
306 * @param r the rectangle of the data field of the graph
308 void DrawGraph(Rect r
) const
310 uint x
, y
; ///< Reused whenever x and y coordinates are needed.
311 ValuesInterval interval
; ///< Interval that contains all of the graph data.
312 int x_axis_offset
; ///< Distance from the top of the graph to the x axis.
314 /* the colours and cost array of GraphDrawer must accommodate
315 * both values for cargo and companies. So if any are higher, quit */
316 static_assert(GRAPH_MAX_DATASETS
>= (int)NUM_CARGO
&& GRAPH_MAX_DATASETS
>= (int)MAX_COMPANIES
);
317 assert(this->num_vert_lines
> 0);
319 /* Rect r will be adjusted to contain just the graph, with labels being
320 * placed outside the area. */
321 r
.top
+= ScaleGUITrad(5) + GetCharacterHeight(FS_SMALL
) / 2;
322 r
.bottom
-= (this->draw_dates
? 2 : 1) * GetCharacterHeight(FS_SMALL
) + ScaleGUITrad(4);
323 r
.left
+= ScaleGUITrad(9);
324 r
.right
-= ScaleGUITrad(5);
326 /* Initial number of horizontal lines. */
327 int num_hori_lines
= 160 / ScaleGUITrad(MIN_GRID_PIXEL_SIZE
);
328 /* For the rest of the height, the number of horizontal lines will increase more slowly. */
329 int resize
= (r
.bottom
- r
.top
- 160) / (2 * ScaleGUITrad(MIN_GRID_PIXEL_SIZE
));
330 if (resize
> 0) num_hori_lines
+= resize
;
332 interval
= GetValuesInterval(num_hori_lines
);
334 int label_width
= GetYLabelWidth(interval
, num_hori_lines
);
336 r
.left
+= label_width
;
338 int x_sep
= (r
.right
- r
.left
) / this->num_vert_lines
;
339 int y_sep
= (r
.bottom
- r
.top
) / num_hori_lines
;
341 /* Redetermine right and bottom edge of graph to fit with the integer
342 * separation values. */
343 r
.right
= r
.left
+ x_sep
* this->num_vert_lines
;
344 r
.bottom
= r
.top
+ y_sep
* num_hori_lines
;
346 OverflowSafeInt64 interval_size
= interval
.highest
+ abs(interval
.lowest
);
347 /* Where to draw the X axis. Use floating point to avoid overflowing and results of zero. */
348 x_axis_offset
= (int)((r
.bottom
- r
.top
) * (double)interval
.highest
/ (double)interval_size
);
350 /* Draw the background of the graph itself. */
351 GfxFillRect(r
.left
, r
.top
, r
.right
, r
.bottom
, GRAPH_BASE_COLOUR
);
353 /* Draw the vertical grid lines. */
355 /* Don't draw the first line, as that's where the axis will be. */
358 int grid_colour
= GRAPH_GRID_COLOUR
;
359 for (int i
= 1; i
< this->num_vert_lines
+ 1; i
++) {
360 /* If using wallclock units, we separate periods with a lighter line. */
361 if (TimerGameEconomy::UsingWallclockUnits()) {
362 grid_colour
= (i
% 4 == 0) ? GRAPH_YEAR_LINE_COLOUR
: GRAPH_GRID_COLOUR
;
364 GfxFillRect(x
, r
.top
, x
, r
.bottom
, grid_colour
);
368 /* Draw the horizontal grid lines. */
371 for (int i
= 0; i
< (num_hori_lines
+ 1); i
++) {
372 GfxFillRect(r
.left
- ScaleGUITrad(3), y
, r
.left
- 1, y
, GRAPH_AXIS_LINE_COLOUR
);
373 GfxFillRect(r
.left
, y
, r
.right
, y
, GRAPH_GRID_COLOUR
);
377 /* Draw the y axis. */
378 GfxFillRect(r
.left
, r
.top
, r
.left
, r
.bottom
, GRAPH_AXIS_LINE_COLOUR
);
380 /* Draw the x axis. */
381 y
= x_axis_offset
+ r
.top
;
382 GfxFillRect(r
.left
, y
, r
.right
, y
, GRAPH_ZERO_LINE_COLOUR
);
384 /* Find the largest value that will be drawn. */
385 if (this->num_on_x_axis
== 0) return;
387 assert(this->num_on_x_axis
> 0);
389 /* draw text strings on the y axis */
390 int64_t y_label
= interval
.highest
;
391 int64_t y_label_separation
= abs(interval
.highest
- interval
.lowest
) / num_hori_lines
;
393 y
= r
.top
- GetCharacterHeight(FS_SMALL
) / 2;
395 for (int i
= 0; i
< (num_hori_lines
+ 1); i
++) {
396 SetDParam(0, this->format_str_y_axis
);
397 SetDParam(1, y_label
);
398 DrawString(r
.left
- label_width
- ScaleGUITrad(4), r
.left
- ScaleGUITrad(4), y
, STR_GRAPH_Y_LABEL
, GRAPH_AXIS_LABEL_COLOUR
, SA_RIGHT
);
400 y_label
-= y_label_separation
;
404 /* Draw x-axis labels and markings for graphs based on financial quarters and years. */
405 if (this->draw_dates
) {
407 y
= r
.bottom
+ ScaleGUITrad(2);
408 TimerGameEconomy::Month month
= this->month
;
409 TimerGameEconomy::Year year
= this->year
;
410 for (int i
= 0; i
< this->num_on_x_axis
; i
++) {
411 SetDParam(0, month
+ STR_MONTH_ABBREV_JAN
);
413 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
, SA_LEFT
);
415 month
+= this->month_increment
;
420 /* Draw a lighter grid line between years. Top and bottom adjustments ensure we don't draw over top and bottom horizontal grid lines. */
421 GfxFillRect(x
+ x_sep
, r
.top
+ 1, x
+ x_sep
, r
.bottom
- 1, GRAPH_YEAR_LINE_COLOUR
);
426 /* Draw x-axis labels for graphs not based on quarterly performance (cargo payment rates, and all graphs when using wallclock units). */
428 y
= r
.bottom
+ ScaleGUITrad(2);
429 uint16_t label
= this->x_values_start
;
431 for (int i
= 0; i
< this->num_on_x_axis
; i
++) {
433 DrawString(x
+ 1, x
+ x_sep
- 1, y
, STR_GRAPH_Y_LABEL_NUMBER
, GRAPH_AXIS_LABEL_COLOUR
, SA_HOR_CENTER
);
435 label
+= this->x_values_increment
;
440 /* draw lines and dots */
441 uint linewidth
= _settings_client
.gui
.graph_line_thickness
;
442 uint pointoffs1
= (linewidth
+ 1) / 2;
443 uint pointoffs2
= linewidth
+ 1 - pointoffs1
;
444 for (int i
= 0; i
< this->num_dataset
; i
++) {
445 if (!HasBit(this->excluded_data
, i
)) {
446 /* Centre the dot between the grid lines. */
447 x
= r
.left
+ (x_sep
/ 2);
449 uint8_t colour
= this->colours
[i
];
450 uint prev_x
= INVALID_DATAPOINT_POS
;
451 uint prev_y
= INVALID_DATAPOINT_POS
;
453 for (int j
= 0; j
< this->num_on_x_axis
; j
++) {
454 OverflowSafeInt64 datapoint
= this->cost
[i
][j
];
456 if (datapoint
!= INVALID_DATAPOINT
) {
458 * Check whether we need to reduce the 'accuracy' of the
459 * datapoint value and the highest value to split overflows.
460 * And when 'drawing' 'one million' or 'one million and one'
461 * there is no significant difference, so the least
462 * significant bits can just be removed.
464 * If there are more bits needed than would fit in a 32 bits
465 * integer, so at about 31 bits because of the sign bit, the
466 * least significant bits are removed.
468 int mult_range
= FindLastBit
<uint32_t>(x_axis_offset
) + FindLastBit
<uint64_t>(abs(datapoint
));
469 int reduce_range
= std::max(mult_range
- 31, 0);
471 /* Handle negative values differently (don't shift sign) */
473 datapoint
= -(abs(datapoint
) >> reduce_range
);
475 datapoint
>>= reduce_range
;
477 y
= r
.top
+ x_axis_offset
- ((r
.bottom
- r
.top
) * datapoint
) / (interval_size
>> reduce_range
);
479 /* Draw the point. */
480 GfxFillRect(x
- pointoffs1
, y
- pointoffs1
, x
+ pointoffs2
, y
+ pointoffs2
, colour
);
482 /* Draw the line connected to the previous point. */
483 if (prev_x
!= INVALID_DATAPOINT_POS
) GfxDrawLine(prev_x
, prev_y
, x
, y
, colour
, linewidth
);
488 prev_x
= INVALID_DATAPOINT_POS
;
489 prev_y
= INVALID_DATAPOINT_POS
;
499 BaseGraphWindow(WindowDesc
&desc
, StringID format_str_y_axis
) :
501 format_str_y_axis(format_str_y_axis
)
503 SetWindowDirty(WC_GRAPH_LEGEND
, 0);
504 this->num_vert_lines
= GRAPH_NUM_MONTHS
;
505 this->month_increment
= 3;
508 void InitializeWindow(WindowNumber number
)
510 /* Initialise the dataset */
511 this->UpdateStatistics(true);
513 this->CreateNestedTree();
515 auto *wid
= this->GetWidget
<NWidgetCore
>(WID_GRAPH_FOOTER
);
516 if (wid
!= nullptr && TimerGameEconomy::UsingWallclockUnits()) {
517 wid
->SetDataTip(STR_GRAPH_LAST_72_MINUTES_TIME_LABEL
, STR_NULL
);
520 this->FinishInitNested(number
);
524 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
526 if (widget
!= WID_GRAPH_GRAPH
) return;
528 uint x_label_width
= 0;
530 /* Draw x-axis labels and markings for graphs based on financial quarters and years. */
531 if (this->draw_dates
) {
532 TimerGameEconomy::Month month
= this->month
;
533 TimerGameEconomy::Year year
= this->year
;
534 for (int i
= 0; i
< this->num_on_x_axis
; i
++) {
535 SetDParam(0, month
+ STR_MONTH_ABBREV_JAN
);
537 x_label_width
= std::max(x_label_width
, GetStringBoundingBox(month
== 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR
: STR_GRAPH_X_LABEL_MONTH
).width
);
539 month
+= this->month_increment
;
546 /* Draw x-axis labels for graphs not based on quarterly performance (cargo payment rates). */
547 SetDParamMaxValue(0, this->x_values_start
+ this->num_on_x_axis
* this->x_values_increment
, 0, FS_SMALL
);
548 x_label_width
= GetStringBoundingBox(STR_GRAPH_Y_LABEL_NUMBER
).width
;
551 SetDParam(0, this->format_str_y_axis
);
552 SetDParam(1, INT64_MAX
);
553 uint y_label_width
= GetStringBoundingBox(STR_GRAPH_Y_LABEL
).width
;
555 size
.width
= std::max
<uint
>(size
.width
, ScaleGUITrad(5) + y_label_width
+ this->num_vert_lines
* (x_label_width
+ ScaleGUITrad(5)) + ScaleGUITrad(9));
556 size
.height
= std::max
<uint
>(size
.height
, ScaleGUITrad(5) + (1 + MIN_GRAPH_NUM_LINES_Y
* 2 + (this->draw_dates
? 3 : 1)) * GetCharacterHeight(FS_SMALL
) + ScaleGUITrad(4));
557 size
.height
= std::max
<uint
>(size
.height
, size
.width
/ 3);
560 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
562 if (widget
!= WID_GRAPH_GRAPH
) return;
567 virtual OverflowSafeInt64
GetGraphData(const Company
*, int)
569 return INVALID_DATAPOINT
;
572 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
574 /* Clicked on legend? */
575 if (widget
== WID_GRAPH_KEY_BUTTON
) ShowGraphLegend();
578 void OnGameTick() override
580 this->UpdateStatistics(false);
584 * Some data on this window has become invalid.
585 * @param data Information about the changed data.
586 * @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.
588 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
590 if (!gui_scope
) return;
591 this->UpdateStatistics(true);
595 * Update the statistics.
596 * @param initialize Initialize the data structure.
598 virtual void UpdateStatistics(bool initialize
)
600 CompanyMask excluded_companies
= _legend_excluded_companies
;
602 /* Exclude the companies which aren't valid */
603 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
604 if (!Company::IsValidID(c
)) SetBit(excluded_companies
, c
);
608 for (const Company
*c
: Company::Iterate()) {
609 nums
= std::min(this->num_vert_lines
, std::max(nums
, c
->num_valid_stat_ent
));
612 int mo
= (TimerGameEconomy::month
/ this->month_increment
- nums
) * this->month_increment
;
613 auto yr
= TimerGameEconomy::year
;
619 if (!initialize
&& this->excluded_data
== excluded_companies
&& this->num_on_x_axis
== nums
&&
620 this->year
== yr
&& this->month
== mo
) {
621 /* There's no reason to get new stats */
625 this->excluded_data
= excluded_companies
;
626 this->num_on_x_axis
= nums
;
631 for (CompanyID k
= COMPANY_FIRST
; k
< MAX_COMPANIES
; k
++) {
632 const Company
*c
= Company::GetIfValid(k
);
634 this->colours
[numd
] = GetColourGradient(c
->colour
, SHADE_LIGHTER
);
635 for (int j
= this->num_on_x_axis
, i
= 0; --j
>= 0;) {
636 if (j
>= c
->num_valid_stat_ent
) {
637 this->cost
[numd
][i
] = INVALID_DATAPOINT
;
639 /* Ensure we never assign INVALID_DATAPOINT, as that has another meaning.
640 * Instead, use the value just under it. Hopefully nobody will notice. */
641 this->cost
[numd
][i
] = std::min(GetGraphData(c
, j
), INVALID_DATAPOINT
- 1);
649 this->num_dataset
= numd
;
654 /********************/
655 /* OPERATING PROFIT */
656 /********************/
658 struct OperatingProfitGraphWindow
: BaseGraphWindow
{
659 OperatingProfitGraphWindow(WindowDesc
&desc
, WindowNumber window_number
) :
660 BaseGraphWindow(desc
, STR_JUST_CURRENCY_SHORT
)
662 this->num_on_x_axis
= GRAPH_NUM_MONTHS
;
663 this->num_vert_lines
= GRAPH_NUM_MONTHS
;
664 this->x_values_start
= ECONOMY_QUARTER_MINUTES
;
665 this->x_values_increment
= ECONOMY_QUARTER_MINUTES
;
666 this->draw_dates
= !TimerGameEconomy::UsingWallclockUnits();
668 this->InitializeWindow(window_number
);
671 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
673 return c
->old_economy
[j
].income
+ c
->old_economy
[j
].expenses
;
677 static constexpr NWidgetPart _nested_operating_profit_widgets
[] = {
678 NWidget(NWID_HORIZONTAL
),
679 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
680 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_OPERATING_PROFIT_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
681 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GRAPH_KEY_BUTTON
), SetMinimalSize(50, 0), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
682 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
683 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
684 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
686 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GRAPH_BACKGROUND
),
687 NWidget(NWID_VERTICAL
),
688 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_GRAPH_GRAPH
), SetMinimalSize(576, 160), SetFill(1, 1), SetResize(1, 1),
689 NWidget(NWID_HORIZONTAL
),
690 NWidget(NWID_SPACER
), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
691 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_GRAPH_FOOTER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_EMPTY
, STR_NULL
),
692 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
693 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_GRAPH_RESIZE
), SetDataTip(RWV_HIDE_BEVEL
, STR_TOOLTIP_RESIZE
),
699 static WindowDesc
_operating_profit_desc(
700 WDP_AUTO
, "graph_operating_profit", 0, 0,
701 WC_OPERATING_PROFIT
, WC_NONE
,
703 _nested_operating_profit_widgets
707 void ShowOperatingProfitGraph()
709 AllocateWindowDescFront
<OperatingProfitGraphWindow
>(_operating_profit_desc
, 0);
717 struct IncomeGraphWindow
: BaseGraphWindow
{
718 IncomeGraphWindow(WindowDesc
&desc
, WindowNumber window_number
) :
719 BaseGraphWindow(desc
, STR_JUST_CURRENCY_SHORT
)
721 this->num_on_x_axis
= GRAPH_NUM_MONTHS
;
722 this->num_vert_lines
= GRAPH_NUM_MONTHS
;
723 this->x_values_start
= ECONOMY_QUARTER_MINUTES
;
724 this->x_values_increment
= ECONOMY_QUARTER_MINUTES
;
725 this->draw_dates
= !TimerGameEconomy::UsingWallclockUnits();
727 this->InitializeWindow(window_number
);
730 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
732 return c
->old_economy
[j
].income
;
736 static constexpr NWidgetPart _nested_income_graph_widgets
[] = {
737 NWidget(NWID_HORIZONTAL
),
738 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
739 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_INCOME_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
740 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GRAPH_KEY_BUTTON
), SetMinimalSize(50, 0), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
741 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
742 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
743 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
745 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GRAPH_BACKGROUND
),
746 NWidget(NWID_VERTICAL
),
747 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_GRAPH_GRAPH
), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
748 NWidget(NWID_HORIZONTAL
),
749 NWidget(NWID_SPACER
), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
750 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_GRAPH_FOOTER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_EMPTY
, STR_NULL
),
751 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
752 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_GRAPH_RESIZE
), SetDataTip(RWV_HIDE_BEVEL
, STR_TOOLTIP_RESIZE
),
758 static WindowDesc
_income_graph_desc(
759 WDP_AUTO
, "graph_income", 0, 0,
760 WC_INCOME_GRAPH
, WC_NONE
,
762 _nested_income_graph_widgets
765 void ShowIncomeGraph()
767 AllocateWindowDescFront
<IncomeGraphWindow
>(_income_graph_desc
, 0);
770 /*******************/
771 /* DELIVERED CARGO */
772 /*******************/
774 struct DeliveredCargoGraphWindow
: BaseGraphWindow
{
775 DeliveredCargoGraphWindow(WindowDesc
&desc
, WindowNumber window_number
) :
776 BaseGraphWindow(desc
, STR_JUST_COMMA
)
778 this->num_on_x_axis
= GRAPH_NUM_MONTHS
;
779 this->num_vert_lines
= GRAPH_NUM_MONTHS
;
780 this->x_values_start
= ECONOMY_QUARTER_MINUTES
;
781 this->x_values_increment
= ECONOMY_QUARTER_MINUTES
;
782 this->draw_dates
= !TimerGameEconomy::UsingWallclockUnits();
784 this->InitializeWindow(window_number
);
787 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
789 return c
->old_economy
[j
].delivered_cargo
.GetSum
<OverflowSafeInt64
>();
793 static constexpr NWidgetPart _nested_delivered_cargo_graph_widgets
[] = {
794 NWidget(NWID_HORIZONTAL
),
795 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
796 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_CARGO_DELIVERED_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
797 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GRAPH_KEY_BUTTON
), SetMinimalSize(50, 0), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
798 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
799 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
800 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
802 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GRAPH_BACKGROUND
),
803 NWidget(NWID_VERTICAL
),
804 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_GRAPH_GRAPH
), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
805 NWidget(NWID_HORIZONTAL
),
806 NWidget(NWID_SPACER
), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
807 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_GRAPH_FOOTER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_EMPTY
, STR_NULL
),
808 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
809 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_GRAPH_RESIZE
), SetDataTip(RWV_HIDE_BEVEL
, STR_TOOLTIP_RESIZE
),
815 static WindowDesc
_delivered_cargo_graph_desc(
816 WDP_AUTO
, "graph_delivered_cargo", 0, 0,
817 WC_DELIVERED_CARGO
, WC_NONE
,
819 _nested_delivered_cargo_graph_widgets
822 void ShowDeliveredCargoGraph()
824 AllocateWindowDescFront
<DeliveredCargoGraphWindow
>(_delivered_cargo_graph_desc
, 0);
827 /***********************/
828 /* PERFORMANCE HISTORY */
829 /***********************/
831 struct PerformanceHistoryGraphWindow
: BaseGraphWindow
{
832 PerformanceHistoryGraphWindow(WindowDesc
&desc
, WindowNumber window_number
) :
833 BaseGraphWindow(desc
, STR_JUST_COMMA
)
835 this->num_on_x_axis
= GRAPH_NUM_MONTHS
;
836 this->num_vert_lines
= GRAPH_NUM_MONTHS
;
837 this->x_values_start
= ECONOMY_QUARTER_MINUTES
;
838 this->x_values_increment
= ECONOMY_QUARTER_MINUTES
;
839 this->draw_dates
= !TimerGameEconomy::UsingWallclockUnits();
841 this->InitializeWindow(window_number
);
844 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
846 return c
->old_economy
[j
].performance_history
;
849 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
851 if (widget
== WID_PHG_DETAILED_PERFORMANCE
) ShowPerformanceRatingDetail();
852 this->BaseGraphWindow::OnClick(pt
, widget
, click_count
);
856 static constexpr NWidgetPart _nested_performance_history_widgets
[] = {
857 NWidget(NWID_HORIZONTAL
),
858 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
859 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_COMPANY_PERFORMANCE_RATINGS_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
860 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_PHG_DETAILED_PERFORMANCE
), SetMinimalSize(50, 0), SetDataTip(STR_PERFORMANCE_DETAIL_KEY
, STR_GRAPH_PERFORMANCE_DETAIL_TOOLTIP
),
861 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GRAPH_KEY_BUTTON
), SetMinimalSize(50, 0), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
862 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
863 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
864 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
866 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GRAPH_BACKGROUND
),
867 NWidget(NWID_VERTICAL
),
868 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_GRAPH_GRAPH
), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
869 NWidget(NWID_HORIZONTAL
),
870 NWidget(NWID_SPACER
), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
871 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_GRAPH_FOOTER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_EMPTY
, STR_NULL
),
872 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
873 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_GRAPH_RESIZE
), SetDataTip(RWV_HIDE_BEVEL
, STR_TOOLTIP_RESIZE
),
879 static WindowDesc
_performance_history_desc(
880 WDP_AUTO
, "graph_performance", 0, 0,
881 WC_PERFORMANCE_HISTORY
, WC_NONE
,
883 _nested_performance_history_widgets
886 void ShowPerformanceHistoryGraph()
888 AllocateWindowDescFront
<PerformanceHistoryGraphWindow
>(_performance_history_desc
, 0);
895 struct CompanyValueGraphWindow
: BaseGraphWindow
{
896 CompanyValueGraphWindow(WindowDesc
&desc
, WindowNumber window_number
) :
897 BaseGraphWindow(desc
, STR_JUST_CURRENCY_SHORT
)
899 this->num_on_x_axis
= GRAPH_NUM_MONTHS
;
900 this->num_vert_lines
= GRAPH_NUM_MONTHS
;
901 this->x_values_start
= ECONOMY_QUARTER_MINUTES
;
902 this->x_values_increment
= ECONOMY_QUARTER_MINUTES
;
903 this->draw_dates
= !TimerGameEconomy::UsingWallclockUnits();
905 this->InitializeWindow(window_number
);
908 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
910 return c
->old_economy
[j
].company_value
;
914 static constexpr NWidgetPart _nested_company_value_graph_widgets
[] = {
915 NWidget(NWID_HORIZONTAL
),
916 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
917 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_COMPANY_VALUES_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
918 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GRAPH_KEY_BUTTON
), SetMinimalSize(50, 0), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
919 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
920 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
921 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
923 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GRAPH_BACKGROUND
),
924 NWidget(NWID_VERTICAL
),
925 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_GRAPH_GRAPH
), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
926 NWidget(NWID_HORIZONTAL
),
927 NWidget(NWID_SPACER
), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
928 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_GRAPH_FOOTER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_EMPTY
, STR_NULL
),
929 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
930 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_GRAPH_RESIZE
), SetDataTip(RWV_HIDE_BEVEL
, STR_TOOLTIP_RESIZE
),
936 static WindowDesc
_company_value_graph_desc(
937 WDP_AUTO
, "graph_company_value", 0, 0,
938 WC_COMPANY_VALUE
, WC_NONE
,
940 _nested_company_value_graph_widgets
943 void ShowCompanyValueGraph()
945 AllocateWindowDescFront
<CompanyValueGraphWindow
>(_company_value_graph_desc
, 0);
952 struct PaymentRatesGraphWindow
: BaseGraphWindow
{
953 uint line_height
; ///< Pixel height of each cargo type row.
954 Scrollbar
*vscroll
; ///< Cargo list scrollbar.
955 uint legend_width
; ///< Width of legend 'blob'.
957 PaymentRatesGraphWindow(WindowDesc
&desc
, WindowNumber window_number
) :
958 BaseGraphWindow(desc
, STR_JUST_CURRENCY_SHORT
)
960 this->num_on_x_axis
= 20;
961 this->num_vert_lines
= 20;
962 this->draw_dates
= false;
963 /* The x-axis is labeled in either seconds or days. A day is two seconds, so we adjust the label if needed. */
964 this->x_values_start
= (TimerGameEconomy::UsingWallclockUnits() ? PAYMENT_GRAPH_X_STEP_SECONDS
: PAYMENT_GRAPH_X_STEP_DAYS
);
965 this->x_values_increment
= (TimerGameEconomy::UsingWallclockUnits() ? PAYMENT_GRAPH_X_STEP_SECONDS
: PAYMENT_GRAPH_X_STEP_DAYS
);
967 this->CreateNestedTree();
968 this->vscroll
= this->GetScrollbar(WID_GRAPH_MATRIX_SCROLLBAR
);
969 this->vscroll
->SetCount(_sorted_standard_cargo_specs
.size());
971 auto *wid
= this->GetWidget
<NWidgetCore
>(WID_GRAPH_FOOTER
);
972 wid
->SetDataTip(TimerGameEconomy::UsingWallclockUnits() ? STR_GRAPH_CARGO_PAYMENT_RATES_SECONDS
: STR_GRAPH_CARGO_PAYMENT_RATES_DAYS
, STR_NULL
);
974 /* Initialise the dataset */
975 this->UpdatePaymentRates();
977 this->FinishInitNested(window_number
);
980 void OnInit() override
982 /* Width of the legend blob. */
983 this->legend_width
= GetCharacterHeight(FS_SMALL
) * 9 / 6;
986 void UpdateExcludedData()
988 this->excluded_data
= 0;
991 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
992 if (HasBit(_legend_excluded_cargo_payment_rates
, cs
->Index())) SetBit(this->excluded_data
, i
);
997 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
999 if (widget
!= WID_GRAPH_MATRIX
) {
1000 BaseGraphWindow::UpdateWidgetSize(widget
, size
, padding
, fill
, resize
);
1004 size
.height
= GetCharacterHeight(FS_SMALL
) + WidgetDimensions::scaled
.framerect
.Vertical();
1006 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
1007 SetDParam(0, cs
->name
);
1008 Dimension d
= GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO
);
1009 d
.width
+= this->legend_width
+ WidgetDimensions::scaled
.hsep_normal
; // colour field
1010 d
.width
+= WidgetDimensions::scaled
.framerect
.Horizontal();
1011 d
.height
+= WidgetDimensions::scaled
.framerect
.Vertical();
1012 size
= maxdim(d
, size
);
1015 this->line_height
= size
.height
;
1016 size
.height
= this->line_height
* 11; /* Default number of cargo types in most climates. */
1018 resize
.height
= this->line_height
;
1021 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
1023 if (widget
!= WID_GRAPH_MATRIX
) {
1024 BaseGraphWindow::DrawWidget(r
, widget
);
1028 bool rtl
= _current_text_dir
== TD_RTL
;
1030 auto [first
, last
] = this->vscroll
->GetVisibleRangeIterators(_sorted_standard_cargo_specs
);
1032 Rect line
= r
.WithHeight(this->line_height
);
1033 for (auto it
= first
; it
!= last
; ++it
) {
1034 const CargoSpec
*cs
= *it
;
1036 bool lowered
= !HasBit(_legend_excluded_cargo_payment_rates
, cs
->Index());
1038 /* Redraw frame if lowered */
1039 if (lowered
) DrawFrameRect(line
, COLOUR_BROWN
, FR_LOWERED
);
1041 const Rect text
= line
.Shrink(WidgetDimensions::scaled
.framerect
);
1043 /* Cargo-colour box with outline */
1044 const Rect cargo
= text
.WithWidth(this->legend_width
, rtl
);
1045 GfxFillRect(cargo
, PC_BLACK
);
1046 GfxFillRect(cargo
.Shrink(WidgetDimensions::scaled
.bevel
), cs
->legend_colour
);
1049 SetDParam(0, cs
->name
);
1050 DrawString(text
.Indent(this->legend_width
+ WidgetDimensions::scaled
.hsep_normal
, rtl
), STR_GRAPH_CARGO_PAYMENT_CARGO
);
1052 line
= line
.Translate(0, this->line_height
);
1056 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
1059 case WID_GRAPH_ENABLE_CARGOES
:
1060 /* Remove all cargoes from the excluded lists. */
1061 _legend_excluded_cargo_payment_rates
= 0;
1062 this->excluded_data
= 0;
1066 case WID_GRAPH_DISABLE_CARGOES
: {
1067 /* Add all cargoes to the excluded lists. */
1069 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
1070 SetBit(_legend_excluded_cargo_payment_rates
, cs
->Index());
1071 SetBit(this->excluded_data
, i
);
1078 case WID_GRAPH_MATRIX
: {
1079 auto it
= this->vscroll
->GetScrolledItemFromWidget(_sorted_standard_cargo_specs
, pt
.y
, this, WID_GRAPH_MATRIX
);
1080 if (it
!= _sorted_standard_cargo_specs
.end()) {
1081 ToggleBit(_legend_excluded_cargo_payment_rates
, (*it
)->Index());
1082 this->UpdateExcludedData();
1090 void OnResize() override
1092 this->vscroll
->SetCapacityFromWidget(this, WID_GRAPH_MATRIX
);
1095 void OnGameTick() override
1097 /* Override default OnGameTick */
1101 * Some data on this window has become invalid.
1102 * @param data Information about the changed data.
1103 * @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.
1105 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
1107 if (!gui_scope
) return;
1108 this->UpdatePaymentRates();
1111 /** Update the payment rates on a regular interval. */
1112 IntervalTimer
<TimerWindow
> update_payment_interval
= {std::chrono::seconds(3), [this](auto) {
1113 this->UpdatePaymentRates();
1117 * Update the payment rates according to the latest information.
1119 void UpdatePaymentRates()
1121 this->UpdateExcludedData();
1124 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
1125 this->colours
[i
] = cs
->legend_colour
;
1126 for (uint j
= 0; j
!= this->num_on_x_axis
; j
++) {
1127 this->cost
[i
][j
] = GetTransportedGoodsIncome(10, 20, j
* 4 + 4, cs
->Index());
1131 this->num_dataset
= i
;
1135 static constexpr NWidgetPart _nested_cargo_payment_rates_widgets
[] = {
1136 NWidget(NWID_HORIZONTAL
),
1137 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1138 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1139 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1140 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1141 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1143 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GRAPH_BACKGROUND
), SetMinimalSize(568, 128),
1144 NWidget(NWID_HORIZONTAL
),
1145 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1146 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_GRAPH_HEADER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_TITLE
, STR_NULL
),
1147 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1149 NWidget(NWID_HORIZONTAL
),
1150 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_GRAPH_GRAPH
), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
1151 NWidget(NWID_VERTICAL
),
1152 NWidget(NWID_SPACER
), SetMinimalSize(0, 24), SetFill(0, 1),
1153 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GRAPH_ENABLE_CARGOES
), SetDataTip(STR_GRAPH_CARGO_ENABLE_ALL
, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL
), SetFill(1, 0),
1154 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GRAPH_DISABLE_CARGOES
), SetDataTip(STR_GRAPH_CARGO_DISABLE_ALL
, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL
), SetFill(1, 0),
1155 NWidget(NWID_SPACER
), SetMinimalSize(0, 4),
1156 NWidget(NWID_HORIZONTAL
),
1157 NWidget(WWT_MATRIX
, COLOUR_BROWN
, WID_GRAPH_MATRIX
), SetFill(1, 0), SetResize(0, 2), SetMatrixDataTip(1, 0, STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO
), SetScrollbar(WID_GRAPH_MATRIX_SCROLLBAR
),
1158 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_GRAPH_MATRIX_SCROLLBAR
),
1160 NWidget(NWID_SPACER
), SetMinimalSize(0, 24), SetFill(0, 1),
1162 NWidget(NWID_SPACER
), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
1164 NWidget(NWID_HORIZONTAL
),
1165 NWidget(NWID_SPACER
), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
1166 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_GRAPH_FOOTER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_NULL
, STR_NULL
),
1167 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1168 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_GRAPH_RESIZE
), SetDataTip(RWV_HIDE_BEVEL
, STR_TOOLTIP_RESIZE
),
1173 static WindowDesc
_cargo_payment_rates_desc(
1174 WDP_AUTO
, "graph_cargo_payment_rates", 0, 0,
1175 WC_PAYMENT_RATES
, WC_NONE
,
1177 _nested_cargo_payment_rates_widgets
1181 void ShowCargoPaymentRates()
1183 AllocateWindowDescFront
<PaymentRatesGraphWindow
>(_cargo_payment_rates_desc
, 0);
1186 /*****************************/
1187 /* PERFORMANCE RATING DETAIL */
1188 /*****************************/
1190 struct PerformanceRatingDetailWindow
: Window
{
1191 static CompanyID company
;
1194 PerformanceRatingDetailWindow(WindowDesc
&desc
, WindowNumber window_number
) : Window(desc
)
1196 this->UpdateCompanyStats();
1198 this->InitNested(window_number
);
1199 this->OnInvalidateData(INVALID_COMPANY
);
1202 void UpdateCompanyStats()
1204 /* Update all company stats with the current data
1205 * (this is because _score_info is not saved to a savegame) */
1206 for (Company
*c
: Company::Iterate()) {
1207 UpdateCompanyRatingAndValue(c
, false);
1210 this->timeout
= Ticks::DAY_TICKS
* 5;
1213 uint score_info_left
;
1214 uint score_info_right
;
1219 uint score_detail_left
;
1220 uint score_detail_right
;
1222 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
1225 case WID_PRD_SCORE_FIRST
:
1226 this->bar_height
= GetCharacterHeight(FS_NORMAL
) + WidgetDimensions::scaled
.fullbevel
.Vertical();
1227 size
.height
= this->bar_height
+ WidgetDimensions::scaled
.matrix
.Vertical();
1229 uint score_info_width
= 0;
1230 for (uint i
= SCORE_BEGIN
; i
< SCORE_END
; i
++) {
1231 score_info_width
= std::max(score_info_width
, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES
+ i
).width
);
1233 SetDParamMaxValue(0, 1000);
1234 score_info_width
+= GetStringBoundingBox(STR_JUST_COMMA
).width
+ WidgetDimensions::scaled
.hsep_wide
;
1236 SetDParamMaxValue(0, 100);
1237 this->bar_width
= GetStringBoundingBox(STR_PERFORMANCE_DETAIL_PERCENT
).width
+ WidgetDimensions::scaled
.hsep_indent
* 2; // Wide bars!
1239 /* At this number we are roughly at the max; it can become wider,
1240 * but then you need at 1000 times more money. At that time you're
1241 * not that interested anymore in the last few digits anyway.
1242 * The 500 is because 999 999 500 to 999 999 999 are rounded to
1243 * 1 000 M, and not 999 999 k. Use negative numbers to account for
1244 * the negative income/amount of money etc. as well. */
1245 int max
= -(999999999 - 500);
1247 /* Scale max for the display currency. Prior to rendering the value
1248 * is converted into the display currency, which may cause it to
1249 * raise significantly. We need to compensate for that since {{CURRCOMPACT}}
1250 * is used, which can produce quite short renderings of very large
1251 * values. Otherwise the calculated width could be too narrow.
1252 * Note that it doesn't work if there was a currency with an exchange
1253 * rate greater than max.
1254 * When the currency rate is more than 1000, the 999 999 k becomes at
1255 * least 999 999 M which roughly is equally long. Furthermore if the
1256 * exchange rate is that high, 999 999 k is usually not enough anymore
1257 * to show the different currency numbers. */
1258 if (GetCurrency().rate
< 1000) max
/= GetCurrency().rate
;
1261 uint score_detail_width
= GetStringBoundingBox(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY
).width
;
1263 size
.width
= WidgetDimensions::scaled
.frametext
.Horizontal() + score_info_width
+ WidgetDimensions::scaled
.hsep_wide
+ this->bar_width
+ WidgetDimensions::scaled
.hsep_wide
+ score_detail_width
;
1264 uint left
= WidgetDimensions::scaled
.frametext
.left
;
1265 uint right
= size
.width
- WidgetDimensions::scaled
.frametext
.right
;
1267 bool rtl
= _current_text_dir
== TD_RTL
;
1268 this->score_info_left
= rtl
? right
- score_info_width
: left
;
1269 this->score_info_right
= rtl
? right
: left
+ score_info_width
;
1271 this->score_detail_left
= rtl
? left
: right
- score_detail_width
;
1272 this->score_detail_right
= rtl
? left
+ score_detail_width
: right
;
1274 this->bar_left
= left
+ (rtl
? score_detail_width
: score_info_width
) + WidgetDimensions::scaled
.hsep_wide
;
1275 this->bar_right
= this->bar_left
+ this->bar_width
- 1;
1280 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
1282 /* No need to draw when there's nothing to draw */
1283 if (this->company
== INVALID_COMPANY
) return;
1285 if (IsInsideMM(widget
, WID_PRD_COMPANY_FIRST
, WID_PRD_COMPANY_LAST
+ 1)) {
1286 if (this->IsWidgetDisabled(widget
)) return;
1287 CompanyID cid
= (CompanyID
)(widget
- WID_PRD_COMPANY_FIRST
);
1288 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
);
1289 DrawCompanyIcon(cid
, CenterBounds(r
.left
, r
.right
, sprite_size
.width
), CenterBounds(r
.top
, r
.bottom
, sprite_size
.height
));
1293 if (!IsInsideMM(widget
, WID_PRD_SCORE_FIRST
, WID_PRD_SCORE_LAST
+ 1)) return;
1295 ScoreID score_type
= (ScoreID
)(widget
- WID_PRD_SCORE_FIRST
);
1297 /* The colours used to show how the progress is going */
1298 int colour_done
= GetColourGradient(COLOUR_GREEN
, SHADE_NORMAL
);
1299 int colour_notdone
= GetColourGradient(COLOUR_RED
, SHADE_NORMAL
);
1301 /* Draw all the score parts */
1302 int64_t val
= _score_part
[company
][score_type
];
1303 int64_t needed
= _score_info
[score_type
].needed
;
1304 int score
= _score_info
[score_type
].score
;
1306 /* SCORE_TOTAL has its own rules ;) */
1307 if (score_type
== SCORE_TOTAL
) {
1308 for (ScoreID i
= SCORE_BEGIN
; i
< SCORE_END
; i
++) score
+= _score_info
[i
].score
;
1312 uint bar_top
= CenterBounds(r
.top
, r
.bottom
, this->bar_height
);
1313 uint text_top
= CenterBounds(r
.top
, r
.bottom
, GetCharacterHeight(FS_NORMAL
));
1315 DrawString(this->score_info_left
, this->score_info_right
, text_top
, STR_PERFORMANCE_DETAIL_VEHICLES
+ score_type
);
1317 /* Draw the score */
1318 SetDParam(0, score
);
1319 DrawString(this->score_info_left
, this->score_info_right
, text_top
, STR_JUST_COMMA
, TC_BLACK
, SA_RIGHT
);
1321 /* Calculate the %-bar */
1322 uint x
= Clamp
<int64_t>(val
, 0, needed
) * this->bar_width
/ needed
;
1323 bool rtl
= _current_text_dir
== TD_RTL
;
1325 x
= this->bar_right
- x
;
1327 x
= this->bar_left
+ x
;
1331 if (x
!= this->bar_left
) GfxFillRect(this->bar_left
, bar_top
, x
, bar_top
+ this->bar_height
- 1, rtl
? colour_notdone
: colour_done
);
1332 if (x
!= this->bar_right
) GfxFillRect(x
, bar_top
, this->bar_right
, bar_top
+ this->bar_height
- 1, rtl
? colour_done
: colour_notdone
);
1335 SetDParam(0, Clamp
<int64_t>(val
, 0, needed
) * 100 / needed
);
1336 DrawString(this->bar_left
, this->bar_right
, text_top
, STR_PERFORMANCE_DETAIL_PERCENT
, TC_FROMSTRING
, SA_HOR_CENTER
);
1338 /* SCORE_LOAN is inversed */
1339 if (score_type
== SCORE_LOAN
) val
= needed
- val
;
1341 /* Draw the amount we have against what is needed
1342 * For some of them it is in currency format */
1344 SetDParam(1, needed
);
1345 switch (score_type
) {
1346 case SCORE_MIN_PROFIT
:
1347 case SCORE_MIN_INCOME
:
1348 case SCORE_MAX_INCOME
:
1351 DrawString(this->score_detail_left
, this->score_detail_right
, text_top
, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY
);
1354 DrawString(this->score_detail_left
, this->score_detail_right
, text_top
, STR_PERFORMANCE_DETAIL_AMOUNT_INT
);
1358 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
1360 /* Check which button is clicked */
1361 if (IsInsideMM(widget
, WID_PRD_COMPANY_FIRST
, WID_PRD_COMPANY_LAST
+ 1)) {
1362 /* Is it no on disable? */
1363 if (!this->IsWidgetDisabled(widget
)) {
1364 this->RaiseWidget(WID_PRD_COMPANY_FIRST
+ this->company
);
1365 this->company
= (CompanyID
)(widget
- WID_PRD_COMPANY_FIRST
);
1366 this->LowerWidget(WID_PRD_COMPANY_FIRST
+ this->company
);
1372 void OnGameTick() override
1374 /* Update the company score every 5 days */
1375 if (--this->timeout
== 0) {
1376 this->UpdateCompanyStats();
1382 * Some data on this window has become invalid.
1383 * @param data the company ID of the company that is going to be removed
1384 * @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.
1386 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
1388 if (!gui_scope
) return;
1389 /* Disable the companies who are not active */
1390 for (CompanyID i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
1391 this->SetWidgetDisabledState(WID_PRD_COMPANY_FIRST
+ i
, !Company::IsValidID(i
));
1394 /* Check if the currently selected company is still active. */
1395 if (this->company
!= INVALID_COMPANY
&& !Company::IsValidID(this->company
)) {
1396 /* Raise the widget for the previous selection. */
1397 this->RaiseWidget(WID_PRD_COMPANY_FIRST
+ this->company
);
1398 this->company
= INVALID_COMPANY
;
1401 if (this->company
== INVALID_COMPANY
) {
1402 for (const Company
*c
: Company::Iterate()) {
1403 this->company
= c
->index
;
1408 /* Make sure the widget is lowered */
1409 if (this->company
!= INVALID_COMPANY
) {
1410 this->LowerWidget(WID_PRD_COMPANY_FIRST
+ this->company
);
1415 CompanyID
PerformanceRatingDetailWindow::company
= INVALID_COMPANY
;
1417 /*******************************/
1418 /* INDUSTRY PRODUCTION HISTORY */
1419 /*******************************/
1421 struct IndustryProductionGraphWindow
: BaseGraphWindow
{
1422 uint line_height
; ///< Pixel height of each cargo type row.
1423 Scrollbar
*vscroll
; ///< Cargo list scrollbar.
1424 uint legend_width
; ///< Width of legend 'blob'.
1426 IndustryProductionGraphWindow(WindowDesc
&desc
, WindowNumber window_number
) :
1427 BaseGraphWindow(desc
, STR_JUST_COMMA
)
1429 this->num_on_x_axis
= GRAPH_NUM_MONTHS
;
1430 this->num_vert_lines
= GRAPH_NUM_MONTHS
;
1431 this->month_increment
= 1;
1432 this->x_values_start
= GRAPH_NUM_MONTHS
;
1433 this->x_values_increment
= -ECONOMY_MONTH_MINUTES
;
1434 this->draw_dates
= !TimerGameEconomy::UsingWallclockUnits();
1436 this->CreateNestedTree();
1437 this->vscroll
= this->GetScrollbar(WID_GRAPH_MATRIX_SCROLLBAR
);
1440 const Industry
*i
= Industry::Get(window_number
);
1441 for (const auto &p
: i
->produced
) {
1442 if (!IsValidCargoID(p
.cargo
)) continue;
1445 this->vscroll
->SetCount(count
);
1447 auto *wid
= this->GetWidget
<NWidgetCore
>(WID_GRAPH_FOOTER
);
1448 wid
->SetDataTip(TimerGameEconomy::UsingWallclockUnits() ? STR_GRAPH_LAST_24_MINUTES_TIME_LABEL
: STR_EMPTY
, STR_NULL
);
1450 this->FinishInitNested(window_number
);
1452 /* Initialise the dataset */
1453 this->UpdateStatistics(true);
1456 void OnInit() override
1458 /* Width of the legend blob. */
1459 this->legend_width
= GetCharacterHeight(FS_SMALL
) * 9 / 6;
1462 void UpdateExcludedData()
1464 this->excluded_data
= 0;
1467 const Industry
*i
= Industry::Get(this->window_number
);
1468 for (const auto &p
: i
->produced
) {
1469 if (!IsValidCargoID(p
.cargo
)) continue;
1470 if (HasBit(_legend_excluded_cargo_production_history
, p
.cargo
)) SetBit(this->excluded_data
, index
);
1475 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
1477 if (widget
!= WID_GRAPH_MATRIX
) {
1478 BaseGraphWindow::UpdateWidgetSize(widget
, size
, padding
, fill
, resize
);
1482 const Industry
*i
= Industry::Get(this->window_number
);
1483 const CargoSpec
*cs
;
1484 for (const auto &p
: i
->produced
) {
1485 if (!IsValidCargoID(p
.cargo
)) continue;
1487 cs
= CargoSpec::Get(p
.cargo
);
1488 SetDParam(0, cs
->name
);
1489 Dimension d
= GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO
);
1490 d
.width
+= this->legend_width
+ WidgetDimensions::scaled
.hsep_normal
; // colour field
1491 d
.width
+= WidgetDimensions::scaled
.framerect
.Horizontal();
1492 d
.height
+= WidgetDimensions::scaled
.framerect
.Vertical();
1493 size
= maxdim(d
, size
);
1496 this->line_height
= size
.height
;
1497 size
.height
= this->line_height
* 11; /* Default number of cargo types in most climates. */
1499 resize
.height
= this->line_height
;
1502 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
1504 if (widget
!= WID_GRAPH_MATRIX
) {
1505 BaseGraphWindow::DrawWidget(r
, widget
);
1509 bool rtl
= _current_text_dir
== TD_RTL
;
1511 int pos
= this->vscroll
->GetPosition();
1512 int max
= pos
+ this->vscroll
->GetCapacity();
1514 Rect line
= r
.WithHeight(this->line_height
);
1515 const Industry
*i
= Industry::Get(this->window_number
);
1516 const CargoSpec
*cs
;
1518 for (const auto &p
: i
->produced
) {
1519 if (!IsValidCargoID(p
.cargo
)) continue;
1521 if (pos
-- > 0) continue;
1522 if (--max
< 0) break;
1524 cs
= CargoSpec::Get(p
.cargo
);
1526 bool lowered
= !HasBit(_legend_excluded_cargo_production_history
, p
.cargo
);
1528 /* Redraw frame if lowered */
1529 if (lowered
) DrawFrameRect(line
, COLOUR_BROWN
, FR_LOWERED
);
1531 const Rect text
= line
.Shrink(WidgetDimensions::scaled
.framerect
);
1533 /* Cargo-colour box with outline */
1534 const Rect cargo
= text
.WithWidth(this->legend_width
, rtl
);
1535 GfxFillRect(cargo
, PC_BLACK
);
1536 GfxFillRect(cargo
.Shrink(WidgetDimensions::scaled
.bevel
), cs
->legend_colour
);
1539 SetDParam(0, cs
->name
);
1540 DrawString(text
.Indent(this->legend_width
+ WidgetDimensions::scaled
.hsep_normal
, rtl
), STR_GRAPH_CARGO_PAYMENT_CARGO
);
1542 line
= line
.Translate(0, this->line_height
);
1546 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
1549 case WID_GRAPH_ENABLE_CARGOES
:
1550 /* Remove all cargoes from the excluded lists. */
1551 _legend_excluded_cargo_production_history
= 0;
1552 this->excluded_data
= 0;
1556 case WID_GRAPH_DISABLE_CARGOES
: {
1557 /* Add all cargoes to the excluded lists. */
1559 const Industry
*i
= Industry::Get(this->window_number
);
1560 for (const auto &p
: i
->produced
) {
1561 if (!IsValidCargoID(p
.cargo
)) continue;
1563 SetBit(_legend_excluded_cargo_production_history
, p
.cargo
);
1564 SetBit(this->excluded_data
, index
);
1571 case WID_GRAPH_MATRIX
: {
1572 int row
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_GRAPH_MATRIX
);
1573 if (row
>= this->vscroll
->GetCount()) return;
1575 const Industry
*i
= Industry::Get(this->window_number
);
1576 for (const auto &p
: i
->produced
) {
1577 if (!IsValidCargoID(p
.cargo
)) continue;
1578 if (row
-- > 0) continue;
1580 ToggleBit(_legend_excluded_cargo_production_history
, p
.cargo
);
1581 this->UpdateExcludedData();
1590 void SetStringParameters(WidgetID widget
) const override
1592 if (widget
== WID_GRAPH_CAPTION
) SetDParam(0, this->window_number
);
1595 void OnResize() override
1597 this->vscroll
->SetCapacityFromWidget(this, WID_GRAPH_MATRIX
);
1600 void UpdateStatistics(bool initialize
) override
1602 CargoTypes excluded_cargo
= this->excluded_data
;
1603 this->UpdateExcludedData();
1605 int mo
= TimerGameEconomy::month
- this->num_vert_lines
;
1606 auto yr
= TimerGameEconomy::year
;
1612 if (!initialize
&& this->excluded_data
== excluded_cargo
&& this->num_on_x_axis
== this->num_vert_lines
&& this->year
== yr
&& this->month
== mo
) {
1613 /* There's no reason to get new stats */
1621 const Industry
*i
= Industry::Get(this->window_number
);
1622 for (const auto &p
: i
->produced
) {
1623 if (!IsValidCargoID(p
.cargo
)) continue;
1625 const CargoSpec
*cs
= CargoSpec::Get(p
.cargo
);
1627 this->colours
[index
] = cs
->legend_colour
;
1628 for (uint j
= 0; j
< GRAPH_NUM_MONTHS
; j
++) {
1629 this->cost
[index
][j
] = p
.history
[GRAPH_NUM_MONTHS
- j
].production
;
1634 this->num_dataset
= index
;
1635 this->vscroll
->SetCount(index
);
1641 static constexpr NWidgetPart _nested_industry_production_widgets
[] = {
1642 NWidget(NWID_HORIZONTAL
),
1643 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1644 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_GRAPH_CAPTION
), SetDataTip(STR_GRAPH_INDUSTRY_PRODUCTION_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1645 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1646 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1647 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1649 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GRAPH_BACKGROUND
), SetMinimalSize(568, 128),
1650 NWidget(NWID_HORIZONTAL
),
1651 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_GRAPH_GRAPH
), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
1652 NWidget(NWID_VERTICAL
),
1653 NWidget(NWID_SPACER
), SetMinimalSize(0, 24), SetFill(0, 1),
1654 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GRAPH_ENABLE_CARGOES
), SetDataTip(STR_GRAPH_CARGO_ENABLE_ALL
, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL
), SetFill(1, 0),
1655 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GRAPH_DISABLE_CARGOES
), SetDataTip(STR_GRAPH_CARGO_DISABLE_ALL
, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL
), SetFill(1, 0),
1656 NWidget(NWID_SPACER
), SetMinimalSize(0, 4),
1657 NWidget(NWID_HORIZONTAL
),
1658 NWidget(WWT_MATRIX
, COLOUR_BROWN
, WID_GRAPH_MATRIX
), SetFill(1, 0), SetResize(0, 2), SetMatrixDataTip(1, 0, STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO
), SetScrollbar(WID_GRAPH_MATRIX_SCROLLBAR
),
1659 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_GRAPH_MATRIX_SCROLLBAR
),
1661 NWidget(NWID_SPACER
), SetMinimalSize(0, 24), SetFill(0, 1),
1663 NWidget(NWID_SPACER
), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
1665 NWidget(NWID_HORIZONTAL
),
1666 NWidget(NWID_SPACER
), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
1667 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_GRAPH_FOOTER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_EMPTY
, STR_NULL
),
1668 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1669 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_GRAPH_RESIZE
), SetDataTip(RWV_HIDE_BEVEL
, STR_TOOLTIP_RESIZE
),
1674 static WindowDesc
_industry_production_desc(
1675 WDP_AUTO
, "graph_industry_production", 0, 0,
1676 WC_INDUSTRY_PRODUCTION
, WC_INDUSTRY_VIEW
,
1678 _nested_industry_production_widgets
1681 void ShowIndustryProductionGraph(WindowNumber window_number
)
1683 AllocateWindowDescFront
<IndustryProductionGraphWindow
>(_industry_production_desc
, window_number
);
1687 * Make a vertical list of panels for outputting score details.
1688 * @return Panel with performance details.
1690 static std::unique_ptr
<NWidgetBase
> MakePerformanceDetailPanels()
1692 auto realtime
= TimerGameEconomy::UsingWallclockUnits();
1693 const StringID performance_tips
[] = {
1694 realtime
? STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP_PERIODS
: STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP_YEARS
,
1695 STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP
,
1696 realtime
? STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP_PERIODS
: STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP_YEARS
,
1697 STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP
,
1698 STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP
,
1699 STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP
,
1700 STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP
,
1701 STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP
,
1702 STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP
,
1703 STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP
,
1706 static_assert(lengthof(performance_tips
) == SCORE_END
- SCORE_BEGIN
);
1708 auto vert
= std::make_unique
<NWidgetVertical
>(NC_EQUALSIZE
);
1709 for (WidgetID widnum
= WID_PRD_SCORE_FIRST
; widnum
<= WID_PRD_SCORE_LAST
; widnum
++) {
1710 auto panel
= std::make_unique
<NWidgetBackground
>(WWT_PANEL
, COLOUR_BROWN
, widnum
);
1711 panel
->SetFill(1, 1);
1712 panel
->SetDataTip(0x0, performance_tips
[widnum
- WID_PRD_SCORE_FIRST
]);
1713 vert
->Add(std::move(panel
));
1718 /** Make a number of rows with buttons for each company for the performance rating detail window. */
1719 std::unique_ptr
<NWidgetBase
> MakeCompanyButtonRowsGraphGUI()
1721 return MakeCompanyButtonRows(WID_PRD_COMPANY_FIRST
, WID_PRD_COMPANY_LAST
, COLOUR_BROWN
, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP
);
1724 static constexpr NWidgetPart _nested_performance_rating_detail_widgets
[] = {
1725 NWidget(NWID_HORIZONTAL
),
1726 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1727 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_PERFORMANCE_DETAIL
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1728 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1729 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1731 NWidget(WWT_PANEL
, COLOUR_BROWN
),
1732 NWidgetFunction(MakeCompanyButtonRowsGraphGUI
), SetPadding(2),
1734 NWidgetFunction(MakePerformanceDetailPanels
),
1737 static WindowDesc
_performance_rating_detail_desc(
1738 WDP_AUTO
, "league_details", 0, 0,
1739 WC_PERFORMANCE_DETAIL
, WC_NONE
,
1741 _nested_performance_rating_detail_widgets
1744 void ShowPerformanceRatingDetail()
1746 AllocateWindowDescFront
<PerformanceRatingDetailWindow
>(_performance_rating_detail_desc
, 0);
1749 void InitializeGraphGui()
1751 _legend_excluded_companies
= 0;
1752 _legend_excluded_cargo_payment_rates
= 0;
1753 _legend_excluded_cargo_production_history
= 0;