OpenTTD Source  20240915-master-g3784a3d3d6
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"
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 
38 public:
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 };
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 
99 template <typename T>
101 public:
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.grffile == nullptr ? 0 : spec->grf_prop.grffile->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 
143 };
144 
147 
149 public:
151  PFM_ALL = 0,
152  PFM_USED = 1,
153  PFM_SAVED = 2,
154  };
155 
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 
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 
186  };
187 
188 protected:
189  void ConstructWindow();
190 
191  PickerCallbacks &callbacks;
192 
193 private:
195  PickerFilterData class_string_filter;
197 
198  void BuildPickerClassList();
199  void EnsureSelectedClassIsValid();
200  void EnsureSelectedClassIsVisible();
201 
203  PickerFilterData type_string_filter;
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 
220 class NWidgetBase;
221 std::unique_ptr<NWidgetBase> MakePickerClassWidgets();
222 std::unique_ptr<NWidgetBase> MakePickerTypeWidgets();
223 
224 #endif /* PICKER_GUI_H */
PickerCallbacksNewGRFClass::HasClassChoice
bool HasClassChoice() const override
Are there multiple classes to chose from?
Definition: picker_gui.h:108
PickerWindow::has_type_picker
bool has_type_picker
Set if this window has a type picker 'component'.
Definition: picker_gui.h:171
PickerCallbacks::GetClassName
virtual StringID GetClassName(int id) const =0
Get the name of a class.
PickerCallbacks::type_last_filtering
Filtering type_last_filtering
Default filtering of PickerTypeList.
Definition: picker_gui.h:89
PickerWindow::PREVIEW_HEIGHT
static const int PREVIEW_HEIGHT
Height of each preview button.
Definition: picker_gui.h:164
PickerWindow::PREVIEW_WIDTH
static const int PREVIEW_WIDTH
Width of each preview button.
Definition: picker_gui.h:163
querystring_gui.h
PickerWindow::PFM_ALL
@ PFM_ALL
Show all classes.
Definition: picker_gui.h:151
PickerFilterData::callbacks
const PickerCallbacks * callbacks
Callbacks for filter functions to access to callbacks.
Definition: picker_gui.h:142
PickerWindow::PFM_USED
@ PFM_USED
Show used types.
Definition: picker_gui.h:152
PickerCallbacks
Class for PickerClassWindow to collect information and retain state.
Definition: picker_gui.h:37
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
timer_game_calendar.h
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
GUIList< int, std::nullptr_t, PickerFilterData & >
IntervalTimer< TimerGameCalendar >
PickerWindow::PFI_POSITION
@ PFI_POSITION
Update scroll positions.
Definition: picker_gui.h:159
PickerCallbacks::GetTypeCount
virtual int GetTypeCount(int cls_id) const =0
Get the number of types in a class.
window_type.h
PickerCallbacks::FillUsedItems
virtual void FillUsedItems(std::set< PickerItem > &items)=0
Fill a set with all items that are used by the current player.
PickerCallbacks::type_last_sorting
Listing type_last_sorting
Default sorting of PickerTypeList.
Definition: picker_gui.h:88
PickerWindow::PCWHK_FOCUS_FILTER_BOX
@ PCWHK_FOCUS_FILTER_BOX
Focus the edit box for editing the filter string.
Definition: picker_gui.h:185
PickerCallbacks::GetSelectedClass
virtual int GetSelectedClass() const =0
Get the index of the selected class.
strings_type.h
PickerCallbacks::SetSelectedType
virtual void SetSelectedType(int id) const =0
Set the selected type.
PickerCallbacks::GetClassTooltip
virtual StringID GetClassTooltip() const =0
Get the tooltip string for the class list.
QueryString
Data stored about a string that can be modified in the GUI.
Definition: querystring_gui.h:20
PickerCallbacks::GetSelectedType
virtual int GetSelectedType() const =0
Get the selected type.
PickerWindow::PickerFilterModes
PickerFilterModes
Definition: picker_gui.h:150
PickerCallbacks::DrawType
virtual void DrawType(int x, int y, int cls_id, int id) const =0
Draw preview image of an item.
PickerCallbacks::HasClassChoice
virtual bool HasClassChoice() const =0
Are there multiple classes to chose from?
WindowDesc
High level window description.
Definition: window_gui.h:162
WidgetID
int WidgetID
Widget ID.
Definition: window_type.h:18
PickerCallbacksNewGRFClass::GetPickerItem
PickerItem GetPickerItem(int cls_id, int id) const override
Get data about an item.
Definition: picker_gui.h:119
window_gui.h
PickerCallbacks::mode
uint8_t mode
Bitmask of PickerFilterModes.
Definition: picker_gui.h:92
PickerCallbacks::class_last_filtering
Filtering class_last_filtering
Default filtering of PickerClassList.
Definition: picker_gui.h:86
Listing
Data structure describing how to show the list (what sort direction and criteria).
Definition: sortlist_type.h:30
PickerWindow::classes
PickerClassList classes
List of classes.
Definition: picker_gui.h:194
PickerWindow::OnHotkey
EventState OnHotkey(int hotkey) override
A hotkey has been pressed.
Definition: picker_gui.cpp:408
Window::resize
ResizeInfo resize
Resize information.
Definition: window_gui.h:317
PickerWindow
Definition: picker_gui.h:148
MakePickerTypeWidgets
std::unique_ptr< NWidgetBase > MakePickerTypeWidgets()
Create nested widgets for the type picker widgets.
Definition: picker_gui.cpp:642
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:940
PickerWindow::PickerClassWindowHotkeys
PickerClassWindowHotkeys
Enum referring to the Hotkeys in the picker window.
Definition: picker_gui.h:184
PickerWindow::BuildPickerClassList
void BuildPickerClassList()
Builds the filter list of classes.
Definition: picker_gui.cpp:447
PickerWindow::OnResize
void OnResize() override
Called after the window got resized.
Definition: picker_gui.cpp:320
PickerWindow::PFI_VALIDATE
@ PFI_VALIDATE
Validate selected item.
Definition: picker_gui.h:160
NWidgetBase
Baseclass for nested widgets.
Definition: widget_type.h:146
PickerWindow::BuildPickerTypeList
void BuildPickerTypeList()
Builds the filter list of types.
Definition: picker_gui.cpp:516
PickerCallbacks::GetClassCount
virtual int GetClassCount() const =0
Get the number of classes.
PickerWindow::PREVIEW_LEFT
static const int PREVIEW_LEFT
Offset from left edge to draw preview.
Definition: picker_gui.h:165
Filtering
Data structure describing what to show in the list (filter criteria).
Definition: sortlist_type.h:35
Window::parent
Window * parent
Parent window.
Definition: window_gui.h:331
sortlist_type.h
timer.h
PickerCallbacks::IsTypeAvailable
virtual bool IsTypeAvailable(int cls_id, int id) const =0
Test if an item is currently buildable.
PickerCallbacks::GetTypeName
virtual StringID GetTypeName(int cls_id, int id) const =0
Get the item of a type.
PickerCallbacks::class_last_sorting
Listing class_last_sorting
Default sorting of PickerClassList.
Definition: picker_gui.h:85
PickerWindow::PFI_CLASS
@ PFI_CLASS
Refresh the class list.
Definition: picker_gui.h:157
PickerCallbacksNewGRFClass::GetClassCount
int GetClassCount() const override
Get the number of classes.
Definition: picker_gui.h:110
PickerWindow::class_editbox
QueryString class_editbox
Filter editbox.
Definition: picker_gui.h:196
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
PickerCallbacks::IsActive
virtual bool IsActive() const =0
Should picker class/type selection be enabled?
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:305
PickerFilterData
Definition: picker_gui.h:141
PickerCallbacks::GetPickerItem
virtual PickerItem GetPickerItem(int cls_id, int id) const =0
Get data about an item.
PickerCallbacks::saved
std::set< PickerItem > saved
Set of saved favourite items.
Definition: picker_gui.h:95
PickerCallbacksNewGRFClass
Helper for PickerCallbacks when the class system is based on NewGRFClass.
Definition: picker_gui.h:100
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
MakePickerClassWidgets
std::unique_ptr< NWidgetBase > MakePickerClassWidgets()
Create nested widgets for the class picker widgets.
Definition: picker_gui.cpp:619
PickerCallbacks::SetSelectedClass
virtual void SetSelectedClass(int id) const =0
Set the selected class.
EventState
EventState
State of handling an event.
Definition: window_type.h:737
PickerWindowBase
Base class for windows opened from a toolbar.
Definition: window_gui.h:989
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
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.
stringfilter_type.h
PickerCallbacks::ini_group
const std::string ini_group
Ini Group for saving favourites.
Definition: picker_gui.h:91
timer_window.h
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:236
PickerWindow::PREVIEW_BOTTOM
static const int PREVIEW_BOTTOM
Offset from bottom edge to draw preview.
Definition: picker_gui.h:166
Window
Data structure for an opened window.
Definition: window_gui.h:276
PickerWindow::has_class_picker
bool has_class_picker
Set if this window has a class picker 'component'.
Definition: picker_gui.h:170
PickerCallbacks::used
std::set< PickerItem > used
Set of items used in the current game by the current company.
Definition: picker_gui.h:94
PickerItem
Definition: picker_gui.h:23
PickerWindow::type_editbox
QueryString type_editbox
Filter editbox.
Definition: picker_gui.h:204
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
PickerCallbacksNewGRFClass::GetTypeCount
int GetTypeCount(int cls_id) const override
Get the number of types in a class.
Definition: picker_gui.h:111
PickerWindow::PFM_SAVED
@ PFM_SAVED
Show saved types.
Definition: picker_gui.h:153
StringFilter
String filter and state.
Definition: stringfilter_type.h:30
PickerWindow::PickerFilterInvalidation
PickerFilterInvalidation
Definition: picker_gui.h:156
PickerCallbacks::GetTypeTooltip
virtual StringID GetTypeTooltip() const =0
Get the tooltip string for the type grid.
PickerWindow::PFI_TYPE
@ PFI_TYPE
Refresh the type list.
Definition: picker_gui.h:158
PickerWindow::types
PickerTypeList types
List of types.
Definition: picker_gui.h:202