OpenTTD Source: src/dropdown.cpp Source File

OpenTTD Source 15.0-RC2

dropdown.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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "core/backup_type.hpp"
12#include "dropdown_type.h"
13#include "dropdown_func.h"
14#include "strings_func.h"
15#include "sound_func.h"
16#include "timer/timer.h"
17#include "timer/timer_window.h"
18#include "window_gui.h"
19#include "window_func.h"
20#include "zoom_func.h"
21
22#include "widgets/dropdown_widget.h"
23
24#include "table/strings.h"
25
26#include "dropdown_common_type.h"
27
28#include "safeguards.h"
29
30std::unique_ptr<DropDownListItem> MakeDropDownListDividerItem()
31{
32 return std::make_unique<DropDownListDividerItem>(-1);
33}
34
35std::unique_ptr<DropDownListItem> MakeDropDownListStringItem(StringID str, int value, bool masked, bool shaded)
36{
37 return MakeDropDownListStringItem(GetString(str), value, masked, shaded);
38}
39
40std::unique_ptr<DropDownListItem> MakeDropDownListStringItem(std::string &&str, int value, bool masked, bool shaded)
41{
42 return std::make_unique<DropDownListStringItem>(std::move(str), value, masked, shaded);
43}
44
45std::unique_ptr<DropDownListItem> MakeDropDownListIconItem(SpriteID sprite, PaletteID palette, StringID str, int value, bool masked, bool shaded)
46{
47 return std::make_unique<DropDownListIconItem>(sprite, palette, GetString(str), value, masked, shaded);
48}
49
50std::unique_ptr<DropDownListItem> MakeDropDownListIconItem(const Dimension &dim, SpriteID sprite, PaletteID palette, StringID str, int value, bool masked, bool shaded)
51{
52 return std::make_unique<DropDownListIconItem>(dim, sprite, palette, GetString(str), value, masked, shaded);
53}
54
55std::unique_ptr<DropDownListItem> MakeDropDownListCheckedItem(bool checked, StringID str, int value, bool masked, bool shaded, uint indent)
56{
57 return std::make_unique<DropDownListCheckedItem>(indent, checked, GetString(str), value, masked, shaded);
58}
59
60static constexpr std::initializer_list<NWidgetPart> _nested_dropdown_menu_widgets = {
61 NWidget(NWID_HORIZONTAL),
62 NWidget(WWT_PANEL, COLOUR_END, WID_DM_ITEMS), SetScrollbar(WID_DM_SCROLL), EndContainer(),
63 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_DM_SHOW_SCROLL),
64 NWidget(NWID_VSCROLLBAR, COLOUR_END, WID_DM_SCROLL),
65 EndContainer(),
66 EndContainer(),
67};
68
69static WindowDesc _dropdown_desc(
70 WDP_MANUAL, {}, 0, 0,
71 WC_DROPDOWN_MENU, WC_NONE,
72 WindowDefaultFlag::NoFocus,
73 _nested_dropdown_menu_widgets
74);
75
77 struct DropdownWindow : Window {
78 WidgetID parent_button{};
79 Rect wi_rect{};
80 DropDownList list{};
81 int selected_result = 0;
82 int selected_click_result = -1;
83 uint8_t click_delay = 0;
84 bool drag_mode = true;
85 DropDownOptions options;
86 int scrolling = 0;
87 Point position{};
88 Scrollbar *vscroll = nullptr;
89
90 Dimension items_dim{};
91
102 DropdownWindow(Window *parent, DropDownList &&list, int selected, WidgetID button, const Rect wi_rect, Colours wi_colour, DropDownOptions options)
103 : Window(_dropdown_desc)
104 , parent_button(button)
105 , wi_rect(wi_rect)
106 , list(std::move(list))
107 , selected_result(selected)
108 , options(options)
109 {
110 assert(!this->list.empty());
111
112 this->parent = parent;
113
114 this->CreateNestedTree();
115
116 this->GetWidget<NWidgetCore>(WID_DM_ITEMS)->colour = wi_colour;
117 this->GetWidget<NWidgetCore>(WID_DM_SCROLL)->colour = wi_colour;
118 this->vscroll = this->GetScrollbar(WID_DM_SCROLL);
119 this->UpdateSizeAndPosition();
120
121 this->FinishInitNested(0);
122 this->flags.Reset(WindowFlag::WhiteBorder);
123 }
124
125 void Close([[maybe_unused]] int data = 0) override
126 {
127 /* Finish closing the dropdown, so it doesn't affect new window placement.
128 * Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
129 this->Window::Close();
130
131 Point pt = _cursor.pos;
132 pt.x -= this->parent->left;
133 pt.y -= this->parent->top;
134 this->parent->OnDropdownClose(pt, this->parent_button, this->selected_result, this->selected_click_result, this->options.Test(DropDownOption::InstantClose));
135
136 /* Set flag on parent widget to indicate that we have just closed. */
137 NWidgetCore *nwc = this->parent->GetWidget<NWidgetCore>(this->parent_button);
138 if (nwc != nullptr) nwc->disp_flags.Set(NWidgetDisplayFlag::DropdownClosed);
139 }
140
141 void OnFocusLost(bool closing) override
142 {
143 if (!closing) {
144 this->options.Reset(DropDownOption::InstantClose);
145 this->Close();
146 }
147 }
148
155 void FitAvailableHeight(Dimension &desired, const Dimension &list, uint available_height)
156 {
157 if (desired.height < available_height) return;
158
159 /* If the dropdown doesn't fully fit, we a need a dropdown. */
160 uint avg_height = list.height / (uint)this->list.size();
161 uint rows = std::max((available_height - WidgetDimensions::scaled.dropdownlist.Vertical()) / avg_height, 1U);
162
163 desired.width = std::max(list.width, desired.width - NWidgetScrollbar::GetVerticalDimension().width);
164 desired.height = rows * avg_height + WidgetDimensions::scaled.dropdownlist.Vertical();
165 }
166
170 void UpdateSizeAndPosition()
171 {
172 Rect button_rect = this->wi_rect.Translate(this->parent->left, this->parent->top);
173
174 /* Get the dimensions required for the list. */
175 Dimension list_dim = GetDropDownListDimension(this->list);
176
177 /* Set up dimensions for the items widget. */
178 Dimension widget_dim = list_dim;
179 widget_dim.width += WidgetDimensions::scaled.dropdownlist.Horizontal();
180 widget_dim.height += WidgetDimensions::scaled.dropdownlist.Vertical();
181
182 /* Width should match at least the width of the parent widget. */
183 widget_dim.width = std::max<uint>(widget_dim.width, button_rect.Width());
184
185 /* Available height below (or above, if the dropdown is placed above the widget). */
186 uint available_height_below = std::max(GetMainViewBottom() - button_rect.bottom - 1, 0);
187 uint available_height_above = std::max(button_rect.top - 1 - GetMainViewTop(), 0);
188
189 /* Is it better to place the dropdown above the widget? */
190 if (widget_dim.height > available_height_below && available_height_above > available_height_below) {
191 FitAvailableHeight(widget_dim, list_dim, available_height_above);
192 this->position.y = button_rect.top - widget_dim.height;
193 } else {
194 FitAvailableHeight(widget_dim, list_dim, available_height_below);
195 this->position.y = button_rect.bottom + 1;
196 }
197
198 if (_current_text_dir == TD_RTL) {
199 /* In case the list is wider than the parent button, the list should be right aligned to the button and overflow to the left. */
200 this->position.x = button_rect.right + 1 - (int)(widget_dim.width + (list_dim.height > widget_dim.height ? NWidgetScrollbar::GetVerticalDimension().width : 0));
201 } else {
202 this->position.x = button_rect.left;
203 }
204
205 this->items_dim = widget_dim;
206 this->GetWidget<NWidgetStacked>(WID_DM_SHOW_SCROLL)->SetDisplayedPlane(list_dim.height > widget_dim.height ? 0 : SZSP_NONE);
207
208 /* Capacity is the average number of items visible */
209 this->vscroll->SetCapacity(widget_dim.height - WidgetDimensions::scaled.dropdownlist.Vertical());
210 this->vscroll->SetStepSize(list_dim.height / this->list.size());
211 this->vscroll->SetCount(list_dim.height);
212
213 /* If the dropdown is positioned above the parent widget, start selection at the bottom. */
214 if (this->position.y < button_rect.top && list_dim.height > widget_dim.height) this->vscroll->UpdatePosition(INT_MAX);
215 }
216
217 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
218 {
219 if (widget == WID_DM_ITEMS) size = this->items_dim;
220 }
221
222 Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
223 {
224 return this->position;
225 }
226
233 bool GetDropDownItem(int &result, int &click_result)
234 {
235 if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false;
236
237 const Rect &r = this->GetWidget<NWidgetBase>(WID_DM_ITEMS)->GetCurrentRect().Shrink(WidgetDimensions::scaled.dropdownlist).Shrink(WidgetDimensions::scaled.dropdowntext, RectPadding::zero);
238 int click_y = _cursor.pos.y - this->top - r.top;
239 int y = -this->vscroll->GetPosition();
240 int y_end = r.Height();
241
242 for (const auto &item : this->list) {
243 int item_height = item->Height();
244
245 /* Skip items that are scrolled up */
246 if (y > y_end) break;
247 if (click_y >= y && click_y < y + item_height) {
248 if (item->masked || !item->Selectable()) return false;
249 result = item->result;
250 click_result = item->OnClick(r.WithY(0, item_height - 1), {_cursor.pos.x - this->left, click_y - y});
251 return true;
252 }
253 y += item_height;
254 }
255
256 return false;
257 }
258
259 void DrawWidget(const Rect &r, WidgetID widget) const override
260 {
261 if (widget != WID_DM_ITEMS) return;
262
263 Colours colour = this->GetWidget<NWidgetCore>(widget)->colour;
264
265 Rect ir = r.Shrink(WidgetDimensions::scaled.dropdownlist);
266
267 /* Setup a clipping rectangle... */
268 DrawPixelInfo tmp_dpi;
269 if (!FillDrawPixelInfo(&tmp_dpi, ir)) return;
270 /* ...but keep coordinates relative to the window. */
271 tmp_dpi.left += ir.left;
272 tmp_dpi.top += ir.top;
273 AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
274
275 int y = -this->vscroll->GetPosition();
276 int y_end = ir.Height();
277
278 for (const auto &item : this->list) {
279 int item_height = item->Height();
280
281 /* Skip items that are scrolled up */
282 if (y > y_end) break;
283 if (y > -item_height) {
284 Rect full = ir.Translate(0, y).WithHeight(item_height);
285
286 bool selected = (this->selected_result == item->result) && item->Selectable();
287 if (selected) GfxFillRect(full, PC_BLACK);
288
289 item->Draw(full, full.Shrink(WidgetDimensions::scaled.dropdowntext, RectPadding::zero), selected, selected ? this->selected_click_result : -1, colour);
290 }
291 y += item_height;
292 }
293 }
294
295 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
296 {
297 if (widget != WID_DM_ITEMS) return;
298 int result, click_result;
299 if (this->GetDropDownItem(result, click_result)) {
300 this->click_delay = 4;
301 this->selected_result = result;
302 this->selected_click_result = click_result;
303 this->SetDirty();
304 }
305 }
306
308 const IntervalTimer<TimerWindow> scroll_interval = {std::chrono::milliseconds(30), [this](auto) {
309 if (this->scrolling == 0) return;
310
311 if (this->vscroll->UpdatePosition(this->scrolling)) this->SetDirty();
312
313 this->scrolling = 0;
314 }};
315
316 void OnMouseLoop() override
317 {
318 if (this->click_delay != 0 && --this->click_delay == 0) {
319 /* Close the dropdown, so it doesn't affect new window placement.
320 * Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
321 if (!this->options.Test(DropDownOption::Persist)) this->Close();
322 this->parent->OnDropdownSelect(this->parent_button, this->selected_result, this->selected_click_result);
323 return;
324 }
325
326 if (this->drag_mode) {
327 int result, click_result;
328
329 if (!_left_button_clicked) {
330 this->drag_mode = false;
331 if (!this->GetDropDownItem(result, click_result)) {
332 if (this->options.Test(DropDownOption::InstantClose)) this->Close();
333 return;
334 }
335 this->click_delay = 2;
336 } else {
337 if (_cursor.pos.y <= this->top + WidgetDimensions::scaled.dropdownlist.top) {
338 /* Cursor is above the list, set scroll up */
339 this->scrolling = -1;
340 return;
341 } else if (_cursor.pos.y >= this->top + this->height - WidgetDimensions::scaled.dropdownlist.bottom) {
342 /* Cursor is below list, set scroll down */
343 this->scrolling = 1;
344 return;
345 }
346
347 if (!this->GetDropDownItem(result, click_result)) return;
348 }
349
350 if (this->selected_result != result || this->selected_click_result != click_result) {
351 this->selected_result = result;
352 this->selected_click_result = click_result;
353 this->SetDirty();
354 }
355 }
356 }
357
358 void ReplaceList(DropDownList &&list, std::optional<int> selected_result)
359 {
360 this->list = std::move(list);
361 if (selected_result.has_value()) this->selected_result = *selected_result;
362 this->UpdateSizeAndPosition();
363 this->ReInit(0, 0);
364 this->InitializePositionSize(this->position.x, this->position.y, this->nested_root->smallest_x, this->nested_root->smallest_y);
365 this->FindWindowPlacementAndResize(this->window_desc.GetDefaultWidth(), this->window_desc.GetDefaultHeight(), true);
366 this->SetDirty();
367 }
368};
369
370void ReplaceDropDownList(Window *parent, DropDownList &&list, std::optional<int> selected_result)
371{
372 DropdownWindow *ddw = dynamic_cast<DropdownWindow *>(parent->FindChildWindow(WC_DROPDOWN_MENU));
373 if (ddw != nullptr) ddw->ReplaceList(std::move(list), selected_result);
374}
375
381 Dimension GetDropDownListDimension(const DropDownList &list)
382{
383 Dimension dim{};
384 for (const auto &item : list) {
385 dim.height += item->Height();
386 dim.width = std::max(dim.width, item->Width());
387 }
388 dim.width += WidgetDimensions::scaled.dropdowntext.Horizontal();
389 return dim;
390}
391
402 void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, WidgetID button, Rect wi_rect, Colours wi_colour, DropDownOptions options)
403{
404 CloseWindowByClass(WC_DROPDOWN_MENU);
405 new DropdownWindow(w, std::move(list), selected, button, wi_rect, wi_colour, options);
406}
407
418 void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, DropDownOptions options)
419{
420 /* Handle the beep of the player's click. */
421 SndClickBeep();
422
423 /* Our parent's button widget is used to determine where to place the drop
424 * down list window. */
425 NWidgetCore *nwi = w->GetWidget<NWidgetCore>(button);
426 Rect wi_rect = nwi->GetCurrentRect();
427 Colours wi_colour = nwi->colour;
428
429 if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
430 nwi->disp_flags.Set(NWidgetDisplayFlag::DropdownActive);
431 } else {
432 nwi->SetLowered(true);
433 }
434 nwi->SetDirty(w);
435
436 if (width != 0) {
437 if (_current_text_dir == TD_RTL) {
438 wi_rect.left = wi_rect.right + 1 - ScaleGUITrad(width);
439 } else {
440 wi_rect.right = wi_rect.left + ScaleGUITrad(width) - 1;
441 }
442 }
443
444 ShowDropDownListAt(w, std::move(list), selected, button, wi_rect, wi_colour, options);
445}
446
458 void ShowDropDownMenu(Window *w, std::span<const StringID> strings, int selected, WidgetID button, uint32_t disabled_mask, uint32_t hidden_mask, uint width)
459{
460 DropDownList list;
461
462 uint i = 0;
463 for (auto string : strings) {
464 if (!HasBit(hidden_mask, i)) {
465 list.push_back(MakeDropDownListStringItem(string, i, HasBit(disabled_mask, i)));
466 }
467 ++i;
468 }
469
470 if (!list.empty()) ShowDropDownList(w, std::move(list), selected, button, width, {});
471}
backup_type.hpp
Class for backupping variables and making sure they are restored later.
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Reset
constexpr Timpl & Reset()
Reset all bits.
Definition base_bitset_type.hpp:83
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
EnumBitSet< DropDownOption, uint8_t >
IntervalTimer
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
NWidgetBase::SetDirty
virtual void SetDirty(const Window *w) const
Mark the widget as 'dirty' (in need of repaint).
Definition widget.cpp:931
NWidgetBase::type
WidgetType type
Type of the widget / nested widget.
Definition widget_type.h:226
NWidgetCore
Base class for a 'real' widget.
Definition widget_type.h:368
NWidgetCore::disp_flags
NWidgetDisplayFlags disp_flags
Flags that affect display and interaction with the widget.
Definition widget_type.h:399
NWidgetCore::colour
Colours colour
Colour of this widget.
Definition widget_type.h:400
NWidgetCore::SetLowered
void SetLowered(bool lowered)
Lower or raise the widget.
Definition widget_type.h:440
Scrollbar
Scrollbar data structure.
Definition widget_type.h:708
Scrollbar::SetCount
void SetCount(size_t num)
Sets the number of elements in the list.
Definition widget_type.h:792
Scrollbar::UpdatePosition
bool UpdatePosition(int difference, ScrollbarStepping unit=SS_SMALL)
Updates the position of the first visible element by the given amount.
Definition widget_type.h:836
Scrollbar::SetCapacity
void SetCapacity(size_t capacity)
Set the capacity of visible elements.
Definition widget_type.h:806
Scrollbar::SetStepSize
void SetStepSize(size_t stepsize)
Set the distance to scroll when using the buttons or the wheel.
Definition widget_type.h:780
Scrollbar::GetPosition
size_type GetPosition() const
Gets the position of the first visible element in the list.
Definition widget_type.h:752
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
WidgetDimensions::dropdownlist
RectPadding dropdownlist
Padding of complete drop down list.
Definition window_gui.h:51
WidgetDimensions::dropdowntext
RectPadding dropdowntext
Padding of drop down list item.
Definition window_gui.h:50
ShowDropDownMenu
void ShowDropDownMenu(Window *w, std::span< const StringID > strings, int selected, WidgetID button, uint32_t disabled_mask, uint32_t hidden_mask, uint width)
Show a dropdown menu window near a widget of the parent window.
Definition dropdown.cpp:458
ShowDropDownListAt
void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, WidgetID button, Rect wi_rect, Colours wi_colour, DropDownOptions options)
Show a drop down list.
Definition dropdown.cpp:402
GetDropDownListDimension
Dimension GetDropDownListDimension(const DropDownList &list)
Determine width and height required to fully display a DropDownList.
Definition dropdown.cpp:381
ShowDropDownList
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, DropDownOptions options)
Show a drop down list.
Definition dropdown.cpp:418
dropdown_common_type.h
Common drop down list components.
dropdown_func.h
Functions related to the drop down widget.
dropdown_type.h
Types related to the drop down widget.
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:56
DropDownOption::InstantClose
@ InstantClose
Set if releasing mouse button should close the list regardless of where the cursor is.
DropDownOption::Persist
@ Persist
Set if this dropdown should stay open after an option is selected.
dropdown_widget.h
Types related to the dropdown widgets.
WID_DM_ITEMS
@ WID_DM_ITEMS
Panel showing the dropdown items.
Definition dropdown_widget.h:15
WID_DM_SCROLL
@ WID_DM_SCROLL
Scrollbar.
Definition dropdown_widget.h:17
WID_DM_SHOW_SCROLL
@ WID_DM_SHOW_SCROLL
Hide scrollbar if too few items.
Definition dropdown_widget.h:16
_left_button_clicked
bool _left_button_clicked
Is left mouse button clicked?
Definition gfx.cpp:42
GfxFillRect
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:115
FillDrawPixelInfo
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
Definition gfx.cpp:1565
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
SetScrollbar
constexpr NWidgetPart SetScrollbar(WidgetID index)
Attach a scrollbar to a widget.
Definition widget_type.h:1399
EndContainer
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition widget_type.h:1229
NWidget
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
Definition widget_type.h:1424
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:966
PC_BLACK
static constexpr PixelColour PC_BLACK
Black palette colour.
Definition palette_func.h:96
safeguards.h
A number of safeguards to prevent using unsafe methods.
SndClickBeep
void SndClickBeep()
Play a beep sound for a click event if enabled in settings.
Definition sound.cpp:253
sound_func.h
Functions related to sound.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
strings_func.h
Functions related to OTTD's strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition strings_type.h:27
AutoRestoreBackup
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
Definition backup_type.hpp:150
Coord2D< int >
Coord2D::y
T y
Y coordinate.
Definition geometry_type.hpp:35
Coord2D::x
T x
X coordinate.
Definition geometry_type.hpp:34
CursorVars::pos
Point pos
logical mouse position
Definition gfx_type.h:125
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
DrawPixelInfo
Data about how and where to blit pixels.
Definition gfx_type.h:157
DropdownWindow
Drop-down menu window.
Definition dropdown.cpp:77
DropdownWindow::OnInitialPosition
Point OnInitialPosition(int16_t sm_width, int16_t sm_height, int window_number) override
Compute the initial position of the window.
Definition dropdown.cpp:222
DropdownWindow::parent_button
WidgetID parent_button
Parent widget number where the window is dropped from.
Definition dropdown.cpp:78
DropdownWindow::OnMouseLoop
void OnMouseLoop() override
Called for every mouse loop run, which is at least once per (game) tick.
Definition dropdown.cpp:316
DropdownWindow::DropdownWindow
DropdownWindow(Window *parent, DropDownList &&list, int selected, WidgetID button, const Rect wi_rect, Colours wi_colour, DropDownOptions options)
Create a dropdown menu.
Definition dropdown.cpp:102
DropdownWindow::UpdateSizeAndPosition
void UpdateSizeAndPosition()
Update size and position of window to fit dropdown list into available space.
Definition dropdown.cpp:170
DropdownWindow::scroll_interval
const IntervalTimer< TimerWindow > scroll_interval
Rate limit how fast scrolling happens.
Definition dropdown.cpp:308
DropdownWindow::wi_rect
Rect wi_rect
Rect of the button that opened the dropdown.
Definition dropdown.cpp:79
DropdownWindow::OnClick
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition dropdown.cpp:295
DropdownWindow::items_dim
Dimension items_dim
Calculated cropped and padded dimension for the items widget.
Definition dropdown.cpp:90
DropdownWindow::UpdateWidgetSize
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 dropdown.cpp:217
DropdownWindow::click_delay
uint8_t click_delay
Timer to delay selection.
Definition dropdown.cpp:83
DropdownWindow::selected_result
int selected_result
Result value of the selected item in the list.
Definition dropdown.cpp:81
DropdownWindow::Close
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
Definition dropdown.cpp:125
DropdownWindow::DrawWidget
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
Definition dropdown.cpp:259
DropdownWindow::options
DropDownOptions options
Options for this drop down menu.
Definition dropdown.cpp:85
DropdownWindow::list
DropDownList list
List with dropdown menu items.
Definition dropdown.cpp:80
DropdownWindow::selected_click_result
int selected_click_result
Click result value, from the OnClick handler of the selected item.
Definition dropdown.cpp:82
DropdownWindow::FitAvailableHeight
void FitAvailableHeight(Dimension &desired, const Dimension &list, uint available_height)
Fit dropdown list into available height, rounding to average item size.
Definition dropdown.cpp:155
DropdownWindow::GetDropDownItem
bool GetDropDownItem(int &result, int &click_result)
Find the dropdown item under the cursor.
Definition dropdown.cpp:233
DropdownWindow::position
Point position
Position of the topleft corner of the window.
Definition dropdown.cpp:87
DropdownWindow::scrolling
int scrolling
If non-zero, auto-scroll the item list (one time).
Definition dropdown.cpp:86
DropdownWindow::OnFocusLost
void OnFocusLost(bool closing) override
The window has lost focus.
Definition dropdown.cpp:141
RectPadding::Horizontal
constexpr uint Horizontal() const
Get total horizontal padding of RectPadding.
Definition geometry_type.hpp:89
RectPadding::Vertical
constexpr uint Vertical() const
Get total vertical padding of RectPadding.
Definition geometry_type.hpp:95
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
Rect::Width
int Width() const
Get width of Rect.
Definition geometry_type.hpp:111
Rect::Shrink
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Definition geometry_type.hpp:124
Rect::WithHeight
Rect WithHeight(int height, bool end=false) const
Copy Rect and set its height.
Definition geometry_type.hpp:237
Rect::Height
int Height() const
Get height of Rect.
Definition geometry_type.hpp:117
Rect::WithY
Rect WithY(int new_top, int new_bottom) const
Create a new Rect, replacing the top and bottom coordiates.
Definition geometry_type.hpp:280
Rect::Translate
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
Definition geometry_type.hpp:200
WindowDesc
High level window description.
Definition window_gui.h:168
WindowDesc::GetDefaultWidth
int16_t GetDefaultWidth() const
Determine default width of window.
Definition window.cpp:137
Window
Data structure for an opened window.
Definition window_gui.h:274
Window::ReInit
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
Definition window.cpp:978
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:1102
Window::FinishInitNested
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1807
Window::InitializePositionSize
void InitializePositionSize(int x, int y, int min_width, int min_height)
Set the position and smallest size of the window.
Definition window.cpp:1455
Window::parent
Window * parent
Parent window.
Definition window_gui.h:329
Window::OnDropdownSelect
virtual void OnDropdownSelect(WidgetID widget, int index, int click_result)
A dropdown option associated to this window has been selected.
Definition window_gui.h:770
Window::resize
ResizeInfo resize
Resize information.
Definition window_gui.h:315
Window::CreateNestedTree
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1797
Window::window_desc
WindowDesc & window_desc
Window description.
Definition window_gui.h:300
Window::FindWindowPlacementAndResize
virtual void FindWindowPlacementAndResize(int def_width, int def_height, bool allow_resize)
Resize window towards the default size.
Definition window.cpp:1474
Window::OnDropdownClose
virtual void OnDropdownClose(Point pt, WidgetID widget, int index, int click_result, bool instant_close)
A dropdown window associated to this window has been closed.
Definition window.cpp:286
Window::left
int left
x position of left edge of the window
Definition window_gui.h:310
Window::FindChildWindow
Window * FindChildWindow(WindowClass wc=WC_INVALID) const
Find the Window whose parent pointer points to this window.
Definition window.cpp:1047
Window::top
int top
y position of top edge of the window
Definition window_gui.h:311
Window::GetWidget
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:983
Window::flags
WindowFlags flags
Window flags.
Definition window_gui.h:301
Window::GetScrollbar
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:313
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
timer.h
Definition of Interval and OneShot timers.
timer_window.h
Definition of the Window system.
GetWidgetFromPos
WidgetID GetWidgetFromPos(const Window *w, int x, int y)
Returns the index for the widget located at the given position relative to the window.
Definition widget.cpp:274
ScaleGUITrad
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
NWID_BUTTON_DROPDOWN
@ NWID_BUTTON_DROPDOWN
Button with a drop-down.
Definition widget_type.h:74
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
NWID_VSCROLLBAR
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:76
NWID_SELECTION
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition widget_type.h:71
DropdownClosed
@ DropdownClosed
Dropdown menu of the dropdown widget has closed.
Definition widget_type.h:350
DropdownActive
@ DropdownActive
Dropdown menu of the button dropdown widget is active.
Definition widget_type.h:342
SZSP_NONE
@ SZSP_NONE
Display plane with zero size in both directions (none filling and resizing).
Definition widget_type.h:506
GetMainViewTop
int GetMainViewTop()
Return the top of the main view available for general use.
Definition window.cpp:2136
CloseWindowByClass
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
Definition window.cpp:1205
GetMainViewBottom
int GetMainViewBottom()
Return the bottom of the main view available for general use.
Definition window.cpp:2147
window_func.h
Window functions not directly related to making/drawing windows.
window_gui.h
Functions, definitions and such used only by the GUI.
WindowDefaultFlag::NoFocus
@ NoFocus
This window won't get focus/make any other window lose focus when click.
WindowFlag::WhiteBorder
@ WhiteBorder
Window white border counter bit mask.
WDP_MANUAL
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
Definition window_gui.h:143
WidgetID
int WidgetID
Widget ID.
Definition window_type.h:20
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:50
WC_DROPDOWN_MENU
@ WC_DROPDOWN_MENU
Drop down menu; Window numbers:
Definition window_type.h:161
zoom_func.h
Functions related to zooming.