The Battle for Wesnoth  1.13.4+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 - 2016 by Ignacio Riquelme Morelle <[email protected]>
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 "game_preferences.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 
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 & & control & m &
61  * Widget which shows a listbox item icon, first item markup column. $
62  *
63  * -label & & control & m &
64  * Widget which shows a listbox item label, second item markup column. $
65  *
66  * -description & & control & 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  const config& campaign)
77  : difficulties_()
78  , campaign_id_(campaign["id"])
79  , selected_difficulty_()
80 {
81  set_restore(true);
82 
83  // Populate local config with difficulty children
84  difficulties_.append_children(campaign, "difficulty");
85 
86  // Convert legacy format to new-style config if latter not present
87  if(difficulties_.empty()) {
88  WRN_WML << "[campaign] difficulties,difficulty_descriptions= is deprecated. Use [difficulty] instead" << std::endl;
89 
90  std::vector<std::string> difficulty_list = utils::split(campaign["difficulties"]);
91  std::vector<std::string> difficulty_opts = utils::split(campaign["difficulty_descriptions"], ';');
92 
93  if(difficulty_opts.size() != difficulty_list.size()) {
94  difficulty_opts = difficulty_list;
95  }
96 
97  for(std::size_t i = 0; i < difficulty_opts.size(); i++)
98  {
99  config temp;
100  gui2::tlegacy_menu_item parsed(difficulty_opts[i]);
101 
102  temp["define"] = difficulty_list[i];
103  temp["image"] = parsed.icon();
104  temp["label"] = parsed.label();
105  temp["description"] = parsed.description();
106  temp["default"] = parsed.is_default();
107  temp["old_markup"] = true; // To prevent double parentheses in the dialog
108 
109  difficulties_.add_child("difficulty", temp);
110  }
111  }
112 }
113 
115 {
116  tlistbox& list = find_widget<tlistbox>(&window, "listbox", false);
117  window.keyboard_capture(&list);
118 
119  std::map<std::string, string_map> data;
120 
121  for (const config &d : difficulties_.child_range("difficulty"))
122  {
123  data["icon"]["label"] = d["image"];
124  data["label"]["label"] = d["label"];
125  data["label"]["use_markup"] = "true";
126  data["description"]["label"] = d["old_markup"].to_bool() || d["description"].empty() ? d["description"]
127  : std::string("(") + d["description"] + std::string(")");
128  data["description"]["use_markup"] = "true";
129 
130  list.add_row(data);
131 
132  const int this_row = list.get_item_count() - 1;
133 
134  if(d["default"].to_bool(false)) {
135  list.select_row(this_row);
136  }
137 
138  tgrid* grid = list.get_row_grid(this_row);
139  assert(grid);
140 
141  twidget *widget = grid->find("victory", false);
142  if (widget && !preferences::is_campaign_completed(campaign_id_, d["define"])) {
144  }
145  }
146 }
147 
149 {
150  if(get_retval() != twindow::OK) {
151  selected_difficulty_ = "CANCEL";
152  return;
153  }
154 
155  tlistbox& list = find_widget<tlistbox>(&window, "listbox", false);
156  selected_difficulty_ = difficulties_.child("difficulty", list.get_selected_row())["define"].str();
157 }
158 }
child_itors child_range(const std::string &key)
Definition: config.cpp:613
int get_retval() const
Definition: dialog.hpp:161
Base container class.
Definition: grid.hpp:29
bool is_campaign_completed(const std::string &campaign_id)
This file contains the window object, this object is a top level container which has the event manage...
bool select_row(const unsigned row, const bool select=true)
Selectes a row.
Definition: listbox.cpp:228
REGISTER_DIALOG(label_settings)
#define d
Implements simple parsing of legacy GUI1 item markup.
Definition: old_markup.hpp:26
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
Definitions for the interface to Wesnoth Markup Language (WML).
const std::string & icon() const
Definition: old_markup.hpp:45
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
#define WRN_WML
Dialog is closed with ok button.
Definition: window.hpp:125
bool is_default() const
Definition: old_markup.hpp:60
This file contains the settings handling of the widget library.
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:138
void 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:74
config & add_child(const std::string &key)
Definition: config.cpp:743
The user sets the widget hidden, that means:
Definition: widget.hpp:91
void pre_show(twindow &window)
Inherited from tdialog.
const std::string & description() const
Definition: old_markup.hpp:55
The listbox class.
Definition: listbox.hpp:39
size_t i
Definition: function.cpp:1057
const std::string & label() const
Definition: old_markup.hpp:50
static lg::log_domain log_wml("wml")
config & child(const std::string &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:658
Base class for all widgets.
Definition: widget.hpp:49
Standard logging facilities (interface).
twidget * find(const std::string &id, const bool must_be_active) override
See twidget::find.
Definition: grid.cpp:612
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:237
void set_visible(const tvisible::scoped_enum visible)
Definition: widget.cpp:445
void post_show(twindow &window)
Inherited from tdialog.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
const tgrid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:215