forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_param.h
445 lines (380 loc) · 11.9 KB
/
config_param.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player 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, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EP_CONFIG_PARAM_H
#define EP_CONFIG_PARAM_H
#include "string_view.h"
#include <array>
#include <cstdint>
#include <lcf/inireader.h>
#include <limits>
#include <string>
#include <vector>
#include <algorithm>
#include <type_traits>
#include <lcf/flag_set.h>
namespace {
inline const std::string& ParamValueToString(const std::string& s) {
return s;
}
inline std::string ParamValueToString(StringView s) {
return ToString(s);
}
inline std::string ParamValueToString(int i) {
return std::to_string(i);
}
}
/**
* Base class for all ConfigParams
*/
template <typename T>
class ConfigParamBase {
public:
using value_type = T;
/**
* @param name Name shown in the settings scene
* @param description Description shown in the help window of the settings scene
* @param value Initial value
*/
ConfigParamBase(StringView name, StringView description, StringView config_section, StringView config_key, T value) :
_name(name), _description(description), _config_section(config_section), _config_key(config_key), _value(value) {}
/** @return current assigned value */
T Get() const { return _value; }
/**
* Sets the value.
* Setting fails when the option is locked or invisible or the value is not
* in a supported range of the option.
*
* @param value Value to set to
* @return Whether the setting succeeded. When it fails the old value remains.
*/
bool Set(const T& value) {
if (IsLocked()) {
return false;
}
if (IsValid(value)) {
_value = std::move(value);
return true;
}
return false;
}
/**
* Check whether a value is acceptable for this setting.
* This function is used for internal purposes.
*
* @see Set
* @param value Value to set to
* @return Whether the value is okay
*/
bool IsValid(const T& value) const {
if (!IsOptionVisible()) {
return false;
}
if (IsLocked()) {
return value == this->_value;
}
return vIsValid(value);
}
/** @return Whether the option is displayed and supported */
bool IsOptionVisible() const {
return _visible;
}
/**
* Sets the visibility of the option in the settings scene.
* Additionally this indicates wheter the setting is supported at all.
* When not visible all write operations to the setting will fail.
*
* @see Lock To indicate that the setting is supported but currently constant
* @param visible Turns visibility on/off
*/
void SetOptionVisible(bool visible) {
_visible = visible;
}
/** @return Whether the option is currently locked and cannot be altered */
bool IsLocked() const {
return _locked;
}
/**
* Locks the option. A locked option cannot be altered.
* Use this when the option is supported but cannot be altered currently due
* to a dependency on another option.
*
* @param value Value to lock the option with
* @return Whether the locking succeeded. When it fails the old value remains.
*/
bool Lock(T value) {
_locked = false;
if (!Set(value)) {
_locked = true;
return false;
}
_locked = true;
return true;
}
/**
* Locks or unlocks the option. The current value stays.
*
* @see Lock
* @param locked turn locking on/off
*/
void SetLocked(bool locked) {
_locked = locked;
}
/** @return name displayed in the settings scene */
StringView GetName() const {
return _name;
}
/**
* @param name new name of the setting
*/
void SetName(StringView name) {
_name = name;
}
/** @return help text displayed in the settings scene */
StringView GetDescription() const {
return _description;
}
/**
* @param description new description of the setting
*/
void SetDescription(StringView description) {
_description = description;
}
/** @return human readable representation of the value for the settings scene */
virtual std::string ValueToString() const = 0;
template <typename U = T, typename std::enable_if<std::is_same<U, std::string>::value, int>::type = 0>
bool FromIni(const lcf::INIReader& ini) {
// FIXME: Migrate IniReader to StringView (or std::string_view with C++17)
if (ini.HasValue(ToString(_config_section), ToString(_config_key))) {
Set(ini.GetString(ToString(_config_section), ToString(_config_key), T()));
return true;
}
return false;
}
template <typename U = T, typename std::enable_if<std::is_same<U, int>::value, int>::type = 0>
bool FromIni(const lcf::INIReader& ini) {
if (ini.HasValue(ToString(_config_section), ToString(_config_key))) {
Set(ini.GetInteger(ToString(_config_section), ToString(_config_key), T()));
return true;
}
return false;
}
template <typename U = T, typename std::enable_if<std::is_same<U, bool>::value, int>::type = 0>
bool FromIni(const lcf::INIReader& ini) {
if (ini.HasValue(ToString(_config_section), ToString(_config_key))) {
Set(ini.GetBoolean(ToString(_config_section), ToString(_config_key), T()));
return true;
}
return false;
}
template <typename U = T, typename std::enable_if<!std::is_enum<U>::value, int>::type = 0>
void ToIni(std::ostream& ini) const {
if (IsOptionVisible()) {
ini << _config_key << '=' << Get() << "\n";
}
}
protected:
virtual bool vIsValid(const T& value) const = 0;
StringView _name;
StringView _description;
StringView _config_section;
StringView _config_key;
T _value = {};
private:
bool _visible = true;
bool _locked = false;
};
/** A configuration parameter with no restrictions */
template <typename T>
class ConfigParam : public ConfigParamBase<T> {
public:
explicit ConfigParam(StringView name, StringView description, StringView config_section, StringView config_key, T value = {}) :
ConfigParamBase<T>(name, description, config_section, config_key, std::move(value)) {}
bool vIsValid(const T&) const override {
return true;
}
std::string ValueToString() const override {
return ParamValueToString(this->Get());
}
};
/** A configuration parameter which is locked by default */
template <typename T>
class LockedConfigParam final : public ConfigParam<T> {
public:
explicit LockedConfigParam(StringView name, StringView description, T value = {}) :
ConfigParam<T>(name, description, "", "", value) {
this->Lock(value);
}
};
using StringConfigParam = ConfigParam<std::string>;
/** A configuration parameter with a range */
template <typename T>
class RangeConfigParam : public ConfigParamBase<T> {
public:
/** Construct with name and initial value */
explicit RangeConfigParam(StringView name, StringView description, StringView config_section, StringView config_key, T value = {}) :
ConfigParamBase<T>(name, description, config_section, config_key, std::move(value)) {}
/** Construct with name and initial value, min, and max */
RangeConfigParam(StringView name, StringView description, StringView config_section, StringView config_key, T value, T minval, T maxval) :
ConfigParamBase<T>(name, description, config_section, config_key, std::move(value)) { SetRange(minval, maxval); }
/**
* Check if a value is valid
* @param value the value to check
* @return true if this value can be set
*/
bool vIsValid(const T& value) const override {
return value >= _min && value <= _max;
}
T GetMin() const {
return _min;
}
T GetMax() const {
return _max;
}
/**
* Set minimum allowed value.
* @param minval the new minimum
* @post If the current value is < minval, it will be set equal to minval
* @post If the current maximum < minval, this parameter is disabled.
*/
void SetMin(T minval) { SetRange(minval, _max); }
/**
* Set maximum allowed value.
* @param maxval the new maximum
* @post If the current value is > maxval, it will be set equal to maxval
* @post If the current minimum > maxval, this parameter is disabled.
*/
void SetMax(T maxval) { SetRange(_min, maxval); }
/**
* Set allowed range of values.
* @param minval the new maximum
* @param maxval the new maximum
* @post If the current value is outside the range, it will be clamped.
* @post If the minval > maxval, this parameter is disabled.
*/
void SetRange(T minval, T maxval) {
this->SetOptionVisible(true);
_min = minval;
_max = maxval;
this->_value = (this->_value < _min) ? _min : this->_value;
this->_value = (this->_value > _max) ? _max : this->_value;
}
std::string ValueToString() const override {
return ParamValueToString(this->_value);
}
private:
T _min = std::numeric_limits<T>::min();
T _max = std::numeric_limits<T>::max();
};
/** A boolean configuration parameter */
class BoolConfigParam : public ConfigParamBase<bool> {
public:
explicit BoolConfigParam(StringView name, StringView description, StringView config_section, StringView config_key, bool value) :
ConfigParamBase<bool>(name, description, config_section, config_key, value) {}
bool vIsValid(const bool&) const override {
return true;
}
void Toggle() {
if (Get()) {
Set(false);
} else {
Set(true);
}
}
std::string ValueToString() const override {
return Get() ? "[ON]" : "[OFF]";
}
};
template <typename E, size_t S>
class EnumConfigParam : public ConfigParamBase<E> {
public:
EnumConfigParam(StringView name, StringView description, StringView config_section, StringView config_key, E value, std::array<StringView, S> values, std::array<StringView, S> tags, std::array<StringView, S> value_descriptions) :
ConfigParamBase<E>(name, description, config_section, config_key, value), _values{ values }, _tags{ tags}, _value_descriptions{ value_descriptions } {
for (size_t i = 0; i < S; ++i) {
_valid[static_cast<E>(i)] = true;
}
}
bool vIsValid(const E& value) const override {
return _valid[value];
}
void ReplaceValidSet(lcf::FlagSet<E> valid) {
_valid = std::move(valid);
if (this->IsOptionVisible() && !this->IsValid(this->_value)) {
this->_value = GetFirstValid();
}
}
void AddToValidSet(E value) {
_valid[value] = true;
}
void RemoveFromValidSet(const E& value) {
_valid[value] = false;
if (this->IsOptionVisible() && !this->IsValid(this->_value)) {
this->_value = GetFirstValid();
}
}
std::string ValueToString() const override {
return ToString(_values[static_cast<int>(this->_value)]);
}
std::array<StringView, S> GetValues() const {
return _values;
}
std::array<StringView, S> GetDescriptions() const {
return _value_descriptions;
}
bool SetFromString(StringView value) {
for (size_t i = 0; i < _tags.size(); ++i) {
if (value == _tags[i]) {
this->Set(static_cast<E>(i));
return true;
}
}
return false;
}
template <typename U = E, typename std::enable_if<std::is_same<U, E>::value, int>::type = 0>
bool FromIni(const lcf::INIReader& ini) {
if (ini.HasValue(ToString(this->_config_section), ToString(this->_config_key))) {
std::string s = ini.GetString(ToString(this->_config_section), ToString(this->_config_key), std::string());
for (size_t i = 0; i < _tags.size(); ++i) {
if (s == _tags[i]) {
this->Set(static_cast<E>(i));
return true;
}
}
}
return false;
}
void ToIni(std::ostream& ini) const {
if (this->IsOptionVisible()) {
ini << this->_config_key << '=' << _tags[static_cast<int>(this->Get())] << "\n";
}
}
private:
lcf::FlagSet<E> _valid;
std::array<StringView, S> _values;
std::array<StringView, S> _tags;
std::array<StringView, S> _value_descriptions;
bool _enabled = true;
E GetFirstValid() const {
for (size_t i = 0; i < _valid.size(); ++i) {
auto e = static_cast<E>(i);
if (_valid[e]) {
return e;
}
}
return E{};
}
};
#endif