The Battle for Wesnoth  1.15.12+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::dialogs
34 {
35 
36 REGISTER_DIALOG(campaign_difficulty)
37 
39 {
40  config result;
41 
42  // Populate local config with difficulty children
43  result.append_children(source, "difficulty");
44 
45  // Issue deprecation warnings about the old difficulties syntax
46  if(result.empty() && source.has_attribute("difficulties")) {
47  deprecated_message("[campaign]difficulties", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
48  if(source.has_attribute("difficulty_descriptions")) {
49  deprecated_message("[campaign]difficulty_descriptions", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
50  }
51  }
52 
53  return result;
54 }
55 
57  : difficulties_(generate_difficulty_config(campaign))
58  , campaign_id_(campaign["id"])
59  , selected_difficulty_("CANCEL")
60 {
61  set_restore(true);
62 }
63 
65 {
66  listbox& list = find_widget<listbox>(&window, "listbox", false);
67  window.keyboard_capture(&list);
68 
69  for(const config& d : difficulties_.child_range("difficulty")) {
70  std::map<std::string, string_map> data;
72 
73  item["label"] = d["image"];
74  data.emplace("icon", item);
75 
76  item["use_markup"] = "true";
77 
78  std::ostringstream ss;
79  ss << d["label"];
80 
81  if(!d["description"].empty()) {
82  if (d["auto_markup"].to_bool(true) == false) {
83  ss << "\n" << d["description"].str();
84  } else if (!d["old_markup"].to_bool()) {
85  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << "(" << d["description"].str() << ")</span></small>";
86  } else {
87  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << d["description"] << "</span></small>";
88  }
89  }
90 
91  item["label"] = ss.str();
92  data.emplace("label", item);
93 
94  grid& grid = list.add_row(data);
95 
96  if(d["default"].to_bool(false)) {
97  list.select_last_row();
98  }
99 
100  widget* widget = grid.find("victory", false);
101  if(widget && !preferences::is_campaign_completed(campaign_id_, d["define"])) {
103  }
104  }
105 }
106 
108 {
109  if(get_retval() == retval::OK) {
110  listbox& list = find_widget<listbox>(&window, "listbox", false);
111  selected_difficulty_ = difficulties_.child("difficulty", list.get_selected_row())["define"].str();
112  }
113 }
114 } // 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:414
const color_t GRAY_COLOR
bool is_campaign_completed(const std::string &campaign_id)
Definition: game.cpp:301
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:356
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:194
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:276
#define d
void append_children(const config &cfg)
Adds children from cfg.
Definition: config.cpp:235
Definitions for the interface to Wesnoth Markup Language (WML).
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::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:475
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:655
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:67
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:34
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
campaign_difficulty(const config &campaign)
bool empty() const
Definition: config.cpp:916
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:409