The Battle for Wesnoth  1.15.2+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"
23 #include "preferences/game.hpp"
24 #include "gui/widgets/listbox.hpp"
25 #include "gui/widgets/window.hpp"
26 #include "log.hpp"
28 #include "deprecation.hpp"
29 
30 static lg::log_domain log_wml("wml");
31 #define WRN_WML LOG_STREAM(warn, log_wml)
32 
33 namespace gui2
34 {
35 namespace dialogs
36 {
37 
38 /*WIKI
39  * @page = GUIWindowDefinitionWML
40  * @order = 2_campaign_difficulty
41  *
42  * == Campaign difficulty ==
43  *
44  * The campaign mode difficulty menu.
45  *
46  * @begin{table}{dialog_widgets}
47  *
48  * title & & label & m &
49  * Dialog title label. $
50  *
51  * message & & scroll_label & o &
52  * Text label displaying a description or instructions. $
53  *
54  * listbox & & listbox & m &
55  * Listbox displaying user choices, defined by WML for each campaign. $
56  *
57  * -icon & & styled_widget & m &
58  * Widget which shows a listbox item icon, first item markup column. $
59  *
60  * -label & & styled_widget & m &
61  * Widget which shows a listbox item label, second item markup column. $
62  *
63  * -description & & styled_widget & m &
64  * Widget which shows a listbox item description, third item markup
65  * column. $
66  *
67  * @end{table}
68  */
69 
70 REGISTER_DIALOG(campaign_difficulty)
71 
73 {
74  config result;
75 
76  // Populate local config with difficulty children
77  result.append_children(source, "difficulty");
78 
79  // Issue deprecation warnings about the old difficulties syntax
80  if(result.empty() && source.has_attribute("difficulties")) {
81  deprecated_message("[campaign]difficulties", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
82  if(source.has_attribute("difficulty_descriptions")) {
83  deprecated_message("[campaign]difficulty_descriptions", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
84  }
85  }
86 
87  return result;
88 }
89 
91  : difficulties_(generate_difficulty_config(campaign))
92  , campaign_id_(campaign["id"])
93  , selected_difficulty_("CANCEL")
94 {
95  set_restore(true);
96 }
97 
99 {
100  listbox& list = find_widget<listbox>(&window, "listbox", false);
101  window.keyboard_capture(&list);
102 
103  for(const config& d : difficulties_.child_range("difficulty")) {
104  std::map<std::string, string_map> data;
106 
107  item["label"] = d["image"];
108  data.emplace("icon", item);
109 
110  item["use_markup"] = "true";
111 
112  std::ostringstream ss;
113  ss << d["label"];
114 
115  if(!d["description"].empty()) {
116  if (d["auto_markup"].to_bool(true) == false) {
117  ss << "\n" << d["description"].str();
118  } else if (!d["old_markup"].to_bool()) {
119  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << "(" << d["description"].str() << ")</span></small>";
120  } else {
121  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << d["description"] << "</span></small>";
122  }
123  }
124 
125  item["label"] = ss.str();
126  data.emplace("label", item);
127 
128  grid& grid = list.add_row(data);
129 
130  if(d["default"].to_bool(false)) {
131  list.select_last_row();
132  }
133 
134  widget* widget = grid.find("victory", false);
135  if(widget && !preferences::is_campaign_completed(campaign_id_, d["define"])) {
137  }
138  }
139 }
140 
142 {
143  if(get_retval() == retval::OK) {
144  listbox& list = find_widget<listbox>(&window, "listbox", false);
145  selected_difficulty_ = difficulties_.child("difficulty", list.get_selected_row())["define"].str();
146  }
147 }
148 } // namespace dialogs
149 } // 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:420
const color_t GRAY_COLOR
bool is_campaign_completed(const std::string &campaign_id)
Definition: game.cpp:333
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
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:241
Definitions for the interface to Wesnoth Markup Language (WML).
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
Various uncategorised dialogs.
config generate_difficulty_config(const config &source)
Helper function to convert old difficulty markup.
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")
The user sets the widget hidden, that means:
Standard logging facilities (interface).
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:68
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
campaign_difficulty(const config &campaign)
bool empty() const
Definition: config.cpp:884
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.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:371