The Battle for Wesnoth  1.15.0+dev
unit_advance.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #define GETTEXT_DOMAIN "wesnoth-lib"
15 
17 
19 #include "gui/widgets/button.hpp"
20 #include "gui/widgets/image.hpp"
21 #include "gui/widgets/label.hpp"
22 #include "gui/widgets/listbox.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "gui/widgets/window.hpp"
26 #include "units/unit.hpp"
27 #include "help/help.hpp"
28 
29 #include "utils/functional.hpp"
30 
31 namespace gui2
32 {
33 namespace dialogs
34 {
35 
36 REGISTER_DIALOG(unit_advance)
37 
38 unit_advance::unit_advance(const unit_ptr_vector& samples, std::size_t real)
39  : previews_(samples)
40  , selected_index_(0)
41  , last_real_advancement_(real)
42 {
43 }
44 
46 {
47  listbox& list = find_widget<listbox>(&window, "advance_choice", false);
48 
49  connect_signal_notify_modified(list, std::bind(&unit_advance::list_item_clicked, this, std::ref(window)));
50 
51  window.keyboard_capture(&list);
52 
54  find_widget<button>(&window, "show_help", false),
55  std::bind(&unit_advance::show_help, this));
56 
57  for(std::size_t i = 0; i < previews_.size(); i++) {
58  const unit& sample = *previews_[i];
59 
60  std::map<std::string, string_map> row_data;
61  string_map column;
62 
63  std::string image_string, name = sample.type_name();
64 
65  // This checks if we've finished iterating over the last unit type advancements
66  // and are into the modification-based advancements.
67  if(i >= last_real_advancement_) {
68  const auto& back = sample.get_modifications().child_range("advancement").back();
69 
70  if(back.has_attribute("image")) {
71  image_string = back["image"].str();
72  }
73 
74  name = back["description"].str();
75  }
76 
77  if(image_string.empty()) {
78  image_string = sample.type().image() + sample.image_mods();
79  }
80 
81  column["label"] = image_string;
82  row_data.emplace("advancement_image", column);
83 
84  column["label"] = name;
85  row_data.emplace("advancement_name", column);
86 
87  list.add_row(row_data);
88  }
89 
90  list_item_clicked(window);
91 
92  // Disable ESC existing
93  window.set_escape_disabled(true);
94 }
95 
97 {
98  const int selected_row
99  = find_widget<listbox>(&window, "advance_choice", false).get_selected_row();
100 
101  if(selected_row == -1) {
102  return;
103  }
104 
105  find_widget<unit_preview_pane>(&window, "advancement_details", false)
106  .set_displayed_unit(*previews_[selected_row]);
107 }
108 
110 {
111  help::show_help("advancement");
112 }
113 
115 {
116  if(get_retval() == retval::OK) {
117  selected_index_ = find_widget<listbox>(&window, "advance_choice", false)
118  .get_selected_row();
119  }
120 }
121 
122 } // namespace dialogs
123 } // namespace gui2
std::vector< unit_const_ptr > unit_ptr_vector
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:116
This class represents a single unit of a specific type.
Definition: unit.hpp:99
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
This file contains the window object, this object is a top level container which has the event manage...
child_itors child_range(config_key_type key)
Definition: config.cpp:362
STL namespace.
Generic file dialog.
Definition: field-fwd.hpp:22
const unit_type & type() const
This unit&#39;s type, accounting for gender and variation.
Definition: unit.hpp:315
The listbox class.
Definition: listbox.hpp:40
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:248
This file contains the settings handling of the widget library.
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:233
const unit_ptr_vector & previews_
const t_string & type_name() const
Gets the translatable name of this unit&#39;s type.
Definition: unit.hpp:332
Various uncategorised dialogs.
std::size_t i
Definition: function.cpp:933
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
grid & add_row(const string_map &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:66
void list_item_clicked(window &window)
const std::string & image() const
Definition: types.hpp:161
Dialog was closed with the OK button.
Definition: retval.hpp:34
config & get_modifications()
Get the raw modifications.
Definition: unit.hpp:1380
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
std::string image_mods() const
Gets an IPF string containing all IPF image mods.
Definition: unit.cpp:2533
virtual void pre_show(window &window) override
Inherited from modal_dialog.