The Battle for Wesnoth  1.19.7+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 "serialization/markup.hpp"
23 #include "game_version.hpp"
24 #include "gui/widgets/listbox.hpp"
25 #include "gui/widgets/window.hpp"
26 #include "log.hpp"
28 
29 static lg::log_domain log_wml("wml");
30 #define WRN_WML LOG_STREAM(warn, log_wml)
31 
32 namespace gui2::dialogs
33 {
34 
35 REGISTER_DIALOG(campaign_difficulty)
36 
38 {
39  config result;
40 
41  // Populate local config with difficulty children
42  result.append_children(source, "difficulty");
43 
44  // Issue deprecation warnings about the old difficulties syntax
45  if(result.empty() && source.has_attribute("difficulties")) {
46  deprecated_message("[campaign]difficulties", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
47  if(source.has_attribute("difficulty_descriptions")) {
48  deprecated_message("[campaign]difficulty_descriptions", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
49  }
50  }
51 
52  return result;
53 }
54 
56  : modal_dialog(window_id())
57  , difficulties_(generate_difficulty_config(campaign))
58  , campaign_id_(campaign["id"])
59  , selected_difficulty_("CANCEL")
60 {
61 }
62 
64 {
65  listbox& list = find_widget<listbox>("listbox");
66  keyboard_capture(&list);
67 
68  unsigned difficulty_count = 0;
69  const unsigned difficulty_max = difficulties_.child_count("difficulty");
70  for(const config& d : difficulties_.child_range("difficulty")) {
72  widget_item item;
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" << markup::tag("small", markup::span_color(font::GRAY_COLOR, "(", d["description"], ")"));
87  } else {
88  ss << "\n" << markup::tag("small", markup::span_color(font::GRAY_COLOR, d["description"]));
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 
102  if(prefs::get().is_campaign_completed(campaign_id_, d["define"])) {
103  // Use different laurels according to the difficulty level, following the
104  // pre-existing convention established in campaign_selection class.
105  // Assumes ascending order of difficulty and gold laurel is set first
106  // in case there is only one difficulty setting.
107  if(difficulty_count + 1 >= difficulty_max) {
109  } else if(difficulty_count == 0) {
111  } else {
113  }
114  } else {
116  }
117 
118  difficulty_count++;
119  }
120 }
121 
123 {
124  if(get_retval() == retval::OK) {
125  listbox& list = find_widget<listbox>("listbox");
126  selected_difficulty_ = difficulties_.mandatory_child("difficulty", list.get_selected_row())["define"].str();
127  }
128 }
129 } // 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:172
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:366
std::size_t child_count(config_key_type key) const
Definition: config.cpp:296
child_itors child_range(config_key_type key)
Definition: config.cpp:272
void append_children(const config &cfg)
Adds children from cfg.
Definition: config.cpp:167
bool empty() const
Definition: config.cpp:849
virtual void post_show() override
Actions to be taken after the window has been shown.
campaign_difficulty(const config &campaign)
virtual void pre_show() 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:41
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list's last row.
Definition: listbox.hpp:186
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:92
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:305
Base class for all widgets.
Definition: widget.hpp:55
void set_visible(const visibility visible)
Definition: widget.cpp:479
@ hidden
The user sets the widget hidden, that means:
T * find_widget(const std::string_view id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition: widget.hpp:753
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
static prefs & get()
bool is_campaign_completed(const std::string &campaign_id)
Definitions for the interface to Wesnoth Markup Language (WML).
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 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:36
std::map< std::string, t_string > widget_item
Definition: widget.hpp:33
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::string tag(const std::string &tag_name, Args &&... contents)
Definition: markup.hpp:45
std::string span_color(const color_t &color, Args &&... data)
Definition: markup.hpp:68
std::string_view data
Definition: picture.cpp:178
#define d