The Battle for Wesnoth  1.17.23+dev
mp_options_helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "preferences/game.hpp"
21 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/slider.hpp"
25 #include "gui/widgets/text_box.hpp"
28 #include "gui/widgets/window.hpp"
29 #include "deprecation.hpp"
30 
31 namespace gui2::dialogs
32 {
33 
35  : create_engine_(create_engine)
36  , options_tree_(find_widget<tree_view>(&window, "custom_options", false))
37  , no_options_notice_(find_widget<styled_widget>(&window, "no_options_notice", false))
38  , node_data_map_()
39  , visible_options_()
40  , options_data_()
41 {
42  for(const auto c : preferences::options().all_children_range()) {
43  for(const auto& saved_option : c.cfg.child_range("option")) {
44  options_data_[c.cfg["id"]][saved_option["id"]] = saved_option["value"];
45  }
46  }
47 
49 }
50 
52 {
53  visible_options_.clear();
54  node_data_map_.clear();
56 
60 }
61 
63 {
64  std::string type;
65 
67  type = "campaign";
68  } else {
69  type = "multiplayer";
70  }
71 
72  // For game options, we check for both types and remove them. This is to prevent options from a game
73  // of one type remaining visible when selecting a game of another type.
74  remove_nodes_for_type("campaign");
75  int pos = remove_nodes_for_type("multiplayer");
76 
78 
80 }
81 
83 {
84  static const std::string type = "era";
85 
86  int pos = remove_nodes_for_type(type);
87 
89 
91 }
92 
94 {
95  static const std::string type = "modification";
96 
97  int pos = remove_nodes_for_type(type);
98 
99  for(const auto& mod : create_engine_.active_mods_data()) {
100  display_custom_options(type, pos, *mod->cfg);
101  }
102 
104 }
105 
107 {
108  // Remove all visible options of the specified source type
109  auto vo_iter = std::remove_if(visible_options_.begin(), visible_options_.end(), [&type](const option_source& source) {
110  return source.level_type == type;
111  });
112 
113  visible_options_.erase(vo_iter, visible_options_.end());
114 
115  // Get the node data for this specific source type
117 
118  auto node_data_map_iter = node_data_map_.end();
119  std::tie(node_data_map_iter, std::ignore) = node_data_map_.emplace(type, type_node_data());
120 
121  data = &node_data_map_iter->second;
122 
123  node_vector& type_node_vector = data->nodes;
124 
125  // The position to insert a new node of this type. If no nodes exist yet, the default value (-1) is
126  // accepted by tree_view_node as meaning at-end.
127  int& position = data->position;
128 
129  // Remove each node in reverse, so that in the end we have the position of the first node removed
130  for(auto i = type_node_vector.rbegin(); i != type_node_vector.rend(); i++) {
131  position = options_tree_.remove_node(*i).second;
132  }
133 
134  type_node_vector.clear();
135 
136  return position;
137 }
138 
140 {
141  // No custom options, display a message
143 }
144 
145 template<typename T>
147 {
148  options_data_[source.id][widget->id()] = widget->get_value();
149 }
150 
151 template<>
153 {
154  options_data_[source.id][widget->id()] = widget->get_value_bool();
155 }
156 
158 {
159  options_data_[source.id][widget->id()] = cfg.child_range("item")[widget->get_value()]["value"].str();
160 }
161 
162 void mp_options_helper::reset_options_data(const option_source& source, bool& handled, bool& halt)
163 {
164  options_data_[source.id].clear();
165 
166  if(source.level_type == "campaign" || source.level_type == "multiplayer") {
168  } else if(source.level_type == "era") {
170  } else if(source.level_type == "modification") {
172  }
173 
174  handled = true;
175  halt = true;
176 }
177 
178 template<typename T>
179 std::pair<T*, config::attribute_value> mp_options_helper::add_node_and_get_widget(
180  tree_view_node& option_node, const std::string& id, data_map& data, const config& cfg)
181 {
182  tree_view_node& node = option_node.add_child(id + "_node", data);
183 
184  T* widget = dynamic_cast<T*>(node.find(id, true));
186 
187  const std::string widget_id = cfg["id"];
188 
189  auto& option_config = options_data_[visible_options_.back().id];
190  if(!option_config.has_attribute(widget_id) || option_config[widget_id].empty()) {
191  option_config[widget_id] = cfg["default"];
192  }
193 
194  widget->set_id(widget_id);
195  widget->set_tooltip(cfg["description"]);
196 
197  return {widget, option_config[widget_id]};
198 }
199 
200 void mp_options_helper::display_custom_options(const std::string& type, int node_position, const config& cfg)
201 {
202  // Needed since some compilers don't like passing just {}
203  static const widget_data empty_map;
204 
205  // This ensures that any game, era, or mod with no options doesn't get an entry in the visible_options_
206  // vector and prevents invalid options from different games, era, or mods being created when the options
207  // config is created.
208  if(!cfg.has_child("options")) {
209  return;
210  }
211 
212  visible_options_.push_back({type, cfg["id"]});
213 
214  // Get the node vector for this specific source type
215  node_vector& type_node_vector = node_data_map_[type].nodes;
216 
217  for(const auto& options : cfg.child_range("options")) {
220 
221  item["label"] = cfg["name"];
222  data.emplace("tree_view_node_label", item);
223 
224  tree_view_node& option_node = options_tree_.add_node("option_node", data, node_position);
225  type_node_vector.push_back(&option_node);
226 
227  for(const config::any_child opt : options.all_children_range()) {
228  data.clear();
229  item.clear();
230 
231  const config& option_cfg = opt.cfg;
232 
233  const auto add_name = [&](const std::string& id) {
234  item["label"] = option_cfg["name"];
235  data.emplace(id, item);
236  };
237 
239 
240  if(opt.key == "checkbox") {
241  add_name("option_checkbox");
242 
243  toggle_button* checkbox;
244  std::tie(checkbox, val) = add_node_and_get_widget<toggle_button>(option_node, "option_checkbox", data, option_cfg);
245 
246  checkbox->set_value(val.to_bool());
247 
249  std::bind(&mp_options_helper::update_options_data_map<toggle_button>, this, checkbox, visible_options_.back()));
250 
251  } else if(opt.key == "spacer") {
252  option_node.add_child("options_spacer_node", empty_map);
253 
254  } else if(opt.key == "choice") {
255  if(!option_cfg.has_child("item")) {
256  continue;
257  }
258 
259  add_name("menu_button_label");
260 
261  std::vector<config> combo_items;
262  std::vector<std::string> combo_values;
263 
264  for(auto i : option_cfg.child_range("item")) {
265  // Comboboxes expect this key to be 'label' not 'name'
266  i["label"] = i["name"];
267 
268  combo_items.push_back(i);
269  combo_values.push_back(i["value"]);
270  }
271 
272  menu_button* menu;
273  std::tie(menu, val) = add_node_and_get_widget<menu_button>(option_node, "option_menu_button", data, option_cfg);
274 
275  // Needs to be called before set_selected
276  menu->set_values(combo_items);
277 
278  auto iter = std::find(combo_values.begin(), combo_values.end(), val.str());
279 
280  if(iter != combo_values.end()) {
281  menu->set_selected(std::distance(combo_values.begin(), iter));
282  }
283 
285  std::bind(&mp_options_helper::update_options_data_map_menu_button, this, menu, visible_options_.back(), option_cfg));
286 
287  } else if(opt.key == "slider") {
288  add_name("slider_label");
289 
290  slider* slide;
291  std::tie(slide, val) = add_node_and_get_widget<slider>(option_node, "option_slider", data, option_cfg);
292 
293  slide->set_value_range(option_cfg["min"].to_int(), option_cfg["max"].to_int());
294  slide->set_step_size(option_cfg["step"].to_int(1));
295  slide->set_value(val.to_int());
296 
298  std::bind(&mp_options_helper::update_options_data_map<slider>, this, slide, visible_options_.back()));
299 
300  } else if(opt.key == "entry") {
301  add_name("text_entry_label");
302 
303  text_box* textbox;
304  std::tie(textbox, val) = add_node_and_get_widget<text_box>(option_node, "option_text_entry", data, option_cfg);
305 
306  textbox->set_value(val.str());
307  textbox->set_text_changed_callback(
308  std::bind(&mp_options_helper::update_options_data_map<text_box>, this, textbox, visible_options_.back()));
309  }
310  }
311 
312  // Add the Defaults button at the end
313  tree_view_node& node = option_node.add_child("options_default_button", empty_map);
314 
315  connect_signal_mouse_left_click(find_widget<button>(&node, "reset_option_values", false),
317  std::placeholders::_3, std::placeholders::_4));
318  }
319 }
320 
322 {
323  config options;
324  for(const auto& source : visible_options_) {
325  config& mod = options.add_child(source.level_type);
326  mod["id"] = source.id;
327 #if 0
328  // TODO: enable this as soon as we drop the old mp configure screen.
329  mod.add_child("options", options_data_[source.id]);
330 #else
331  for(const auto& option : options_data_[source.id].attribute_range()) {
332  mod.add_child("option", config {"id", option.first, "value", option.second});
333  }
334 #endif
335  }
336 
337  return options;
338 }
339 
340 } // namespace dialogs
static void add_name(std::string &temp_string, bool active, const std::string name, std::set< std::string > &checking_name)
Definition: abilities.cpp:895
Variant for storing WML attributes.
std::string str(const std::string &fallback="") const
bool to_bool(bool def=false) const
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:321
const_all_children_itors all_children_range() const
In-order iteration over all children.
Definition: config.cpp:891
child_itors child_range(config_key_type key)
Definition: config.cpp:277
config & add_child(config_key_type key)
Definition: config.cpp:445
std::map< std::string, config > options_data_
void reset_options_data(const option_source &source, bool &handled, bool &halt)
mp_options_helper(window &window, ng::create_engine &create_engine)
void update_options_data_map_menu_button(menu_button *widget, const option_source &source, const config &cfg)
void display_custom_options(const std::string &type, int node_position, const config &data)
std::map< std::string, type_node_data > node_data_map_
std::vector< tree_view_node * > node_vector
std::pair< T *, config::attribute_value > add_node_and_get_widget(tree_view_node &option_node, const std::string &id, data_map &data, const config &cfg)
std::vector< option_source > visible_options_
int remove_nodes_for_type(const std::string &type)
void update_options_data_map(T *widget, const option_source &source)
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:62
void set_selected(unsigned selected, bool fire_event=true)
void set_values(const std::vector<::config > &values, unsigned selected=0)
A slider is a control that can select a value by moving a grip on a groove.
Definition: slider.hpp:60
void set_step_size(int step_size)
Definition: slider.cpp:277
virtual void set_value(int value) override
Inherited from integer_selector.
Definition: slider.cpp:82
void set_value_range(int min_value, int max_value)
Definition: slider.cpp:251
Base class for all visible items.
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
Class for a single line text area.
Definition: text_box.hpp:142
Class for a toggle button.
virtual void set_value(unsigned selected, bool fire_event=false) override
Inherited from selectable_item.
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
tree_view_node & add_child(const std::string &id, const widget_data &data, const int index=-1)
Constructs a new child node.
A tree view is a control that holds several items of the same or different types.
Definition: tree_view.hpp:61
bool empty() const
Definition: tree_view.cpp:100
tree_view_node & add_node(const std::string &id, const widget_data &data, const int index=-1)
Definition: tree_view.cpp:57
std::pair< std::shared_ptr< tree_view_node >, int > remove_node(tree_view_node *node)
Removes the given node as a child of its parent node.
Definition: tree_view.cpp:63
Base class for all widgets.
Definition: widget.hpp:54
void set_visible(const visibility visible)
Definition: widget.cpp:471
void set_id(const std::string &id)
Definition: widget.cpp:99
const std::string & id() const
Definition: widget.cpp:111
@ visible
The user sets the widget visible, that means:
@ invisible
The user set the widget invisible, that means:
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
const config & curent_era_cfg() const
std::vector< extras_metadata_ptr > active_mods_data()
bool is_campaign() const
Wrapper to simplify the is-type-campaign-or-sp-campaign check.
level & current_level() const
const config & data() const
std::size_t i
Definition: function.cpp:968
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
This file contains the window object, this object is a top level container which has the event manage...
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:205
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:179
t_string missing_widget(const std::string &id)
Returns a default error message if a mandatory widget is omitted.
Definition: helper.cpp:91
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
std::map< std::string, t_string > widget_item
Definition: widget.hpp:32
T * find_widget(utils::const_clone_ptr< widget, T > widget, const std::string &id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition: find_widget.hpp:69
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:414
const config & options()
Definition: game.cpp:555
std::string_view data
Definition: picture.cpp:199
mock_char c
#define VALIDATE(cond, message)
The macro to use for the validation of WML.