OpenTTD Source 15.0-beta2
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"
17#include "dropdown_type.h"
18#include "dropdown_func.h"
19
20#include "widgets/date_widget.h"
21
22#include "table/strings.h"
23
24#include "safeguards.h"
25
26
30 void *callback_data = nullptr;
31 TimerGameEconomy::YearMonthDay date{};
34
46 Window(desc),
49 min_year(std::max(EconomyTime::MIN_YEAR, min_year)),
50 max_year(std::min(EconomyTime::MAX_YEAR, max_year))
51 {
52 assert(this->min_year <= this->max_year);
53 this->parent = parent;
54 this->InitNested(window_number);
55
56 if (initial_date == 0) initial_date = TimerGameEconomy::date;
57 this->date = TimerGameEconomy::ConvertDateToYMD(initial_date);
58 this->date.year = Clamp(this->date.year, min_year, max_year);
59 }
60
61 Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
62 {
63 Point pt = { this->parent->left + this->parent->width / 2 - sm_width / 2, this->parent->top + this->parent->height / 2 - sm_height / 2 };
64 return pt;
65 }
66
72 {
73 int selected;
74 DropDownList list;
75
76 switch (widget) {
77 default: NOT_REACHED();
78
79 case WID_SD_DAY:
80 for (uint i = 0; i < 31; i++) {
81 list.push_back(MakeDropDownListStringItem(STR_DAY_NUMBER_1ST + i, i + 1));
82 }
83 selected = this->date.day;
84 break;
85
86 case WID_SD_MONTH:
87 for (uint i = 0; i < 12; i++) {
88 list.push_back(MakeDropDownListStringItem(STR_MONTH_JAN + i, i));
89 }
90 selected = this->date.month;
91 break;
92
93 case WID_SD_YEAR:
94 for (TimerGameEconomy::Year i = this->min_year; i <= this->max_year; i++) {
95 list.push_back(MakeDropDownListStringItem(GetString(STR_JUST_INT, i), i.base()));
96 }
97 selected = this->date.year.base();
98 break;
99 }
100
101 ShowDropDownList(this, std::move(list), selected, widget);
102 }
103
104 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
105 {
106 Dimension d = {0, 0};
107 switch (widget) {
108 default: return;
109
110 case WID_SD_DAY:
111 for (uint i = 0; i < 31; i++) {
112 d = maxdim(d, GetStringBoundingBox(STR_DAY_NUMBER_1ST + i));
113 }
114 break;
115
116 case WID_SD_MONTH:
117 for (uint i = 0; i < 12; i++) {
118 d = maxdim(d, GetStringBoundingBox(STR_MONTH_JAN + i));
119 }
120 break;
121
122 case WID_SD_YEAR:
123 d = maxdim(d, GetStringBoundingBox(GetString(STR_JUST_INT, GetParamMaxValue(this->max_year.base()))));
124 break;
125 }
126
127 d.width += padding.width;
128 d.height += padding.height;
129 size = d;
130 }
131
132 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
133 {
134 switch (widget) {
135 case WID_SD_DAY: return GetString(STR_DAY_NUMBER_1ST + this->date.day - 1);
136 case WID_SD_MONTH: return GetString(STR_MONTH_JAN + this->date.month);
137 case WID_SD_YEAR: return GetString(STR_JUST_INT, this->date.year);
138 default: return this->Window::GetWidgetString(widget, stringid);
139 }
140 }
141
142 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
143 {
144 switch (widget) {
145 case WID_SD_DAY:
146 case WID_SD_MONTH:
147 case WID_SD_YEAR:
148 ShowDateDropDown(widget);
149 break;
150
151 case WID_SD_SET_DATE:
152 if (this->callback != nullptr) this->callback(this, TimerGameEconomy::ConvertYMDToDate(this->date.year, this->date.month, this->date.day), this->callback_data);
153 this->Close();
154 break;
155 }
156 }
157
158 void OnDropdownSelect(WidgetID widget, int index) override
159 {
160 switch (widget) {
161 case WID_SD_DAY:
162 this->date.day = index;
163 break;
164
165 case WID_SD_MONTH:
166 this->date.month = index;
167 break;
168
169 case WID_SD_YEAR:
170 this->date.year = TimerGameEconomy::Year{index};
171 break;
172 }
173 this->SetDirty();
174 }
175};
176
180 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
181 NWidget(WWT_CAPTION, COLOUR_BROWN), SetStringTip(STR_DATE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
182 EndContainer(),
183 NWidget(WWT_PANEL, COLOUR_BROWN),
184 NWidget(NWID_VERTICAL), SetPIP(6, 6, 6),
186 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_DAY), SetFill(1, 0), SetToolTip(STR_DATE_DAY_TOOLTIP),
187 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_MONTH), SetFill(1, 0), SetToolTip(STR_DATE_MONTH_TOOLTIP),
188 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_SD_YEAR), SetFill(1, 0), SetToolTip(STR_DATE_YEAR_TOOLTIP),
189 EndContainer(),
192 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SD_SET_DATE), SetMinimalSize(100, 12), SetStringTip(STR_DATE_SET_DATE, STR_DATE_SET_DATE_TOOLTIP),
194 EndContainer(),
195 EndContainer(),
197};
198
201 WDP_CENTER, nullptr, 0, 0,
203 {},
205);
206
217void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data)
218{
220 new SetDateWindow(_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback, callback_data);
221}
Storage class for Economy time constants.
static Date date
Current date in days (day counter).
static YearMonthDay ConvertDateToYMD(Date date)
Converts a Date to a Year, Month & Day.
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
static constexpr NWidgetPart _nested_set_date_widgets[]
Widgets for the date setting window.
Definition date_gui.cpp:178
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:217
static WindowDesc _set_date_desc(WDP_CENTER, nullptr, 0, 0, WC_SET_DATE, WC_NONE, {}, _nested_set_date_widgets)
Description of the date setting window.
Functions related to the graphical selection of a date.
void SetDateCallback(const Window *w, TimerGameEconomy::Date date, void *data)
Callback for when a date has been chosen.
Definition date_gui.h:21
Types related to the date widgets.
@ WID_SD_DAY
Dropdown for the day.
Definition date_widget.h:15
@ WID_SD_YEAR
Dropdown for the year.
Definition date_widget.h:17
@ WID_SD_SET_DATE
Actually set the date.
Definition date_widget.h:18
@ WID_SD_MONTH
Dropdown for the month.
Definition date_widget.h:16
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, bool instant_close, bool persist)
Show a drop down list.
Definition dropdown.cpp:407
Functions related to the drop down widget.
Types related to the drop down widget.
std::vector< std::unique_ptr< const DropDownListItem > > DropDownList
A drop down list is a collection of drop down list items.
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:852
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetPIP(uint8_t pre, uint8_t inter, uint8_t post)
Widget part function for setting a pre/inter/post spaces.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetToolTip(StringID tip)
Widget part function for setting tooltip and clearing the widget data.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:945
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
uint64_t GetParamMaxValue(uint64_t max_value, uint min_count, FontSize size)
Get some number that is suitable for string size computations.
Definition strings.cpp:246
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Dimensions (a width and height) of a rectangle in 2D.
Partial widget specification to allow NWidgets to be written nested.
Coordinates of a point in 2D.
Window to select a date graphically by using dropdowns.
Definition date_gui.cpp:28
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:45
Point OnInitialPosition(int16_t sm_width, int16_t sm_height, int window_number) override
Compute the initial position of the window.
Definition date_gui.cpp:61
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition date_gui.cpp:142
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
Definition date_gui.cpp:104
void OnDropdownSelect(WidgetID widget, int index) override
A dropdown option associated to this window has been selected.
Definition date_gui.cpp:158
TimerGameEconomy::Year min_year
The minimum year in the year dropdown.
Definition date_gui.cpp:32
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
Definition date_gui.cpp:132
SetDateCallback * callback
Callback to call when a date has been selected.
Definition date_gui.cpp:29
void * callback_data
Callback data pointer.
Definition date_gui.cpp:30
void ShowDateDropDown(WidgetID widget)
Helper function to construct the dropdown.
Definition date_gui.cpp:71
TimerGameEconomy::Year max_year
The maximum year (inclusive) in the year dropdown.
Definition date_gui.cpp:33
TimerGameEconomy::YearMonthDay date
The currently selected date.
Definition date_gui.cpp:31
Templated helper to make a type-safe 'typedef' representing a single POD value.
High level window description.
Definition window_gui.h:168
Number to differentiate different windows of the same class.
Data structure for an opened window.
Definition window_gui.h:274
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1052
Window * parent
Parent window.
Definition window_gui.h:329
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:504
ResizeInfo resize
Resize information.
Definition window_gui.h:315
int left
x position of left edge of the window
Definition window_gui.h:310
int top
y position of top edge of the window
Definition window_gui.h:311
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1749
int height
Height of the window (number of pixels down in y direction)
Definition window_gui.h:313
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:312
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
Definition of the game-economy-timer.
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ NWID_SPACER
Invisible widget that takes some space.
Definition widget_type.h:69
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:65
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:40
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:51
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:67
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:59
@ WWT_DROPDOWN
Drop down list.
Definition widget_type.h:60
@ EqualSize
Containers should keep all their (resizing) children equally large.
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
Definition window.cpp:1155
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ WDP_CENTER
Center the window.
Definition window_gui.h:146
int WidgetID
Widget ID.
Definition window_type.h:20
@ WC_SET_DATE
Set date; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:47