OpenTTD Source: src/settingentry_gui.h Source File

OpenTTD Source 15.0-RC2

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
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
21 enum class SettingEntryFlag : uint8_t {
22 LeftDepressed,
23 RightDepressed,
24 LastField,
25 Filtered,
26};
27using SettingEntryFlags = EnumBitSet<SettingEntryFlag, uint8_t>;
28
29 static constexpr SettingEntryFlags SEF_BUTTONS_MASK = {SettingEntryFlag::LeftDepressed, SettingEntryFlag::RightDepressed};
30
32 enum RestrictionMode : uint8_t {
33 RM_BASIC,
34 RM_ADVANCED,
35 RM_ALL,
36 RM_CHANGED_AGAINST_DEFAULT,
37 RM_CHANGED_AGAINST_NEW,
38 RM_END,
39};
40 DECLARE_INCREMENT_DECREMENT_OPERATORS(RestrictionMode)
41
42
43struct SettingFilter {
44 StringFilter string;
45 RestrictionMode min_cat;
46 bool type_hides;
47 RestrictionMode mode;
48 SettingType type;
49};
50
52 struct BaseSettingEntry {
53 SettingEntryFlags flags;
54 uint8_t level;
55
56 BaseSettingEntry() : flags(), level(0) {}
57 virtual ~BaseSettingEntry() = default;
58
59 virtual void Init(uint8_t level = 0);
60 virtual void FoldAll() {}
61 virtual void UnFoldAll() {}
62 virtual void ResetAll() = 0;
63
68 void SetLastField(bool last_field) { this->flags.Set(SettingEntryFlag::LastField, last_field); }
69
70 virtual uint Length() const = 0;
71 virtual void GetFoldingState([[maybe_unused]] bool &all_folded, [[maybe_unused]] bool &all_unfolded) const {}
72 virtual bool IsVisible(const BaseSettingEntry *item) const;
73 virtual BaseSettingEntry *FindEntry(uint row, uint *cur_row);
74 virtual uint GetMaxHelpHeight([[maybe_unused]] int maxw) { return 0; }
75
80 bool IsFiltered() const { return this->flags.Test(SettingEntryFlag::Filtered); }
81
82 virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible) = 0;
83
84 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;
85
86 static inline Dimension circle_size;
87 static inline int line_height;
88
89protected:
90 virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const = 0;
91};
92
94 struct SettingEntry : BaseSettingEntry {
95 const std::string_view name;
96 const IntSettingDesc *setting = nullptr;
97
98 SettingEntry(std::string_view name) : name(name) {}
99
100 void Init(uint8_t level = 0) override;
101 void ResetAll() override;
102 uint Length() const override;
103 uint GetMaxHelpHeight(int maxw) override;
104 bool UpdateFilterState(SettingFilter &filter, bool force_visible) override;
105
106 void SetButtons(SettingEntryFlags new_val);
107
108protected:
109 void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override;
110
111private:
112 bool IsVisibleByRestrictionMode(RestrictionMode mode) const;
113};
114
116 struct SettingsContainer {
117 typedef std::vector<BaseSettingEntry*> EntryVector;
118 EntryVector entries;
119
120 template <typename T>
121 T *Add(T *item)
122 {
123 this->entries.push_back(item);
124 return item;
125 }
126
127 void Init(uint8_t level = 0);
128 void ResetAll();
129 void FoldAll();
130 void UnFoldAll();
131
132 uint Length() const;
133 void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
134 bool IsVisible(const BaseSettingEntry *item) const;
135 BaseSettingEntry *FindEntry(uint row, uint *cur_row);
136 uint GetMaxHelpHeight(int maxw);
137
138 bool UpdateFilterState(SettingFilter &filter, bool force_visible);
139
140 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;
141};
142
144 struct SettingsPage : BaseSettingEntry, SettingsContainer {
145 StringID title;
146 bool folded;
147
148 SettingsPage(StringID title);
149
150 void Init(uint8_t level = 0) override;
151 void ResetAll() override;
152 void FoldAll() override;
153 void UnFoldAll() override;
154
155 uint Length() const override;
156 void GetFoldingState(bool &all_folded, bool &all_unfolded) const override;
157 bool IsVisible(const BaseSettingEntry *item) const override;
158 BaseSettingEntry *FindEntry(uint row, uint *cur_row) override;
159 uint GetMaxHelpHeight(int maxw) override { return SettingsContainer::GetMaxHelpHeight(maxw); }
160
161 bool UpdateFilterState(SettingFilter &filter, bool force_visible) override;
162
163 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;
164
165protected:
166 void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override;
167};
168
169 SettingsContainer &GetSettingsTree();
170const void *ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc *sd);
171
172#endif /* SETTINGENTRY_GUI_H */
173
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
EnumBitSet< SettingEntryFlag, uint8_t >
enum_type.hpp
Type (helpers) for enums.
DECLARE_INCREMENT_DECREMENT_OPERATORS
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:63
RestrictionMode
RestrictionMode
How the list of advanced settings is filtered.
Definition settingentry_gui.h:32
RM_CHANGED_AGAINST_DEFAULT
@ RM_CHANGED_AGAINST_DEFAULT
Show only settings which are different compared to default values.
Definition settingentry_gui.h:36
RM_ADVANCED
@ RM_ADVANCED
Display settings associated to the "advanced" list.
Definition settingentry_gui.h:34
RM_ALL
@ RM_ALL
List all settings regardless of the default/newgame/... values.
Definition settingentry_gui.h:35
RM_CHANGED_AGAINST_NEW
@ RM_CHANGED_AGAINST_NEW
Show only settings which are different compared to the user's new game setting values.
Definition settingentry_gui.h:37
RM_END
@ RM_END
End for iteration.
Definition settingentry_gui.h:38
RM_BASIC
@ RM_BASIC
Display settings associated to the "basic" list.
Definition settingentry_gui.h:33
SEF_BUTTONS_MASK
static constexpr SettingEntryFlags SEF_BUTTONS_MASK
Mask for button flags.
Definition settingentry_gui.h:29
SettingEntryFlag
SettingEntryFlag
Flags for SettingEntry.
Definition settingentry_gui.h:21
SettingEntryFlag::LeftDepressed
@ LeftDepressed
Of a numeric setting entry, the left button is depressed.
SettingEntryFlag::LastField
@ LastField
This entry is the last one in a (sub-)page.
SettingEntryFlag::RightDepressed
@ RightDepressed
Of a numeric setting entry, the right button is depressed.
SettingEntryFlag::Filtered
@ Filtered
Entry is hidden by the string filter.
GetSettingsTree
SettingsContainer & GetSettingsTree()
Construct settings tree.
Definition settingentry_gui.cpp:619
settings_internal.h
Functions and types used internally for the settings configurations.
SettingType
SettingType
Type of settings for filtering.
Definition settings_internal.h:60
stringfilter_type.h
Searching and filtering using a stringterm.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
BaseSettingEntry
Data structure describing a single setting in a tab.
Definition settingentry_gui.h:52
BaseSettingEntry::SetLastField
void SetLastField(bool last_field)
Set whether this is the last visible entry of the parent node.
Definition settingentry_gui.h:68
BaseSettingEntry::IsFiltered
bool IsFiltered() const
Check whether an entry is hidden due to filters.
Definition settingentry_gui.h:80
BaseSettingEntry::FindEntry
virtual BaseSettingEntry * FindEntry(uint row, uint *cur_row)
Find setting entry at row row_num.
Definition settingentry_gui.cpp:54
BaseSettingEntry::circle_size
static Dimension circle_size
Dimension of the circle +/- icon.
Definition settingentry_gui.h:86
BaseSettingEntry::Init
virtual void Init(uint8_t level=0)
Initialization of a setting entry.
Definition settingentry_gui.cpp:31
BaseSettingEntry::line_height
static int line_height
Height of a single setting.
Definition settingentry_gui.h:87
BaseSettingEntry::Draw
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.
Definition settingentry_gui.cpp:91
BaseSettingEntry::flags
SettingEntryFlags flags
Flags of the setting entry.
Definition settingentry_gui.h:53
BaseSettingEntry::IsVisible
virtual bool IsVisible(const BaseSettingEntry *item) const
Check whether an entry is visible and not folded or filtered away.
Definition settingentry_gui.cpp:42
BaseSettingEntry::level
uint8_t level
Nesting level of this setting entry.
Definition settingentry_gui.h:54
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
GameSettings
All settings together for the game.
Definition settings_type.h:632
IntSettingDesc
Base integer type, including boolean, settings.
Definition settings_internal.h:148
SettingEntry
Standard setting.
Definition settingentry_gui.h:94
SettingEntry::Length
uint Length() const override
Return number of rows needed to display the (filtered) entry.
Definition settingentry_gui.cpp:156
SettingEntry::SetButtons
void SetButtons(SettingEntryFlags new_val)
Set the button-depressed flags (#SettingsEntryFlag::LeftDepressed and #SettingsEntryFlag::RightDepres...
Definition settingentry_gui.cpp:148
SettingEntry::DrawSetting
void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override
Function to draw setting value (button + text + current value)
Definition settingentry_gui.cpp:268
SettingEntry::IsVisibleByRestrictionMode
bool IsVisibleByRestrictionMode(RestrictionMode mode) const
Checks whether an entry shall be made visible based on the restriction mode.
Definition settingentry_gui.cpp:176
SettingEntry::name
const std::string_view name
Name of the setting.
Definition settingentry_gui.h:95
SettingEntry::GetMaxHelpHeight
uint GetMaxHelpHeight(int maxw) override
Get the biggest height of the help text(s), if the width is at least maxw.
Definition settingentry_gui.cpp:166
SettingEntry::Init
void Init(uint8_t level=0) override
Initialization of a setting entry.
Definition settingentry_gui.cpp:131
SettingEntry::setting
const IntSettingDesc * setting
Setting description of the setting.
Definition settingentry_gui.h:96
SettingEntry::UpdateFilterState
bool UpdateFilterState(SettingFilter &filter, bool force_visible) override
Update the filter state.
Definition settingentry_gui.cpp:217
SettingFilter
Filter for settings list.
Definition settingentry_gui.h:43
SettingFilter::type
SettingType type
Filter based on type.
Definition settingentry_gui.h:48
SettingFilter::type_hides
bool type_hides
Whether the type hides filtered strings.
Definition settingentry_gui.h:46
SettingFilter::mode
RestrictionMode mode
Filter based on category.
Definition settingentry_gui.h:47
SettingFilter::min_cat
RestrictionMode min_cat
Minimum category needed to display all filtered strings (RM_BASIC, RM_ADVANCED, or RM_ALL).
Definition settingentry_gui.h:45
SettingFilter::string
StringFilter string
Filter string.
Definition settingentry_gui.h:44
SettingsContainer
Containers for BaseSettingEntry.
Definition settingentry_gui.h:116
SettingsContainer::UpdateFilterState
bool UpdateFilterState(SettingFilter &filter, bool force_visible)
Update the filter state.
Definition settingentry_gui.cpp:354
SettingsContainer::GetFoldingState
void GetFoldingState(bool &all_folded, bool &all_unfolded) const
Recursively accumulate the folding state of the tree.
Definition settingentry_gui.cpp:341
SettingsContainer::GetMaxHelpHeight
uint GetMaxHelpHeight(int maxw)
Get the biggest height of the help texts, if the width is at least maxw.
Definition settingentry_gui.cpp:414
SettingsContainer::ResetAll
void ResetAll()
Resets all settings to their default values.
Definition settingentry_gui.cpp:313
SettingsContainer::IsVisible
bool IsVisible(const BaseSettingEntry *item) const
Check whether an entry is visible and not folded or filtered away.
Definition settingentry_gui.cpp:373
SettingsContainer::FoldAll
void FoldAll()
Recursively close all folds of sub-pages.
Definition settingentry_gui.cpp:321
SettingsContainer::UnFoldAll
void UnFoldAll()
Recursively open all folds of sub-pages.
Definition settingentry_gui.cpp:329
SettingsContainer::FindEntry
BaseSettingEntry * FindEntry(uint row, uint *cur_row)
Find the setting entry at row number row_num.
Definition settingentry_gui.cpp:397
SettingsContainer::entries
EntryVector entries
Settings on this page.
Definition settingentry_gui.h:118
SettingsContainer::Length
uint Length() const
Return number of rows needed to display the whole page.
Definition settingentry_gui.cpp:382
SettingsContainer::Draw
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.
Definition settingentry_gui.cpp:438
SettingsContainer::Init
void Init(uint8_t level=0)
Initialization of an entire setting page.
Definition settingentry_gui.cpp:305
SettingsPage
Data structure describing one page of settings in the settings window.
Definition settingentry_gui.h:144
SettingsPage::title
StringID title
Title of the sub-page.
Definition settingentry_gui.h:145
SettingsPage::IsVisible
bool IsVisible(const BaseSettingEntry *item) const override
Check whether an entry is visible and not folded or filtered away.
Definition settingentry_gui.cpp:538
SettingsPage::UnFoldAll
void UnFoldAll() override
Recursively open all (filtered) folds of sub-pages.
Definition settingentry_gui.cpp:487
SettingsPage::FoldAll
void FoldAll() override
Recursively close all (filtered) folds of sub-pages.
Definition settingentry_gui.cpp:478
SettingsPage::Length
uint Length() const override
Return number of rows needed to display the (filtered) entry.
Definition settingentry_gui.cpp:548
SettingsPage::FindEntry
BaseSettingEntry * FindEntry(uint row, uint *cur_row) override
Find setting entry at row row_num.
Definition settingentry_gui.cpp:562
SettingsPage::Draw
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.
Definition settingentry_gui.cpp:586
SettingsPage::Init
void Init(uint8_t level=0) override
Initialization of an entire setting page.
Definition settingentry_gui.cpp:463
SettingsPage::GetFoldingState
void GetFoldingState(bool &all_folded, bool &all_unfolded) const override
Recursively accumulate the folding state of the (filtered) tree.
Definition settingentry_gui.cpp:500
SettingsPage::ResetAll
void ResetAll() override
Resets all settings to their default values.
Definition settingentry_gui.cpp:470
SettingsPage::folded
bool folded
Sub-page is folded (not visible except for its title)
Definition settingentry_gui.h:146
SettingsPage::UpdateFilterState
bool UpdateFilterState(SettingFilter &filter, bool force_visible) override
Update the filter state.
Definition settingentry_gui.cpp:519
SettingsPage::DrawSetting
void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const override
Function to draw setting value (button + text + current value)
Definition settingentry_gui.cpp:611
StringFilter
String filter and state.
Definition stringfilter_type.h:30