OpenTTD Source 20250312-master-gcdcc6b491d
picker_gui.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef PICKER_GUI_H
11#define PICKER_GUI_H
12
13#include "newgrf_badge.h"
14#include "querystring_gui.h"
15#include "sortlist_type.h"
16#include "stringfilter_type.h"
17#include "strings_type.h"
18#include "timer/timer.h"
20#include "timer/timer_window.h"
21#include "window_gui.h"
22#include "window_type.h"
23
24struct PickerItem {
25 uint32_t grfid;
26 uint16_t local_id;
27 int class_index;
28 int index;
29
30 inline auto operator<=>(const PickerItem &other) const
31 {
32 if (auto cmp = this->grfid <=> other.grfid; cmp != 0) return cmp;
33 return this->local_id <=> other.local_id;
34 }
35};
36
39public:
40 explicit PickerCallbacks(const std::string &ini_group);
41 virtual ~PickerCallbacks();
42
43 virtual void Close(int) { }
44
45 virtual GrfSpecFeature GetFeature() const = 0;
47 virtual bool IsActive() const = 0;
49 virtual bool HasClassChoice() const = 0;
50
51 /* Class callbacks */
53 virtual StringID GetClassTooltip() const = 0;
55 virtual int GetClassCount() const = 0;
57 virtual int GetSelectedClass() const = 0;
59 virtual void SetSelectedClass(int id) const = 0;
61 virtual StringID GetClassName(int id) const = 0;
62
63 /* Type callbacks */
65 virtual StringID GetTypeTooltip() const = 0;
67 virtual int GetTypeCount(int cls_id) const = 0;
68
70 virtual int GetSelectedType() const = 0;
72 virtual void SetSelectedType(int id) const = 0;
74 virtual PickerItem GetPickerItem(int cls_id, int id) const = 0;
76 virtual StringID GetTypeName(int cls_id, int id) const = 0;
78 virtual std::span<const BadgeID> GetTypeBadges(int cls_id, int id) const = 0;
80 virtual bool IsTypeAvailable(int cls_id, int id) const = 0;
82 virtual void DrawType(int x, int y, int cls_id, int id) const = 0;
83
85 virtual void FillUsedItems(std::set<PickerItem> &items) = 0;
87 virtual std::set<PickerItem> UpdateSavedItems(const std::set<PickerItem> &src) = 0;
88
89 Listing class_last_sorting = { false, 0 };
91
92 Listing type_last_sorting = { false, 0 };
94
95 const std::string ini_group;
96 uint8_t mode = 0;
97
98 std::set<PickerItem> used;
99 std::set<PickerItem> saved;
100};
101
103template <typename T>
105public:
106 explicit PickerCallbacksNewGRFClass(const std::string &ini_group) : PickerCallbacks(ini_group) {}
107
108 inline typename T::index_type GetClassIndex(int cls_id) const { return static_cast<typename T::index_type>(cls_id); }
109 inline const T *GetClass(int cls_id) const { return T::Get(this->GetClassIndex(cls_id)); }
110 inline const typename T::spec_type *GetSpec(int cls_id, int id) const { return this->GetClass(cls_id)->GetSpec(id); }
111
112 bool HasClassChoice() const override { return T::GetUIClassCount() > 1; }
113
114 int GetClassCount() const override { return T::GetClassCount(); }
115 int GetTypeCount(int cls_id) const override { return this->GetClass(cls_id)->GetSpecCount(); }
116
117 PickerItem GetPickerItem(const typename T::spec_type *spec, int cls_id = -1, int id = -1) const
118 {
119 if (spec == nullptr) return {0, 0, cls_id, id};
120 return {spec->grf_prop.grfid, spec->grf_prop.local_id, spec->class_index, spec->index};
121 }
122
123 PickerItem GetPickerItem(int cls_id, int id) const override
124 {
125 return GetPickerItem(GetClass(cls_id)->GetSpec(id), cls_id, id);
126 }
127
128 std::set<PickerItem> UpdateSavedItems(const std::set<PickerItem> &src) override
129 {
130 if (src.empty()) return {};
131
132 std::set<PickerItem> dst;
133 for (const auto &item : src) {
134 const auto *spec = T::GetByGrf(item.grfid, item.local_id);
135 if (spec == nullptr) {
136 dst.insert({item.grfid, item.local_id, -1, -1});
137 } else {
138 dst.insert(GetPickerItem(spec));
139 }
140 }
141 return dst;
142 }
143};
144
147 std::optional<BadgeTextFilter> btf;
148};
149
152
154public:
160
162 Class,
163 Type,
164 Position,
165 Validate,
166 };
167 using PickerInvalidations = EnumBitSet<PickerInvalidation, uint8_t>;
168
169 static constexpr PickerInvalidations PICKER_INVALIDATION_ALL{PickerInvalidation::Class, PickerInvalidation::Type, PickerInvalidation::Position, PickerInvalidation::Validate};
170
171 static const int PREVIEW_WIDTH = 64;
172 static const int PREVIEW_HEIGHT = 48;
173 static const int PREVIEW_LEFT = 31;
174 static const int PREVIEW_BOTTOM = 31;
175
176 static const uint EDITBOX_MAX_SIZE = 16;
177
178 bool has_class_picker = false;
179 bool has_type_picker = false;
180
182 void OnInit() override;
183 void Close(int data = 0) override;
184 void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override;
185 void DrawWidget(const Rect &r, WidgetID widget) const override;
186 void OnResize() override;
187 void OnClick(Point pt, WidgetID widget, int click_count) override;
188 void OnInvalidateData(int data = 0, bool gui_scope = true) override;
189 EventState OnHotkey(int hotkey) override;
190 void OnEditboxChanged(WidgetID wid) override;
191
196
197 void InvalidateData(PickerInvalidations data) { this->Window::InvalidateData(data.base()); }
198
199protected:
200 void ConstructWindow();
201
202 PickerCallbacks &callbacks;
203
204private:
206 PickerFilterData class_string_filter;
208
210 void EnsureSelectedClassIsValid();
211 void EnsureSelectedClassIsVisible();
212
214 PickerFilterData type_string_filter;
216
217 void RefreshUsedTypeList();
218 void BuildPickerTypeList();
219 void EnsureSelectedTypeIsValid();
220 void EnsureSelectedTypeIsVisible();
221
222 GUIBadgeClasses badge_classes;
223
224 IntervalTimer<TimerGameCalendar> yearly_interval = {{TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE}, [this](auto) {
225 this->SetDirty();
226 }};
227
228 IntervalTimer<TimerWindow> refresh_interval = {std::chrono::seconds(3), [this](auto) {
229 RefreshUsedTypeList();
230 }};
231};
232
233class NWidgetBase;
234std::unique_ptr<NWidgetBase> MakePickerClassWidgets();
235std::unique_ptr<NWidgetBase> MakePickerTypeWidgets();
236
237#endif /* PICKER_GUI_H */
Enum-as-bit-set wrapper.
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
Baseclass for nested widgets.
Helper for PickerCallbacks when the class system is based on NewGRFClass.
Definition picker_gui.h:104
PickerItem GetPickerItem(int cls_id, int id) const override
Get data about an item.
Definition picker_gui.h:123
int GetClassCount() const override
Get the number of classes.
Definition picker_gui.h:114
std::set< PickerItem > UpdateSavedItems(const std::set< PickerItem > &src) override
Update link between grfid/localidx and class_index/index in saved items.
Definition picker_gui.h:128
int GetTypeCount(int cls_id) const override
Get the number of types in a class.
Definition picker_gui.h:115
bool HasClassChoice() const override
Are there multiple classes to chose from?
Definition picker_gui.h:112
Class for PickerClassWindow to collect information and retain state.
Definition picker_gui.h:38
virtual int GetSelectedClass() const =0
Get the index of the selected class.
Filtering type_last_filtering
Default filtering of PickerTypeList.
Definition picker_gui.h:93
virtual PickerItem GetPickerItem(int cls_id, int id) const =0
Get data about an item.
const std::string ini_group
Ini Group for saving favourites.
Definition picker_gui.h:95
virtual void SetSelectedClass(int id) const =0
Set the selected class.
virtual bool IsActive() const =0
Should picker class/type selection be enabled?
virtual StringID GetTypeName(int cls_id, int id) const =0
Get the item of a type.
virtual bool IsTypeAvailable(int cls_id, int id) const =0
Test if an item is currently buildable.
virtual void FillUsedItems(std::set< PickerItem > &items)=0
Fill a set with all items that are used by the current player.
virtual StringID GetTypeTooltip() const =0
Get the tooltip string for the type grid.
virtual bool HasClassChoice() const =0
Are there multiple classes to chose from?
Listing type_last_sorting
Default sorting of PickerTypeList.
Definition picker_gui.h:92
std::set< PickerItem > used
Set of items used in the current game by the current company.
Definition picker_gui.h:98
virtual StringID GetClassTooltip() const =0
Get the tooltip string for the class list.
virtual int GetTypeCount(int cls_id) const =0
Get the number of types in a class.
virtual int GetClassCount() const =0
Get the number of classes.
virtual void SetSelectedType(int id) const =0
Set the selected type.
virtual StringID GetClassName(int id) const =0
Get the name of a class.
virtual std::set< PickerItem > UpdateSavedItems(const std::set< PickerItem > &src)=0
Update link between grfid/localidx and class_index/index in saved items.
virtual void DrawType(int x, int y, int cls_id, int id) const =0
Draw preview image of an item.
virtual std::span< const BadgeID > GetTypeBadges(int cls_id, int id) const =0
Get the item of a type.
Filtering class_last_filtering
Default filtering of PickerClassList.
Definition picker_gui.h:90
virtual int GetSelectedType() const =0
Get the selected type.
uint8_t mode
Bitmask of PickerFilterModes.
Definition picker_gui.h:96
std::set< PickerItem > saved
Set of saved favourite items.
Definition picker_gui.h:99
Listing class_last_sorting
Default sorting of PickerClassList.
Definition picker_gui.h:89
Base class for windows opened from a toolbar.
Definition window_gui.h:982
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
bool has_class_picker
Set if this window has a class picker 'component'.
Definition picker_gui.h:178
static const int PREVIEW_LEFT
Offset from left edge to draw preview.
Definition picker_gui.h:173
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
@ Position
Update scroll positions.
@ Class
Refresh the class list.
@ Type
Refresh the type list.
@ Validate
Validate selected item.
PickerTypeList types
List of types.
Definition picker_gui.h:213
QueryString class_editbox
Filter editbox.
Definition picker_gui.h:207
bool has_type_picker
Set if this window has a type picker 'component'.
Definition picker_gui.h:179
void BuildPickerClassList()
Builds the filter list of classes.
@ PFM_USED
Show used types.
Definition picker_gui.h:157
@ PFM_ALL
Show all classes.
Definition picker_gui.h:156
@ PFM_SAVED
Show saved types.
Definition picker_gui.h:158
void OnEditboxChanged(WidgetID wid) override
The text in an editbox has been edited.
void BuildPickerTypeList()
Builds the filter list of types.
void OnResize() override
Called after the window got resized.
PickerClassWindowHotkeys
Enum referring to the Hotkeys in the picker window.
Definition picker_gui.h:193
@ PCWHK_FOCUS_FILTER_BOX
Focus the edit box for editing the filter string.
Definition picker_gui.h:194
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
static const int PREVIEW_WIDTH
Width of each preview button.
Definition picker_gui.h:171
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
QueryString type_editbox
Filter editbox.
Definition picker_gui.h:215
static const int PREVIEW_BOTTOM
Offset from bottom edge to draw preview.
Definition picker_gui.h:174
EventState OnHotkey(int hotkey) override
A hotkey has been pressed.
static const uint EDITBOX_MAX_SIZE
The maximum number of characters for the filter edit box.
Definition picker_gui.h:176
void OnInit() override
Notification that the nested widget tree gets initialized.
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.
PickerClassList classes
List of classes.
Definition picker_gui.h:205
static const int PREVIEW_HEIGHT
Height of each preview button.
Definition picker_gui.h:172
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:943
GrfSpecFeature
Definition newgrf.h:68
Functions related to NewGRF badges.
std::unique_ptr< NWidgetBase > MakePickerClassWidgets()
Create nested widgets for the class picker widgets.
std::unique_ptr< NWidgetBase > MakePickerTypeWidgets()
Create nested widgets for the type picker widgets.
Base for the GUIs that have an edit box in them.
Base types for having sorted lists in GUIs.
Searching and filtering using a stringterm.
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Dimensions (a width and height) of a rectangle in 2D.
Data structure describing what to show in the list (filter criteria).
Data structure describing how to show the list (what sort direction and criteria).
const PickerCallbacks * callbacks
Callbacks for filter functions to access to callbacks.
Definition picker_gui.h:146
Coordinates of a point in 2D.
Data stored about a string that can be modified in the GUI.
Specification of a rectangle with absolute coordinates of all edges.
String filter and state.
High level window description.
Definition window_gui.h:168
Data structure for an opened window.
Definition window_gui.h:274
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing)
Definition window.cpp:3164
Window * parent
Parent window.
Definition window_gui.h:329
ResizeInfo resize
Resize information.
Definition window_gui.h:315
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:973
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
Definition of Interval and OneShot timers.
Definition of the game-calendar-timer.
Definition of the Window system.
Functions, definitions and such used only by the GUI.
Types related to windows.
int WidgetID
Widget ID.
Definition window_type.h:20
EventState
State of handling an event.