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"
24 #include "zoom_func.h"
26 #include "widgets/graph_widget.h"
28 #include "table/strings.h"
29 #include "table/sprites.h"
32 #include "safeguards.h"
34 /* Bitmasks of company and cargo indices that shouldn't be drawn. */
35 static CompanyMask _legend_excluded_companies
;
36 static CargoTypes _legend_excluded_cargo
;
38 /* Apparently these don't play well with enums. */
39 static const OverflowSafeInt64
INVALID_DATAPOINT(INT64_MAX
); // Value used for a datapoint that shouldn't be drawn.
40 static const uint INVALID_DATAPOINT_POS
= UINT_MAX
; // Used to determine if the previous point was drawn.
46 struct GraphLegendWindow
: Window
{
47 GraphLegendWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
49 this->InitNested(window_number
);
51 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
52 if (!HasBit(_legend_excluded_companies
, c
)) this->LowerWidget(c
+ WID_GL_FIRST_COMPANY
);
54 this->OnInvalidateData(c
);
58 void DrawWidget(const Rect
&r
, int widget
) const override
60 if (!IsInsideMM(widget
, WID_GL_FIRST_COMPANY
, MAX_COMPANIES
+ WID_GL_FIRST_COMPANY
)) return;
62 CompanyID cid
= (CompanyID
)(widget
- WID_GL_FIRST_COMPANY
);
64 if (!Company::IsValidID(cid
)) return;
66 bool rtl
= _current_text_dir
== TD_RTL
;
68 const Rect ir
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
69 Dimension d
= GetSpriteSize(SPR_COMPANY_ICON
);
70 DrawCompanyIcon(cid
, rtl
? ir
.right
- d
.width
: ir
.left
, CenterBounds(ir
.top
, ir
.bottom
, d
.height
));
72 const Rect tr
= ir
.Indent(d
.width
+ WidgetDimensions::scaled
.hsep_normal
, rtl
);
75 DrawString(tr
.left
, tr
.right
, CenterBounds(tr
.top
, tr
.bottom
, FONT_HEIGHT_NORMAL
), STR_COMPANY_NAME_COMPANY_NUM
, HasBit(_legend_excluded_companies
, cid
) ? TC_BLACK
: TC_WHITE
);
78 void OnClick(Point pt
, int widget
, int click_count
) override
80 if (!IsInsideMM(widget
, WID_GL_FIRST_COMPANY
, MAX_COMPANIES
+ WID_GL_FIRST_COMPANY
)) return;
82 ToggleBit(_legend_excluded_companies
, widget
- WID_GL_FIRST_COMPANY
);
83 this->ToggleWidgetLoweredState(widget
);
85 InvalidateWindowData(WC_INCOME_GRAPH
, 0);
86 InvalidateWindowData(WC_OPERATING_PROFIT
, 0);
87 InvalidateWindowData(WC_DELIVERED_CARGO
, 0);
88 InvalidateWindowData(WC_PERFORMANCE_HISTORY
, 0);
89 InvalidateWindowData(WC_COMPANY_VALUE
, 0);
93 * Some data on this window has become invalid.
94 * @param data Information about the changed data.
95 * @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.
97 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
99 if (!gui_scope
) return;
100 if (Company::IsValidID(data
)) return;
102 SetBit(_legend_excluded_companies
, data
);
103 this->RaiseWidget(data
+ WID_GL_FIRST_COMPANY
);
108 * Construct a vertical list of buttons, one for each company.
109 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
110 * @return Panel with company buttons.
111 * @post \c *biggest_index contains the largest used index in the tree.
113 static NWidgetBase
*MakeNWidgetCompanyLines(int *biggest_index
)
115 NWidgetVertical
*vert
= new NWidgetVertical(NC_EQUALSIZE
);
116 vert
->SetPadding(2, 2, 2, 2);
117 uint sprite_height
= GetSpriteSize(SPR_COMPANY_ICON
, nullptr, ZOOM_LVL_OUT_4X
).height
;
119 for (int widnum
= WID_GL_FIRST_COMPANY
; widnum
<= WID_GL_LAST_COMPANY
; widnum
++) {
120 NWidgetBackground
*panel
= new NWidgetBackground(WWT_PANEL
, COLOUR_BROWN
, widnum
);
121 panel
->SetMinimalSize(246, sprite_height
+ WidgetDimensions::unscaled
.framerect
.Vertical());
122 panel
->SetMinimalTextLines(1, WidgetDimensions::unscaled
.framerect
.Vertical(), FS_NORMAL
);
123 panel
->SetFill(1, 1);
124 panel
->SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION_TOOLTIP
);
127 *biggest_index
= WID_GL_LAST_COMPANY
;
131 static const NWidgetPart _nested_graph_legend_widgets
[] = {
132 NWidget(NWID_HORIZONTAL
),
133 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
134 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_KEY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
135 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
136 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
138 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GL_BACKGROUND
),
139 NWidgetFunction(MakeNWidgetCompanyLines
),
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_BASE_COLOUR
= GREY_SCALE(2);
169 static const int GRAPH_GRID_COLOUR
= GREY_SCALE(3);
170 static const int GRAPH_AXIS_LINE_COLOUR
= GREY_SCALE(1);
171 static const int GRAPH_ZERO_LINE_COLOUR
= GREY_SCALE(8);
172 static const int GRAPH_YEAR_LINE_COLOUR
= GREY_SCALE(5);
173 static const int GRAPH_NUM_MONTHS
= 24; ///< Number of months displayed in the graph.
175 static const TextColour GRAPH_AXIS_LABEL_COLOUR
= TC_BLACK
; ///< colour of the graph axis label.
177 static const int MIN_GRAPH_NUM_LINES_Y
= 9; ///< Minimal number of horizontal lines to draw.
178 static const int MIN_GRID_PIXEL_SIZE
= 20; ///< Minimum distance between graph lines.
180 uint64 excluded_data
; ///< bitmask of the datasets that shouldn't be displayed.
185 /* The starting month and year that values are plotted against. If month is
186 * 0xFF, use x_values_start and x_values_increment below instead. */
190 /* These values are used if the graph is being plotted against values
191 * rather than the dates specified by month and year. */
192 uint16 x_values_start
;
193 uint16 x_values_increment
;
196 StringID format_str_y_axis
;
197 byte colours
[GRAPH_MAX_DATASETS
];
198 OverflowSafeInt64 cost
[GRAPH_MAX_DATASETS
][GRAPH_NUM_MONTHS
]; ///< Stored costs for the last #GRAPH_NUM_MONTHS months
201 * Get the interval that contains the graph's data. Excluded data is ignored to show smaller values in
202 * better detail when disabling higher ones.
203 * @param num_hori_lines Number of horizontal lines to be drawn.
204 * @return Highest and lowest values of the graph (ignoring disabled data).
206 ValuesInterval
GetValuesInterval(int num_hori_lines
) const
208 assert(num_hori_lines
> 0);
210 ValuesInterval current_interval
;
211 current_interval
.highest
= INT64_MIN
;
212 current_interval
.lowest
= INT64_MAX
;
214 for (int i
= 0; i
< this->num_dataset
; i
++) {
215 if (HasBit(this->excluded_data
, i
)) continue;
216 for (int j
= 0; j
< this->num_on_x_axis
; j
++) {
217 OverflowSafeInt64 datapoint
= this->cost
[i
][j
];
219 if (datapoint
!= INVALID_DATAPOINT
) {
220 current_interval
.highest
= std::max(current_interval
.highest
, datapoint
);
221 current_interval
.lowest
= std::min(current_interval
.lowest
, datapoint
);
226 /* Prevent showing values too close to the graph limits. */
227 current_interval
.highest
= (11 * current_interval
.highest
) / 10;
228 current_interval
.lowest
= (11 * current_interval
.lowest
) / 10;
230 /* Always include zero in the shown range. */
231 double abs_lower
= (current_interval
.lowest
> 0) ? 0 : (double)abs(current_interval
.lowest
);
232 double abs_higher
= (current_interval
.highest
< 0) ? 0 : (double)current_interval
.highest
;
237 if (abs_lower
!= 0 || abs_higher
!= 0) {
238 /* The number of grids to reserve for the positive part is: */
239 num_pos_grids
= (int)floor(0.5 + num_hori_lines
* abs_higher
/ (abs_higher
+ abs_lower
));
241 /* If there are any positive or negative values, force that they have at least one grid. */
242 if (num_pos_grids
== 0 && abs_higher
!= 0) num_pos_grids
++;
243 if (num_pos_grids
== num_hori_lines
&& abs_lower
!= 0) num_pos_grids
--;
245 /* Get the required grid size for each side and use the maximum one. */
246 int64 grid_size_higher
= (abs_higher
> 0) ? ((int64
)abs_higher
+ num_pos_grids
- 1) / num_pos_grids
: 0;
247 int64 grid_size_lower
= (abs_lower
> 0) ? ((int64
)abs_lower
+ num_hori_lines
- num_pos_grids
- 1) / (num_hori_lines
- num_pos_grids
) : 0;
248 grid_size
= std::max(grid_size_higher
, grid_size_lower
);
250 /* If both values are zero, show an empty graph. */
251 num_pos_grids
= num_hori_lines
/ 2;
255 current_interval
.highest
= num_pos_grids
* grid_size
;
256 current_interval
.lowest
= -(num_hori_lines
- num_pos_grids
) * grid_size
;
257 return current_interval
;
261 * Get width for Y labels.
262 * @param current_interval Interval that contains all of the graph data.
263 * @param num_hori_lines Number of horizontal lines to be drawn.
265 uint
GetYLabelWidth(ValuesInterval current_interval
, int num_hori_lines
) const
267 /* draw text strings on the y axis */
268 int64 y_label
= current_interval
.highest
;
269 int64 y_label_separation
= (current_interval
.highest
- current_interval
.lowest
) / num_hori_lines
;
273 for (int i
= 0; i
< (num_hori_lines
+ 1); i
++) {
274 SetDParam(0, this->format_str_y_axis
);
275 SetDParam(1, y_label
);
276 Dimension d
= GetStringBoundingBox(STR_GRAPH_Y_LABEL
);
277 if (d
.width
> max_width
) max_width
= d
.width
;
279 y_label
-= y_label_separation
;
286 * Actually draw the graph.
287 * @param r the rectangle of the data field of the graph
289 void DrawGraph(Rect r
) const
291 uint x
, y
; ///< Reused whenever x and y coordinates are needed.
292 ValuesInterval interval
; ///< Interval that contains all of the graph data.
293 int x_axis_offset
; ///< Distance from the top of the graph to the x axis.
295 /* the colours and cost array of GraphDrawer must accommodate
296 * both values for cargo and companies. So if any are higher, quit */
297 static_assert(GRAPH_MAX_DATASETS
>= (int)NUM_CARGO
&& GRAPH_MAX_DATASETS
>= (int)MAX_COMPANIES
);
298 assert(this->num_vert_lines
> 0);
300 /* Rect r will be adjusted to contain just the graph, with labels being
301 * placed outside the area. */
302 r
.top
+= ScaleGUITrad(5) + GetCharacterHeight(FS_SMALL
) / 2;
303 r
.bottom
-= (this->month
== 0xFF ? 1 : 2) * GetCharacterHeight(FS_SMALL
) + ScaleGUITrad(4);
304 r
.left
+= ScaleGUITrad(9);
305 r
.right
-= ScaleGUITrad(5);
307 /* Initial number of horizontal lines. */
308 int num_hori_lines
= 160 / ScaleGUITrad(MIN_GRID_PIXEL_SIZE
);
309 /* For the rest of the height, the number of horizontal lines will increase more slowly. */
310 int resize
= (r
.bottom
- r
.top
- 160) / (2 * ScaleGUITrad(MIN_GRID_PIXEL_SIZE
));
311 if (resize
> 0) num_hori_lines
+= resize
;
313 interval
= GetValuesInterval(num_hori_lines
);
315 int label_width
= GetYLabelWidth(interval
, num_hori_lines
);
317 r
.left
+= label_width
;
319 int x_sep
= (r
.right
- r
.left
) / this->num_vert_lines
;
320 int y_sep
= (r
.bottom
- r
.top
) / num_hori_lines
;
322 /* Redetermine right and bottom edge of graph to fit with the integer
323 * separation values. */
324 r
.right
= r
.left
+ x_sep
* this->num_vert_lines
;
325 r
.bottom
= r
.top
+ y_sep
* num_hori_lines
;
327 OverflowSafeInt64 interval_size
= interval
.highest
+ abs(interval
.lowest
);
328 /* Where to draw the X axis. Use floating point to avoid overflowing and results of zero. */
329 x_axis_offset
= (int)((r
.bottom
- r
.top
) * (double)interval
.highest
/ (double)interval_size
);
331 /* Draw the background of the graph itself. */
332 GfxFillRect(r
.left
, r
.top
, r
.right
, r
.bottom
, GRAPH_BASE_COLOUR
);
334 /* Draw the vertical grid lines. */
336 /* Don't draw the first line, as that's where the axis will be. */
339 for (int i
= 0; i
< this->num_vert_lines
; i
++) {
340 GfxFillRect(x
, r
.top
, x
, r
.bottom
, GRAPH_GRID_COLOUR
);
344 /* Draw the horizontal grid lines. */
347 for (int i
= 0; i
< (num_hori_lines
+ 1); i
++) {
348 GfxFillRect(r
.left
- ScaleGUITrad(3), y
, r
.left
- 1, y
, GRAPH_AXIS_LINE_COLOUR
);
349 GfxFillRect(r
.left
, y
, r
.right
, y
, GRAPH_GRID_COLOUR
);
353 /* Draw the y axis. */
354 GfxFillRect(r
.left
, r
.top
, r
.left
, r
.bottom
, GRAPH_AXIS_LINE_COLOUR
);
356 /* Draw the x axis. */
357 y
= x_axis_offset
+ r
.top
;
358 GfxFillRect(r
.left
, y
, r
.right
, y
, GRAPH_ZERO_LINE_COLOUR
);
360 /* Find the largest value that will be drawn. */
361 if (this->num_on_x_axis
== 0) return;
363 assert(this->num_on_x_axis
> 0);
364 assert(this->num_dataset
> 0);
366 /* draw text strings on the y axis */
367 int64 y_label
= interval
.highest
;
368 int64 y_label_separation
= abs(interval
.highest
- interval
.lowest
) / num_hori_lines
;
370 y
= r
.top
- GetCharacterHeight(FS_SMALL
) / 2;
372 for (int i
= 0; i
< (num_hori_lines
+ 1); i
++) {
373 SetDParam(0, this->format_str_y_axis
);
374 SetDParam(1, y_label
);
375 DrawString(r
.left
- label_width
- ScaleGUITrad(4), r
.left
- ScaleGUITrad(4), y
, STR_GRAPH_Y_LABEL
, GRAPH_AXIS_LABEL_COLOUR
, SA_RIGHT
);
377 y_label
-= y_label_separation
;
381 /* Draw x-axis labels and markings for graphs based on financial quarters and years. */
382 if (this->month
!= 0xFF) {
384 y
= r
.bottom
+ ScaleGUITrad(2);
385 byte month
= this->month
;
386 Year year
= this->year
;
387 for (int i
= 0; i
< this->num_on_x_axis
; i
++) {
388 SetDParam(0, month
+ STR_MONTH_ABBREV_JAN
);
390 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
);
397 /* Draw a lighter grid line between years. Top and bottom adjustments ensure we don't draw over top and bottom horizontal grid lines. */
398 GfxFillRect(x
+ x_sep
, r
.top
+ 1, x
+ x_sep
, r
.bottom
- 1, GRAPH_YEAR_LINE_COLOUR
);
403 /* Draw x-axis labels for graphs not based on quarterly performance (cargo payment rates). */
405 y
= r
.bottom
+ ScaleGUITrad(2);
406 uint16 label
= this->x_values_start
;
408 for (int i
= 0; i
< this->num_on_x_axis
; i
++) {
410 DrawString(x
+ 1, x
+ x_sep
- 1, y
, STR_GRAPH_Y_LABEL_NUMBER
, GRAPH_AXIS_LABEL_COLOUR
, SA_HOR_CENTER
);
412 label
+= this->x_values_increment
;
417 /* draw lines and dots */
418 uint linewidth
= _settings_client
.gui
.graph_line_thickness
;
419 uint pointoffs1
= (linewidth
+ 1) / 2;
420 uint pointoffs2
= linewidth
+ 1 - pointoffs1
;
421 for (int i
= 0; i
< this->num_dataset
; i
++) {
422 if (!HasBit(this->excluded_data
, i
)) {
423 /* Centre the dot between the grid lines. */
424 x
= r
.left
+ (x_sep
/ 2);
426 byte colour
= this->colours
[i
];
427 uint prev_x
= INVALID_DATAPOINT_POS
;
428 uint prev_y
= INVALID_DATAPOINT_POS
;
430 for (int j
= 0; j
< this->num_on_x_axis
; j
++) {
431 OverflowSafeInt64 datapoint
= this->cost
[i
][j
];
433 if (datapoint
!= INVALID_DATAPOINT
) {
435 * Check whether we need to reduce the 'accuracy' of the
436 * datapoint value and the highest value to split overflows.
437 * And when 'drawing' 'one million' or 'one million and one'
438 * there is no significant difference, so the least
439 * significant bits can just be removed.
441 * If there are more bits needed than would fit in a 32 bits
442 * integer, so at about 31 bits because of the sign bit, the
443 * least significant bits are removed.
445 int mult_range
= FindLastBit(x_axis_offset
) + FindLastBit(abs(datapoint
));
446 int reduce_range
= std::max(mult_range
- 31, 0);
448 /* Handle negative values differently (don't shift sign) */
450 datapoint
= -(abs(datapoint
) >> reduce_range
);
452 datapoint
>>= reduce_range
;
454 y
= r
.top
+ x_axis_offset
- ((r
.bottom
- r
.top
) * datapoint
) / (interval_size
>> reduce_range
);
456 /* Draw the point. */
457 GfxFillRect(x
- pointoffs1
, y
- pointoffs1
, x
+ pointoffs2
, y
+ pointoffs2
, colour
);
459 /* Draw the line connected to the previous point. */
460 if (prev_x
!= INVALID_DATAPOINT_POS
) GfxDrawLine(prev_x
, prev_y
, x
, y
, colour
, linewidth
);
465 prev_x
= INVALID_DATAPOINT_POS
;
466 prev_y
= INVALID_DATAPOINT_POS
;
476 BaseGraphWindow(WindowDesc
*desc
, int widget
, StringID format_str_y_axis
) :
478 format_str_y_axis(format_str_y_axis
)
480 SetWindowDirty(WC_GRAPH_LEGEND
, 0);
481 this->num_vert_lines
= 24;
482 this->graph_widget
= widget
;
485 void InitializeWindow(WindowNumber number
)
487 /* Initialise the dataset */
488 this->UpdateStatistics(true);
490 this->InitNested(number
);
494 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
496 if (widget
!= this->graph_widget
) return;
498 uint x_label_width
= 0;
500 /* Draw x-axis labels and markings for graphs based on financial quarters and years. */
501 if (this->month
!= 0xFF) {
502 byte month
= this->month
;
503 Year year
= this->year
;
504 for (int i
= 0; i
< this->num_on_x_axis
; i
++) {
505 SetDParam(0, month
+ STR_MONTH_ABBREV_JAN
);
507 x_label_width
= std::max(x_label_width
, GetStringBoundingBox(month
== 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR
: STR_GRAPH_X_LABEL_MONTH
).width
);
516 /* Draw x-axis labels for graphs not based on quarterly performance (cargo payment rates). */
517 SetDParamMaxValue(0, this->x_values_start
+ this->num_on_x_axis
* this->x_values_increment
, 0, FS_SMALL
);
518 x_label_width
= GetStringBoundingBox(STR_GRAPH_Y_LABEL_NUMBER
).width
;
521 SetDParam(0, this->format_str_y_axis
);
522 SetDParam(1, INT64_MAX
);
523 uint y_label_width
= GetStringBoundingBox(STR_GRAPH_Y_LABEL
).width
;
525 size
->width
= std::max
<uint
>(size
->width
, ScaleGUITrad(5) + y_label_width
+ this->num_on_x_axis
* (x_label_width
+ ScaleGUITrad(5)) + ScaleGUITrad(9));
526 size
->height
= std::max
<uint
>(size
->height
, ScaleGUITrad(5) + (1 + MIN_GRAPH_NUM_LINES_Y
* 2 + (this->month
!= 0xFF ? 3 : 1)) * FONT_HEIGHT_SMALL
+ ScaleGUITrad(4));
527 size
->height
= std::max
<uint
>(size
->height
, size
->width
/ 3);
530 void DrawWidget(const Rect
&r
, int widget
) const override
532 if (widget
!= this->graph_widget
) return;
537 virtual OverflowSafeInt64
GetGraphData(const Company
*c
, int j
)
539 return INVALID_DATAPOINT
;
542 void OnClick(Point pt
, int widget
, int click_count
) override
544 /* Clicked on legend? */
545 if (widget
== WID_CV_KEY_BUTTON
) ShowGraphLegend();
548 void OnGameTick() override
550 this->UpdateStatistics(false);
554 * Some data on this window has become invalid.
555 * @param data Information about the changed data.
556 * @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.
558 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
560 if (!gui_scope
) return;
561 this->UpdateStatistics(true);
565 * Update the statistics.
566 * @param initialize Initialize the data structure.
568 void UpdateStatistics(bool initialize
)
570 CompanyMask excluded_companies
= _legend_excluded_companies
;
572 /* Exclude the companies which aren't valid */
573 for (CompanyID c
= COMPANY_FIRST
; c
< MAX_COMPANIES
; c
++) {
574 if (!Company::IsValidID(c
)) SetBit(excluded_companies
, c
);
578 for (const Company
*c
: Company::Iterate()) {
579 nums
= std::min(this->num_vert_lines
, std::max(nums
, c
->num_valid_stat_ent
));
582 int mo
= (_cur_month
/ 3 - nums
) * 3;
589 if (!initialize
&& this->excluded_data
== excluded_companies
&& this->num_on_x_axis
== nums
&&
590 this->year
== yr
&& this->month
== mo
) {
591 /* There's no reason to get new stats */
595 this->excluded_data
= excluded_companies
;
596 this->num_on_x_axis
= nums
;
601 for (CompanyID k
= COMPANY_FIRST
; k
< MAX_COMPANIES
; k
++) {
602 const Company
*c
= Company::GetIfValid(k
);
604 this->colours
[numd
] = _colour_gradient
[c
->colour
][6];
605 for (int j
= this->num_on_x_axis
, i
= 0; --j
>= 0;) {
606 this->cost
[numd
][i
] = (j
>= c
->num_valid_stat_ent
) ? INVALID_DATAPOINT
: GetGraphData(c
, j
);
613 this->num_dataset
= numd
;
618 /********************/
619 /* OPERATING PROFIT */
620 /********************/
622 struct OperatingProfitGraphWindow
: BaseGraphWindow
{
623 OperatingProfitGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
624 BaseGraphWindow(desc
, WID_CV_GRAPH
, STR_JUST_CURRENCY_SHORT
)
626 this->InitializeWindow(window_number
);
629 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
631 return c
->old_economy
[j
].income
+ c
->old_economy
[j
].expenses
;
635 static const NWidgetPart _nested_operating_profit_widgets
[] = {
636 NWidget(NWID_HORIZONTAL
),
637 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
638 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_OPERATING_PROFIT_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
639 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_CV_KEY_BUTTON
), SetMinimalSize(50, 0), SetMinimalTextLines(1, WidgetDimensions::unscaled
.framerect
.Vertical() + 2), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
640 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
641 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
642 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
644 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_CV_BACKGROUND
),
645 NWidget(NWID_HORIZONTAL
),
646 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_CV_GRAPH
), SetMinimalSize(576, 160), SetFill(1, 1), SetResize(1, 1),
647 NWidget(NWID_VERTICAL
),
648 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
649 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_CV_RESIZE
),
655 static WindowDesc
_operating_profit_desc(
656 WDP_AUTO
, "graph_operating_profit", 0, 0,
657 WC_OPERATING_PROFIT
, WC_NONE
,
659 _nested_operating_profit_widgets
, lengthof(_nested_operating_profit_widgets
)
663 void ShowOperatingProfitGraph()
665 AllocateWindowDescFront
<OperatingProfitGraphWindow
>(&_operating_profit_desc
, 0);
673 struct IncomeGraphWindow
: BaseGraphWindow
{
674 IncomeGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
675 BaseGraphWindow(desc
, WID_CV_GRAPH
, STR_JUST_CURRENCY_SHORT
)
677 this->InitializeWindow(window_number
);
680 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
682 return c
->old_economy
[j
].income
;
686 static const NWidgetPart _nested_income_graph_widgets
[] = {
687 NWidget(NWID_HORIZONTAL
),
688 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
689 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_INCOME_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
690 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_CV_KEY_BUTTON
), SetMinimalSize(50, 0), SetMinimalTextLines(1, WidgetDimensions::unscaled
.framerect
.Vertical() + 2), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
691 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
692 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
693 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
695 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_CV_BACKGROUND
),
696 NWidget(NWID_HORIZONTAL
),
697 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_CV_GRAPH
), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
698 NWidget(NWID_VERTICAL
),
699 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
700 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_CV_RESIZE
),
706 static WindowDesc
_income_graph_desc(
707 WDP_AUTO
, "graph_income", 0, 0,
708 WC_INCOME_GRAPH
, WC_NONE
,
710 _nested_income_graph_widgets
, lengthof(_nested_income_graph_widgets
)
713 void ShowIncomeGraph()
715 AllocateWindowDescFront
<IncomeGraphWindow
>(&_income_graph_desc
, 0);
718 /*******************/
719 /* DELIVERED CARGO */
720 /*******************/
722 struct DeliveredCargoGraphWindow
: BaseGraphWindow
{
723 DeliveredCargoGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
724 BaseGraphWindow(desc
, WID_CV_GRAPH
, STR_JUST_COMMA
)
726 this->InitializeWindow(window_number
);
729 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
731 return c
->old_economy
[j
].delivered_cargo
.GetSum
<OverflowSafeInt64
>();
735 static const NWidgetPart _nested_delivered_cargo_graph_widgets
[] = {
736 NWidget(NWID_HORIZONTAL
),
737 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
738 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_CARGO_DELIVERED_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
739 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_CV_KEY_BUTTON
), SetMinimalSize(50, 0), SetMinimalTextLines(1, WidgetDimensions::unscaled
.framerect
.Vertical() + 2), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
740 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
741 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
742 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
744 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_CV_BACKGROUND
),
745 NWidget(NWID_HORIZONTAL
),
746 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_CV_GRAPH
), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
747 NWidget(NWID_VERTICAL
),
748 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
749 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_CV_RESIZE
),
755 static WindowDesc
_delivered_cargo_graph_desc(
756 WDP_AUTO
, "graph_delivered_cargo", 0, 0,
757 WC_DELIVERED_CARGO
, WC_NONE
,
759 _nested_delivered_cargo_graph_widgets
, lengthof(_nested_delivered_cargo_graph_widgets
)
762 void ShowDeliveredCargoGraph()
764 AllocateWindowDescFront
<DeliveredCargoGraphWindow
>(&_delivered_cargo_graph_desc
, 0);
767 /***********************/
768 /* PERFORMANCE HISTORY */
769 /***********************/
771 struct PerformanceHistoryGraphWindow
: BaseGraphWindow
{
772 PerformanceHistoryGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
773 BaseGraphWindow(desc
, WID_PHG_GRAPH
, STR_JUST_COMMA
)
775 this->InitializeWindow(window_number
);
778 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
780 return c
->old_economy
[j
].performance_history
;
783 void OnClick(Point pt
, int widget
, int click_count
) override
785 if (widget
== WID_PHG_DETAILED_PERFORMANCE
) ShowPerformanceRatingDetail();
786 this->BaseGraphWindow::OnClick(pt
, widget
, click_count
);
790 static const NWidgetPart _nested_performance_history_widgets
[] = {
791 NWidget(NWID_HORIZONTAL
),
792 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
793 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_COMPANY_PERFORMANCE_RATINGS_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
794 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_PHG_DETAILED_PERFORMANCE
), SetMinimalSize(50, 0), SetMinimalTextLines(1, WidgetDimensions::unscaled
.framerect
.Vertical() + 2), SetDataTip(STR_PERFORMANCE_DETAIL_KEY
, STR_GRAPH_PERFORMANCE_DETAIL_TOOLTIP
),
795 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_PHG_KEY
), SetMinimalSize(50, 0), SetMinimalTextLines(1, WidgetDimensions::unscaled
.framerect
.Vertical() + 2), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
796 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
797 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
798 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
800 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_PHG_BACKGROUND
),
801 NWidget(NWID_HORIZONTAL
),
802 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_PHG_GRAPH
), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
803 NWidget(NWID_VERTICAL
),
804 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
805 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_PHG_RESIZE
),
811 static WindowDesc
_performance_history_desc(
812 WDP_AUTO
, "graph_performance", 0, 0,
813 WC_PERFORMANCE_HISTORY
, WC_NONE
,
815 _nested_performance_history_widgets
, lengthof(_nested_performance_history_widgets
)
818 void ShowPerformanceHistoryGraph()
820 AllocateWindowDescFront
<PerformanceHistoryGraphWindow
>(&_performance_history_desc
, 0);
827 struct CompanyValueGraphWindow
: BaseGraphWindow
{
828 CompanyValueGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
829 BaseGraphWindow(desc
, WID_CV_GRAPH
, STR_JUST_CURRENCY_SHORT
)
831 this->InitializeWindow(window_number
);
834 OverflowSafeInt64
GetGraphData(const Company
*c
, int j
) override
836 return c
->old_economy
[j
].company_value
;
840 static const NWidgetPart _nested_company_value_graph_widgets
[] = {
841 NWidget(NWID_HORIZONTAL
),
842 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
843 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_COMPANY_VALUES_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
844 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_CV_KEY_BUTTON
), SetMinimalSize(50, 0), SetMinimalTextLines(1, WidgetDimensions::unscaled
.framerect
.Vertical() + 2), SetDataTip(STR_GRAPH_KEY_BUTTON
, STR_GRAPH_KEY_TOOLTIP
),
845 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
846 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
847 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
849 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_CV_BACKGROUND
),
850 NWidget(NWID_HORIZONTAL
),
851 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_CV_GRAPH
), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
852 NWidget(NWID_VERTICAL
),
853 NWidget(NWID_SPACER
), SetFill(0, 1), SetResize(0, 1),
854 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_CV_RESIZE
),
860 static WindowDesc
_company_value_graph_desc(
861 WDP_AUTO
, "graph_company_value", 0, 0,
862 WC_COMPANY_VALUE
, WC_NONE
,
864 _nested_company_value_graph_widgets
, lengthof(_nested_company_value_graph_widgets
)
867 void ShowCompanyValueGraph()
869 AllocateWindowDescFront
<CompanyValueGraphWindow
>(&_company_value_graph_desc
, 0);
876 struct PaymentRatesGraphWindow
: BaseGraphWindow
{
877 uint line_height
; ///< Pixel height of each cargo type row.
878 Scrollbar
*vscroll
; ///< Cargo list scrollbar.
879 uint legend_width
; ///< Width of legend 'blob'.
881 PaymentRatesGraphWindow(WindowDesc
*desc
, WindowNumber window_number
) :
882 BaseGraphWindow(desc
, WID_CPR_GRAPH
, STR_JUST_CURRENCY_SHORT
)
884 this->num_on_x_axis
= 20;
885 this->num_vert_lines
= 20;
887 this->x_values_start
= 10;
888 this->x_values_increment
= 10;
890 this->CreateNestedTree();
891 this->vscroll
= this->GetScrollbar(WID_CPR_MATRIX_SCROLLBAR
);
892 this->vscroll
->SetCount(static_cast<int>(_sorted_standard_cargo_specs
.size()));
894 /* Initialise the dataset */
895 this->OnHundredthTick();
897 this->FinishInitNested(window_number
);
900 void OnInit() override
902 /* Width of the legend blob. */
903 this->legend_width
= (FONT_HEIGHT_SMALL
- ScaleGUITrad(1)) * 8 / 5;
906 void UpdateExcludedData()
908 this->excluded_data
= 0;
911 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
912 if (HasBit(_legend_excluded_cargo
, cs
->Index())) SetBit(this->excluded_data
, i
);
917 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
919 if (widget
!= WID_CPR_MATRIX
) {
920 BaseGraphWindow::UpdateWidgetSize(widget
, size
, padding
, fill
, resize
);
924 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
925 SetDParam(0, cs
->name
);
926 Dimension d
= GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO
);
927 d
.width
+= this->legend_width
+ WidgetDimensions::scaled
.hsep_normal
; // colour field
928 d
.width
+= WidgetDimensions::scaled
.framerect
.Horizontal();
929 d
.height
+= WidgetDimensions::scaled
.framerect
.Vertical();
930 *size
= maxdim(d
, *size
);
933 this->line_height
= size
->height
;
934 size
->height
= this->line_height
* 11; /* Default number of cargo types in most climates. */
936 resize
->height
= this->line_height
;
939 void DrawWidget(const Rect
&r
, int widget
) const override
941 if (widget
!= WID_CPR_MATRIX
) {
942 BaseGraphWindow::DrawWidget(r
, widget
);
946 bool rtl
= _current_text_dir
== TD_RTL
;
948 int pos
= this->vscroll
->GetPosition();
949 int max
= pos
+ this->vscroll
->GetCapacity();
951 Rect line
= r
.WithHeight(this->line_height
);
952 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
953 if (pos
-- > 0) continue;
954 if (--max
< 0) break;
956 bool lowered
= !HasBit(_legend_excluded_cargo
, cs
->Index());
958 /* Redraw frame if lowered */
959 if (lowered
) DrawFrameRect(line
, COLOUR_BROWN
, FR_LOWERED
);
961 const Rect text
= line
.Shrink(WidgetDimensions::scaled
.framerect
).Translate(lowered
? WidgetDimensions::scaled
.pressed
: 0, lowered
? WidgetDimensions::scaled
.pressed
: 0);
963 /* Cargo-colour box with outline */
964 const Rect cargo
= text
.WithWidth(this->legend_width
, rtl
);
965 GfxFillRect(cargo
, PC_BLACK
);
966 GfxFillRect(cargo
.Shrink(WidgetDimensions::scaled
.bevel
), cs
->legend_colour
);
969 SetDParam(0, cs
->name
);
970 DrawString(text
.Indent(this->legend_width
+ WidgetDimensions::scaled
.hsep_normal
, rtl
), STR_GRAPH_CARGO_PAYMENT_CARGO
);
972 line
= line
.Translate(0, this->line_height
);
976 void OnClick(Point pt
, int widget
, int click_count
) override
979 case WID_CPR_ENABLE_CARGOES
:
980 /* Remove all cargoes from the excluded lists. */
981 _legend_excluded_cargo
= 0;
982 this->excluded_data
= 0;
986 case WID_CPR_DISABLE_CARGOES
: {
987 /* Add all cargoes to the excluded lists. */
989 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
990 SetBit(_legend_excluded_cargo
, cs
->Index());
991 SetBit(this->excluded_data
, i
);
998 case WID_CPR_MATRIX
: {
999 uint row
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_CPR_MATRIX
);
1000 if (row
>= this->vscroll
->GetCount()) return;
1002 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
1003 if (row
-- > 0) continue;
1005 ToggleBit(_legend_excluded_cargo
, cs
->Index());
1006 this->UpdateExcludedData();
1015 void OnResize() override
1017 this->vscroll
->SetCapacityFromWidget(this, WID_CPR_MATRIX
);
1020 void OnGameTick() override
1022 /* Override default OnGameTick */
1026 * Some data on this window has become invalid.
1027 * @param data Information about the changed data.
1028 * @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.
1030 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1032 if (!gui_scope
) return;
1033 this->OnHundredthTick();
1036 void OnHundredthTick() override
1038 this->UpdateExcludedData();
1041 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
1042 this->colours
[i
] = cs
->legend_colour
;
1043 for (uint j
= 0; j
!= 20; j
++) {
1044 this->cost
[i
][j
] = GetTransportedGoodsIncome(10, 20, j
* 4 + 4, cs
->Index());
1048 this->num_dataset
= i
;
1052 static const NWidgetPart _nested_cargo_payment_rates_widgets
[] = {
1053 NWidget(NWID_HORIZONTAL
),
1054 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1055 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1056 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1057 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1058 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1060 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_CPR_BACKGROUND
), SetMinimalSize(568, 128),
1061 NWidget(NWID_HORIZONTAL
),
1062 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1063 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_CPR_HEADER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_TITLE
, STR_NULL
),
1064 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1066 NWidget(NWID_HORIZONTAL
),
1067 NWidget(WWT_EMPTY
, COLOUR_BROWN
, WID_CPR_GRAPH
), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
1068 NWidget(NWID_VERTICAL
),
1069 NWidget(NWID_SPACER
), SetMinimalSize(0, 24), SetFill(0, 1),
1070 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_CPR_ENABLE_CARGOES
), SetDataTip(STR_GRAPH_CARGO_ENABLE_ALL
, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL
), SetFill(1, 0),
1071 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_CPR_DISABLE_CARGOES
), SetDataTip(STR_GRAPH_CARGO_DISABLE_ALL
, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL
), SetFill(1, 0),
1072 NWidget(NWID_SPACER
), SetMinimalSize(0, 4),
1073 NWidget(NWID_HORIZONTAL
),
1074 NWidget(WWT_MATRIX
, COLOUR_BROWN
, WID_CPR_MATRIX
), SetResize(0, 2), SetMatrixDataTip(1, 0, STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO
), SetScrollbar(WID_CPR_MATRIX_SCROLLBAR
),
1075 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_CPR_MATRIX_SCROLLBAR
),
1077 NWidget(NWID_SPACER
), SetMinimalSize(0, 24), SetFill(0, 1),
1079 NWidget(NWID_SPACER
), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
1081 NWidget(NWID_HORIZONTAL
),
1082 NWidget(NWID_SPACER
), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
1083 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_CPR_FOOTER
), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_X_LABEL
, STR_NULL
),
1084 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1085 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
, WID_CPR_RESIZE
),
1090 static WindowDesc
_cargo_payment_rates_desc(
1091 WDP_AUTO
, "graph_cargo_payment_rates", 0, 0,
1092 WC_PAYMENT_RATES
, WC_NONE
,
1094 _nested_cargo_payment_rates_widgets
, lengthof(_nested_cargo_payment_rates_widgets
)
1098 void ShowCargoPaymentRates()
1100 AllocateWindowDescFront
<PaymentRatesGraphWindow
>(&_cargo_payment_rates_desc
, 0);
1103 /************************/
1104 /* COMPANY LEAGUE TABLE */
1105 /************************/
1107 static const StringID _performance_titles
[] = {
1108 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER
,
1109 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER
,
1110 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER
,
1111 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER
,
1112 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR
,
1113 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR
,
1114 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR
,
1115 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR
,
1116 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR
,
1117 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR
,
1118 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE
,
1119 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE
,
1120 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN
,
1121 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN
,
1122 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT
,
1123 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON
,
1126 static inline StringID
GetPerformanceTitleFromValue(uint value
)
1128 return _performance_titles
[std::min(value
, 1000u) >> 6];
1131 class CompanyLeagueWindow
: public Window
{
1133 GUIList
<const Company
*> companies
;
1134 uint ordinal_width
; ///< The width of the ordinal number
1135 uint text_width
; ///< The width of the actual text
1136 int line_height
; ///< Height of the text lines
1137 Dimension icon
; ///< Dimenion of the company icon.
1140 * (Re)Build the company league list
1142 void BuildCompanyList()
1144 if (!this->companies
.NeedRebuild()) return;
1146 this->companies
.clear();
1148 for (const Company
*c
: Company::Iterate()) {
1149 this->companies
.push_back(c
);
1152 this->companies
.shrink_to_fit();
1153 this->companies
.RebuildDone();
1156 /** Sort the company league by performance history */
1157 static bool PerformanceSorter(const Company
* const &c1
, const Company
* const &c2
)
1159 return c2
->old_economy
[0].performance_history
< c1
->old_economy
[0].performance_history
;
1163 CompanyLeagueWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
1165 this->InitNested(window_number
);
1166 this->companies
.ForceRebuild();
1167 this->companies
.NeedResort();
1170 void OnPaint() override
1172 this->BuildCompanyList();
1173 this->companies
.Sort(&PerformanceSorter
);
1175 this->DrawWidgets();
1178 void DrawWidget(const Rect
&r
, int widget
) const override
1180 if (widget
!= WID_CL_BACKGROUND
) return;
1182 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
1183 int icon_y_offset
= (this->line_height
- this->icon
.height
) / 2;
1184 int text_y_offset
= (this->line_height
- FONT_HEIGHT_NORMAL
) / 2;
1186 bool rtl
= _current_text_dir
== TD_RTL
;
1187 Rect ordinal
= ir
.WithWidth(this->ordinal_width
, rtl
);
1188 uint icon_left
= ir
.Indent(rtl
? this->text_width
: this->ordinal_width
, rtl
).left
;
1189 Rect text
= ir
.WithWidth(this->text_width
, !rtl
);
1191 for (uint i
= 0; i
!= this->companies
.size(); i
++) {
1192 const Company
*c
= this->companies
[i
];
1193 DrawString(ordinal
.left
, ordinal
.right
, ir
.top
+ text_y_offset
, i
+ STR_ORDINAL_NUMBER_1ST
, i
== 0 ? TC_WHITE
: TC_YELLOW
);
1195 DrawCompanyIcon(c
->index
, icon_left
, ir
.top
+ icon_y_offset
);
1197 SetDParam(0, c
->index
);
1198 SetDParam(1, c
->index
);
1199 SetDParam(2, GetPerformanceTitleFromValue(c
->old_economy
[0].performance_history
));
1200 DrawString(text
.left
, text
.right
, ir
.top
+ text_y_offset
, STR_COMPANY_LEAGUE_COMPANY_NAME
);
1201 ir
.top
+= this->line_height
;
1205 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
1207 if (widget
!= WID_CL_BACKGROUND
) return;
1209 this->ordinal_width
= 0;
1210 for (uint i
= 0; i
< MAX_COMPANIES
; i
++) {
1211 this->ordinal_width
= std::max(this->ordinal_width
, GetStringBoundingBox(STR_ORDINAL_NUMBER_1ST
+ i
).width
);
1213 this->ordinal_width
+= WidgetDimensions::scaled
.hsep_wide
; // Keep some extra spacing
1215 uint widest_width
= 0;
1216 uint widest_title
= 0;
1217 for (uint i
= 0; i
< lengthof(_performance_titles
); i
++) {
1218 uint width
= GetStringBoundingBox(_performance_titles
[i
]).width
;
1219 if (width
> widest_width
) {
1221 widest_width
= width
;
1225 this->icon
= GetSpriteSize(SPR_COMPANY_ICON
);
1226 this->line_height
= std::max
<int>(this->icon
.height
+ WidgetDimensions::scaled
.vsep_normal
, FONT_HEIGHT_NORMAL
);
1228 for (const Company
*c
: Company::Iterate()) {
1229 SetDParam(0, c
->index
);
1230 SetDParam(1, c
->index
);
1231 SetDParam(2, _performance_titles
[widest_title
]);
1232 widest_width
= std::max(widest_width
, GetStringBoundingBox(STR_COMPANY_LEAGUE_COMPANY_NAME
).width
);
1235 this->text_width
= widest_width
+ WidgetDimensions::scaled
.hsep_indent
* 3; // Keep some extra spacing
1237 size
->width
= WidgetDimensions::scaled
.framerect
.Horizontal() + this->ordinal_width
+ this->icon
.width
+ this->text_width
+ WidgetDimensions::scaled
.framerect
.Horizontal();
1238 size
->height
= this->line_height
* MAX_COMPANIES
+ WidgetDimensions::scaled
.framerect
.Vertical();
1242 void OnGameTick() override
1244 if (this->companies
.NeedResort()) {
1250 * Some data on this window has become invalid.
1251 * @param data Information about the changed data.
1252 * @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.
1254 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1257 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
1258 this->companies
.ForceRebuild();
1260 this->companies
.ForceResort();
1265 static const NWidgetPart _nested_company_league_widgets
[] = {
1266 NWidget(NWID_HORIZONTAL
),
1267 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1268 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_COMPANY_LEAGUE_TABLE_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1269 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1270 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1272 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_CL_BACKGROUND
), SetMinimalSize(400, 0), SetMinimalTextLines(15, WidgetDimensions::unscaled
.framerect
.Vertical()),
1275 static WindowDesc
_company_league_desc(
1276 WDP_AUTO
, "league", 0, 0,
1277 WC_COMPANY_LEAGUE
, WC_NONE
,
1279 _nested_company_league_widgets
, lengthof(_nested_company_league_widgets
)
1282 void ShowCompanyLeagueTable()
1284 AllocateWindowDescFront
<CompanyLeagueWindow
>(&_company_league_desc
, 0);
1287 /*****************************/
1288 /* PERFORMANCE RATING DETAIL */
1289 /*****************************/
1291 struct PerformanceRatingDetailWindow
: Window
{
1292 static CompanyID company
;
1295 PerformanceRatingDetailWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
1297 this->UpdateCompanyStats();
1299 this->InitNested(window_number
);
1300 this->OnInvalidateData(INVALID_COMPANY
);
1303 void UpdateCompanyStats()
1305 /* Update all company stats with the current data
1306 * (this is because _score_info is not saved to a savegame) */
1307 for (Company
*c
: Company::Iterate()) {
1308 UpdateCompanyRatingAndValue(c
, false);
1311 this->timeout
= DAY_TICKS
* 5;
1314 uint score_info_left
;
1315 uint score_info_right
;
1320 uint score_detail_left
;
1321 uint score_detail_right
;
1323 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
1326 case WID_PRD_SCORE_FIRST
:
1327 this->bar_height
= FONT_HEIGHT_NORMAL
+ WidgetDimensions::scaled
.fullbevel
.Vertical();
1328 size
->height
= this->bar_height
+ WidgetDimensions::scaled
.matrix
.Vertical();
1330 uint score_info_width
= 0;
1331 for (uint i
= SCORE_BEGIN
; i
< SCORE_END
; i
++) {
1332 score_info_width
= std::max(score_info_width
, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES
+ i
).width
);
1334 SetDParamMaxValue(0, 1000);
1335 score_info_width
+= GetStringBoundingBox(STR_BLACK_COMMA
).width
+ WidgetDimensions::scaled
.hsep_wide
;
1337 SetDParamMaxValue(0, 100);
1338 this->bar_width
= GetStringBoundingBox(STR_PERFORMANCE_DETAIL_PERCENT
).width
+ WidgetDimensions::scaled
.hsep_indent
* 2; // Wide bars!
1340 /* At this number we are roughly at the max; it can become wider,
1341 * but then you need at 1000 times more money. At that time you're
1342 * not that interested anymore in the last few digits anyway.
1343 * The 500 is because 999 999 500 to 999 999 999 are rounded to
1344 * 1 000 M, and not 999 999 k. Use negative numbers to account for
1345 * the negative income/amount of money etc. as well. */
1346 int max
= -(999999999 - 500);
1348 /* Scale max for the display currency. Prior to rendering the value
1349 * is converted into the display currency, which may cause it to
1350 * raise significantly. We need to compensate for that since {{CURRCOMPACT}}
1351 * is used, which can produce quite short renderings of very large
1352 * values. Otherwise the calculated width could be too narrow.
1353 * Note that it doesn't work if there was a currency with an exchange
1354 * rate greater than max.
1355 * When the currency rate is more than 1000, the 999 999 k becomes at
1356 * least 999 999 M which roughly is equally long. Furthermore if the
1357 * exchange rate is that high, 999 999 k is usually not enough anymore
1358 * to show the different currency numbers. */
1359 if (_currency
->rate
< 1000) max
/= _currency
->rate
;
1362 uint score_detail_width
= GetStringBoundingBox(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY
).width
;
1364 size
->width
= WidgetDimensions::scaled
.frametext
.Horizontal() + score_info_width
+ WidgetDimensions::scaled
.hsep_wide
+ this->bar_width
+ WidgetDimensions::scaled
.hsep_wide
+ score_detail_width
;
1365 uint left
= WidgetDimensions::scaled
.frametext
.left
;
1366 uint right
= size
->width
- WidgetDimensions::scaled
.frametext
.right
;
1368 bool rtl
= _current_text_dir
== TD_RTL
;
1369 this->score_info_left
= rtl
? right
- score_info_width
: left
;
1370 this->score_info_right
= rtl
? right
: left
+ score_info_width
;
1372 this->score_detail_left
= rtl
? left
: right
- score_detail_width
;
1373 this->score_detail_right
= rtl
? left
+ score_detail_width
: right
;
1375 this->bar_left
= left
+ (rtl
? score_detail_width
: score_info_width
) + WidgetDimensions::scaled
.hsep_wide
;
1376 this->bar_right
= this->bar_left
+ this->bar_width
- 1;
1381 void DrawWidget(const Rect
&r
, int widget
) const override
1383 /* No need to draw when there's nothing to draw */
1384 if (this->company
== INVALID_COMPANY
) return;
1386 if (IsInsideMM(widget
, WID_PRD_COMPANY_FIRST
, WID_PRD_COMPANY_LAST
+ 1)) {
1387 if (this->IsWidgetDisabled(widget
)) return;
1388 CompanyID cid
= (CompanyID
)(widget
- WID_PRD_COMPANY_FIRST
);
1389 int offset
= (cid
== this->company
) ? WidgetDimensions::scaled
.pressed
: 0;
1390 Dimension sprite_size
= GetSpriteSize(SPR_COMPANY_ICON
);
1391 DrawCompanyIcon(cid
, CenterBounds(r
.left
, r
.right
, sprite_size
.width
) + offset
, CenterBounds(r
.top
, r
.bottom
, sprite_size
.height
) + offset
);
1395 if (!IsInsideMM(widget
, WID_PRD_SCORE_FIRST
, WID_PRD_SCORE_LAST
+ 1)) return;
1397 ScoreID score_type
= (ScoreID
)(widget
- WID_PRD_SCORE_FIRST
);
1399 /* The colours used to show how the progress is going */
1400 int colour_done
= _colour_gradient
[COLOUR_GREEN
][4];
1401 int colour_notdone
= _colour_gradient
[COLOUR_RED
][4];
1403 /* Draw all the score parts */
1404 int64 val
= _score_part
[company
][score_type
];
1405 int64 needed
= _score_info
[score_type
].needed
;
1406 int score
= _score_info
[score_type
].score
;
1408 /* SCORE_TOTAL has its own rules ;) */
1409 if (score_type
== SCORE_TOTAL
) {
1410 for (ScoreID i
= SCORE_BEGIN
; i
< SCORE_END
; i
++) score
+= _score_info
[i
].score
;
1414 uint bar_top
= CenterBounds(r
.top
, r
.bottom
, this->bar_height
);
1415 uint text_top
= CenterBounds(r
.top
, r
.bottom
, FONT_HEIGHT_NORMAL
);
1417 DrawString(this->score_info_left
, this->score_info_right
, text_top
, STR_PERFORMANCE_DETAIL_VEHICLES
+ score_type
);
1419 /* Draw the score */
1420 SetDParam(0, score
);
1421 DrawString(this->score_info_left
, this->score_info_right
, text_top
, STR_BLACK_COMMA
, TC_FROMSTRING
, SA_RIGHT
);
1423 /* Calculate the %-bar */
1424 uint x
= Clamp
<int64
>(val
, 0, needed
) * this->bar_width
/ needed
;
1425 bool rtl
= _current_text_dir
== TD_RTL
;
1427 x
= this->bar_right
- x
;
1429 x
= this->bar_left
+ x
;
1433 if (x
!= this->bar_left
) GfxFillRect(this->bar_left
, bar_top
, x
, bar_top
+ this->bar_height
- 1, rtl
? colour_notdone
: colour_done
);
1434 if (x
!= this->bar_right
) GfxFillRect(x
, bar_top
, this->bar_right
, bar_top
+ this->bar_height
- 1, rtl
? colour_done
: colour_notdone
);
1437 SetDParam(0, Clamp
<int64
>(val
, 0, needed
) * 100 / needed
);
1438 DrawString(this->bar_left
, this->bar_right
, text_top
, STR_PERFORMANCE_DETAIL_PERCENT
, TC_FROMSTRING
, SA_HOR_CENTER
);
1440 /* SCORE_LOAN is inversed */
1441 if (score_type
== SCORE_LOAN
) val
= needed
- val
;
1443 /* Draw the amount we have against what is needed
1444 * For some of them it is in currency format */
1446 SetDParam(1, needed
);
1447 switch (score_type
) {
1448 case SCORE_MIN_PROFIT
:
1449 case SCORE_MIN_INCOME
:
1450 case SCORE_MAX_INCOME
:
1453 DrawString(this->score_detail_left
, this->score_detail_right
, text_top
, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY
);
1456 DrawString(this->score_detail_left
, this->score_detail_right
, text_top
, STR_PERFORMANCE_DETAIL_AMOUNT_INT
);
1460 void OnClick(Point pt
, int widget
, int click_count
) override
1462 /* Check which button is clicked */
1463 if (IsInsideMM(widget
, WID_PRD_COMPANY_FIRST
, WID_PRD_COMPANY_LAST
+ 1)) {
1464 /* Is it no on disable? */
1465 if (!this->IsWidgetDisabled(widget
)) {
1466 this->RaiseWidget(this->company
+ WID_PRD_COMPANY_FIRST
);
1467 this->company
= (CompanyID
)(widget
- WID_PRD_COMPANY_FIRST
);
1468 this->LowerWidget(this->company
+ WID_PRD_COMPANY_FIRST
);
1474 void OnGameTick() override
1476 /* Update the company score every 5 days */
1477 if (--this->timeout
== 0) {
1478 this->UpdateCompanyStats();
1484 * Some data on this window has become invalid.
1485 * @param data the company ID of the company that is going to be removed
1486 * @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.
1488 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1490 if (!gui_scope
) return;
1491 /* Disable the companies who are not active */
1492 for (CompanyID i
= COMPANY_FIRST
; i
< MAX_COMPANIES
; i
++) {
1493 this->SetWidgetDisabledState(i
+ WID_PRD_COMPANY_FIRST
, !Company::IsValidID(i
));
1496 /* Check if the currently selected company is still active. */
1497 if (this->company
!= INVALID_COMPANY
&& !Company::IsValidID(this->company
)) {
1498 /* Raise the widget for the previous selection. */
1499 this->RaiseWidget(this->company
+ WID_PRD_COMPANY_FIRST
);
1500 this->company
= INVALID_COMPANY
;
1503 if (this->company
== INVALID_COMPANY
) {
1504 for (const Company
*c
: Company::Iterate()) {
1505 this->company
= c
->index
;
1510 /* Make sure the widget is lowered */
1511 this->LowerWidget(this->company
+ WID_PRD_COMPANY_FIRST
);
1515 CompanyID
PerformanceRatingDetailWindow::company
= INVALID_COMPANY
;
1518 * Make a vertical list of panels for outputting score details.
1519 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
1520 * @return Panel with performance details.
1521 * @post \c *biggest_index contains the largest used index in the tree.
1523 static NWidgetBase
*MakePerformanceDetailPanels(int *biggest_index
)
1525 const StringID performance_tips
[] = {
1526 STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP
,
1527 STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP
,
1528 STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP
,
1529 STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP
,
1530 STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP
,
1531 STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP
,
1532 STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP
,
1533 STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP
,
1534 STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP
,
1535 STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP
,
1538 static_assert(lengthof(performance_tips
) == SCORE_END
- SCORE_BEGIN
);
1540 NWidgetVertical
*vert
= new NWidgetVertical(NC_EQUALSIZE
);
1541 for (int widnum
= WID_PRD_SCORE_FIRST
; widnum
<= WID_PRD_SCORE_LAST
; widnum
++) {
1542 NWidgetBackground
*panel
= new NWidgetBackground(WWT_PANEL
, COLOUR_BROWN
, widnum
);
1543 panel
->SetFill(1, 1);
1544 panel
->SetDataTip(0x0, performance_tips
[widnum
- WID_PRD_SCORE_FIRST
]);
1547 *biggest_index
= WID_PRD_SCORE_LAST
;
1551 /** Make a number of rows with buttons for each company for the performance rating detail window. */
1552 NWidgetBase
*MakeCompanyButtonRowsGraphGUI(int *biggest_index
)
1554 return MakeCompanyButtonRows(biggest_index
, WID_PRD_COMPANY_FIRST
, WID_PRD_COMPANY_LAST
, COLOUR_BROWN
, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP
);
1557 static const NWidgetPart _nested_performance_rating_detail_widgets
[] = {
1558 NWidget(NWID_HORIZONTAL
),
1559 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1560 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_PERFORMANCE_DETAIL
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1561 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1562 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1564 NWidget(WWT_PANEL
, COLOUR_BROWN
),
1565 NWidgetFunction(MakeCompanyButtonRowsGraphGUI
), SetPadding(2),
1567 NWidgetFunction(MakePerformanceDetailPanels
),
1570 static WindowDesc
_performance_rating_detail_desc(
1571 WDP_AUTO
, "league_details", 0, 0,
1572 WC_PERFORMANCE_DETAIL
, WC_NONE
,
1574 _nested_performance_rating_detail_widgets
, lengthof(_nested_performance_rating_detail_widgets
)
1577 void ShowPerformanceRatingDetail()
1579 AllocateWindowDescFront
<PerformanceRatingDetailWindow
>(&_performance_rating_detail_desc
, 0);
1582 void InitializeGraphGui()
1584 _legend_excluded_companies
= 0;
1585 _legend_excluded_cargo
= 0;