/
mp_options.cpp
607 lines (504 loc) · 14.1 KB
/
mp_options.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
Copyright (C) 2012 - 2014 by Boldizsár Lipka <lipkab@zoho.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program 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 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "mp_options.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/window_builder.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/slider.hpp"
#include "gui/widgets/text_box.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "widgets/slider.hpp"
#include "widgets/textbox.hpp"
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
static lg::log_domain log_mp_create_options("mp/create/options");
#define DBG_MP LOG_STREAM(debug, log_mp_create_options)
namespace mp
{
namespace options
{
config to_event(const config& cfg)
{
config result;
if (!cfg) {
return result;
}
result["name"] = "prestart";
BOOST_FOREACH (const config& c, cfg.child_range("option")) {
config ev;
ev["name"] = c["id"];
ev["value"] = c["value"];
result.add_child("set_variable", ev);
}
return result;
}
void manager::init_info(const config& cfg, const std::string& key)
{
BOOST_FOREACH (const config& comp, cfg.child_range(key)) {
config entry;
entry["id"] = comp["id"];
entry["name"] = comp["name"];
if (comp.has_child("options") && comp["allow_new_game"].to_bool(true)) {
const config& options = comp.child("options");
BOOST_FOREACH (const config::any_child& c,
options.all_children_range()) {
entry.add_child(c.key, c.cfg);
}
}
// We need to store components even if they don't have any options in
// order to have set_xxx_by_index work properly
options_info_.add_child(key, entry);
}
}
void manager::init_widgets()
{
BOOST_FOREACH(option_display* od, widgets_ordered_) {
delete od;
}
widgets_.clear();
widgets_ordered_.clear();
BOOST_FOREACH (const config::any_child& comp, options_info_.all_children_range()) {
if (comp.cfg.all_children_count() == 0 || !is_active(comp.cfg["id"])) {
continue;
}
widgets_ordered_.push_back(new title_display(display_.video(), comp.cfg["name"]));
BOOST_FOREACH (const config::any_child& c, comp.cfg.all_children_range()) {
const std::string id = c.cfg["id"];
if (c.key == "slider") {
widgets_ordered_.push_back(new slider_display(display_.video(), c.cfg));
} else if (c.key == "entry") {
widgets_ordered_.push_back(new entry_display(display_.video(), c.cfg));
} else if (c.key == "checkbox") {
widgets_ordered_.push_back(new checkbox_display(display_.video(), c.cfg));
} else if (c.key == "combo") {
widgets_ordered_.push_back(new combo_display(display_, c.cfg));
}
widgets_ordered_.back()->set_value(get_stored_value(id));
widgets_[id] = widgets_ordered_.back();
}
}
}
manager::manager(const config &gamecfg, game_display &display, gui::scrollpane *pane, const config &values)
: options_info_()
, values_(values)
, display_(display)
, pane_(pane)
, era_()
, scenario_()
, modifications_()
, widgets_()
, widgets_ordered_()
{
DBG_MP << "Initializing the options manager" << std::endl;
init_info(gamecfg, "modification");
init_info(gamecfg, "era");
init_info(gamecfg, "multiplayer");
BOOST_FOREACH (const config::any_child& i,
options_info_.all_children_range())
{
BOOST_FOREACH (const config::any_child& j, i.cfg.all_children_range())
{
if (is_valid_option(j.key, j.cfg)) {
config& value = get_value_cfg(j.cfg["id"]);
value["value"] = get_stored_value(j.cfg["id"]);
}
}
}
init_widgets();
}
manager::~manager()
{
BOOST_FOREACH(option_display* od, widgets_ordered_)
{
delete od;
}
}
void manager::set_values(const config& c)
{
values_ = c;
}
void manager::set_era(const std::string& era)
{
era_ = era;
}
void manager::set_era_by_index(int index)
{
era_ = options_info_.child("era", index)["id"].str();
}
void manager::set_scenario(const std::string& scenario)
{
scenario_ = scenario;
}
void manager::set_scenario_by_index(int index)
{
scenario_ = options_info_.child("multiplayer", index - 1)["id"].str();
}
void manager::set_modifications(const std::vector<std::string>& modifications)
{
modifications_ = modifications;
}
void manager::insert_element(elem_type type, const config& data, int pos)
{
switch (type)
{
case SCENARIO:
options_info_.add_child_at("multiplayer", data, pos);
break;
case ERA:
options_info_.add_child_at("era", data, pos);
break;
case MODIFICATION:
options_info_.add_child_at("modification", data, pos);
break;
}
}
void manager::layout_widgets(int startx, int starty)
{
int ypos = starty;
int border_size = 3;
BOOST_FOREACH(option_display* od, widgets_ordered_)
{
od->layout(startx, ypos, border_size, pane_);
ypos += border_size;
}
}
void manager::process_event()
{
for (std::map<std::string, option_display*>::iterator i = widgets_.begin();
i != widgets_.end(); ++i)
{
i->second->process_event();
}
}
void manager::hide_children(bool hide)
{
for (std::map<std::string, option_display*>::iterator i = widgets_.begin();
i != widgets_.end(); ++i)
{
i->second->hide_children(hide);
}
}
const config &manager::get_values()
{
update_values();
return values_;
}
config& manager::get_value_cfg(const std::string& id)
{
{
const config& value_cfg = get_value_cfg_or_empty(id);
if (!value_cfg.empty()) {
return const_cast<config&>(value_cfg);
}
}
config::any_child info = get_option_parent(id);
config* parent_cfg;
if (!values_.find_child(info.key, "id", info.cfg["id"])) {
parent_cfg = &values_.add_child(info.key);
(*parent_cfg)["id"] = info.cfg["id"];
} else {
parent_cfg = &values_.find_child(info.key, "id", info.cfg["id"]);
}
config& value_cfg = parent_cfg->add_child("option");
value_cfg["id"] = id;
return value_cfg;
}
const config& manager::get_value_cfg_or_empty(const std::string& id) const
{
static const config empty;
BOOST_FOREACH (const config::any_child& i, values_.all_children_range()) {
BOOST_FOREACH (const config& j, i.cfg.child_range("option")) {
if (j["id"] == id) {
return j;
}
}
}
return empty;
}
config::any_child manager::get_option_parent(const std::string& id) const
{
static const config empty;
static const std::string empty_key = "";
static config::any_child not_found(&empty_key, &empty);
BOOST_FOREACH (const config::any_child& i,
options_info_.all_children_range()) {
BOOST_FOREACH (const config::any_child& j, i.cfg.all_children_range()) {
if (j.cfg["id"] == id) {
return i;
}
}
}
return not_found;
}
const config& manager::get_option_info_cfg(const std::string& id) const
{
static const config empty;
BOOST_FOREACH (const config::any_child& i,
options_info_.all_children_range()) {
BOOST_FOREACH (const config::any_child& j, i.cfg.all_children_range()) {
if (j.cfg["id"] == id) {
return j.cfg;
}
}
}
return empty;
}
config::attribute_value manager::get_stored_value(const std::string& id) const
{
const config& valcfg = get_value_cfg_or_empty(id);
if (!valcfg["value"].empty()) {
// There's a saved value for this option
return valcfg["value"];
}
// Fall back to the option's default
return get_default_value(id);
}
config::attribute_value manager::get_default_value(const std::string& id) const
{
const config& optinfo = get_option_info_cfg(id);
return optinfo["default"];
}
void manager::extract_values(const std::string& key, const std::string& id)
{
BOOST_FOREACH (const config::any_child& c,
options_info_.find_child(key, "id", id).all_children_range())
{
if (!is_valid_option(c.key, c.cfg)) {
continue;
}
config& out = get_value_cfg(c.cfg["id"].str());
out["value"] = widgets_[c.cfg["id"]]->get_value();
}
}
void manager::update_values()
{
extract_values("era", era_);
extract_values("multiplayer", scenario_);
BOOST_FOREACH(const std::string& str, modifications_) {
extract_values("modification", str);
}
}
bool manager::is_valid_option(const std::string& key, const config& option)
{
return (key == "slider" || key == "entry" || key == "checkbox" || key == "combo") &&
(!option["id"].empty());
}
void manager::restore_defaults(manager* m)
{
const config& era = m->options_info_.find_child("era", "id", m->era_);
restore_defaults_for_component(era, m);
const config& scen = m->options_info_.find_child("multiplayer", "id",
m->scenario_);
restore_defaults_for_component(scen, m);
BOOST_FOREACH (const std::string& id, m->modifications_) {
const config& mod = m->options_info_.find_child("modification", "id",
id);
restore_defaults_for_component(mod, m);
}
}
void manager::restore_defaults_for_component(const config& c, manager* m)
{
BOOST_FOREACH (const config::any_child& i, c.all_children_range()) {
if (!is_valid_option(i.key, i.cfg)) {
continue;
}
const std::string id = i.cfg["id"].str();
m->widgets_[id]->set_value(m->get_default_value(id));
}
}
bool manager::is_active(const std::string &id) const
{
return (era_ == id) || (scenario_ == id) ||
(std::find(modifications_.begin(), modifications_.end(), id) != modifications_.end());
}
entry_display::entry_display(CVideo &video, const config &cfg) :
entry_(new gui::textbox(video, 150, cfg["default"])),
label_(new gui::label(video, cfg["name"]))
{
entry_->set_help_string(cfg["description"]);
}
entry_display::~entry_display()
{
delete entry_;
delete label_;
}
void entry_display::layout(int &xpos, int &ypos, int border_size, gui::scrollpane *pane)
{
pane->add_widget(label_, xpos, ypos);
pane->add_widget(entry_, xpos + label_->width() + border_size, ypos);
ypos += std::max(label_->height(), entry_->height()) + border_size;
}
void entry_display::set_value(const config::attribute_value &val)
{
entry_->set_text(val);
}
config::attribute_value entry_display::get_value() const
{
config::attribute_value res;
res = entry_->text();
return res;
}
void entry_display::hide_children(bool hide)
{
label_->hide(hide);
entry_->hide(hide);
}
slider_display::slider_display(CVideo &video, const config &cfg) :
slider_(new gui::slider(video)),
label_(new gui::label(video, cfg["name"], font::SIZE_SMALL)),
last_value_(cfg["default"].to_int()),
label_text_(cfg["name"])
{
slider_->set_min(cfg["min"].to_int());
slider_->set_max(cfg["max"].to_int());
slider_->set_increment(cfg["step"].to_int());
slider_->set_width(150);
slider_->set_value(cfg["default"].to_int());
slider_->set_help_string(cfg["description"]);
update_label();
}
slider_display::~slider_display()
{
delete slider_;
delete label_;
}
void slider_display::layout(int &xpos, int &ypos, int border_size, gui::scrollpane *pane)
{
pane->add_widget(label_, xpos, ypos);
ypos += label_->height() + border_size;
pane->add_widget(slider_, xpos, ypos);
ypos += slider_->height() + border_size;
}
void slider_display::set_value(const config::attribute_value &val)
{
slider_->set_value(val.to_int());
}
config::attribute_value slider_display::get_value() const
{
config::attribute_value res;
res = slider_->value();
return res;
}
void slider_display::process_event()
{
if (slider_->value() != last_value_) {
last_value_ = slider_->value();
update_label();
}
}
void slider_display::hide_children(bool hide)
{
label_->hide(hide);
slider_->hide(hide);
}
void slider_display::update_label()
{
std::stringstream ss;
ss << label_text_ << ' ' << last_value_;
label_->set_text(ss.str());
}
checkbox_display::checkbox_display(CVideo &video, const config &cfg) :
checkbox_(new gui::button(video, cfg["name"], gui::button::TYPE_CHECK))
{
checkbox_->set_check(cfg["default"].to_bool());
checkbox_->set_help_string(cfg["description"]);
}
checkbox_display::~checkbox_display()
{
delete checkbox_;
}
void checkbox_display::layout(int &xpos, int &ypos, int border_size, gui::scrollpane *pane)
{
pane->add_widget(checkbox_, xpos, ypos);
ypos += checkbox_->height() + border_size;
}
void checkbox_display::set_value(const config::attribute_value &val)
{
checkbox_->set_check(val.to_bool());
}
config::attribute_value checkbox_display::get_value() const
{
config::attribute_value res;
res = checkbox_->checked();
return res;
}
void checkbox_display::hide_children(bool hide)
{
checkbox_->hide(hide);
}
title_display::title_display(CVideo &video, const std::string &label) :
title_(new gui::label(video, "`~" + label, font::SIZE_PLUS, font::LOBBY_COLOR))
{}
title_display::~title_display()
{
delete title_;
}
void title_display::layout(int &xpos, int &ypos, int border_size, gui::scrollpane *pane)
{
ypos += 4*border_size;
pane->add_widget(title_, xpos, ypos);
ypos += title_->height() + 2*border_size;
}
void title_display::hide_children(bool hide)
{
title_->hide(hide);
}
combo_display::combo_display(game_display &display, const config &cfg) :
label_(new gui::label(display.video(), cfg["name"])),
combo_(new gui::combo(display, std::vector<std::string>())),
values_()
{
std::vector<std::string> items;
BOOST_FOREACH(const config& item, cfg.child_range("item")) {
items.push_back(item["name"]);
values_.push_back(item["value"]);
}
combo_->set_items(items);
combo_->set_help_string(cfg["description"]);
set_value(cfg["default"]);
}
combo_display::~combo_display()
{
delete label_;
delete combo_;
}
void combo_display::layout(int &xpos, int &ypos, int border_size, gui::scrollpane *pane)
{
pane->add_widget(label_, xpos, ypos);
pane->add_widget(combo_, xpos + label_->width() + border_size, ypos);
ypos += std::max(label_->height(), combo_->height()) + border_size;
}
void combo_display::set_value(const config::attribute_value &val)
{
const std::string value = val;
for (size_t i = 0; i<values_.size(); i++) {
if (value == values_[i]) {
combo_->set_selected(i);
break;
}
}
}
config::attribute_value combo_display::get_value() const
{
config::attribute_value res;
res = values_[combo_->selected()];
return res;
}
void combo_display::hide_children(bool hide)
{
label_->hide(hide);
combo_->hide(hide);
}
} // namespace options
} // namespace mp