The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mp_create_game.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include <boost/multi_index_container.hpp>
20 // ^ This is apparently unnecessary, but if removed it doesn't compile...
21 
22 #include "game_preferences.hpp"
23 #include "gettext.hpp"
24 #include "gui/auxiliary/field.hpp"
25 #include "gui/dialogs/helper.hpp"
27 #ifdef GUI2_EXPERIMENTAL_LISTBOX
28 #include "gui/widgets/list.hpp"
29 #else
30 #include "gui/widgets/listbox.hpp"
31 #endif
32 #include "gui/widgets/minimap.hpp"
33 #include "gui/widgets/settings.hpp"
34 #include "settings.hpp"
35 
36 #ifdef GUI2_EXPERIMENTAL_LISTBOX
37 #include "utils/functional.hpp"
38 #endif
39 
40 namespace gui2
41 {
42 
43 REGISTER_DIALOG(mp_create_game)
44 
46  : cfg_(cfg)
47  , scenario_(nullptr)
48  , use_map_settings_(register_bool(
50  true,
51  preferences::use_map_settings,
54  &tmp_create_game::update_map_settings>))
55  , fog_(register_bool("fog", true, preferences::fog, preferences::set_fog))
56  , shroud_(register_bool(
57  "shroud", true, preferences::shroud, preferences::set_shroud))
58  , start_time_(register_bool("random_start_time",
59  true,
60  preferences::random_start_time,
62  , turns_(register_integer(
63  "turn_count", true, preferences::turns, preferences::set_turns))
64  , gold_(register_integer("village_gold",
65  true,
66  preferences::village_gold,
68  , support_(register_integer("village_support",
69  false,
70  preferences::village_support,
72  , experience_(register_integer("experience_modifier",
73  true,
76 {
77 }
78 
80 {
81  find_widget<tminimap>(&window, "minimap", false).set_config(&cfg_);
82 
83  tlistbox& list = find_widget<tlistbox>(&window, "map_list", false);
84 #ifdef GUI2_EXPERIMENTAL_LISTBOX
86  boost::bind(&tmp_create_game::update_map,
87  *this,
88  boost::ref(window)));
89 #else
91  dialog_callback<tmp_create_game, &tmp_create_game::update_map>);
92 #endif
93 
94  // Load option (might turn it into a button later).
95  string_map item;
96  item.insert(std::make_pair("label", _("Load Game")));
97  item.insert(std::make_pair("tooltip", _("Load Game...")));
98  list.add_row(item);
99 
100  // User maps
101  /* FIXME implement user maps
102  std::vector<std::string> maps;
103  filesystem::get_files_in_dir(filesystem::get_user_data_dir() + "/editor/maps", &maps, nullptr,
104  filesystem::FILE_NAME_ONLY);
105 
106  for(const auto& map : maps) {
107  std::map<std::string, t_string> item;
108  item.insert(std::make_pair("label", map));
109  list->add_row(item);
110  }
111  */
112 
113  // Standard maps
114  int i = 0;
115  for(const auto & map : cfg_.child_range("multiplayer"))
116  {
117  if(map["allow_new_game"].to_bool(true)) {
118  string_map item;
119  item.insert(std::make_pair("label", map["name"].str()));
120  item.insert(std::make_pair("tooltip", map["name"].str()));
121  list.add_row(item);
122 
123  // This hack is needed since the next item is too wide to fit.
124  // and the scrollbar can't truncate text yet.
125  if(++i == 46) {
126  break;
127  }
128  }
129  }
130 
131  update_map_settings(window);
132 }
133 
135 {
136  if(get_retval() == twindow::OK) {
137  find_widget<tlistbox>(&window, "map_list", false);
138  }
139 }
140 
142 {
143  tminimap& minimap = find_widget<tminimap>(&window, "minimap", false);
144 
145  const int index = find_widget<tlistbox>(&window, "map_list", false)
146  .get_selected_row() - 1;
147 
148  if(index >= 0) {
149  config::const_child_itors children = cfg_.child_range("multiplayer");
150  std::advance(children.first, index);
151  scenario_ = &*children.first;
152  minimap.set_map_data((*scenario_)["map_data"]);
153  } else {
154  minimap.set_map_data("");
155  scenario_ = nullptr;
156  }
157 
158  update_map_settings(window);
159 }
160 
162 {
164 
165  fog_->widget_set_enabled(window, !use_map_settings, false);
166  shroud_->widget_set_enabled(window, !use_map_settings, false);
167  start_time_->widget_set_enabled(window, !use_map_settings, false);
168 
169  turns_->widget_set_enabled(window, !use_map_settings, false);
170  gold_->widget_set_enabled(window, !use_map_settings, false);
171  support_->widget_set_enabled(window, !use_map_settings, false);
172  experience_->widget_set_enabled(window, !use_map_settings, false);
173 
174  if(use_map_settings) {
175  if(scenario_) {
176  const config& side = scenario_->child("side");
177 
178  fog_->set_widget_value(window, side["fog"].to_bool(true));
179  shroud_->set_widget_value(window, side["shroud"].to_bool(false));
181  window, (*scenario_)["random_start_time"].to_bool(true));
182 
184  window, ::settings::get_turns((*scenario_)["turns"].str()));
186  window, ::settings::get_village_gold(side["village_gold"].str()));
188  window,
189  ::settings::get_village_support(side["village_support"].str()));
191  window,
193  (*scenario_)["experience_modifier"].str()));
194  }
195  // No scenario selected just leave the state unchanged for now.
196 
197  } else {
198 
199  // Fixme we should store the value and reuse it later...
203 
208  }
209 }
210 
211 } // namespace gui2
void update_map(twindow &window)
child_itors child_range(const std::string &key)
Definition: config.cpp:613
tfield_integer * experience_
const config * scenario_
void set_village_support(int value)
tfield_bool * use_map_settings_
All fields are also in the normal field vector, but they need to be manually controlled as well so ad...
int get_retval() const
Definition: dialog.hpp:161
int village_support
Definition: game_config.cpp:39
void set_shroud(bool value)
The basic minimap class.
Definition: minimap.hpp:36
tfield_bool * start_time_
void update_map_settings(twindow &window)
void connect_signal_notify_modified(tdispatcher &dispatcher, const tsignal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.hpp:725
REGISTER_DIALOG(label_settings)
tfield_integer * turns_
int get_village_gold(const std::string &value, const game_classification *classification)
Gets the village gold.
Definition: settings.cpp:42
T get_widget_value(twindow &window)
Gets the value of the field.
Definition: field.hpp:398
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed...
std::pair< const_child_iterator, const_child_iterator > const_child_itors
Definition: config.hpp:214
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
int get_village_support(const std::string &value)
Gets the village unit level support.
Definition: settings.cpp:47
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
Dialog is closed with ok button.
Definition: window.hpp:125
This file contains the settings handling of the widget library.
void add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:74
void set_random_start_time(bool value)
Modify, read and display user preferences.
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
void set_widget_value(twindow &window, CT value)
Sets the value of the field.
Definition: field.hpp:361
GLuint index
Definition: glew.h:1782
The listbox class.
Definition: listbox.hpp:39
size_t i
Definition: function.cpp:1057
int get_xp_modifier(const std::string &value)
Gets the xp modifier.
Definition: settings.cpp:52
tfield_integer * gold_
void set_turns(int value)
void pre_show(twindow &window)
Inherited from tdialog.
GLenum GLint ref
Definition: glew.h:1813
config & child(const std::string &key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:658
void widget_set_enabled(twindow &window, const bool enable, const bool sync)
Enables a widget.
Definition: field.hpp:172
void set_callback_value_change(const std::function< void(twidget &)> &callback)
Definition: listbox.hpp:225
void set_fog(bool value)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void post_show(twindow &window)
Inherited from tdialog.
bool random_start_time()
void set_xp_modifier(int value)
int get_turns(const std::string &value)
Gets the number of turns.
Definition: settings.cpp:29
void dialog_callback(twidget &caller)
Template for dialog callbacks.
Definition: helper.hpp:31
void set_village_gold(int value)
void set_use_map_settings(bool value)
tfield_integer * support_
void set_map_data(const std::string &map_data)
Definition: minimap.hpp:59