forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.h
373 lines (305 loc) · 13.5 KB
/
options.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
#pragma once
#ifndef CATA_SRC_OPTIONS_H
#define CATA_SRC_OPTIONS_H
#include <functional>
#include <map>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include <tuple>
#include "translations.h"
class JsonIn;
class JsonOut;
class options_manager
{
public:
class id_and_option : public std::pair<std::string, translation>
{
public:
id_and_option( const std::string &first, const std::string &second )
: std::pair<std::string, translation>( first, to_translation( second ) ) {
}
id_and_option( const std::string &first, const translation &second )
: std::pair<std::string, translation>( first, second ) {
}
};
private:
static std::vector<id_and_option> build_tilesets_list();
static std::vector<id_and_option> build_soundpacks_list();
static std::vector<id_and_option> load_tilesets_from(
const std::string &path );
static std::vector<id_and_option> load_soundpack_from(
const std::string &path );
void enable_json( const std::string &var );
void add_retry( const std::string &var, const std::string &val );
std::map<std::string, std::string> post_json_verify;
std::map<std::string, std::pair<std::string, std::map<std::string, std::string> > > mMigrateOption;
friend options_manager &get_options();
options_manager();
void addOptionToPage( const std::string &name, const std::string &page );
void cache_to_globals(); // cache some options to globals due to heavy usage
public:
enum copt_hide_t {
/** Don't hide this option */
COPT_NO_HIDE,
/** Hide this option in SDL build */
COPT_SDL_HIDE,
/** Show this option in SDL builds only */
COPT_CURSES_HIDE,
/** Hide this option in non-Windows Curses builds */
COPT_POSIX_CURSES_HIDE,
/** Hide this option in builds without sound support */
COPT_NO_SOUND_HIDE,
/** Hide this option always, it should not be changed by user directly through UI. **/
COPT_ALWAYS_HIDE
};
class cOpt
{
friend class options_manager;
public:
cOpt();
/**
* Option should be hidden in current build.
* @return true if option should be hidden, false if not.
*/
bool is_hidden() const;
std::string getName() const;
std::string getPage() const;
/// The translated displayed option name.
std::string getMenuText() const;
/// The translated displayed option tool tip.
std::string getTooltip() const;
std::string getType() const;
std::string getValue( bool classis_locale = false ) const;
/// The translated currently selected option value.
std::string getValueName() const;
std::string getDefaultText( bool bTranslated = true ) const;
int getItemPos( const std::string &sSearch ) const;
std::vector<id_and_option> getItems() const;
int getIntPos( int iSearch ) const;
std::optional< std::tuple<int, std::string> > findInt( int iSearch ) const;
int getMaxLength() const;
//set to next item
void setNext();
//set to previous item
void setPrev();
//set value
void setValue( std::string sSetIn );
void setValue( float fSetIn );
void setValue( int iSetIn );
template<typename T>
T value_as() const;
bool operator==( const cOpt &rhs ) const;
bool operator!=( const cOpt &rhs ) const {
return !operator==( rhs );
}
static std::vector<std::string> getPrerequisiteSupportedTypes() {
return { "bool", "string", "string_select", "string_input" };
}
void setPrerequisites( const std::string &sOption, const std::vector<std::string> &sAllowedValues );
void setPrerequisite( const std::string &sOption, const std::string &sAllowedValue = "true" ) {
setPrerequisites( sOption, { sAllowedValue } );
}
std::string getPrerequisite() const;
bool hasPrerequisite() const;
bool checkPrerequisite() const;
enum COPT_VALUE_TYPE {
CVT_UNKNOWN = 0,
CVT_BOOL = 1,
CVT_STRING = 2,
CVT_FLOAT = 3,
CVT_INT = 4,
CVT_VOID = 5
};
private:
std::string sName;
std::string sPage;
// The *untranslated* displayed option name ( short string ).
std::string sMenuText;
// The *untranslated* displayed option tool tip ( longer string ).
std::string sTooltip;
std::string sType;
bool verbose = false;
std::string format;
std::string sPrerequisite;
std::vector<std::string> sPrerequisiteAllowedValues;
copt_hide_t hide;
COPT_VALUE_TYPE eType;
//sType == "string"
std::string sSet;
// first is internal value, second is untranslated text
std::vector<id_and_option> vItems;
std::string sDefault;
int iMaxLength = 0;
//sType == "bool"
bool bSet = false;
bool bDefault = false;
//sType == "int"
int iSet = 0;
int iMin = 0;
int iMax = 0;
int iDefault = 0;
std::vector< std::tuple<int, std::string> > mIntValues;
//sType == "float"
float fSet = 0.0f;
float fMin = 0.0f;
float fMax = 0.0f;
float fDefault = 0.0f;
float fStep = 0.0f;
};
using options_container = std::unordered_map<std::string, cOpt>;
void init();
void add_options_general();
void add_options_interface();
void add_options_graphics();
void add_options_debug();
void add_options_world_default();
void add_options_android();
void load();
bool save();
std::string show( bool ingame = false, bool world_options_only = false,
const std::function<bool()> &on_quit = nullptr );
void add_value( const std::string &lvar, const std::string &lval,
const translation &lvalname );
void serialize( JsonOut &json ) const;
void deserialize( JsonIn &jsin );
std::string migrateOptionName( const std::string &name ) const;
std::string migrateOptionValue( const std::string &name, const std::string &val ) const;
/**
* Returns a copy of the options in the "world default" page. The options have their
* current value, which acts as the default for new worlds.
*/
options_container get_world_defaults() const;
void set_world_options( options_container *options );
/** Check if an option exists? */
bool has_option( const std::string &name ) const;
cOpt &get_option( const std::string &name );
//add hidden external option with value
void add_external( const std::string &sNameIn, const std::string &sPageIn, const std::string &sType,
const std::string &sMenuTextIn, const std::string &sTooltipIn );
//add string select option
void add( const std::string &sNameIn, const std::string &sPageIn,
const std::string &sMenuTextIn, const std::string &sTooltipIn,
// first is option value, second is display name of that value
const std::vector<id_and_option> &sItemsIn, std::string sDefaultIn,
copt_hide_t opt_hide = COPT_NO_HIDE );
//add string input option
void add( const std::string &sNameIn, const std::string &sPageIn,
const std::string &sMenuTextIn, const std::string &sTooltipIn,
const std::string &sDefaultIn, int iMaxLengthIn,
copt_hide_t opt_hide = COPT_NO_HIDE );
//add bool option
void add( const std::string &sNameIn, const std::string &sPageIn,
const std::string &sMenuTextIn, const std::string &sTooltipIn,
bool bDefaultIn, copt_hide_t opt_hide = COPT_NO_HIDE );
//add int option
void add( const std::string &sNameIn, const std::string &sPageIn,
const std::string &sMenuTextIn, const std::string &sTooltipIn,
int iMinIn, int iMaxIn, int iDefaultIn,
copt_hide_t opt_hide = COPT_NO_HIDE,
const std::string &format = "%i" );
//add int map option
void add( const std::string &sNameIn, const std::string &sPageIn,
const std::string &sMenuTextIn, const std::string &sTooltipIn,
const std::vector< std::tuple<int, std::string> > &mIntValuesIn,
int iInitialIn, int iDefaultIn, copt_hide_t opt_hide = COPT_NO_HIDE,
bool verbose = false );
//add float option
void add( const std::string &sNameIn, const std::string &sPageIn,
const std::string &sMenuTextIn, const std::string &sTooltipIn,
float fMinIn, float fMaxIn,
float fDefaultIn, float fStepIn,
copt_hide_t opt_hide = COPT_NO_HIDE,
const std::string &format = "%.2f" );
private:
options_container options;
std::optional<options_container *> world_options;
/** Option group. */
class Group
{
public:
/** Group identifier. Should be unique across all pages. */
std::string id_;
/** Group name */
translation name_;
/** Tooltip with description */
translation tooltip_;
Group() = default;
Group( const std::string &id, const translation &name, const translation &tooltip )
: id_( id ), name_( name ), tooltip_( tooltip ) { }
};
/** Page item type. */
enum class ItemType {
BlankLine,
GroupHeader,
Option,
};
/** Single page item (entry). */
class PageItem
{
public:
ItemType type;
std::string data;
/** Empty if not assigned to any group. */
std::string group;
PageItem() : type( ItemType::BlankLine ) { }
PageItem( ItemType type, const std::string &data, const std::string &group )
: type( type ), data( data ), group( group ) { }
std::string fmt_tooltip( const Group &group, const options_container &cont ) const;
};
/**
* A page (or tab) to be displayed in the options UI.
*/
class Page
{
public:
/** Page identifier */
std::string id_;
/** Page name */
translation name_;
/** Page items (entries) */
std::vector<PageItem> items_;
void removeRepeatedEmptyLines();
Page( const std::string &id, const translation &name ) : id_( id ), name_( name ) { }
};
std::vector<Page> pages_;
std::string adding_to_group_;
std::vector<Group> groups_;
/**
* Specify option group.
*
* Option groups are used for visual separation of options on pages,
* and allow some additional UI functionality (i.e. collapse/expand).
*
* @param page_id Page to create group at.
* @param group Group to create.
* @param entries Page entries added within this closure will be assigned to the group.
* Receives "page_id" as it's only argument.
*/
void add_option_group( const std::string &page_id, const Group &group,
std::function<void( const std::string & )> entries );
/** Add empty line to page. */
void add_empty_line( const std::string &sPageIn );
/** Find group by id. */
const Group &find_group( const std::string &id ) const;
};
bool use_narrow_sidebar(); // short-circuits to on if terminal is too small
/** A mapping(string:string) that stores all tileset values.
* Firsts string is tileset NAME from config.
* Second string is directory that contain tileset.
*/
extern std::map<std::string, std::string> TILESETS;
/** A mapping(string:string) that stores all soundpack values.
* Firsts string is soundpack NAME from config.
* Second string is directory that contains soundpack.
*/
extern std::map<std::string, std::string> SOUNDPACKS;
options_manager &get_options();
template<typename T>
inline T get_option( const std::string &name )
{
return get_options().get_option( name ).value_as<T>();
}
#endif // CATA_SRC_OPTIONS_H