OpenTTD Source  14.0-beta1
date_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 "strings_func.h"
13 #include "window_func.h"
14 #include "window_gui.h"
15 #include "date_gui.h"
16 #include "core/geometry_func.hpp"
17 
18 #include "widgets/dropdown_type.h"
19 #include "widgets/date_widget.h"
20 
21 #include "safeguards.h"
22 
23 
27  void *callback_data;
28  TimerGameEconomy::YearMonthDay date;
31 
43  Window(desc),
46  min_year(std::max(EconomyTime::MIN_YEAR, min_year)),
47  max_year(std::min(EconomyTime::MAX_YEAR, max_year))
48  {
49  assert(this->min_year <= this->max_year);
50  this->parent = parent;
51  this->InitNested(window_number);
52 
53  if (initial_date == 0) initial_date = TimerGameEconomy::date;
54  this->date = TimerGameEconomy::ConvertDateToYMD(initial_date);
55  this->date.year = Clamp(this->date.year, min_year, max_year);
56  }
57 
58  Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
59  {
60  Point pt = { this->parent->left + this->parent->width / 2 - sm_width / 2, this->parent->top + this->parent->height / 2 - sm_height / 2 };
61  return pt;
62  }
63 
69  {
70  int selected;
71  DropDownList list;
72 
73  switch (widget) {
74  default: NOT_REACHED();
75 
76  case WID_SD_DAY:
77  for (uint i = 0; i < 31; i++) {
78  list.push_back(std::make_unique<DropDownListStringItem>(STR_DAY_NUMBER_1ST + i, i + 1, false));
79  }
80  selected = this->date.day;
81  break;
82 
83  case WID_SD_MONTH:
84  for (uint i = 0; i < 12; i++) {
85  list.push_back(std::make_unique<DropDownListStringItem>(STR_MONTH_JAN + i, i, false));
86  }
87  selected = this->date.month;
88  break;
89 
90  case WID_SD_YEAR:
91  for (TimerGameEconomy::Year i = this->min_year; i <= this->max_year; i++) {
92  SetDParam(0, i);
93  list.push_back(std::make_unique<DropDownListStringItem>(STR_JUST_INT, i.base(), false));
94  }
95  selected = this->date.year.base();
96  break;
97  }
98 
99  ShowDropDownList(this, std::move(list), selected, widget);
100  }
101 
102  void UpdateWidgetSize(WidgetID widget, Dimension *size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension *fill, [[maybe_unused]] Dimension *resize) override
103  {
104  Dimension d = {0, 0};
105  switch (widget) {
106  default: return;
107 
108  case WID_SD_DAY:
109  for (uint i = 0; i < 31; i++) {
110  d = maxdim(d, GetStringBoundingBox(STR_DAY_NUMBER_1ST + i));
111  }
112  break;
113 
114  case WID_SD_MONTH:
115  for (uint i = 0; i < 12; i++) {
116  d = maxdim(d, GetStringBoundingBox(STR_MONTH_JAN + i));
117  }
118  break;
119 
120  case WID_SD_YEAR:
121  SetDParamMaxValue(0, this->max_year);
122  d = maxdim(d, GetStringBoundingBox(STR_JUST_INT));
123  break;
124  }
125 
126  d.width += padding.width;
127  d.height += padding.height;
128  *size = d;
129  }
130 
131  void SetStringParameters(WidgetID widget) const override
132  {
133  switch (widget) {
134  case WID_SD_DAY: SetDParam(0, this->date.day - 1 + STR_DAY_NUMBER_1ST); break;
135  case WID_SD_MONTH: SetDParam(0, this->date.month + STR_MONTH_JAN); break;
136  case WID_SD_YEAR: SetDParam(0, this->date.year); break;
137  }
138  }
139 
140  void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
141  {
142  switch (widget) {
143  case WID_SD_DAY:
144  case WID_SD_MONTH:
145  case WID_SD_YEAR:
146  ShowDateDropDown(widget);
147  break;
148 
149  case WID_SD_SET_DATE:
150  if (this->callback != nullptr) this->callback(this, TimerGameEconomy::ConvertYMDToDate(this->date.year, this->date.month, this->date.day), this->callback_data);
151  this->Close();
152  break;
153  }
154  }
155 
156  void OnDropdownSelect(WidgetID widget, int index) override
157  {
158  switch (widget) {
159  case WID_SD_DAY:
160  this->date.day = index;
161  break;
162 
163  case WID_SD_MONTH:
164  this->date.month = index;
165  break;
166 
167  case WID_SD_YEAR:
168  this->date.year = index;
169  break;
170  }
171  this->SetDirty();
172  }
173 };
174 
176 static constexpr NWidgetPart _nested_set_date_widgets[] = {
178  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
179  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_DATE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
180  EndContainer(),
181  NWidget(WWT_PANEL, COLOUR_BROWN),
182  NWidget(NWID_VERTICAL), SetPIP(6, 6, 6),
184  NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_DAY), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_DAY_TOOLTIP),
185  NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_MONTH), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_DATE_MONTH_TOOLTIP),
186  NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_YEAR), SetFill(1, 0), SetDataTip(STR_JUST_INT, STR_DATE_YEAR_TOOLTIP),
187  EndContainer(),
189  NWidget(NWID_SPACER), SetFill(1, 0),
190  NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SD_SET_DATE), SetMinimalSize(100, 12), SetDataTip(STR_DATE_SET_DATE, STR_DATE_SET_DATE_TOOLTIP),
191  NWidget(NWID_SPACER), SetFill(1, 0),
192  EndContainer(),
193  EndContainer(),
194  EndContainer()
195 };
196 
198 static WindowDesc _set_date_desc(__FILE__, __LINE__,
199  WDP_CENTER, nullptr, 0, 0,
201  0,
203 );
204 
215 void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data)
216 {
218  new SetDateWindow(&_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback, callback_data);
219 }
SetDateCallback
void SetDateCallback(const Window *w, TimerGameEconomy::Date date, void *data)
Callback for when a date has been chosen.
Definition: date_gui.h:21
SetFill
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
Definition: widget_type.h:1141
CloseWindowByClass
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
Definition: window.cpp:1153
SetDateWindow::max_year
TimerGameEconomy::Year max_year
The maximum year (inclusive) in the year dropdown.
Definition: date_gui.cpp:30
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
SetDateWindow::callback
SetDateCallback * callback
Callback to call when a date has been selected.
Definition: date_gui.cpp:26
TimerGameEconomy::ConvertYMDToDate
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: timer_game_economy.cpp:66
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:63
DropDownList
std::vector< std::unique_ptr< const DropDownListItem > > DropDownList
A drop down list is a collection of drop down list items.
Definition: dropdown_type.h:210
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
Window::Close
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition: window.cpp:1048
EndContainer
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1151
WID_SD_MONTH
@ WID_SD_MONTH
Dropdown for the month.
Definition: date_widget.h:16
EconomyTime
Storage class for Economy time constants.
Definition: timer_game_economy.h:49
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition: strong_typedef_type.hpp:150
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:1038
WindowDesc
High level window description.
Definition: window_gui.h:153
WidgetID
int WidgetID
Widget ID.
Definition: window_type.h:18
window_gui.h
NC_EQUALSIZE
@ NC_EQUALSIZE
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:508
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
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1747
Window::height
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:306
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:941
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:110
SetDateWindow::callback_data
void * callback_data
Callback data pointer.
Definition: date_gui.cpp:27
dropdown_type.h
Window::parent
Window * parent
Parent window.
Definition: window_gui.h:322
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
safeguards.h
Window::left
int left
x position of left edge of the window
Definition: window_gui.h:303
WC_SET_DATE
@ WC_SET_DATE
Set date; Window numbers:
Definition: window_type.h:168
SetDateWindow
Window to select a date graphically by using dropdowns.
Definition: date_gui.cpp:25
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
WID_SD_YEAR
@ WID_SD_YEAR
Dropdown for the year.
Definition: date_widget.h:17
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
date_gui.h
SetDateWindow::date
TimerGameEconomy::YearMonthDay date
The currently selected date.
Definition: date_gui.cpp:28
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:45
NWID_VERTICAL
@ NWID_VERTICAL
Vertical container.
Definition: widget_type.h:79
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:71
_nested_set_date_widgets
static constexpr NWidgetPart _nested_set_date_widgets[]
Widgets for the date setting window.
Definition: date_gui.cpp:176
strings_func.h
ShowDropDownList
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, bool instant_close)
Show a drop down list.
Definition: dropdown.cpp:349
ShowSetDateWindow
void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data)
Create the new 'set date' window.
Definition: date_gui.cpp:215
date_widget.h
SetPIP
constexpr NWidgetPart SetPIP(uint8_t pre, uint8_t inter, uint8_t post)
Widget part function for setting a pre/inter/post spaces.
Definition: widget_type.h:1220
SetDParamMaxValue
void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count, FontSize size)
Set DParam n to some number that is suitable for string size computations.
Definition: strings.cpp:127
_set_date_desc
static WindowDesc _set_date_desc(__FILE__, __LINE__, WDP_CENTER, nullptr, 0, 0, WC_SET_DATE, WC_NONE, 0, std::begin(_nested_set_date_widgets), std::end(_nested_set_date_widgets))
Description of the date setting window.
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
SetDateWindow::ShowDateDropDown
void ShowDateDropDown(WidgetID widget)
Helper function to construct the dropdown.
Definition: date_gui.cpp:68
geometry_func.hpp
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:52
WID_SD_SET_DATE
@ WID_SD_SET_DATE
Actually set the date.
Definition: date_widget.h:18
SetDateWindow::min_year
TimerGameEconomy::Year min_year
The minimum year in the year dropdown.
Definition: date_gui.cpp:29
NWID_SPACER
@ NWID_SPACER
Invisible widget that takes some space.
Definition: widget_type.h:81
Window::top
int top
y position of top edge of the window
Definition: window_gui.h:304
window_func.h
Window::width
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:305
SetMinimalSize
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
Definition: widget_type.h:1097
Window
Data structure for an opened window.
Definition: window_gui.h:267
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
SetDataTip
constexpr NWidgetPart SetDataTip(uint32_t data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1162
WDP_CENTER
@ WDP_CENTER
Center the window.
Definition: window_gui.h:142
GetStringBoundingBox
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:852
timer_game_economy.h
WID_SD_DAY
@ WID_SD_DAY
Dropdown for the day.
Definition: date_widget.h:15
SetDateWindow::SetDateWindow
SetDateWindow(WindowDesc *desc, WindowNumber window_number, Window *parent, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data)
Create the new 'set date' window.
Definition: date_gui.cpp:42
WWT_DROPDOWN
@ WWT_DROPDOWN
Drop down list.
Definition: widget_type.h:72
TimerGameEconomy::date
static Date date
Current date in days (day counter).
Definition: timer_game_economy.h:37