The Battle for Wesnoth  1.15.2+dev
unit_create.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Iris Morelle <shadowm2006@gmail.com>
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 
18 
20 #include "gui/core/log.hpp"
21 #include "gui/widgets/listbox.hpp"
22 #include "gui/widgets/settings.hpp"
23 #include "gui/widgets/button.hpp"
24 #include "gui/widgets/image.hpp"
25 #include "gui/widgets/label.hpp"
26 #include "gui/widgets/grid.hpp"
27 #include "gui/widgets/text_box.hpp"
30 #include "gui/widgets/window.hpp"
31 #include "help/help.hpp"
32 #include "gettext.hpp"
33 #include "play_controller.hpp"
34 #include "units/types.hpp"
35 
36 #include "utils/functional.hpp"
37 #include <boost/dynamic_bitset.hpp>
38 
39 static std::string last_chosen_type_id = "";
41 
42 namespace gui2
43 {
44 namespace dialogs
45 {
46 
47 /*WIKI
48  * @page = GUIWindowDefinitionWML
49  * @order = 2_unit_create
50  *
51  * == Unit create ==
52  *
53  * This shows the debug-mode dialog to create new units on the map.
54  *
55  * @begin{table}{dialog_widgets}
56  *
57  * male_toggle & & toggle_button & m &
58  * Option button to select the "male" gender for created units. $
59  *
60  * female_toggle & & toggle_button & m &
61  * Option button to select the "female" gender for created units. $
62  *
63  * unit_type_list & & listbox & m &
64  * Listbox displaying existing unit types sorted by name and race. $
65  *
66  * -unit_type & & styled_widget & m &
67  * Widget which shows the unit type name label. $
68  *
69  * -race & & styled_widget & m &
70  * Widget which shows the unit race name label. $
71  *
72  * @end{table}
73  */
74 
75 REGISTER_DIALOG(unit_create)
76 
78  : gender_(last_gender)
79  , choice_(last_chosen_type_id)
80  , last_words_()
81 {
82  set_restore(true);
83 }
84 
86 {
87  toggle_button& male_toggle
88  = find_widget<toggle_button>(&window, "male_toggle", false);
89  toggle_button& female_toggle
90  = find_widget<toggle_button>(&window, "female_toggle", false);
91 
94 
96 
98  std::bind(&unit_create::gender_toggle_callback, this));
99 
100  listbox& list = find_widget<listbox>(&window, "unit_type_list", false);
101 
102  text_box* filter
103  = find_widget<text_box>(&window, "filter_box", false, true);
104 
106  std::bind(&unit_create::filter_text_changed, this, _1, _2));
107 
108  window.keyboard_capture(filter);
109  window.add_to_keyboard_chain(&list);
110 
111  connect_signal_notify_modified(list, std::bind(&unit_create::list_item_clicked, this, std::ref(window)));
112 
113  list.clear();
114 
115  for(const auto & i : unit_types.types())
116  {
117  // Make sure this unit type is built with the data we need.
119 
120  units_.push_back(&i.second);
121 
122  std::map<std::string, string_map> row_data;
123  string_map column;
124 
125  column["label"] = units_.back()->race()->plural_name();
126  row_data.emplace("race", column);
127 
128  column["label"] = units_.back()->type_name();
129  if(units_.back()->type_name().str() != units_.back()->id()) {
130  column["label"] += " (" + units_.back()->id() + ")";
131  }
132  row_data.emplace("unit_type", column);
133 
134  list.add_row(row_data);
135 
136  // Select the previous choice, if any.
137  if(!choice_.empty() && choice_ == i.first) {
138  list.select_last_row();
139  }
140  }
141 
142  if(units_.empty()) {
143  ERR_GUI_G << "no unit types found for unit create dialog; not good"
144  << std::endl;
145  }
146 
147  list.register_translatable_sorting_option(0, [this](const int i) { return (*units_[i]).race()->plural_name().str(); });
148  list.register_translatable_sorting_option(1, [this](const int i) { return (*units_[i]).type_name().str(); });
149 
150  // Select the first entry on sort if no previous selection was provided.
152 
153  list_item_clicked(window);
154 }
155 
157 {
158  listbox& list = find_widget<listbox>(&window, "unit_type_list", false);
159 
160  choice_ = "";
161 
162  if(get_retval() != retval::OK) {
163  return;
164  }
165 
166  const int selected_row = list.get_selected_row();
167  if(selected_row < 0) {
168  return;
169  } else if(static_cast<std::size_t>(selected_row) >= units_.size()) {
170  // FIXME: maybe assert?
171  ERR_GUI_G << "unit create dialog has more list items than known unit "
172  "types; not good\n";
173  return;
174  }
175 
176  last_chosen_type_id = choice_ = units_[selected_row]->id();
178 }
179 
181 {
182  window* w = get_window();
183 
184  const int selected_row
185  = find_widget<listbox>(w, "unit_type_list", false).get_selected_row();
186 
187  if(selected_row == -1) {
188  return;
189  }
190 
191  find_widget<unit_preview_pane>(w, "unit_details", false)
192  .set_displayed_type(units_[selected_row]->get_gender_unit_type(gender_));
193 }
194 
196 {
197  const int selected_row
198  = find_widget<listbox>(&window, "unit_type_list", false).get_selected_row();
199 
200  if(selected_row == -1) {
201  return;
202  }
203 
205 
207  return units_[selected_row]->has_gender_variation(gender);
208  });
209 }
210 
211 void unit_create::filter_text_changed(text_box_base* textbox, const std::string& text)
212 {
213  window& window = *textbox->get_window();
214 
215  listbox& list = find_widget<listbox>(&window, "unit_type_list", false);
216 
217  const std::vector<std::string> words = utils::split(text, ' ');
218 
219  if(words == last_words_)
220  return;
221  last_words_ = words;
222 
223  boost::dynamic_bitset<> show_items;
224  show_items.resize(list.get_item_count(), true);
225 
226  if(!text.empty()) {
227  for(unsigned int i = 0; i < list.get_item_count(); i++) {
228  grid* row = list.get_row_grid(i);
229 
230  grid::iterator it = row->begin();
231  label& type_label
232  = find_widget<label>(*it, "unit_type", false);
233  label& race_label
234  = find_widget<label>(*it, "race", false);
235 
236  assert(i < units_.size());
237  const std::string& unit_type_id = units_[i] ? units_[i]->id() : "";
238 
239  bool found = false;
240  for(const auto & word : words)
241  {
242  found = translation::ci_search(type_label.get_label().str(), word) ||
243  translation::ci_search(race_label.get_label().str(), word) ||
244  translation::ci_search(unit_type_id, word);
245 
246  if(!found) {
247  // one word doesn't match, we don't reach words.end()
248  break;
249  }
250  }
251 
252  show_items[i] = found;
253  }
254  }
255 
256  list.set_row_shown(show_items);
257 }
258 
260 {
262 
264 }
265 } // namespace dialogs
266 } // namespace gui2
Define the common log macros for the gui toolkit.
void set_active_sorting_option(const order_pair &sort_by, const bool select_first=false)
Sorts the listbox by a pre-set sorting option.
Definition: listbox.cpp:640
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
Abstract base class for text items.
iterator begin()
Definition: grid.hpp:479
unit_race::GENDER gender()
Gender choice from the user.
Definition: unit_create.hpp:53
T get_active_member_value()
Returns the value paired with the currently actively toggled member of the group. ...
Definition: group.hpp:95
This file contains the window object, this object is a top level container which has the event manage...
void set_members_enabled(std::function< bool(const T &)> predicate)
Wrapper for enabling or disabling member widgets.
Definition: group.hpp:142
#define ERR_GUI_G
Definition: log.hpp:43
unit_type_data unit_types
Definition: types.cpp:1529
virtual void post_show(window &window) override
Inherited from modal_dialog.
void register_translatable_sorting_option(const int col, translatable_sorter_func_t f)
Registers a special sorting function specifically for translatable values.
Definition: listbox.cpp:632
window * get_window() const
Returns a pointer to the dialog&#39;s window.
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list&#39;s last row.
Definition: listbox.hpp:192
Label showing a text.
Definition: label.hpp:32
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:272
const unit_type_map & types() const
Definition: types.hpp:366
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.
group< unit_race::GENDER > gender_toggle
Definition: unit_create.hpp:83
Class for a single line text area.
Definition: text_box.hpp:121
void update_displayed_type() const
Generic file dialog.
Definition: field-fwd.hpp:22
void build_unit_type(const unit_type &ut, unit_type::BUILD_STATUS status) const
Makes sure the provided unit_type is built to the specified level.
Definition: types.hpp:379
unit_race::GENDER gender_
Definition: unit_create.hpp:61
The listbox class.
Definition: listbox.hpp:40
Base container class.
Definition: grid.hpp:30
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 list_item_clicked(window &window)
Callbacks.
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:125
static std::string last_chosen_type_id
Definition: unit_create.cpp:39
Iterator for the child items.
Definition: grid.hpp:440
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:131
static unit_race::GENDER last_gender
Definition: unit_create.cpp:40
std::vector< const unit_type * > units_
Definition: unit_create.hpp:59
Various uncategorised dialogs.
bool ci_search(const std::string &s1, const std::string &s2)
Definition: gettext.cpp:519
void add_member(selectable_item *w, const T &value)
Adds a widget/value pair to the group map.
Definition: group.hpp:41
std::vector< std::string > last_words_
Definition: unit_create.hpp:65
std::size_t i
Definition: function.cpp:933
void filter_text_changed(text_box_base *textbox, const std::string &text)
window * get_window()
Get the parent window.
Definition: widget.cpp:114
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Definition: unit_create.cpp:85
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
int w
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:237
const t_string & get_label() const
Dialog was closed with the OK button.
Definition: retval.hpp:34
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:63
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:143
Class for a toggle button.
void set_member_states(const T &value)
Sets the toggle values for all widgets besides the one associated with the specified value to false...
Definition: group.hpp:110
void set_callback_on_value_change(std::function< void(widget &)> func)
Sets a common callback function for all members.
Definition: group.hpp:120