The Battle for Wesnoth  1.15.1+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 https://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 #include "gui/widgets/listbox.hpp"
26 #include "gui/widgets/settings.hpp"
27 #include "gui/widgets/text_box.hpp"
31 #include "gui/widgets/window.hpp"
32 #include "lexical_cast.hpp"
33 #include "preferences/game.hpp"
34 
35 #include "utils/functional.hpp"
36 #include "utils/irdya_datetime.hpp"
37 
38 namespace gui2
39 {
40 namespace dialogs
41 {
42 /*WIKI
43  * @page = GUIWindowDefinitionWML
44  * @order = 2_campaign_selection
45  *
46  * == Campaign selection ==
47  *
48  * This shows the dialog which allows the user to choose which campaign to
49  * play.
50  *
51  * @begin{table}{dialog_widgets}
52  *
53  * campaign_tree & & tree_view & m &
54  * A tree_view that contains all available campaigns. $
55  *
56  * -icon & & image & o &
57  * The icon for the campaign. $
58  *
59  * -name & & styled_widget & o &
60  * The name of the campaign. $
61  *
62  * -victory & & image & o &
63  * The icon to show when the user finished the campaign. The engine
64  * determines whether or not the user has finished the campaign and
65  * sets the visible flag for the widget accordingly. $
66  *
67  * campaign_details & & multi_page & m &
68  * A multi page widget that shows more details for the selected
69  * campaign. $
70  *
71  * -image & & image & o &
72  * The image for the campaign. $
73  *
74  * -description & & styled_widget & o &
75  * The description of the campaign. $
76  *
77  * @end{table}
78  */
79 
80 REGISTER_DIALOG(campaign_selection)
81 
82 void campaign_selection::campaign_selected(window& window)
83 {
84  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
85  if(tree.empty()) {
86  return;
87  }
88 
89  assert(tree.selected_item());
90 
91  if(!tree.selected_item()->id().empty()) {
92  auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());
93 
94  const int choice = std::distance(page_ids_.begin(), iter);
95  if(iter == page_ids_.end()) {
96  return;
97  }
98 
99  multi_page& pages = find_widget<multi_page>(&window, "campaign_details", false);
100  pages.select_page(choice);
101 
102  engine_.set_current_level(choice);
103  }
104 }
105 
107 {
108  using level_ptr = ng::create_engine::level_ptr;
109 
110  auto levels = engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN);
111 
112  switch(order) {
113  case RANK: // Already sorted by rank
114  // This'll actually never happen, but who knows if that'll ever change...
115  if(!ascending) {
116  std::reverse(levels.begin(), levels.end());
117  }
118 
119  break;
120 
121  case DATE:
122  std::sort(levels.begin(), levels.end(), [ascending](const level_ptr& a, const level_ptr& b) {
123  auto cpn_a = std::dynamic_pointer_cast<ng::campaign>(a);
124  auto cpn_b = std::dynamic_pointer_cast<ng::campaign>(b);
125 
126  if(cpn_b == nullptr) {
127  return cpn_a != nullptr;
128  }
129 
130  if(cpn_a == nullptr) {
131  return false;
132  }
133 
134  return ascending
135  ? cpn_a->dates().first < cpn_b->dates().first
136  : cpn_a->dates().first > cpn_b->dates().first;
137  });
138 
139  break;
140 
141  case NAME:
142  std::sort(levels.begin(), levels.end(), [ascending](const level_ptr& a, const level_ptr& b) {
143  const int cmp = translation::icompare(a->name(), b->name());
144  return ascending ? cmp < 0 : cmp > 0;
145  });
146 
147  break;
148  }
149 
150  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
151 
152  // Remember which campaign was selected...
153  std::string was_selected;
154  if(!tree.empty()) {
155  was_selected = tree.selected_item()->id();
156  tree.clear();
157  }
158 
159  boost::dynamic_bitset<> show_items;
160  show_items.resize(levels.size(), true);
161 
162  if(!last_search_words_.empty()) {
163  for(unsigned i = 0; i < levels.size(); ++i) {
164  bool found = false;
165  for(const auto& word : last_search_words_) {
166  found = translation::ci_search(levels[i]->name(), word);
167 
168  if(!found) {
169  break;
170  }
171  }
172 
173  show_items[i] = found;
174  }
175  }
176 
177  bool exists_in_filtered_result = false;
178  for(unsigned i = 0; i < levels.size(); ++i) {
179  if(show_items[i]) {
180  add_campaign_to_tree(window, levels[i]->data());
181 
182  if (!exists_in_filtered_result) {
183  exists_in_filtered_result = levels[i]->id() == was_selected;
184  }
185  }
186  }
187 
188  if(!was_selected.empty() && exists_in_filtered_result) {
189  find_widget<tree_view_node>(&window, was_selected, false).select_node();
190  } else {
191  campaign_selected(window);
192  }
193 }
194 
196 {
197  static bool force = false;
198  if(force) {
199  return;
200  }
201 
202  if(current_sorting_ == order) {
204  currently_sorted_asc_ = false;
205  } else {
206  currently_sorted_asc_ = true;
208  }
209  } else if(current_sorting_ == RANK) {
210  currently_sorted_asc_ = true;
211  current_sorting_ = order;
212  } else {
213  currently_sorted_asc_ = true;
214  current_sorting_ = order;
215 
216  force = true;
217 
218  if(order == NAME) {
219  find_widget<toggle_button>(&window, "sort_time", false).set_value(0);
220  } else if(order == DATE) {
221  find_widget<toggle_button>(&window, "sort_name", false).set_value(0);
222  }
223 
224  force = false;
225  }
226 
228 }
229 
230 void campaign_selection::filter_text_changed(text_box_base* textbox, const std::string& text)
231 {
232  const std::vector<std::string> words = utils::split(text, ' ');
233 
234  if(words == last_search_words_) {
235  return;
236  }
237 
238  last_search_words_ = words;
239  window& window = *textbox->get_window();
241 }
242 
244 {
245  text_box* filter = find_widget<text_box>(&window, "filter_box", false, true);
247  std::bind(&campaign_selection::filter_text_changed, this, _1, _2));
248 
249  /***** Setup campaign tree. *****/
250  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
251 
252  tree.set_selection_change_callback(std::bind(&campaign_selection::campaign_selected, this, std::ref(window)));
253 
254  toggle_button& sort_name = find_widget<toggle_button>(&window, "sort_name", false);
255  toggle_button& sort_time = find_widget<toggle_button>(&window, "sort_time", false);
256 
258  std::bind(&campaign_selection::toggle_sorting_selection, this, std::ref(window), NAME));
259 
261  std::bind(&campaign_selection::toggle_sorting_selection, this, std::ref(window), DATE));
262 
263  window.keyboard_capture(filter);
264  window.add_to_keyboard_chain(&tree);
265 
266  /***** Setup campaign details. *****/
267  multi_page& pages = find_widget<multi_page>(&window, "campaign_details", false);
268 
269  for(const auto& level : engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN)) {
270  const config& campaign = level->data();
271 
272  /*** Add tree item ***/
273  add_campaign_to_tree(window, campaign);
274 
275  /*** Add detail item ***/
276  std::map<std::string, string_map> data;
278 
279  item["label"] = campaign["description"];
280  item["use_markup"] = "true";
281 
282  if(!campaign["description_alignment"].empty()) {
283  item["text_alignment"] = campaign["description_alignment"];
284  }
285 
286  data.emplace("description", item);
287 
288  item["label"] = campaign["image"];
289  data.emplace("image", item);
290 
291  pages.add_page(data);
292  page_ids_.push_back(campaign["id"]);
293  }
294 
295  //
296  // Set up Mods selection dropdown
297  //
298  multimenu_button& mods_menu = find_widget<multimenu_button>(&window, "mods_menu", false);
299 
301  std::vector<config> mod_menu_values;
302  std::vector<std::string> enabled = engine_.active_mods();
303 
305  const bool active = std::find(enabled.begin(), enabled.end(), mod->id) != enabled.end();
306 
307  mod_menu_values.emplace_back("label", mod->name, "checkbox", active);
308 
309  mod_states_.push_back(active);
310  }
311 
312  mods_menu.set_values(mod_menu_values);
313  mods_menu.select_options(mod_states_);
314 
315  connect_signal_notify_modified(mods_menu, std::bind(&campaign_selection::mod_toggled, this, std::ref(window)));
316  } else {
317  mods_menu.set_active(false);
318  mods_menu.set_label(_("active_modifications^None"));
319  }
320 
321  campaign_selected(window);
322 }
323 
325 {
326  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
327  std::map<std::string, string_map> data;
329 
330  item["label"] = campaign["icon"];
331  data.emplace("icon", item);
332 
333  item["label"] = campaign["name"];
334  data.emplace("name", item);
335 
336  // We completed the campaign! Calculate the appropriate victory laurel.
337  if(campaign["completed"].to_bool()) {
338  config::const_child_itors difficulties = campaign.child_range("difficulty");
339 
340  auto did_complete_at = [](const config& c) { return c["completed_at"].to_bool(); };
341 
342  // Check for non-completion on every difficulty save the first.
343  const bool only_first_completed = difficulties.size() > 1 &&
344  std::none_of(difficulties.begin() + 1, difficulties.end(), did_complete_at);
345 
346  /*
347  * Criteria:
348  *
349  * - Use the gold laurel (hardest) for campaigns with only one difficulty OR
350  * if out of two or more difficulties, the last one has been completed.
351  *
352  * - Use the bronze laurel (easiest) only if the first difficulty out of two
353  * or more has been completed.
354  *
355  * - Use the silver laurel otherwise.
356  */
357  if(!difficulties.empty() && did_complete_at(difficulties.back())) {
359  } else if(only_first_completed && did_complete_at(difficulties.front())) {
361  } else {
362  item["label"] = game_config::images::victory_laurel;
363  }
364 
365  data.emplace("victory", item);
366  }
367 
368  tree.add_node("campaign", data).set_id(campaign["id"]);
369 }
370 
372 {
373  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
374 
375  if(tree.empty()) {
376  return;
377  }
378 
379  assert(tree.selected_item());
380  if(!tree.selected_item()->id().empty()) {
381  auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());
382  if(iter != page_ids_.end()) {
383  choice_ = std::distance(page_ids_.begin(), iter);
384  }
385  }
386 
387  deterministic_ = find_widget<toggle_button>(&window, "checkbox_deterministic", false).get_value_bool();
388 
390 }
391 
393 {
394  boost::dynamic_bitset<> new_mod_states =
395  find_widget<multimenu_button>(&window, "mods_menu", false).get_toggle_states();
396 
397  // Get a mask of any mods that were toggled, regardless of new state
398  mod_states_ = mod_states_ ^ new_mod_states;
399 
400  for(unsigned i = 0; i < mod_states_.size(); i++) {
401  if(mod_states_[i]) {
402  engine_.toggle_mod(i);
403  }
404  }
405 
406  // Save the full toggle states for next time
407  mod_states_ = new_mod_states;
408 }
409 
410 } // namespace dialogs
411 } // namespace gui2
void campaign_selected(window &window)
Called when another campaign is selected.
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
std::string victory_laurel
Abstract base class for text items.
std::shared_ptr< level > level_ptr
void set_selection_change_callback(std::function< void(widget &)> callback)
Definition: tree_view.hpp:105
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
tree_view_node * selected_item()
Definition: tree_view.hpp:95
std::string victory_laurel_easy
Simple push button.
std::pair< irdya_date, irdya_date > dates() const
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
std::vector< std::pair< const std::string *, const stats * > > levels
Stats (and name) for each scenario. The pointers are never nullptr.
Definition: statistics.hpp:137
Class for a single line text area.
Definition: text_box.hpp:121
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 const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
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:248
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:238
bool ci_search(const std::string &s1, const std::string &s2)
Definition: gettext.cpp:519
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
window * get_window()
Get the parent window.
Definition: widget.cpp:114
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
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.
Definition: gettext.cpp:476
bool empty() const
Definition: tree_view.cpp:119
void filter_text_changed(text_box_base *textbox, const std::string &text)
void set_modifications(const std::vector< std::string > &value, bool mp)
Definition: game.cpp:750
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.
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:92
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:61
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:371
std::vector< std::string > last_search_words_
void add_campaign_to_tree(window &window, const config &campaign)