The Battle for Wesnoth  1.15.12+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 
19 #include "font/text_formatting.hpp"
22 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/image.hpp"
24 #include "gui/widgets/listbox.hpp"
29 #include "gui/widgets/settings.hpp"
30 #include "gui/widgets/text_box.hpp"
34 #include "gui/widgets/window.hpp"
35 #include "lexical_cast.hpp"
36 #include "preferences/game.hpp"
37 
38 #include <functional>
39 #include "utils/irdya_datetime.hpp"
40 
41 namespace gui2::dialogs
42 {
43 
44 REGISTER_DIALOG(campaign_selection)
45 
46 void campaign_selection::campaign_selected()
47 {
48  tree_view& tree = find_widget<tree_view>(get_window(), "campaign_tree", false);
49  if(tree.empty()) {
50  return;
51  }
52 
53  assert(tree.selected_item());
54 
55  if(!tree.selected_item()->id().empty()) {
56  auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());
57 
58  const int choice = std::distance(page_ids_.begin(), iter);
59  if(iter == page_ids_.end()) {
60  return;
61  }
62 
63  multi_page& pages = find_widget<multi_page>(get_window(), "campaign_details", false);
64  pages.select_page(choice);
65 
66  engine_.set_current_level(choice);
67 
68  styled_widget& background = find_widget<styled_widget>(get_window(), "campaign_background", false);
69  background.set_label(engine_.current_level().data()["background"].str());
70 
71  // Rebuild difficulty menu
72  difficulties_.clear();
73 
74  auto& diff_menu = find_widget<menu_button>(get_window(), "difficulty_menu", false);
75 
76  const auto& diff_config = generate_difficulty_config(engine_.current_level().data());
77  diff_menu.set_active(diff_config.child_count("difficulty") > 1);
78 
79  if(!diff_config.empty()) {
80  std::vector<config> entry_list;
81  unsigned n = 0, selection = 0, max_n = diff_config.child_count("difficulty");
82 
83  for(const auto& cfg : diff_config.child_range("difficulty")) {
84  config entry;
85 
86  // FIXME: description may have markup that will display weird on the menu_button proper
87  entry["label"] = cfg["label"].str() + " (" + cfg["description"].str() + ")";
88  entry["image"] = cfg["image"].str("misc/blank-hex.png");
89 
90  if(preferences::is_campaign_completed(tree.selected_item()->id(), cfg["define"])) {
91  std::string laurel;
92 
93  if(n + 1 >= max_n) {
95  } else if(n == 0) {
97  } else {
99  }
100 
101  entry["image"] = laurel + "~BLIT(" + entry["image"] + ")";
102  }
103 
104  if(!cfg["description"].empty()) {
105  std::string desc;
106  if(cfg["auto_markup"].to_bool(true) == false) {
107  desc = cfg["description"].str();
108  } else {
109  //desc = "<small>";
110  if(!cfg["old_markup"].to_bool()) {
111  desc += font::span_color(font::GRAY_COLOR) + "(" + cfg["description"].str() + ")</span>";
112  } else {
113  desc += font::span_color(font::GRAY_COLOR) + cfg["description"].str() + "</span>";
114  }
115  //desc += "</small>";
116  }
117 
118  // Icons get displayed instead of the labels on the dropdown menu itself,
119  // so we want to prepend each label to its description here
120  desc = cfg["label"].str() + "\n" + desc;
121 
122  entry["details"] = std::move(desc);
123  }
124 
125  entry_list.emplace_back(std::move(entry));
126  difficulties_.emplace_back(cfg["define"].str());
127 
128  if(cfg["default"].to_bool(false)) {
129  selection = n;
130  }
131 
132  ++n;
133  }
134 
135  diff_menu.set_values(entry_list);
136  diff_menu.set_selected(selection);
137  }
138  }
139 }
140 
142 {
143  const std::size_t selection = find_widget<menu_button>(get_window(), "difficulty_menu", false).get_value();
144  current_difficulty_ = difficulties_.at(std::min(difficulties_.size() - 1, selection));
145 }
146 
148 {
149  using level_ptr = ng::create_engine::level_ptr;
150 
151  auto levels = engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN);
152 
153  switch(order) {
154  case RANK: // Already sorted by rank
155  // This'll actually never happen, but who knows if that'll ever change...
156  if(!ascending) {
157  std::reverse(levels.begin(), levels.end());
158  }
159 
160  break;
161 
162  case DATE:
163  std::sort(levels.begin(), levels.end(), [ascending](const level_ptr& a, const level_ptr& b) {
164  auto cpn_a = std::dynamic_pointer_cast<ng::campaign>(a);
165  auto cpn_b = std::dynamic_pointer_cast<ng::campaign>(b);
166 
167  if(cpn_b == nullptr) {
168  return cpn_a != nullptr;
169  }
170 
171  if(cpn_a == nullptr) {
172  return false;
173  }
174 
175  return ascending
176  ? cpn_a->dates().first < cpn_b->dates().first
177  : cpn_a->dates().first > cpn_b->dates().first;
178  });
179 
180  break;
181 
182  case NAME:
183  std::sort(levels.begin(), levels.end(), [ascending](const level_ptr& a, const level_ptr& b) {
184  const int cmp = translation::icompare(a->name(), b->name());
185  return ascending ? cmp < 0 : cmp > 0;
186  });
187 
188  break;
189  }
190 
191  tree_view& tree = find_widget<tree_view>(get_window(), "campaign_tree", false);
192 
193  // Remember which campaign was selected...
194  std::string was_selected;
195  if(!tree.empty()) {
196  was_selected = tree.selected_item()->id();
197  tree.clear();
198  }
199 
200  boost::dynamic_bitset<> show_items;
201  show_items.resize(levels.size(), true);
202 
203  if(!last_search_words_.empty()) {
204  for(unsigned i = 0; i < levels.size(); ++i) {
205  bool found = false;
206  for(const auto& word : last_search_words_) {
207  found = translation::ci_search(levels[i]->name(), word) ||
208  translation::ci_search(levels[i]->data()["name"].t_str().base_str(), word) ||
209  translation::ci_search(levels[i]->description(), word) ||
210  translation::ci_search(levels[i]->data()["description"].t_str().base_str(), word) ||
211  translation::ci_search(levels[i]->data()["abbrev"], word) ||
212  translation::ci_search(levels[i]->data()["abbrev"].t_str().base_str(), word);
213 
214  if(!found) {
215  break;
216  }
217  }
218 
219  show_items[i] = found;
220  }
221  }
222 
223  bool exists_in_filtered_result = false;
224  for(unsigned i = 0; i < levels.size(); ++i) {
225  if(show_items[i]) {
226  add_campaign_to_tree(levels[i]->data());
227 
228  if (!exists_in_filtered_result) {
229  exists_in_filtered_result = levels[i]->id() == was_selected;
230  }
231  }
232  }
233 
234  if(!was_selected.empty() && exists_in_filtered_result) {
235  find_widget<tree_view_node>(get_window(), was_selected, false).select_node();
236  } else {
238  }
239 }
240 
242 {
243  static bool force = false;
244  if(force) {
245  return;
246  }
247 
248  if(current_sorting_ == order) {
250  currently_sorted_asc_ = false;
251  } else {
252  currently_sorted_asc_ = true;
254  }
255  } else if(current_sorting_ == RANK) {
256  currently_sorted_asc_ = true;
257  current_sorting_ = order;
258  } else {
259  currently_sorted_asc_ = true;
260  current_sorting_ = order;
261 
262  force = true;
263 
264  if(order == NAME) {
265  find_widget<toggle_button>(get_window(), "sort_time", false).set_value(0);
266  } else if(order == DATE) {
267  find_widget<toggle_button>(get_window(), "sort_name", false).set_value(0);
268  }
269 
270  force = false;
271  }
272 
274 }
275 
276 void campaign_selection::filter_text_changed(const std::string& text)
277 {
278  const std::vector<std::string> words = utils::split(text, ' ');
279 
280  if(words == last_search_words_) {
281  return;
282  }
283 
284  last_search_words_ = words;
286 }
287 
289 {
290  text_box* filter = find_widget<text_box>(&window, "filter_box", false, true);
292  std::bind(&campaign_selection::filter_text_changed, this, std::placeholders::_2));
293 
294  /***** Setup campaign tree. *****/
295  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
296 
298  std::bind(&campaign_selection::campaign_selected, this));
299 
300  toggle_button& sort_name = find_widget<toggle_button>(&window, "sort_name", false);
301  toggle_button& sort_time = find_widget<toggle_button>(&window, "sort_time", false);
302 
305 
308 
309  window.keyboard_capture(filter);
310  window.add_to_keyboard_chain(&tree);
311 
312  /***** Setup campaign details. *****/
313  multi_page& pages = find_widget<multi_page>(&window, "campaign_details", false);
314 
315  for(const auto& level : engine_.get_levels_by_type_unfiltered(ng::level::TYPE::SP_CAMPAIGN)) {
316  const config& campaign = level->data();
317 
318  /*** Add tree item ***/
319  add_campaign_to_tree(campaign);
320 
321  /*** Add detail item ***/
322  std::map<std::string, string_map> data;
324 
325  item["label"] = campaign["description"];
326  item["use_markup"] = "true";
327 
328  if(!campaign["description_alignment"].empty()) {
329  item["text_alignment"] = campaign["description_alignment"];
330  }
331 
332  data.emplace("description", item);
333 
334  item["label"] = campaign["image"];
335  data.emplace("image", item);
336 
337  pages.add_page(data);
338  page_ids_.push_back(campaign["id"]);
339  }
340 
341  //
342  // Set up Mods selection dropdown
343  //
344  multimenu_button& mods_menu = find_widget<multimenu_button>(&window, "mods_menu", false);
345 
347  std::vector<config> mod_menu_values;
348  std::vector<std::string> enabled = engine_.active_mods();
349 
351  const bool active = std::find(enabled.begin(), enabled.end(), mod->id) != enabled.end();
352 
353  mod_menu_values.emplace_back("label", mod->name, "checkbox", active);
354 
355  mod_states_.push_back(active);
356  }
357 
358  mods_menu.set_values(mod_menu_values);
359  mods_menu.select_options(mod_states_);
360 
362  } else {
363  mods_menu.set_active(false);
364  mods_menu.set_label(_("active_modifications^None"));
365  }
366 
367  //
368  // Set up Difficulty dropdown
369  //
370  menu_button& diff_menu = find_widget<menu_button>(get_window(), "difficulty_menu", false);
371 
372  diff_menu.set_use_markup(true);
374 
376 }
377 
379 {
380  tree_view& tree = find_widget<tree_view>(get_window(), "campaign_tree", false);
381  std::map<std::string, string_map> data;
383 
384  item["label"] = campaign["icon"];
385  data.emplace("icon", item);
386 
387  item["label"] = campaign["name"];
388  data.emplace("name", item);
389 
390  // We completed the campaign! Calculate the appropriate victory laurel.
391  if(campaign["completed"].to_bool()) {
392  config::const_child_itors difficulties = campaign.child_range("difficulty");
393 
394  auto did_complete_at = [](const config& c) { return c["completed_at"].to_bool(); };
395 
396  // Check for non-completion on every difficulty save the first.
397  const bool only_first_completed = difficulties.size() > 1 &&
398  std::none_of(difficulties.begin() + 1, difficulties.end(), did_complete_at);
399 
400  /*
401  * Criteria:
402  *
403  * - Use the gold laurel (hardest) for campaigns with only one difficulty OR
404  * if out of two or more difficulties, the last one has been completed.
405  *
406  * - Use the bronze laurel (easiest) only if the first difficulty out of two
407  * or more has been completed.
408  *
409  * - Use the silver laurel otherwise.
410  */
411  if(!difficulties.empty() && did_complete_at(difficulties.back())) {
413  } else if(only_first_completed && did_complete_at(difficulties.front())) {
415  } else {
416  item["label"] = game_config::images::victory_laurel;
417  }
418 
419  data.emplace("victory", item);
420  }
421 
422  tree.add_node("campaign", data).set_id(campaign["id"]);
423 }
424 
426 {
427  tree_view& tree = find_widget<tree_view>(&window, "campaign_tree", false);
428 
429  if(tree.empty()) {
430  return;
431  }
432 
433  assert(tree.selected_item());
434  if(!tree.selected_item()->id().empty()) {
435  auto iter = std::find(page_ids_.begin(), page_ids_.end(), tree.selected_item()->id());
436  if(iter != page_ids_.end()) {
437  choice_ = std::distance(page_ids_.begin(), iter);
438  }
439  }
440 
441 
442  rng_mode_ = RNG_MODE(std::clamp<unsigned>(find_widget<menu_button>(&window, "rng_menu", false).get_value(), RNG_DEFAULT, RNG_BIASED));
443 
445 }
446 
448 {
449  boost::dynamic_bitset<> new_mod_states =
450  find_widget<multimenu_button>(get_window(), "mods_menu", false).get_toggle_states();
451 
452  // Get a mask of any mods that were toggled, regardless of new state
453  mod_states_ = mod_states_ ^ new_mod_states;
454 
455  for(unsigned i = 0; i < mod_states_.size(); i++) {
456  if(mod_states_[i]) {
457  engine_.toggle_mod(i);
458  }
459  }
460 
461  // Save the full toggle states for next time
462  mod_states_ = new_mod_states;
463 }
464 
465 } // namespace dialogs
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
void add_campaign_to_tree(const config &campaign) const
std::shared_ptr< level > level_ptr
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:60
This shows the dialog which allows the user to choose which campaign to play.
const color_t GRAY_COLOR
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:109
bool is_campaign_completed(const std::string &campaign_id)
Definition: game.cpp:301
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:356
tree_view_node * selected_item()
Definition: tree_view.hpp:115
window * get_window() const
Returns a pointer to the dialog&#39;s window.
std::string victory_laurel_easy
A multimenu_button is a styled_widget to choose an element from a list of elements.
std::pair< irdya_date, irdya_date > dates() const
static std::string _(const char *str)
Definition: gettext.hpp:92
void campaign_selected()
Called when another campaign is selected.
std::vector< std::pair< const std::string *, const stats * > > levels
Stats (and name) for each scenario.
Definition: statistics.hpp:135
Class for a single line text area.
Definition: text_box.hpp:140
#define b
void filter_text_changed(const std::string &text)
virtual void set_label(const t_string &label)
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
void difficulty_selected()
Called when the difficulty selection changes.
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:186
std::string victory_laurel_hardest
RNG_MODE rng_mode_
whether the player checked the "Deterministic" checkbox.
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
Actions to be taken after the window has been shown.
virtual void set_use_markup(bool use_markup)
A tree view is a control that holds several items of the same or different types. ...
Definition: tree_view.hpp:59
std::vector< std::string > & active_mods()
void sort_campaigns(CAMPAIGN_ORDER order, bool ascending)
std::vector< std::string > page_ids_
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:204
bool ci_search(const std::string &s1, const std::string &s2)
Definition: gettext.cpp:517
virtual void set_active(const bool active) override
See styled_widget::set_active.
std::size_t i
Definition: function.cpp:940
void toggle_sorting_selection(CAMPAIGN_ORDER order)
config generate_difficulty_config(const config &source)
Helper function to convert old difficulty markup.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
void select_page(const unsigned page, const bool select=true)
Selects a page.
Definition: multi_page.cpp:106
std::vector< std::string > difficulties_
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
Base class for all visible items.
static int sort(lua_State *L)
Definition: ltablib.cpp:397
RNG_MODE
RNG mode selection values.
A multi page is a control that contains several &#39;pages&#39; of which only one is visible.
Definition: multi_page.hpp:48
std::vector< std::string > split(const config_attribute_value &val)
int icompare(const std::string &s1, const std::string &s2)
Case-insensitive lexicographical comparison.
Definition: gettext.cpp:474
bool empty() const
Definition: tree_view.cpp:110
void set_modifications(const std::vector< std::string > &value, bool mp)
Definition: game.cpp:728
void set_id(const std::string &id)
Definition: widget.cpp:97
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:203
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
mock_char c
static map_location::DIRECTION n
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
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:56
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:409
std::vector< std::string > last_search_words_