The Battle for Wesnoth  1.15.0-dev
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 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 "config.hpp"
20 #include "font/text_formatting.hpp"
21 #include "formatter.hpp"
24 #include "preferences/game.hpp"
25 #include "gui/widgets/listbox.hpp"
26 #include "gui/widgets/window.hpp"
27 #include "log.hpp"
29 #include "deprecation.hpp"
30 
31 static lg::log_domain log_wml("wml");
32 #define WRN_WML LOG_STREAM(warn, log_wml)
33 
34 namespace gui2
35 {
36 namespace dialogs
37 {
38 
39 /*WIKI
40  * @page = GUIWindowDefinitionWML
41  * @order = 2_campaign_difficulty
42  *
43  * == Campaign difficulty ==
44  *
45  * The campaign mode difficulty menu.
46  *
47  * @begin{table}{dialog_widgets}
48  *
49  * title & & label & m &
50  * Dialog title label. $
51  *
52  * message & & scroll_label & o &
53  * Text label displaying a description or instructions. $
54  *
55  * listbox & & listbox & m &
56  * Listbox displaying user choices, defined by WML for each campaign. $
57  *
58  * -icon & & styled_widget & m &
59  * Widget which shows a listbox item icon, first item markup column. $
60  *
61  * -label & & styled_widget & m &
62  * Widget which shows a listbox item label, second item markup column. $
63  *
64  * -description & & styled_widget & m &
65  * Widget which shows a listbox item description, third item markup
66  * column. $
67  *
68  * @end{table}
69  */
70 
71 REGISTER_DIALOG(campaign_difficulty)
72 
74 {
75  config result;
76 
77  // Populate local config with difficulty children
78  result.append_children(source, "difficulty");
79 
80  // Convert legacy format to new-style config if latter not present
81  if(result.empty() && source.has_attribute("difficulties")) {
82  deprecated_message("[campaign]difficulties", DEP_LEVEL::FOR_REMOVAL, {1, 15, 0}, "Use [difficulty] instead.");
83  if(source.has_attribute("difficulty_descriptions")) {
84  deprecated_message("[campaign]difficulty_descriptions", DEP_LEVEL::FOR_REMOVAL, {1, 15, 0}, "Use [difficulty] instead.");
85  }
86 
87  std::vector<std::string> difficulty_list = utils::split(source["difficulties"]);
88  std::vector<std::string> difficulty_opts = utils::split(source["difficulty_descriptions"], ';');
89 
90  if(difficulty_opts.size() != difficulty_list.size()) {
91  difficulty_opts = difficulty_list;
92  }
93 
94  for(std::size_t i = 0; i < difficulty_opts.size(); ++i) {
95  config temp;
96  gui2::legacy_menu_item parsed(difficulty_opts[i], "Use [difficulty] instead.");
97 
98  temp["define"] = difficulty_list[i];
99  temp["image"] = parsed.icon();
100  temp["label"] = parsed.label();
101  temp["description"] = parsed.description();
102  temp["default"] = parsed.is_default();
103  temp["old_markup"] = true; // To prevent double parentheses in the dialog
104 
105  result.add_child("difficulty", std::move(temp));
106  }
107  }
108 
109  return result;
110 }
111 
113  : difficulties_(generate_difficulty_config(campaign))
114  , campaign_id_(campaign["id"])
115  , selected_difficulty_("CANCEL")
116 {
117  set_restore(true);
118 }
119 
121 {
122  listbox& list = find_widget<listbox>(&window, "listbox", false);
123  window.keyboard_capture(&list);
124 
125  for(const config& d : difficulties_.child_range("difficulty")) {
126  std::map<std::string, string_map> data;
127  string_map item;
128 
129  item["label"] = d["image"];
130  data.emplace("icon", item);
131 
132  item["use_markup"] = "true";
133 
134  std::ostringstream ss;
135  ss << d["label"];
136 
137  if(!d["description"].empty()) {
138  if (!d["old_markup"].to_bool()) {
139  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << "(" << d["description"].str() << ")</span></small>";
140  } else {
141  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << d["description"] << "</span></small>";
142  }
143  }
144 
145  item["label"] = ss.str();
146  data.emplace("label", item);
147 
148  grid& grid = list.add_row(data);
149 
150  if(d["default"].to_bool(false)) {
151  list.select_last_row();
152  }
153 
154  widget* widget = grid.find("victory", false);
155  if(widget && !preferences::is_campaign_completed(campaign_id_, d["define"])) {
157  }
158  }
159 }
160 
162 {
163  if(get_retval() == retval::OK) {
164  listbox& list = find_widget<listbox>(&window, "listbox", false);
165  selected_difficulty_ = difficulties_.child("difficulty", list.get_selected_row())["define"].str();
166  }
167 }
168 } // namespace dialogs
169 } // namespace gui2
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:423
const color_t GRAY_COLOR
const std::string & icon() const
Definition: old_markup.hpp:44
bool is_campaign_completed(const std::string &campaign_id)
Definition: game.cpp:322
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
Base class for all widgets.
Definition: widget.hpp:47
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list&#39;s last row.
Definition: listbox.hpp:192
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:272
#define d
void append_children(const config &cfg)
Adds children from cfg.
Definition: config.cpp:245
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.
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
Base container class.
Definition: grid.hpp:30
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:29
void set_visible(const visibility visible)
Definition: widget.cpp:473
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:655
bool is_default() const
Definition: old_markup.hpp:59
Various uncategorised dialogs.
std::size_t i
Definition: function.cpp:933
config generate_difficulty_config(const config &source)
Helper function to convert old difficulty markup.
const std::string & label() const
Definition: old_markup.hpp:49
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
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:66
static lg::log_domain log_wml("wml")
config & add_child(config_key_type key)
Definition: config.cpp:479
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
Dialog was closed with the OK button.
Definition: retval.hpp:34
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
campaign_difficulty(const config &campaign)
bool empty() const
Definition: config.cpp:886
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.
const std::string & description() const
Definition: old_markup.hpp:54