The Battle for Wesnoth  1.15.2+dev
unit_recruit.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 
16 #include "font/text_formatting.hpp"
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/text_box.hpp"
27 #include "gui/widgets/window.hpp"
28 #include "gettext.hpp"
29 #include "help/help.hpp"
30 #include "play_controller.hpp"
31 #include "resources.hpp"
32 #include "team.hpp"
33 #include "units/types.hpp"
34 #include "whiteboard/manager.hpp"
35 
36 #include "utils/functional.hpp"
37 
38 namespace gui2
39 {
40 namespace dialogs
41 {
42 
43 REGISTER_DIALOG(unit_recruit)
44 
45 unit_recruit::unit_recruit(std::map<const unit_type*, t_string>& recruit_map, team& team)
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(text_box_base* textbox, const std::string& text)
70 {
71  window& window = *textbox->get_window();
72 
73  listbox& list = find_widget<listbox>(&window, "recruit_list", false);
74 
75  const std::vector<std::string> words = utils::split(text, ' ');
76 
77  if(words == last_words_)
78  return;
79  last_words_ = words;
80 
81  boost::dynamic_bitset<> show_items;
82  show_items.resize(list.get_item_count(), true);
83 
84  if(!text.empty()) {
85  for(unsigned int i = 0; i < list.get_item_count(); i++) {
86  assert(i < recruit_list_.size());
87  const unit_type* type = recruit_list_[i];
88  if(!type) continue;
89 
90  bool found = false;
91  for(const auto & word : words)
92  {
93  // Search for the name in the local language.
94  // In debug mode, also search for the type id.
95  found = (game_config::debug && translation::ci_search(type->id(), word)) ||
96  translation::ci_search(type->type_name(), word);
97 
98  if(!found) {
99  // one word doesn't match, we don't reach words.end()
100  break;
101  }
102  }
103 
104  show_items[i] = found;
105  }
106  }
107 
108  list.set_row_shown(show_items);
109 }
110 
112 {
113  text_box* filter = find_widget<text_box>(&window, "filter_box", false, true);
115  std::bind(&unit_recruit::filter_text_changed, this, _1, _2));
116 
117  listbox& list = find_widget<listbox>(&window, "recruit_list", false);
118 
119  connect_signal_notify_modified(list, std::bind(&unit_recruit::list_item_clicked, this, std::ref(window)));
120 
121  window.keyboard_capture(filter);
122  window.add_to_keyboard_chain(&list);
123 
125  find_widget<button>(&window, "show_help", false),
126  std::bind(&unit_recruit::show_help, this));
127 
128  for(const auto& recruit : recruit_list_)
129  {
130  const t_string& error = recruit_map_[recruit];
131  std::map<std::string, string_map> row_data;
132  string_map column;
133 
134  std::string image_string = recruit->image() + "~RC(" + recruit->flag_rgb() + ">"
135  + team_.color() + ")";
136 
137  const bool is_recruitable = error.empty();
138 
139  const std::string cost_string = std::to_string(recruit->cost());
140 
141  column["use_markup"] = "true";
142  if(!error.empty()) {
143  // Just set the tooltip on every single element in this row.
144  column["tooltip"] = error;
145  }
146 
147  column["label"] = image_string + (is_recruitable ? "" : "~GS()");
148  row_data.emplace("unit_image", column);
149 
150  column["label"] = gray_if_unrecruitable(recruit->type_name(), is_recruitable);
151  row_data.emplace("unit_type", column);
152 
153  column["label"] = gray_if_unrecruitable(cost_string, is_recruitable);
154  row_data.emplace("unit_cost", column);
155 
156  grid& grid = list.add_row(row_data);
157  if(!is_recruitable) {
158  image *gold_icon = dynamic_cast<image*>(grid.find("gold_icon", false));
159  assert(gold_icon);
160  gold_icon->set_image(gold_icon->get_image() + "~GS()");
161  }
162  }
163 
164  list_item_clicked(window);
165 }
166 
168 {
169  const int selected_row
170  = find_widget<listbox>(&window, "recruit_list", false).get_selected_row();
171 
172  if(selected_row == -1) {
173  return;
174  }
175 
176  find_widget<unit_preview_pane>(&window, "recruit_details", false)
177  .set_displayed_type(*recruit_list_[selected_row]);
178 }
179 
181 {
182  help::show_help("recruit_and_recall");
183 }
184 
186 {
187  if(get_retval() == retval::OK) {
188  selected_index_ = find_widget<listbox>(&window, "recruit_list", false)
189  .get_selected_row();
190  }
191 }
192 
193 } // namespace dialogs
194 } // namespace gui2
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:115
Abstract base class for text items.
static const color_t inactive_row_color(0x96, 0x96, 0x96)
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
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:58
STL namespace.
-file sdl_utils.hpp
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
A single unit type that the player may recruit.
Definition: types.hpp:42
std::vector< const unit_type * > recruit_list_
A vector of unit types in the order listed in the UI. Used by unit_recruit::post_show.
Class for a single line text area.
Definition: text_box.hpp:121
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
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:30
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
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
virtual void pre_show(window &window) override
Inherited from modal_dialog.
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:135
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
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: grid.cpp:655
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:131
Various uncategorised dialogs.
bool ci_search(const std::string &s1, const std::string &s2)
Definition: gettext.cpp:519
std::size_t i
Definition: function.cpp:933
window * get_window()
Get the parent window.
Definition: widget.cpp:114
void list_item_clicked(window &window)
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
const bool & debug
static int sort(lua_State *L)
Definition: ltablib.cpp:411
void set_image(const t_string &label)
Wrapper for set_label.
Definition: image.hpp:45
bool empty() const
Definition: tstring.hpp:182
const std::string & color() const
Definition: team.hpp:256
this module manages the cache of images.
static std::string gray_if_unrecruitable(const std::string &text, const bool is_recruitable)
void filter_text_changed(text_box_base *textbox, const std::string &text)
Dialog was closed with the OK button.
Definition: retval.hpp:34
std::vector< std::string > last_words_
const std::string & str() const
Definition: tstring.hpp:186
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:143
std::map< const unit_type *, t_string > & recruit_map_