Change: Recolour graph windows to brown (#8700)
[openttd-github.git] / src / signs_gui.cpp
blobea55488cc710f393134595ecf072fb4da204e999
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 */
8 /** @file signs_gui.cpp The GUI for signs. */
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 "stringfilter_type.h"
24 #include "string_func.h"
25 #include "core/geometry_func.hpp"
26 #include "hotkeys.h"
27 #include "transparency.h"
28 #include "gui.h"
30 #include "widgets/sign_widget.h"
32 #include "table/strings.h"
33 #include "table/sprites.h"
35 #include "safeguards.h"
37 struct SignList {
38 /**
39 * A GUIList contains signs and uses a StringFilter for filtering.
41 typedef GUIList<const Sign *, StringFilter &> GUISignList;
43 GUISignList signs;
45 StringFilter string_filter; ///< The match string to be used when the GUIList is (re)-sorted.
46 static bool match_case; ///< Should case sensitive matching be used?
47 static char default_name[64]; ///< Default sign name, used if Sign::name is nullptr.
49 /**
50 * Creates a SignList with filtering disabled by default.
52 SignList() : string_filter(&match_case)
56 void BuildSignsList()
58 if (!this->signs.NeedRebuild()) return;
60 DEBUG(misc, 3, "Building sign list");
62 this->signs.clear();
64 for (const Sign *si : Sign::Iterate()) this->signs.push_back(si);
66 this->signs.SetFilterState(true);
67 this->FilterSignList();
68 this->signs.shrink_to_fit();
69 this->signs.RebuildDone();
72 /** Sort signs by their name */
73 static bool SignNameSorter(const Sign * const &a, const Sign * const &b)
75 /* Signs are very very rarely using the default text, but there can also be
76 * a lot of them. Therefore a worthwhile performance gain can be made by
77 * directly comparing Sign::name instead of going through the string
78 * system for each comparison. */
79 const char *a_name = a->name.empty() ? SignList::default_name : a->name.c_str();
80 const char *b_name = b->name.empty() ? SignList::default_name : b->name.c_str();
82 int r = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
84 return r != 0 ? r < 0 : (a->index < b->index);
87 void SortSignsList()
89 if (!this->signs.Sort(&SignNameSorter)) return;
92 /** Filter sign list by sign name */
93 static bool CDECL SignNameFilter(const Sign * const *a, StringFilter &filter)
95 /* Same performance benefit as above for sorting. */
96 const char *a_name = (*a)->name.empty() ? SignList::default_name : (*a)->name.c_str();
98 filter.ResetState();
99 filter.AddLine(a_name);
100 return filter.GetState();
103 /** Filter sign list excluding OWNER_DEITY */
104 static bool CDECL OwnerDeityFilter(const Sign * const *a, StringFilter &filter)
106 /* You should never be able to edit signs of owner DEITY */
107 return (*a)->owner != OWNER_DEITY;
110 /** Filter sign list by owner */
111 static bool CDECL OwnerVisibilityFilter(const Sign * const *a, StringFilter &filter)
113 assert(!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS));
114 /* Hide sign if non-own signs are hidden in the viewport */
115 return (*a)->owner == _local_company || (*a)->owner == OWNER_DEITY;
118 /** Filter out signs from the sign list that does not match the name filter */
119 void FilterSignList()
121 this->signs.Filter(&SignNameFilter, this->string_filter);
122 if (_game_mode != GM_EDITOR) this->signs.Filter(&OwnerDeityFilter, this->string_filter);
123 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS)) {
124 this->signs.Filter(&OwnerVisibilityFilter, this->string_filter);
129 bool SignList::match_case = false;
130 char SignList::default_name[64];
132 /** Enum referring to the Hotkeys in the sign list window */
133 enum SignListHotkeys {
134 SLHK_FOCUS_FILTER_BOX, ///< Focus the edit box for editing the filter string
137 struct SignListWindow : Window, SignList {
138 QueryString filter_editbox; ///< Filter editbox;
139 int text_offset; ///< Offset of the sign text relative to the left edge of the WID_SIL_LIST widget.
140 Scrollbar *vscroll;
142 SignListWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc), filter_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
144 this->CreateNestedTree();
145 this->vscroll = this->GetScrollbar(WID_SIL_SCROLLBAR);
146 this->FinishInitNested(window_number);
147 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case);
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;
153 /* Initialize the filtering variables */
154 this->SetFilterString("");
156 /* Create initial list. */
157 this->signs.ForceRebuild();
158 this->signs.ForceResort();
159 this->BuildSortSignList();
162 void OnInit() override
164 /* Default sign name, used if Sign::name is nullptr. */
165 GetString(SignList::default_name, STR_DEFAULT_SIGN_NAME, lastof(SignList::default_name));
166 this->signs.ForceResort();
167 this->SortSignsList();
168 this->SetDirty();
172 * This function sets the filter string of the sign list. The contents of
173 * the edit widget is not updated by this function. Depending on if the
174 * new string is zero-length or not the clear button is made
175 * disabled/enabled. The sign list is updated according to the new filter.
177 void SetFilterString(const char *new_filter_string)
179 /* check if there is a new filter string */
180 this->string_filter.SetFilterTerm(new_filter_string);
182 /* Rebuild the list of signs */
183 this->InvalidateData();
186 void OnPaint() override
188 if (!this->IsShaded() && this->signs.NeedRebuild()) this->BuildSortSignList();
189 this->DrawWidgets();
192 void DrawWidget(const Rect &r, int widget) const override
194 switch (widget) {
195 case WID_SIL_LIST: {
196 uint y = r.top + WD_FRAMERECT_TOP; // Offset from top of widget.
197 /* No signs? */
198 if (this->vscroll->GetCount() == 0) {
199 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_STATION_LIST_NONE);
200 return;
203 bool rtl = _current_text_dir == TD_RTL;
204 int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
205 uint icon_left = 4 + (rtl ? r.right - this->text_offset : r.left);
206 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
207 uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
209 /* At least one sign available. */
210 for (uint16 i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
211 const Sign *si = this->signs[i];
213 if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, y + sprite_offset_y);
215 SetDParam(0, si->index);
216 DrawString(text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
217 y += this->resize.step_height;
219 break;
224 void SetStringParameters(int widget) const override
226 if (widget == WID_SIL_CAPTION) SetDParam(0, this->vscroll->GetCount());
229 void OnClick(Point pt, int widget, int click_count) override
231 switch (widget) {
232 case WID_SIL_LIST: {
233 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SIL_LIST, WD_FRAMERECT_TOP);
234 if (id_v == INT_MAX) return;
236 const Sign *si = this->signs[id_v];
237 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
238 break;
241 case WID_SIL_FILTER_ENTER_BTN:
242 if (this->signs.size() >= 1) {
243 const Sign *si = this->signs[0];
244 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
246 break;
248 case WID_SIL_FILTER_MATCH_CASE_BTN:
249 SignList::match_case = !SignList::match_case; // Toggle match case
250 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case); // Toggle button pushed state
251 this->InvalidateData(); // Rebuild the list of signs
252 break;
256 void OnResize() override
258 this->vscroll->SetCapacityFromWidget(this, WID_SIL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
261 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
263 switch (widget) {
264 case WID_SIL_LIST: {
265 Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
266 this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
267 resize->height = std::max<uint>(FONT_HEIGHT_NORMAL, spr_dim.height);
268 Dimension d = {(uint)(this->text_offset + WD_FRAMETEXT_RIGHT), WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
269 *size = maxdim(*size, d);
270 break;
273 case WID_SIL_CAPTION:
274 SetDParamMaxValue(0, Sign::GetPoolSize(), 3);
275 *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
276 size->height += padding.height;
277 size->width += padding.width;
278 break;
282 EventState OnHotkey(int hotkey) override
284 switch (hotkey) {
285 case SLHK_FOCUS_FILTER_BOX:
286 this->SetFocusedWidget(WID_SIL_FILTER_TEXT);
287 SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
288 break;
290 default:
291 return ES_NOT_HANDLED;
294 return ES_HANDLED;
297 void OnEditboxChanged(int widget) override
299 if (widget == WID_SIL_FILTER_TEXT) this->SetFilterString(this->filter_editbox.text.buf);
302 void BuildSortSignList()
304 if (this->signs.NeedRebuild()) {
305 this->BuildSignsList();
306 this->vscroll->SetCount((uint)this->signs.size());
307 this->SetWidgetDirty(WID_SIL_CAPTION);
309 this->SortSignsList();
312 void OnHundredthTick() override
314 this->BuildSortSignList();
315 this->SetDirty();
319 * Some data on this window has become invalid.
320 * @param data Information about the changed data.
321 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
323 void OnInvalidateData(int data = 0, bool gui_scope = true) override
325 /* When there is a filter string, we always need to rebuild the list even if
326 * the amount of signs in total is unchanged, as the subset of signs that is
327 * accepted by the filter might has changed. */
328 if (data == 0 || data == -1 || !this->string_filter.IsEmpty()) { // New or deleted sign, changed visibility setting or there is a filter string
329 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
330 this->signs.ForceRebuild();
331 } else { // Change of sign contents while there is no filter string
332 this->signs.ForceResort();
336 static HotkeyList hotkeys;
340 * Handler for global hotkeys of the SignListWindow.
341 * @param hotkey Hotkey
342 * @return ES_HANDLED if hotkey was accepted.
344 static EventState SignListGlobalHotkeys(int hotkey)
346 if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
347 Window *w = ShowSignList();
348 if (w == nullptr) return ES_NOT_HANDLED;
349 return w->OnHotkey(hotkey);
352 static Hotkey signlist_hotkeys[] = {
353 Hotkey('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX),
354 HOTKEY_LIST_END
356 HotkeyList SignListWindow::hotkeys("signlist", signlist_hotkeys, SignListGlobalHotkeys);
358 static const NWidgetPart _nested_sign_list_widgets[] = {
359 NWidget(NWID_HORIZONTAL),
360 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
361 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SIL_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
362 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
363 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
364 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
365 EndContainer(),
366 NWidget(NWID_HORIZONTAL),
367 NWidget(NWID_VERTICAL),
368 NWidget(WWT_PANEL, COLOUR_BROWN, WID_SIL_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + 255 + WD_FRAMETEXT_RIGHT, 50),
369 SetResize(1, 10), SetFill(1, 0), SetScrollbar(WID_SIL_SCROLLBAR), EndContainer(),
370 NWidget(NWID_HORIZONTAL),
371 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 1),
372 NWidget(WWT_EDITBOX, COLOUR_BROWN, WID_SIL_FILTER_TEXT), SetMinimalSize(80, 12), SetResize(1, 0), SetFill(1, 0), SetPadding(2, 2, 2, 2),
373 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
374 EndContainer(),
375 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SIL_FILTER_MATCH_CASE_BTN), SetDataTip(STR_SIGN_LIST_MATCH_CASE, STR_SIGN_LIST_MATCH_CASE_TOOLTIP),
376 EndContainer(),
377 EndContainer(),
378 NWidget(NWID_VERTICAL),
379 NWidget(NWID_VERTICAL), SetFill(0, 1),
380 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_SIL_SCROLLBAR),
381 EndContainer(),
382 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
383 EndContainer(),
384 EndContainer(),
387 static WindowDesc _sign_list_desc(
388 WDP_AUTO, "list_signs", 358, 138,
389 WC_SIGN_LIST, WC_NONE,
391 _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets),
392 &SignListWindow::hotkeys
396 * Open the sign list window
398 * @return newly opened sign list window, or nullptr if the window could not be opened.
400 Window *ShowSignList()
402 return AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
406 * Actually rename the sign.
407 * @param index the sign to rename.
408 * @param text the new name.
409 * @return true if the window will already be removed after returning.
411 static bool RenameSign(SignID index, const char *text)
413 bool remove = StrEmpty(text);
414 DoCommandP(0, index, 0, CMD_RENAME_SIGN | (StrEmpty(text) ? CMD_MSG(STR_ERROR_CAN_T_DELETE_SIGN) : CMD_MSG(STR_ERROR_CAN_T_CHANGE_SIGN_NAME)), nullptr, text);
415 return remove;
418 struct SignWindow : Window, SignList {
419 QueryString name_editbox;
420 SignID cur_sign;
422 SignWindow(WindowDesc *desc, const Sign *si) : Window(desc), name_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
424 this->querystrings[WID_QES_TEXT] = &this->name_editbox;
425 this->name_editbox.caption = STR_EDIT_SIGN_CAPTION;
426 this->name_editbox.cancel_button = WID_QES_CANCEL;
427 this->name_editbox.ok_button = WID_QES_OK;
429 this->InitNested(WN_QUERY_STRING_SIGN);
431 UpdateSignEditWindow(si);
432 this->SetFocusedWidget(WID_QES_TEXT);
435 void UpdateSignEditWindow(const Sign *si)
437 /* Display an empty string when the sign hasn't been edited yet */
438 if (!si->name.empty()) {
439 SetDParam(0, si->index);
440 this->name_editbox.text.Assign(STR_SIGN_NAME);
441 } else {
442 this->name_editbox.text.DeleteAll();
445 this->cur_sign = si->index;
447 this->SetWidgetDirty(WID_QES_TEXT);
448 this->SetFocusedWidget(WID_QES_TEXT);
452 * Returns a pointer to the (alphabetically) previous or next sign of the current sign.
453 * @param next false if the previous sign is wanted, true if the next sign is wanted
454 * @return pointer to the previous/next sign
456 const Sign *PrevNextSign(bool next)
458 /* Rebuild the sign list */
459 this->signs.ForceRebuild();
460 this->signs.NeedResort();
461 this->BuildSignsList();
462 this->SortSignsList();
464 /* Search through the list for the current sign, excluding
465 * - the first sign if we want the previous sign or
466 * - the last sign if we want the next sign */
467 size_t end = this->signs.size() - (next ? 1 : 0);
468 for (uint i = next ? 0 : 1; i < end; i++) {
469 if (this->cur_sign == this->signs[i]->index) {
470 /* We've found the current sign, so return the sign before/after it */
471 return this->signs[i + (next ? 1 : -1)];
474 /* If we haven't found the current sign by now, return the last/first sign */
475 return next ? this->signs.front() : this->signs.back();
478 void SetStringParameters(int widget) const override
480 switch (widget) {
481 case WID_QES_CAPTION:
482 SetDParam(0, this->name_editbox.caption);
483 break;
487 void OnClick(Point pt, int widget, int click_count) override
489 switch (widget) {
490 case WID_QES_LOCATION: {
491 const Sign *si = Sign::Get(this->cur_sign);
492 TileIndex tile = TileVirtXY(si->x, si->y);
493 if (_ctrl_pressed) {
494 ShowExtraViewportWindow(tile);
495 } else {
496 ScrollMainWindowToTile(tile);
498 break;
501 case WID_QES_PREVIOUS:
502 case WID_QES_NEXT: {
503 const Sign *si = this->PrevNextSign(widget == WID_QES_NEXT);
505 /* Rebuild the sign list */
506 this->signs.ForceRebuild();
507 this->signs.NeedResort();
508 this->BuildSignsList();
509 this->SortSignsList();
511 /* Scroll to sign and reopen window */
512 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
513 UpdateSignEditWindow(si);
514 break;
517 case WID_QES_DELETE:
518 /* Only need to set the buffer to null, the rest is handled as the OK button */
519 RenameSign(this->cur_sign, "");
520 /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
521 break;
523 case WID_QES_OK:
524 if (RenameSign(this->cur_sign, this->name_editbox.text.buf)) break;
525 FALLTHROUGH;
527 case WID_QES_CANCEL:
528 delete this;
529 break;
534 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
535 NWidget(NWID_HORIZONTAL),
536 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
537 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QES_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
538 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_QES_LOCATION), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_EDIT_SIGN_LOCATION_TOOLTIP),
539 EndContainer(),
540 NWidget(WWT_PANEL, COLOUR_GREY),
541 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QES_TEXT), SetMinimalSize(256, 12), SetDataTip(STR_EDIT_SIGN_SIGN_OSKTITLE, STR_NULL), SetPadding(2, 2, 2, 2),
542 EndContainer(),
543 NWidget(NWID_HORIZONTAL),
544 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
545 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
546 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
547 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
548 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
549 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
550 EndContainer(),
553 static WindowDesc _query_sign_edit_desc(
554 WDP_CENTER, "query_sign", 0, 0,
555 WC_QUERY_STRING, WC_NONE,
556 WDF_CONSTRUCTION,
557 _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets)
561 * Handle clicking on a sign.
562 * @param si The sign that was clicked on.
564 void HandleClickOnSign(const Sign *si)
566 if (_ctrl_pressed && (si->owner == _local_company || (si->owner == OWNER_DEITY && _game_mode == GM_EDITOR))) {
567 RenameSign(si->index, nullptr);
568 return;
570 ShowRenameSignWindow(si);
574 * Show the window to change the text of a sign.
575 * @param si The sign to show the window for.
577 void ShowRenameSignWindow(const Sign *si)
579 /* Delete all other edit windows */
580 DeleteWindowByClass(WC_QUERY_STRING);
582 new SignWindow(&_query_sign_edit_desc, si);
586 * Close the sign window associated with the given sign.
587 * @param sign The sign to close the window for.
589 void DeleteRenameSignWindow(SignID sign)
591 SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WC_QUERY_STRING, WN_QUERY_STRING_SIGN));
593 if (w != nullptr && w->cur_sign == sign) delete w;