OpenTTD Source 20261112-master-gbcbec5e63f
signs_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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "company_gui.h"
12#include "company_func.h"
13#include "signs_base.h"
14#include "signs_func.h"
15#include "debug.h"
16#include "command_func.h"
17#include "strings_func.h"
18#include "window_func.h"
19#include "map_func.h"
20#include "viewport_func.h"
21#include "querystring_gui.h"
22#include "sortlist_type.h"
23#include "tilehighlight_func.h"
24#include "stringfilter_type.h"
25#include "string_func.h"
27#include "hotkeys.h"
28#include "transparency.h"
29#include "gui.h"
30#include "signs_cmd.h"
31#include "timer/timer.h"
32#include "timer/timer_window.h"
33#include "dropdown_func.h"
34
35#include "widgets/sign_widget.h"
36
37#include "table/strings.h"
38#include "table/sprites.h"
39
40#include "safeguards.h"
41
42struct SignList {
47
48 GUISignList signs;
49
51 static bool match_case;
52 static std::string default_name;
53
60
61 void BuildSignsList()
62 {
63 if (!this->signs.NeedRebuild()) return;
64
65 Debug(Facility::Misc, Severity::Notice, "Building sign list");
66
67 this->signs.clear();
68 this->signs.reserve(Sign::GetNumItems());
69
70 for (const Sign *si : Sign::Iterate()) this->signs.push_back(si);
71
72 this->signs.SetFilterState(true);
73 this->FilterSignList();
74 this->signs.RebuildDone();
75 }
76
78 static bool SignNameSorter(const Sign * const &a, const Sign * const &b)
79 {
80 /* Signs are very very rarely using the default text, but there can also be
81 * a lot of them. Therefore a worthwhile performance gain can be made by
82 * directly comparing Sign::name instead of going through the string
83 * system for each comparison. */
84 const std::string &a_name = a->name.empty() ? SignList::default_name : a->name;
85 const std::string &b_name = b->name.empty() ? SignList::default_name : b->name;
86
87 int r = StrNaturalCompare(a_name, b_name); // Sort by name (natural sorting).
88
89 return r != 0 ? r < 0 : (a->index < b->index);
90 }
91
92 void SortSignsList()
93 {
94 if (!this->signs.Sort(&SignNameSorter)) return;
95 }
96
98 static bool SignNameFilter(const Sign * const *item, StringFilter &filter)
99 {
100 /* Same performance benefit as above for sorting. */
101 const std::string_view name = (*item)->name.empty() ? SignList::default_name : (*item)->name;
102
103 filter.ResetState();
104 filter.AddLine(name);
105 return filter.GetState();
106 }
107
109 static bool OwnerDeityFilter(const Sign * const *item, [[maybe_unused]] StringFilter &filter)
110 {
111 /* You should never be able to edit signs of owner DEITY */
112 return (*item)->owner != OWNER_DEITY;
113 }
114
116 static bool OwnerVisibilityFilter(const Sign * const *item, [[maybe_unused]] StringFilter &filter)
117 {
119 /* Hide sign if non-own signs are hidden in the viewport */
120 return (*item)->owner == _local_company || (*item)->owner == OWNER_DEITY;
121 }
122
125 {
126 this->signs.Filter(&SignNameFilter, this->string_filter);
127 if (_game_mode != GameMode::Editor) this->signs.Filter(&OwnerDeityFilter, this->string_filter);
129 this->signs.Filter(&OwnerVisibilityFilter, this->string_filter);
130 }
131 }
132};
133
134bool SignList::match_case = false;
135std::string SignList::default_name;
136
137struct SignListWindow : Window, SignList {
139 int text_offset = 0;
140 Scrollbar *vscroll = nullptr;
141
143 {
144 this->CreateNestedTree();
145 this->vscroll = this->GetScrollbar(WID_SIL_SCROLLBAR);
146 this->FinishInitNested(window_number);
148
149 /* Initialize the text edit widget */
150 this->querystrings[WID_SIL_FILTER_TEXT] = &this->filter_editbox;
151 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
152
153 /* Initialize the filtering variables */
154 this->SetFilterString("");
155
156 /* Create initial list. */
157 this->signs.ForceRebuild();
158 this->signs.ForceResort();
159 this->BuildSortSignList();
160 }
161
162 void OnInit() override
163 {
164 /* Default sign name, used if Sign::name is nullptr. */
165 SignList::default_name = GetString(STR_DEFAULT_SIGN_NAME);
166 this->signs.ForceResort();
167 this->SortSignsList();
168 this->SetDirty();
169 }
170
178 void SetFilterString(std::string_view new_filter_string)
179 {
180 /* check if there is a new filter string */
181 this->string_filter.SetFilterTerm(new_filter_string);
182
183 /* Rebuild the list of signs */
184 this->InvalidateData();
185 }
186
187 void OnPaint() override
188 {
189 if (!this->IsShaded() && this->signs.NeedRebuild()) this->BuildSortSignList();
190 this->DrawWidgets();
191 }
192
193 void DrawWidget(const Rect &r, WidgetID widget) const override
194 {
195 switch (widget) {
196 case WID_SIL_LIST: {
197 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
198 uint text_offset_y = (this->resize.step_height - GetCharacterHeight(FontSize::Normal) + 1) / 2;
199 /* No signs? */
200 if (this->vscroll->GetCount() == 0) {
201 DrawString(tr.left, tr.right, tr.top + text_offset_y, STR_STATION_LIST_NONE);
202 return;
203 }
204
206 bool rtl = _current_text_dir == TD_RTL;
207 int sprite_offset_y = (this->resize.step_height - d.height + 1) / 2;
208 uint icon_left = rtl ? tr.right - this->text_offset : tr.left;
209 tr = tr.Indent(this->text_offset, rtl);
210
211 /* At least one sign available. */
212 auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->signs);
213 for (auto it = first; it != last; ++it) {
214 const Sign *si = *it;
215
216 if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, tr.top + sprite_offset_y);
217
218 DrawString(tr.left, tr.right, tr.top + text_offset_y, GetString(STR_SIGN_NAME, si->index), TextColour::Yellow);
219 tr.top += this->resize.step_height;
220 }
221 break;
222 }
223 }
224 }
225
226 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
227 {
228 if (widget == WID_SIL_CAPTION) return GetString(STR_SIGN_LIST_CAPTION, this->vscroll->GetCount());
229
230 return this->Window::GetWidgetString(widget, stringid);
231 }
232
233 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
234 {
235 switch (widget) {
236 case WID_SIL_LIST: {
237 auto it = this->vscroll->GetScrolledItemFromWidget(this->signs, pt.y, this, WID_SIL_LIST, WidgetDimensions::scaled.framerect.top);
238 if (it == this->signs.end()) return;
239
240 const Sign *si = *it;
241 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
242 break;
243 }
244
246 SignList::match_case = !SignList::match_case; // Toggle match case
248 this->InvalidateData(); // Rebuild the list of signs
249 break;
250 }
251 }
252
253 void OnResize() override
254 {
255 this->vscroll->SetCapacityFromWidget(this, WID_SIL_LIST, WidgetDimensions::scaled.framerect.Vertical());
256 }
257
258 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
259 {
260 switch (widget) {
261 case WID_SIL_LIST: {
263 this->text_offset = WidgetDimensions::scaled.frametext.left + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
264 fill.height = resize.height = std::max<uint>(GetCharacterHeight(FontSize::Normal), spr_dim.height + 2);
265 Dimension d = {(uint)(this->text_offset + WidgetDimensions::scaled.frametext.right), padding.height + 5 * resize.height};
266 size = maxdim(size, d);
267 break;
268 }
269
270 case WID_SIL_CAPTION:
271 size = GetStringBoundingBox(GetString(STR_SIGN_LIST_CAPTION, GetParamMaxValue(Sign::GetPoolSize(), 3)));
272 size.height += padding.height;
273 size.width += padding.width;
274 break;
275 }
276 }
277
278 void OnEditboxChanged(WidgetID widget) override
279 {
280 if (widget == WID_SIL_FILTER_TEXT) this->SetFilterString(this->filter_editbox.text.GetText());
281 }
282
283 void BuildSortSignList()
284 {
285 if (this->signs.NeedRebuild()) {
286 this->BuildSignsList();
287 this->vscroll->SetCount(this->signs.size());
289 }
290 this->SortSignsList();
291 }
292
294 const IntervalTimer<TimerWindow> rebuild_interval = {std::chrono::seconds(3), [this](auto) {
295 this->BuildSortSignList();
296 this->SetDirty();
297 }};
298
304 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
305 {
306 /* When there is a filter string, we always need to rebuild the list even if
307 * the amount of signs in total is unchanged, as the subset of signs that is
308 * accepted by the filter might has changed. */
309 if (data == 0 || data == -1 || !this->string_filter.IsEmpty()) { // New or deleted sign, changed visibility setting or there is a filter string
310 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
311 this->signs.ForceRebuild();
312 } else { // Change of sign contents while there is no filter string
313 this->signs.ForceResort();
314 }
315 }
316
323 {
324 if (_game_mode == GameMode::Menu) return EventState::NotHandled;
325 Window *w = ShowSignList();
326 if (w == nullptr) return EventState::NotHandled;
327 return w->OnHotkey(hotkey);
328 }
329
330 static inline HotkeyList hotkeys{"signlist", {
331 Hotkey('F', "focus_filter_box", WID_SIL_FILTER_TEXT),
333};
334
335static constexpr std::initializer_list<NWidgetPart> _nested_sign_list_widgets = {
342 EndContainer(),
350 SetStringTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
351 EndContainer(),
352 NWidget(WWT_TEXTBTN, Colours::Brown, WID_SIL_FILTER_MATCH_CASE_BTN), SetStringTip(STR_SIGN_LIST_MATCH_CASE, STR_SIGN_LIST_MATCH_CASE_TOOLTIP),
353 EndContainer(),
354 EndContainer(),
358 EndContainer(),
359 EndContainer(),
360};
361
364 WindowPosition::Automatic, "list_signs", 358, 138,
365 WindowClass::SignList, WindowClass::None,
366 {},
367 _nested_sign_list_widgets,
368 &SignListWindow::hotkeys
369);
370
380
388static bool RenameSign(SignID index, std::string_view text, Colours text_colour)
389{
390 bool remove = text.empty();
391 Command<Commands::RenameSign>::Post(remove ? STR_ERROR_CAN_T_DELETE_SIGN : STR_ERROR_CAN_T_CHANGE_SIGN_NAME, index, std::string{text}, text_colour);
392 return remove;
393}
394
400void MoveSign(SignID index, TileIndex tile)
401{
402 Command<Commands::MoveSign>::Post(STR_ERROR_CAN_T_PLACE_SIGN_HERE, index, tile);
403}
404
405struct SignWindow : Window, SignList {
406 QueryString name_editbox;
407 SignID cur_sign{};
409 std::optional<Colours> new_colour;
410
411 SignWindow(WindowDesc &desc, const Sign *si) : Window(desc), name_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS), cur_sign(si->index)
412 {
413 this->querystrings[WID_QES_TEXT] = &this->name_editbox;
414 this->name_editbox.caption = STR_EDIT_SIGN_CAPTION;
415 this->name_editbox.cancel_button = WID_QES_CANCEL;
416 this->name_editbox.ok_button = WID_QES_OK;
417
419
420 if (_game_mode != GameMode::Editor) {
422 this->ReInit();
423 }
424
425 UpdateSignEditWindow(si);
427 }
428
429 void UpdateSignEditWindow(const Sign *si)
430 {
431 /* Display an empty string when the sign hasn't been edited yet */
432 if (!si->name.empty()) {
433 this->name_editbox.text.Assign(GetString(STR_SIGN_NAME, si->index));
434 } else {
435 this->name_editbox.text.DeleteAll();
436 }
437
438 this->cur_sign = si->index;
439 this->new_colour.reset();
440
444 }
445
451 const Sign *PrevNextSign(bool next)
452 {
453 /* Rebuild the sign list */
454 this->signs.ForceRebuild();
455 this->signs.NeedResort();
456 this->BuildSignsList();
457 this->SortSignsList();
458
459 /* Search through the list for the current sign, excluding
460 * - the first sign if we want the previous sign or
461 * - the last sign if we want the next sign */
462 size_t end = this->signs.size() - (next ? 1 : 0);
463 for (uint i = next ? 0 : 1; i < end; i++) {
464 if (this->cur_sign == this->signs[i]->index) {
465 /* We've found the current sign, so return the sign before/after it */
466 return this->signs[i + (next ? 1 : -1)];
467 }
468 }
469 /* If we haven't found the current sign by now, return the last/first sign */
470 return next ? this->signs.front() : this->signs.back();
471 }
472
473 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
474 {
475 switch (widget) {
476 case WID_QES_CAPTION:
477 return GetString(this->name_editbox.caption);
478
479 case WID_QES_COLOUR:
480 return GetString(STR_COLOUR_DARK_BLUE + to_underlying(this->new_colour.value_or(Sign::Get(this->cur_sign)->text_colour)));
481
482 default:
483 return this->Window::GetWidgetString(widget, stringid);
484 }
485 }
486
487 void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
488 {
489 if (widget == WID_QES_COLOUR) {
490 const Dimension square_size = GetSpriteSize(SPR_SQUARE);
491 const uint string_padding = square_size.width + WidgetDimensions::scaled.hsep_normal + padding.width;
492 for (Colours colour : EnumRange(Colours::End)) {
493 size.width = std::max(size.width, GetStringBoundingBox(STR_COLOUR_DARK_BLUE + to_underlying(colour)).width + string_padding);
494 }
495 size.width = std::max(size.width, GetStringBoundingBox(STR_COLOUR_DEFAULT).width + string_padding);
496 return;
497 }
498
499 Window::UpdateWidgetSize(widget, size, padding, fill, resize);
500 }
501
502 void ShowColourDropDownMenu()
503 {
504 DropDownList list;
505 for (Colours colour : EnumRange(Colours::End)) {
506 list.emplace_back(MakeDropDownListIconItem(SPR_SQUARE, GetColourPalette(colour), STR_COLOUR_DARK_BLUE + to_underlying(colour), colour));
507 }
508 const int selected = to_underlying(this->new_colour.value_or(Sign::Get(this->cur_sign)->text_colour));
509 ShowDropDownList(this, std::move(list), selected, WID_QES_COLOUR);
510 }
511
512 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
513 {
514 switch (widget) {
515 case WID_QES_COLOUR: {
516 ShowColourDropDownMenu();
517 break;
518 }
519
520 case WID_QES_LOCATION: {
521 const Sign *si = Sign::Get(this->cur_sign);
522 TileIndex tile = TileVirtXY(si->x, si->y);
523 if (IsCtrlPressed()) {
525 } else {
527 }
528 break;
529 }
530
531 case WID_QES_PREVIOUS:
532 case WID_QES_NEXT: {
533 const Sign *si = this->PrevNextSign(widget == WID_QES_NEXT);
534
535 /* Rebuild the sign list */
536 this->signs.ForceRebuild();
537 this->signs.NeedResort();
538 this->BuildSignsList();
539 this->SortSignsList();
540
541 /* Scroll to sign and reopen window */
542 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
543 UpdateSignEditWindow(si);
544 break;
545 }
546
547 case WID_QES_OK:
548 if (RenameSign(this->cur_sign, this->name_editbox.text.GetText(), this->new_colour.value_or(Colours::Invalid))) break;
549 [[fallthrough]];
550
551 case WID_QES_CANCEL:
552 this->Close();
553 break;
554
555 case WID_QES_DELETE:
556 /* Only need to set the buffer to null, the rest is handled as the OK button */
557 RenameSign(this->cur_sign, "", Colours::Invalid);
558 /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
559 break;
560
561 case WID_QES_MOVE:
563 this->last_user_action = widget;
564 break;
565 }
566 }
567
568 void OnPlaceObject([[maybe_unused]] Point pt, TileIndex tile) override
569 {
570 switch (this->last_user_action) {
571 case WID_QES_MOVE: // Place sign button
572 RenameSign(this->cur_sign, this->name_editbox.text.GetText(), this->new_colour.value_or(Colours::Invalid));
573 MoveSign(this->cur_sign, tile);
574 this->Close();
575 break;
576
577 default: NOT_REACHED();
578 }
579 }
580
581 void OnPlaceObjectAbort() override
582 {
583 this->RaiseButtons();
584 }
585
586 void OnDropdownSelect(WidgetID widget, int index, int) override
587 {
588 if (widget == WID_QES_COLOUR) this->new_colour = static_cast<Colours>(index);
589 }
590};
591
592static constexpr std::initializer_list<NWidgetPart> _nested_query_sign_edit_widgets = {
596 NWidget(WWT_PUSHIMGBTN, Colours::Grey, WID_QES_LOCATION), SetAspect(WidgetDimensions::ASPECT_LOCATION), SetSpriteTip(SPR_GOTO_LOCATION, STR_EDIT_SIGN_LOCATION_TOOLTIP),
597 EndContainer(),
599 NWidget(WWT_EDITBOX, Colours::Grey, WID_QES_TEXT), SetMinimalSize(256, 0), SetStringTip(STR_EDIT_SIGN_SIGN_OSKTITLE), SetPadding(2, 2, 2, 2),
600 EndContainer(),
604 NWidget(WWT_PUSHTXTBTN, Colours::Grey, WID_QES_DELETE), SetMinimalSize(60, 12), SetStringTip(STR_TOWN_VIEW_DELETE_BUTTON),
607 NWidget(WWT_DROPDOWN, Colours::Grey, WID_QES_COLOUR), SetMinimalSize(60, 12), SetToolTip(STR_EDIT_SIGN_TEXT_COLOUR_TOOLTIP),
608 EndContainer(),
612 EndContainer(),
613};
614
617 WindowPosition::Center, {}, 0, 0,
618 WindowClass::QueryString, WindowClass::None,
620 _nested_query_sign_edit_widgets
621);
622
627void HandleClickOnSign(const Sign *si)
628{
629 /* If we can't edit the sign, don't even open the rename GUI. */
630 if (!CompanyCanEditSign(si)) return;
631
632 if (IsCtrlPressed() && (si->owner == _local_company || (si->owner == OWNER_DEITY && _game_mode == GameMode::Editor))) {
633 RenameSign(si->index, "", Colours::Invalid);
634 return;
635 }
636
638}
639
645{
646 /* Delete all other edit windows */
647 CloseWindowByClass(WindowClass::QueryString);
648
650}
651
657{
658 SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WindowClass::QueryString, QueryStringWindowNumber::Sign));
659
660 if (w != nullptr && w->cur_sign == sign) w->Close();
661}
Iterate a range of enum values.
List template of 'things' T to sort in a GUI.
bool Filter(FilterFunction *decide, F filter_data)
Filter the list.
void RebuildDone()
Notify the sortlist that the rebuild is done.
void SetFilterState(bool state)
Enable or disable the filter.
bool NeedRebuild() const
Check if a rebuild is needed.
void ForceRebuild()
Force that a rebuild is needed.
bool Sort(Comp compare)
Sort the list.
void ForceResort()
Force a resort next Sort call Reset the resort timer if used too.
bool NeedResort()
Check if a resort is needed next loop If used the resort timer will decrease every call till 0.
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
Scrollbar data structure.
void SetCount(size_t num)
Sets the number of elements in the list.
auto GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Return an iterator pointing to the element of a scrolled widget that a user clicked in.
void SetCapacityFromWidget(Window *w, WidgetID widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget.
Definition widget.cpp:2548
size_type GetCount() const
Gets the number of elements in the list.
auto GetVisibleRangeIterators(Tcontainer &container) const
Get a pair of iterators for the range of visible elements in a container.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:94
Functions related to commands.
void DrawCompanyIcon(CompanyID c, int x, int y)
Draw the icon of a company.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Functions related to companies.
GUI Functions related to companies.
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
static constexpr Owner OWNER_NONE
The tile has no ownership.
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Misc
Misc message facility.
Definition debug_type.h:32
@ Notice
Notice.
Definition debug_type.h:18
std::unique_ptr< DropDownListItem > MakeDropDownListIconItem(SpriteID sprite, PaletteID palette, StringID str, int value, bool masked, bool shaded)
Creates new DropDownListIconItem.
Definition dropdown.cpp:70
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, DropDownOptions options, std::string *const persistent_filter_text)
Show a drop down list.
Definition dropdown.cpp:591
Functions 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.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:88
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition gfx.cpp:966
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:894
int DrawString(int left, int right, int top, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:663
bool IsCtrlPressed()
Test if the ctrl key is pressed.
Definition gfx_func.h:82
@ Normal
Index of the normal font in the font tables.
Definition gfx_type.h:249
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:283
@ Invalid
Invalid marker.
Definition gfx_type.h:302
@ End
End-of-array marker.
Definition gfx_type.h:301
@ Grey
Grey.
Definition gfx_type.h:299
@ Brown
Brown.
Definition gfx_type.h:298
@ White
White colour.
Definition gfx_type.h:330
@ Yellow
Yellow colour.
Definition gfx_type.h:326
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetSpriteTip(SpriteID sprite, StringID tip={})
Widget part function for setting the sprite and tooltip.
constexpr NWidgetPart SetScrollbar(WidgetID index)
Attach a scrollbar to a widget.
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetAspect(float ratio, AspectFlags flags=AspectFlag::ResizeX)
Widget part function for setting the aspect ratio.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
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,...
constexpr NWidgetPart SetTextStyle(TextColour colour, FontSize size=FontSize::Normal)
Widget part function for setting the text style.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetArrowWidgetTypeTip(ArrowWidgetType widget_type, StringID tip={})
Widget part function for setting the arrow widget type and tooltip.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
static const CursorID SPR_CURSOR_SIGN
Definition sprites.h:1574
void SetDirty() const
Mark entire window as dirty (in need of re-paint).
Definition window.cpp:1013
GUI functions that shouldn't be here.
void ShowExtraViewportWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
Hotkey related functions.
#define Point
Macro that prevents name conflicts between included headers.
bool HandlePlacePushButton(Window *w, WidgetID widget, CursorID cursor, HighLightStyle mode)
This code is shared for the majority of the pushbuttons.
Definition main_gui.cpp:58
Functions related to maps.
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition map_func.h:407
@ ShowCompetitorSigns
Display signs, station names and waypoint names of opponent companies. Buoys and oilrig-stations are ...
Definition openttd.h:52
@ Editor
In the scenario editor.
Definition openttd.h:21
@ Menu
In the main menu.
Definition openttd.h:19
Base for the GUIs that have an edit box in them.
A number of safeguards to prevent using unsafe methods.
Types related to the sign widgets.
@ WID_QES_PREVIOUS
Previous button.
Definition sign_widget.h:34
@ WID_QES_MOVE
Move Sign button.
Definition sign_widget.h:33
@ WID_QES_LOCATION
Scroll to sign location.
Definition sign_widget.h:26
@ WID_QES_CANCEL
Cancel button.
Definition sign_widget.h:29
@ WID_QES_OK
OK button.
Definition sign_widget.h:28
@ WID_QES_COLOUR_PANE
Pane to show/hide the color dropdown.
Definition sign_widget.h:31
@ WID_QES_COLOUR
Colour selection dropdown.
Definition sign_widget.h:32
@ WID_QES_NEXT
Next button.
Definition sign_widget.h:35
@ WID_QES_DELETE
Delete button.
Definition sign_widget.h:30
@ WID_QES_TEXT
Text of the query.
Definition sign_widget.h:27
@ WID_QES_CAPTION
Caption of the window.
Definition sign_widget.h:25
@ WID_SIL_SCROLLBAR
Scrollbar of list.
Definition sign_widget.h:18
@ WID_SIL_FILTER_MATCH_CASE_BTN
Button to toggle if case sensitive filtering should be used.
Definition sign_widget.h:20
@ WID_SIL_FILTER_TEXT
Text box for typing a filter string.
Definition sign_widget.h:19
@ WID_SIL_CAPTION
Caption of the window.
Definition sign_widget.h:16
@ WID_SIL_LIST
List of signs.
Definition sign_widget.h:17
bool CompanyCanEditSign(const Sign *si)
Check if the current company can rename or move a given sign.
Definition signs.cpp:62
Base class for signs.
Command definitions related to signs.
Functions related to signs.
void HandleClickOnSign(const Sign *si)
Handle clicking on a sign.
void DeleteRenameSignWindow(SignID sign)
Close the sign window associated with the given sign.
static WindowDesc _query_sign_edit_desc(WindowPosition::Center, {}, 0, 0, WindowClass::QueryString, WindowClass::None, WindowDefaultFlag::Construction, _nested_query_sign_edit_widgets)
Window definition for the sign editor window.
void MoveSign(SignID index, TileIndex tile)
Actually move the sign.
static bool RenameSign(SignID index, std::string_view text, Colours text_colour)
Actually rename the sign.
static WindowDesc _sign_list_desc(WindowPosition::Automatic, "list_signs", 358, 138, WindowClass::SignList, WindowClass::None, {}, _nested_sign_list_widgets, &SignListWindow::hotkeys)
Window definition for the sign list window.
void ShowRenameSignWindow(const Sign *si)
Show the window to change the text of a sign.
Window * ShowSignList()
Open the sign list window.
PoolID< uint16_t, struct SignIDTag, 64000, 0xFFFF > SignID
The type of the IDs of signs.
Definition signs_type.h:16
static const uint MAX_LENGTH_SIGN_NAME_CHARS
The maximum length of a sign name in characters including '\0'.
Definition signs_type.h:20
Base types for having sorted lists in GUIs.
static PaletteID GetColourPalette(Colours colour)
Get recolour palette for a colour.
Definition sprite.h:221
This file contains all sprite-related enums and defines.
static const SpriteID SPR_COMPANY_ICON
Icon showing company colour.
Definition sprites.h:386
static const SpriteID SPR_SQUARE
Coloured square (used for newgrf compatibility).
Definition sprites.h:71
Definition of base types and functions in a cross-platform compatible way.
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition string.cpp:428
Functions related to low-level strings.
Searching and filtering using a stringterm.
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:235
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:423
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:55
Functions related to OTTD's strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Dimensions (a width and height) of a rectangle in 2D.
value_type height
The height of the rectangle.
value_type width
The width of the rectangle.
List of hotkeys for a window.
Definition hotkeys.h:44
All data for a single hotkey.
Definition hotkeys.h:20
static Pool::IterateWrapper< Sign > Iterate(size_t from=0)
static Sign * Get(auto index)
Data stored about a string that can be modified in the GUI.
int ok_button
Widget button of parent window to simulate when pressing OK in OSK.
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
static const int ACTION_CLEAR
Clear editbox.
Specification of a rectangle with absolute coordinates of all edges.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
const IntervalTimer< TimerWindow > rebuild_interval
Resort the sign listing on a regular interval.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
void OnEditboxChanged(WidgetID widget) override
The text in an editbox has been edited.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
int text_offset
Offset of the sign text relative to the left edge of the WID_SIL_LIST widget.
QueryString filter_editbox
Filter editbox;.
void OnPaint() override
The window must be repainted.
void OnInit() override
Notification that the nested widget tree gets initialized.
static EventState SignListGlobalHotkeys(int hotkey)
Handler for global hotkeys of the SignListWindow.
void SetFilterString(std::string_view new_filter_string)
This function sets the filter string of the sign list.
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.
void OnResize() override
Called after the window got resized.
GUIList< const Sign *, std::nullptr_t, StringFilter & > GUISignList
A GUIList contains signs and uses a StringFilter for filtering.
Definition signs_gui.cpp:46
static bool SignNameFilter(const Sign *const *item, StringFilter &filter)
Filter sign list by sign name.
Definition signs_gui.cpp:98
static bool SignNameSorter(const Sign *const &a, const Sign *const &b)
Sort signs by their name.
Definition signs_gui.cpp:78
SignList()
Creates a SignList with filtering disabled by default.
Definition signs_gui.cpp:57
void FilterSignList()
Filter out signs from the sign list that does not match the name filter.
static bool match_case
Should case sensitive matching be used?
Definition signs_gui.cpp:51
static bool OwnerVisibilityFilter(const Sign *const *item, StringFilter &filter)
Filter sign list by owner.
static std::string default_name
Default sign name, used if Sign::name is nullptr.
Definition signs_gui.cpp:52
StringFilter string_filter
The match string to be used when the GUIList is (re)-sorted.
Definition signs_gui.cpp:50
static bool OwnerDeityFilter(const Sign *const *item, StringFilter &filter)
Filter sign list excluding OWNER_DEITY.
void OnPlaceObject(Point pt, TileIndex tile) override
The user clicked some place on the map when a tile highlight mode has been set.
std::optional< Colours > new_colour
New colour selected by the user. Will be assigned when the OK button is clicked.
void OnPlaceObjectAbort() override
The user cancelled a tile highlight mode that has been set.
void OnDropdownSelect(WidgetID widget, int index, int) override
A dropdown option associated to this window has been selected.
const Sign * PrevNextSign(bool next)
Returns a pointer to the (alphabetically) previous or next sign of the current sign.
WidgetID last_user_action
Last started user action.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a 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.
Owner owner
Placed by this company. Anyone can delete them though. OWNER_NONE for gray signs from old games.
Definition signs_base.h:28
String filter and state.
bool IsEmpty() const
Check whether any filter words were entered.
void SetFilterTerm(std::string_view str)
Set the term to filter on.
void ResetState()
Reset the matching state to process a new item.
bool GetState() const
Get the matching state of the current item.
std::string_view GetText() const
Get the current text.
Definition textbuf.cpp:283
void Assign(std::string_view text)
Copy a string into the textbuffer.
Definition textbuf.cpp:419
High level window description.
Definition window_gui.h:171
Number to differentiate different windows of the same class.
Data structure for an opened window.
Definition window_gui.h:272
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
Definition window.cpp:1025
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1150
virtual void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize)
Update size and resize step of a widget in the window.
Definition window_gui.h:622
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1855
std::map< WidgetID, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
Definition window_gui.h:319
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:792
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing).
Definition window.cpp:3317
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
Definition window.cpp:565
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:513
ResizeInfo resize
Resize information.
Definition window_gui.h:313
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1845
bool SetFocusedWidget(WidgetID widget_index)
Set focus within this window to the given widget.
Definition window.cpp:494
void RaiseButtons(bool autoraise=false)
Raise the buttons of the window.
Definition window.cpp:539
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:440
bool IsShaded() const
Is window shaded currently?
Definition window_gui.h:561
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition window.cpp:1879
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1869
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:322
virtual EventState OnHotkey(int hotkey)
A hotkey has been pressed.
Definition window.cpp:579
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:310
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:301
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
Functions related to tile highlights.
@ HT_RECT
rectangle (stations, depots, ...)
Definition of Interval and OneShot timers.
Definition of the Window system.
Functions related to transparency.
DisplayOptions _display_opt
What do we want to draw/do?
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Functions related to (drawing on) viewports.
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
@ WWT_PUSHARROWBTN
Normal push-button (no toggle button) with arrow caption.
@ WWT_EDITBOX
a textbox for typing
Definition widget_type.h:62
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
@ WWT_TEXTBTN
(Toggle) Button with text
Definition widget_type.h:44
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX).
Definition widget_type.h:57
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX).
Definition widget_type.h:55
@ WWT_CAPTION
Window caption (window title between closebox and stickybox).
Definition widget_type.h:52
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:76
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:68
@ WWT_CLOSEBOX
Close box (at top-left of a window).
Definition widget_type.h:60
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window).
Definition widget_type.h:59
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX).
Definition widget_type.h:56
@ WWT_DROPDOWN
Drop down list.
Definition widget_type.h:61
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition widget_type.h:71
@ SZSP_VERTICAL
Display plane with zero size horizontally, and filling and resizing vertically.
@ Decrease
Arrow to the left or in case of RTL to the right.
Definition widget_type.h:20
@ Increase
Arrow to the right or in case of RTL to the left.
Definition widget_type.h:21
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
Definition window.cpp:1255
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1199
Window functions not directly related to making/drawing windows.
@ Construction
This window is used for construction; close it whenever changing company.
Definition window_gui.h:154
Twindow * AllocateWindowDescFront(WindowDesc &desc, WindowNumber window_number, Targs... extra_arguments)
Open a new window.
@ Automatic
Find a place automatically.
Definition window_gui.h:145
@ Center
Center the window.
Definition window_gui.h:146
int WidgetID
Widget ID.
Definition window_type.h:21
EventState
State of handling an event.
@ NotHandled
The passed event is not handled.
static constexpr WidgetID INVALID_WIDGET
An invalid widget index.
Definition window_type.h:24
@ Sign
Query string for signs.
Definition window_type.h:38