The Battle for Wesnoth  1.19.0-dev
unit_recruit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
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 
17 #include "font/text_formatting.hpp"
20 #include "gui/widgets/button.hpp"
21 #include "gui/widgets/image.hpp"
22 #include "gui/widgets/listbox.hpp"
24 #include "gui/widgets/text_box.hpp"
25 #include "gui/widgets/window.hpp"
26 #include "gettext.hpp"
27 #include "help/help.hpp"
28 #include "team.hpp"
29 #include "units/types.hpp"
30 
31 #include <functional>
32 
33 namespace gui2::dialogs
34 {
35 
36 REGISTER_DIALOG(unit_recruit)
37 
38 unit_recruit::unit_recruit(std::map<const unit_type*, t_string>& recruit_map, team& team)
39  : modal_dialog(window_id())
40  , recruit_list_()
41  , recruit_map_(recruit_map)
42  , team_(team)
43  , selected_index_(0)
44 {
45  for(const auto& pair : recruit_map) {
46  recruit_list_.push_back(pair.first);
47  }
48  // Ensure the recruit list is sorted by name
49  std::sort(recruit_list_.begin(), recruit_list_.end(), [](const unit_type* t1, const unit_type* t2) {
50  return t1->type_name().str() < t2->type_name().str();
51  });
52 
53 }
54 
55 static const color_t inactive_row_color(0x96, 0x96, 0x96);
56 
57 static inline std::string gray_if_unrecruitable(const std::string& text, const bool is_recruitable)
58 {
59  return is_recruitable ? text : font::span_color(inactive_row_color, text);
60 }
61 
62 // Compare unit_create::filter_text_change
63 void unit_recruit::filter_text_changed(const std::string& text)
64 {
65  listbox& list = find_widget<listbox>(get_window(), "recruit_list", false);
66 
67  const std::vector<std::string> words = utils::split(text, ' ');
68 
69  if(words == last_words_)
70  return;
71  last_words_ = words;
72 
73  boost::dynamic_bitset<> show_items;
74  show_items.resize(list.get_item_count(), true);
75 
76  if(!text.empty()) {
77  for(unsigned int i = 0; i < list.get_item_count(); i++) {
78  assert(i < recruit_list_.size());
79  const unit_type* type = recruit_list_[i];
80  if(!type) continue;
81 
82  auto default_gender = !type->genders().empty()
83  ? type->genders().front() : unit_race::MALE;
84  const auto* race = type->race();
85 
86  // List of possible match criteria for this unit type. Empty values will
87  // never match.
88  auto criteria = std::make_tuple(
89  (game_config::debug ? type->id() : ""),
90  type->type_name(),
91  std::to_string(type->level()),
92  unit_type::alignment_description(type->alignment(), default_gender),
93  (race ? race->name(default_gender) : ""),
94  (race ? race->plural_name() : "")
95  );
96 
97  bool found = false;
98  for(const auto & word : words)
99  {
100  // Search for the name in the local language.
101  // In debug mode, also search for the type id.
102  std::apply([&](auto&&... criterion) {
103  found = (translation::ci_search(criterion, word) || ...);
104  }, criteria);
105 
106  if(!found) {
107  // one word doesn't match, we don't reach words.end()
108  break;
109  }
110  }
111 
112  show_items[i] = found;
113  }
114  }
115 
116  list.set_row_shown(show_items);
117 }
118 
120 {
121  text_box* filter = find_widget<text_box>(&window, "filter_box", false, true);
123  std::bind(&unit_recruit::filter_text_changed, this, std::placeholders::_2));
124 
125  listbox& list = find_widget<listbox>(&window, "recruit_list", false);
126 
128 
129  window.keyboard_capture(filter);
131 
133  find_widget<button>(&window, "show_help", false),
134  std::bind(&unit_recruit::show_help, this));
135 
136  for(const auto& recruit : recruit_list_)
137  {
138  const t_string& error = recruit_map_[recruit];
139  widget_data row_data;
140  widget_item column;
141 
142  std::string image_string = recruit->image() + "~RC(" + recruit->flag_rgb() + ">"
143  + team_.color() + ")";
144 
145  const bool is_recruitable = error.empty();
146 
147  const std::string cost_string = std::to_string(recruit->cost());
148 
149  column["use_markup"] = "true";
150  if(!error.empty()) {
151  // Just set the tooltip on every single element in this row.
152  column["tooltip"] = error;
153  }
154 
155  column["label"] = image_string + (is_recruitable ? "" : "~GS()");
156  row_data.emplace("unit_image", column);
157 
158  column["label"] = gray_if_unrecruitable(recruit->type_name(), is_recruitable);
159  row_data.emplace("unit_type", column);
160 
161  column["label"] = gray_if_unrecruitable(cost_string, is_recruitable);
162  row_data.emplace("unit_cost", column);
163 
164  grid& grid = list.add_row(row_data);
165  if(!is_recruitable) {
166  image *gold_icon = dynamic_cast<image*>(grid.find("gold_icon", false));
167  assert(gold_icon);
168  gold_icon->set_image(gold_icon->get_image() + "~GS()");
169  }
170  }
171 
173 }
174 
176 {
177  const int selected_row
178  = find_widget<listbox>(get_window(), "recruit_list", false).get_selected_row();
179 
180  if(selected_row == -1) {
181  return;
182  }
183 
184  find_widget<unit_preview_pane>(get_window(), "recruit_details", false)
185  .set_displayed_type(*recruit_list_[selected_row]);
186 }
187 
189 {
190  help::show_help("recruit_and_recall");
191 }
192 
194 {
195  if(get_retval() == retval::OK) {
196  selected_index_ = find_widget<listbox>(&window, "recruit_list", false)
197  .get_selected_row();
198  }
199 }
200 
201 } // namespace dialogs
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.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
std::map< const unit_type *, t_string > & recruit_map_
void filter_text_changed(const std::string &text)
std::vector< std::string > last_words_
std::vector< const unit_type * > recruit_list_
A vector of unit types in the order listed in the UI.
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
t_string get_image() const
Wrapper for label.
Definition: image.hpp:66
void set_image(const t_string &label)
Wrapper for set_label.
Definition: image.hpp:53
The listbox class.
Definition: listbox.hpp:43
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:136
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:59
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:124
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
Class for a single line text area.
Definition: text_box.hpp:142
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void keyboard_capture(widget *widget)
Definition: window.cpp:1215
void add_to_keyboard_chain(widget *widget)
Adds the widget to the keyboard chain.
Definition: window.cpp:1221
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
bool empty() const
Definition: tstring.hpp:186
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
const std::string & color() const
Definition: team.hpp:242
@ MALE
Definition: race.hpp:27
A single unit type that the player may recruit.
Definition: types.hpp:43
static std::string alignment_description(unit_alignments::type align, unit_race::GENDER gender=unit_race::MALE)
Implementation detail of unit_type::alignment_description.
Definition: types.cpp:838
std::size_t i
Definition: function.cpp:968
This file contains the window object, this object is a top level container which has the event manage...
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
const bool & debug
Definition: game_config.cpp:91
static std::string gray_if_unrecruitable(const std::string &text, const bool is_recruitable)
static const color_t inactive_row_color(0x96, 0x96, 0x96)
REGISTER_DIALOG(tod_new_schedule)
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
@ 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:140
Functions to load and save images from/to disk.
bool ci_search(const std::string &s1, const std::string &s2)
Definition: gettext.cpp:565
std::vector< std::string > split(const config_attribute_value &val)
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59