-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cpp
More file actions
556 lines (461 loc) · 18.8 KB
/
Settings.cpp
File metadata and controls
556 lines (461 loc) · 18.8 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "Settings.h"
#include <codecvt>
#include "Globals.h"
#include <fstream>
#include <regex>
#include <fmt/core.h>
#include <iostream>
#include "Game/GameInterface.h"
#include "Game/GameStatistics.h"
#include "ResourceManagers/FontManager.h"
#include "UI/Checkbox.h"
#include "UI/ComboBox.h"
#include "UI/TextField.h"
void restoreDefaultTextFields(TextField* txtFieldPtr);
void setSettingFromTextField(TextField* txtFieldPtr);
void restoreDefaultCheckboxes(Checkbox* checkBoxPtr);
void restoreDefaultComboBoxes(ComboBox* comboBoxPtr);
void setSettingFromComboBoxes(ComboBox* comboBoxPtr);
Settings* Settings::settings_ = nullptr;
Settings::Settings() {
defaultSettingsMap = std::unordered_map<std::string, std::string>();
settingsMap = std::unordered_map<std::string, std::string>();
}
Settings* Settings::getInstance() {
if(settings_ == nullptr){
settings_ = new Settings();
}
return settings_;
}
// This method setups the `defaultSettingsMap` with specified values
// Each time it is called the 'settingsMap' is set to 'defaultSettingsMap'
// Meaning it is called not only to setup initial map values, but also to reset 'settingsMap' back to default.
void Settings::preLoadSettings(bool resetDefaultSettings) {
if(resetDefaultSettings) {
defaultSettingsMap["words_fontName"] = "aleoRegular";
defaultSettingsMap["words_frequency"] = "2.1";
defaultSettingsMap["words_speed"] = "0.001";
defaultSettingsMap["words_size"] = "1.0";
defaultSettingsMap["words_highlight"] = "true";
defaultSettingsMap["ui_scale"] = "1.0";
defaultSettingsMap["endGame_missedWords_bool"] = "true";
defaultSettingsMap["endGame_time_bool"] = "false";
defaultSettingsMap["endGame_score_bool"] = "false";
defaultSettingsMap["endGame_never_bool"] = "false";
defaultSettingsMap["endGame_missedWords_value"] = "5";
defaultSettingsMap["endGame_time_value"] = "60";
defaultSettingsMap["endGame_score_value"] = "20";
defaultSettingsMap["word_locale"] = "english";
}
settingsMap = defaultSettingsMap;
}
// Loads settings from settings.txt file to a 'settingsMap'
void Settings::loadSettingsFromTxt() {
auto file = std::fstream(projectPath + "/settings.txt");
if (!file) {
throw std::runtime_error("Unable to open settings.txt.");
}
std::regex pattern("([a-zA-Z_]+)[=]{1}(.+)");
std::smatch matches;
for (auto line = std::string(); std::getline(file, line); ) {
if(line.empty() || line[0] == '/')
continue;
if (std::regex_match(line, matches, pattern)) {
std::string option = matches[1].str();
std::string value = matches[2].str();
if (settingsMap.contains(option)) {
settingsMap[option] = value;
} else {
throw std::runtime_error(fmt::format("Settings.txt option: {} does not exist in settingsMap.\nThe full line is: {}", option, matches[0].str()));
}
} else {
throw std::runtime_error("Malformed settings.txt line: " + line);
}
}
}
// Restores default values (like input's, checkboxes' checks etc.) in PANEL_SETTINGS
// Sets settingsMap to default values (by calling preLoadSettings(false)) and calls 'update' on UI elements.
void Settings::restoreDefaultSettings() {
// Reset values in TextField(s) etc.
auto settingsPanel = GameInterface::getInstance()->getPanelByType(PANEL_SETTINGS);
for(auto uielement : settingsPanel->UIElements) {
// https://en.cppreference.com/w/cpp/language/dynamic_cast
// Safely converts pointers and references to classes up, down, and sideways along the inheritance hierarchy.
// Im using static_cast though, as its cheaper and I am sure uielement is parent class
switch(uielement->getType()) {
case TEXTFIELD:
{
auto txtFieldPtr = static_cast<TextField*>(uielement);
restoreDefaultTextFields(txtFieldPtr);
break;
}
case CHECKBOX:
{
auto checkBoxPtr = static_cast<Checkbox*>(uielement);
restoreDefaultCheckboxes(checkBoxPtr);
break;
}
case COMBOBOX:
{
auto comboBoxPtr = static_cast<ComboBox*>(uielement);
restoreDefaultComboBoxes(comboBoxPtr);
break;
}
default:
break;
}
}
// This basically will reset all the keys in the map (meaning it will reset settings)
preLoadSettings(false);
// We reset UIScale to default and so we should refresh UI
for(auto panel : GameInterface::getInstance()->panels) {
panel->update();
}
GameInterface::getInstance()->updateGameTitle();
}
void Settings::setWordsFontName(std::string const& value) {
if(FontManager::Fonts.contains(value)) {
settingsMap["words_fontName"] = value;
} else {
settingsMap["words_fontName"] = defaultSettingsMap["words_fontName"];
}
}
void Settings::setWordsFrequency(std::string const& value) {
try {
// We do this to make sure the string we will save to map is of good type
// for frequency the string must be convertable to double
auto tempValue = std::stod(value);
settingsMap["words_frequency"] = value;
} catch (const std::invalid_argument& e) {
settingsMap["words_frequency"] = defaultSettingsMap["words_frequency"];
}
}
void Settings::setWordsSpeed(std::string const& value) {
try {
auto tempValue = std::stod(value);
settingsMap["words_speed"] = value;
} catch (const std::invalid_argument& e) {
settingsMap["words_speed"] = defaultSettingsMap["words_speed"];
}
}
void Settings::setWordsSize(std::string const& value) {
try {
auto tempValue = std::stod(value);
settingsMap["words_size"] = value;
} catch (const std::invalid_argument& e) {
settingsMap["words_size"] = defaultSettingsMap["words_size"];
}
}
void Settings::setWordsHighlight(std::string const& value) {
settingsMap["words_highlight"] = value;
}
void Settings::setWordsHighlight(bool const &value) {
if(value == true) {
settingsMap["words_highlight"] = "true";
} else {
settingsMap["words_highlight"] = "false";
}
}
void Settings::setUIScale(std::string const& value) {
try {
auto tempValue = std::stof(value);
if(tempValue > 0) {
settingsMap["ui_scale"] = value;
} else { // Failsafe from setting ui_scale to 0 (or less) which means UI would disappear
settingsMap["ui_scale"] = 0.1;
}
} catch (const std::invalid_argument& e) {
settingsMap["ui_scale"] = defaultSettingsMap["ui_scale"];
}
// Refresh panel sizes
for(auto panel : GameInterface::getInstance()->panels) {
panel->update();
}
GameInterface::getInstance()->updateGameTitle();
}
std::string Settings::getWordsFontName(bool defaultValue) {
if(defaultValue == true)
return defaultSettingsMap["words_fontName"];
return settingsMap["words_fontName"];
}
double Settings::getWordsFrequency(bool defaultValue) {
if(defaultValue == true)
return std::stod(defaultSettingsMap["words_frequency"]);
auto value = 0.0;
try {
value = std::stod(settingsMap["words_frequency"]);
} catch (const std::invalid_argument& e) {
value = std::stod(defaultSettingsMap["words_frequency"]);
}
return value;
}
double Settings::getWordsSpeed(bool defaultValue) {
if(defaultValue == true)
return std::stod(defaultSettingsMap["words_speed"]);
auto value = 0.0;
try {
value = std::stod(settingsMap["words_speed"]);
} catch (const std::invalid_argument& e) {
value = std::stod(defaultSettingsMap["words_speed"]);
}
return value;
}
double Settings::getWordsSize(bool defaultValue) {
if(defaultValue == true)
return std::stod(defaultSettingsMap["words_size"]);
auto value = 0.0;
try {
value = std::stod(settingsMap["words_size"]);
} catch (const std::invalid_argument& e) {
value = std::stod(defaultSettingsMap["words_size"]);
}
return value;
}
bool Settings::isWordsHighlightEnabled(bool defaultValue) {\
std::string value = "true";
if(defaultValue)
value = defaultSettingsMap["words_highlight"];
else
value = settingsMap["words_highlight"];
return value == "true" || value == "True";
}
float Settings::getUIScale(bool defaultValue) {
if(defaultValue == true)
return std::stof(defaultSettingsMap["ui_scale"]);
auto value = 0.0f;
try {
value = std::stof(settingsMap["ui_scale"]);
} catch (const std::invalid_argument& e) {
value = std::stof(defaultSettingsMap["ui_scale"]);
}
return value;
}
void restoreDefaultTextFields(TextField* txtFieldPtr) {
auto instance = Settings::getInstance();
std::string text = txtFieldPtr->getText().getString();
if (text.starts_with("Word Speed"))
txtFieldPtr->setInput(fmt::format("{:.5f}", instance->getWordsSpeed(true)));
else if (text.starts_with("Word Frequency"))
txtFieldPtr->setInput(fmt::format("{:.2f}", instance->getWordsFrequency(true)));
else if (text.starts_with("Word Size"))
txtFieldPtr->setInput(fmt::format("{:.2f}", instance->getWordsSize(true)));
else if (text.starts_with("Word Locale"))
txtFieldPtr->setInput(fmt::format("{}", instance->getWordLocale(true)));
else if (text.starts_with("UI Scale")) {
txtFieldPtr->setInput(fmt::format("{:.2f}", instance->getUIScale(true)));
}
txtFieldPtr->update();
}
void restoreDefaultCheckboxes(Checkbox* checkBoxPtr) {
auto instance = Settings::getInstance();
std::string text = checkBoxPtr->getText().getString();
if (text.starts_with("Word Highlight")) {
instance->isWordsHighlightEnabled(true) == true ? checkBoxPtr->check() : checkBoxPtr->uncheck();
}
}
void restoreDefaultComboBoxes(ComboBox* comboBoxPtr) {
auto instance = Settings::getInstance();
std::string text = comboBoxPtr->getText().getString();
if (text.starts_with("Word Font")) {
comboBoxPtr->setChosenText(fmt::format("{}", instance->getWordsFontName(true)));
}
}
void setSettingFromTextField(TextField* txtFieldPtr) {
auto instance = Settings::getInstance();
std::string text = txtFieldPtr->getText().getString();
auto value = txtFieldPtr->getInputString();
if (text.starts_with("Word Speed"))
instance->setWordsSpeed(value);
else if (text.starts_with("Word Frequency"))
instance->setWordsFrequency(value);
else if (text.starts_with("Word Size"))
instance->setWordsSize(value);
else if (text.starts_with("Word Locale"))
instance->setWordLocale(value);
else if (text.starts_with("UI Scale")) {
instance->setUIScale(value);
GameInterface::getInstance()->updatePanels();
GameInterface::getInstance()->updateGameTitle();
}
}
void setSettingFromComboBoxes(ComboBox* comboBoxPtr) {
auto instance = Settings::getInstance();
std::string text = comboBoxPtr->getText().getString();
if (text.starts_with("Word Font")) {
instance->setWordsFontName(comboBoxPtr->getChosenTextString());
}
}
void Settings::saveSettingsToTxt() {
// Allow for both reading AND writing
auto file = std::fstream(projectPath + "/settings.txt", std::ios::in | std::ios::out);
if (!file) {
throw std::runtime_error("Unable to open settings.txt.");
}
std::regex pattern("([a-zA-Z_]+)[=]{1}(.+)");
std::smatch matches;
std::stringstream buffer;
for (std::string line; std::getline(file, line); ) {
if (std::regex_match(line, matches, pattern)) {
std::string option = matches[1].str();
std::string newValue;
if (option == "words_fontName") {
newValue = Settings::getWordsFontName(false);
} else if (option == "words_frequency") {
newValue = fmt::format("{:.2f}", Settings::getWordsFrequency(false));
} else if (option == "words_speed") {
newValue = fmt::format("{:.5f}", Settings::getWordsSpeed(false));
} else if (option == "words_size") {
newValue = fmt::format("{:.2f}", Settings::getWordsSize(false));
} else if (option == "word_locale") {
newValue = fmt::format("{}", Settings::getWordLocale(false));
} else if (option == "words_highlight") {
newValue = Settings::isWordsHighlightEnabled(false) ? "true" : "false";
} else if (option == "ui_scale") {
newValue = fmt::format("{:.2f}", Settings::getUIScale(false));
}
buffer << option << "=" << newValue << "\n";
} else {
buffer << line << "\n";
}
}
// Close and then update file
file.close();
std::ofstream outFile(projectPath + "/settings.txt");
outFile << buffer.str();
outFile.close();
}
// Loads settings and updates the settings map based on values in UIElements of Settings Panel
// It's crucial, because "loadGame()" overrides settings map and "New Game" wouldn't have correct settings
// then (after some game load happened)
void Settings::loadSetingsOnNewGame() {
auto settingsPanel = GameInterface::getInstance()->getPanelByType(PANEL_SETTINGS);
if(settingsPanel == nullptr) {
throw std::runtime_error("Unable to find settingsPanel while calling Settings::loadSetingsOnNewGame() method");
}
// This will be used for updating game end criteriums
auto newGameSetupPanel = GameInterface::getInstance()->getPanelByType(PANEL_NEWGAMESETUP);
if(newGameSetupPanel == nullptr) {
throw std::runtime_error("Unable to find newGameSetupPanel while calling Settings::loadSetingsOnNewGame() method");
}
for(auto uielement : settingsPanel->UIElements) {
// https://en.cppreference.com/w/cpp/language/dynamic_cast
// Safely converts pointers and references to classes up, down, and sideways along the inheritance hierarchy.
// Im using static_cast though, as its cheaper and I am sure uielement is parent class
switch(uielement->getType()) {
case CHECKBOX:
{
auto checkBoxPtr = static_cast<Checkbox*>(uielement);
checkBoxPtr->onCheckboxUpdate(checkBoxPtr->isChecked());
break;
}
case TEXTFIELD:
{
auto txtFieldPtr = static_cast<TextField*>(uielement);
txtFieldPtr->onTextFieldUpdate();
break;
}
case COMBOBOX:
{
auto comboBoxPtr = static_cast<ComboBox*>(uielement);
std::string text = comboBoxPtr->getText().getString();
if (text.starts_with("Word Font")) {
settingsMap["words_fontName"] = comboBoxPtr->getChosenTextString();
}
break;
}
default:
break;
}
}
for(auto uielement : newGameSetupPanel->UIElements) {
// https://en.cppreference.com/w/cpp/language/dynamic_cast
// Safely converts pointers and references to classes up, down, and sideways along the inheritance hierarchy.
// Im using static_cast though, as its cheaper and I am sure uielement is parent class
switch(uielement->getType()) {
case CHECKBOX:
{
auto checkBoxPtr = static_cast<Checkbox*>(uielement);
checkBoxPtr->onCheckboxUpdate(checkBoxPtr->isChecked());
break;
}
case TEXTFIELD:
{
auto txtFieldPtr = static_cast<TextField*>(uielement);
txtFieldPtr->onTextFieldUpdate();
break;
}
default:
break;
}
}
}
// Returns string that dynamic text label in game statistics (during the game) uses.
// Depending on checked options like game ending on missed words it will 'build' a string
// and then dynamic label will show it nicely.
std::string Settings::buildEndGameSettings() {
std::string result;
Settings::setEndGameCriteriumBool("endGame_never_bool", true);
if (Settings::getEndGameCriteriumBool("endGame_missedWords_bool") == true) {
Settings::setEndGameCriteriumBool("endGame_never_bool", false);
result += fmt::format("Ends on:\t{} missed\t", getEndGameCriterium_missedWords());
}
if (Settings::getEndGameCriteriumBool("endGame_score_bool") == true) {
Settings::setEndGameCriteriumBool("endGame_never_bool", false);
result += fmt::format("Ends on:\t{} score\t", getEndGameCriterium_score());
}
if (Settings::getEndGameCriteriumBool("endGame_time_bool") == true) {
Settings::setEndGameCriteriumBool("endGame_never_bool", false);
result += fmt::format("Ends on:\t{} time\t", GameStatistics::getInstance()->formatTime(getEndGameCriterium_time()));
}
if(Settings::getEndGameCriteriumBool("endGame_never_bool") == true) {
result += fmt::format("This game never ends");
}
return result;
}
void Settings::setEndGameCriteriumBool(std::string const& criterium, bool value) {
if(value == true) {
settingsMap[criterium] = "true";
} else {
settingsMap[criterium] = "false";
}
}
bool Settings::getEndGameCriteriumBool(std::string const& criterium) {
auto value = settingsMap[criterium];
return value == "true" || value == "True";
}
void Settings::setEndGameCriterium_missedWords(int value) {
settingsMap["endGame_missedWords_value"] = std::to_string(value);
}
void Settings::setEndGameCriterium_missedWords(std::string value) {
settingsMap["endGame_missedWords_value"] = value;
}
int Settings::getEndGameCriterium_missedWords() {
return std::stoi(settingsMap["endGame_missedWords_value"]);
}
void Settings::setEndGameCriterium_time(double value) {
settingsMap["endGame_time_value"] = std::to_string(value);
}
void Settings::setEndGameCriterium_time(std::string value) {
settingsMap["endGame_time_value"] = value;
}
std::chrono::duration<double> Settings::getEndGameCriterium_time() {
return std::chrono::duration<double>(std::stod(settingsMap["endGame_time_value"]));
}
void Settings::setEndGameCriterium_score(int value) {
settingsMap["endGame_score_value"] = std::to_string(value);
}
void Settings::setEndGameCriterium_score(std::string value) {
settingsMap["endGame_score_value"] = value;
}
int Settings::getEndGameCriterium_score() {
return std::stoi(settingsMap["endGame_score_value"]);
}
void Settings::setWordLocale(std::string value) {
settingsMap["word_locale"] = value;
}
std::string Settings::getWordLocale(bool defaultValue) {
if(defaultValue) {
return defaultSettingsMap["word_locale"];
}
return settingsMap["word_locale"];
}