The Battle for Wesnoth  1.17.21+dev
campaign_difficulty.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2023
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 "formatter.hpp"
24 #include "game_version.hpp"
26 #include "gui/widgets/listbox.hpp"
27 #include "gui/widgets/window.hpp"
28 #include "log.hpp"
29 #include "preferences/game.hpp"
31 
32 static lg::log_domain log_wml("wml");
33 #define WRN_WML LOG_STREAM(warn, log_wml)
34 
35 namespace gui2::dialogs
36 {
37 
38 REGISTER_DIALOG(campaign_difficulty)
39 
41 {
42  config result;
43 
44  // Populate local config with difficulty children
45  result.append_children(source, "difficulty");
46 
47  // Issue deprecation warnings about the old difficulties syntax
48  if(result.empty() && source.has_attribute("difficulties")) {
49  deprecated_message("[campaign]difficulties", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
50  if(source.has_attribute("difficulty_descriptions")) {
51  deprecated_message("[campaign]difficulty_descriptions", DEP_LEVEL::REMOVED, {1, 15, 0}, "Use [difficulty] instead.");
52  }
53  }
54 
55  return result;
56 }
57 
59  : modal_dialog(window_id())
60  , difficulties_(generate_difficulty_config(campaign))
61  , campaign_id_(campaign["id"])
62  , selected_difficulty_("CANCEL")
63 {
64 }
65 
67 {
68  listbox& list = find_widget<listbox>(&window, "listbox", false);
69  window.keyboard_capture(&list);
70 
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  widget* widget = grid.find("victory", false);
105  }
106  }
107 }
108 
110 {
111  if(get_retval() == retval::OK) {
112  listbox& list = find_widget<listbox>(&window, "listbox", false);
113  selected_difficulty_ = difficulties_.mandatory_child("difficulty", list.get_selected_row())["define"].str();
114  }
115 }
116 } // 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:161
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:371
child_itors child_range(config_key_type key)
Definition: config.cpp:277
void append_children(const config &cfg)
Adds children from cfg.
Definition: config.cpp:169
bool empty() const
Definition: config.cpp:856
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
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
The listbox class.
Definition: listbox.hpp:46
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list's last row.
Definition: listbox.hpp:192
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:62
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:271
Base class for all widgets.
Definition: widget.hpp:54
void set_visible(const visibility visible)
Definition: widget.cpp:456
@ 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:67
void keyboard_capture(widget *widget)
Definition: window.cpp:1130
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:30
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).
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
const color_t GRAY_COLOR
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
config generate_difficulty_config(const config &source)
Helper function to convert old difficulty markup.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
std::map< std::string, t_string > widget_item
Definition: widget.hpp:32
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:414
bool is_campaign_completed(const std::string &campaign_id)
Definition: game.cpp:293
std::string_view data
Definition: picture.cpp:199
#define d