ngscopeclient v0.3
Loading...
Searching...
No Matches
Preference.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* ngscopeclient *
4* *
5* Copyright (c) 2012-2026 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
36#ifndef Preference_h
37#define Preference_h
38
39#include <algorithm>
40#include <string>
41#include <type_traits>
42#include <utility>
43#include <type_traits>
44#include <cstdint>
45#include <set>
46#include <vector>
47
48#include <imgui.h>
49
50#include "Unit.h"
51#include "FontManager.h"
52#include "PreferenceTypes.h"
53
54enum class PreferenceType
55{
56 Boolean,
57 String,
58 Real,
59 ThemedColor,
60 Enum,
61 Font,
62 Int,
63 None // Only for moved-from values
64};
65
66namespace impl
67{
68 class PreferenceBuilder;
69
70 struct Color
71 {
72 Color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a)
73 : m_r{r}, m_g{g}, m_b{b}, m_a{a}
74 {
75
76 }
77
78 std::uint8_t m_r, m_g, m_b, m_a;
79
80 ImU32 GetColor() const
81 {
82 return
83 (m_b << IM_COL32_B_SHIFT) |
84 (m_g << IM_COL32_G_SHIFT) |
85 (m_r << IM_COL32_R_SHIFT) |
86 (m_a << IM_COL32_A_SHIFT);
87 }
88 };
89
91 {
92 //for now take 3 explicit colors
94 : m_colors{cLight, cDark, cClassic}
95 {
96 }
97
98 //or two making dark and classic the same
100 : m_colors{cLight, cDark, cDark}
101 {
102 }
103
104 Color m_colors[THEME_COUNT];
105 };
106}
107
109{
110 using base_type = std::int64_t;
111
112 public:
114 {
115
116 }
117
118 public:
119 void AddEnumMember(const std::string& name, base_type value);
120 const std::string& GetName(base_type value) const;
121 base_type GetValue(const std::string& name) const;
122 const std::vector<std::string>& GetNames() const;
123 bool HasNameFor(base_type value) const;
124 bool HasValueFor(const std::string& name) const;
125
126 public:
127 template<
128 typename E/*,
129 typename = typename std::enable_if<
130 std::is_convertible<E, base_type>::value &&
131 not std::is_same<E, base_type>::value
132 >::type*/
133 >
134 void AddEnumMember(const std::string& name, E value)
135 {
136 const auto val = static_cast<base_type>(value);
137 this->AddEnumMember(name, val);
138 }
139
140 protected:
141 std::map<std::string, base_type> m_forwardMap;
142 std::map<base_type, std::string> m_backwardMap;
143 std::vector<std::string> m_names;
144};
145
147{
148 friend class impl::PreferenceBuilder;
149
150private:
151 using PreferenceValue =
152 typename std::aligned_union<
153 std::max(
154 {
155 sizeof(std::int64_t),
156 sizeof(impl::ThemedColor),
157 sizeof(bool),
158 sizeof(double),
159 sizeof(std::string),
160 sizeof(FontDescription)
161 }),
162 bool,
163 std::string,
164 double,
167 std::int64_t,
168 FontDescription
169 >::type;
170
171private:
172 std::string m_identifier;
173 std::string m_label;
174 std::string m_description;
175 PreferenceType m_type;
176 PreferenceValue m_value;
177 PreferenceValue m_defaultValue;
178 bool m_isVisible{true};
179 Unit m_unit{Unit::UNIT_COUNTS};
180 bool m_hasValue{false};
181 EnumMapping m_mapping;
182
183public:
184 Preference(PreferenceType type, std::string identifier)
185 : m_identifier{std::move(identifier)}, m_type{type}
186 {
187
188 }
189
191 {
192 CleanUp();
193 }
194
195public:
196 Preference(const Preference&) = delete;
197 Preference(Preference&& other)
198 {
199 MoveFrom(other);
200 }
201
202 Preference& operator=(const Preference&) = delete;
203 Preference& operator=(Preference&& other)
204 {
205 CleanUp();
206 MoveFrom(other);
207 return *this;
208 }
209
210public:
211 const std::string& GetIdentifier() const;
212 const std::string& GetLabel() const;
213 const std::string& GetDescription() const;
214 void SetFont(const FontDescription& font);
215 FontDescription GetFont() const;
216 std::int64_t GetEnumRaw() const;
217 void SetEnumRaw(std::int64_t value);
218 PreferenceType GetType() const;
219 bool GetBool() const;
220 double GetReal() const;
221 std::int64_t GetInt() const;
222 const std::string& GetString() const;
223 std::string ToString() const;
224 bool GetIsVisible() const;
225 const impl::ThemedColor& GetThemedColor() const;
226 void SetBool(bool value);
227 void SetReal(double value);
228 void SetInt(std::int64_t value);
229 void SetString(const std::string& value);
230 void SetThemedColor(const impl::ThemedColor& value);
231 void SetThemedColor(size_t i, const impl::Color& value);
232 void SetColor(const ImU32& value);
233 void SetColorRaw(const impl::Color& value);
234 void SetLabel(std::string label);
235 void SetDescription(std::string description);
236 bool HasUnit();
237 void ResetToDefault();
238 Unit& GetUnit();
239 const EnumMapping& GetMapping() const;
240
241 template< typename E >
242 E GetEnum() const
243 {
244 return static_cast<E>(this->GetEnumRaw());
245 }
246
247 template< typename E >
248 void SetEnum(E value)
249 {
250 this->SetEnumRaw(static_cast<std::int64_t>(value));
251 }
252
253public:
254 static impl::PreferenceBuilder Int(std::string identifier, int64_t defaultValue);
255 static impl::PreferenceBuilder Real(std::string identifier, double defaultValue);
256 static impl::PreferenceBuilder Bool(std::string identifier, bool defaultValue);
257 static impl::PreferenceBuilder String(std::string identifier, std::string defaultValue);
258 static impl::PreferenceBuilder ThemedColor(
259 std::string identifier,
261 const ImU32& defaultValueDark);
262 static impl::PreferenceBuilder EnumRaw(std::string identifier, std::int64_t defaultValue);
263 static impl::PreferenceBuilder Font(std::string identifier, FontDescription defaultValue);
264
265 template< typename E >
266 static impl::PreferenceBuilder Enum(std::string identifier, E defaultValue);
267
268
269private:
270 void SetMapping(EnumMapping mapping);
271
272 template<typename T>
273 const T& GetValueRaw() const
274 {
275 return *reinterpret_cast<const T*>(&m_value);
276 }
277
278 template<typename T>
279 T& GetValueRaw()
280 {
281 return *reinterpret_cast<T*>(&m_value);
282 }
283
284 void CleanUp();
285
286 template<typename T>
287 void Construct(T value)
288 {
289 new (&m_value) T(std::move(value));
290 m_hasValue = true;
291 }
292
293 void MoveFrom(Preference& other);
294};
295
296namespace impl
297{
298 class PreferenceBuilder
299 {
300 protected:
301 Preference m_pref;
302
303 public:
304 PreferenceBuilder(Preference&& pref);
305
306 public:
307 PreferenceBuilder Invisible() &&;
308 PreferenceBuilder Label(std::string label) &&;
309 PreferenceBuilder Description(std::string description) &&;
310 PreferenceBuilder Unit(Unit::UnitType type) &&;
311 Preference Build() &&;
312
313 template< typename E >
314 PreferenceBuilder EnumValue(const std::string& name, E value) &&
315 {
316 this->m_pref.m_mapping.AddEnumMember<E>(name, value);
317 return std::move(*this);
318 }
319 };
320}
321
322template< typename E >
323impl::PreferenceBuilder Preference::Enum(std::string identifier, E defaultValue)
324{
325 return EnumRaw(std::move(identifier), static_cast<std::int64_t>(defaultValue));
326}
327
328#endif // Preference_h
Declaration of FontManager.
Enum definitions for preference settings.
Declaration of Unit.
Definition AcceleratorBuffer.h:204
Definition Preference.h:109
Definition Preference.h:147
A unit of measurement, plus conversion to pretty-printed output.
Definition Unit.h:59
Definition Preference.h:71
Definition Preference.h:91