The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
unit_advance.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 - 2025
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
23#include "units/unit.hpp"
24#include "units/types.hpp"
25#include "help/help.hpp"
26
27#include <functional>
28
29namespace gui2::dialogs
30{
31
32REGISTER_DIALOG(unit_advance)
33
34unit_advance::unit_advance(const std::vector<unit_const_ptr>& samples, std::size_t real)
35 : modal_dialog(window_id())
36 , previews_(samples)
37 , selected_index_(0)
38 , last_real_advancement_(real)
39{
40}
41
43{
44 listbox& list = find_widget<listbox>("advance_choice");
45
46 connect_signal_notify_modified(list, std::bind(&unit_advance::list_item_clicked, this));
47
48 keyboard_capture(&list);
49
50 connect_signal_mouse_left_click(
51 find_widget<button>("show_help"),
52 std::bind(&unit_advance::show_help, this));
53
54 for(std::size_t i = 0; i < previews_.size(); i++) {
55 const unit& sample = *previews_[i];
56
57 widget_data row_data;
58 widget_item column;
59
60 std::string image_string, name = sample.type_name();
61
62 // This checks if we've finished iterating over the last unit type advancements
63 // and are into the modification-based advancements.
65 const auto range = sample.get_modifications().child_range("advancement");
66 const auto& back = range.back();
67
68 if(back.has_attribute("image")) {
69 image_string = back["image"].str();
70 }
71
72 name = back["description"].str();
73 }
74
75 if(image_string.empty()) {
76 image_string = sample.type().image() + sample.image_mods();
77 }
78
79 column["label"] = image_string;
80 row_data.emplace("advancement_image", column);
81
82 column["label"] = name;
83 row_data.emplace("advancement_name", column);
84
85 list.add_row(row_data);
86 }
87
89
90 // Disable ESC existing
92}
93
95{
96 const int selected_row
97 = find_widget<listbox>("advance_choice").get_selected_row();
98
99 if(selected_row == -1) {
100 return;
101 }
102
103 find_widget<unit_preview_pane>("advancement_details")
104 .set_display_data(*previews_[selected_row]);
105}
106
108{
109 help::show_help("advancement");
110}
111
113{
114 if(get_retval() == retval::OK) {
115 selected_index_ = find_widget<listbox>("advance_choice")
116 .get_selected_row();
117 }
118}
119
120} // namespace dialogs
child_itors child_range(std::string_view key)
Definition config.cpp:268
Abstract base class for all modal dialogs.
const std::vector< unit_const_ptr > & previews_
virtual void pre_show() override
Actions to be taken before showing the window.
virtual void post_show() override
Actions to be taken after the window has been shown.
The listbox class.
Definition listbox.hpp:41
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
void keyboard_capture(widget *widget)
Definition window.cpp:1197
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition window.hpp:326
int get_retval()
Definition window.hpp:394
const std::string & image() const
Definition types.hpp:180
This class represents a single unit of a specific type.
Definition unit.hpp:39
const unit_type & type() const
This unit's type, accounting for gender and variation.
Definition unit.hpp:261
const t_string & type_name() const
Gets the translatable name of this unit's type.
Definition unit.hpp:275
std::string image_mods() const
Gets an IPF string containing all IPF image mods.
Definition unit.cpp:2792
config & get_modifications()
Get the raw modifications.
Definition unit.hpp:1449
This file contains the window object, this object is a top level container which has the event manage...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
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
void show_help(const std::string &show_topic)
Open the help browser.
Definition help.cpp:84
STL namespace.
std::shared_ptr< const unit > unit_const_ptr
Definition ptr.hpp:27
#define i