OpenTTD

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