The Battle for Wesnoth  1.17.10+dev
unit_recruit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2022
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/label.hpp"
23 #include "gui/widgets/listbox.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "gui/widgets/text_box.hpp"
28 #include "gui/widgets/window.hpp"
29 #include "gettext.hpp"
30 #include "help/help.hpp"
31 #include "play_controller.hpp"
32 #include "resources.hpp"
33 #include "team.hpp"
34 #include "units/types.hpp"
35 #include "whiteboard/manager.hpp"
36 
37 #include <functional>
38 
39 namespace gui2::dialogs
40 {
41 
42 REGISTER_DIALOG(unit_recruit)
43 
44 unit_recruit::unit_recruit(std::map<const unit_type*, t_string>& recruit_map, team& team)
45  : modal_dialog(window_id())
46  , recruit_list_()
47  , recruit_map_(recruit_map)
48  , team_(team)
49  , selected_index_(0)
50 {
51  for(const auto& pair : recruit_map) {
52  recruit_list_.push_back(pair.first);
53  }
54  // Ensure the recruit list is sorted by name
55  std::sort(recruit_list_.begin(), recruit_list_.end(), [](const unit_type* t1, const unit_type* t2) {
56  return t1->type_name().str() < t2->type_name().str();
57  });
58 
59 }
60 
61 static const color_t inactive_row_color(0x96, 0x96, 0x96);
62 
63 static inline std::string gray_if_unrecruitable(const std::string& text, const bool is_recruitable)
64 {
65  return is_recruitable ? text : font::span_color(inactive_row_color, text);
66 }
67 
68 // Compare unit_create::filter_text_change
69 void unit_recruit::filter_text_changed(const std::string& text)
70 {
71  listbox& list = find_widget<listbox>(get_window(), "recruit_list", false);
72 
73  const std::vector<std::string> words = utils::split(text, ' ');
74 
75  if(words == last_words_)
76  return;
77  last_words_ = words;
78 
79  boost::dynamic_bitset<> show_items;
80  show_items.resize(list.get_item_count(), true);
81 
82  if(!text.empty()) {
83  for(unsigned int i = 0; i < list.get_item_count(); i++) {
84  assert(i < recruit_list_.size());
85  const unit_type* type = recruit_list_[i];
86  if(!type) continue;
87 
88  bool found = false;
89  for(const auto & word : words)
90  {
91  // Search for the name in the local language.
92  // In debug mode, also search for the type id.
93  found = (game_config::debug && translation::ci_search(type->id(), word)) ||
94  translation::ci_search(type->type_name(), word);
95 
96  if(!found) {
97  // one word doesn't match, we don't reach words.end()
98  break;
99  }
100  }
101 
102  show_items[i] = found;
103  }
104  }
105 
106  list.set_row_shown(show_items);
107 }
108 
110 {
111  text_box* filter = find_widget<text_box>(&window, "filter_box", false, true);
113  std::bind(&unit_recruit::filter_text_changed, this, std::placeholders::_2));
114 
115  listbox& list = find_widget<listbox>(&window, "recruit_list", false);
116 
118 
119  window.keyboard_capture(filter);
120  window.add_to_keyboard_chain(&list);
121 
123  find_widget<button>(&window, "show_help", false),
124  std::bind(&unit_recruit::show_help, this));
125 
126  for(const auto& recruit : recruit_list_)
127  {
128  const t_string& error = recruit_map_[recruit];
129  widget_data row_data;
130  widget_item column;
131 
132  std::string image_string = recruit->image() + "~RC(" + recruit->flag_rgb() + ">"
133  + team_.color() + ")";
134 
135  const bool is_recruitable = error.empty();
136 
137  const std::string cost_string = std::to_string(recruit->cost());
138 
139  column["use_markup"] = "true";
140  if(!error.empty()) {
141  // Just set the tooltip on every single element in this row.
142  column["tooltip"] = error;
143  }
144 
145  column["label"] = image_string + (is_recruitable ? "" : "~GS()");
146  row_data.emplace("unit_image", column);
147 
148  column["label"] = gray_if_unrecruitable(recruit->type_name(), is_recruitable);
149  row_data.emplace("unit_type", column);
150 
151  column["label"] = gray_if_unrecruitable(cost_string, is_recruitable);
152  row_data.emplace("unit_cost", column);
153 
154  grid& grid = list.add_row(row_data);
155  if(!is_recruitable) {
156  image *gold_icon = dynamic_cast<image*>(grid.find("gold_icon", false));
157  assert(gold_icon);
158  gold_icon->set_image(gold_icon->get_image() + "~GS()");
159  }
160  }
161 
163 }
164 
166 {
167  const int selected_row
168  = find_widget<listbox>(get_window(), "recruit_list", false).get_selected_row();
169 
170  if(selected_row == -1) {
171  return;
172  }
173 
174  find_widget<unit_preview_pane>(get_window(), "recruit_details", false)
175  .set_displayed_type(*recruit_list_[selected_row]);
176 }
177 
179 {
180  help::show_help("recruit_and_recall");
181 }
182 
184 {
185  if(get_retval() == retval::OK) {
186  selected_index_ = find_widget<listbox>(&window, "recruit_list", false)
187  .get_selected_row();
188  }
189 }
190 
191 } // namespace dialogs
window(const builder_window::window_resolution &definition)
< Needs to be initialized in show.
Definition: window.cpp:263
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
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
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
static const color_t inactive_row_color(0x96, 0x96, 0x96)
This file contains the window object, this object is a top level container which has the event manage...
t_string get_image() const
Wrapper for label.
Definition: image.hpp:66
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:61
STL namespace.
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
A single unit type that the player may recruit.
Definition: types.hpp:45
std::vector< const unit_type * > recruit_list_
A vector of unit types in the order listed in the UI.
Class for a single line text area.
Definition: text_box.hpp:141
The listbox class.
Definition: listbox.hpp:45
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
Base container class.
Definition: grid.hpp:31
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:75
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
std::map< std::string, t_string > widget_item
Definition: widget.hpp:32
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
This file contains the settings handling of the widget library.
const t_string & type_name() const
The name of the unit in the current language setting.
Definition: types.hpp:141
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:58
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:645
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:126
void filter_text_changed(const std::string &text)
window * get_window()
Returns a pointer to the dialog&#39;s window.
bool ci_search(const std::string &s1, const std::string &s2)
Definition: gettext.cpp:569
std::size_t i
Definition: function.cpp:967
const bool & debug
void set_image(const t_string &label)
Wrapper for set_label.
Definition: image.hpp:53
int get_retval() const
Returns the cached window exit code.
bool empty() const
Definition: tstring.hpp:187
const std::string & color() const
Definition: team.hpp:244
std::vector< std::string > split(const config_attribute_value &val)
Abstract base class for all modal dialogs.
Functions to load and save images from/to disk.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
static std::string gray_if_unrecruitable(const std::string &text, const bool is_recruitable)
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::vector< std::string > last_words_
const std::string & str() const
Definition: tstring.hpp:191
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:138
std::map< const unit_type *, t_string > & recruit_map_
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