OpenTTD
subsidy_gui.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include "industry.h"
12 #include "town.h"
13 #include "window_gui.h"
14 #include "strings_func.h"
15 #include "date_func.h"
16 #include "viewport_func.h"
17 #include "gui.h"
18 #include "subsidy_func.h"
19 #include "subsidy_base.h"
20 #include "core/geometry_func.hpp"
21 
22 #include "widgets/subsidy_widget.h"
23 
24 #include "table/strings.h"
25 
26 #include "safeguards.h"
27 
29  Scrollbar *vscroll;
30 
32  {
33  this->CreateNestedTree();
34  this->vscroll = this->GetScrollbar(WID_SUL_SCROLLBAR);
35  this->FinishInitNested(window_number);
36  this->OnInvalidateData(0);
37  }
38 
39  void OnClick(Point pt, int widget, int click_count) override
40  {
41  if (widget != WID_SUL_PANEL) return;
42 
43  int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SUL_PANEL, WD_FRAMERECT_TOP);
44  int num = 0;
45  for (const Subsidy *s : Subsidy::Iterate()) {
46  if (!s->IsAwarded()) {
47  y--;
48  if (y == 0) {
49  this->HandleClick(s);
50  return;
51  }
52  num++;
53  }
54  }
55 
56  if (num == 0) {
57  y--; // "None"
58  if (y < 0) return;
59  }
60 
61  y -= 2; // "Services already subsidised:"
62  if (y < 0) return;
63 
64  for (const Subsidy *s : Subsidy::Iterate()) {
65  if (s->IsAwarded()) {
66  y--;
67  if (y == 0) {
68  this->HandleClick(s);
69  return;
70  }
71  }
72  }
73  }
74 
75  void HandleClick(const Subsidy *s)
76  {
77  /* determine src coordinate for subsidy and try to scroll to it */
78  TileIndex xy;
79  switch (s->src_type) {
80  case ST_INDUSTRY: xy = Industry::Get(s->src)->location.tile; break;
81  case ST_TOWN: xy = Town::Get(s->src)->xy; break;
82  default: NOT_REACHED();
83  }
84 
87 
88  /* otherwise determine dst coordinate for subsidy and scroll to it */
89  switch (s->dst_type) {
90  case ST_INDUSTRY: xy = Industry::Get(s->dst)->location.tile; break;
91  case ST_TOWN: xy = Town::Get(s->dst)->xy; break;
92  default: NOT_REACHED();
93  }
94 
95  if (_ctrl_pressed) {
97  } else {
99  }
100  }
101  }
102 
107  uint CountLines()
108  {
109  /* Count number of (non) awarded subsidies */
110  uint num_awarded = 0;
111  uint num_not_awarded = 0;
112  for (const Subsidy *s : Subsidy::Iterate()) {
113  if (!s->IsAwarded()) {
114  num_not_awarded++;
115  } else {
116  num_awarded++;
117  }
118  }
119 
120  /* Count the 'none' lines */
121  if (num_awarded == 0) num_awarded = 1;
122  if (num_not_awarded == 0) num_not_awarded = 1;
123 
124  /* Offered, accepted and an empty line before the accepted ones. */
125  return 3 + num_awarded + num_not_awarded;
126  }
127 
128  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
129  {
130  if (widget != WID_SUL_PANEL) return;
131  Dimension d = maxdim(GetStringBoundingBox(STR_SUBSIDIES_OFFERED_TITLE), GetStringBoundingBox(STR_SUBSIDIES_SUBSIDISED_TITLE));
132 
133  resize->height = d.height;
134 
135  d.height *= 5;
136  d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
137  d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
138  *size = maxdim(*size, d);
139  }
140 
141  void DrawWidget(const Rect &r, int widget) const override
142  {
143  if (widget != WID_SUL_PANEL) return;
144 
145  YearMonthDay ymd;
146  ConvertDateToYMD(_date, &ymd);
147 
148  int right = r.right - WD_FRAMERECT_RIGHT;
149  int y = r.top + WD_FRAMERECT_TOP;
150  int x = r.left + WD_FRAMERECT_LEFT;
151 
152  int pos = -this->vscroll->GetPosition();
153  const int cap = this->vscroll->GetCapacity();
154 
155  /* Section for drawing the offered subsidies */
156  if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_TITLE);
157  pos++;
158 
159  uint num = 0;
160  for (const Subsidy *s : Subsidy::Iterate()) {
161  if (!s->IsAwarded()) {
162  if (IsInsideMM(pos, 0, cap)) {
163  /* Displays the two offered towns */
164  SetupSubsidyDecodeParam(s, true);
165  SetDParam(7, _date - ymd.day + s->remaining * 32);
166  DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_FROM_TO);
167  }
168  pos++;
169  num++;
170  }
171  }
172 
173  if (num == 0) {
174  if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
175  pos++;
176  }
177 
178  /* Section for drawing the already granted subsidies */
179  pos++;
180  if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_TITLE);
181  pos++;
182  num = 0;
183 
184  for (const Subsidy *s : Subsidy::Iterate()) {
185  if (s->IsAwarded()) {
186  if (IsInsideMM(pos, 0, cap)) {
187  SetupSubsidyDecodeParam(s, true);
188  SetDParam(7, s->awarded);
189  SetDParam(8, _date - ymd.day + s->remaining * 32);
190 
191  /* Displays the two connected stations */
192  DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_FROM_TO);
193  }
194  pos++;
195  num++;
196  }
197  }
198 
199  if (num == 0) {
200  if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
201  pos++;
202  }
203  }
204 
205  void OnResize() override
206  {
207  this->vscroll->SetCapacityFromWidget(this, WID_SUL_PANEL);
208  }
209 
215  void OnInvalidateData(int data = 0, bool gui_scope = true) override
216  {
217  if (!gui_scope) return;
218  this->vscroll->SetCount(this->CountLines());
219  }
220 };
221 
222 static const NWidgetPart _nested_subsidies_list_widgets[] = {
224  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
225  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
226  NWidget(WWT_SHADEBOX, COLOUR_BROWN),
227  NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
228  NWidget(WWT_STICKYBOX, COLOUR_BROWN),
229  EndContainer(),
231  NWidget(WWT_PANEL, COLOUR_BROWN, WID_SUL_PANEL), SetDataTip(0x0, STR_SUBSIDIES_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetResize(1, 1), SetScrollbar(WID_SUL_SCROLLBAR), EndContainer(),
233  NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_SUL_SCROLLBAR),
234  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
235  EndContainer(),
236  EndContainer(),
237 };
238 
239 static WindowDesc _subsidies_list_desc(
240  WDP_AUTO, "list_subsidies", 500, 127,
242  0,
243  _nested_subsidies_list_widgets, lengthof(_nested_subsidies_list_widgets)
244 );
245 
246 
247 void ShowSubsidiesList()
248 {
249  AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
250 }
Functions related to OTTD&#39;s strings.
Source/destination is a town.
Definition: cargo_type.h:148
Pair SetupSubsidyDecodeParam(const Subsidy *s, bool mode)
Setup the string parameters for printing the subsidy at the screen, and compute the news reference fo...
Definition: subsidy.cpp:74
ResizeInfo resize
Resize information.
Definition: window_gui.h:322
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:928
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1867
CompanyID awarded
Subsidy is awarded to this company; INVALID_COMPANY if it&#39;s not awarded to anyone.
Definition: subsidy_base.h:25
High level window description.
Definition: window_gui.h:166
SourceID src
Index of source. Either TownID or IndustryID.
Definition: subsidy_base.h:28
bool IsAwarded() const
Tests whether this subsidy has been awarded to someone.
Definition: subsidy_base.h:45
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
Functions related to dates.
Day day
Day (1..31)
Definition: date_type.h:104
Scrollbar data structure.
Definition: widget_type.h:587
SourceType dst_type
Destination of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:27
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:62
Horizontal container.
Definition: widget_type.h:73
SourceID dst
Index of destination. Either TownID or IndustryID.
Definition: subsidy_base.h:29
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:66
uint CountLines()
Count the number of lines in this window.
Close box (at top-left of a window)
Definition: widget_type.h:67
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1828
Functions, definitions and such used only by the GUI.
void SetCount(int num)
Sets the number of elements in the list.
Definition: widget_type.h:668
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:908
Functions related to (drawing on) viewports.
Data structure for an opened window.
Definition: window_gui.h:276
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:35
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1844
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:264
Types related to the subsidy widgets.
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX) ...
Definition: widget_type.h:63
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:1957
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:176
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1012
Definition of base types and functions in a cross-platform compatible way.
Data structure to convert between Date and triplet (year, month, and day).
Definition: date_type.h:101
A number of safeguards to prevent using unsafe methods.
Geometry functions.
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Simple depressed panel.
Definition: widget_type.h:48
const Scrollbar * GetScrollbar(uint widnum) const
Return the Scrollbar to a widget index.
Definition: window.cpp:309
Subsidies list; Window numbers:
Definition: window_type.h:253
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:1112
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:63
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:498
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
void ShowExtraViewPortWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
Struct about subsidies, offered and awarded.
Definition: subsidy_base.h:22
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:700
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:38
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:340
Subsidy base class.
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Vertical container.
Definition: widget_type.h:75
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:997
SourceType src_type
Source of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:26
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2396
Coordinates of a point in 2D.
uint16 GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:620
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: subsidy_gui.cpp:39
Main panel of window.
Base of all industries.
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:61
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:64
Base of the town class.
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:705
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:1971
Specification of a rectangle with absolute coordinates of all edges.
Vertical scrollbar.
Definition: widget_type.h:82
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:312
byte remaining
Remaining months when this subsidy is valid.
Definition: subsidy_base.h:24
Find a place automatically.
Definition: window_gui.h:154
void OnResize() override
Called after the window got resized.
GUI functions that shouldn&#39;t be here.
Date _date
Current date in days (day counter)
Definition: date.cpp:26
void ConvertDateToYMD(Date date, YearMonthDay *ymd)
Converts a Date to a Year, Month & Day.
Definition: date.cpp:92
static NWidgetPart SetScrollbar(int index)
Attach a scrollbar to a widget.
Definition: widget_type.h:1093
Dimensions (a width and height) of a rectangle in 2D.
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:60
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:62
Functions related to subsidies.
Source/destination is an industry.
Definition: cargo_type.h:147
uint16 GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:629
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:199
Scrollbar of panel.