OpenTTD Source  12.0-beta2
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 */
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)) {
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 }
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
WD_FRAMERECT_TOP
@ WD_FRAMERECT_TOP
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:64
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
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:2445
SetScrollbar
static NWidgetPart SetScrollbar(int index)
Attach a scrollbar to a widget.
Definition: widget_type.h:1188
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:662
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:320
WC_SUBSIDIES_LIST
@ WC_SUBSIDIES_LIST
Subsidies list; Window numbers:
Definition: window_type.h:252
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:1760
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
Scrollbar::SetCount
void SetCount(int num)
Sets the number of elements in the list.
Definition: widget_type.h:710
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:35
SetResize
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:993
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:642
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:215
WindowNumber
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:711
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:2098
Scrollbar
Scrollbar data structure.
Definition: widget_type.h:629
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:72
Window::Window
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1799
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:971
SetDataTip
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1107
GetStringBoundingBox
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:888
ST_TOWN
@ ST_TOWN
Source/destination is a town.
Definition: cargo_type.h:149
IsInsideMM
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:204
WindowDesc
High level window description.
Definition: window_gui.h:168
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:156
Window::resize
ResizeInfo resize
Resize information.
Definition: window_gui.h:317
_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
WD_FRAMERECT_LEFT
@ WD_FRAMERECT_LEFT
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:62
Subsidy::src
SourceID src
Index of source. Either TownID or IndustryID.
Definition: subsidy_base.h:28
WD_FRAMERECT_RIGHT
@ WD_FRAMERECT_RIGHT
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:63
WD_FRAMERECT_BOTTOM
@ WD_FRAMERECT_BOTTOM
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:65
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:307
viewport_func.h
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:37
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:1092
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:165
NWidget
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1207
geometry_func.hpp
SubsidyListWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition: subsidy_gui.cpp:205
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:671
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:1776
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
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:2172
gui.h
Window
Data structure for an opened window.
Definition: window_gui.h:279
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:47
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