OpenTTD Source 20260218-master-g2123fca5ea
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) {}
57 virtual ~BaseSettingEntry() = default;
58
59 virtual void Init(uint8_t level = 0);
61 virtual void FoldAll() {}
63 virtual void UnFoldAll() {}
65 virtual void ResetAll() = 0;
66
71 void SetLastField(bool last_field) { this->flags.Set(SettingEntryFlag::LastField, last_field); }
72
77 virtual uint Length() const = 0;
83 virtual void GetFoldingState([[maybe_unused]] bool &all_folded, [[maybe_unused]] bool &all_unfolded) const {}
84 virtual bool IsVisible(const BaseSettingEntry *item) const;
85 virtual BaseSettingEntry *FindEntry(uint row, uint *cur_row);
91 virtual uint GetMaxHelpHeight([[maybe_unused]] int maxw) { return 0; }
92
97 bool IsFiltered() const { return this->flags.Test(SettingEntryFlag::Filtered); }
98
105 virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible) = 0;
106
107 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;
108
109 static inline Dimension circle_size;
110 static inline int line_height;
111
112protected:
121 virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const = 0;
122};
123
125struct SettingEntry : BaseSettingEntry {
126 const std::string_view name;
127 const IntSettingDesc *setting = nullptr;
128
133 SettingEntry(std::string_view name) : name(name) {}
134
135 void Init(uint8_t level = 0) override;
136 void ResetAll() override;
137 uint Length() const override;
138 uint GetMaxHelpHeight(int maxw) override;
139 bool UpdateFilterState(SettingFilter &filter, bool force_visible) override;
140
141 void SetButtons(SettingEntryFlags new_val);
142
143protected:
144 void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override;
145
146private:
148};
149
152 using EntryVector = std::vector<BaseSettingEntry*>;
154
160 template <typename T>
161 T *Add(T *item)
162 {
163 this->entries.push_back(item);
164 return item;
165 }
166
167 void Init(uint8_t level = 0);
168 void ResetAll();
169 void FoldAll();
170 void UnFoldAll();
171
172 uint Length() const;
173 void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
174 bool IsVisible(const BaseSettingEntry *item) const;
175 BaseSettingEntry *FindEntry(uint row, uint *cur_row);
176 uint GetMaxHelpHeight(int maxw);
177
178 bool UpdateFilterState(SettingFilter &filter, bool force_visible);
179
180 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;
181};
182
184struct SettingsPage : BaseSettingEntry, SettingsContainer {
186 bool folded;
187
189
190 void Init(uint8_t level = 0) override;
191 void ResetAll() override;
192 void FoldAll() override;
193 void UnFoldAll() override;
194
195 uint Length() const override;
196 void GetFoldingState(bool &all_folded, bool &all_unfolded) const override;
197 bool IsVisible(const BaseSettingEntry *item) const override;
198 BaseSettingEntry *FindEntry(uint row, uint *cur_row) override;
199 uint GetMaxHelpHeight(int maxw) override { return SettingsContainer::GetMaxHelpHeight(maxw); }
200
201 bool UpdateFilterState(SettingFilter &filter, bool force_visible) override;
202
203 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;
204
205protected:
206 void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override;
207};
208
210const void *ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc *sd);
211
212#endif /* SETTINGENTRY_GUI_H */
213
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.
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
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 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.
virtual void Init(uint8_t level=0)
Initialization of a setting entry.
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.
void Init(uint8_t level=0) override
Initialization of a setting entry.
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.
void Init(uint8_t level=0)
Initialization of an entire setting page.
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.
void Init(uint8_t level=0) override
Initialization of a setting entry.
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.