OpenTTD Source  13.2.1
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, WidgetDimensions::scaled.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 = FONT_HEIGHT_NORMAL;
134 
135  d.height *= 5;
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  Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
149 
150  int pos = -this->vscroll->GetPosition();
151  const int cap = this->vscroll->GetCapacity();
152 
153  /* Section for drawing the offered subsidies */
154  if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_TITLE);
155  pos++;
156 
157  uint num = 0;
158  for (const Subsidy *s : Subsidy::Iterate()) {
159  if (!s->IsAwarded()) {
160  if (IsInsideMM(pos, 0, cap)) {
161  /* Displays the two offered towns */
163  SetDParam(7, _date - ymd.day + s->remaining * 32);
164  DrawString(tr.left, tr.right, tr.top + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_FROM_TO);
165  }
166  pos++;
167  num++;
168  }
169  }
170 
171  if (num == 0) {
172  if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
173  pos++;
174  }
175 
176  /* Section for drawing the already granted subsidies */
177  pos++;
178  if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_TITLE);
179  pos++;
180  num = 0;
181 
182  for (const Subsidy *s : Subsidy::Iterate()) {
183  if (s->IsAwarded()) {
184  if (IsInsideMM(pos, 0, cap)) {
186  SetDParam(7, s->awarded);
187  SetDParam(8, _date - ymd.day + s->remaining * 32);
188 
189  /* Displays the two connected stations */
190  DrawString(tr.left, tr.right, tr.top + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_FROM_TO);
191  }
192  pos++;
193  num++;
194  }
195  }
196 
197  if (num == 0) {
198  if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
199  pos++;
200  }
201  }
202 
203  void OnResize() override
204  {
205  this->vscroll->SetCapacityFromWidget(this, WID_SUL_PANEL);
206  }
207 
213  void OnInvalidateData(int data = 0, bool gui_scope = true) override
214  {
215  if (!gui_scope) return;
216  this->vscroll->SetCount(this->CountLines());
217  }
218 };
219 
220 static const NWidgetPart _nested_subsidies_list_widgets[] = {
222  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
223  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
224  NWidget(WWT_SHADEBOX, COLOUR_BROWN),
225  NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
226  NWidget(WWT_STICKYBOX, COLOUR_BROWN),
227  EndContainer(),
229  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(),
231  NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_SUL_SCROLLBAR),
232  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
233  EndContainer(),
234  EndContainer(),
235 };
236 
237 static WindowDesc _subsidies_list_desc(
238  WDP_AUTO, "list_subsidies", 500, 127,
240  0,
241  _nested_subsidies_list_widgets, lengthof(_nested_subsidies_list_widgets)
242 );
243 
244 
245 void ShowSubsidiesList()
246 {
247  AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
248 }
Subsidy::remaining
uint16 remaining
Remaining months when this subsidy is valid.
Definition: subsidy_base.h:24
YearMonthDay::day
Day day
Day (1..31)
Definition: date_type.h:107
IsInsideMM
static constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition: math_func.hpp:230
SubsidyListWindow::CountLines
uint CountLines()
Count the number of lines in this window.
Definition: subsidy_gui.cpp:107
Pool::PoolItem<&_industry_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
ScrollMainWindowToTile
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2456
SetScrollbar
static NWidgetPart SetScrollbar(int index)
Attach a scrollbar to a widget.
Definition: widget_type.h:1210
ShowExtraViewportWindow
void ShowExtraViewportWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
Definition: viewport_gui.cpp:168
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
Scrollbar::GetCapacity
uint16 GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:669
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:64
Window::GetScrollbar
const Scrollbar * GetScrollbar(uint widnum) const
Return the Scrollbar to a widget index.
Definition: window.cpp:319
Rect::Shrink
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Definition: geometry_type.hpp:92
WC_SUBSIDIES_LIST
@ WC_SUBSIDIES_LIST
Subsidies list; Window numbers:
Definition: window_type.h:253
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
WWT_DEFSIZEBOX
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition: widget_type.h:63
Window::CreateNestedTree
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1775
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:73
maxdim
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Definition: geometry_func.cpp:22
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
Scrollbar::SetCount
void SetCount(int num)
Sets the number of elements in the list.
Definition: widget_type.h:717
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:38
SetResize
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:997
DrawString
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:644
Subsidy::IsAwarded
bool IsAwarded() const
Tests whether this subsidy has been awarded to someone.
Definition: subsidy_base.h:45
town.h
SubsidyListWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: subsidy_gui.cpp:213
WindowNumber
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:713
Scrollbar::GetScrolledRowFromWidget
int GetScrolledRowFromWidget(int clickpos, const Window *const w, int widget, int padding=0) const
Compute the row of a scrolled widget that a user clicked in.
Definition: widget.cpp:2353
Scrollbar
Scrollbar data structure.
Definition: widget_type.h:636
SetupSubsidyDecodeParam
std::pair< NewsReferenceType, NewsReferenceType > SetupSubsidyDecodeParam(const Subsidy *s, SubsidyDecodeParamType mode, uint parameter_offset)
Setup the string parameters for printing the subsidy at the screen, and compute the news reference fo...
Definition: subsidy.cpp:73
Window::Window
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1814
SetDParam
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:196
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:975
SetDataTip
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1111
GetStringBoundingBox
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:890
ST_TOWN
@ ST_TOWN
Source/destination is a town.
Definition: cargo_type.h:149
WindowDesc
High level window description.
Definition: window_gui.h:102
window_gui.h
Subsidy
Struct about subsidies, offered and awarded.
Definition: subsidy_base.h:22
WDP_AUTO
@ WDP_AUTO
Find a place automatically.
Definition: window_gui.h:90
Window::resize
ResizeInfo resize
Resize information.
Definition: window_gui.h:251
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
SubsidyListWindow::OnClick
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
RectPadding::Horizontal
uint Horizontal() const
Get total horizontal padding of RectPadding.
Definition: geometry_type.hpp:59
Subsidy::src
SourceID src
Index of source. Either TownID or IndustryID.
Definition: subsidy_base.h:28
ST_INDUSTRY
@ ST_INDUSTRY
Source/destination is an industry.
Definition: cargo_type.h:148
ConvertDateToYMD
void ConvertDateToYMD(Date date, YearMonthDay *ymd)
Converts a Date to a Year, Month & Day.
Definition: date.cpp:94
industry.h
safeguards.h
Subsidy::dst
SourceID dst
Index of destination. Either TownID or IndustryID.
Definition: subsidy_base.h:29
SubsidyDecodeParamType::Gui
@ Gui
Subsidies listed in the Subsidy GUI.
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
SubsidyListWindow::DrawWidget
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: subsidy_gui.cpp:141
date_func.h
stdafx.h
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:241
RectPadding::Vertical
uint Vertical() const
Get total vertical padding of RectPadding.
Definition: geometry_type.hpp:65
viewport_func.h
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:38
subsidy_widget.h
NWID_VERTICAL
@ NWID_VERTICAL
Vertical container.
Definition: widget_type.h:75
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:67
WWT_RESIZEBOX
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:66
WID_SUL_PANEL
@ WID_SUL_PANEL
Main panel of window.
Definition: subsidy_widget.h:16
Subsidy::dst_type
SourceType dst_type
Destination of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:27
SubsidyListWindow::UpdateWidgetSize
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.
Definition: subsidy_gui.cpp:128
EndContainer
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1096
Pool::PoolItem<&_subsidy_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
strings_func.h
NWID_VSCROLLBAR
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition: widget_type.h:82
subsidy_func.h
FONT_HEIGHT_NORMAL
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:206
NWidget
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1229
geometry_func.hpp
SubsidyListWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition: subsidy_gui.cpp:203
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:48
Scrollbar::GetPosition
uint16 GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:678
subsidy_base.h
Window::FinishInitNested
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1791
WidgetDimensions::framerect
RectPadding framerect
Offsets within frame area.
Definition: window_gui.h:47
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:386
YearMonthDay
Data structure to convert between Date and triplet (year, month, and day).
Definition: date_type.h:104
Scrollbar::SetCapacityFromWidget
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:2427
gui.h
Window
Data structure for an opened window.
Definition: window_gui.h:213
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition: window_gui.h:68
Subsidy::src_type
SourceType src_type
Source of subsidised path (ST_INDUSTRY or ST_TOWN)
Definition: subsidy_base.h:26
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:69
Subsidy::awarded
CompanyID awarded
Subsidy is awarded to this company; INVALID_COMPANY if it's not awarded to anyone.
Definition: subsidy_base.h:25
SubsidyListWindow
Definition: subsidy_gui.cpp:28
WID_SUL_SCROLLBAR
@ WID_SUL_SCROLLBAR
Scrollbar of panel.
Definition: subsidy_widget.h:17
WWT_SHADEBOX
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:62