23#include "table/strings.h"
29std::unique_ptr<DropDownListItem> MakeDropDownListDividerItem()
31 return std::make_unique<DropDownListDividerItem>(-1);
34std::unique_ptr<DropDownListItem> MakeDropDownListStringItem(
StringID str,
int value,
bool masked,
bool shaded)
36 return MakeDropDownListStringItem(
GetString(str), value, masked, shaded);
39std::unique_ptr<DropDownListItem> MakeDropDownListStringItem(std::string &&str,
int value,
bool masked,
bool shaded)
41 return std::make_unique<DropDownListStringItem>(std::move(str), value, masked, shaded);
44std::unique_ptr<DropDownListItem> MakeDropDownListIconItem(
SpriteID sprite,
PaletteID palette,
StringID str,
int value,
bool masked,
bool shaded)
46 return std::make_unique<DropDownListIconItem>(sprite, palette,
GetString(str), value, masked, shaded);
51 return std::make_unique<DropDownListIconItem>(dim, sprite, palette,
GetString(str), value, masked, shaded);
54std::unique_ptr<DropDownListItem> MakeDropDownListCheckedItem(
bool checked,
StringID str,
int value,
bool masked,
bool shaded, uint indent)
56 return std::make_unique<DropDownListCheckedItem>(indent, checked,
GetString(str), value, masked, shaded);
59static constexpr NWidgetPart _nested_dropdown_menu_widgets[] = {
72 _nested_dropdown_menu_widgets
83 bool drag_mode =
true;
112 assert(!this->
list.empty());
118 this->GetWidget<NWidgetCore>(
WID_DM_ITEMS)->colour = wi_colour;
119 this->GetWidget<NWidgetCore>(
WID_DM_SCROLL)->colour = wi_colour;
127 void Close([[maybe_unused]]
int data = 0)
override
127 void Close([[maybe_unused]]
int data = 0)
override {
…}
146 this->instant_close =
false;
159 if (desired.height < available_height)
return;
162 uint avg_height =
list.height / (uint)this->list.size();
165 desired.width = std::max(
list.width, desired.width - NWidgetScrollbar::GetVerticalDimension().width);
185 widget_dim.width = std::max<uint>(widget_dim.width, button_rect.
Width());
188 uint available_height_below = std::max(
GetMainViewBottom() - button_rect.bottom - 1, 0);
189 uint available_height_above = std::max(button_rect.top - 1 -
GetMainViewTop(), 0);
192 if (widget_dim.height > available_height_below && available_height_above > available_height_below) {
194 this->
position.
y = button_rect.top - widget_dim.height;
197 this->
position.
y = button_rect.bottom + 1;
202 this->
position.
x = button_rect.right + 1 - (int)(widget_dim.width + (list_dim.height > widget_dim.height ? NWidgetScrollbar::GetVerticalDimension().width : 0));
215 if (this->
position.
y < button_rect.top && list_dim.height > widget_dim.height) this->vscroll->
UpdatePosition(INT_MAX);
239 int y = _cursor.
pos.
y - this->
top - r.top;
242 for (
const auto &item : this->
list) {
244 if (--pos >= 0)
continue;
246 int item_height = item->Height();
248 if (y < item_height) {
249 if (item->masked || !item->Selectable())
return false;
250 result = item->result;
251 click_result = item->OnClick({r.left, 0, r.right, item_height - 1}, {_cursor.
pos.
x - this->
left, y});
265 Colours colour = this->GetWidget<NWidgetCore>(widget)->colour;
270 for (
const auto &item : this->
list) {
271 int item_height = item->Height();
274 if (--pos >= 0)
continue;
276 if (y + item_height - 1 <= ir.bottom) {
277 Rect full{ir.left, y, ir.right, y + item_height - 1};
279 bool selected = (this->selected_result == item->result) && item->Selectable();
291 int result, click_result;
293 this->click_delay = 4;
294 this->selected_result = result;
295 this->selected_click_result = click_result;
302 if (this->scrolling == 0)
return;
311 if (this->click_delay != 0 && --this->click_delay == 0) {
314 if (!this->persist) this->
Close();
319 if (this->drag_mode) {
320 int result, click_result;
323 this->drag_mode =
false;
325 if (this->instant_close) this->
Close();
328 this->click_delay = 2;
332 this->scrolling = -1;
343 if (this->selected_result != result || this->selected_click_result != click_result) {
344 this->selected_result = result;
345 this->selected_click_result = click_result;
363void ReplaceDropDownList(
Window *parent,
DropDownList &&list, std::optional<int> selected_result)
366 if (ddw !=
nullptr) ddw->ReplaceList(std::move(list), selected_result);
377 for (
const auto &item : list) {
378 dim.height += item->Height();
379 dim.width = std::max(dim.width, item->Width());
400 new DropdownWindow(w, std::move(list), selected, button, wi_rect, instant_close, wi_colour, persist);
423 Rect wi_rect = nwi->GetCurrentRect();
424 Colours wi_colour = nwi->
colour;
441 ShowDropDownListAt(w, std::move(list), selected, button, wi_rect, wi_colour, instant_close, persist);
460 for (
auto string : strings) {
461 if (!
HasBit(hidden_mask, i)) {
462 list.push_back(MakeDropDownListStringItem(
string, i,
HasBit(disabled_mask, i)));
467 if (!list.empty())
ShowDropDownList(w, std::move(list), selected, button, width);
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
An interval timer will fire every interval, and will continue to fire until it is deleted.
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.
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, bool instant_close, bool persist)
Show a drop down list.
Dimension GetDropDownListDimension(const DropDownList &list)
Determine width and height required to fully display a DropDownList.
void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, WidgetID button, Rect wi_rect, Colours wi_colour, bool instant_close, bool persist)
Show a drop down list.
Common drop down list components.
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.
bool _left_button_clicked
Is left mouse button clicked?
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.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
uint32_t PaletteID
The number of the palette.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
static constexpr PixelColour PC_BLACK
Black palette colour.
A number of safeguards to prevent using unsafe methods.
void SndClickBeep()
Play a beep sound for a click event if enabled in settings.
Functions related to sound.
Definition of base types and functions in a cross-platform compatible way.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
TextDirection _current_text_dir
Text direction of the currently selected language.
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
Point pos
logical mouse position
Dimensions (a width and height) of a rectangle in 2D.
Point OnInitialPosition(int16_t sm_width, int16_t sm_height, int window_number) override
Compute the initial position of the window.
WidgetID parent_button
Parent widget number where the window is dropped from.
void OnMouseLoop() override
Called for every mouse loop run, which is at least once per (game) tick.
bool persist
Persist dropdown menu.
DropdownWindow(Window *parent, DropDownList &&list, int selected, WidgetID button, const Rect wi_rect, bool instant_close, Colours wi_colour, bool persist)
Create a dropdown menu.
void UpdateSizeAndPosition()
Update size and position of window to fit dropdown list into available space.
const IntervalTimer< TimerWindow > scroll_interval
Rate limit how fast scrolling happens.
Rect wi_rect
Rect of the button that opened the dropdown.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
bool instant_close
Close the window when the mouse button is raised.
Dimension items_dim
Calculated cropped and padded dimension for the items widget.
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.
uint8_t click_delay
Timer to delay selection.
int selected_result
Result value of the selected item in the list.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
DropDownList list
List with dropdown menu items.
int selected_click_result
Click result value, from the OnClick handler of the selected item.
void FitAvailableHeight(Dimension &desired, const Dimension &list, uint available_height)
Fit dropdown list into available height, rounding to average item size.
bool GetDropDownItem(int &result, int &click_result)
Find the dropdown item under the cursor.
Point position
Position of the topleft corner of the window.
int scrolling
If non-zero, auto-scroll the item list (one time).
void OnFocusLost(bool closing) override
The window has lost focus.
constexpr uint Horizontal() const
Get total horizontal padding of RectPadding.
constexpr uint Vertical() const
Get total vertical padding of RectPadding.
Specification of a rectangle with absolute coordinates of all edges.
int Width() const
Get width of Rect.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
High level window description.
int16_t GetDefaultWidth() const
Determine default width of window.
Data structure for an opened window.
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
void InitializePositionSize(int x, int y, int min_width, int min_height)
Set the position and smallest size of the window.
Window * parent
Parent window.
virtual void OnDropdownSelect(WidgetID widget, int index, int click_result)
A dropdown option associated to this window has been selected.
ResizeInfo resize
Resize information.
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
WindowDesc & window_desc
Window description.
virtual void FindWindowPlacementAndResize(int def_width, int def_height, bool allow_resize)
Resize window towards the default size.
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.
int left
x position of left edge of the window
Window * FindChildWindow(WindowClass wc=WC_INVALID) const
Find the Window whose parent pointer points to this window.
int top
y position of top edge of the window
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
WindowFlags flags
Window flags.
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
WindowNumber window_number
Window number within the window class.
Definition of Interval and OneShot timers.
Definition of the Window system.
int GetMainViewTop()
Return the top of the main view available for general use.
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
int GetMainViewBottom()
Return the bottom of the main view available for general use.
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ NoFocus
This window won't get focus/make any other window lose focus when click.
@ WhiteBorder
Window white border counter bit mask.
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
@ WC_DROPDOWN_MENU
Drop down menu; Window numbers:
Functions related to zooming.