The Battle for Wesnoth  1.19.5+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 "serialization/markup.hpp"
19 #include "gui/widgets/button.hpp"
20 #include "gui/widgets/image.hpp"
21 #include "gui/widgets/listbox.hpp"
23 #include "gui/widgets/text_box.hpp"
24 #include "gui/widgets/window.hpp"
25 #include "gettext.hpp"
26 #include "help/help.hpp"
27 #include "team.hpp"
28 #include "units/types.hpp"
29 
30 #include <functional>
31 
32 namespace gui2::dialogs
33 {
34 
35 REGISTER_DIALOG(unit_recruit)
36 
37 unit_recruit::unit_recruit(std::map<const unit_type*, t_string>& recruit_map, team& team)
38  : modal_dialog(window_id())
39  , recruit_list_()
40  , recruit_map_(recruit_map)
41  , team_(team)
42  , selected_index_(0)
43 {
44  for(const auto& pair : recruit_map) {
45  recruit_list_.push_back(pair.first);
46  }
47  // Ensure the recruit list is sorted by name
48  std::sort(recruit_list_.begin(), recruit_list_.end(), [](const unit_type* t1, const unit_type* t2) {
49  return t1->type_name().str() < t2->type_name().str();
50  });
51 
52 }
53 
54 static const color_t inactive_row_color(0x96, 0x96, 0x96);
55 
56 static inline std::string gray_if_unrecruitable(const std::string& text, const bool is_recruitable)
57 {
58  return is_recruitable ? text : markup::span_color(inactive_row_color, text);
59 }
60 
61 // Compare unit_create::filter_text_change
62 void unit_recruit::filter_text_changed(const std::string& text)
63 {
64  listbox& list = find_widget<listbox>("recruit_list");
65 
66  const std::vector<std::string> words = utils::split(text, ' ');
67 
68  if(words == last_words_)
69  return;
70  last_words_ = words;
71 
72  boost::dynamic_bitset<> show_items;
73  show_items.resize(list.get_item_count(), true);
74 
75  if(!text.empty()) {
76  for(unsigned int i = 0; i < list.get_item_count(); i++) {
77  assert(i < recruit_list_.size());
78  const unit_type* type = recruit_list_[i];
79  if(!type) continue;
80 
81  auto default_gender = !type->genders().empty()
82  ? type->genders().front() : unit_race::MALE;
83  const auto* race = type->race();
84 
85  // List of possible match criteria for this unit type. Empty values will
86  // never match.
87  auto criteria = std::make_tuple(
88  (game_config::debug ? type->id() : ""),
89  type->type_name(),
90  std::to_string(type->level()),
91  unit_type::alignment_description(type->alignment(), default_gender),
92  (race ? race->name(default_gender) : ""),
93  (race ? race->plural_name() : "")
94  );
95 
96  bool found = false;
97  for(const auto & word : words)
98  {
99  // Search for the name in the local language.
100  // In debug mode, also search for the type id.
101  std::apply([&](auto&&... criterion) {
102  found = (translation::ci_search(criterion, word) || ...);
103  }, criteria);
104 
105  if(!found) {
106  // one word doesn't match, we don't reach words.end()
107  break;
108  }
109  }
110 
111  show_items[i] = found;
112  }
113  }
114 
115  list.set_row_shown(show_items);
116 }
117 
119 {
120  text_box* filter = find_widget<text_box>("filter_box", false, true);
122  std::bind(&unit_recruit::filter_text_changed, this, std::placeholders::_2));
123 
124  listbox& list = find_widget<listbox>("recruit_list");
125 
127 
128  keyboard_capture(filter);
129  add_to_keyboard_chain(&list);
130 
132  find_widget<button>("show_help"),
133  std::bind(&unit_recruit::show_help, this));
134 
135  for(const auto& recruit : recruit_list_)
136  {
137  const t_string& error = recruit_map_[recruit];
138  widget_data row_data;
139  widget_item column;
140 
141  std::string image_string = recruit->image() + "~RC(" + recruit->flag_rgb() + ">"
142  + team_.color() + ")";
143 
144  const bool is_recruitable = error.empty();
145 
146  const std::string cost_string = std::to_string(recruit->cost());
147 
148  column["use_markup"] = "true";
149  if(!error.empty()) {
150  // Just set the tooltip on every single element in this row.
151  column["tooltip"] = error;
152  }
153 
154  column["label"] = image_string + (is_recruitable ? "" : "~GS()");
155  row_data.emplace("unit_image", column);
156 
157  column["label"] = gray_if_unrecruitable(recruit->type_name(), is_recruitable);
158  row_data.emplace("unit_type", column);
159 
160  column["label"] = gray_if_unrecruitable(cost_string, is_recruitable);
161  row_data.emplace("unit_cost", column);
162 
163  grid& grid = list.add_row(row_data);
164  if(!is_recruitable) {
165  image *gold_icon = dynamic_cast<image*>(grid.find("gold_icon", false));
166  assert(gold_icon);
167  gold_icon->set_image(gold_icon->get_image() + "~GS()");
168  }
169  }
170 
172 }
173 
175 {
176  const int selected_row
177  = find_widget<listbox>("recruit_list").get_selected_row();
178 
179  if(selected_row == -1) {
180  return;
181  }
182 
183  find_widget<unit_preview_pane>("recruit_details")
184  .set_displayed_type(*recruit_list_[selected_row]);
185 }
186 
188 {
189  help::show_help("recruit_and_recall");
190 }
191 
193 {
194  if(get_retval() == retval::OK) {
195  selected_index_ = find_widget<listbox>("recruit_list")
196  .get_selected_row();
197  }
198 }
199 
200 } // namespace dialogs
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
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.
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:58
void set_image(const t_string &label)
Wrapper for set_label.
Definition: image.hpp:45
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:135
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:58
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:123
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
void add_to_keyboard_chain(widget *widget)
Adds the widget to the keyboard chain.
Definition: window.cpp:1213
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
bool empty() const
Definition: tstring.hpp:194
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:75
const std::string & color() const
Definition: team.hpp:242
@ MALE
Definition: race.hpp:28
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:841
std::size_t i
Definition: function.cpp:1028
This file contains the window object, this object is a top level container which has the event manage...
const bool & debug
Definition: game_config.cpp:94
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(editor_edit_unit)
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: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, show topic with id show_topic.
Definition: help.cpp:140
Functions to load and save images from/to disk.
std::string span_color(const color_t &color, Args &&... data)
Definition: markup.hpp:68
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