-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQRetroOptions.cpp
506 lines (434 loc) · 12.1 KB
/
QRetroOptions.cpp
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
#include <QApplication>
#include <QDir>
#include <QSettings>
#include <QCheckBox>
#include <QComboBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include "QRetroOptions.h"
static const char* type_name(uint8_t type)
{
switch (type)
{
case QRetroOption::Choice:
return "Choice";
case QRetroOption::Bool:
return "Bool";
case QRetroOption::Int:
return "Int";
case QRetroOption::Float:
return "Float";
default:
return "Unknown";
}
}
QRetroOptionCategory::QRetroOptionCategory(retro_core_option_v2_category* us,
retro_core_option_v2_category* local)
{
m_Title[Language::Default] = us->desc;
m_Description[Language::Default] = us->info;
if (local)
{
m_Title[Language::Local] = local->desc;
m_Description[Language::Local] = local->info;
}
}
QRetroOption::QRetroOption(retro_variable *var)
{
/*
The friendly name of the option comes before the first ';' in the string
var->value.
*/
m_Title[Default] =
QString(var->value).section(';', 0, 0).toStdString();
/*
The possible values specified by the core come after the first ';' and
are separated by '|'. There is usually a space after the ';'.
TODO: Would we ever want duplicates or blank strings as options?
*/
m_PossibleValues[Default] = QString(var->value).section(";", 1).trimmed().split("|");
m_PossibleValues[Default].removeDuplicates();
m_PossibleValues[Default].removeAll(QString(""));
/* The value the option is initialized to is the first one listed. */
m_DefaultValue = m_PossibleValues[Default][0].toStdString();
/*
By reading the possible values, we can try to provide a hint about what
kind of data the option represents, which can then be used to properly
edit it in a UI.
ie. boolean values can be represented by checkboxes, ints by sliders, etc.
*/
determineType();
qDebug("%s (%s):\n"
" Possible (%u): [%s]\n"
" Default: %s\n"
" Type = %s",
m_Title[Default].c_str(),
var->key,
m_PossibleValues[Default].count(),
m_PossibleValues[Default].join(", ").toStdString().c_str(),
m_DefaultValue.c_str(),
type_name(m_Type));
}
/* TODO: Support option titles and multilanguage choices */
QRetroOption::QRetroOption(retro_core_option_definition* us,
retro_core_option_definition* local)
{
m_Title[Default] = QString(us->desc).toStdString();
if (local)
m_Title[Local] = QString(local->desc).toStdString();
auto choice = us->values;
/* If label is not set, use the actual value */
while (choice->value)
{
m_PossibleValues[Default].append(choice->value);
choice++;
}
/* If default value is not set, default to the first value */
m_DefaultValue = us->default_value ? std::string(us->default_value) :
std::string(us->values[0].value);
determineType();
qDebug("%s (%s):\n"
" Possible (%u): [%s]\n"
" Default: %s\n"
" Type = %s",
m_Title[local ? Language::Local : Language::Default].c_str(),
us->key,
m_PossibleValues[Default].count(),
m_PossibleValues[Default].join(", ").toStdString().c_str(),
m_DefaultValue.c_str(),
type_name(m_Type));
}
QRetroOption::QRetroOption(retro_core_option_v2_definition* us,
retro_core_option_v2_definition* local)
{
m_Title[Language::Default] = QString(us->desc).toStdString();
m_TitleCategorized[Language::Default] = QString(us->desc_categorized).toStdString();
if (local)
{
m_Title[Language::Local] = QString(local->desc).toStdString();
m_TitleCategorized[Language::Local] = QString(local->desc_categorized).toStdString();
}
auto *choice = us->values;
while (choice->label && choice->value)
{
m_PossibleValues[Default].append(choice->value);
choice++;
}
if (local)
{
choice = local->values;
while (choice->label && choice->value)
{
m_PossibleValues[Local].append(choice->value);
choice++;
}
}
m_DefaultValue = std::string(us->default_value);
determineType();
qDebug("%s (%s):\n"
" Possible (%u): [%s]\n"
" Default: %s\n"
" Type = %s",
m_Title[local ? Language::Local : Language::Default].c_str(),
us->key,
m_PossibleValues[Default].count(),
m_PossibleValues[Default].join(", ").toStdString().c_str(),
m_DefaultValue.c_str(),
type_name(m_Type));
}
bool QRetroOption::determineType()
{
auto values = m_PossibleValues[Default];
if (values.isEmpty())
{
m_Type = QRetroOption::None;
return false;
}
/* If it only allows two choices (enabled and disabled), it is boolean. */
else if (values.contains("enabled") &&
values.contains("disabled") &&
values.count() == 2)
{
m_Type = QRetroOption::Bool;
return true;
}
/* Loop through the options to see if they are all a type of number. */
else
{
bool ok_float = true;
bool ok_int = true;
bool ok;
for (int i = 0; i < values.count(); i++)
{
if (ok_int)
{
values[i].toInt(&ok, 10);
ok_int &= ok;
}
if (ok_float)
{
values[i].toFloat(&ok);
ok_float &= ok;
}
}
/* TODO: Does, say, 2.0 -> 2 and return success on toInt? */
if (ok_float && !ok_int)
{
m_Type = QRetroOption::Float;
return true;
}
else if (ok_int)
{
m_Type = QRetroOption::Int;
return true;
}
}
/* None of the checks succeeded, but there are options to choose from. */
m_Type = QRetroOption::Choice;
return false;
}
const char* QRetroOption::title(Language lang)
{
if (lang == Language::Local && !m_Title[lang].empty())
return m_Title[Language::Local].c_str();
else
return m_Title[Language::Default].c_str();
}
bool QRetroOption::setToDefaultValue()
{
if (m_PossibleValues[Default].isEmpty())
return false;
else
{
setValue(m_DefaultValue);
return true;
}
}
QRetroOptions::QRetroOptions()
{
m_Filename = QDir::currentPath() + "/config.ini";
}
QRetroOptions::~QRetroOptions()
{
QSettings settings(m_Filename, QSettings::IniFormat);
settings.beginGroup(m_CoreName);
for (auto iter = m_Variables.begin(); iter != m_Variables.end(); iter++)
{
if (!iter->first.empty() && iter->second)
{
settings.setValue(iter->first.c_str(), iter->second->getValue());
delete iter->second;
}
}
settings.sync();
}
QRetroOption* QRetroOptions::getOption(const char *key)
{
return m_Variables[key];
}
const char* QRetroOptions::getOptionValue(const char *key)
{
auto var = getOption(key);
return var ? var->getValue() : nullptr;
}
void QRetroOptions::setOptionValue(const char* key, const char* value)
{
auto var = getOption(key);
if (var)
{
var->setValue(value);
m_VariablesUpdated = true;
}
}
void QRetroOptions::setOptions(retro_variable *vars)
{
QSettings settings(m_Filename, QSettings::IniFormat);
settings.beginGroup(m_CoreName);
while (vars && vars->key && vars->value)
{
std::string value;
auto entry = new QRetroOption(vars);
value = settings.value(vars->key, "").toString().toStdString();
if (value.empty())
{
entry->setToDefaultValue();
settings.setValue(vars->key, entry->getValue());
}
else
entry->setValue(value);
m_Variables[vars->key] = entry;
vars++;
}
settings.sync();
m_Version = v0;
}
void QRetroOptions::setOptions(retro_core_option_definition** vars)
{
QSettings settings(m_Filename, QSettings::IniFormat);
auto var = *vars;
settings.beginGroup(m_CoreName);
while (var && var->key)
{
std::string value;
auto entry = new QRetroOption(var);
value = settings.value(var->key, "").toString().toStdString();
if (value.empty())
{
entry->setToDefaultValue();
settings.setValue(var->key, entry->getValue());
}
else
entry->setValue(value);
m_Variables[var->key] = entry;
var++;
}
settings.sync();
m_Version = v1;
}
void QRetroOptions::setOptions(retro_core_options_intl* vars)
{
QSettings settings(m_Filename, QSettings::IniFormat);
unsigned i = 0;
if (!vars->local || vars->us == vars->local)
{
setOptions(&vars->us);
return;
}
settings.beginGroup(m_CoreName);
while (vars->us[i].key)
{
std::string value;
/*
* Find the local option definition with a matching key to the us one.
* In practice, these should always align, so this may not be necessary.
*/
retro_core_option_definition *local = nullptr;
unsigned j = 0;
do
{
local = &vars->local[j];
if (!strcmp(local->key, vars->us[i].key))
break;
j++;
} while (local->key);
if (!local->key)
local = nullptr;
auto entry = new QRetroOption(&vars->us[i], &vars->local[i]);
value = settings.value(vars->us[i].key, "").toString().toStdString();
if (value.empty())
{
entry->setToDefaultValue();
settings.setValue(vars->us[i].key, entry->getValue());
}
else
entry->setValue(value);
m_Variables[vars->us[i].key] = entry;
i++;
}
settings.sync();
m_Version = v1;
}
void QRetroOptions::setOptions(retro_core_options_v2* vars)
{
QSettings settings(m_Filename, QSettings::IniFormat);
auto var = vars->definitions;
settings.beginGroup(m_CoreName);
while (var && var->key)
{
std::string value;
auto entry = new QRetroOption(var);
value = settings.value(var->key, "").toString().toStdString();
if (value.empty())
{
entry->setToDefaultValue();
settings.setValue(var->key, entry->getValue());
}
else
entry->setValue(value);
m_Variables[var->key] = entry;
var++;
}
settings.sync();
m_Version = v2;
}
void QRetroOptions::setVisibility(const char *key, bool enabled)
{
auto var = getOption(key);
if (var)
var->setVisibility(enabled);
}
void QRetroOptions::onOptionBoolChanged(int state)
{
setOptionValue(sender()->objectName().toStdString().c_str(),
state == Qt::Unchecked ? "disabled" : "enabled");
}
void QRetroOptions::onOptionChoiceChanged(const QString& choice)
{
setOptionValue(sender()->objectName().toStdString().c_str(),
choice.toStdString().c_str());
}
void QRetroOptions::update()
{
QGridLayout *layout = new QGridLayout();
int i = 0;
/* Delete everything if the layout has already been initialized before */
if (this->layout())
{
QLayoutItem* item;
while ((item = this->layout()->takeAt(0)) != nullptr)
{
delete item->widget();
delete item;
}
delete this->layout();
}
for (auto iter = m_Variables.begin(); iter != m_Variables.end(); iter++, i++)
{
auto var = iter->second;
if (!var)
continue;
else switch (var->type())
{
case QRetroOption::Bool:
{
auto elem = new QCheckBox(this);
elem->setChecked(strcmp(var->getValue(), "disabled"));
elem->setObjectName(QString::fromStdString(iter->first));
connect(elem, SIGNAL(stateChanged(int)),
this, SLOT(onOptionBoolChanged(int)));
auto label = new QLabel(var->title(), this);
label->setEnabled(var->getVisibility());
label->setBuddy(elem);
layout->addWidget(label, i, 0);
layout->addWidget(elem, i, 1);
break;
}
default:
{
auto elem = new QComboBox(this);
elem->addItems(var->possibleValues());
elem->setCurrentText(var->getValue());
elem->setObjectName(QString::fromStdString(iter->first));
connect(elem, SIGNAL(currentTextChanged(const QString&)),
this, SLOT(onOptionChoiceChanged(const QString&)));
auto label = new QLabel(var->title(), this);
label->setEnabled(var->getVisibility());
label->setBuddy(elem);
layout->addWidget(label, i, 0);
layout->addWidget(elem, i, 1);
}
}
}
auto label = new QLabel(QString("Using core options v%1.").arg(QString::number(m_Version)), this);
layout->addWidget(label, i, 1);
setWindowTitle(QString("%1 Options").arg(m_CoreName));
setLayout(layout);
}
bool QRetroOptions::variablesUpdated(bool quiet)
{
bool updated = m_VariablesUpdated;
if (!quiet)
m_VariablesUpdated = false;
return updated;
}