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 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 
18 
19 #include "config.hpp"
20 #include "font/text_formatting.hpp"
21 #include "formatter.hpp"
23 #include "preferences/game.hpp"
24 #ifdef GUI2_EXPERIMENTAL_LISTBOX
25 #include "gui/widgets/list.hpp"
26 #else
27 #include "gui/widgets/listbox.hpp"
28 #endif
29 #include "gui/widgets/window.hpp"
30 #include "log.hpp"
32 
33 static lg::log_domain log_wml("wml");
34 #define WRN_WML LOG_STREAM(warn, log_wml)
35 
36 namespace gui2
37 {
38 namespace dialogs
39 {
40 
41 /*WIKI
42  * @page = GUIWindowDefinitionWML
43  * @order = 2_campaign_difficulty
44  *
45  * == Campaign difficulty ==
46  *
47  * The campaign mode difficulty menu.
48  *
49  * @begin{table}{dialog_widgets}
50  *
51  * title & & label & m &
52  * Dialog title label. $
53  *
54  * message & & scroll_label & o &
55  * Text label displaying a description or instructions. $
56  *
57  * listbox & & listbox & m &
58  * Listbox displaying user choices, defined by WML for each campaign. $
59  *
60  * -icon & & styled_widget & m &
61  * Widget which shows a listbox item icon, first item markup column. $
62  *
63  * -label & & styled_widget & m &
64  * Widget which shows a listbox item label, second item markup column. $
65  *
66  * -description & & styled_widget & m &
67  * Widget which shows a listbox item description, third item markup
68  * column. $
69  *
70  * @end{table}
71  */
72 
73 REGISTER_DIALOG(campaign_difficulty)
74 
76 {
77  config result;
78 
79  // Populate local config with difficulty children
80  result.append_children(source, "difficulty");
81  return result;
82 }
83 
85  : difficulties_(generate_difficulty_config(campaign))
86  , campaign_id_(campaign["id"])
87  , selected_difficulty_("CANCEL")
88 {
89 }
90 
92 {
93  listbox& list = find_widget<listbox>(&window, "listbox", false);
94  window.keyboard_capture(&list);
95 
96  for(const config& d : difficulties_.child_range("difficulty")) {
97  std::map<std::string, string_map> data;
98  string_map item;
99 
100  item["label"] = d["image"];
101  data.emplace("icon", item);
102 
103  item["use_markup"] = "true";
104 
105  item["label"] = d["label"];
106  data.emplace("label", item);
107 
108  if(!d["description"].empty()) {
109  item["label"] = (formatter() <<
110  font::span_color(font::GRAY_COLOR) << "(" << d["description"].str() << ")</span>").str();
111  data.emplace("description", item);
112  }
113 
114  grid& grid = list.add_row(data);
115 
116  if(d["default"].to_bool(false)) {
117  list.select_last_row();
118  }
119 
120  widget* widget = grid.find("victory", false);
121  if(widget && !preferences::is_campaign_completed(campaign_id_, d["define"])) {
123  }
124  }
125 }
126 
128 {
129  if(get_retval() == retval::OK) {
130  listbox& list = find_widget<listbox>(&window, "listbox", false);
131  selected_difficulty_ = difficulties_.child("difficulty", list.get_selected_row())["define"].str();
132  }
133 }
134 } // namespace dialogs
135 } // 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:323
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:194
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).
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:42
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::ostringstream wrapper.
Definition: formatter.hpp:38
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:640
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:68
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:63
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.