-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
mp_options_helper.cpp
346 lines (259 loc) · 10.6 KB
/
mp_options_helper.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
/*
Copyright (C) 2008 - 2018 by 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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/multiplayer/mp_options_helper.hpp"
#include "preferences/game.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/menu_button.hpp"
#include "gui/widgets/slider.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/text_box.hpp"
#include "gui/widgets/tree_view.hpp"
#include "gui/widgets/tree_view_node.hpp"
#include "gui/widgets/window.hpp"
namespace gui2
{
namespace dialogs
{
mp_options_helper::mp_options_helper(window& window, ng::create_engine& create_engine)
: create_engine_(create_engine)
, options_tree_(find_widget<tree_view>(&window, "custom_options", false))
, no_options_notice_(find_widget<styled_widget>(&window, "no_options_notice", false))
, node_data_map_()
, visible_options_()
, options_data_()
{
for(const auto& c : preferences::options().all_children_range()) {
// Parse old [option] tag range syntax.
for(const auto& saved_option : c.cfg.child_range("option")) {
options_data_[c.cfg["id"]][saved_option["id"]] = saved_option["value"];
}
// Parse new [options] id = value syntax.
for(const auto& saved_option : c.cfg.child_or_empty("options").attribute_range()) {
options_data_[c.cfg["id"]][saved_option.first] = saved_option.second;
}
}
update_all_options();
}
void mp_options_helper::update_all_options()
{
visible_options_.clear();
node_data_map_.clear();
options_tree_.clear();
update_game_options();
update_era_options();
update_mod_options();
}
void mp_options_helper::update_game_options()
{
const ng::level::TYPE current_type = create_engine_.current_level_type();
std::string type;
if(current_type == ng::level::TYPE::CAMPAIGN || current_type == ng::level::TYPE::SP_CAMPAIGN) {
type = "campaign";
} else {
type = "multiplayer";
}
// For game options, we check for both types and remove them. This is to prevent options from a game
// of one type remaining visible when selecting a game of another type.
remove_nodes_for_type("campaign");
int pos = remove_nodes_for_type("multiplayer");
display_custom_options(type, pos, create_engine_.current_level().data());
update_status_label();
}
void mp_options_helper::update_era_options()
{
static const std::string type = "era";
int pos = remove_nodes_for_type(type);
display_custom_options(type, pos, create_engine_.curent_era_cfg());
update_status_label();
}
void mp_options_helper::update_mod_options()
{
static const std::string type = "modification";
int pos = remove_nodes_for_type(type);
for(const auto& mod : create_engine_.active_mods_data()) {
display_custom_options(type, pos, *mod->cfg);
}
update_status_label();
}
int mp_options_helper::remove_nodes_for_type(const std::string& type)
{
// Remove all visible options of the specified source type
auto vo_iter = std::remove_if(visible_options_.begin(), visible_options_.end(), [&type](const option_source& source) {
return source.level_type == type;
});
visible_options_.erase(vo_iter, visible_options_.end());
// Get the node data for this specific source type
type_node_data* data;
auto node_data_map_iter = node_data_map_.end();
std::tie(node_data_map_iter, std::ignore) = node_data_map_.emplace(type, type_node_data());
data = &node_data_map_iter->second;
node_vector& type_node_vector = data->nodes;
// The position to insert a new node of this type. If no nodes exist yet, the default value (-1) is
// accepted by tree_view_node as meaning at-end.
int& position = data->position;
// Remove each node in reverse, so that in the end we have the position of the first node removed
for(auto i = type_node_vector.rbegin(); i != type_node_vector.rend(); i++) {
position = options_tree_.remove_node(*i);
}
type_node_vector.clear();
return position;
}
void mp_options_helper::update_status_label()
{
// No custom options, display a message
no_options_notice_.set_visible(options_tree_.empty() ? widget::visibility::visible : widget::visibility::invisible);
}
template<typename T>
void mp_options_helper::update_options_data_map(T* widget, const option_source& source)
{
options_data_[source.id][widget->id()] = widget->get_value();
}
template<>
void mp_options_helper::update_options_data_map(toggle_button* widget, const option_source& source)
{
options_data_[source.id][widget->id()] = widget->get_value_bool();
}
void mp_options_helper::update_options_data_map_menu_button(menu_button* widget, const option_source& source, const config& cfg)
{
options_data_[source.id][widget->id()] = cfg.child_range("item")[widget->get_value()]["value"].str();
}
void mp_options_helper::reset_options_data(const option_source& source, bool& handled, bool& halt)
{
options_data_[source.id].clear();
if(source.level_type == "campaign" || source.level_type == "multiplayer") {
update_game_options();
} else if(source.level_type == "era") {
update_era_options();
} else if(source.level_type == "modification") {
update_mod_options();
}
handled = true;
halt = true;
}
template<typename T>
std::pair<T*, config::attribute_value> mp_options_helper::add_node_and_get_widget(
tree_view_node& option_node, const std::string& id, data_map& data, const config& cfg)
{
tree_view_node& node = option_node.add_child(id + "_node", data);
T* widget = dynamic_cast<T*>(node.find(id, true));
VALIDATE(widget, missing_widget(id));
const std::string widget_id = cfg["id"];
auto& option_config = options_data_[visible_options_.back().id];
if(!option_config.has_attribute(widget_id) || option_config[widget_id].empty()) {
option_config[widget_id] = cfg["default"];
}
widget->set_id(widget_id);
widget->set_tooltip(cfg["description"]);
return {widget, option_config[widget_id]};
}
void mp_options_helper::display_custom_options(const std::string& type, int node_position, const config& cfg)
{
// Needed since some compilers don't like passing just {}
static const std::map<std::string, string_map> empty_map;
// This ensures that any game, era, or mod with no options doesn't get an entry in the visible_options_
// vector and prevents invalid options from different games, era, or mods being created when the options
// config is created.
if(!cfg.has_child("options")) {
return;
}
visible_options_.push_back({type, cfg["id"]});
// Get the node vector for this specific source type
node_vector& type_node_vector = node_data_map_[type].nodes;
for(const auto& options : cfg.child_range("options")) {
std::map<std::string, string_map> data;
string_map item;
item["label"] = cfg["name"];
data.emplace("tree_view_node_label", item);
tree_view_node& option_node = options_tree_.add_node("option_node", data, node_position);
type_node_vector.push_back(&option_node);
for(const config::any_child& opt : options.all_children_range()) {
data.clear();
item.clear();
const config& option_cfg = opt.cfg;
const auto add_name = [&](const std::string& id) {
item["label"] = option_cfg["name"];
data.emplace(id, item);
};
config::attribute_value val;
if(opt.key == "checkbox") {
add_name("option_checkbox");
toggle_button* checkbox;
std::tie(checkbox, val) = add_node_and_get_widget<toggle_button>(option_node, "option_checkbox", data, option_cfg);
checkbox->set_value(val.to_bool());
connect_signal_notify_modified(*checkbox,
std::bind(&mp_options_helper::update_options_data_map<toggle_button>, this, checkbox, visible_options_.back()));
} else if(opt.key == "spacer") {
option_node.add_child("options_spacer_node", empty_map);
} else if(opt.key == "choice" || opt.key == "combo") {
if(opt.key == "combo") {
lg::wml_error() << "[options][combo] is deprecated; use [choice] instead\n";
}
if(!option_cfg.has_child("item")) {
continue;
}
add_name("menu_button_label");
std::vector<config> combo_items;
std::vector<std::string> combo_values;
for(auto i : option_cfg.child_range("item")) {
// Comboboxes expect this key to be 'label' not 'name'
i["label"] = i["name"];
combo_items.push_back(i);
combo_values.push_back(i["value"]);
}
menu_button* menu;
std::tie(menu, val) = add_node_and_get_widget<menu_button>(option_node, "option_menu_button", data, option_cfg);
// Needs to be called before set_selected
menu->set_values(combo_items);
auto iter = std::find(combo_values.begin(), combo_values.end(), val.str());
if(iter != combo_values.end()) {
menu->set_selected(iter - combo_values.begin());
}
menu->connect_click_handler(
std::bind(&mp_options_helper::update_options_data_map_menu_button, this, menu, visible_options_.back(), option_cfg));
} else if(opt.key == "slider") {
add_name("slider_label");
slider* slide;
std::tie(slide, val) = add_node_and_get_widget<slider>(option_node, "option_slider", data, option_cfg);
slide->set_value_range(option_cfg["min"].to_int(), option_cfg["max"].to_int());
slide->set_step_size(option_cfg["step"].to_int(1));
slide->set_value(val.to_int());
connect_signal_notify_modified(*slide,
std::bind(&mp_options_helper::update_options_data_map<slider>, this, slide, visible_options_.back()));
} else if(opt.key == "entry") {
add_name("text_entry_label");
text_box* textbox;
std::tie(textbox, val) = add_node_and_get_widget<text_box>(option_node, "option_text_entry", data, option_cfg);
textbox->set_value(val.str());
textbox->set_text_changed_callback(
std::bind(&mp_options_helper::update_options_data_map<text_box>, this, textbox, visible_options_.back()));
}
}
// Add the Defaults button at the end
tree_view_node& node = option_node.add_child("options_default_button", empty_map);
connect_signal_mouse_left_click(find_widget<button>(&node, "reset_option_values", false),
std::bind(&mp_options_helper::reset_options_data, this, visible_options_.back(),
std::placeholders::_3, std::placeholders::_4));
}
}
config mp_options_helper::get_options_config()
{
config options;
for(const auto& source : visible_options_) {
config& mod = options.add_child(source.level_type);
mod["id"] = source.id;
mod.add_child("options", options_data_[source.id]);
}
return options;
}
} // namespace dialogs
} // namespace gui2 // namespace gui2