The Battle for Wesnoth  1.17.0-dev
campaign_difficulty.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "config.hpp"
21 #include "font/text_formatting.hpp"
22 #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::dialogs
35 {
36 
37 REGISTER_DIALOG(campaign_difficulty)
38 
40 {
41  config result;
42 
43  // Populate local config with difficulty children
44  result.append_children(source, "difficulty");
45 
46  // Issue deprecation warnings about the old difficulties syntax
47  if(result.empty() && source.has_attribute("difficulties")) {
48  deprecated_message("[campaign]difficulties", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
49  if(source.has_attribute("difficulty_descriptions")) {
50  deprecated_message("[campaign]difficulty_descriptions", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
51  }
52  }
53 
54  return result;
55 }
56 
58  : difficulties_(generate_difficulty_config(campaign))
59  , campaign_id_(campaign["id"])
60  , selected_difficulty_("CANCEL")
61 {
62  set_restore(true);
63 }
64 
66 {
67  listbox& list = find_widget<listbox>(&window, "listbox", false);
68  window.keyboard_capture(&list);
69 
70  for(const config& d : difficulties_.child_range("difficulty")) {
71  std::map<std::string, string_map> data;
73 
74  item["label"] = d["image"];
75  data.emplace("icon", item);
76 
77  item["use_markup"] = "true";
78 
79  std::ostringstream ss;
80  ss << d["label"];
81 
82  if(!d["description"].empty()) {
83  if (d["auto_markup"].to_bool(true) == false) {
84  ss << "\n" << d["description"].str();
85  } else if (!d["old_markup"].to_bool()) {
86  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << "(" << d["description"].str() << ")</span></small>";
87  } else {
88  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << d["description"] << "</span></small>";
89  }
90  }
91 
92  item["label"] = ss.str();
93  data.emplace("label", item);
94 
95  grid& grid = list.add_row(data);
96 
97  if(d["default"].to_bool(false)) {
98  list.select_last_row();
99  }
100 
101  widget* widget = grid.find("victory", false);
102  if(widget && !preferences::is_campaign_completed(campaign_id_, d["define"])) {
104  }
105  }
106 }
107 
109 {
110  if(get_retval() == retval::OK) {
111  listbox& list = find_widget<listbox>(&window, "listbox", false);
112  selected_difficulty_ = difficulties_.child("difficulty", list.get_selected_row())["define"].str();
113  }
114 }
115 } // namespace dialogs
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:402
const color_t GRAY_COLOR
bool is_campaign_completed(const std::string &campaign_id)
Definition: game.cpp:307
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:344
Base class for all widgets.
Definition: widget.hpp:49
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list&#39;s last row.
Definition: listbox.hpp:195
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:277
#define d
void append_children(const config &cfg)
Adds children from cfg.
Definition: config.cpp:223
Definitions for the interface to Wesnoth Markup Language (WML).
The listbox class.
Definition: listbox.hpp:43
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:31
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:30
void set_visible(const visibility visible)
Definition: widget.cpp:476
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:656
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:26
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")
int get_retval() const
Returns the cached window exit code.
The user sets the widget hidden, that means:
Standard logging facilities (interface).
Dialog was closed with the OK button.
Definition: retval.hpp:35
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
campaign_difficulty(const config &campaign)
bool empty() const
Definition: config.cpp:941
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
void set_restore(const bool restore)
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410