The Battle for Wesnoth  1.15.0-dev
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/widgets/button.hpp"
21 #include "gui/widgets/image.hpp"
22 #ifdef GUI2_EXPERIMENTAL_LISTBOX
23 #include "gui/widgets/list.hpp"
24 #else
25 #include "gui/widgets/listbox.hpp"
26 #endif
30 #include "gui/widgets/settings.hpp"
34 #include "gui/widgets/window.hpp"
35 #include "lexical_cast.hpp"
36 #include "preferences/game.hpp"
37 
38 #include "utils/functional.hpp"
39 #include "utils/irdya_datetime.hpp"
40 
41 namespace gui2
42 {
43 namespace dialogs
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_tree & & tree_view & m &
57  * A tree_view that contains all available campaigns. $
58  *
59  * -icon & & image & o &
60  * The icon for the campaign. $
61  *
62  * -name & & styled_widget & 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 & & styled_widget & o &
78  * The description of the campaign. $
79  *
80  * @end{table}
81  */
82 
83 REGISTER_DIALOG(campaign_selection)
84 
85 void campaign_selection::campaign_selected(window& window)
86 {
87  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
88  if(tree.empty()) {
89  return;
90  }
91 
92  assert(tree.selected_item());
93 
94  if(!tree.selected_item()->id().empty()) {
95  auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());
96 
97  const int choice = std::distance(page_ids_.begin(), iter);
98  if(iter == page_ids_.end()) {
99  return;
100  }
101 
102  multi_page& pages = find_widget<multi_page>(&window, "campaign_details", false);
103  pages.select_page(choice);
104 
105  engine_.set_current_level(choice);
106  }
107 }
108 
110 {
111  using level_ptr = ng::create_engine::level_ptr;
112 
113  auto levels = engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN);
114 
115  switch(order) {
116  case RANK: // Already sorted by rank
117  // This'll actually never happen, but who knows if that'll ever change...
118  if(!ascending) {
119  std::reverse(levels.begin(), levels.end());
120  }
121 
122  break;
123 
124  case DATE:
125  std::sort(levels.begin(), levels.end(), [ascending](const level_ptr& a, const level_ptr& b) {
126  auto cpn_a = std::dynamic_pointer_cast<ng::campaign>(a);
127  auto cpn_b = std::dynamic_pointer_cast<ng::campaign>(b);
128 
129  if(cpn_b == nullptr) {
130  return cpn_a != nullptr;
131  }
132 
133  if(cpn_a == nullptr) {
134  return false;
135  }
136 
137  return ascending
138  ? cpn_a->dates().first < cpn_b->dates().first
139  : cpn_a->dates().first > cpn_b->dates().first;
140  });
141 
142  break;
143 
144  case NAME:
145  std::sort(levels.begin(), levels.end(), [ascending](const level_ptr& a, const level_ptr& b) {
146  const int cmp = translation::icompare(a->name(), b->name());
147  return ascending ? cmp < 0 : cmp > 0;
148  });
149 
150  break;
151  }
152 
153  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
154 
155  // Remember which campaign was selected...
156  std::string was_selected = tree.selected_item()->id();
157 
158  tree.clear();
159 
160  for(const auto& level : levels) {
161  add_campaign_to_tree(window, level->data());
162  }
163 
164  if(!was_selected.empty()) {
165  find_widget<tree_view_node>(&window, was_selected, false).select_node();
166  }
167 }
168 
170 {
171  static bool force = false;
172  if(force) {
173  return;
174  }
175 
176  if(current_sorting_ == order) {
178  currently_sorted_asc_ = false;
179  } else {
180  currently_sorted_asc_ = true;
182  }
183  } else if(current_sorting_ == RANK) {
184  currently_sorted_asc_ = true;
185  current_sorting_ = order;
186  } else {
187  currently_sorted_asc_ = true;
188  current_sorting_ = order;
189 
190  force = true;
191 
192  if(order == NAME) {
193  find_widget<toggle_button>(&window, "sort_time", false).set_value(0);
194  } else if(order == DATE) {
195  find_widget<toggle_button>(&window, "sort_name", false).set_value(0);
196  }
197 
198  force = false;
199  }
200 
202 }
203 
205 {
206  /***** Setup campaign tree. *****/
207  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
208 
209  tree.set_selection_change_callback(std::bind(&campaign_selection::campaign_selected, this, std::ref(window)));
210 
211  toggle_button& sort_name = find_widget<toggle_button>(&window, "sort_name", false);
212  toggle_button& sort_time = find_widget<toggle_button>(&window, "sort_time", false);
213 
215  std::bind(&campaign_selection::toggle_sorting_selection, this, std::ref(window), NAME));
216 
218  std::bind(&campaign_selection::toggle_sorting_selection, this, std::ref(window), DATE));
219 
220  window.keyboard_capture(&tree);
221 
222  /***** Setup campaign details. *****/
223  multi_page& pages = find_widget<multi_page>(&window, "campaign_details", false);
224 
225  for(const auto& level : engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN)) {
226  const config& campaign = level->data();
227 
228  /*** Add tree item ***/
229  add_campaign_to_tree(window, campaign);
230 
231  /*** Add detail item ***/
232  std::map<std::string, string_map> data;
233  string_map item;
234 
235  item["label"] = campaign["description"];
236  item["use_markup"] = "true";
237 
238  if(!campaign["description_alignment"].empty()) {
239  item["text_alignment"] = campaign["description_alignment"];
240  }
241 
242  data.emplace("description", item);
243 
244  item["label"] = campaign["image"];
245  data.emplace("image", item);
246 
247  pages.add_page(data);
248  page_ids_.push_back(campaign["id"]);
249  }
250 
251  //
252  // Set up Mods selection dropdown
253  //
254  multimenu_button& mods_menu = find_widget<multimenu_button>(&window, "mods_menu", false);
255 
257  std::vector<config> mod_menu_values;
258  std::vector<std::string> enabled = engine_.active_mods();
259 
261  const bool active = std::find(enabled.begin(), enabled.end(), mod->id) != enabled.end();
262 
263  mod_menu_values.emplace_back("label", mod->name, "checkbox", active);
264 
265  mod_states_.push_back(active);
266  }
267 
268  mods_menu.set_values(mod_menu_values);
269  mods_menu.select_options(mod_states_);
270 
271  connect_signal_notify_modified(mods_menu, std::bind(&campaign_selection::mod_toggled, this, std::ref(window)));
272  } else {
273  mods_menu.set_active(false);
274  mods_menu.set_label(_("active_modifications^None"));
275  }
276 
277  campaign_selected(window);
278 }
279 
281 {
282  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
283  std::map<std::string, string_map> data;
284  string_map item;
285 
286  item["label"] = campaign["icon"];
287  data.emplace("icon", item);
288 
289  item["label"] = campaign["name"];
290  data.emplace("name", item);
291 
292  // We completed the campaign! Calculate the appropriate victory laurel.
293  if(campaign["completed"].to_bool()) {
294  config::const_child_itors difficulties = campaign.child_range("difficulty");
295 
296  auto did_complete_at = [](const config& c) { return c["completed_at"].to_bool(); };
297 
298  // Check for non-completion on every difficulty save the first.
299  const bool only_first_completed = difficulties.size() > 1 &&
300  std::none_of(difficulties.begin() + 1, difficulties.end(), did_complete_at);
301 
302  /*
303  * Criteria:
304  *
305  * - Use the gold laurel (hardest) for campaigns with only one difficulty OR
306  * if out of two or more difficulties, the last one has been completed.
307  *
308  * - Use the bronze laurel (easiest) only if the first difficulty out of two
309  * or more has been completed.
310  *
311  * - Use the silver laurel otherwise.
312  */
313  if(!difficulties.empty() && did_complete_at(difficulties.back())) {
315  } else if(only_first_completed && did_complete_at(difficulties.front())) {
317  } else {
318  item["label"] = game_config::images::victory_laurel;
319  }
320 
321  data.emplace("victory", item);
322  }
323 
324  tree.add_node("campaign", data).set_id(campaign["id"]);
325 }
326 
328 {
329  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
330 
331  if(tree.empty()) {
332  return;
333  }
334 
335  assert(tree.selected_item());
336  if(!tree.selected_item()->id().empty()) {
337  auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());
338  if(iter != page_ids_.end()) {
339  choice_ = std::distance(page_ids_.begin(), iter);
340  }
341  }
342 
343  deterministic_ = find_widget<toggle_button>(&window, "checkbox_deterministic", false).get_value_bool();
344 
346 }
347 
349 {
350  boost::dynamic_bitset<> new_mod_states =
351  find_widget<multimenu_button>(&window, "mods_menu", false).get_toggle_states();
352 
353  // Get a mask of any mods that were toggled, regardless of new state
354  mod_states_ = mod_states_ ^ new_mod_states;
355 
356  for(unsigned i = 0; i < mod_states_.size(); i++) {
357  if(mod_states_[i]) {
358  engine_.toggle_mod(i);
359  }
360  }
361 
362  // Save the full toggle states for next time
363  mod_states_ = new_mod_states;
364 }
365 
366 } // namespace dialogs
367 } // namespace gui2
void campaign_selected(window &window)
Called when another campaign is selected.
std::string victory_laurel
std::shared_ptr< level > level_ptr
void set_selection_change_callback(std::function< void(widget &)> callback)
Definition: tree_view.hpp:91
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:106
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:366
tree_view_node * selected_item()
Definition: tree_view.hpp:81
std::string victory_laurel_easy
Simple push button.
std::pair< irdya_date, irdya_date > dates() const
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:179
std::string victory_laurel_hardest
This file contains the settings handling of the widget library.
std::vector< level_ptr > get_levels_by_type_unfiltered(level::TYPE type) const
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:210
virtual void set_active(const bool active) override
See styled_widget::set_active.
std::size_t i
Definition: function.cpp:933
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
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
static int sort(lua_State *L)
Definition: ltablib.cpp:411
The multi page class.
Definition: multi_page.hpp:35
int icompare(const std::string &s1, const std::string &s2)
Case-insensitive lexicographical comparison.
Definition: gettext.cpp:476
bool empty() const
Definition: tree_view.cpp:106
void set_modifications(const std::vector< std::string > &value, bool mp)
Definition: game.cpp:737
void set_id(const std::string &id)
Definition: widget.cpp:94
void select_options(boost::dynamic_bitset<> states)
Set the options selected in the menu.
const std::vector< extras_metadata_ptr > & get_const_extras_by_type(const MP_EXTRA extra_type) 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:68
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:63
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)