OpenTTD Source: src/picker_gui.h Source File

OpenTTD Source 15.0-beta1

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 "querystring_gui.h"
14#include "sortlist_type.h"
15#include "stringfilter_type.h"
16#include "strings_type.h"
17#include "timer/timer.h"
18#include "timer/timer_game_calendar.h"
19#include "timer/timer_window.h"
20#include "window_gui.h"
21#include "window_type.h"
22
23 struct PickerItem {
24 uint32_t grfid;
25 uint16_t local_id;
26 int class_index;
27 int index;
28
29 inline auto operator<=>(const PickerItem &other) const
30 {
31 if (auto cmp = this->grfid <=> other.grfid; cmp != 0) return cmp;
32 return this->local_id <=> other.local_id;
33 }
34};
35
37 class PickerCallbacks {
38public:
39 explicit PickerCallbacks(const std::string &ini_group);
40 virtual ~PickerCallbacks();
41
42 virtual void Close(int) { }
43
45 virtual bool IsActive() const = 0;
47 virtual bool HasClassChoice() const = 0;
48
49 /* Class callbacks */
51 virtual StringID GetClassTooltip() const = 0;
53 virtual int GetClassCount() const = 0;
55 virtual int GetSelectedClass() const = 0;
57 virtual void SetSelectedClass(int id) const = 0;
59 virtual StringID GetClassName(int id) const = 0;
60
61 /* Type callbacks */
63 virtual StringID GetTypeTooltip() const = 0;
65 virtual int GetTypeCount(int cls_id) const = 0;
66
68 virtual int GetSelectedType() const = 0;
70 virtual void SetSelectedType(int id) const = 0;
72 virtual PickerItem GetPickerItem(int cls_id, int id) const = 0;
74 virtual StringID GetTypeName(int cls_id, int id) const = 0;
76 virtual bool IsTypeAvailable(int cls_id, int id) const = 0;
78 virtual void DrawType(int x, int y, int cls_id, int id) const = 0;
79
81 virtual void FillUsedItems(std::set<PickerItem> &items) = 0;
83 virtual std::set<PickerItem> UpdateSavedItems(const std::set<PickerItem> &src) = 0;
84
85 Listing class_last_sorting = { false, 0 };
86 Filtering class_last_filtering = { false, 0 };
87
88 Listing type_last_sorting = { false, 0 };
89 Filtering type_last_filtering = { false, 0 };
90
91 const std::string ini_group;
92 uint8_t mode = 0;
93
94 std::set<PickerItem> used;
95 std::set<PickerItem> saved;
96};
97
99template <typename T>
100 class PickerCallbacksNewGRFClass : public PickerCallbacks {
101public:
102 explicit PickerCallbacksNewGRFClass(const std::string &ini_group) : PickerCallbacks(ini_group) {}
103
104 inline typename T::index_type GetClassIndex(int cls_id) const { return static_cast<typename T::index_type>(cls_id); }
105 inline const T *GetClass(int cls_id) const { return T::Get(this->GetClassIndex(cls_id)); }
106 inline const typename T::spec_type *GetSpec(int cls_id, int id) const { return this->GetClass(cls_id)->GetSpec(id); }
107
108 bool HasClassChoice() const override { return T::GetUIClassCount() > 1; }
109
110 int GetClassCount() const override { return T::GetClassCount(); }
111 int GetTypeCount(int cls_id) const override { return this->GetClass(cls_id)->GetSpecCount(); }
112
113 PickerItem GetPickerItem(const typename T::spec_type *spec, int cls_id = -1, int id = -1) const
114 {
115 if (spec == nullptr) return {0, 0, cls_id, id};
116 return {spec->grf_prop.grfid, spec->grf_prop.local_id, spec->class_index, spec->index};
117 }
118
119 PickerItem GetPickerItem(int cls_id, int id) const override
120 {
121 return GetPickerItem(GetClass(cls_id)->GetSpec(id), cls_id, id);
122 }
123
124 std::set<PickerItem> UpdateSavedItems(const std::set<PickerItem> &src) override
125 {
126 if (src.empty()) return {};
127
128 std::set<PickerItem> dst;
129 for (const auto &item : src) {
130 const auto *spec = T::GetByGrf(item.grfid, item.local_id);
131 if (spec == nullptr) {
132 dst.insert({item.grfid, item.local_id, -1, -1});
133 } else {
134 dst.insert(GetPickerItem(spec));
135 }
136 }
137 return dst;
138 }
139};
140
141 struct PickerFilterData : StringFilter {
142 const PickerCallbacks *callbacks;
143};
144
145 using PickerClassList = GUIList<int, std::nullptr_t, PickerFilterData &>;
146 using PickerTypeList = GUIList<PickerItem, std::nullptr_t, PickerFilterData &>;
147
148 class PickerWindow : public PickerWindowBase {
149public:
150 enum PickerFilterModes {
151 PFM_ALL = 0,
152 PFM_USED = 1,
153 PFM_SAVED = 2,
154 };
155
156 enum PickerFilterInvalidation {
157 PFI_CLASS = 1U << 0,
158 PFI_TYPE = 1U << 1,
159 PFI_POSITION = 1U << 2,
160 PFI_VALIDATE = 1U << 3,
161 };
162
163 static const int PREVIEW_WIDTH = 64;
164 static const int PREVIEW_HEIGHT = 48;
165 static const int PREVIEW_LEFT = 31;
166 static const int PREVIEW_BOTTOM = 31;
167
168 static const uint EDITBOX_MAX_SIZE = 16;
169
170 bool has_class_picker = false;
171 bool has_type_picker = false;
172
173 PickerWindow(WindowDesc &desc, Window *parent, int window_number, PickerCallbacks &callbacks);
174 void Close(int data = 0) override;
175 void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override;
176 void DrawWidget(const Rect &r, WidgetID widget) const override;
177 void OnResize() override;
178 void OnClick(Point pt, WidgetID widget, int click_count) override;
179 void OnInvalidateData(int data = 0, bool gui_scope = true) override;
180 EventState OnHotkey(int hotkey) override;
181 void OnEditboxChanged(WidgetID wid) override;
182
184 enum PickerClassWindowHotkeys {
185 PCWHK_FOCUS_FILTER_BOX,
186 };
187
188protected:
189 void ConstructWindow();
190
191 PickerCallbacks &callbacks;
192
193private:
194 PickerClassList classes;
195 PickerFilterData class_string_filter;
196 QueryString class_editbox;
197
198 void BuildPickerClassList();
199 void EnsureSelectedClassIsValid();
200 void EnsureSelectedClassIsVisible();
201
202 PickerTypeList types;
203 PickerFilterData type_string_filter;
204 QueryString type_editbox;
205
206 void RefreshUsedTypeList();
207 void BuildPickerTypeList();
208 void EnsureSelectedTypeIsValid();
209 void EnsureSelectedTypeIsVisible();
210
211 IntervalTimer<TimerGameCalendar> yearly_interval = {{TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE}, [this](auto) {
212 this->SetDirty();
213 }};
214
215 IntervalTimer<TimerWindow> refresh_interval = {std::chrono::seconds(3), [this](auto) {
216 RefreshUsedTypeList();
217 }};
218};
219
220class NWidgetBase;
221std::unique_ptr<NWidgetBase> MakePickerClassWidgets();
222std::unique_ptr<NWidgetBase> MakePickerTypeWidgets();
223
224#endif /* PICKER_GUI_H */
GUIList< int, std::nullptr_t, PickerFilterData & >
IntervalTimer
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
NWidgetBase
Baseclass for nested widgets.
Definition widget_type.h:145
PickerCallbacksNewGRFClass
Helper for PickerCallbacks when the class system is based on NewGRFClass.
Definition picker_gui.h:100
PickerCallbacksNewGRFClass::GetPickerItem
PickerItem GetPickerItem(int cls_id, int id) const override
Get data about an item.
Definition picker_gui.h:119
PickerCallbacksNewGRFClass::GetClassCount
int GetClassCount() const override
Get the number of classes.
Definition picker_gui.h:110
PickerCallbacksNewGRFClass::UpdateSavedItems
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:124
PickerCallbacksNewGRFClass::GetTypeCount
int GetTypeCount(int cls_id) const override
Get the number of types in a class.
Definition picker_gui.h:111
PickerCallbacksNewGRFClass::HasClassChoice
bool HasClassChoice() const override
Are there multiple classes to chose from?
Definition picker_gui.h:108
PickerCallbacks
Class for PickerClassWindow to collect information and retain state.
Definition picker_gui.h:37
PickerCallbacks::GetSelectedClass
virtual int GetSelectedClass() const =0
Get the index of the selected class.
PickerCallbacks::type_last_filtering
Filtering type_last_filtering
Default filtering of PickerTypeList.
Definition picker_gui.h:89
PickerCallbacks::GetPickerItem
virtual PickerItem GetPickerItem(int cls_id, int id) const =0
Get data about an item.
PickerCallbacks::ini_group
const std::string ini_group
Ini Group for saving favourites.
Definition picker_gui.h:91
PickerCallbacks::SetSelectedClass
virtual void SetSelectedClass(int id) const =0
Set the selected class.
PickerCallbacks::IsActive
virtual bool IsActive() const =0
Should picker class/type selection be enabled?
PickerCallbacks::GetTypeName
virtual StringID GetTypeName(int cls_id, int id) const =0
Get the item of a type.
PickerCallbacks::IsTypeAvailable
virtual bool IsTypeAvailable(int cls_id, int id) const =0
Test if an item is currently buildable.
PickerCallbacks::FillUsedItems
virtual void FillUsedItems(std::set< PickerItem > &items)=0
Fill a set with all items that are used by the current player.
PickerCallbacks::GetTypeTooltip
virtual StringID GetTypeTooltip() const =0
Get the tooltip string for the type grid.
PickerCallbacks::HasClassChoice
virtual bool HasClassChoice() const =0
Are there multiple classes to chose from?
PickerCallbacks::type_last_sorting
Listing type_last_sorting
Default sorting of PickerTypeList.
Definition picker_gui.h:88
PickerCallbacks::used
std::set< PickerItem > used
Set of items used in the current game by the current company.
Definition picker_gui.h:94
PickerCallbacks::GetClassTooltip
virtual StringID GetClassTooltip() const =0
Get the tooltip string for the class list.
PickerCallbacks::GetTypeCount
virtual int GetTypeCount(int cls_id) const =0
Get the number of types in a class.
PickerCallbacks::GetClassCount
virtual int GetClassCount() const =0
Get the number of classes.
PickerCallbacks::SetSelectedType
virtual void SetSelectedType(int id) const =0
Set the selected type.
PickerCallbacks::GetClassName
virtual StringID GetClassName(int id) const =0
Get the name of a class.
PickerCallbacks::UpdateSavedItems
virtual std::set< PickerItem > UpdateSavedItems(const std::set< PickerItem > &src)=0
Update link between grfid/localidx and class_index/index in saved items.
PickerCallbacks::DrawType
virtual void DrawType(int x, int y, int cls_id, int id) const =0
Draw preview image of an item.
PickerCallbacks::class_last_filtering
Filtering class_last_filtering
Default filtering of PickerClassList.
Definition picker_gui.h:86
PickerCallbacks::GetSelectedType
virtual int GetSelectedType() const =0
Get the selected type.
PickerCallbacks::mode
uint8_t mode
Bitmask of PickerFilterModes.
Definition picker_gui.h:92
PickerCallbacks::saved
std::set< PickerItem > saved
Set of saved favourite items.
Definition picker_gui.h:95
PickerCallbacks::class_last_sorting
Listing class_last_sorting
Default sorting of PickerClassList.
Definition picker_gui.h:85
PickerWindowBase
Base class for windows opened from a toolbar.
Definition window_gui.h:986
PickerWindow
Definition picker_gui.h:148
PickerWindow::Close
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
Definition picker_gui.cpp:240
PickerWindow::has_class_picker
bool has_class_picker
Set if this window has a class picker 'component'.
Definition picker_gui.h:170
PickerWindow::PREVIEW_LEFT
static const int PREVIEW_LEFT
Offset from left edge to draw preview.
Definition picker_gui.h:165
PickerWindow::DrawWidget
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
Definition picker_gui.cpp:274
PickerWindow::PickerFilterModes
PickerFilterModes
Definition picker_gui.h:150
PickerWindow::PFM_USED
@ PFM_USED
Show used types.
Definition picker_gui.h:152
PickerWindow::PFM_ALL
@ PFM_ALL
Show all classes.
Definition picker_gui.h:151
PickerWindow::PFM_SAVED
@ PFM_SAVED
Show saved types.
Definition picker_gui.h:153
PickerWindow::types
PickerTypeList types
List of types.
Definition picker_gui.h:202
PickerWindow::class_editbox
QueryString class_editbox
Filter editbox.
Definition picker_gui.h:196
PickerWindow::has_type_picker
bool has_type_picker
Set if this window has a type picker 'component'.
Definition picker_gui.h:171
PickerWindow::BuildPickerClassList
void BuildPickerClassList()
Builds the filter list of classes.
Definition picker_gui.cpp:451
PickerWindow::PickerClassWindowHotkeys
PickerClassWindowHotkeys
Enum referring to the Hotkeys in the picker window.
Definition picker_gui.h:184
PickerWindow::PCWHK_FOCUS_FILTER_BOX
@ PCWHK_FOCUS_FILTER_BOX
Focus the edit box for editing the filter string.
Definition picker_gui.h:185
PickerWindow::OnEditboxChanged
void OnEditboxChanged(WidgetID wid) override
The text in an editbox has been edited.
Definition picker_gui.cpp:430
PickerWindow::BuildPickerTypeList
void BuildPickerTypeList()
Builds the filter list of types.
Definition picker_gui.cpp:520
PickerWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition picker_gui.cpp:324
PickerWindow::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 picker_gui.cpp:331
PickerWindow::PickerFilterInvalidation
PickerFilterInvalidation
Definition picker_gui.h:156
PickerWindow::PFI_POSITION
@ PFI_POSITION
Update scroll positions.
Definition picker_gui.h:159
PickerWindow::PFI_VALIDATE
@ PFI_VALIDATE
Validate selected item.
Definition picker_gui.h:160
PickerWindow::PFI_CLASS
@ PFI_CLASS
Refresh the class list.
Definition picker_gui.h:157
PickerWindow::PFI_TYPE
@ PFI_TYPE
Refresh the type list.
Definition picker_gui.h:158
PickerWindow::PREVIEW_WIDTH
static const int PREVIEW_WIDTH
Width of each preview button.
Definition picker_gui.h:163
PickerWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition picker_gui.cpp:390
PickerWindow::type_editbox
QueryString type_editbox
Filter editbox.
Definition picker_gui.h:204
PickerWindow::PREVIEW_BOTTOM
static const int PREVIEW_BOTTOM
Offset from bottom edge to draw preview.
Definition picker_gui.h:166
PickerWindow::OnHotkey
EventState OnHotkey(int hotkey) override
A hotkey has been pressed.
Definition picker_gui.cpp:412
PickerWindow::EDITBOX_MAX_SIZE
static const uint EDITBOX_MAX_SIZE
The maximum number of characters for the filter edit box.
Definition picker_gui.h:168
PickerWindow::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 picker_gui.cpp:246
PickerWindow::classes
PickerClassList classes
List of classes.
Definition picker_gui.h:194
PickerWindow::PREVIEW_HEIGHT
static const int PREVIEW_HEIGHT
Height of each preview button.
Definition picker_gui.h:164
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:940
MakePickerClassWidgets
std::unique_ptr< NWidgetBase > MakePickerClassWidgets()
Create nested widgets for the class picker widgets.
Definition picker_gui.cpp:623
MakePickerTypeWidgets
std::unique_ptr< NWidgetBase > MakePickerTypeWidgets()
Create nested widgets for the type picker widgets.
Definition picker_gui.cpp:646
querystring_gui.h
Base for the GUIs that have an edit box in them.
sortlist_type.h
Base types for having sorted lists in GUIs.
stringfilter_type.h
Searching and filtering using a stringterm.
strings_type.h
Types related to strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:16
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:30
Filtering
Data structure describing what to show in the list (filter criteria).
Definition sortlist_type.h:35
Listing
Data structure describing how to show the list (what sort direction and criteria).
Definition sortlist_type.h:30
PickerFilterData
Definition picker_gui.h:141
PickerFilterData::callbacks
const PickerCallbacks * callbacks
Callbacks for filter functions to access to callbacks.
Definition picker_gui.h:142
PickerItem
Definition picker_gui.h:23
Point
Coordinates of a point in 2D.
Definition geometry_type.hpp:21
QueryString
Data stored about a string that can be modified in the GUI.
Definition querystring_gui.h:20
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:75
StringFilter
String filter and state.
Definition stringfilter_type.h:30
WindowDesc
High level window description.
Definition window_gui.h:159
Window
Data structure for an opened window.
Definition window_gui.h:273
Window::parent
Window * parent
Parent window.
Definition window_gui.h:328
Window::resize
ResizeInfo resize
Resize information.
Definition window_gui.h:314
Window::GetWidget
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:977
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:302
timer.h
Definition of Interval and OneShot timers.
timer_game_calendar.h
Definition of the game-calendar-timer.
timer_window.h
Definition of the Window system.
window_gui.h
Functions, definitions and such used only by the GUI.
window_type.h
Types related to windows.
WidgetID
int WidgetID
Widget ID.
Definition window_type.h:18
EventState
EventState
State of handling an event.
Definition window_type.h:743