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"
23 #include "preferences/game.hpp"
24 #include "gui/widgets/listbox.hpp"
25 #include "gui/widgets/window.hpp"
26 #include "log.hpp"
28 
29 static lg::log_domain log_wml("wml");
30 #define WRN_WML LOG_STREAM(warn, log_wml)
31 
32 namespace gui2
33 {
34 namespace dialogs
35 {
36 
37 /*WIKI
38  * @page = GUIWindowDefinitionWML
39  * @order = 2_campaign_difficulty
40  *
41  * == Campaign difficulty ==
42  *
43  * The campaign mode difficulty menu.
44  *
45  * @begin{table}{dialog_widgets}
46  *
47  * title & & label & m &
48  * Dialog title label. $
49  *
50  * message & & scroll_label & o &
51  * Text label displaying a description or instructions. $
52  *
53  * listbox & & listbox & m &
54  * Listbox displaying user choices, defined by WML for each campaign. $
55  *
56  * -icon & & styled_widget & m &
57  * Widget which shows a listbox item icon, first item markup column. $
58  *
59  * -label & & styled_widget & m &
60  * Widget which shows a listbox item label, second item markup column. $
61  *
62  * -description & & styled_widget & m &
63  * Widget which shows a listbox item description, third item markup
64  * column. $
65  *
66  * @end{table}
67  */
68 
69 REGISTER_DIALOG(campaign_difficulty)
70 
72 {
73  config result;
74 
75  // Populate local config with difficulty children
76  result.append_children(source, "difficulty");
77  return result;
78 }
79 
81  : difficulties_(generate_difficulty_config(campaign))
82  , campaign_id_(campaign["id"])
83  , selected_difficulty_("CANCEL")
84 {
85 }
86 
88 {
89  listbox& list = find_widget<listbox>(&window, "listbox", false);
90  window.keyboard_capture(&list);
91 
92  for(const config& d : difficulties_.child_range("difficulty")) {
93  widget_data data;
94  widget_item item;
95 
96  item["label"] = d["image"];
97  data.emplace("icon", item);
98 
99  item["use_markup"] = "true";
100 
101  std::ostringstream ss;
102  ss << d["label"];
103 
104  if(!d["description"].empty()) {
105  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << "(" << d["description"].str() << ")</span></small>";
106  }
107 
108  item["label"] = ss.str();
109  data.emplace("label", item);
110 
111  grid& grid = list.add_row(data);
112 
113  if(d["default"].to_bool(false)) {
114  list.select_last_row();
115  }
116 
117  widget* widget = grid.find("victory", false);
118  if(widget && !preferences::is_campaign_completed(campaign_id_, d["define"])) {
120  }
121  }
122 }
123 
125 {
126  if(get_retval() == retval::OK) {
127  listbox& list = find_widget<listbox>(&window, "listbox", false);
128  selected_difficulty_ = difficulties_.child("difficulty", list.get_selected_row())["define"].str();
129  }
130 }
131 } // namespace dialogs
132 } // 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
bool is_campaign_completed(const std::string &campaign_id)
Definition: game.cpp:320
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:48
grid & add_row(const widget_item &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:65
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list&#39;s last row.
Definition: listbox.hpp:194
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:269
#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).
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::map< std::string, t_string > widget_item
Definition: widget.hpp:27
void set_visible(const visibility visible)
Definition: widget.cpp:445
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:641
Various uncategorised dialogs.
config generate_difficulty_config(const config &source)
Helper function to convert old difficulty markup.
static lg::log_domain log_wml("wml")
The user sets the widget hidden, that means:
Standard logging facilities (interface).
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:30
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)
virtual void pre_show(window &window) override
Inherited from modal_dialog.
virtual void post_show(window &window) override
Inherited from modal_dialog.