The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
campaign_selection.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 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 "game_preferences.hpp"
21 #include "gui/dialogs/helper.hpp"
23 #include "gui/widgets/button.hpp"
24 #include "gui/widgets/image.hpp"
25 #ifdef GUI2_EXPERIMENTAL_LISTBOX
26 #include "gui/widgets/list.hpp"
27 #else
28 #include "gui/widgets/listbox.hpp"
29 #endif
32 #include "gui/widgets/settings.hpp"
36 #include "gui/widgets/window.hpp"
38 
39 #include "utils/functional.hpp"
40 #include "video.hpp"
41 
42 namespace gui2
43 {
44 
45 /*WIKI
46  * @page = GUIWindowDefinitionWML
47  * @order = 2_campaign_selection
48  *
49  * == Campaign selection ==
50  *
51  * This shows the dialog which allows the user to choose which campaign to
52  * play.
53  *
54  * @begin{table}{dialog_widgets}
55  *
56  * campaign_list & & listbox & m &
57  * A listbox that contains all available campaigns. $
58  *
59  * -icon & & image & o &
60  * The icon for the campaign. $
61  *
62  * -name & & control & o &
63  * The name of the campaign. $
64  *
65  * -victory & & image & o &
66  * The icon to show when the user finished the campaign. The engine
67  * determines whether or not the user has finished the campaign and
68  * sets the visible flag for the widget accordingly. $
69  *
70  * campaign_details & & multi_page & m &
71  * A multi page widget that shows more details for the selected
72  * campaign. $
73  *
74  * -image & & image & o &
75  * The image for the campaign. $
76  *
77  * -description & & control & o &
78  * The description of the campaign. $
79  *
80  * @end{table}
81  */
82 
83 REGISTER_DIALOG(campaign_selection)
84 
85 void tcampaign_selection::campaign_selected(twindow& window)
86 {
87  if(new_widgets || true) {
88  ttree_view& tree
89  = find_widget<ttree_view>(&window, "campaign_tree", false);
90 
91  if(tree.empty()) {
92  return;
93  }
94 
95  assert(tree.selected_item());
96  if(tree.selected_item()->id() != "") {
97  const unsigned choice
98  = lexical_cast<unsigned>(tree.selected_item()->id());
99 
100  tmulti_page& multi_page
101  = find_widget<tmulti_page>(&window, "campaign_details", false);
102  multi_page.select_page(choice);
103  engine_.set_current_level(choice);
104  }
105  } else {
106  const int selected_row
107  = find_widget<tlistbox>(&window, "campaign_list", false)
108  .get_selected_row();
109 
110  tmulti_page& multi_page
111  = find_widget<tmulti_page>(&window, "campaign_details", false);
112 
113  multi_page.select_page(selected_row);
114  engine_.set_current_level(selected_row);
115  }
116 }
117 
119  tcampaign_settings settings_dlg(engine_);
120  settings_dlg.show(video);
121 }
122 
124 {
125  if(new_widgets || true) {
126  /***** Setup campaign tree. *****/
127  ttree_view& tree
128  = find_widget<ttree_view>(&window, "campaign_tree", false);
129 
132  this,
133  std::ref(window)));
134 
135  window.keyboard_capture(&tree);
136 
137  string_map tree_group_field;
138  std::map<std::string, string_map> tree_group_item;
139  /***** Setup campaign details. *****/
140  tmulti_page& multi_page
141  = find_widget<tmulti_page>(&window, "campaign_details", false);
142 
143  unsigned id = 0;
144  for(const auto & level : engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN))
145  {
146  const config& campaign = level->data();
147 
148  /*** Add tree item ***/
149  tree_group_field["label"] = campaign["icon"];
150  tree_group_item["icon"] = tree_group_field;
151 
152  tree_group_field["label"] = campaign["name"];
153  tree_group_item["name"] = tree_group_field;
154 
155  tree_group_field["label"] = campaign["completed"].to_bool() ? "misc/laurel.png" : "misc/blank-hex.png";
156  tree_group_item["victory"] = tree_group_field;
157 
158  tree.add_node("campaign", tree_group_item).set_id(std::to_string(id++));
159 
160  /*** Add detail item ***/
161  string_map detail_item;
162  std::map<std::string, string_map> detail_page;
163 
164  detail_item["label"] = campaign["description"];
165  detail_item["use_markup"] = "true";
166 
167  if(!campaign["description_alignment"].empty()) {
168  detail_item["text_alignment"] = campaign["description_alignment"];
169  }
170 
171  detail_page.insert(std::make_pair("description", detail_item));
172 
173  detail_item["label"] = campaign["image"];
174  detail_page.insert(std::make_pair("image", detail_item));
175 
176  multi_page.add_page(detail_page);
177  }
179 
180  tree_group_field["label"] = "Modifications";
181  tree_group_item["tree_view_node_label"] = tree_group_field;
182  //tree_group_item["tree_view_node_label"] = tree_group_field;
183  ttree_view_node& mods_node = tree.add_node("campaign_group", tree_group_item);
184  std::vector<std::string> enabled = engine_.active_mods();
185 
186  id = 0;
187  tree_group_item.clear();
189  {
190  bool active = std::find(enabled.begin(), enabled.end(), mod->id) != enabled.end();
191  /*** Add tree item ***/
192  tree_group_field["label"] = mod->name;
193  tree_group_item["checkb"] = tree_group_field;
194 
195  ttree_view_node & node = mods_node.add_child("modification", tree_group_item);
196  ttoggle_button* checkbox = dynamic_cast<ttoggle_button*>(node.find("checkb", true));
197  VALIDATE(checkbox, missing_widget("checkb"));
198  checkbox->set_value(active);
199  checkbox->set_label(mod->name);
200  checkbox->set_callback_state_change(std::bind(&tcampaign_selection::mod_toggled, this, id, _1));
201  ++id;
202  }
203  }
204  } else {
205  /***** Hide the tree view. *****/
206  if(ttree_view* tree
207  = find_widget<ttree_view>(&window, "campaign_tree", false, false)) {
208 
209  tree->set_visible(twidget::tvisible::invisible);
210  }
211 
212  /***** Setup campaign list. *****/
213  tlistbox& list = find_widget<tlistbox>(&window, "campaign_list", false);
214 #ifdef GUI2_EXPERIMENTAL_LISTBOX
216  list,
218  this,
219  std::ref(window)));
220 #else
224 #endif
225  window.keyboard_capture(&list);
226 
227  /***** Setup campaign details. *****/
228  tmulti_page& multi_page
229  = find_widget<tmulti_page>(&window, "campaign_details", false);
230 
231  for(const auto & level : engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN))
232  {
233  const config& campaign = level->data();
234 
235  /*** Add list item ***/
236  string_map list_item;
237  std::map<std::string, string_map> list_item_item;
238 
239  list_item["label"] = campaign["icon"];
240  list_item_item.insert(std::make_pair("icon", list_item));
241 
242  list_item["label"] = campaign["name"];
243  list_item_item.insert(std::make_pair("name", list_item));
244 
245  list.add_row(list_item_item);
246 
247  tgrid* grid = list.get_row_grid(list.get_item_count() - 1);
248  assert(grid);
249 
250  twidget* widget = grid->find("victory", false);
251  if(widget && !campaign["completed"].to_bool()) {
253  }
254 
255  /*** Add detail item ***/
256  string_map detail_item;
257  std::map<std::string, string_map> detail_page;
258 
259  detail_item["label"] = campaign["description"];
260  detail_item["use_markup"] = "true";
261  detail_page.insert(std::make_pair("description", detail_item));
262 
263  detail_item["label"] = campaign["image"];
264  detail_page.insert(std::make_pair("image", detail_item));
265 
266  multi_page.add_page(detail_page);
267  }
268  }
269  campaign_selected(window);
270 
271  /***** Setup advanced settings button *****/
272  tbutton* advanced_settings_button =
273  find_widget<tbutton>(&window, "advanced_settings", false, false);
274  if(advanced_settings_button) {
275  advanced_settings_button->connect_click_handler(
276  std::bind(&tcampaign_selection::show_settings, this, std::ref(window.video())));
277  }
278 }
279 
281 {
282  if(new_widgets || true) {
283  ttree_view& tree
284  = find_widget<ttree_view>(&window, "campaign_tree", false);
285 
286  if(tree.empty()) {
287  return;
288  }
289 
290  assert(tree.selected_item());
291  if(tree.selected_item()->id() != "") {
292  choice_ = lexical_cast<unsigned>(tree.selected_item()->id());
293  }
294  deterministic_ = find_widget<ttoggle_button>(&window,
295  "checkbox_deterministic",
296  false).get_value_bool();
297 
299  } else {
300  choice_ = find_widget<tlistbox>(&window, "campaign_list", false)
301  .get_selected_row();
302  deterministic_ = find_widget<ttoggle_button>(&window,
303  "checkbox_deterministic",
304  false).get_value_bool();
305  }
306 }
307 
309 {
312 }
313 
314 } // namespace gui2
void set_current_mod_index(const size_t index)
bool new_widgets
Do we wish to use the new library or not.
Definition: settings.cpp:40
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1806
GLenum GLsizei const GLuint GLboolean enabled
Definition: glew.h:2497
const std::vector< extras_metadata_ptr > & get_const_extras_by_type(const MP_EXTRA extra_type) const
GLint level
Definition: glew.h:1220
bool show(CVideo &video, const unsigned auto_close_time=0)
Shows the window.
Definition: dialog.cpp:34
void connect_click_handler(const event::tsignal_function &signal)
Inherited from tclickable.
Definition: button.hpp:49
Definition: video.hpp:58
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
Base container class.
Definition: grid.hpp:29
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(label_settings)
std::vector< level_ptr > get_levels_by_type_unfiltered(level::TYPE type) const
ttree_view_node * selected_item()
Definition: tree_view.hpp:82
virtual void set_label(const t_string &label)
Definition: control.cpp:330
To lexical_cast(From value)
Lexical cast converts one type to another.
ttree_view_node & add_child(const std::string &id, const std::map< std::string, string_map > &data, const int index=-1)
Adds a child item to the list of child nodes.
int choice_
The chosen campaign.
-file util.hpp
bool toggle_current_mod(bool force=false)
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
Class for a toggle button.
ttree_view_node & add_node(const std::string &id, const std::map< std::string, string_map > &data)
Definition: tree_view.cpp:58
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
bool deterministic_
whether the player checked the "Deterministic" checkbox.
Simple push button.
Definition: button.hpp:32
void set_selection_change_callback(std::function< void(twidget &)> callback)
Definition: tree_view.hpp:92
void pre_show(twindow &window)
Inherited from tdialog.
The user set the widget invisible, that means:
Definition: widget.hpp:103
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:138
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_callback_state_change(std::function< void(twidget &)> callback)
Inherited from tselectable_.
The user sets the widget hidden, that means:
Definition: widget.hpp:91
void add_page(const string_map &item)
Adds single page to the grid.
Definition: multi_page.cpp:42
The multi page class.
Definition: multi_page.hpp:37
void set_id(const std::string &id)
Definition: widget.cpp:97
static size_t id
Ids for the timers.
Definition: timer.cpp:39
void post_show(twindow &window)
Inherited from tdialog.
std::vector< std::string > & active_mods()
void mod_toggled(int id, twidget &)
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
void show_settings(CVideo &video)
The listbox class.
Definition: listbox.hpp:39
const std::string & id() const
Definition: widget.cpp:109
void select_page(const unsigned page, const bool select=true)
Selectes a page.
Definition: multi_page.cpp:84
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
GLenum GLint ref
Definition: glew.h:1813
bool find(E event, F functor)
Tests whether an event handler is available.
void set_modifications(const std::vector< std::string > &value, bool mp)
Base class for all widgets.
Definition: widget.hpp:49
void set_callback_value_change(const std::function< void(twidget &)> &callback)
Definition: listbox.hpp:225
bool empty() const
Definition: tree_view.cpp:107
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: grid.cpp:612
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void set_value(const unsigned selected)
Inherited from tselectable_.
void set_visible(const tvisible::scoped_enum visible)
Definition: widget.cpp:445
void campaign_selected(twindow &window)
Called when another campaign is selected.
void dialog_callback(twidget &caller)
Template for dialog callbacks.
Definition: helper.hpp:31
t_string missing_widget(const std::string &id)
Returns a default error message if a mandatory widget is omitted.
Definition: helper.cpp:123
const tgrid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:215