The Battle for Wesnoth  1.13.11+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 - 2018 by Mark de Wever <koraq@xs4all.nl>
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 
20 #include "gui/dialogs/helper.hpp"
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/image.hpp"
23 #ifdef GUI2_EXPERIMENTAL_LISTBOX
24 #include "gui/widgets/list.hpp"
25 #else
26 #include "gui/widgets/listbox.hpp"
27 #endif
31 #include "gui/widgets/settings.hpp"
35 #include "gui/widgets/window.hpp"
36 #include "lexical_cast.hpp"
37 #include "preferences/game.hpp"
38 
39 #include "utils/functional.hpp"
40 #include "utils/irdya_datetime.hpp"
41 
42 namespace gui2
43 {
44 namespace dialogs
45 {
46 /*WIKI
47  * @page = GUIWindowDefinitionWML
48  * @order = 2_campaign_selection
49  *
50  * == Campaign selection ==
51  *
52  * This shows the dialog which allows the user to choose which campaign to
53  * play.
54  *
55  * @begin{table}{dialog_widgets}
56  *
57  * campaign_tree & & tree_view & m &
58  * A tree_view that contains all available campaigns. $
59  *
60  * -icon & & image & o &
61  * The icon for the campaign. $
62  *
63  * -name & & styled_widget & o &
64  * The name of the campaign. $
65  *
66  * -victory & & image & o &
67  * The icon to show when the user finished the campaign. The engine
68  * determines whether or not the user has finished the campaign and
69  * sets the visible flag for the widget accordingly. $
70  *
71  * campaign_details & & multi_page & m &
72  * A multi page widget that shows more details for the selected
73  * campaign. $
74  *
75  * -image & & image & o &
76  * The image for the campaign. $
77  *
78  * -description & & styled_widget & o &
79  * The description of the campaign. $
80  *
81  * @end{table}
82  */
83 
84 REGISTER_DIALOG(campaign_selection)
85 
86 void campaign_selection::campaign_selected(window& window)
87 {
88  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
89  if(tree.empty()) {
90  return;
91  }
92 
93  assert(tree.selected_item());
94 
95  if(!tree.selected_item()->id().empty()) {
96  auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());
97 
98  const int choice = std::distance(page_ids_.begin(), iter);
99  if(iter == page_ids_.end()) {
100  return;
101  }
102 
103  multi_page& pages = find_widget<multi_page>(&window, "campaign_details", false);
104  pages.select_page(choice);
105 
106  engine_.set_current_level(choice);
107  }
108 }
109 
111 {
112  using level_ptr = ng::create_engine::level_ptr;
113 
114  auto levels = engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN);
115 
116  switch(order) {
117  case RANK: // Already sorted by rank
118  // This'll actually never happen, but who knows if that'll ever change...
119  if(!ascending) {
120  std::reverse(levels.begin(), levels.end());
121  }
122 
123  break;
124 
125  case DATE:
126  std::sort(levels.begin(), levels.end(), [ascending](const level_ptr& a, const level_ptr& b) {
127  auto cpn_a = std::dynamic_pointer_cast<ng::campaign>(a);
128  auto cpn_b = std::dynamic_pointer_cast<ng::campaign>(b);
129 
130  if(cpn_b == nullptr) {
131  return cpn_a != nullptr;
132  }
133 
134  if(cpn_a == nullptr) {
135  return false;
136  }
137 
138  return ascending
139  ? cpn_a->dates().first < cpn_b->dates().first
140  : cpn_a->dates().first > cpn_b->dates().first;
141  });
142 
143  break;
144 
145  case NAME:
146  std::sort(levels.begin(), levels.end(), [ascending](const level_ptr& a, const level_ptr& b) {
147  const int cmp = translation::icompare(a->name(), b->name());
148  return ascending ? cmp < 0 : cmp > 0;
149  });
150 
151  break;
152  }
153 
154  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
155 
156  // Remember which campaign was selected...
157  std::string was_selected = tree.selected_item()->id();
158 
159  tree.clear();
160 
161  for(const auto& level : levels) {
162  add_campaign_to_tree(window, level->data());
163  }
164 
165  if(!was_selected.empty()) {
166  find_widget<tree_view_node>(&window, was_selected, false).select_node();
167  }
168 }
169 
171 {
172  static bool force = false;
173  if(force) {
174  return;
175  }
176 
177  if(current_sorting_ == order) {
179  currently_sorted_asc_ = false;
180  } else {
181  currently_sorted_asc_ = true;
183  }
184  } else if(current_sorting_ == RANK) {
185  currently_sorted_asc_ = true;
186  current_sorting_ = order;
187  } else {
188  currently_sorted_asc_ = true;
189  current_sorting_ = order;
190 
191  force = true;
192 
193  if(order == NAME) {
194  find_widget<toggle_button>(&window, "sort_time", false).set_value(0);
195  } else if(order == DATE) {
196  find_widget<toggle_button>(&window, "sort_name", false).set_value(0);
197  }
198 
199  force = false;
200  }
201 
203 }
204 
206 {
207  /***** Setup campaign tree. *****/
208  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
209 
210  tree.set_selection_change_callback(std::bind(&campaign_selection::campaign_selected, this, std::ref(window)));
211 
212  toggle_button& sort_name = find_widget<toggle_button>(&window, "sort_name", false);
213  toggle_button& sort_time = find_widget<toggle_button>(&window, "sort_time", false);
214 
216  std::bind(&campaign_selection::toggle_sorting_selection, this, std::ref(window), NAME));
217 
219  std::bind(&campaign_selection::toggle_sorting_selection, this, std::ref(window), DATE));
220 
221  window.keyboard_capture(&tree);
222 
223  /***** Setup campaign details. *****/
224  multi_page& pages = find_widget<multi_page>(&window, "campaign_details", false);
225 
226  for(const auto& level : engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN)) {
227  const config& campaign = level->data();
228 
229  /*** Add tree item ***/
230  add_campaign_to_tree(window, campaign);
231 
232  /*** Add detail item ***/
233  std::map<std::string, string_map> data;
234  string_map item;
235 
236  item["label"] = campaign["description"];
237  item["use_markup"] = "true";
238 
239  if(!campaign["description_alignment"].empty()) {
240  item["text_alignment"] = campaign["description_alignment"];
241  }
242 
243  data.emplace("description", item);
244 
245  item["label"] = campaign["image"];
246  data.emplace("image", item);
247 
248  pages.add_page(data);
249  page_ids_.push_back(campaign["id"]);
250  }
251 
252  //
253  // Set up Mods selection dropdown
254  //
255  multimenu_button& mods_menu = find_widget<multimenu_button>(&window, "mods_menu", false);
256 
258  std::vector<config> mod_menu_values;
259  std::vector<std::string> enabled = engine_.active_mods();
260 
262  const bool active = std::find(enabled.begin(), enabled.end(), mod->id) != enabled.end();
263 
264  mod_menu_values.emplace_back(config {"label", mod->name, "checkbox", active});
265 
266  mod_states_.push_back(active);
267  }
268 
269  mods_menu.set_values(mod_menu_values);
270  mods_menu.select_options(mod_states_);
271 
272  connect_signal_notify_modified(mods_menu, std::bind(&campaign_selection::mod_toggled, this, std::ref(window)));
273  } else {
274  mods_menu.set_active(false);
275  mods_menu.set_label(_("None"));
276  }
277 
278  campaign_selected(window);
279 }
280 
282 {
283  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
284  std::map<std::string, string_map> data;
285  string_map item;
286 
287  item["label"] = campaign["icon"];
288  data.emplace("icon", item);
289 
290  item["label"] = campaign["name"];
291  data.emplace("name", item);
292 
293  // We completed the campaign! Calculate the appropriate victory laurel.
294  if(campaign["completed"].to_bool()) {
295  config::const_child_itors difficulties = campaign.child_range("difficulty");
296 
297  auto did_complete_at = [](const config& c) { return c["completed_at"].to_bool(); };
298 
299  // Check for non-completion on every difficulty save the first.
300  const bool only_first_completed = difficulties.size() > 1 &&
301  std::none_of(difficulties.begin() + 1, difficulties.end(), did_complete_at);
302 
303  /*
304  * Criteria:
305  *
306  * - Use the gold laurel (hardest) for campaigns with only one difficulty OR
307  * if out of two or more difficulties, the last one has been completed.
308  *
309  * - Use the bronze laurel (easiest) only if the first difficulty out of two
310  * or more has been completed.
311  *
312  * - Use the silver laurel otherwise.
313  */
314  if(!difficulties.empty() && did_complete_at(difficulties.back())) {
316  } else if(only_first_completed && did_complete_at(difficulties.front())) {
318  } else {
319  item["label"] = game_config::images::victory_laurel;
320  }
321 
322  data.emplace("victory", item);
323  }
324 
325  tree.add_node("campaign", data).set_id(campaign["id"]);
326 }
327 
329 {
330  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
331 
332  if(tree.empty()) {
333  return;
334  }
335 
336  assert(tree.selected_item());
337  if(!tree.selected_item()->id().empty()) {
338  auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());
339  if(iter != page_ids_.end()) {
340  choice_ = std::distance(page_ids_.begin(), iter);
341  }
342  }
343 
344  deterministic_ = find_widget<toggle_button>(&window, "checkbox_deterministic", false).get_value_bool();
345 
347 }
348 
350 {
351  boost::dynamic_bitset<> new_mod_states =
352  find_widget<multimenu_button>(&window, "mods_menu", false).get_toggle_states();
353 
354  // Get a mask of any mods that were toggled, regardless of new state
355  mod_states_ = mod_states_ ^ new_mod_states;
356 
357  for(unsigned i = 0; i < mod_states_.size(); i++) {
358  if(mod_states_[i]) {
359  engine_.toggle_mod(i);
360  }
361  }
362 
363  // Save the full toggle states for next time
364  mod_states_ = new_mod_states;
365 }
366 
367 } // namespace dialogs
368 } // namespace gui2
void campaign_selected(window &window)
Called when another campaign is selected.
std::string victory_laurel
std::vector< char_t > string
std::shared_ptr< level > level_ptr
void set_selection_change_callback(std::function< void(widget &)> callback)
Definition: tree_view.hpp:96
const std::vector< extras_metadata_ptr > & get_const_extras_by_type(const MP_EXTRA extra_type) const
grid & add_page(const string_map &item)
Adds single page to the grid.
Definition: multi_page.cpp:43
New lexcical_cast header.
#define a
const std::string & id() const
Definition: widget.cpp:107
This file contains the window object, this object is a top level container which has the event manage...
child_itors child_range(config_key_type key)
Definition: config.cpp:362
std::vector< level_ptr > get_levels_by_type_unfiltered(level::TYPE type) const
tree_view_node * selected_item()
Definition: tree_view.hpp:86
std::string victory_laurel_easy
Simple push button.
std::vector< std::pair< const std::string *, const stats * > > levels
Stats (and name) for each scenario. The pointers are never nullptr.
Definition: statistics.hpp:114
Generic file dialog.
Definition: field-fwd.hpp:22
void toggle_sorting_selection(window &window, CAMPAIGN_ORDER order)
#define b
virtual void set_label(const t_string &label)
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
bool toggle_mod(int index, bool force=false)
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:233
std::string victory_laurel_hardest
This file contains the settings handling of the widget library.
virtual void post_show(window &window) override
Inherited from modal_dialog.
void sort_campaigns(window &window, CAMPAIGN_ORDER order, bool ascending)
std::vector< std::string > & active_mods()
Various uncategorised dialogs.
std::vector< std::string > page_ids_
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:239
virtual void set_active(const bool active) override
See styled_widget::set_active.
virtual void pre_show(window &window) override
Inherited from modal_dialog.
void select_page(const unsigned page, const bool select=true)
Selectes a page.
Definition: multi_page.cpp:106
size_t i
Definition: function.cpp:933
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
static int sort(lua_State *L)
Definition: ltablib.cpp:411
bool empty() const
Definition: tree_view.cpp:117
The multi page class.
Definition: multi_page.hpp:35
bool find(E event, F functor)
Tests whether an event handler is available.
int icompare(const std::string &s1, const std::string &s2)
Case-insensitive lexicographical comparison.
void set_modifications(const std::vector< std::string > &value, bool mp)
Definition: game.cpp:738
void set_id(const std::string &id)
Definition: widget.cpp:95
void select_options(boost::dynamic_bitset<> states)
Set the options selected in the menu.
std::pair< irdya_date, irdya_date > dates() const
void set_values(const std::vector<::config > &values)
Set the available menu options.
static void reverse(lua_State *L, StkId from, StkId to)
Definition: lapi.cpp:193
bool deterministic_
whether the player checked the "Deterministic" checkbox.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
mock_char c
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.
tree_view_node & add_node(const std::string &id, const std::map< std::string, string_map > &data, const int index=-1)
Definition: tree_view.cpp:62
void add_campaign_to_tree(window &window, const config &campaign)