OpenTTD Source 20260311-master-g511d3794ce
settingentry_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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef SETTINGENTRY_GUI_H
11#define SETTINGENTRY_GUI_H
12
13#include "core/enum_type.hpp"
14#include "settings_internal.h"
15#include "stringfilter_type.h"
16
28
30
41
42
50
52struct BaseSettingEntry {
54 uint8_t level;
55
56 BaseSettingEntry() : flags(), level(0) {}
58 virtual ~BaseSettingEntry() = default;
59
60 virtual void Init(uint8_t level = 0);
62 virtual void FoldAll() {}
64 virtual void UnFoldAll() {}
66 virtual void ResetAll() = 0;
67
72 void SetLastField(bool last_field) { this->flags.Set(SettingEntryFlag::LastField, last_field); }
73
78 virtual uint Length() const = 0;
84 virtual void GetFoldingState([[maybe_unused]] bool &all_folded, [[maybe_unused]] bool &all_unfolded) const {}
85 virtual bool IsVisible(const BaseSettingEntry *item) const;
86 virtual BaseSettingEntry *FindEntry(uint row, uint *cur_row);
92 virtual uint GetMaxHelpHeight([[maybe_unused]] int maxw) { return 0; }
93
98 bool IsFiltered() const { return this->flags.Test(SettingEntryFlag::Filtered); }
99
106 virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible) = 0;
107
108 virtual uint Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row = 0, uint parent_last = 0) const;
109
110 static inline Dimension circle_size;
111 static inline int line_height;
112
113protected:
122 virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const = 0;
123};
124
126struct SettingEntry : BaseSettingEntry {
127 const std::string_view name;
128 const IntSettingDesc *setting = nullptr;
129
134 SettingEntry(std::string_view name) : name(name) {}
135
136 void Init(uint8_t level = 0) override;
137 void ResetAll() override;
138 uint Length() const override;
139 uint GetMaxHelpHeight(int maxw) override;
140 bool UpdateFilterState(SettingFilter &filter, bool force_visible) override;
141
142 void SetButtons(SettingEntryFlags new_val);
143
144protected:
145 void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override;
146
147private:
149};
150
153 using EntryVector = std::vector<BaseSettingEntry*>;
155
161 template <typename T>
162 T *Add(T *item)
163 {
164 this->entries.push_back(item);
165 return item;
166 }
167
168 void Init(uint8_t level = 0);
169 void ResetAll();
170 void FoldAll();
171 void UnFoldAll();
172
173 uint Length() const;
174 void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
175 bool IsVisible(const BaseSettingEntry *item) const;
176 BaseSettingEntry *FindEntry(uint row, uint *cur_row);
177 uint GetMaxHelpHeight(int maxw);
178
179 bool UpdateFilterState(SettingFilter &filter, bool force_visible);
180
181 uint Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row = 0, uint parent_last = 0) const;
182};
183
185struct SettingsPage : BaseSettingEntry, SettingsContainer {
187 bool folded;
188
190
191 void Init(uint8_t level = 0) override;
192 void ResetAll() override;
193 void FoldAll() override;
194 void UnFoldAll() override;
195
196 uint Length() const override;
197 void GetFoldingState(bool &all_folded, bool &all_unfolded) const override;
198 bool IsVisible(const BaseSettingEntry *item) const override;
199 BaseSettingEntry *FindEntry(uint row, uint *cur_row) override;
200 uint GetMaxHelpHeight(int maxw) override { return SettingsContainer::GetMaxHelpHeight(maxw); }
201
202 bool UpdateFilterState(SettingFilter &filter, bool force_visible) override;
203
204 uint Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row = 0, uint parent_last = 0) const override;
205
206protected:
207 void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override;
208};
209
211const void *ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc *sd);
212
213#endif /* SETTINGENTRY_GUI_H */
214
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
Enum-as-bit-set wrapper.
#define T
Climate temperate.
Definition engines.h:91
Type (helpers) for enums.
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:86
@ Init
Second step of NewGRF loading; load all actions into memory.
Definition newgrf.h:51
const void * ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc *sd)
Resolve the underlying object where to dynamically load/save a setting to.
RestrictionMode
How the list of advanced settings is filtered.
@ RM_CHANGED_AGAINST_DEFAULT
Show only settings which are different compared to default values.
@ RM_ADVANCED
Display settings associated to the "advanced" list.
@ RM_ALL
List all settings regardless of the default/newgame/... values.
@ RM_CHANGED_AGAINST_NEW
Show only settings which are different compared to the user's new game setting values.
@ RM_END
End for iteration.
@ RM_BASIC
Display settings associated to the "basic" list.
static constexpr SettingEntryFlags SEF_BUTTONS_MASK
Mask for button flags.
SettingEntryFlag
Flags for SettingEntry.
@ LeftDepressed
Of a numeric setting entry, the left button is depressed.
@ LastField
This entry is the last one in a (sub-)page.
@ RightDepressed
Of a numeric setting entry, the right button is depressed.
@ Filtered
Entry is hidden by the string filter.
EnumBitSet< SettingEntryFlag, uint8_t > SettingEntryFlags
Bitset of the SettingEntryFlag elements.
SettingsContainer & GetSettingsTree()
Construct settings tree.
Functions and types used internally for the settings configurations.
SettingType
Type of settings for filtering.
Searching and filtering using a stringterm.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Data structure describing a single setting in a tab.
virtual ~BaseSettingEntry()=default
Ensure the destructor of the sub classes are called as well.
virtual void UnFoldAll()
Recursively open all folds of sub-pages.
virtual void GetFoldingState(bool &all_folded, bool &all_unfolded) const
Recursively accumulate the folding state of the tree.
virtual void ResetAll()=0
Resets all settings to their default values.
void SetLastField(bool last_field)
Set whether this is the last visible entry of the parent node.
virtual uint Length() const =0
Get the number of rows needed to show this entry.
bool IsFiltered() const
Check whether an entry is hidden due to filters.
virtual BaseSettingEntry * FindEntry(uint row, uint *cur_row)
Find setting entry at row row_num.
static Dimension circle_size
Dimension of the circle +/- icon.
static int line_height
Height of a single setting.
virtual void FoldAll()
Recursively close all folds of sub-pages.
virtual uint Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row=0, uint parent_last=0) const
Draw a row in the settings panel.
virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const =0
Function to draw setting value (button + text + current value).
SettingEntryFlags flags
Flags of the setting entry.
virtual uint GetMaxHelpHeight(int maxw)
Get the biggest height of the help text(s), if the width is at least maxw.
virtual bool IsVisible(const BaseSettingEntry *item) const
Check whether an entry is visible and not folded or filtered away.
virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible)=0
Update the filter state.
uint8_t level
Nesting level of this setting entry.
Dimensions (a width and height) of a rectangle in 2D.
All settings together for the game.
Base integer type, including boolean, settings.
uint Length() const override
Get the number of rows needed to show this entry.
SettingEntry(std::string_view name)
Create the entry.
void SetButtons(SettingEntryFlags new_val)
Set the button-depressed flags (SettingsEntryFlag::LeftDepressed and SettingsEntryFlag::RightDepresse...
void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override
Function to draw setting value (button + text + current value).
bool IsVisibleByRestrictionMode(RestrictionMode mode) const
Checks whether an entry shall be made visible based on the restriction mode.
const std::string_view name
Name of the setting.
uint GetMaxHelpHeight(int maxw) override
Get the biggest height of the help text(s), if the width is at least maxw.
const IntSettingDesc * setting
Setting description of the setting.
bool UpdateFilterState(SettingFilter &filter, bool force_visible) override
Update the filter state.
void ResetAll() override
Resets all settings to their default values.
Filter for settings list.
SettingType type
Filter based on type.
bool type_hides
Whether the type hides filtered strings.
RestrictionMode mode
Filter based on category.
RestrictionMode min_cat
Minimum category needed to display all filtered strings (RM_BASIC, RM_ADVANCED, or RM_ALL).
StringFilter string
Filter string.
Containers for BaseSettingEntry.
bool UpdateFilterState(SettingFilter &filter, bool force_visible)
Update the filter state.
void GetFoldingState(bool &all_folded, bool &all_unfolded) const
Recursively accumulate the folding state of the tree.
uint GetMaxHelpHeight(int maxw)
Get the biggest height of the help texts, if the width is at least maxw.
void ResetAll()
Resets all settings to their default values.
bool IsVisible(const BaseSettingEntry *item) const
Check whether an entry is visible and not folded or filtered away.
void FoldAll()
Recursively close all folds of sub-pages.
void UnFoldAll()
Recursively open all folds of sub-pages.
BaseSettingEntry * FindEntry(uint row, uint *cur_row)
Find the setting entry at row number row_num.
EntryVector entries
Settings on this page.
uint Length() const
Return number of rows needed to display the whole page.
uint Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row=0, uint parent_last=0) const
Draw a row in the settings panel.
std::vector< BaseSettingEntry * > EntryVector
Vector of pointers.
T * Add(T *item)
Add an item to the container.
StringID title
Title of the sub-page.
bool IsVisible(const BaseSettingEntry *item) const override
Check whether an entry is visible and not folded or filtered away.
SettingsPage(StringID title)
Constructor for a sub-page in the 'advanced settings' window.
void UnFoldAll() override
Recursively open all folds of sub-pages.
void FoldAll() override
Recursively close all folds of sub-pages.
uint Length() const override
Get the number of rows needed to show this entry.
BaseSettingEntry * FindEntry(uint row, uint *cur_row) override
Find setting entry at row row_num.
uint Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row=0, uint parent_last=0) const override
Draw a row in the settings panel.
uint GetMaxHelpHeight(int maxw) override
Get the biggest height of the help text(s), if the width is at least maxw.
void GetFoldingState(bool &all_folded, bool &all_unfolded) const override
Recursively accumulate the folding state of the tree.
void ResetAll() override
Resets all settings to their default values.
bool folded
Sub-page is folded (not visible except for its title).
bool UpdateFilterState(SettingFilter &filter, bool force_visible) override
Update the filter state.
void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override
Function to draw setting value (button + text + current value).
String filter and state.