The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mp_options.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2016 by Boldizsár Lipka <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef MP_OPTIONS_HPP_INCLUDED
16 #define MP_OPTIONS_HPP_INCLUDED
17 
18 #include <string>
19 #include "config.hpp"
20 #include "gui/widgets/widget.hpp"
21 #include "gui/widgets/window.hpp"
22 #include "widgets/scrollpane.hpp"
23 #include "widgets/label.hpp"
24 #include "widgets/button.hpp"
25 #include "widgets/textbox.hpp"
26 #include "widgets/slider.hpp"
27 #include "widgets/combo.hpp"
28 
29 namespace mp
30 {
31 
32 namespace options
33 {
34 
36 {
37 public:
38  virtual ~option_display() {}
39  virtual void layout(int& xpos, int& ypos, int w, int border_size, gui::scrollpane* pane) = 0;
40  virtual void set_value(const config::attribute_value& val) = 0;
41  virtual config::attribute_value get_value() const = 0;
42  virtual void process_event() {}
43  virtual void hide_children(bool hide) = 0;
44 };
45 
47 {
48 public:
49  entry_display(CVideo& video, const config& cfg);
51 
52  void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane);
55  virtual void hide_children(bool hide);
56 
57 private:
60 };
61 
63 {
64 public:
65  slider_display(CVideo& video, const config& cfg);
67 
68  void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane);
71  void process_event();
72  virtual void hide_children(bool hide);
73 
74 private:
75  void update_label();
76 
81 };
82 
84 {
85 public:
86  checkbox_display(CVideo& video, const config& cfg);
88 
89  void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane);
92  virtual void hide_children(bool hide);
93 private:
95 };
96 
98 {
99 public:
100  combo_display(CVideo& video, const config& cfg);
101  ~combo_display();
102 
103  void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane);
106  void hide_children(bool hide);
107 
108 private:
111 
112  std::vector<std::string> values_;
113 };
114 
116 {
117 public:
118  title_display(CVideo& video, const std::string& label);
119  ~title_display();
120 
121  void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane);
122  void set_value(const config::attribute_value &/*val*/) {}
124  virtual void hide_children(bool hide);
125 
126 private:
128 };
129 
130 class manager;
131 
133 {
134 public:
135  reset_display(CVideo& video, const std::string& comp, manager &m);
136  ~reset_display();
137 
138  void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane);
139  void set_value(const config::attribute_value &/*val*/) {}
141  void hide_children(bool hide);
142  void process_event();
143 
144 private:
148 };
149 
150 class manager
151 {
152 public:
153 
154  /**
155  * Constructor.
156  *
157  * @param gamecfg The config object holding all eras, scenarios
158  * and modifications.
159  *
160  * @param display The screen to display the dialog on.
161  *
162  * @param initial_value The initial values for each option.
163  */
164  manager(const config& gamecfg, CVideo& video, gui::scrollpane* pane, const config& initial_value);
165 
166  ~manager();
167 
168  /**
169  * Set the current values the options. This overrides ALL previously set
170  * values, even if a not all options are provided a new value for.
171  *
172  * @param values The new values for each option.
173  */
174  void set_values(const config& values);
175 
176  /**
177  * Sets the selected era. Whenever show_dialog is called, only
178  * options for the selected era will be displayed.
179  *
180  * @param id The era's id.
181  */
182  void set_era(const std::string& id);
183 
184  /**
185  * Sets the selected scenario. Whenever show_dialog is called, only
186  * options for the selected scenario will be displayed.
187  *
188  * @param id The scenario's id.
189  */
190  void set_scenario(const std::string& id);
191 
192  /**
193  * Sets the selected campaign. Whenever show_dialog is called, only
194  * options for the campaign scenario will be displayed.
195  *
196  * @param id The campaign's id.
197  */
198  void set_campaign(const std::string& id);
199 
200  /**
201  * Sets the activated modifications. Whenever show_dialog is called, only
202  * options for the activated modifications will be displayed.
203  *
204  * @param ids The ids of the modifications
205  */
206  void set_modifications(const std::vector<std::string>& ids);
207 
208  void layout_widgets(int startx, int starty, int w);
209  void process_event();
210  void hide_children(bool hide=true);
211 
212  /**
213  * Returns the the values for each option.
214  *
215  * @return A config containing the values.
216  */
217  const config& get_values();
218 
219  void init_widgets();
220 
221  void restore_defaults(const std::string &component);
222 
223  bool has_options() const;
224 private:
225 
226  /** Stores needed info about each element and their configuration options */
228 
229  /** Stores the selected values for each option */
231 
232  /** The screen to display the dialog on */
234 
235  /** The scrollarea to put the widgets on */
237 
238  /** The id of the selected era */
240 
241  /** The id of the selected [multiplayer] or [campaign]*/
244  /** The ids of the selected modifications */
245  std::vector<std::string> modifications_;
246 
247  std::map<std::string, option_display*> widgets_;
248  std::vector<option_display*> widgets_ordered_;
249 
250  /**
251  * Adds the necessary information about the specified component
252  * to options_info_.
253  *
254  * @param cfg The component's data.
255  * @param key The component's type.
256  */
257  void init_info(const config& cfg, const std::string& key);
258 
259  /**
260  * Returns the node which holds the selected value of an option. If that
261  * node is not yet created, the function creates it.
262  *
263  * @param id The id of the option.
264  *
265  * @return A reference to the config which the value
266  * for this option should be written into.
267  */
268  config& get_value_cfg(const std::string& id);
269 
270  /**
271  * Returns the node which holds the selected value of an option. If that
272  * node is not yet created, the function returns an empty config.
273  *
274  * @param id The id of the option.
275  *
276  * @return A reference to the config which the value
277  * for this option should be written into or
278  * an empty config if that doesn't exist.
279  */
280  const config& get_value_cfg_or_empty(const std::string& id) const;
281 
282  /**
283  * Returns the information about an option.
284  *
285  * @param id The id of the option.
286  *
287  * @return The config object which contains the
288  * settings of the option, or an empty config
289  * if the option was not found.
290  */
291  const config& get_option_info_cfg(const std::string& id) const;
292 
293  const config& get_component_cfg(const std::string& id) const;
294 
295  /**
296  * Finds the parent node of an options.
297  *
298  * @param id The id of the option.
299  *
300  * @return A config::any_child object containing the
301  * key and the data of the parent node, or ""
302  * for the key and an empty config if the
303  * option was not found.
304  */
306 
307  /**
308  * Retrieves the saved value for a certain option, or the default, if
309  * there's no such.
310  *
311  * @param id The id of the option.
312  *
313  * @return The value saved in values_ for this option
314  * or its specified default value if a saved
315  * value can't be found.
316  */
318 
319  /**
320  * Retrieves the default value for a certain option.
321  *
322  * @param id The id of the option.
323  *
324  * @return The default value for this option.
325  */
327 
328  /**
329  * Writes all the values for the options of a certain component from a
330  * specified window into values_.
331  *
332  * @param key The component's type.
333  * @param id The component's id.
334  */
335  void extract_values(const std::string& key, const std::string& id);
336 
337  void update_values();
338 
339  /**
340  * Decides whether a config is a sane option node or not.
341  * A valid option node:
342  * - Must have an id field.
343  * - Its key must be "slider", "entry" or "checkbox"
344  *
345  * @param key The option's key.
346  * @param option The option's data.
347  *
348  * @return True if the option is valid, false if not.
349  */
350  static bool is_valid_option(const std::string& key, const config& option);
351 
352  bool is_active(const std::string& id) const;
353 };
354 
355 } // namespace options
356 
357 } // namespace mp
358 #endif
virtual void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane)=0
void set_value(const config::attribute_value &)
Definition: mp_options.hpp:122
void restore_defaults(const std::string &component)
Definition: mp_options.cpp:92
title_display(CVideo &video, const std::string &label)
Definition: mp_options.cpp:499
void set_value(const config::attribute_value &val)
Definition: mp_options.cpp:382
entry_display(CVideo &video, const config &cfg)
Definition: mp_options.cpp:362
CVideo & video_
The screen to display the dialog on.
Definition: mp_options.hpp:233
virtual void process_event()
Definition: mp_options.hpp:42
gui::scrollpane * pane_
The scrollarea to put the widgets on.
Definition: mp_options.hpp:236
virtual void hide_children(bool hide)
Definition: mp_options.cpp:515
std::string era_
The id of the selected era.
Definition: mp_options.hpp:239
void hide_children(bool hide=true)
Definition: mp_options.cpp:197
void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane)
Definition: mp_options.cpp:584
Definition: video.hpp:58
void set_scenario(const std::string &id)
Sets the selected scenario.
Definition: mp_options.cpp:161
std::vector< std::string > modifications_
The ids of the selected modifications.
Definition: mp_options.hpp:245
config options_info_
Stores needed info about each element and their configuration options.
Definition: mp_options.hpp:227
This file contains the window object, this object is a top level container which has the event manage...
void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane)
Definition: mp_options.cpp:476
GLuint const GLfloat * val
Definition: glew.h:2614
config values_
Stores the selected values for each option.
Definition: mp_options.hpp:230
Scrollpane.
Definition: scrollpane.hpp:30
void set_modifications(const std::vector< std::string > &ids)
Sets the activated modifications.
Definition: mp_options.cpp:173
void init_info(const config &cfg, const std::string &key)
Adds the necessary information about the specified component to options_info_.
Definition: mp_options.cpp:37
reset_display(CVideo &video, const std::string &comp, manager &m)
Definition: mp_options.cpp:573
std::string scenario_
The id of the selected [multiplayer] or [campaign].
Definition: mp_options.hpp:242
GLuint GLenum option
Definition: glew.h:2815
Definitions for the interface to Wesnoth Markup Language (WML).
Variant for storing WML attributes.
Definition: config.hpp:223
config::attribute_value get_value() const
Definition: mp_options.cpp:560
GLboolean GLenum GLenum GLvoid * values
Definition: glew.h:3799
bool is_active(const std::string &id) const
Definition: mp_options.cpp:356
This module controls the multiplayer lobby.
config::attribute_value get_default_value(const std::string &id) const
Retrieves the default value for a certain option.
Definition: mp_options.cpp:316
const config & options()
void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane)
Definition: mp_options.cpp:375
void hide_children(bool hide)
Definition: mp_options.cpp:590
config::attribute_value get_value() const
Definition: mp_options.hpp:140
const config & get_component_cfg(const std::string &id) const
Definition: mp_options.cpp:284
virtual void hide_children(bool hide)=0
virtual void hide_children(bool hide)
Definition: mp_options.cpp:494
std::map< std::string, option_display * > widgets_
Definition: mp_options.hpp:247
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
config & get_value_cfg(const std::string &id)
Returns the node which holds the selected value of an option.
Definition: mp_options.cpp:213
void set_era(const std::string &id)
Sets the selected era.
Definition: mp_options.cpp:156
void extract_values(const std::string &key, const std::string &id)
Writes all the values for the options of a certain component from a specified window into values_...
Definition: mp_options.cpp:323
void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane)
Definition: mp_options.cpp:422
slider_display(CVideo &video, const config &cfg)
Definition: mp_options.cpp:400
void set_value(const config::attribute_value &)
Definition: mp_options.hpp:139
checkbox_display(CVideo &video, const config &cfg)
Definition: mp_options.cpp:464
std::vector< std::string > values_
Definition: mp_options.hpp:112
virtual void hide_children(bool hide)
Definition: mp_options.cpp:451
config::attribute_value get_value() const
Definition: mp_options.hpp:123
const std::string label_text_
Definition: mp_options.hpp:80
std::vector< option_display * > widgets_ordered_
Definition: mp_options.hpp:248
void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane)
Definition: mp_options.cpp:542
manager(const config &gamecfg, CVideo &video, gui::scrollpane *pane, const config &initial_value)
Constructor.
Definition: mp_options.cpp:110
bool has_options() const
Definition: mp_options.cpp:105
config::any_child get_option_parent(const std::string &id) const
Finds the parent node of an options.
Definition: mp_options.cpp:252
void layout(int &xpos, int &ypos, int w, int border_size, gui::scrollpane *pane)
Definition: mp_options.cpp:508
combo_display(CVideo &video, const config &cfg)
Definition: mp_options.cpp:520
config::attribute_value get_value() const
Definition: mp_options.cpp:387
virtual config::attribute_value get_value() const =0
config::attribute_value get_stored_value(const std::string &id) const
Retrieves the saved value for a certain option, or the default, if there's no such.
Definition: mp_options.cpp:303
void set_value(const config::attribute_value &val)
Definition: mp_options.cpp:549
void set_value(const config::attribute_value &val)
Definition: mp_options.cpp:431
virtual void hide_children(bool hide)
Definition: mp_options.cpp:394
const config & get_option_info_cfg(const std::string &id) const
Returns the information about an option.
Definition: mp_options.cpp:269
const GLdouble * m
Definition: glew.h:6968
void set_campaign(const std::string &id)
Sets the selected campaign.
Definition: mp_options.cpp:167
config::attribute_value get_value() const
Definition: mp_options.cpp:487
config::attribute_value get_value() const
Definition: mp_options.cpp:436
const config & get_value_cfg_or_empty(const std::string &id) const
Returns the node which holds the selected value of an option.
Definition: mp_options.cpp:237
const config & get_values()
Returns the the values for each option.
Definition: mp_options.cpp:207
void hide_children(bool hide)
Definition: mp_options.cpp:567
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual void set_value(const config::attribute_value &val)=0
static bool is_valid_option(const std::string &key, const config &option)
Decides whether a config is a sane option node or not.
Definition: mp_options.cpp:350
const GLuint * ids
Definition: glew.h:1652
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void layout_widgets(int startx, int starty, int w)
Definition: mp_options.cpp:178
void set_values(const config &values)
Set the current values the options.
Definition: mp_options.cpp:151
void set_value(const config::attribute_value &val)
Definition: mp_options.cpp:482