The Battle for Wesnoth  1.19.0-dev
mp_options_helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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 
30 namespace gui2::dialogs
31 {
32 
34  : create_engine_(create_engine)
35  , options_tree_(find_widget<tree_view>(&window, "custom_options", false))
36  , no_options_notice_(find_widget<styled_widget>(&window, "no_options_notice", false))
37  , node_data_map_()
38  , visible_options_()
39  , options_data_()
40 {
41  for(const auto c : preferences::options().all_children_range()) {
42  for(const auto& saved_option : c.cfg.child_range("option")) {
43  options_data_[c.cfg["id"]][saved_option["id"]] = saved_option["value"];
44  }
45  }
46 
48 }
49 
51 {
52  visible_options_.clear();
53  node_data_map_.clear();
55 
59 }
60 
62 {
63  std::string type;
64 
66  type = "campaign";
67  } else {
68  type = "multiplayer";
69  }
70 
71  // For game options, we check for both types and remove them. This is to prevent options from a game
72  // of one type remaining visible when selecting a game of another type.
73  remove_nodes_for_type("campaign");
74  int pos = remove_nodes_for_type("multiplayer");
75 
77 
79 }
80 
82 {
83  static const std::string type = "era";
84 
85  int pos = remove_nodes_for_type(type);
86 
88 
90 }
91 
93 {
94  static const std::string type = "modification";
95 
96  int pos = remove_nodes_for_type(type);
97 
98  for(const auto& mod : create_engine_.active_mods_data()) {
99  display_custom_options(type, pos, *mod->cfg);
100  }
101 
103 }
104 
106 {
107  // Remove all visible options of the specified source type
108  auto vo_iter = std::remove_if(visible_options_.begin(), visible_options_.end(), [&type](const option_source& source) {
109  return source.level_type == type;
110  });
111 
112  visible_options_.erase(vo_iter, visible_options_.end());
113 
114  // Get the node data for this specific source type
116 
117  auto node_data_map_iter = node_data_map_.end();
118  std::tie(node_data_map_iter, std::ignore) = node_data_map_.emplace(type, type_node_data());
119 
120  data = &node_data_map_iter->second;
121 
122  node_vector& type_node_vector = data->nodes;
123 
124  // The position to insert a new node of this type. If no nodes exist yet, the default value (-1) is
125  // accepted by tree_view_node as meaning at-end.
126  int& position = data->position;
127 
128  // Remove each node in reverse, so that in the end we have the position of the first node removed
129  for(auto i = type_node_vector.rbegin(); i != type_node_vector.rend(); i++) {
130  position = options_tree_.remove_node(*i).second;
131  }
132 
133  type_node_vector.clear();
134 
135  return position;
136 }
137 
139 {
140  // No custom options, display a message
142 }
143 
144 template<typename T>
146 {
147  options_data_[source.id][widget->id()] = widget->get_value();
148 }
149 
150 template<>
152 {
153  options_data_[source.id][widget->id()] = widget->get_value_bool();
154 }
155 
157 {
158  options_data_[source.id][widget->id()] = cfg.child_range("item")[widget->get_value()]["value"].str();
159 }
160 
161 void mp_options_helper::reset_options_data(const option_source& source, bool& handled, bool& halt)
162 {
163  options_data_[source.id].clear();
164 
165  if(source.level_type == "campaign" || source.level_type == "multiplayer") {
167  } else if(source.level_type == "era") {
169  } else if(source.level_type == "modification") {
171  }
172 
173  handled = true;
174  halt = true;
175 }
176 
177 template<typename T>
178 std::pair<T*, config::attribute_value> mp_options_helper::add_node_and_get_widget(
179  tree_view_node& option_node, const std::string& id, data_map& data, const config& cfg)
180 {
181  tree_view_node& node = option_node.add_child(id + "_node", data);
182 
183  T* widget = dynamic_cast<T*>(node.find(id, true));
185 
186  const std::string widget_id = cfg["id"];
187 
188  auto& option_config = options_data_[visible_options_.back().id];
189  if(!option_config.has_attribute(widget_id) || option_config[widget_id].empty()) {
190  option_config[widget_id] = cfg["default"];
191  }
192 
193  widget->set_id(widget_id);
194  widget->set_tooltip(cfg["description"]);
195 
196  return {widget, option_config[widget_id]};
197 }
198 
199 void mp_options_helper::display_custom_options(const std::string& type, int node_position, const config& cfg)
200 {
201  // Needed since some compilers don't like passing just {}
202  static const widget_data empty_map;
203 
204  // This ensures that any game, era, or mod with no options doesn't get an entry in the visible_options_
205  // vector and prevents invalid options from different games, era, or mods being created when the options
206  // config is created.
207  if(!cfg.has_child("options")) {
208  return;
209  }
210 
211  visible_options_.push_back({type, cfg["id"]});
212 
213  // Get the node vector for this specific source type
214  node_vector& type_node_vector = node_data_map_[type].nodes;
215 
216  for(const auto& options : cfg.child_range("options")) {
219 
220  item["label"] = cfg["name"];
221  data.emplace("tree_view_node_label", item);
222 
223  tree_view_node& option_node = options_tree_.add_node("option_node", data, node_position);
224  type_node_vector.push_back(&option_node);
225 
226  for(const config::any_child opt : options.all_children_range()) {
227  data.clear();
228  item.clear();
229 
230  const config& option_cfg = opt.cfg;
231 
232  const auto add_name = [&](const std::string& id) {
233  item["label"] = option_cfg["name"];
234  data.emplace(id, item);
235  };
236 
238 
239  if(opt.key == "checkbox") {
240  add_name("option_checkbox");
241 
242  toggle_button* checkbox;
243  std::tie(checkbox, val) = add_node_and_get_widget<toggle_button>(option_node, "option_checkbox", data, option_cfg);
244 
245  checkbox->set_value(val.to_bool());
246 
248  std::bind(&mp_options_helper::update_options_data_map<toggle_button>, this, checkbox, visible_options_.back()));
249 
250  } else if(opt.key == "spacer") {
251  option_node.add_child("options_spacer_node", empty_map);
252 
253  } else if(opt.key == "choice") {
254  if(!option_cfg.has_child("item")) {
255  continue;
256  }
257 
258  add_name("menu_button_label");
259 
260  std::vector<config> combo_items;
261  std::vector<std::string> combo_values;
262 
263  for(auto i : option_cfg.child_range("item")) {
264  // Comboboxes expect this key to be 'label' not 'name'
265  i["label"] = i["name"];
266 
267  combo_items.push_back(i);
268  combo_values.push_back(i["value"]);
269  }
270 
271  menu_button* menu;
272  std::tie(menu, val) = add_node_and_get_widget<menu_button>(option_node, "option_menu_button", data, option_cfg);
273 
274  // Needs to be called before set_selected
275  menu->set_values(combo_items);
276 
277  auto iter = std::find(combo_values.begin(), combo_values.end(), val.str());
278 
279  if(iter != combo_values.end()) {
280  menu->set_selected(std::distance(combo_values.begin(), iter));
281  }
282 
284  std::bind(&mp_options_helper::update_options_data_map_menu_button, this, menu, visible_options_.back(), option_cfg));
285 
286  } else if(opt.key == "slider") {
287  add_name("slider_label");
288 
289  slider* slide;
290  std::tie(slide, val) = add_node_and_get_widget<slider>(option_node, "option_slider", data, option_cfg);
291 
292  slide->set_value_range(option_cfg["min"].to_int(), option_cfg["max"].to_int());
293  slide->set_step_size(option_cfg["step"].to_int(1));
294  slide->set_value(val.to_int());
295 
297  std::bind(&mp_options_helper::update_options_data_map<slider>, this, slide, visible_options_.back()));
298 
299  } else if(opt.key == "entry") {
300  add_name("text_entry_label");
301 
302  text_box* textbox;
303  std::tie(textbox, val) = add_node_and_get_widget<text_box>(option_node, "option_text_entry", data, option_cfg);
304 
305  textbox->set_value(val.str());
306  textbox->set_text_changed_callback(
307  std::bind(&mp_options_helper::update_options_data_map<text_box>, this, textbox, visible_options_.back()));
308  }
309  }
310 
311  // Add the Defaults button at the end
312  tree_view_node& node = option_node.add_child("options_default_button", empty_map);
313 
314  connect_signal_mouse_left_click(find_widget<button>(&node, "reset_option_values", false),
316  std::placeholders::_3, std::placeholders::_4));
317  }
318 }
319 
321 {
322  config options;
323  for(const auto& source : visible_options_) {
324  config& mod = options.add_child(source.level_type);
325  mod["id"] = source.id;
326 #if 0
327  // TODO: enable this as soon as we drop the old mp configure screen.
328  mod.add_child("options", options_data_[source.id]);
329 #else
330  for(const auto& option : options_data_[source.id].attribute_range()) {
331  mod.add_child("option", config {"id", option.first, "value", option.second});
332  }
333 #endif
334  }
335 
336  return options;
337 }
338 
339 } // 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:893
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:159
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:317
const_all_children_itors all_children_range() const
In-order iteration over all children.
Definition: config.cpp:887
child_itors child_range(config_key_type key)
Definition: config.cpp:273
config & add_child(config_key_type key)
Definition: config.cpp:441
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:59
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:59
void set_step_size(int step_size)
Definition: slider.cpp:276
virtual void set_value(int value) override
Inherited from integer_selector.
Definition: slider.cpp:81
void set_value_range(int min_value, int max_value)
Definition: slider.cpp:250
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:60
bool empty() const
Definition: tree_view.cpp:99
tree_view_node & add_node(const std::string &id, const widget_data &data, const int index=-1)
Definition: tree_view.cpp:56
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:62
Base class for all widgets.
Definition: widget.hpp:53
void set_visible(const visibility visible)
Definition: widget.cpp:470
void set_id(const std::string &id)
Definition: widget.cpp:98
const std::string & id() const
Definition: widget.cpp:110
@ 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:63
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:207
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:203
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
t_string missing_widget(const std::string &id)
Returns a default error message if a mandatory widget is omitted.
Definition: helper.cpp:89
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
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:412
const config & options()
Definition: game.cpp:552
std::string_view data
Definition: picture.cpp:194
mock_char c
#define VALIDATE(cond, message)
The macro to use for the validation of WML.