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