The Battle for Wesnoth  1.19.0+dev
campaign_difficulty.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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 "deprecation.hpp"
22 #include "font/text_formatting.hpp"
23 #include "game_version.hpp"
25 #include "gui/widgets/listbox.hpp"
26 #include "gui/widgets/window.hpp"
27 #include "log.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  : modal_dialog(window_id())
58  , difficulties_(generate_difficulty_config(campaign))
59  , campaign_id_(campaign["id"])
60  , selected_difficulty_("CANCEL")
61 {
62 }
63 
65 {
66  listbox& list = find_widget<listbox>(&window, "listbox", false);
67  window.keyboard_capture(&list);
68 
69  unsigned difficulty_count = 0;
70  const unsigned difficulty_max = difficulties_.child_count("difficulty");
71  for(const config& d : difficulties_.child_range("difficulty")) {
74 
75  item["label"] = d["image"];
76  data.emplace("icon", item);
77 
78  item["use_markup"] = "true";
79 
80  std::ostringstream ss;
81  ss << d["label"];
82 
83  if(!d["description"].empty()) {
84  if (d["auto_markup"].to_bool(true) == false) {
85  ss << "\n" << d["description"].str();
86  } else if (!d["old_markup"].to_bool()) {
87  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << "(" << d["description"].str() << ")</span></small>";
88  } else {
89  ss << "\n<small>" << font::span_color(font::GRAY_COLOR) << d["description"] << "</span></small>";
90  }
91  }
92 
93  item["label"] = ss.str();
94  data.emplace("label", item);
95 
96  grid& grid = list.add_row(data);
97 
98  if(d["default"].to_bool(false)) {
99  list.select_last_row();
100  }
101 
102  styled_widget& widget = find_widget<styled_widget>(&grid, "victory", false);
103  if(prefs::get().is_campaign_completed(campaign_id_, d["define"])) {
104  // Use different laurels according to the difficulty level, following the
105  // pre-existing convention established in campaign_selection class.
106  // Assumes ascending order of difficulty and gold laurel is set first
107  // in case there is only one difficulty setting.
108  if(difficulty_count + 1 >= difficulty_max) {
110  } else if(difficulty_count == 0) {
112  } else {
114  }
115  } else {
117  }
118 
119  difficulty_count++;
120  }
121 }
122 
124 {
125  if(get_retval() == retval::OK) {
126  listbox& list = find_widget<listbox>(&window, "listbox", false);
127  selected_difficulty_ = difficulties_.mandatory_child("difficulty", list.get_selected_row())["define"].str();
128  }
129 }
130 } // namespace dialogs
static lg::log_domain log_wml("wml")
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
Definition: config.cpp:367
std::size_t child_count(config_key_type key) const
Definition: config.cpp:297
child_itors child_range(config_key_type key)
Definition: config.cpp:273
void append_children(const config &cfg)
Adds children from cfg.
Definition: config.cpp:165
bool empty() const
Definition: config.cpp:852
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
campaign_difficulty(const config &campaign)
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
Base container class.
Definition: grid.hpp:32
The listbox class.
Definition: listbox.hpp:43
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list's last row.
Definition: listbox.hpp:189
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:59
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:268
Base class for all widgets.
Definition: widget.hpp:53
void set_visible(const visibility visible)
Definition: widget.cpp:469
@ hidden
The user sets the widget hidden, that means:
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
void keyboard_capture(widget *widget)
Definition: window.cpp:1221
static prefs & get()
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:29
Interfaces for manipulating version numbers of engine, add-ons, etc.
This file contains the window object, this object is a top level container which has the event manage...
Standard logging facilities (interface).
const color_t GRAY_COLOR
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
std::string victory_laurel_hardest
std::string victory_laurel
std::string victory_laurel_easy
config generate_difficulty_config(const config &source)
Helper function to convert old difficulty markup.
REGISTER_DIALOG(editor_edit_unit)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
std::string_view data
Definition: picture.cpp:194
#define d