OpenTTD
graph_gui.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 CompanyMask _legend_excluded_companies;
37 static CargoTypes _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 = 64;
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 
176  uint64 excluded_data;
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 OnGameTick()
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  CompanyMask 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  uint line_height;
874 
876  BaseGraphWindow(desc, WID_CPR_GRAPH, STR_JUST_CURRENCY_SHORT)
877  {
878  this->num_on_x_axis = 20;
879  this->num_vert_lines = 20;
880  this->month = 0xFF;
881  this->x_values_start = 10;
882  this->x_values_increment = 10;
883 
884  this->CreateNestedTree();
885  this->vscroll = this->GetScrollbar(WID_CPR_MATRIX_SCROLLBAR);
887 
888  /* Initialise the dataset */
889  this->OnHundredthTick();
890 
891  this->FinishInitNested(window_number);
892  }
893 
894  void UpdateExcludedData()
895  {
896  this->excluded_data = 0;
897 
898  int i = 0;
899  const CargoSpec *cs;
901  if (HasBit(_legend_excluded_cargo, cs->Index())) SetBit(this->excluded_data, i);
902  i++;
903  }
904  }
905 
906  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
907  {
908  if (widget != WID_CPR_MATRIX) {
909  BaseGraphWindow::UpdateWidgetSize(widget, size, padding, fill, resize);
910  return;
911  }
912 
913  const CargoSpec *cs;
915  SetDParam(0, cs->name);
916  Dimension d = GetStringBoundingBox(STR_GRAPH_CARGO_PAYMENT_CARGO);
917  d.width += 14; // colour field
920  *size = maxdim(d, *size);
921  }
922 
923  this->line_height = size->height;
924  size->height = this->line_height * 11; /* Default number of cargo types in most climates. */
925  resize->width = 0;
926  resize->height = this->line_height;
927  }
928 
929  virtual void DrawWidget(const Rect &r, int widget) const
930  {
931  if (widget != WID_CPR_MATRIX) {
932  BaseGraphWindow::DrawWidget(r, widget);
933  return;
934  }
935 
936  bool rtl = _current_text_dir == TD_RTL;
937 
938  int x = r.left + WD_FRAMERECT_LEFT;
939  int y = r.top;
940 
941  int pos = this->vscroll->GetPosition();
942  int max = pos + this->vscroll->GetCapacity();
943 
944  const CargoSpec *cs;
946  if (pos-- > 0) continue;
947  if (--max < 0) break;
948 
949  bool lowered = !HasBit(_legend_excluded_cargo, cs->Index());
950 
951  /* Redraw box if lowered */
952  if (lowered) DrawFrameRect(r.left, y, r.right, y + this->line_height - 1, COLOUR_ORANGE, lowered ? FR_LOWERED : FR_NONE);
953 
954  byte clk_dif = lowered ? 1 : 0;
955  int rect_x = clk_dif + (rtl ? r.right - 12 : r.left + WD_FRAMERECT_LEFT);
956 
957  GfxFillRect(rect_x, y + clk_dif, rect_x + 8, y + 5 + clk_dif, PC_BLACK);
958  GfxFillRect(rect_x + 1, y + 1 + clk_dif, rect_x + 7, y + 4 + clk_dif, cs->legend_colour);
959  SetDParam(0, cs->name);
960  DrawString(rtl ? r.left : x + 14 + clk_dif, (rtl ? r.right - 14 + clk_dif : r.right), y + clk_dif, STR_GRAPH_CARGO_PAYMENT_CARGO);
961 
962  y += this->line_height;
963  }
964  }
965 
966  virtual void OnClick(Point pt, int widget, int click_count)
967  {
968  switch (widget) {
970  /* Remove all cargoes from the excluded lists. */
971  _legend_excluded_cargo = 0;
972  this->excluded_data = 0;
973  this->SetDirty();
974  break;
975 
977  /* Add all cargoes to the excluded lists. */
978  int i = 0;
979  const CargoSpec *cs;
981  SetBit(_legend_excluded_cargo, cs->Index());
982  SetBit(this->excluded_data, i);
983  i++;
984  }
985  this->SetDirty();
986  break;
987  }
988 
989  case WID_CPR_MATRIX: {
990  uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_CPR_MATRIX, 0, this->line_height);
991  if (row >= this->vscroll->GetCount()) return;
992 
993  const CargoSpec *cs;
995  if (row-- > 0) continue;
996 
997  ToggleBit(_legend_excluded_cargo, cs->Index());
998  this->UpdateExcludedData();
999  this->SetDirty();
1000  break;
1001  }
1002  break;
1003  }
1004  }
1005  }
1006 
1007  virtual void OnResize()
1008  {
1009  this->vscroll->SetCapacityFromWidget(this, WID_CPR_MATRIX);
1010  }
1011 
1012  virtual void OnGameTick()
1013  {
1014  /* Override default OnGameTick */
1015  }
1016 
1022  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1023  {
1024  if (!gui_scope) return;
1025  this->OnHundredthTick();
1026  }
1027 
1028  virtual void OnHundredthTick()
1029  {
1030  this->UpdateExcludedData();
1031 
1032  int i = 0;
1033  const CargoSpec *cs;
1035  this->colours[i] = cs->legend_colour;
1036  for (uint j = 0; j != 20; j++) {
1037  this->cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->Index());
1038  }
1039  i++;
1040  }
1041  this->num_dataset = i;
1042  }
1043 };
1044 
1045 static const NWidgetPart _nested_cargo_payment_rates_widgets[] = {
1047  NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1048  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1049  NWidget(WWT_SHADEBOX, COLOUR_GREY),
1050  NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1051  NWidget(WWT_STICKYBOX, COLOUR_GREY),
1052  EndContainer(),
1053  NWidget(WWT_PANEL, COLOUR_GREY, WID_CPR_BACKGROUND), SetMinimalSize(568, 128),
1055  NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1056  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),
1057  NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1058  EndContainer(),
1060  NWidget(WWT_EMPTY, COLOUR_GREY, WID_CPR_GRAPH), SetMinimalSize(495, 0), SetFill(1, 1), SetResize(1, 1),
1062  NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1),
1063  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CPR_ENABLE_CARGOES), SetDataTip(STR_GRAPH_CARGO_ENABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_ENABLE_ALL), SetFill(1, 0),
1064  NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CPR_DISABLE_CARGOES), SetDataTip(STR_GRAPH_CARGO_DISABLE_ALL, STR_GRAPH_CARGO_TOOLTIP_DISABLE_ALL), SetFill(1, 0),
1067  NWidget(WWT_MATRIX, COLOUR_ORANGE, WID_CPR_MATRIX), SetResize(0, 2), SetMatrixDataTip(1, 0, STR_GRAPH_CARGO_PAYMENT_TOGGLE_CARGO), SetScrollbar(WID_CPR_MATRIX_SCROLLBAR),
1069  EndContainer(),
1070  NWidget(NWID_SPACER), SetMinimalSize(0, 24), SetFill(0, 1),
1071  EndContainer(),
1072  NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetFill(0, 1), SetResize(0, 1),
1073  EndContainer(),
1076  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),
1077  NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1078  NWidget(WWT_RESIZEBOX, COLOUR_GREY, WID_CPR_RESIZE),
1079  EndContainer(),
1080  EndContainer(),
1081 };
1082 
1083 static WindowDesc _cargo_payment_rates_desc(
1084  WDP_AUTO, "graph_cargo_payment_rates", 0, 0,
1086  0,
1087  _nested_cargo_payment_rates_widgets, lengthof(_nested_cargo_payment_rates_widgets)
1088 );
1089 
1090 
1091 void ShowCargoPaymentRates()
1092 {
1093  AllocateWindowDescFront<PaymentRatesGraphWindow>(&_cargo_payment_rates_desc, 0);
1094 }
1095 
1096 /************************/
1097 /* COMPANY LEAGUE TABLE */
1098 /************************/
1099 
1100 static const StringID _performance_titles[] = {
1101  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
1102  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
1103  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
1104  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
1105  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
1106  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
1107  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
1108  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
1109  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
1110  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
1111  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
1112  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
1113  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
1114  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
1115  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT,
1116  STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON,
1117 };
1118 
1119 static inline StringID GetPerformanceTitleFromValue(uint value)
1120 {
1121  return _performance_titles[minu(value, 1000) >> 6];
1122 }
1123 
1124 class CompanyLeagueWindow : public Window {
1125 private:
1126  GUIList<const Company*> companies;
1128  uint text_width;
1129  uint icon_width;
1131 
1136  {
1137  if (!this->companies.NeedRebuild()) return;
1138 
1139  this->companies.Clear();
1140 
1141  const Company *c;
1142  FOR_ALL_COMPANIES(c) {
1143  *this->companies.Append() = c;
1144  }
1145 
1146  this->companies.Compact();
1147  this->companies.RebuildDone();
1148  }
1149 
1151  static int CDECL PerformanceSorter(const Company * const *c1, const Company * const *c2)
1152  {
1153  return (*c2)->old_economy[0].performance_history - (*c1)->old_economy[0].performance_history;
1154  }
1155 
1156 public:
1158  {
1159  this->InitNested(window_number);
1160  this->companies.ForceRebuild();
1161  this->companies.NeedResort();
1162  }
1163 
1164  virtual void OnPaint()
1165  {
1166  this->BuildCompanyList();
1167  this->companies.Sort(&PerformanceSorter);
1168 
1169  this->DrawWidgets();
1170  }
1171 
1172  virtual void DrawWidget(const Rect &r, int widget) const
1173  {
1174  if (widget != WID_CL_BACKGROUND) return;
1175 
1176  int icon_y_offset = 1 + (FONT_HEIGHT_NORMAL - this->line_height) / 2;
1177  uint y = r.top + WD_FRAMERECT_TOP - icon_y_offset;
1178 
1179  bool rtl = _current_text_dir == TD_RTL;
1180  uint ordinal_left = rtl ? r.right - WD_FRAMERECT_LEFT - this->ordinal_width : r.left + WD_FRAMERECT_LEFT;
1181  uint ordinal_right = rtl ? r.right - WD_FRAMERECT_LEFT : r.left + WD_FRAMERECT_LEFT + this->ordinal_width;
1182  uint icon_left = r.left + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT + (rtl ? this->text_width : this->ordinal_width);
1183  uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - WD_FRAMERECT_LEFT - this->text_width;
1184  uint text_right = rtl ? r.left + WD_FRAMERECT_LEFT + this->text_width : r.right - WD_FRAMERECT_LEFT;
1185 
1186  for (uint i = 0; i != this->companies.Length(); i++) {
1187  const Company *c = this->companies[i];
1188  DrawString(ordinal_left, ordinal_right, y, i + STR_ORDINAL_NUMBER_1ST, i == 0 ? TC_WHITE : TC_YELLOW);
1189 
1190  DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
1191 
1192  SetDParam(0, c->index);
1193  SetDParam(1, c->index);
1194  SetDParam(2, GetPerformanceTitleFromValue(c->old_economy[0].performance_history));
1195  DrawString(text_left, text_right, y, STR_COMPANY_LEAGUE_COMPANY_NAME);
1196  y += this->line_height;
1197  }
1198  }
1199 
1200  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1201  {
1202  if (widget != WID_CL_BACKGROUND) return;
1203 
1204  this->ordinal_width = 0;
1205  for (uint i = 0; i < MAX_COMPANIES; i++) {
1206  this->ordinal_width = max(this->ordinal_width, GetStringBoundingBox(STR_ORDINAL_NUMBER_1ST + i).width);
1207  }
1208  this->ordinal_width += 5; // Keep some extra spacing
1209 
1210  uint widest_width = 0;
1211  uint widest_title = 0;
1212  for (uint i = 0; i < lengthof(_performance_titles); i++) {
1213  uint width = GetStringBoundingBox(_performance_titles[i]).width;
1214  if (width > widest_width) {
1215  widest_title = i;
1216  widest_width = width;
1217  }
1218  }
1219 
1220  Dimension d = GetSpriteSize(SPR_COMPANY_ICON);
1221  this->icon_width = d.width + 2;
1222  this->line_height = max<int>(d.height + 2, FONT_HEIGHT_NORMAL);
1223 
1224  const Company *c;
1225  FOR_ALL_COMPANIES(c) {
1226  SetDParam(0, c->index);
1227  SetDParam(1, c->index);
1228  SetDParam(2, _performance_titles[widest_title]);
1229  widest_width = max(widest_width, GetStringBoundingBox(STR_COMPANY_LEAGUE_COMPANY_NAME).width);
1230  }
1231 
1232  this->text_width = widest_width + 30; // Keep some extra spacing
1233 
1234  size->width = WD_FRAMERECT_LEFT + this->ordinal_width + WD_FRAMERECT_RIGHT + this->icon_width + WD_FRAMERECT_LEFT + this->text_width + WD_FRAMERECT_RIGHT;
1235  size->height = WD_FRAMERECT_TOP + this->line_height * MAX_COMPANIES + WD_FRAMERECT_BOTTOM;
1236  }
1237 
1238 
1239  virtual void OnGameTick()
1240  {
1241  if (this->companies.NeedResort()) {
1242  this->SetDirty();
1243  }
1244  }
1245 
1251  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1252  {
1253  if (data == 0) {
1254  /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
1255  this->companies.ForceRebuild();
1256  } else {
1257  this->companies.ForceResort();
1258  }
1259  }
1260 };
1261 
1262 static const NWidgetPart _nested_company_league_widgets[] = {
1264  NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1265  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_LEAGUE_TABLE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1266  NWidget(WWT_SHADEBOX, COLOUR_GREY),
1267  NWidget(WWT_STICKYBOX, COLOUR_GREY),
1268  EndContainer(),
1270 };
1271 
1272 static WindowDesc _company_league_desc(
1273  WDP_AUTO, "league", 0, 0,
1275  0,
1276  _nested_company_league_widgets, lengthof(_nested_company_league_widgets)
1277 );
1278 
1279 void ShowCompanyLeagueTable()
1280 {
1281  AllocateWindowDescFront<CompanyLeagueWindow>(&_company_league_desc, 0);
1282 }
1283 
1284 /*****************************/
1285 /* PERFORMANCE RATING DETAIL */
1286 /*****************************/
1287 
1289  static CompanyID company;
1290  int timeout;
1291 
1292  PerformanceRatingDetailWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
1293  {
1294  this->UpdateCompanyStats();
1295 
1296  this->InitNested(window_number);
1298  }
1299 
1300  void UpdateCompanyStats()
1301  {
1302  /* Update all company stats with the current data
1303  * (this is because _score_info is not saved to a savegame) */
1304  Company *c;
1305  FOR_ALL_COMPANIES(c) {
1306  UpdateCompanyRatingAndValue(c, false);
1307  }
1308 
1309  this->timeout = DAY_TICKS * 5;
1310  }
1311 
1312  uint score_info_left;
1313  uint score_info_right;
1314  uint bar_left;
1315  uint bar_right;
1316  uint bar_width;
1317  uint bar_height;
1318  uint score_detail_left;
1319  uint score_detail_right;
1320 
1321  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1322  {
1323  switch (widget) {
1324  case WID_PRD_SCORE_FIRST:
1325  this->bar_height = FONT_HEIGHT_NORMAL + 4;
1326  size->height = this->bar_height + 2 * WD_MATRIX_TOP;
1327 
1328  uint score_info_width = 0;
1329  for (uint i = SCORE_BEGIN; i < SCORE_END; i++) {
1330  score_info_width = max(score_info_width, GetStringBoundingBox(STR_PERFORMANCE_DETAIL_VEHICLES + i).width);
1331  }
1332  SetDParamMaxValue(0, 1000);
1333  score_info_width += GetStringBoundingBox(STR_BLACK_COMMA).width + WD_FRAMERECT_LEFT;
1334 
1335  SetDParamMaxValue(0, 100);
1336  this->bar_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_PERCENT).width + 20; // Wide bars!
1337 
1338  /* At this number we are roughly at the max; it can become wider,
1339  * but then you need at 1000 times more money. At that time you're
1340  * not that interested anymore in the last few digits anyway.
1341  * The 500 is because 999 999 500 to 999 999 999 are rounded to
1342  * 1 000 M, and not 999 999 k. Use negative numbers to account for
1343  * the negative income/amount of money etc. as well. */
1344  int max = -(999999999 - 500);
1345 
1346  /* Scale max for the display currency. Prior to rendering the value
1347  * is converted into the display currency, which may cause it to
1348  * raise significantly. We need to compensate for that since {{CURRCOMPACT}}
1349  * is used, which can produce quite short renderings of very large
1350  * values. Otherwise the calculated width could be too narrow.
1351  * Note that it doesn't work if there was a currency with an exchange
1352  * rate greater than max.
1353  * When the currency rate is more than 1000, the 999 999 k becomes at
1354  * least 999 999 M which roughly is equally long. Furthermore if the
1355  * exchange rate is that high, 999 999 k is usually not enough anymore
1356  * to show the different currency numbers. */
1357  if (_currency->rate < 1000) max /= _currency->rate;
1358  SetDParam(0, max);
1359  SetDParam(1, max);
1360  uint score_detail_width = GetStringBoundingBox(STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY).width;
1361 
1362  size->width = 7 + score_info_width + 5 + this->bar_width + 5 + score_detail_width + 7;
1363  uint left = 7;
1364  uint right = size->width - 7;
1365 
1366  bool rtl = _current_text_dir == TD_RTL;
1367  this->score_info_left = rtl ? right - score_info_width : left;
1368  this->score_info_right = rtl ? right : left + score_info_width;
1369 
1370  this->score_detail_left = rtl ? left : right - score_detail_width;
1371  this->score_detail_right = rtl ? left + score_detail_width : right;
1372 
1373  this->bar_left = left + (rtl ? score_detail_width : score_info_width) + 5;
1374  this->bar_right = this->bar_left + this->bar_width;
1375  break;
1376  }
1377  }
1378 
1379  virtual void DrawWidget(const Rect &r, int widget) const
1380  {
1381  /* No need to draw when there's nothing to draw */
1382  if (this->company == INVALID_COMPANY) return;
1383 
1385  if (this->IsWidgetDisabled(widget)) return;
1386  CompanyID cid = (CompanyID)(widget - WID_PRD_COMPANY_FIRST);
1387  int offset = (cid == this->company) ? 1 : 0;
1388  Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
1389  DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2 + offset, (r.top + r.bottom - sprite_size.height) / 2 + offset);
1390  return;
1391  }
1392 
1393  if (!IsInsideMM(widget, WID_PRD_SCORE_FIRST, WID_PRD_SCORE_LAST + 1)) return;
1394 
1395  ScoreID score_type = (ScoreID)(widget - WID_PRD_SCORE_FIRST);
1396 
1397  /* The colours used to show how the progress is going */
1398  int colour_done = _colour_gradient[COLOUR_GREEN][4];
1399  int colour_notdone = _colour_gradient[COLOUR_RED][4];
1400 
1401  /* Draw all the score parts */
1402  int64 val = _score_part[company][score_type];
1403  int64 needed = _score_info[score_type].needed;
1404  int score = _score_info[score_type].score;
1405 
1406  /* SCORE_TOTAL has his own rules ;) */
1407  if (score_type == SCORE_TOTAL) {
1408  for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) score += _score_info[i].score;
1409  needed = SCORE_MAX;
1410  }
1411 
1412  uint bar_top = r.top + WD_MATRIX_TOP;
1413  uint text_top = bar_top + 2;
1414 
1415  DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
1416 
1417  /* Draw the score */
1418  SetDParam(0, score);
1419  DrawString(this->score_info_left, this->score_info_right, text_top, STR_BLACK_COMMA, TC_FROMSTRING, SA_RIGHT);
1420 
1421  /* Calculate the %-bar */
1422  uint x = Clamp<int64>(val, 0, needed) * this->bar_width / needed;
1423  bool rtl = _current_text_dir == TD_RTL;
1424  if (rtl) {
1425  x = this->bar_right - x;
1426  } else {
1427  x = this->bar_left + x;
1428  }
1429 
1430  /* Draw the bar */
1431  if (x != this->bar_left) GfxFillRect(this->bar_left, bar_top, x, bar_top + this->bar_height, rtl ? colour_notdone : colour_done);
1432  if (x != this->bar_right) GfxFillRect(x, bar_top, this->bar_right, bar_top + this->bar_height, rtl ? colour_done : colour_notdone);
1433 
1434  /* Draw it */
1435  SetDParam(0, Clamp<int64>(val, 0, needed) * 100 / needed);
1436  DrawString(this->bar_left, this->bar_right, text_top, STR_PERFORMANCE_DETAIL_PERCENT, TC_FROMSTRING, SA_HOR_CENTER);
1437 
1438  /* SCORE_LOAN is inversed */
1439  if (score_type == SCORE_LOAN) val = needed - val;
1440 
1441  /* Draw the amount we have against what is needed
1442  * For some of them it is in currency format */
1443  SetDParam(0, val);
1444  SetDParam(1, needed);
1445  switch (score_type) {
1446  case SCORE_MIN_PROFIT:
1447  case SCORE_MIN_INCOME:
1448  case SCORE_MAX_INCOME:
1449  case SCORE_MONEY:
1450  case SCORE_LOAN:
1451  DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY);
1452  break;
1453  default:
1454  DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_INT);
1455  }
1456  }
1457 
1458  virtual void OnClick(Point pt, int widget, int click_count)
1459  {
1460  /* Check which button is clicked */
1462  /* Is it no on disable? */
1463  if (!this->IsWidgetDisabled(widget)) {
1464  this->RaiseWidget(this->company + WID_PRD_COMPANY_FIRST);
1465  this->company = (CompanyID)(widget - WID_PRD_COMPANY_FIRST);
1466  this->LowerWidget(this->company + WID_PRD_COMPANY_FIRST);
1467  this->SetDirty();
1468  }
1469  }
1470  }
1471 
1472  virtual void OnGameTick()
1473  {
1474  /* Update the company score every 5 days */
1475  if (--this->timeout == 0) {
1476  this->UpdateCompanyStats();
1477  this->SetDirty();
1478  }
1479  }
1480 
1486  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1487  {
1488  if (!gui_scope) return;
1489  /* Disable the companies who are not active */
1490  for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1492  }
1493 
1494  /* Check if the currently selected company is still active. */
1495  if (this->company != INVALID_COMPANY && !Company::IsValidID(this->company)) {
1496  /* Raise the widget for the previous selection. */
1497  this->RaiseWidget(this->company + WID_PRD_COMPANY_FIRST);
1498  this->company = INVALID_COMPANY;
1499  }
1500 
1501  if (this->company == INVALID_COMPANY) {
1502  const Company *c;
1503  FOR_ALL_COMPANIES(c) {
1504  this->company = c->index;
1505  break;
1506  }
1507  }
1508 
1509  /* Make sure the widget is lowered */
1510  this->LowerWidget(this->company + WID_PRD_COMPANY_FIRST);
1511  }
1512 };
1513 
1514 CompanyID PerformanceRatingDetailWindow::company = INVALID_COMPANY;
1515 
1522 static NWidgetBase *MakePerformanceDetailPanels(int *biggest_index)
1523 {
1524  const StringID performance_tips[] = {
1525  STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP,
1526  STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP,
1527  STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP,
1528  STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP,
1529  STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP,
1530  STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP,
1531  STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP,
1532  STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP,
1533  STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP,
1534  STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP,
1535  };
1536 
1537  assert_compile(lengthof(performance_tips) == SCORE_END - SCORE_BEGIN);
1538 
1540  for (int widnum = WID_PRD_SCORE_FIRST; widnum <= WID_PRD_SCORE_LAST; widnum++) {
1541  NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
1542  panel->SetFill(1, 1);
1543  panel->SetDataTip(0x0, performance_tips[widnum - WID_PRD_SCORE_FIRST]);
1544  vert->Add(panel);
1545  }
1546  *biggest_index = WID_PRD_SCORE_LAST;
1547  return vert;
1548 }
1549 
1552 {
1553  return MakeCompanyButtonRows(biggest_index, WID_PRD_COMPANY_FIRST, WID_PRD_COMPANY_LAST, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP);
1554 }
1555 
1556 static const NWidgetPart _nested_performance_rating_detail_widgets[] = {
1558  NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1559  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PERFORMANCE_DETAIL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1560  NWidget(WWT_SHADEBOX, COLOUR_GREY),
1561  NWidget(WWT_STICKYBOX, COLOUR_GREY),
1562  EndContainer(),
1563  NWidget(WWT_PANEL, COLOUR_GREY),
1565  EndContainer(),
1567 };
1568 
1569 static WindowDesc _performance_rating_detail_desc(
1570  WDP_AUTO, "league_details", 0, 0,
1572  0,
1573  _nested_performance_rating_detail_widgets, lengthof(_nested_performance_rating_detail_widgets)
1574 );
1575 
1576 void ShowPerformanceRatingDetail()
1577 {
1578  AllocateWindowDescFront<PerformanceRatingDetailWindow>(&_performance_rating_detail_desc, 0);
1579 }
1580 
1581 void InitializeGraphGui()
1582 {
1583  _legend_excluded_companies = 0;
1584  _legend_excluded_cargo = 0;
1585 }
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:1022
Empty widget, place holder to reserve space in widget array.
Definition: widget_type.h:48
virtual void OnGameTick()
Called once per (game) tick.
Definition: graph_gui.cpp:1012
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:324
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:1851
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:113
virtual void OnResize()
Called after the window got resized.
Definition: graph_gui.cpp:1007
void SetWidgetDisabledState(byte widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition: window_gui.h:394
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3201
static NWidgetPart NWidgetFunction(NWidgetFunctionType *func_ptr)
Obtain a nested widget (sub)tree from an external source.
Definition: widget_type.h:1146
Last entry in the score list.
Definition: graph_widget.h:64
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
int left
x position of left edge of the window
Definition: window_gui.h:319
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.
Scrollbar data structure.
Definition: widget_type.h:589
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 OnHundredthTick()
Called once every 100 (game) ticks.
Definition: graph_gui.cpp:1028
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:1151
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
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
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:1551
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:1172
First company.
Definition: graph_widget.h:66
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:68
Background of the window.
Definition: graph_widget.h:58
void DrawCompanyIcon(CompanyID c, int x, int y)
Draw the icon of a company.
virtual void OnGameTick()
Called once per (game) tick.
Definition: graph_gui.cpp:1239
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:465
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
uint64 excluded_data
bitmask of the datasets that shouldn&#39;t be displayed.
Definition: graph_gui.cpp:176
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:1128
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:485
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
Resize button.
Definition: graph_widget.h:48
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1812
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:1522
StringID name
Name of this type of cargo.
Definition: cargotype.h:71
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.
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:177
T * Append(uint to_add=1)
Append an item and return it.
void SetCount(int num)
Sets the number of elements in the list.
Definition: widget_type.h:670
virtual void OnPaint()
The window must be repainted.
Definition: graph_gui.cpp:1164
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:1200
Data structure for an opened window.
Definition: window_gui.h:278
Income graph; Window numbers:
Definition: window_type.h:524
Money expenses
The amount of expenses.
Definition: company_base.h:25
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1828
static NWidgetPart SetMatrixDataTip(uint8 cols, uint8 rows, StringID tip)
Widget part function for setting the data and tooltip of WWT_MATRIX widgets.
Definition: widget_type.h:1032
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
virtual void OnGameTick()
Called once per (game) tick.
Definition: graph_gui.cpp:1472
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1841
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:1458
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:97
#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.
int GetScrolledRowFromWidget(int clickpos, const Window *const w, int widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition: widget.cpp:1959
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
void SetDataTip(uint32 widget_data, StringID tool_tip)
Set data and tool tip of the nested widget.
Definition: widget.cpp:894
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
Cargo list scrollbar.
Definition: graph_widget.h:53
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:1379
bool IsWidgetDisabled(byte widget_index) const
Gets the enabled/disabled status of a widget.
Definition: window_gui.h:423
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
const Scrollbar * GetScrollbar(uint widnum) const
Return the Scrollbar to a widget index.
Definition: window.cpp:311
GUI Functions related to companies.
void LowerWidget(byte widget_index)
Marks a widget as lowered.
Definition: window_gui.h:476
uint icon_width
The width of the company icon.
Definition: graph_gui.cpp:1129
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:1321
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
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:500
Graph itself.
Definition: graph_widget.h:39
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
Grid of rows and columns.
Definition: widget_type.h:59
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:1130
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:968
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:700
Performance detail window; Window numbers:
Definition: window_type.h:566
uint16 GetCount() const
Gets the number of elements in the list.
Definition: widget_type.h:613
Month _cur_month
Current month (0..11)
Definition: date.cpp:27
Cargo list.
Definition: graph_widget.h:52
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
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
Scrollbar * vscroll
Cargo list scrollbar.
Definition: graph_gui.cpp:873
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
virtual void OnGameTick()
Called once per (game) tick.
Definition: graph_gui.cpp:542
Last company.
Definition: graph_widget.h:67
Background of the window.
Definition: graph_widget.h:45
If set the frame is lowered and the background colour brighter (ie. buttons when pressed) ...
Definition: window_gui.h:31
uint ordinal_width
The width of the ordinal number.
Definition: graph_gui.cpp:1127
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:108
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:96
First entry in the score list.
Definition: graph_widget.h:63
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:906
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:769
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
uint16 GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:622
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:1251
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
uint line_height
Pixel height of each cargo type row.
Definition: graph_gui.cpp:872
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:321
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:707
void SetCapacityFromWidget(Window *w, int widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget. ...
Definition: widget.cpp:1973
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:966
Specification of a rectangle with absolute coordinates of all edges.
Vertical scrollbar.
Definition: widget_type.h:84
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:314
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:1135
virtual void OnHundredthTick()
Called once every 100 (game) ticks.
Definition: window_gui.h:701
Legend for graphs; Window numbers:
Definition: window_type.h:512
An invalid company.
Definition: company_type.h:32
static NWidgetPart SetScrollbar(int index)
Attach a scrollbar to a widget.
Definition: widget_type.h:1095
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:929
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:123
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:3301
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:322
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:621
Key button.
Definition: graph_widget.h:28
uint16 GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:631
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:1486
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