OpenTTD Source: src/subsidy_gui.cpp Source File

OpenTTD Source 14.0-beta1

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 "timer/timer_game_calendar.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
28 struct SubsidyListWindow : Window {
29 Scrollbar *vscroll;
30
31 SubsidyListWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
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([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] 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 SourceType::Industry: xy = Industry::Get(s->src)->location.tile; break;
81 case SourceType::Town: xy = Town::Get(s->src)->xy; break;
82 default: NOT_REACHED();
83 }
84
85 if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) {
86 if (_ctrl_pressed) ShowExtraViewportWindow(xy);
87
88 /* otherwise determine dst coordinate for subsidy and scroll to it */
89 switch (s->dst_type) {
90 case SourceType::Industry: xy = Industry::Get(s->dst)->location.tile; break;
91 case SourceType::Town: xy = Town::Get(s->dst)->xy; break;
92 default: NOT_REACHED();
93 }
94
95 if (_ctrl_pressed) {
96 ShowExtraViewportWindow(xy);
97 } else {
98 ScrollMainWindowToTile(xy);
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(WidgetID widget, Dimension *size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension *fill, [[maybe_unused]] 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 = GetCharacterHeight(FS_NORMAL);
134
135 d.height *= 5;
136 d.width += WidgetDimensions::scaled.framerect.Horizontal();
137 d.height += WidgetDimensions::scaled.framerect.Vertical();
138 *size = maxdim(*size, d);
139 }
140
141 void DrawWidget(const Rect &r, WidgetID widget) const override
142 {
143 if (widget != WID_SUL_PANEL) return;
144
145 TimerGameEconomy::YearMonthDay ymd = TimerGameEconomy::ConvertDateToYMD(TimerGameEconomy::date);
146
147 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
148
149 int pos = -this->vscroll->GetPosition();
150 const int cap = this->vscroll->GetCapacity();
151
152 /* Section for drawing the offered subsidies */
153 if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_OFFERED_TITLE);
154 pos++;
155
156 uint num = 0;
157 for (const Subsidy *s : Subsidy::Iterate()) {
158 if (!s->IsAwarded()) {
159 if (IsInsideMM(pos, 0, cap)) {
160 /* Displays the two offered towns */
161 SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::Gui);
162 /* If using wallclock units, show minutes remaining. Otherwise show the date when the subsidy ends. */
163 if (TimerGameEconomy::UsingWallclockUnits()) {
164 SetDParam(7, STR_SUBSIDIES_OFFERED_EXPIRY_TIME);
165 SetDParam(8, s->remaining + 1); // We get the rest of the current economy month for free, since the expiration is checked on each new month.
166 } else {
167 SetDParam(7, STR_SUBSIDIES_OFFERED_EXPIRY_DATE);
168 SetDParam(8, TimerGameEconomy::date - ymd.day + s->remaining * 32);
169 }
170
171 DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_OFFERED_FROM_TO);
172 }
173 pos++;
174 num++;
175 }
176 }
177
178 if (num == 0) {
179 if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_NONE);
180 pos++;
181 }
182
183 /* Section for drawing the already granted subsidies */
184 pos++;
185 if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_SUBSIDISED_TITLE);
186 pos++;
187 num = 0;
188
189 for (const Subsidy *s : Subsidy::Iterate()) {
190 if (s->IsAwarded()) {
191 if (IsInsideMM(pos, 0, cap)) {
192 SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::Gui);
193 SetDParam(7, s->awarded);
194 /* If using wallclock units, show minutes remaining. Otherwise show the date when the subsidy ends. */
195 if (TimerGameEconomy::UsingWallclockUnits()) {
196 SetDParam(8, STR_SUBSIDIES_SUBSIDISED_EXPIRY_TIME);
197 SetDParam(9, s->remaining);
198 }
199 else {
200 SetDParam(8, STR_SUBSIDIES_SUBSIDISED_EXPIRY_DATE);
201 SetDParam(9, TimerGameEconomy::date - ymd.day + s->remaining * 32);
202 }
203
204 /* Displays the two connected stations */
205 DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_SUBSIDISED_FROM_TO);
206 }
207 pos++;
208 num++;
209 }
210 }
211
212 if (num == 0) {
213 if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_NONE);
214 pos++;
215 }
216 }
217
218 void OnResize() override
219 {
220 this->vscroll->SetCapacityFromWidget(this, WID_SUL_PANEL);
221 }
222
228 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
229 {
230 if (!gui_scope) return;
231 this->vscroll->SetCount(this->CountLines());
232 }
233 };
234
235 static constexpr NWidgetPart _nested_subsidies_list_widgets[] = {
236 NWidget(NWID_HORIZONTAL),
237 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
238 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
239 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
240 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
241 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
242 EndContainer(),
243 NWidget(NWID_HORIZONTAL),
244 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(),
245 NWidget(NWID_VERTICAL),
246 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_SUL_SCROLLBAR),
247 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
248 EndContainer(),
249 EndContainer(),
250 };
251
252 static WindowDesc _subsidies_list_desc(__FILE__, __LINE__,
253 WDP_AUTO, "list_subsidies", 500, 127,
254 WC_SUBSIDIES_LIST, WC_NONE,
255 0,
256 std::begin(_nested_subsidies_list_widgets), std::end(_nested_subsidies_list_widgets)
257 );
258
259
260 void ShowSubsidiesList()
261 {
262 AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
263 }
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:335
ScrollMainWindowToTile
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2509
Subsidy::remaining
uint16_t remaining
Remaining months when this subsidy is valid.
Definition: subsidy_base.h:24
ShowExtraViewportWindow
void ShowExtraViewportWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
Definition: viewport_gui.cpp:156
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
IsInsideMM
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:268
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition: window_gui.h:68
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:68
Rect::Shrink
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Definition: geometry_type.hpp:98
SubsidyListWindow::OnInvalidateData
void OnInvalidateData([[maybe_unused]] int data=0, [[maybe_unused]] bool gui_scope=true) override
Some data on this window has become invalid.
Definition: subsidy_gui.cpp:228
timer_game_calendar.h
WC_SUBSIDIES_LIST
@ WC_SUBSIDIES_LIST
Subsidies list; Window numbers:
Definition: window_type.h:260
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:63
WWT_DEFSIZEBOX
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition: widget_type.h:67
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:77
maxdim
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Definition: geometry_func.cpp:22
EndContainer
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1151
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:37
Scrollbar::SetCapacityFromWidget
void SetCapacityFromWidget(Window *w, WidgetID widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget.
Definition: widget.cpp:2334
Subsidy::IsAwarded
bool IsAwarded() const
Tests whether this subsidy has been awarded to someone.
Definition: subsidy_base.h:45
TimerGameEconomy::UsingWallclockUnits
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
Definition: timer_game_economy.cpp:97
town.h
RectPadding::Vertical
constexpr uint Vertical() const
Get total vertical padding of RectPadding.
Definition: geometry_type.hpp:69
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Scrollbar
Scrollbar data structure.
Definition: widget_type.h:678
Window::GetScrollbar
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition: window.cpp:315
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:76
Window::Window
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1757
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:1038
Scrollbar::GetPosition
uint16_t GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:720
WindowDesc
High level window description.
Definition: window_gui.h:153
WidgetID
int WidgetID
Widget ID.
Definition: window_type.h:18
RectPadding::Horizontal
constexpr uint Horizontal() const
Get total horizontal padding of RectPadding.
Definition: geometry_type.hpp:63
window_gui.h
SetResize
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
Definition: widget_type.h:1086
Subsidy
Struct about subsidies, offered and awarded.
Definition: subsidy_base.h:22
WDP_AUTO
@ WDP_AUTO
Find a place automatically.
Definition: window_gui.h:141
Window::resize
ResizeInfo resize
Resize information.
Definition: window_gui.h:308
WindowNumber
int32_t WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:732
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:203
SetScrollbar
constexpr NWidgetPart SetScrollbar(WidgetID index)
Attach a scrollbar to a widget.
Definition: widget_type.h:1244
Subsidy::src
SourceID src
Index of source. Either TownID or IndustryID.
Definition: subsidy_base.h:28
WID_SUL_PANEL
@ WID_SUL_PANEL
Main panel of window.
Definition: subsidy_widget.h:16
Scrollbar::GetCapacity
uint16_t GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:711
NWidget
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1258
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
SourceType::Industry
@ Industry
Source/destination is an industry.
TimerGameEconomy::ConvertDateToYMD
static YearMonthDay ConvertDateToYMD(Date date)
Converts a Date to a Year, Month & Day.
Definition: timer_game_economy.cpp:46
stdafx.h
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:296
viewport_func.h
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:45
SourceType::Town
@ Town
Source/destination is a town.
subsidy_widget.h
NWID_VERTICAL
@ NWID_VERTICAL
Vertical container.
Definition: widget_type.h:79
Scrollbar::GetScrolledRowFromWidget
int GetScrolledRowFromWidget(int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition: widget.cpp:2260
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:71
WWT_RESIZEBOX
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:70
Subsidy::dst_type
SourceType dst_type
Destination of subsidised path (SourceType::Industry or SourceType::Town)
Definition: subsidy_base.h:27
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:384
Window::CreateNestedTree
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1724
strings_func.h
NWID_VSCROLLBAR
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition: widget_type.h:86
subsidy_func.h
WID_SUL_SCROLLBAR
@ WID_SUL_SCROLLBAR
Scrollbar of panel.
Definition: subsidy_widget.h:17
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings.cpp:104
geometry_func.hpp
SubsidyListWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition: subsidy_gui.cpp:218
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:52
Scrollbar::SetCount
void SetCount(size_t num)
Sets the number of elements in the list.
Definition: widget_type.h:760
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:1734
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:78
DrawString
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:658
gui.h
Window
Data structure for an opened window.
Definition: window_gui.h:267
SetDataTip
constexpr NWidgetPart SetDataTip(uint32_t data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1162
Subsidy::src_type
SourceType src_type
Source of subsidised path (SourceType::Industry or SourceType::Town)
Definition: subsidy_base.h:26
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
WidgetDimensions::framerect
RectPadding framerect
Standard padding inside many panels.
Definition: window_gui.h:42
Subsidy::awarded
CompanyID awarded
Subsidy is awarded to this company; INVALID_COMPANY if it's not awarded to anyone.
Definition: subsidy_base.h:25
GetStringBoundingBox
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:852
SubsidyListWindow
Definition: subsidy_gui.cpp:28
WWT_SHADEBOX
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:66
TimerGameEconomy::date
static Date date
Current date in days (day counter).
Definition: timer_game_economy.h:37