OpenTTD Source 15.0-beta2
graph_gui.cpp
Go to the documentation of this file.
1/*
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/>.
6 */
7
10#include "stdafx.h"
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 "gfx_func.h"
21#include "currency.h"
22#include "timer/timer.h"
23#include "timer/timer_window.h"
26#include "zoom_func.h"
27#include "industry.h"
28
30
31#include "table/strings.h"
32#include "table/sprites.h"
33
34#include "safeguards.h"
35
36/* Bitmasks of company and cargo indices that shouldn't be drawn. */
37static CompanyMask _legend_excluded_companies;
38static CargoTypes _legend_excluded_cargo_payment_rates;
39static CargoTypes _legend_excluded_cargo_production_history;
40
41/* Apparently these don't play well with enums. */
42static const OverflowSafeInt64 INVALID_DATAPOINT(INT64_MAX); // Value used for a datapoint that shouldn't be drawn.
43static const uint INVALID_DATAPOINT_POS = UINT_MAX; // Used to determine if the previous point was drawn.
44
45constexpr double INT64_MAX_IN_DOUBLE = static_cast<double>(INT64_MAX - 512);
46static_assert(static_cast<int64_t>(INT64_MAX_IN_DOUBLE) < INT64_MAX);
47
48/****************/
49/* GRAPH LEGEND */
50/****************/
51
54 {
55 this->InitNested(window_number);
56
57 for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
58 if (!_legend_excluded_companies.Test(c)) this->LowerWidget(WID_GL_FIRST_COMPANY + c);
59
60 this->OnInvalidateData(c.base());
61 }
62 }
63
64 void DrawWidget(const Rect &r, WidgetID widget) const override
65 {
66 if (!IsInsideMM(widget, WID_GL_FIRST_COMPANY, WID_GL_FIRST_COMPANY + MAX_COMPANIES)) return;
67
69
70 if (!Company::IsValidID(cid)) return;
71
72 bool rtl = _current_text_dir == TD_RTL;
73
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));
77
78 const Rect tr = ir.Indent(d.width + WidgetDimensions::scaled.hsep_normal, rtl);
79 DrawString(tr.left, tr.right, CenterBounds(tr.top, tr.bottom, GetCharacterHeight(FS_NORMAL)), GetString(STR_COMPANY_NAME_COMPANY_NUM, cid, cid), _legend_excluded_companies.Test(cid) ? TC_BLACK : TC_WHITE);
80 }
81
82 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
83 {
84 if (!IsInsideMM(widget, WID_GL_FIRST_COMPANY, WID_GL_FIRST_COMPANY + MAX_COMPANIES)) return;
85
86 _legend_excluded_companies.Flip(static_cast<CompanyID>(widget - WID_GL_FIRST_COMPANY));
87 this->ToggleWidgetLoweredState(widget);
88 this->SetDirty();
94 }
95
101 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
102 {
103 if (!gui_scope) return;
104 if (Company::IsValidID(data)) return;
105
106 _legend_excluded_companies.Set(static_cast<CompanyID>(data));
107 this->RaiseWidget(data + WID_GL_FIRST_COMPANY);
108 }
109};
110
115static std::unique_ptr<NWidgetBase> MakeNWidgetCompanyLines()
116{
117 auto vert = std::make_unique<NWidgetVertical>(NWidContainerFlag::EqualSize);
118 vert->SetPadding(2, 2, 2, 2);
119 uint sprite_height = GetSpriteSize(SPR_COMPANY_ICON, nullptr, ZOOM_LVL_NORMAL).height;
120
121 for (WidgetID widnum = WID_GL_FIRST_COMPANY; widnum <= WID_GL_LAST_COMPANY; widnum++) {
122 auto panel = std::make_unique<NWidgetBackground>(WWT_PANEL, COLOUR_BROWN, widnum);
123 panel->SetMinimalSize(246, sprite_height + WidgetDimensions::unscaled.framerect.Vertical());
124 panel->SetMinimalTextLines(1, WidgetDimensions::unscaled.framerect.Vertical(), FS_NORMAL);
125 panel->SetFill(1, 1);
126 panel->SetToolTip(STR_GRAPH_KEY_COMPANY_SELECTION_TOOLTIP);
127 vert->Add(std::move(panel));
128 }
129 return vert;
130}
131
132static constexpr NWidgetPart _nested_graph_legend_widgets[] = {
134 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
135 NWidget(WWT_CAPTION, COLOUR_BROWN), SetStringTip(STR_GRAPH_KEY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
136 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
137 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
138 EndContainer(),
139 NWidget(WWT_PANEL, COLOUR_BROWN, WID_GL_BACKGROUND),
141 EndContainer(),
142};
143
144static WindowDesc _graph_legend_desc(
145 WDP_AUTO, "graph_legend", 0, 0,
147 {},
148 _nested_graph_legend_widgets
149);
150
151static void ShowGraphLegend()
152{
153 AllocateWindowDescFront<GraphLegendWindow>(_graph_legend_desc, 0);
154}
155
161
162/******************/
163/* BASE OF GRAPHS */
164/*****************/
165
167protected:
168 static const int GRAPH_MAX_DATASETS = 64;
169 static const int GRAPH_BASE_COLOUR = GREY_SCALE(2);
170 static const int GRAPH_GRID_COLOUR = GREY_SCALE(3);
171 static const int GRAPH_AXIS_LINE_COLOUR = GREY_SCALE(1);
172 static const int GRAPH_ZERO_LINE_COLOUR = GREY_SCALE(8);
173 static const int GRAPH_YEAR_LINE_COLOUR = GREY_SCALE(5);
174 static const int GRAPH_NUM_MONTHS = 24;
175 static const int GRAPH_PAYMENT_RATE_STEPS = 20;
176 static const int PAYMENT_GRAPH_X_STEP_DAYS = 10;
177 static const int PAYMENT_GRAPH_X_STEP_SECONDS = 20;
178 static const int ECONOMY_QUARTER_MINUTES = 3;
179 static const int ECONOMY_MONTH_MINUTES = 1;
180
181 static const TextColour GRAPH_AXIS_LABEL_COLOUR = TC_BLACK;
182
183 static const int MIN_GRAPH_NUM_LINES_Y = 9;
184 static const int MIN_GRID_PIXEL_SIZE = 20;
185
186 uint64_t excluded_data = 0;
187 uint64_t excluded_range = 0;
188 uint8_t num_on_x_axis = 0;
189 uint8_t num_vert_lines = GRAPH_NUM_MONTHS;
190
191 /* The starting month and year that values are plotted against. */
193 TimerGameEconomy::Year year{};
194 uint8_t month_increment = 3;
195
196 bool draw_dates = true;
197
198 /* These values are used if the graph is being plotted against values
199 * rather than the dates specified by month and year. */
200 bool x_values_reversed = true;
201 int16_t x_values_increment = ECONOMY_QUARTER_MINUTES;
202
203 StringID format_str_y_axis{};
204
205 struct DataSet {
206 std::array<OverflowSafeInt64, GRAPH_NUM_MONTHS> values;
207 uint8_t colour;
208 uint8_t exclude_bit;
209 uint8_t range_bit;
210 uint8_t dash;
211 };
212 std::vector<DataSet> data{};
213
214 std::span<const StringID> ranges = {};
215
221 std::span<const OverflowSafeInt64> GetDataSetRange(const DataSet &dataset) const
222 {
223 return {std::begin(dataset.values), std::begin(dataset.values) + this->num_on_x_axis};
224 }
225
232 ValuesInterval GetValuesInterval(int num_hori_lines) const
233 {
234 assert(num_hori_lines > 0);
235
236 ValuesInterval current_interval;
237 current_interval.highest = INT64_MIN;
238 current_interval.lowest = INT64_MAX;
239
240 for (const DataSet &dataset : this->data) {
241 if (HasBit(this->excluded_data, dataset.exclude_bit)) continue;
242 if (HasBit(this->excluded_range, dataset.range_bit)) continue;
243
244 for (const OverflowSafeInt64 &datapoint : this->GetDataSetRange(dataset)) {
245 if (datapoint != INVALID_DATAPOINT) {
246 current_interval.highest = std::max(current_interval.highest, datapoint);
247 current_interval.lowest = std::min(current_interval.lowest, datapoint);
248 }
249 }
250 }
251
252 /* Always include zero in the shown range. */
253 double abs_lower = (current_interval.lowest > 0) ? 0 : (double)abs(current_interval.lowest);
254 double abs_higher = (current_interval.highest < 0) ? 0 : (double)current_interval.highest;
255
256 /* Prevent showing values too close to the graph limits. */
257 abs_higher = (11.0 * abs_higher) / 10.0;
258 abs_lower = (11.0 * abs_lower) / 10.0;
259
260 int num_pos_grids;
261 OverflowSafeInt64 grid_size;
262
263 if (abs_lower != 0 || abs_higher != 0) {
264 /* The number of grids to reserve for the positive part is: */
265 num_pos_grids = (int)floor(0.5 + num_hori_lines * abs_higher / (abs_higher + abs_lower));
266
267 /* If there are any positive or negative values, force that they have at least one grid. */
268 if (num_pos_grids == 0 && abs_higher != 0) num_pos_grids++;
269 if (num_pos_grids == num_hori_lines && abs_lower != 0) num_pos_grids--;
270
271 /* Get the required grid size for each side and use the maximum one. */
272
273 OverflowSafeInt64 grid_size_higher = 0;
274 if (abs_higher > 0) {
275 grid_size_higher = abs_higher > INT64_MAX_IN_DOUBLE ? INT64_MAX : static_cast<int64_t>(abs_higher);
276 grid_size_higher = (grid_size_higher + num_pos_grids - 1) / num_pos_grids;
277 }
278
279 OverflowSafeInt64 grid_size_lower = 0;
280 if (abs_lower > 0) {
281 grid_size_lower = abs_lower > INT64_MAX_IN_DOUBLE ? INT64_MAX : static_cast<int64_t>(abs_lower);
282 grid_size_lower = (grid_size_lower + num_hori_lines - num_pos_grids - 1) / (num_hori_lines - num_pos_grids);
283 }
284
285 grid_size = std::max(grid_size_higher, grid_size_lower);
286 } else {
287 /* If both values are zero, show an empty graph. */
288 num_pos_grids = num_hori_lines / 2;
289 grid_size = 1;
290 }
291
292 current_interval.highest = num_pos_grids * grid_size;
293 current_interval.lowest = -(num_hori_lines - num_pos_grids) * grid_size;
294 return current_interval;
295 }
296
302 uint GetYLabelWidth(ValuesInterval current_interval, int num_hori_lines) const
303 {
304 /* draw text strings on the y axis */
305 int64_t y_label = current_interval.highest;
306 int64_t y_label_separation = (current_interval.highest - current_interval.lowest) / num_hori_lines;
307
308 uint max_width = 0;
309
310 for (int i = 0; i < (num_hori_lines + 1); i++) {
311 Dimension d = GetStringBoundingBox(GetString(STR_GRAPH_Y_LABEL, this->format_str_y_axis, y_label));
312 if (d.width > max_width) max_width = d.width;
313
314 y_label -= y_label_separation;
315 }
316
317 return max_width;
318 }
319
324 void DrawGraph(Rect r) const
325 {
326 uint x, y;
327 ValuesInterval interval;
328 int x_axis_offset;
329
330 /* the colours and cost array of GraphDrawer must accommodate
331 * both values for cargo and companies. So if any are higher, quit */
332 static_assert(GRAPH_MAX_DATASETS >= (int)NUM_CARGO && GRAPH_MAX_DATASETS >= (int)MAX_COMPANIES);
333 assert(this->num_vert_lines > 0);
334
335 bool rtl = _current_text_dir == TD_RTL;
336
337 /* Rect r will be adjusted to contain just the graph, with labels being
338 * placed outside the area. */
339 r.top += ScaleGUITrad(5) + GetCharacterHeight(FS_SMALL) / 2;
340 r.bottom -= (this->draw_dates ? 2 : 1) * GetCharacterHeight(FS_SMALL) + ScaleGUITrad(4);
341 r.left += ScaleGUITrad(rtl ? 5 : 9);
342 r.right -= ScaleGUITrad(rtl ? 9 : 5);
343
344 /* Initial number of horizontal lines. */
345 int num_hori_lines = 160 / ScaleGUITrad(MIN_GRID_PIXEL_SIZE);
346 /* For the rest of the height, the number of horizontal lines will increase more slowly. */
347 int resize = (r.bottom - r.top - 160) / (2 * ScaleGUITrad(MIN_GRID_PIXEL_SIZE));
348 if (resize > 0) num_hori_lines += resize;
349
350 interval = GetValuesInterval(num_hori_lines);
351
352 int label_width = GetYLabelWidth(interval, num_hori_lines);
353
354 if (rtl) {
355 r.right -= label_width;
356 } else {
357 r.left += label_width;
358 }
359
360 int x_sep = (r.right - r.left) / this->num_vert_lines;
361 int y_sep = (r.bottom - r.top) / num_hori_lines;
362
363 /* Redetermine right and bottom edge of graph to fit with the integer
364 * separation values. */
365 if (rtl) {
366 r.left = r.right - x_sep * this->num_vert_lines;
367 } else {
368 r.right = r.left + x_sep * this->num_vert_lines;
369 }
370 r.bottom = r.top + y_sep * num_hori_lines;
371
372 OverflowSafeInt64 interval_size = interval.highest + abs(interval.lowest);
373 /* Where to draw the X axis. Use floating point to avoid overflowing and results of zero. */
374 x_axis_offset = (int)((r.bottom - r.top) * (double)interval.highest / (double)interval_size);
375
376 /* Draw the background of the graph itself. */
377 GfxFillRect(r.left, r.top, r.right, r.bottom, GRAPH_BASE_COLOUR);
378
379 /* Draw the vertical grid lines. */
380
381 /* Don't draw the first line, as that's where the axis will be. */
382 if (rtl) {
383 x_sep = -x_sep;
384 x = r.right + x_sep;
385 } else {
386 x = r.left + x_sep;
387 }
388
389 int grid_colour = GRAPH_GRID_COLOUR;
390 for (int i = 1; i < this->num_vert_lines + 1; i++) {
391 /* If using wallclock units, we separate periods with a lighter line. */
393 grid_colour = (i % 4 == 0) ? GRAPH_YEAR_LINE_COLOUR : GRAPH_GRID_COLOUR;
394 }
395 GfxFillRect(x, r.top, x, r.bottom, grid_colour);
396 x += x_sep;
397 }
398
399 /* Draw the horizontal grid lines. */
400 y = r.bottom;
401
402 for (int i = 0; i < (num_hori_lines + 1); i++) {
403 if (rtl) {
404 GfxFillRect(r.right + 1, y, r.right + ScaleGUITrad(3), y, GRAPH_AXIS_LINE_COLOUR);
405 } else {
406 GfxFillRect(r.left - ScaleGUITrad(3), y, r.left - 1, y, GRAPH_AXIS_LINE_COLOUR);
407 }
408 GfxFillRect(r.left, y, r.right, y, GRAPH_GRID_COLOUR);
409 y -= y_sep;
410 }
411
412 /* Draw the y axis. */
413 GfxFillRect(r.left, r.top, r.left, r.bottom, GRAPH_AXIS_LINE_COLOUR);
414
415 /* Draw the x axis. */
416 y = x_axis_offset + r.top;
417 GfxFillRect(r.left, y, r.right, y, GRAPH_ZERO_LINE_COLOUR);
418
419 /* Find the largest value that will be drawn. */
420 if (this->num_on_x_axis == 0) return;
421
422 assert(this->num_on_x_axis > 0);
423
424 /* draw text strings on the y axis */
425 int64_t y_label = interval.highest;
426 int64_t y_label_separation = abs(interval.highest - interval.lowest) / num_hori_lines;
427
428 y = r.top - GetCharacterHeight(FS_SMALL) / 2;
429
430 for (int i = 0; i < (num_hori_lines + 1); i++) {
431 if (rtl) {
432 DrawString(r.right + ScaleGUITrad(4), r.right + label_width + ScaleGUITrad(4), y,
433 GetString(STR_GRAPH_Y_LABEL, this->format_str_y_axis, y_label),
435 } else {
436 DrawString(r.left - label_width - ScaleGUITrad(4), r.left - ScaleGUITrad(4), y,
437 GetString(STR_GRAPH_Y_LABEL, this->format_str_y_axis, y_label),
439 }
440
441 y_label -= y_label_separation;
442 y += y_sep;
443 }
444
445 /* Draw x-axis labels and markings for graphs based on financial quarters and years. */
446 if (this->draw_dates) {
447 x = rtl ? r.right : r.left;
448 y = r.bottom + ScaleGUITrad(2);
449 TimerGameEconomy::Month month = this->month;
450 TimerGameEconomy::Year year = this->year;
451 for (int i = 0; i < this->num_on_x_axis; i++) {
452 if (rtl) {
453 DrawStringMultiLine(x + x_sep, x, y, this->height,
454 GetString(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH, STR_MONTH_ABBREV_JAN + month, year),
456 } else {
457 DrawStringMultiLine(x, x + x_sep, y, this->height,
458 GetString(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH, STR_MONTH_ABBREV_JAN + month, year),
460 }
461
462 month += this->month_increment;
463 if (month >= 12) {
464 month = 0;
465 year++;
466
467 /* Draw a lighter grid line between years. Top and bottom adjustments ensure we don't draw over top and bottom horizontal grid lines. */
468 GfxFillRect(x + x_sep, r.top + 1, x + x_sep, r.bottom - 1, GRAPH_YEAR_LINE_COLOUR);
469 }
470 x += x_sep;
471 }
472 } else {
473 /* Draw x-axis labels for graphs not based on quarterly performance (cargo payment rates, and all graphs when using wallclock units). */
474 x = rtl ? r.right : r.left;
475 y = r.bottom + ScaleGUITrad(2);
476
477 int16_t iterator;
478 uint16_t label;
479 if (this->x_values_reversed) {
480 label = this->x_values_increment * this->num_on_x_axis;
481 iterator = -this->x_values_increment;
482 } else {
483 label = this->x_values_increment;
484 iterator = this->x_values_increment;
485 }
486
487 for (int i = 0; i < this->num_on_x_axis; i++) {
488 if (rtl) {
489 DrawString(x + x_sep + 1, x - 1, y, GetString(STR_GRAPH_Y_LABEL_NUMBER, label), GRAPH_AXIS_LABEL_COLOUR, SA_HOR_CENTER);
490 } else {
491 DrawString(x + 1, x + x_sep - 1, y, GetString(STR_GRAPH_Y_LABEL_NUMBER, label), GRAPH_AXIS_LABEL_COLOUR, SA_HOR_CENTER);
492 }
493
494 label += iterator;
495 x += x_sep;
496 }
497 }
498
499 /* draw lines and dots */
501 uint pointoffs1 = (linewidth + 1) / 2;
502 uint pointoffs2 = linewidth + 1 - pointoffs1;
503
504 for (const DataSet &dataset : this->data) {
505 if (HasBit(this->excluded_data, dataset.exclude_bit)) continue;
506 if (HasBit(this->excluded_range, dataset.range_bit)) continue;
507
508 /* Centre the dot between the grid lines. */
509 if (rtl) {
510 x = r.right + (x_sep / 2);
511 } else {
512 x = r.left + (x_sep / 2);
513 }
514
515 uint prev_x = INVALID_DATAPOINT_POS;
516 uint prev_y = INVALID_DATAPOINT_POS;
517
518 const uint dash = ScaleGUITrad(dataset.dash);
519 for (OverflowSafeInt64 datapoint : this->GetDataSetRange(dataset)) {
520 if (datapoint != INVALID_DATAPOINT) {
521 /*
522 * Check whether we need to reduce the 'accuracy' of the
523 * datapoint value and the highest value to split overflows.
524 * And when 'drawing' 'one million' or 'one million and one'
525 * there is no significant difference, so the least
526 * significant bits can just be removed.
527 *
528 * If there are more bits needed than would fit in a 32 bits
529 * integer, so at about 31 bits because of the sign bit, the
530 * least significant bits are removed.
531 */
532 int mult_range = FindLastBit<uint32_t>(x_axis_offset) + FindLastBit<uint64_t>(abs(datapoint));
533 int reduce_range = std::max(mult_range - 31, 0);
534
535 /* Handle negative values differently (don't shift sign) */
536 if (datapoint < 0) {
537 datapoint = -(abs(datapoint) >> reduce_range);
538 } else {
539 datapoint >>= reduce_range;
540 }
541 y = r.top + x_axis_offset - ((r.bottom - r.top) * datapoint) / (interval_size >> reduce_range);
542
543 /* Draw the point. */
544 GfxFillRect(x - pointoffs1, y - pointoffs1, x + pointoffs2, y + pointoffs2, dataset.colour);
545
546 /* Draw the line connected to the previous point. */
547 if (prev_x != INVALID_DATAPOINT_POS) GfxDrawLine(prev_x, prev_y, x, y, dataset.colour, linewidth, dash);
548
549 prev_x = x;
550 prev_y = y;
551 } else {
552 prev_x = INVALID_DATAPOINT_POS;
553 prev_y = INVALID_DATAPOINT_POS;
554 }
555
556 x += x_sep;
557 }
558 }
559 }
560
561 BaseGraphWindow(WindowDesc &desc, StringID format_str_y_axis) :
562 Window(desc),
563 format_str_y_axis(format_str_y_axis)
564 {
566 }
567
568 void InitializeWindow(WindowNumber number)
569 {
570 /* Initialise the dataset */
571 this->UpdateStatistics(true);
572
573 this->CreateNestedTree();
574
575 auto *wid = this->GetWidget<NWidgetCore>(WID_GRAPH_FOOTER);
576 if (wid != nullptr && TimerGameEconomy::UsingWallclockUnits()) {
577 wid->SetString(STR_GRAPH_LAST_72_MINUTES_TIME_LABEL);
578 }
579
580 this->FinishInitNested(number);
581 }
582
583public:
584 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
585 {
586 switch (widget) {
588 for (const StringID &str : this->ranges) {
589 size = maxdim(size, GetStringBoundingBox(str, FS_SMALL));
590 }
591
594
595 /* Set fixed height for number of ranges. */
596 size.height *= static_cast<uint>(std::size(this->ranges));
597
598 resize.width = 0;
599 resize.height = 0;
600 this->GetWidget<NWidgetCore>(WID_GRAPH_RANGE_MATRIX)->SetMatrixDimension(1, ClampTo<uint32_t>(std::size(this->ranges)));
601 break;
602
603 case WID_GRAPH_GRAPH: {
604 uint x_label_width = 0;
605
606 /* Draw x-axis labels and markings for graphs based on financial quarters and years. */
607 if (this->draw_dates) {
608 TimerGameEconomy::Month month = this->month;
609 TimerGameEconomy::Year year = this->year;
610 for (int i = 0; i < this->num_on_x_axis; i++) {
611 x_label_width = std::max(x_label_width, GetStringBoundingBox(GetString(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH, STR_MONTH_ABBREV_JAN + month, year)).width);
612
613 month += this->month_increment;
614 if (month >= 12) {
615 month = 0;
616 year++;
617 }
618 }
619 } else {
620 /* Draw x-axis labels for graphs not based on quarterly performance (cargo payment rates). */
621 uint64_t max_value = GetParamMaxValue((this->num_on_x_axis + 1) * this->x_values_increment, 0, FS_SMALL);
622 x_label_width = GetStringBoundingBox(GetString(STR_GRAPH_Y_LABEL_NUMBER, max_value)).width;
623 }
624
625 uint y_label_width = GetStringBoundingBox(GetString(STR_GRAPH_Y_LABEL, this->format_str_y_axis, INT64_MAX)).width;
626
627 size.width = std::max<uint>(size.width, ScaleGUITrad(5) + y_label_width + this->num_vert_lines * (x_label_width + ScaleGUITrad(5)) + ScaleGUITrad(9));
628 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));
629 size.height = std::max<uint>(size.height, size.width / 3);
630 break;
631 }
632
633 default: break;
634 }
635 }
636
637 void DrawWidget(const Rect &r, WidgetID widget) const override
638 {
639 switch (widget) {
640 case WID_GRAPH_GRAPH:
641 this->DrawGraph(r);
642 break;
643
646 uint index = 0;
647 Rect line = r.WithHeight(line_height);
648 for (const auto &str : this->ranges) {
649 bool lowered = !HasBit(this->excluded_range, index);
650
651 /* Redraw frame if lowered */
652 if (lowered) DrawFrameRect(line, COLOUR_BROWN, FrameFlag::Lowered);
653
654 const Rect text = line.Shrink(WidgetDimensions::scaled.framerect);
655 DrawString(text, str, TC_BLACK, SA_CENTER, false, FS_SMALL);
656
657 line = line.Translate(0, line_height);
658 ++index;
659 }
660 break;
661 }
662
663 default: break;
664 }
665 }
666
667 virtual OverflowSafeInt64 GetGraphData(const Company *, int)
668 {
669 return INVALID_DATAPOINT;
670 }
671
672 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
673 {
674 /* Clicked on legend? */
675 switch (widget) {
677 ShowGraphLegend();
678 break;
679
681 int row = GetRowFromWidget(pt.y, widget, 0, GetCharacterHeight(FS_SMALL) + WidgetDimensions::scaled.framerect.Vertical());
682
683 ToggleBit(this->excluded_range, row);
684 this->SetDirty();
685 break;
686 }
687
688 default: break;
689 }
690 }
691
692 void OnGameTick() override
693 {
694 this->UpdateStatistics(false);
695 }
696
702 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
703 {
704 if (!gui_scope) return;
705 this->UpdateStatistics(true);
706 }
707
712 virtual void UpdateStatistics(bool initialize)
713 {
714 CompanyMask excluded_companies = _legend_excluded_companies;
715
716 /* Exclude the companies which aren't valid */
717 for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
718 if (!Company::IsValidID(c)) excluded_companies.Set(c);
719 }
720
721 uint8_t nums = 0;
722 for (const Company *c : Company::Iterate()) {
723 nums = std::min(this->num_vert_lines, std::max(nums, c->num_valid_stat_ent));
724 }
725
726 int mo = (TimerGameEconomy::month / this->month_increment - nums) * this->month_increment;
727 auto yr = TimerGameEconomy::year;
728 while (mo < 0) {
729 yr--;
730 mo += 12;
731 }
732
733 if (!initialize && this->excluded_data == excluded_companies.base() && this->num_on_x_axis == nums &&
734 this->year == yr && this->month == mo) {
735 /* There's no reason to get new stats */
736 return;
737 }
738
739 this->excluded_data = excluded_companies.base();
740 this->num_on_x_axis = nums;
741 this->year = yr;
742 this->month = mo;
743
744 this->data.clear();
745 for (CompanyID k = CompanyID::Begin(); k < MAX_COMPANIES; ++k) {
746 const Company *c = Company::GetIfValid(k);
747 if (c == nullptr) continue;
748
749 DataSet &dataset = this->data.emplace_back();
750 dataset.colour = GetColourGradient(c->colour, SHADE_LIGHTER);
751 dataset.exclude_bit = k.base();
752
753 for (int j = this->num_on_x_axis, i = 0; --j >= 0;) {
754 if (j >= c->num_valid_stat_ent) {
755 dataset.values[i] = INVALID_DATAPOINT;
756 } else {
757 /* Ensure we never assign INVALID_DATAPOINT, as that has another meaning.
758 * Instead, use the value just under it. Hopefully nobody will notice. */
759 dataset.values[i] = std::min(GetGraphData(c, j), INVALID_DATAPOINT - 1);
760 }
761 i++;
762 }
763 }
764 }
765};
766
767
768/********************/
769/* OPERATING PROFIT */
770/********************/
771
774 BaseGraphWindow(desc, STR_JUST_CURRENCY_SHORT)
775 {
776 this->num_on_x_axis = GRAPH_NUM_MONTHS;
777 this->num_vert_lines = GRAPH_NUM_MONTHS;
778 this->draw_dates = !TimerGameEconomy::UsingWallclockUnits();
779
780 this->InitializeWindow(window_number);
781 }
782
783 OverflowSafeInt64 GetGraphData(const Company *c, int j) override
784 {
785 return c->old_economy[j].income + c->old_economy[j].expenses;
786 }
787};
788
789static constexpr NWidgetPart _nested_operating_profit_widgets[] = {
791 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
792 NWidget(WWT_CAPTION, COLOUR_BROWN), SetStringTip(STR_GRAPH_OPERATING_PROFIT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
793 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_GRAPH_KEY_BUTTON), SetMinimalSize(50, 0), SetStringTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
794 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
795 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
796 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
797 EndContainer(),
798 NWidget(WWT_PANEL, COLOUR_BROWN, WID_GRAPH_BACKGROUND),
800 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GRAPH_GRAPH), SetMinimalSize(576, 160), SetFill(1, 1), SetResize(1, 1),
802 NWidget(NWID_SPACER), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
803 NWidget(WWT_TEXT, INVALID_COLOUR, WID_GRAPH_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetStringTip(STR_EMPTY),
804 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
805 NWidget(WWT_RESIZEBOX, COLOUR_BROWN, WID_GRAPH_RESIZE), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
806 EndContainer(),
807 EndContainer(),
808 EndContainer(),
809};
810
811static WindowDesc _operating_profit_desc(
812 WDP_AUTO, "graph_operating_profit", 0, 0,
814 {},
815 _nested_operating_profit_widgets
816);
817
818
819void ShowOperatingProfitGraph()
820{
821 AllocateWindowDescFront<OperatingProfitGraphWindow>(_operating_profit_desc, 0);
822}
823
824
825/****************/
826/* INCOME GRAPH */
827/****************/
828
831 BaseGraphWindow(desc, STR_JUST_CURRENCY_SHORT)
832 {
833 this->num_on_x_axis = GRAPH_NUM_MONTHS;
834 this->num_vert_lines = GRAPH_NUM_MONTHS;
835 this->draw_dates = !TimerGameEconomy::UsingWallclockUnits();
836
837 this->InitializeWindow(window_number);
838 }
839
840 OverflowSafeInt64 GetGraphData(const Company *c, int j) override
841 {
842 return c->old_economy[j].income;
843 }
844};
845
846static constexpr NWidgetPart _nested_income_graph_widgets[] = {
848 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
849 NWidget(WWT_CAPTION, COLOUR_BROWN), SetStringTip(STR_GRAPH_INCOME_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
850 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_GRAPH_KEY_BUTTON), SetMinimalSize(50, 0), SetStringTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
851 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
852 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
853 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
854 EndContainer(),
855 NWidget(WWT_PANEL, COLOUR_BROWN, WID_GRAPH_BACKGROUND),
857 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GRAPH_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
859 NWidget(NWID_SPACER), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
860 NWidget(WWT_TEXT, INVALID_COLOUR, WID_GRAPH_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetStringTip(STR_EMPTY),
861 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
862 NWidget(WWT_RESIZEBOX, COLOUR_BROWN, WID_GRAPH_RESIZE), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
863 EndContainer(),
864 EndContainer(),
865 EndContainer(),
866};
867
868static WindowDesc _income_graph_desc(
869 WDP_AUTO, "graph_income", 0, 0,
871 {},
872 _nested_income_graph_widgets
873);
874
875void ShowIncomeGraph()
876{
877 AllocateWindowDescFront<IncomeGraphWindow>(_income_graph_desc, 0);
878}
879
880/*******************/
881/* DELIVERED CARGO */
882/*******************/
883
886 BaseGraphWindow(desc, STR_JUST_COMMA)
887 {
888 this->num_on_x_axis = GRAPH_NUM_MONTHS;
889 this->num_vert_lines = GRAPH_NUM_MONTHS;
890 this->draw_dates = !TimerGameEconomy::UsingWallclockUnits();
891
892 this->InitializeWindow(window_number);
893 }
894
895 OverflowSafeInt64 GetGraphData(const Company *c, int j) override
896 {
897 return c->old_economy[j].delivered_cargo.GetSum<OverflowSafeInt64>();
898 }
899};
900
901static constexpr NWidgetPart _nested_delivered_cargo_graph_widgets[] = {
903 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
904 NWidget(WWT_CAPTION, COLOUR_BROWN), SetStringTip(STR_GRAPH_CARGO_DELIVERED_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
905 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_GRAPH_KEY_BUTTON), SetMinimalSize(50, 0), SetStringTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
906 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
907 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
908 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
909 EndContainer(),
910 NWidget(WWT_PANEL, COLOUR_BROWN, WID_GRAPH_BACKGROUND),
912 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GRAPH_GRAPH), SetMinimalSize(576, 128), SetFill(1, 1), SetResize(1, 1),
914 NWidget(NWID_SPACER), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
915 NWidget(WWT_TEXT, INVALID_COLOUR, WID_GRAPH_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetStringTip(STR_EMPTY),
916 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
917 NWidget(WWT_RESIZEBOX, COLOUR_BROWN, WID_GRAPH_RESIZE), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
918 EndContainer(),
919 EndContainer(),
920 EndContainer(),
921};
922
923static WindowDesc _delivered_cargo_graph_desc(
924 WDP_AUTO, "graph_delivered_cargo", 0, 0,
926 {},
927 _nested_delivered_cargo_graph_widgets
928);
929
930void ShowDeliveredCargoGraph()
931{
932 AllocateWindowDescFront<DeliveredCargoGraphWindow>(_delivered_cargo_graph_desc, 0);
933}
934
935/***********************/
936/* PERFORMANCE HISTORY */
937/***********************/
938
941 BaseGraphWindow(desc, STR_JUST_COMMA)
942 {
943 this->num_on_x_axis = GRAPH_NUM_MONTHS;
944 this->num_vert_lines = GRAPH_NUM_MONTHS;
945 this->draw_dates = !TimerGameEconomy::UsingWallclockUnits();
946
947 this->InitializeWindow(window_number);
948 }
949
950 OverflowSafeInt64 GetGraphData(const Company *c, int j) override
951 {
952 return c->old_economy[j].performance_history;
953 }
954
955 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
956 {
957 if (widget == WID_PHG_DETAILED_PERFORMANCE) ShowPerformanceRatingDetail();
958 this->BaseGraphWindow::OnClick(pt, widget, click_count);
959 }
960};
961
962static constexpr NWidgetPart _nested_performance_history_widgets[] = {
964 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
965 NWidget(WWT_CAPTION, COLOUR_BROWN), SetStringTip(STR_GRAPH_COMPANY_PERFORMANCE_RATINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
966 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_PHG_DETAILED_PERFORMANCE), SetMinimalSize(50, 0), SetStringTip(STR_PERFORMANCE_DETAIL_KEY, STR_GRAPH_PERFORMANCE_DETAIL_TOOLTIP),
967 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_GRAPH_KEY_BUTTON), SetMinimalSize(50, 0), SetStringTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
968 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
969 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
970 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
971 EndContainer(),
972 NWidget(WWT_PANEL, COLOUR_BROWN, WID_GRAPH_BACKGROUND),
974 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GRAPH_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
976 NWidget(NWID_SPACER), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
977 NWidget(WWT_TEXT, INVALID_COLOUR, WID_GRAPH_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetStringTip(STR_EMPTY),
978 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
979 NWidget(WWT_RESIZEBOX, COLOUR_BROWN, WID_GRAPH_RESIZE), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
980 EndContainer(),
981 EndContainer(),
982 EndContainer(),
983};
984
985static WindowDesc _performance_history_desc(
986 WDP_AUTO, "graph_performance", 0, 0,
988 {},
989 _nested_performance_history_widgets
990);
991
992void ShowPerformanceHistoryGraph()
993{
994 AllocateWindowDescFront<PerformanceHistoryGraphWindow>(_performance_history_desc, 0);
995}
996
997/*****************/
998/* COMPANY VALUE */
999/*****************/
1000
1003 BaseGraphWindow(desc, STR_JUST_CURRENCY_SHORT)
1004 {
1005 this->num_on_x_axis = GRAPH_NUM_MONTHS;
1006 this->num_vert_lines = GRAPH_NUM_MONTHS;
1007 this->draw_dates = !TimerGameEconomy::UsingWallclockUnits();
1008
1009 this->InitializeWindow(window_number);
1010 }
1011
1012 OverflowSafeInt64 GetGraphData(const Company *c, int j) override
1013 {
1014 return c->old_economy[j].company_value;
1015 }
1016};
1017
1018static constexpr NWidgetPart _nested_company_value_graph_widgets[] = {
1020 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1021 NWidget(WWT_CAPTION, COLOUR_BROWN), SetStringTip(STR_GRAPH_COMPANY_VALUES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1022 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_GRAPH_KEY_BUTTON), SetMinimalSize(50, 0), SetStringTip(STR_GRAPH_KEY_BUTTON, STR_GRAPH_KEY_TOOLTIP),
1023 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1024 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1025 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1026 EndContainer(),
1027 NWidget(WWT_PANEL, COLOUR_BROWN, WID_GRAPH_BACKGROUND),
1029 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GRAPH_GRAPH), SetMinimalSize(576, 224), SetFill(1, 1), SetResize(1, 1),
1031 NWidget(NWID_SPACER), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
1032 NWidget(WWT_TEXT, INVALID_COLOUR, WID_GRAPH_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetStringTip(STR_EMPTY),
1033 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1034 NWidget(WWT_RESIZEBOX, COLOUR_BROWN, WID_GRAPH_RESIZE), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
1035 EndContainer(),
1036 EndContainer(),
1037 EndContainer(),
1038};
1039
1040static WindowDesc _company_value_graph_desc(
1041 WDP_AUTO, "graph_company_value", 0, 0,
1043 {},
1044 _nested_company_value_graph_widgets
1045);
1046
1047void ShowCompanyValueGraph()
1048{
1049 AllocateWindowDescFront<CompanyValueGraphWindow>(_company_value_graph_desc, 0);
1050}
1051
1052/*****************/
1053/* PAYMENT RATES */
1054/*****************/
1055
1057 uint line_height = 0;
1058 Scrollbar *vscroll = nullptr;
1059 uint legend_width = 0;
1060
1062 BaseGraphWindow(desc, STR_JUST_CURRENCY_SHORT)
1063 {
1064 this->num_on_x_axis = GRAPH_PAYMENT_RATE_STEPS;
1065 this->num_vert_lines = GRAPH_PAYMENT_RATE_STEPS;
1066 this->draw_dates = false;
1067
1068 this->x_values_reversed = false;
1069 /* The x-axis is labeled in either seconds or days. A day is two seconds, so we adjust the label if needed. */
1071
1072 this->CreateNestedTree();
1073 this->vscroll = this->GetScrollbar(WID_GRAPH_MATRIX_SCROLLBAR);
1074 this->vscroll->SetCount(_sorted_standard_cargo_specs.size());
1075
1076 auto *wid = this->GetWidget<NWidgetCore>(WID_GRAPH_FOOTER);
1077 wid->SetString(TimerGameEconomy::UsingWallclockUnits() ? STR_GRAPH_CARGO_PAYMENT_RATES_SECONDS : STR_GRAPH_CARGO_PAYMENT_RATES_DAYS);
1078
1079 /* Initialise the dataset */
1080 this->UpdatePaymentRates();
1081
1082 this->FinishInitNested(window_number);
1083 }
1084
1085 void OnInit() override
1086 {
1087 /* Width of the legend blob. */
1088 this->legend_width = GetCharacterHeight(FS_SMALL) * 9 / 6;
1089 }
1090
1091 void UpdateExcludedData()
1092 {
1093 this->excluded_data = _legend_excluded_cargo_payment_rates;
1094 }
1095
1096 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
1097 {
1098 if (widget != WID_GRAPH_MATRIX) {
1099 BaseGraphWindow::UpdateWidgetSize(widget, size, padding, fill, resize);
1100 return;
1101 }
1102
1104
1105 for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
1106 Dimension d = GetStringBoundingBox(GetString(STR_GRAPH_CARGO_PAYMENT_CARGO, cs->name));
1107 d.width += this->legend_width + WidgetDimensions::scaled.hsep_normal; // colour field
1110 size = maxdim(d, size);
1111 }
1112
1113 this->line_height = size.height;
1114 size.height = this->line_height * 11; /* Default number of cargo types in most climates. */
1115 resize.width = 0;
1116 resize.height = this->line_height;
1117 }
1118
1119 void DrawWidget(const Rect &r, WidgetID widget) const override
1120 {
1121 if (widget != WID_GRAPH_MATRIX) {
1122 BaseGraphWindow::DrawWidget(r, widget);
1123 return;
1124 }
1125
1126 bool rtl = _current_text_dir == TD_RTL;
1127
1128 auto [first, last] = this->vscroll->GetVisibleRangeIterators(_sorted_standard_cargo_specs);
1129
1130 Rect line = r.WithHeight(this->line_height);
1131 for (auto it = first; it != last; ++it) {
1132 const CargoSpec *cs = *it;
1133
1134 bool lowered = !HasBit(_legend_excluded_cargo_payment_rates, cs->Index());
1135
1136 /* Redraw frame if lowered */
1137 if (lowered) DrawFrameRect(line, COLOUR_BROWN, FrameFlag::Lowered);
1138
1139 const Rect text = line.Shrink(WidgetDimensions::scaled.framerect);
1140
1141 /* Cargo-colour box with outline */
1142 const Rect cargo = text.WithWidth(this->legend_width, rtl);
1143 GfxFillRect(cargo, PC_BLACK);
1144 GfxFillRect(cargo.Shrink(WidgetDimensions::scaled.bevel), cs->legend_colour);
1145
1146 /* Cargo name */
1147 DrawString(text.Indent(this->legend_width + WidgetDimensions::scaled.hsep_normal, rtl), GetString(STR_GRAPH_CARGO_PAYMENT_CARGO, cs->name));
1148
1149 line = line.Translate(0, this->line_height);
1150 }
1151 }
1152
1153 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
1154 {
1155 switch (widget) {
1157 /* Remove all cargoes from the excluded lists. */
1158 _legend_excluded_cargo_payment_rates = 0;
1159 this->excluded_data = 0;
1160 this->SetDirty();
1161 break;
1162
1164 /* Add all cargoes to the excluded lists. */
1165 for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
1166 SetBit(_legend_excluded_cargo_payment_rates, cs->Index());
1167 SetBit(this->excluded_data, cs->Index());
1168 }
1169 this->SetDirty();
1170 break;
1171 }
1172
1173 case WID_GRAPH_MATRIX: {
1174 auto it = this->vscroll->GetScrolledItemFromWidget(_sorted_standard_cargo_specs, pt.y, this, WID_GRAPH_MATRIX);
1175 if (it != _sorted_standard_cargo_specs.end()) {
1176 ToggleBit(_legend_excluded_cargo_payment_rates, (*it)->Index());
1177 this->UpdateExcludedData();
1178 this->SetDirty();
1179 }
1180 break;
1181 }
1182
1183 default:
1184 this->BaseGraphWindow::OnClick(pt, widget, click_count);
1185 break;
1186 }
1187 }
1188
1189 void OnResize() override
1190 {
1191 this->vscroll->SetCapacityFromWidget(this, WID_GRAPH_MATRIX);
1192 }
1193
1194 void OnGameTick() override
1195 {
1196 /* Override default OnGameTick */
1197 }
1198
1204 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
1205 {
1206 if (!gui_scope) return;
1207 this->UpdatePaymentRates();
1208 }
1209
1211 IntervalTimer<TimerWindow> update_payment_interval = {std::chrono::seconds(3), [this](auto) {
1212 this->UpdatePaymentRates();
1213 }};
1214
1219 {
1220 this->UpdateExcludedData();
1221
1222 this->data.clear();
1223 for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
1224 DataSet &dataset = this->data.emplace_back();
1225 dataset.colour = cs->legend_colour;
1226 dataset.exclude_bit = cs->Index();
1227
1228 for (uint j = 0; j != this->num_on_x_axis; j++) {
1229 dataset.values[j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->Index());
1230 }
1231 }
1232 }
1233};
1234
1235static constexpr NWidgetPart _nested_cargo_payment_rates_widgets[] = {
1237 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1238 NWidget(WWT_CAPTION, COLOUR_BROWN), SetStringTip(STR_GRAPH_CARGO_PAYMENT_RATES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1239 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1240 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1241 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1242 EndContainer(),
1243 NWidget(WWT_PANEL, COLOUR_BROWN, WID_GRAPH_BACKGROUND), SetMinimalSize(568, 128),
1245 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1246 NWidget(WWT_TEXT, INVALID_COLOUR, WID_GRAPH_HEADER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetStringTip(STR_GRAPH_CARGO_PAYMENT_RATES_TITLE),
1247 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1248 EndContainer(),
1250 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GRAPH_GRAPH), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
1252 NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1),
1253 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_GRAPH_ENABLE_CARGOES), SetStringTip(STR_GRAPH_CARGO_ENABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL), SetFill(1, 0),
1254 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_GRAPH_DISABLE_CARGOES), SetStringTip(STR_GRAPH_CARGO_DISABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL), SetFill(1, 0),
1257 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),
1259 EndContainer(),
1260 NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1),
1261 EndContainer(),
1262 NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
1263 EndContainer(),
1265 NWidget(NWID_SPACER), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
1266 NWidget(WWT_TEXT, INVALID_COLOUR, WID_GRAPH_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0),
1267 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1268 NWidget(WWT_RESIZEBOX, COLOUR_BROWN, WID_GRAPH_RESIZE), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
1269 EndContainer(),
1270 EndContainer(),
1271};
1272
1273static WindowDesc _cargo_payment_rates_desc(
1274 WDP_AUTO, "graph_cargo_payment_rates", 0, 0,
1276 {},
1277 _nested_cargo_payment_rates_widgets
1278);
1279
1280
1281void ShowCargoPaymentRates()
1282{
1283 AllocateWindowDescFront<PaymentRatesGraphWindow>(_cargo_payment_rates_desc, 0);
1284}
1285
1286/*****************************/
1287/* PERFORMANCE RATING DETAIL */
1288/*****************************/
1289
1291 static CompanyID company;
1292 int timeout = 0;
1293 uint score_info_left = 0;
1294 uint score_info_right = 0;
1295 uint bar_left = 0;
1296 uint bar_right = 0;
1297 uint bar_width = 0;
1298 uint bar_height = 0;
1299 uint score_detail_left = 0;
1300 uint score_detail_right = 0;
1301
1303 {
1304 this->UpdateCompanyStats();
1305
1306 this->InitNested(window_number);
1307 this->OnInvalidateData(CompanyID::Invalid().base());
1308 }
1309
1310 void UpdateCompanyStats()
1311 {
1312 /* Update all company stats with the current data
1313 * (this is because _score_info is not saved to a savegame) */
1314 for (Company *c : Company::Iterate()) {
1316 }
1317
1318 this->timeout = Ticks::DAY_TICKS * 5;
1319 }
1320
1321 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
1322 {
1323 switch (widget) {
1326 size.height = this->bar_height + WidgetDimensions::scaled.matrix.Vertical();
1327
1328 uint score_info_width = 0;
1329 for (uint i = SCORE_BEGIN; i < SCORE_END; i++) {
1330 score_info_width = std::max(score_info_width, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES + i).width);
1331 }
1332 score_info_width += GetStringBoundingBox(GetString(STR_JUST_COMMA, GetParamMaxValue(1000))).width + WidgetDimensions::scaled.hsep_wide;
1333
1334 this->bar_width = GetStringBoundingBox(GetString(STR_PERFORMANCE_DETAIL_PERCENT, GetParamMaxValue(100))).width + WidgetDimensions::scaled.hsep_indent * 2; // Wide bars!
1335
1336 /* At this number we are roughly at the max; it can become wider,
1337 * but then you need at 1000 times more money. At that time you're
1338 * not that interested anymore in the last few digits anyway.
1339 * The 500 is because 999 999 500 to 999 999 999 are rounded to
1340 * 1 000 M, and not 999 999 k. Use negative numbers to account for
1341 * the negative income/amount of money etc. as well. */
1342 int max = -(999999999 - 500);
1343
1344 /* Scale max for the display currency. Prior to rendering the value
1345 * is converted into the display currency, which may cause it to
1346 * raise significantly. We need to compensate for that since {{CURRCOMPACT}}
1347 * is used, which can produce quite short renderings of very large
1348 * values. Otherwise the calculated width could be too narrow.
1349 * Note that it doesn't work if there was a currency with an exchange
1350 * rate greater than max.
1351 * When the currency rate is more than 1000, the 999 999 k becomes at
1352 * least 999 999 M which roughly is equally long. Furthermore if the
1353 * exchange rate is that high, 999 999 k is usually not enough anymore
1354 * to show the different currency numbers. */
1355 if (GetCurrency().rate < 1000) max /= GetCurrency().rate;
1356 uint score_detail_width = GetStringBoundingBox(GetString(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY, max, max)).width;
1357
1358 size.width = WidgetDimensions::scaled.frametext.Horizontal() + score_info_width + WidgetDimensions::scaled.hsep_wide + this->bar_width + WidgetDimensions::scaled.hsep_wide + score_detail_width;
1360 uint right = size.width - WidgetDimensions::scaled.frametext.right;
1361
1362 bool rtl = _current_text_dir == TD_RTL;
1363 this->score_info_left = rtl ? right - score_info_width : left;
1364 this->score_info_right = rtl ? right : left + score_info_width;
1365
1366 this->score_detail_left = rtl ? left : right - score_detail_width;
1367 this->score_detail_right = rtl ? left + score_detail_width : right;
1368
1369 this->bar_left = left + (rtl ? score_detail_width : score_info_width) + WidgetDimensions::scaled.hsep_wide;
1370 this->bar_right = this->bar_left + this->bar_width - 1;
1371 break;
1372 }
1373 }
1374
1375 void DrawWidget(const Rect &r, WidgetID widget) const override
1376 {
1377 /* No need to draw when there's nothing to draw */
1378 if (this->company == CompanyID::Invalid()) return;
1379
1381 if (this->IsWidgetDisabled(widget)) return;
1382 CompanyID cid = (CompanyID)(widget - WID_PRD_COMPANY_FIRST);
1383 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
1384 DrawCompanyIcon(cid, CenterBounds(r.left, r.right, sprite_size.width), CenterBounds(r.top, r.bottom, sprite_size.height));
1385 return;
1386 }
1387
1388 if (!IsInsideMM(widget, WID_PRD_SCORE_FIRST, WID_PRD_SCORE_LAST + 1)) return;
1389
1390 ScoreID score_type = (ScoreID)(widget - WID_PRD_SCORE_FIRST);
1391
1392 /* The colours used to show how the progress is going */
1393 int colour_done = GetColourGradient(COLOUR_GREEN, SHADE_NORMAL);
1394 int colour_notdone = GetColourGradient(COLOUR_RED, SHADE_NORMAL);
1395
1396 /* Draw all the score parts */
1397 int64_t val = _score_part[company][score_type];
1398 int64_t needed = _score_info[score_type].needed;
1399 int score = _score_info[score_type].score;
1400
1401 /* SCORE_TOTAL has its own rules ;) */
1402 if (score_type == SCORE_TOTAL) {
1403 for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) score += _score_info[i].score;
1404 needed = SCORE_MAX;
1405 }
1406
1407 uint bar_top = CenterBounds(r.top, r.bottom, this->bar_height);
1408 uint text_top = CenterBounds(r.top, r.bottom, GetCharacterHeight(FS_NORMAL));
1409
1410 DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
1411
1412 /* Draw the score */
1413 DrawString(this->score_info_left, this->score_info_right, text_top, GetString(STR_JUST_COMMA, score), TC_BLACK, SA_RIGHT);
1414
1415 /* Calculate the %-bar */
1416 uint x = Clamp<int64_t>(val, 0, needed) * this->bar_width / needed;
1417 bool rtl = _current_text_dir == TD_RTL;
1418 if (rtl) {
1419 x = this->bar_right - x;
1420 } else {
1421 x = this->bar_left + x;
1422 }
1423
1424 /* Draw the bar */
1425 if (x != this->bar_left) GfxFillRect(this->bar_left, bar_top, x, bar_top + this->bar_height - 1, rtl ? colour_notdone : colour_done);
1426 if (x != this->bar_right) GfxFillRect(x, bar_top, this->bar_right, bar_top + this->bar_height - 1, rtl ? colour_done : colour_notdone);
1427
1428 /* Draw it */
1429 DrawString(this->bar_left, this->bar_right, text_top, GetString(STR_PERFORMANCE_DETAIL_PERCENT, Clamp<int64_t>(val, 0, needed) * 100 / needed), TC_FROMSTRING, SA_HOR_CENTER);
1430
1431 /* SCORE_LOAN is inversed */
1432 if (score_type == SCORE_LOAN) val = needed - val;
1433
1434 /* Draw the amount we have against what is needed
1435 * For some of them it is in currency format */
1436 switch (score_type) {
1437 case SCORE_MIN_PROFIT:
1438 case SCORE_MIN_INCOME:
1439 case SCORE_MAX_INCOME:
1440 case SCORE_MONEY:
1441 case SCORE_LOAN:
1442 DrawString(this->score_detail_left, this->score_detail_right, text_top, GetString(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY, val, needed));
1443 break;
1444 default:
1445 DrawString(this->score_detail_left, this->score_detail_right, text_top, GetString(STR_PERFORMANCE_DETAIL_AMOUNT_INT, val, needed));
1446 break;
1447 }
1448 }
1449
1450 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
1451 {
1452 /* Check which button is clicked */
1454 /* Is it no on disable? */
1455 if (!this->IsWidgetDisabled(widget)) {
1456 this->RaiseWidget(WID_PRD_COMPANY_FIRST + this->company);
1457 this->company = (CompanyID)(widget - WID_PRD_COMPANY_FIRST);
1458 this->LowerWidget(WID_PRD_COMPANY_FIRST + this->company);
1459 this->SetDirty();
1460 }
1461 }
1462 }
1463
1464 void OnGameTick() override
1465 {
1466 /* Update the company score every 5 days */
1467 if (--this->timeout == 0) {
1468 this->UpdateCompanyStats();
1469 this->SetDirty();
1470 }
1471 }
1472
1478 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
1479 {
1480 if (!gui_scope) return;
1481 /* Disable the companies who are not active */
1482 for (CompanyID i = CompanyID::Begin(); i < MAX_COMPANIES; ++i) {
1484 }
1485
1486 /* Check if the currently selected company is still active. */
1487 if (this->company != CompanyID::Invalid() && !Company::IsValidID(this->company)) {
1488 /* Raise the widget for the previous selection. */
1489 this->RaiseWidget(WID_PRD_COMPANY_FIRST + this->company);
1490 this->company = CompanyID::Invalid();
1491 }
1492
1493 if (this->company == CompanyID::Invalid()) {
1494 for (const Company *c : Company::Iterate()) {
1495 this->company = c->index;
1496 break;
1497 }
1498 }
1499
1500 /* Make sure the widget is lowered */
1501 if (this->company != CompanyID::Invalid()) {
1502 this->LowerWidget(WID_PRD_COMPANY_FIRST + this->company);
1503 }
1504 }
1505};
1506
1507CompanyID PerformanceRatingDetailWindow::company = CompanyID::Invalid();
1508
1509/*******************************/
1510/* INDUSTRY PRODUCTION HISTORY */
1511/*******************************/
1512
1514 uint line_height = 0;
1515 Scrollbar *vscroll = nullptr;
1516 uint legend_width = 0;
1517
1518 static inline constexpr StringID RANGE_LABELS[] = {
1519 STR_GRAPH_INDUSTRY_RANGE_PRODUCED,
1520 STR_GRAPH_INDUSTRY_RANGE_TRANSPORTED
1521 };
1522
1524 BaseGraphWindow(desc, STR_JUST_COMMA)
1525 {
1526 this->num_on_x_axis = GRAPH_NUM_MONTHS;
1527 this->num_vert_lines = GRAPH_NUM_MONTHS;
1528 this->month_increment = 1;
1529 this->x_values_increment = ECONOMY_MONTH_MINUTES;
1530 this->draw_dates = !TimerGameEconomy::UsingWallclockUnits();
1531 this->ranges = RANGE_LABELS;
1532
1533 this->CreateNestedTree();
1534 this->vscroll = this->GetScrollbar(WID_GRAPH_MATRIX_SCROLLBAR);
1535
1536 int count = 0;
1538 for (const auto &p : i->produced) {
1539 if (!IsValidCargoType(p.cargo)) continue;
1540 count++;
1541 }
1542 this->vscroll->SetCount(count);
1543
1544 auto *wid = this->GetWidget<NWidgetCore>(WID_GRAPH_FOOTER);
1545 wid->SetString(TimerGameEconomy::UsingWallclockUnits() ? STR_GRAPH_LAST_24_MINUTES_TIME_LABEL : STR_EMPTY);
1546
1547 this->FinishInitNested(window_number);
1548
1549 /* Initialise the dataset */
1550 this->UpdateStatistics(true);
1551 }
1552
1553 void OnInit() override
1554 {
1555 /* Width of the legend blob. */
1556 this->legend_width = GetCharacterHeight(FS_SMALL) * 9 / 6;
1557 }
1558
1559 void UpdateExcludedData()
1560 {
1561 this->excluded_data = 0;
1562
1563 const Industry *i = Industry::Get(this->window_number);
1564 for (const auto &p : i->produced) {
1565 if (!IsValidCargoType(p.cargo)) continue;
1566 if (HasBit(_legend_excluded_cargo_production_history, p.cargo)) SetBit(this->excluded_data, p.cargo);
1567 }
1568 }
1569
1570 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
1571 {
1572 if (widget != WID_GRAPH_MATRIX) {
1573 BaseGraphWindow::UpdateWidgetSize(widget, size, padding, fill, resize);
1574 return;
1575 }
1576
1577 const Industry *i = Industry::Get(this->window_number);
1578 const CargoSpec *cs;
1579 for (const auto &p : i->produced) {
1580 if (!IsValidCargoType(p.cargo)) continue;
1581
1582 cs = CargoSpec::Get(p.cargo);
1583 Dimension d = GetStringBoundingBox(GetString(STR_GRAPH_CARGO_PAYMENT_CARGO, cs->name));
1584 d.width += this->legend_width + WidgetDimensions::scaled.hsep_normal; // colour field
1587 size = maxdim(d, size);
1588 }
1589
1590 this->line_height = size.height;
1591 size.height = this->line_height * 11; /* Default number of cargo types in most climates. */
1592 resize.width = 0;
1593 resize.height = this->line_height;
1594 }
1595
1596 void DrawWidget(const Rect &r, WidgetID widget) const override
1597 {
1598 if (widget != WID_GRAPH_MATRIX) {
1599 BaseGraphWindow::DrawWidget(r, widget);
1600 return;
1601 }
1602
1603 bool rtl = _current_text_dir == TD_RTL;
1604
1605 int pos = this->vscroll->GetPosition();
1606 int max = pos + this->vscroll->GetCapacity();
1607
1608 Rect line = r.WithHeight(this->line_height);
1609 const Industry *i = Industry::Get(this->window_number);
1610 const CargoSpec *cs;
1611
1612 for (const auto &p : i->produced) {
1613 if (!IsValidCargoType(p.cargo)) continue;
1614
1615 if (pos-- > 0) continue;
1616 if (--max < 0) break;
1617
1618 cs = CargoSpec::Get(p.cargo);
1619
1620 bool lowered = !HasBit(_legend_excluded_cargo_production_history, p.cargo);
1621
1622 /* Redraw frame if lowered */
1623 if (lowered) DrawFrameRect(line, COLOUR_BROWN, FrameFlag::Lowered);
1624
1625 const Rect text = line.Shrink(WidgetDimensions::scaled.framerect);
1626
1627 /* Cargo-colour box with outline */
1628 const Rect cargo = text.WithWidth(this->legend_width, rtl);
1629 GfxFillRect(cargo, PC_BLACK);
1630 GfxFillRect(cargo.Shrink(WidgetDimensions::scaled.bevel), cs->legend_colour);
1631
1632 /* Cargo name */
1633 DrawString(text.Indent(this->legend_width + WidgetDimensions::scaled.hsep_normal, rtl), GetString(STR_GRAPH_CARGO_PAYMENT_CARGO, cs->name));
1634
1635 line = line.Translate(0, this->line_height);
1636 }
1637 }
1638
1639 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
1640 {
1641 switch (widget) {
1643 /* Remove all cargoes from the excluded lists. */
1644 _legend_excluded_cargo_production_history = 0;
1645 this->excluded_data = 0;
1646 this->SetDirty();
1647 break;
1648
1650 /* Add all cargoes to the excluded lists. */
1651 const Industry *i = Industry::Get(this->window_number);
1652 for (const auto &p : i->produced) {
1653 if (!IsValidCargoType(p.cargo)) continue;
1654
1655 SetBit(_legend_excluded_cargo_production_history, p.cargo);
1656 SetBit(this->excluded_data, p.cargo);
1657 }
1658 this->SetDirty();
1659 break;
1660 }
1661
1662 case WID_GRAPH_MATRIX: {
1663 int row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GRAPH_MATRIX);
1664 if (row >= this->vscroll->GetCount()) return;
1665
1666 const Industry *i = Industry::Get(this->window_number);
1667 for (const auto &p : i->produced) {
1668 if (!IsValidCargoType(p.cargo)) continue;
1669 if (row-- > 0) continue;
1670
1671 ToggleBit(_legend_excluded_cargo_production_history, p.cargo);
1672 this->UpdateExcludedData();
1673 this->SetDirty();
1674 break;
1675 }
1676 break;
1677 }
1678
1679 default:
1680 this->BaseGraphWindow::OnClick(pt, widget, click_count);
1681 break;
1682 }
1683 }
1684
1685 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
1686 {
1687 if (widget == WID_GRAPH_CAPTION) return GetString(STR_GRAPH_INDUSTRY_PRODUCTION_CAPTION, this->window_number);
1688
1689 return this->Window::GetWidgetString(widget, stringid);
1690 }
1691
1692 void OnResize() override
1693 {
1694 this->vscroll->SetCapacityFromWidget(this, WID_GRAPH_MATRIX);
1695 }
1696
1697 void UpdateStatistics(bool initialize) override
1698 {
1699 CargoTypes excluded_cargo = this->excluded_data;
1700 this->UpdateExcludedData();
1701
1702 int mo = TimerGameEconomy::month - this->num_vert_lines;
1703 auto yr = TimerGameEconomy::year;
1704 while (mo < 0) {
1705 yr--;
1706 mo += 12;
1707 }
1708
1709 if (!initialize && this->excluded_data == excluded_cargo && this->num_on_x_axis == this->num_vert_lines && this->year == yr && this->month == mo) {
1710 /* There's no reason to get new stats */
1711 return;
1712 }
1713
1714 this->year = yr;
1715 this->month = mo;
1716
1717 const Industry *i = Industry::Get(this->window_number);
1718
1719 this->data.clear();
1720 for (const auto &p : i->produced) {
1721 if (!IsValidCargoType(p.cargo)) continue;
1722 const CargoSpec *cs = CargoSpec::Get(p.cargo);
1723
1724 DataSet &produced = this->data.emplace_back();
1725 produced.colour = cs->legend_colour;
1726 produced.exclude_bit = cs->Index();
1727 produced.range_bit = 0;
1728
1729 for (uint j = 0; j < GRAPH_NUM_MONTHS; j++) {
1730 produced.values[j] = p.history[GRAPH_NUM_MONTHS - j].production;
1731 }
1732
1733 DataSet &transported = this->data.emplace_back();
1734 transported.colour = cs->legend_colour;
1735 transported.exclude_bit = cs->Index();
1736 transported.range_bit = 1;
1737 transported.dash = 2;
1738
1739 for (uint j = 0; j < GRAPH_NUM_MONTHS; j++) {
1740 transported.values[j] = p.history[GRAPH_NUM_MONTHS - j].transported;
1741 }
1742 }
1743
1744 this->vscroll->SetCount(std::size(this->data));
1745
1746 this->SetDirty();
1747 }
1748};
1749
1750static constexpr NWidgetPart _nested_industry_production_widgets[] = {
1752 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1753 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_GRAPH_CAPTION),
1754 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1755 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1756 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1757 EndContainer(),
1758 NWidget(WWT_PANEL, COLOUR_BROWN, WID_GRAPH_BACKGROUND), SetMinimalSize(568, 128),
1760 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GRAPH_GRAPH), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
1762 NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1),
1763 NWidget(WWT_MATRIX, COLOUR_BROWN, WID_GRAPH_RANGE_MATRIX), SetFill(1, 0), SetResize(0, 0), SetMatrixDataTip(1, 0, STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO),
1765 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_GRAPH_ENABLE_CARGOES), SetStringTip(STR_GRAPH_CARGO_ENABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL), SetFill(1, 0),
1766 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_GRAPH_DISABLE_CARGOES), SetStringTip(STR_GRAPH_CARGO_DISABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL), SetFill(1, 0),
1769 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),
1771 EndContainer(),
1772 NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1),
1773 EndContainer(),
1774 NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
1775 EndContainer(),
1777 NWidget(NWID_SPACER), SetMinimalSize(12, 0), SetFill(1, 0), SetResize(1, 0),
1778 NWidget(WWT_TEXT, INVALID_COLOUR, WID_GRAPH_FOOTER), SetMinimalSize(0, 6), SetPadding(2, 0, 2, 0), SetStringTip(STR_EMPTY),
1779 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1780 NWidget(WWT_RESIZEBOX, COLOUR_BROWN, WID_GRAPH_RESIZE), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
1781 EndContainer(),
1782 EndContainer(),
1783};
1784
1785static WindowDesc _industry_production_desc(
1786 WDP_AUTO, "graph_industry_production", 0, 0,
1788 {},
1789 _nested_industry_production_widgets
1790);
1791
1792void ShowIndustryProductionGraph(WindowNumber window_number)
1793{
1794 AllocateWindowDescFront<IndustryProductionGraphWindow>(_industry_production_desc, window_number);
1795}
1796
1801static std::unique_ptr<NWidgetBase> MakePerformanceDetailPanels()
1802{
1803 auto realtime = TimerGameEconomy::UsingWallclockUnits();
1804 const StringID performance_tips[] = {
1805 realtime ? STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP_PERIODS : STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP_YEARS,
1806 STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP,
1807 realtime ? STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP_PERIODS : STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP_YEARS,
1808 STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP,
1809 STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP,
1810 STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP,
1811 STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP,
1812 STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP,
1813 STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP,
1814 STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP,
1815 };
1816
1817 static_assert(lengthof(performance_tips) == SCORE_END - SCORE_BEGIN);
1818
1819 auto vert = std::make_unique<NWidgetVertical>(NWidContainerFlag::EqualSize);
1820 for (WidgetID widnum = WID_PRD_SCORE_FIRST; widnum <= WID_PRD_SCORE_LAST; widnum++) {
1821 auto panel = std::make_unique<NWidgetBackground>(WWT_PANEL, COLOUR_BROWN, widnum);
1822 panel->SetFill(1, 1);
1823 panel->SetToolTip(performance_tips[widnum - WID_PRD_SCORE_FIRST]);
1824 vert->Add(std::move(panel));
1825 }
1826 return vert;
1827}
1828
1830std::unique_ptr<NWidgetBase> MakeCompanyButtonRowsGraphGUI()
1831{
1832 return MakeCompanyButtonRows(WID_PRD_COMPANY_FIRST, WID_PRD_COMPANY_LAST, COLOUR_BROWN, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP);
1833}
1834
1835static constexpr NWidgetPart _nested_performance_rating_detail_widgets[] = {
1837 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1838 NWidget(WWT_CAPTION, COLOUR_BROWN), SetStringTip(STR_PERFORMANCE_DETAIL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1839 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1840 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1841 EndContainer(),
1842 NWidget(WWT_PANEL, COLOUR_BROWN),
1844 EndContainer(),
1846};
1847
1848static WindowDesc _performance_rating_detail_desc(
1849 WDP_AUTO, "league_details", 0, 0,
1851 {},
1852 _nested_performance_rating_detail_widgets
1853);
1854
1855void ShowPerformanceRatingDetail()
1856{
1857 AllocateWindowDescFront<PerformanceRatingDetailWindow>(_performance_rating_detail_desc, 0);
1858}
1859
1860void InitializeGraphGui()
1861{
1862 _legend_excluded_companies = CompanyMask{};
1863 _legend_excluded_cargo_payment_rates = 0;
1864 _legend_excluded_cargo_production_history = 0;
1865}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr T ToggleBit(T &x, const uint8_t y)
Toggles a bit in a variable.
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:106
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:75
std::span< const CargoSpec * > _sorted_standard_cargo_specs
Standard cargo specifications sorted alphabetically by name.
Types/functions related to cargoes.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
constexpr Timpl & Flip(Tvalue_type value)
Flip the value-th bit.
constexpr Timpl & Set()
Set all bits.
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
Scrollbar data structure.
size_type GetCapacity() const
Gets the number of visible elements of the scrollbar.
void SetCount(size_t num)
Sets the number of elements in the list.
auto GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Return an iterator pointing to the element of a scrolled widget that a user clicked in.
size_type GetScrolledRowFromWidget(int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition widget.cpp:2449
void SetCapacityFromWidget(Window *w, WidgetID widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget.
Definition widget.cpp:2523
size_type GetCount() const
Gets the number of elements in the list.
auto GetVisibleRangeIterators(Tcontainer &container) const
Get a pair of iterators for the range of visible elements in a container.
size_type GetPosition() const
Gets the position of the first visible element in the list.
static constexpr TimerGameTick::Ticks DAY_TICKS
1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885.
static Year year
Current year, starting at 0.
static Month month
Current month (0..11).
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
uint8_t Month
Type for the month, note: 0 based, i.e.
RectPadding framerect
Standard padding inside many panels.
Definition window_gui.h:40
RectPadding frametext
Padding inside frame with text.
Definition window_gui.h:41
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:29
int hsep_wide
Wide horizontal spacing.
Definition window_gui.h:62
RectPadding fullbevel
Always-scaled bevel thickness.
Definition window_gui.h:39
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:94
RectPadding matrix
Padding of WWT_MATRIX items.
Definition window_gui.h:42
int hsep_normal
Normal horizontal spacing.
Definition window_gui.h:61
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition window_gui.h:38
int hsep_indent
Width of indentation for tree layouts.
Definition window_gui.h:63
Definition of stuff that is very close to a company, like the company struct itself.
void DrawCompanyIcon(CompanyID c, int x, int y)
Draw the icon of a company.
GUI Functions related to companies.
Functions to handle different currencies.
const CurrencySpec & GetCurrency()
Get the currently selected currency.
Definition currency.h:118
int UpdateCompanyRatingAndValue(Company *c, bool update)
if update is set to true, the economy is updated with this score (also the house is updated,...
Definition economy.cpp:202
const ScoreInfo _score_info[]
Score info, values used for computing the detailed performance rating.
Definition economy.cpp:91
Functions related to the economy.
ScoreID
Score categories in the detailed performance rating.
@ SCORE_END
How many scores are there..
@ SCORE_TOTAL
This must always be the last entry.
static constexpr int SCORE_MAX
The max score that can be in the performance history.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition gfx.cpp:923
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:852
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:658
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:115
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition gfx.cpp:775
Functions related to the gfx engine.
int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition gfx_func.h:166
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:246
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:245
@ SA_LEFT
Left align the text.
Definition gfx_type.h:377
@ SA_RIGHT
Right align the text (must be a single bit).
Definition gfx_type.h:379
@ SA_HOR_CENTER
Horizontally center the text.
Definition gfx_type.h:378
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
Definition gfx_type.h:389
@ SA_CENTER
Center both horizontally and vertically.
Definition gfx_type.h:387
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:296
constexpr double INT64_MAX_IN_DOUBLE
The biggest double that when cast to int64_t still fits in a int64_t.
Definition graph_gui.cpp:45
static std::unique_ptr< NWidgetBase > MakeNWidgetCompanyLines()
Construct a vertical list of buttons, one for each company.
std::unique_ptr< NWidgetBase > MakeCompanyButtonRowsGraphGUI()
Make a number of rows with buttons for each company for the performance rating detail window.
static std::unique_ptr< NWidgetBase > MakePerformanceDetailPanels()
Make a vertical list of panels for outputting score details.
Graph GUI functions.
Types related to the graph widgets.
@ WID_GRAPH_FOOTER
Footer.
@ WID_GRAPH_RESIZE
Resize button.
@ WID_GRAPH_BACKGROUND
Background of the window.
@ WID_GRAPH_GRAPH
Graph itself.
@ WID_PHG_DETAILED_PERFORMANCE
Detailed performance.
@ WID_GRAPH_HEADER
Header.
@ WID_GRAPH_MATRIX_SCROLLBAR
Cargo list scrollbar.
@ WID_GRAPH_DISABLE_CARGOES
Disable cargoes button.
@ WID_GRAPH_CAPTION
Caption.
@ WID_GRAPH_KEY_BUTTON
Key button.
@ WID_GRAPH_MATRIX
Cargo list.
@ WID_GRAPH_ENABLE_CARGOES
Enable cargoes button.
@ WID_GRAPH_RANGE_MATRIX
Range list.
@ WID_GL_FIRST_COMPANY
First company in the legend.
@ WID_GL_LAST_COMPANY
Last company in the legend.
@ WID_GL_BACKGROUND
Background of the window.
@ WID_PRD_COMPANY_FIRST
First company.
@ WID_PRD_SCORE_FIRST
First entry in the score list.
@ WID_PRD_SCORE_LAST
Last entry in the score list.
@ WID_PRD_COMPANY_LAST
Last company.
constexpr NWidgetPart SetMatrixDataTip(uint32_t cols, uint32_t rows, StringID tip={})
Widget part function for setting the data and tooltip of WWT_MATRIX widgets.
constexpr NWidgetPart NWidgetFunction(NWidgetFunctionType *func_ptr)
Obtain a nested widget (sub)tree from an external source.
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetScrollbar(WidgetID index)
Attach a scrollbar to a widget.
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
constexpr NWidgetPart SetResizeWidgetTypeTip(ResizeWidgetValues widget_type, StringID tip)
Widget part function for setting the resize widget type and tooltip.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:945
Base of all industries.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
uint8_t GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:387
static const uint8_t PC_BLACK
Black palette colour.
#define GREY_SCALE(level)
Return the colour for a particular greyscale level.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:58
This file contains all sprite-related enums and defines.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
uint64_t GetParamMaxValue(uint64_t max_value, uint min_count, FontSize size)
Get some number that is suitable for string size computations.
Definition strings.cpp:246
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:57
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
uint GetYLabelWidth(ValuesInterval current_interval, int num_hori_lines) const
Get width for Y labels.
void OnGameTick() override
Called once per (game) tick.
static const int MIN_GRID_PIXEL_SIZE
Minimum distance between graph lines.
static const int GRAPH_NUM_MONTHS
Number of months displayed in the graph.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
static const int ECONOMY_QUARTER_MINUTES
Minutes per economic quarter.
static const int GRAPH_PAYMENT_RATE_STEPS
Number of steps on Payment rate graph.
std::span< const OverflowSafeInt64 > GetDataSetRange(const DataSet &dataset) const
Get appropriate part of dataset values for the current number of horizontal points.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
static const int ECONOMY_MONTH_MINUTES
Minutes per economic month.
uint64_t excluded_data
bitmask of the datasets that shouldn't be displayed.
bool draw_dates
Should we draw months and years on the time axis?
static const int MIN_GRAPH_NUM_LINES_Y
Minimal number of horizontal lines to draw.
uint64_t excluded_range
bitmask of ranges that should not be displayed.
uint8_t month_increment
month increment between vertical lines. must be divisor of 12.
static const int PAYMENT_GRAPH_X_STEP_DAYS
X-axis step label for cargo payment rates "Days in transit".
ValuesInterval GetValuesInterval(int num_hori_lines) const
Get the interval that contains the graph's data.
virtual void UpdateStatistics(bool initialize)
Update the statistics.
static const TextColour GRAPH_AXIS_LABEL_COLOUR
colour of the graph axis label.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void DrawGraph(Rect r) const
Actually draw the graph.
static const int PAYMENT_GRAPH_X_STEP_SECONDS
X-axis step label for cargo payment rates "Seconds in transit".
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
Specification of a cargo type.
Definition cargotype.h:74
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:137
CargoType Index() const
Determines index of this cargospec.
Definition cargotype.h:108
StringID name
Name of this type of cargo.
Definition cargotype.h:91
GUISettings gui
settings related to the GUI
Colours colour
Company colour.
std::array< CompanyEconomyEntry, MAX_HISTORY_QUARTERS > old_economy
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
uint8_t num_valid_stat_ent
Number of valid statistical entries in old_economy.
uint16_t rate
The conversion rate compared to the base currency.
Definition currency.h:77
Dimensions (a width and height) of a rectangle in 2D.
uint8_t graph_line_thickness
the thickness of the lines in the various graph guis
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition graph_gui.cpp:82
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
Definition graph_gui.cpp:64
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void UpdateStatistics(bool initialize) override
Update the statistics.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
void OnResize() override
Called after the window got resized.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
uint line_height
Pixel height of each cargo type row.
uint legend_width
Width of legend 'blob'.
void OnInit() override
Notification that the nested widget tree gets initialized.
Scrollbar * vscroll
Cargo list scrollbar.
Defines the internal data of a functional industry.
Definition industry.h:63
ProducedCargoes produced
produced cargo slots
Definition industry.h:94
Partial widget specification to allow NWidgets to be written nested.
uint line_height
Pixel height of each cargo type row.
void OnResize() override
Called after the window got resized.
void OnInit() override
Notification that the nested widget tree gets initialized.
void OnGameTick() override
Called once per (game) tick.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
void UpdatePaymentRates()
Update the payment rates according to the latest information.
IntervalTimer< TimerWindow > update_payment_interval
Update the payment rates on a regular interval.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
uint legend_width
Width of legend 'blob'.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Scrollbar * vscroll
Cargo list scrollbar.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
void OnGameTick() override
Called once per (game) tick.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Coordinates of a point in 2D.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * Get(auto index)
Returns Titem with given index.
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static Titem * GetIfValid(auto index)
Returns Titem with given index.
constexpr uint Horizontal() const
Get total horizontal padding of RectPadding.
constexpr uint Vertical() const
Get total vertical padding of RectPadding.
Specification of a rectangle with absolute coordinates of all edges.
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Rect WithHeight(int height, bool end=false) const
Copy Rect and set its height.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
int needed
How much you need to get the perfect score.
int score
How much score it will give.
Templated helper to make a type-safe 'typedef' representing a single POD value.
Contains the interval of a graph's data.
OverflowSafeInt64 lowest
Lowest value of this interval. Must be zero or less.
OverflowSafeInt64 highest
Highest value of this interval. Must be zero or greater.
High level window description.
Definition window_gui.h:168
Number to differentiate different windows of the same class.
Data structure for an opened window.
Definition window_gui.h:274
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1736
void RaiseWidget(WidgetID widget_index)
Marks a widget as raised.
Definition window_gui.h:470
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:504
ResizeInfo resize
Resize information.
Definition window_gui.h:315
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1726
bool IsWidgetDisabled(WidgetID widget_index) const
Gets the enabled/disabled status of a widget.
Definition window_gui.h:411
int left
x position of left edge of the window
Definition window_gui.h:310
int GetRowFromWidget(int clickpos, WidgetID widget, int padding, int line_height=-1) const
Compute the row of a widget that a user clicked in.
Definition window.cpp:212
void LowerWidget(WidgetID widget_index)
Marks a widget as lowered.
Definition window_gui.h:461
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1749
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:313
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:382
int height
Height of the window (number of pixels down in y direction)
Definition window_gui.h:313
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:312
void ToggleWidgetLoweredState(WidgetID widget_index)
Invert the lowered/raised status of a widget.
Definition window_gui.h:451
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
Definition of Interval and OneShot timers.
Definition of the game-economy-timer.
Definition of the tick-based game-timer.
Definition of the Window system.
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:296
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:48
std::unique_ptr< NWidgetBase > MakeCompanyButtonRows(WidgetID widget_first, WidgetID widget_last, Colours button_colour, int max_length, StringID button_tooltip, bool resizable)
Make a number of rows with button-like graphics, for enabling/disabling each company.
Definition widget.cpp:3422
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ NWID_SPACER
Invisible widget that takes some space.
Definition widget_type.h:69
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:65
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:40
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:56
@ WWT_MATRIX
Grid of rows and columns.
Definition widget_type.h:49
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:54
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:51
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:75
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:67
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:59
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:38
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:58
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:55
@ WWT_TEXT
Pure simple text.
Definition widget_type.h:48
@ EqualSize
Containers should keep all their (resizing) children equally large.
@ RWV_HIDE_BEVEL
Bevel of resize box is hidden.
Definition widget_type.h:30
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition window.cpp:3224
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition window.cpp:3106
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ Lowered
If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
@ WDP_AUTO
Find a place automatically.
Definition window_gui.h:145
int WidgetID
Widget ID.
Definition window_type.h:20
@ WC_PERFORMANCE_HISTORY
Performance history graph; Window numbers:
@ WC_PERFORMANCE_DETAIL
Performance detail window; Window numbers:
@ WC_PAYMENT_RATES
Payment rates graph; Window numbers:
@ WC_GRAPH_LEGEND
Legend for graphs; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:47
@ WC_OPERATING_PROFIT
Operating profit graph; Window numbers:
@ WC_INDUSTRY_PRODUCTION
Industry production history graph; Window numbers:
@ WC_INDUSTRY_VIEW
Industry view; Window numbers:
@ WC_INCOME_GRAPH
Income graph; Window numbers:
@ WC_DELIVERED_CARGO
Delivered cargo graph; Window numbers:
@ WC_COMPANY_VALUE
Company value graph; Window numbers:
Functions related to zooming.
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition zoom_type.h:21