The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
campaign_difficulty.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2018 by Iris Morelle <shadowm2006@gmail.com>
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 
17 #include "config.hpp"
18 #include "preferences/game.hpp"
19 #include "formula/string_utils.hpp"
20 
22 
25 #ifdef GUI2_EXPERIMENTAL_LISTBOX
26 #include "gui/widgets/list.hpp"
27 #else
28 #include "gui/widgets/listbox.hpp"
29 #endif
30 #include "gui/widgets/settings.hpp"
31 #include "gui/widgets/window.hpp"
32 
33 #include "log.hpp"
34 
35 static lg::log_domain log_wml("wml");
36 #define WRN_WML LOG_STREAM(warn, log_wml)
37 
38 namespace gui2
39 {
40 namespace dialogs
41 {
42 
43 /*WIKI
44  * @page = GUIWindowDefinitionWML
45  * @order = 2_campaign_difficulty
46  *
47  * == Campaign difficulty ==
48  *
49  * The campaign mode difficulty menu.
50  *
51  * @begin{table}{dialog_widgets}
52  *
53  * title & & label & m &
54  * Dialog title label. $
55  *
56  * message & & scroll_label & o &
57  * Text label displaying a description or instructions. $
58  *
59  * listbox & & listbox & m &
60  * Listbox displaying user choices, defined by WML for each campaign. $
61  *
62  * -icon & & styled_widget & m &
63  * Widget which shows a listbox item icon, first item markup column. $
64  *
65  * -label & & styled_widget & m &
66  * Widget which shows a listbox item label, second item markup column. $
67  *
68  * -description & & styled_widget & m &
69  * Widget which shows a listbox item description, third item markup
70  * column. $
71  *
72  * @end{table}
73  */
74 
75 REGISTER_DIALOG(campaign_difficulty)
76 
78 {
79  config result;
80 
81  // Populate local config with difficulty children
82  result.append_children(source, "difficulty");
83 
84  // Convert legacy format to new-style config if latter not present
85  if(result.empty()) {
86  WRN_WML << "[campaign] difficulties,difficulty_descriptions= is deprecated. Use [difficulty] instead" << std::endl;
87 
88  std::vector<std::string> difficulty_list = utils::split(source["difficulties"]);
89  std::vector<std::string> difficulty_opts = utils::split(source["difficulty_descriptions"], ';');
90 
91  if(difficulty_opts.size() != difficulty_list.size()) {
92  difficulty_opts = difficulty_list;
93  }
94 
95  for(std::size_t i = 0; i < difficulty_opts.size(); i++)
96  {
97  config temp;
98  gui2::legacy_menu_item parsed(difficulty_opts[i]);
99 
100  temp["define"] = difficulty_list[i];
101  temp["image"] = parsed.icon();
102  temp["label"] = parsed.label();
103  temp["description"] = parsed.description();
104  temp["default"] = parsed.is_default();
105  temp["old_markup"] = true; // To prevent double parentheses in the dialog
106 
107  result.add_child("difficulty", std::move(temp));
108  }
109  }
110 
111  return result;
112 }
113 
115  : difficulties_()
116  , campaign_id_(campaign["id"])
117  , selected_difficulty_()
118 {
119  set_restore(true);
120 
122 }
123 
125 {
126  listbox& list = find_widget<listbox>(&window, "listbox", false);
127  window.keyboard_capture(&list);
128 
129  std::map<std::string, string_map> data;
130 
131  for (const config &d : difficulties_.child_range("difficulty"))
132  {
133  data["icon"]["label"] = d["image"];
134  data["label"]["label"] = d["label"];
135  data["label"]["use_markup"] = "true";
136  data["description"]["label"] = d["old_markup"].to_bool() || d["description"].empty() ? d["description"]
137  : std::string("(") + d["description"] + std::string(")");
138  data["description"]["use_markup"] = "true";
139 
140  list.add_row(data);
141 
142  const int this_row = list.get_item_count() - 1;
143 
144  if(d["default"].to_bool(false)) {
145  list.select_row(this_row);
146  }
147 
148  grid* grid = list.get_row_grid(this_row);
149  assert(grid);
150 
151  widget *widget = grid->find("victory", false);
152  if (widget && !preferences::is_campaign_completed(campaign_id_, d["define"])) {
154  }
155  }
156 }
157 
159 {
160  if(get_retval() != window::OK) {
161  selected_difficulty_ = "CANCEL";
162  return;
163  }
164 
165  listbox& list = find_widget<listbox>(&window, "listbox", false);
166  selected_difficulty_ = difficulties_.child("difficulty", list.get_selected_row())["define"].str();
167 }
168 } // namespace dialogs
169 } // namespace gui2
Dialog is closed with ok button.
Definition: window.hpp:101
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:417
const std::string & description() const
Definition: old_markup.hpp:54
std::vector< char_t > string
bool is_campaign_completed(const std::string &campaign_id)
Definition: game.cpp:319
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:360
Base class for all widgets.
Definition: widget.hpp:48
#define d
void append_children(const config &cfg)
Adds children from cfg.
Definition: config.cpp:241
bool empty() const
Definition: config.cpp:828
Definitions for the interface to Wesnoth Markup Language (WML).
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.
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:252
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:42
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:274
Base container class.
Definition: grid.hpp:30
#define WRN_WML
This file contains the settings handling of the widget library.
void set_visible(const visibility visible)
Definition: widget.cpp:479
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:655
Various uncategorised dialogs.
config generate_difficulty_config(const config &source)
Helper function to convert old difficulty markup.
size_t i
Definition: function.cpp:933
grid & 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:68
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:239
bool is_default() const
Definition: old_markup.hpp:59
static lg::log_domain log_wml("wml")
config & add_child(config_key_type key)
Definition: config.cpp:473
const std::string & icon() const
Definition: old_markup.hpp:44
The user sets the widget hidden, that means:
Standard logging facilities (interface).
Implements simple parsing of legacy GUI1 item markup.
Definition: old_markup.hpp:25
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:133
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
const std::string & label() const
Definition: old_markup.hpp:49
campaign_difficulty(const config &campaign)
virtual void pre_show(window &window) override
Inherited from modal_dialog.
void set_restore(const bool restore)
virtual void post_show(window &window) override
Inherited from modal_dialog.