Debian Codegraph @

Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <algorithm>
7 #include <atomic>
8 #include <functional>
9 #include <mutex>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 #include "Common/CommonTypes.h"
15 #include "Common/IniFile.h"
16 #include "InputCommon/ControlReference/ControlReference.h"
17 #include "InputCommon/ControllerInterface/CoreDevice.h"
18
19 namespace ControllerEmu
20 {
21 enum class SettingType
22 {
23 Int,
24 Double,
25 Bool,
26 };
27
28 enum class SettingVisibility
29 {
30 Normal,
31 Advanced,
32 };
33
34 struct NumericSettingDetails
35 {
36 NumericSettingDetails(const char* const _ini_name, const char* const _ui_suffix = nullptr,
37 const char* const _ui_description = nullptr,
38 const char* const _ui_name = nullptr,
39 SettingVisibility _visibility = SettingVisibility::Normal)
40 : ini_name(_ini_name), ui_suffix(_ui_suffix), ui_description(_ui_description),
41 ui_name(_ui_name ? _ui_name : _ini_name), visibility(_visibility)
42 {
43 }
44
45 // The name used in ini files.
46 const char* const ini_name;
47
48 // A string applied to the number in the UI (unit of measure).
49 const char* const ui_suffix;
50
51 // Detailed description of the setting.
52 const char* const ui_description;
53
54 // The name used in the UI (if different from ini file).
55 const char* const ui_name;
56
57 // Advanced settings should be harder to change in the UI. They might confuse users.
58 const SettingVisibility visibility;
59 };
60
61 class NumericSettingBase
62 {
63 public:
64 NumericSettingBase(const NumericSettingDetails& details);
65
66 virtual ~NumericSettingBase() = default;
67
68 virtual void LoadFromIni(const Common::IniFile::Section& section,
69 const std::string& group_name) = 0;
70 virtual void SaveToIni(Common::IniFile::Section& section,
71 const std::string& group_name) const = 0;
72
73 virtual InputReference& GetInputReference() = 0;
74 virtual const InputReference& GetInputReference() const = 0;
75
76 virtual bool IsSimpleValue() const = 0;
77
78 // Convert a literal expression e.g. "7.0" to a regular value. (disables expression parsing)
79 virtual void SimplifyIfPossible() = 0;
80
81 // Convert a regular value to an expression. (used before expression editing)
82 virtual void SetExpressionFromValue() = 0;
83
84 virtual SettingType GetType() const = 0;
85
86 virtual void SetToDefault() = 0;
87
88 const char* GetININame() const;
89 const char* GetUIName() const;
90 const char* GetUISuffix() const;
91 const char* GetUIDescription() const;
92 SettingVisibility GetVisibility() const;
93
94 protected:
95 NumericSettingDetails m_details;
96 };
97
98 template <typename T>
99 class SettingValue;
100
101 template <typename T>
102 class NumericSetting final : public NumericSettingBase
103 {
104 public:
105 using ValueType = T;
106
107 static_assert(std::is_same<ValueType, int>() || std::is_same<ValueType, double>() ||
108 std::is_same<ValueType, bool>(),
109 "NumericSetting is only implemented for int, double, and bool.");
110
111 NumericSetting(SettingValue<ValueType>* value, const NumericSettingDetails& details,
112 ValueType default_value, ValueType min_value, ValueType max_value)
113 : NumericSettingBase(details), m_value(*value), m_default_value(default_value),
114 m_min_value(min_value), m_max_value(max_value)
115 {
116 SetToDefault();
117 }
118
119 void SetToDefault() override { m_value.SetValue(m_default_value); }
120
121 void LoadFromIni(const Common::IniFile::Section& section, const std::string& group_name) override
122 {
123 std::string str_value;
124 if (section.Get(group_name + m_details.ini_name, &str_value))
125 {
126 m_value.m_input.SetExpression(std::move(str_value));
127 SimplifyIfPossible();
128 }
129 else
130 {
131 SetValue(m_default_value);
132 }
133 }
134
135 void SaveToIni(Common::IniFile::Section& section, const std::string& group_name) const override
136 {
137 if (IsSimpleValue())
138 {
139 section.Set(group_name + m_details.ini_name, GetValue(), m_default_value);
140 }
141 else
142 {
143 // We can't save line breaks in a single line config. Restoring them is too complicated.
144 std::string expression = m_value.m_input.GetExpression();
145 ReplaceBreaksWithSpaces(expression);
146 section.Set(group_name + m_details.ini_name, expression, "");
147 }
148 }
149
150 bool IsSimpleValue() const override { return m_value.IsSimpleValue(); }
151 void SimplifyIfPossible() override;
152 void SetExpressionFromValue() override;
153 InputReference& GetInputReference() override { return m_value.m_input; }
154 const InputReference& GetInputReference() const override { return m_value.m_input; }
155
156 ValueType GetValue() const { return m_value.GetValue(); }
157 void SetValue(ValueType value) { m_value.SetValue(value); }
158
159 ValueType GetDefaultValue() const { return m_default_value; }
160 ValueType GetMinValue() const { return m_min_value; }
161 ValueType GetMaxValue() const { return m_max_value; }
162
163 SettingType GetType() const override;
164
165 private:
166 SettingValue<ValueType>& m_value;
167
168 const ValueType m_default_value;
169 const ValueType m_min_value;
170 const ValueType m_max_value;
171 };
172
173 template <typename T>
174 class SettingValue
175 {
176 using ValueType = T;
177
178 friend class NumericSetting<T>;
179
180 public:
181 virtual ~SettingValue() = default;
182
183 virtual ValueType GetValue() const
184 {
185 // Only update dynamic values when the input gate is enabled.
186 // Otherwise settings will all change to 0 when window focus is lost.
187 // This is very undesirable for things like battery level or attached extension.
188 if (!IsSimpleValue() && ControlReference::GetInputGate())
189 m_value = m_input.GetState<ValueType>();
190
191 return m_value;
192 }
193
194 ValueType GetCachedValue() const { return m_value; }
195
196 bool IsSimpleValue() const { return m_input.GetExpression().empty(); }
197
198 virtual void SetValue(const ValueType value)
199 {
200 m_value = value;
201
202 // Clear the expression to use our new "simple" value.
203 m_input.SetExpression("");
204 }
205
206 private:
207 // Values are R/W by both UI and CPU threads.
208 mutable std::atomic<ValueType> m_value = {};
209
210 // Unfortunately InputReference's state grabbing is non-const requiring mutable here.
211 mutable InputReference m_input;
212 };
213
214 template <typename ValueType>
215 class SubscribableSettingValue final : public SettingValue<ValueType>
216 {
217 public:
218 using Base = SettingValue<ValueType>;
219
220 ValueType GetValue() const override
221 {
222 const ValueType cached_value = GetCachedValue();
223 if (IsSimpleValue())
224 return cached_value;
225
226 const ValueType updated_value = Base::GetValue();
227 if (updated_value != cached_value)
228 TriggerCallbacks();
229
230 return updated_value;
231 }
232
233 void SetValue(const ValueType value) override
234 {
235 if (value != GetCachedValue())
236 {
237 Base::SetValue(value);
238 TriggerCallbacks();
239 }
240 else if (!IsSimpleValue())
241 {
242 // The setting has an expression with a cached value equal to the one currently being set.
243 // Don't trigger the callbacks (since the value didn't change), but clear the expression and
244 // make the setting a simple value instead.
245 Base::SetValue(value);
246 }
247 }
248
249 ValueType GetCachedValue() const { return Base::GetCachedValue(); }
250 bool IsSimpleValue() const { return Base::IsSimpleValue(); }
251
252 using SettingChangedCallback = std::function<void(ValueType)>;
253
254 int AddCallback(const SettingChangedCallback& callback)
255 {
256 std::lock_guard lock(m_mutex);
257 const int callback_id = m_next_callback_id;
258 ++m_next_callback_id;
259 m_callback_pairs.emplace_back(callback_id, callback);
260
261 return callback_id;
262 }
263
264 void RemoveCallback(const int id)
265 {
266 std::lock_guard lock(m_mutex);
267 const auto iter = std::ranges::find(m_callback_pairs, id, &IDCallbackPair::first);
268 if (iter != m_callback_pairs.end())
269 m_callback_pairs.erase(iter);
270 }
271
272 private:
273 void TriggerCallbacks() const
274 {
275 std::lock_guard lock(m_mutex);
276 const ValueType value = Base::GetValue();
277 for (const auto& pair : m_callback_pairs)
278 pair.second(value);
279 }
280
281 using IDCallbackPair = std::pair<int, SettingChangedCallback>;
282 std::vector<IDCallbackPair> m_callback_pairs;
283 int m_next_callback_id = 0;
284
285 mutable std::mutex m_mutex;
286 };
287
288 } // namespace ControllerEmu