The Battle for Wesnoth  1.17.23+dev
unit_advance.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2023
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 
20 #include "gui/widgets/button.hpp"
21 #include "gui/widgets/image.hpp"
22 #include "gui/widgets/label.hpp"
23 #include "gui/widgets/listbox.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "gui/widgets/window.hpp"
27 #include "units/unit.hpp"
28 #include "units/types.hpp"
29 #include "help/help.hpp"
30 
31 #include <functional>
32 
33 namespace gui2::dialogs
34 {
35 
36 REGISTER_DIALOG(unit_advance)
37 
38 unit_advance::unit_advance(const std::vector<unit_const_ptr>& samples, std::size_t real)
39  : modal_dialog(window_id())
40  , previews_(samples)
41  , selected_index_(0)
42  , last_real_advancement_(real)
43 {
44 }
45 
47 {
48  listbox& list = find_widget<listbox>(&window, "advance_choice", false);
49 
51 
52  window.keyboard_capture(&list);
53 
55  find_widget<button>(&window, "show_help", false),
56  std::bind(&unit_advance::show_help, this));
57 
58  for(std::size_t i = 0; i < previews_.size(); i++) {
59  const unit& sample = *previews_[i];
60 
61  widget_data row_data;
62  widget_item column;
63 
64  std::string image_string, name = sample.type_name();
65 
66  // This checks if we've finished iterating over the last unit type advancements
67  // and are into the modification-based advancements.
68  if(i >= last_real_advancement_) {
69  const auto& back = sample.get_modifications().child_range("advancement").back();
70 
71  if(back.has_attribute("image")) {
72  image_string = back["image"].str();
73  }
74 
75  name = back["description"].str();
76  }
77 
78  if(image_string.empty()) {
79  image_string = sample.type().image() + sample.image_mods();
80  }
81 
82  column["label"] = image_string;
83  row_data.emplace("advancement_image", column);
84 
85  column["label"] = name;
86  row_data.emplace("advancement_name", column);
87 
88  list.add_row(row_data);
89  }
90 
92 
93  // Disable ESC existing
95 }
96 
98 {
99  const int selected_row
100  = find_widget<listbox>(get_window(), "advance_choice", false).get_selected_row();
101 
102  if(selected_row == -1) {
103  return;
104  }
105 
106  find_widget<unit_preview_pane>(get_window(), "advancement_details", false)
107  .set_displayed_unit(*previews_[selected_row]);
108 }
109 
111 {
112  help::show_help("advancement");
113 }
114 
116 {
117  if(get_retval() == retval::OK) {
118  selected_index_ = find_widget<listbox>(&window, "advance_choice", false)
119  .get_selected_row();
120  }
121 }
122 
123 } // namespace dialogs
child_itors child_range(config_key_type key)
Definition: config.cpp:277
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
window * get_window()
Returns a pointer to the dialog's window.
const std::vector< unit_const_ptr > & previews_
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
The listbox class.
Definition: listbox.hpp:46
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
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:1224
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:344
const std::string & image() const
Definition: types.hpp:179
This class represents a single unit of a specific type.
Definition: unit.hpp:135
std::size_t i
Definition: function.cpp:968
const t_string & type_name() const
Gets the translatable name of this unit's type.
Definition: unit.hpp:371
const unit_type & type() const
This unit's type, accounting for gender and variation.
Definition: unit.hpp:357
std::string image_mods() const
Gets an IPF string containing all IPF image mods.
Definition: unit.cpp:2756
config & get_modifications()
Get the raw modifications.
Definition: unit.hpp:1481
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.
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:205
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:179
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
void show_help(const std::string &show_topic, int xloc, int yloc)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:144
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
This file contains the settings handling of the widget library.