The Battle for Wesnoth  1.19.7+dev
unit_create.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "gui/core/log.hpp"
21 #include "gui/widgets/listbox.hpp"
22 #include "gui/widgets/label.hpp"
23 #include "gui/widgets/grid.hpp"
25 #include "gui/widgets/text_box.hpp"
28 #include "gui/widgets/window.hpp"
29 #include "gettext.hpp"
30 #include "units/types.hpp"
31 #include "utils/ci_searcher.hpp"
32 
33 #include <functional>
34 
35 static std::string last_chosen_type_id = "";
36 static std::string last_variation = "";
38 
39 namespace gui2::dialogs
40 {
41 
42 REGISTER_DIALOG(unit_create)
43 
45  : modal_dialog(window_id())
46  , gender_(last_gender)
47  , choice_(last_chosen_type_id)
48  , variation_(last_variation)
49 {
50 }
51 
53 {
54  toggle_button& male_toggle
55  = find_widget<toggle_button>("male_toggle");
56  toggle_button& female_toggle
57  = find_widget<toggle_button>("female_toggle");
58 
61 
63 
65  std::bind(&unit_create::gender_toggle_callback, this, std::placeholders::_2));
66 
67  menu_button& var_box = find_widget<menu_button>("variation_box");
68 
70 
71  listbox& list = find_widget<listbox>("unit_type_list");
72 
73  text_box* filter
74  = find_widget<text_box>("filter_box", false, true);
75 
77  std::bind(&unit_create::filter_text_changed, this, std::placeholders::_2));
78 
79  keyboard_capture(filter);
80  add_to_keyboard_chain(&list);
81 
83 
84  list.clear();
85 
86  for(const auto& i : unit_types.types())
87  {
88  // Make sure this unit type is built with the data we need.
90 
91  units_.push_back(&i.second);
92 
93  widget_data row_data;
94  widget_item column;
95 
96  column["label"] = units_.back()->race()->plural_name();
97  row_data.emplace("race", column);
98 
99  column["label"] = units_.back()->type_name();
100  if(units_.back()->type_name().str() != units_.back()->id()) {
101  column["label"] += " (" + units_.back()->id() + ")";
102  }
103  row_data.emplace("unit_type", column);
104 
105  list.add_row(row_data);
106 
107  // Select the previous choice, if any.
108  if(!choice_.empty() && choice_ == i.first) {
109  list.select_last_row();
110  }
111  }
112 
113  if(units_.empty()) {
114  ERR_GUI_G << "no unit types found for unit create dialog; not good"
115  << std::endl;
116  }
117 
118  list.set_sorters(
119  [this](const std::size_t i) { return units_[i]->race()->plural_name(); },
120  [this](const std::size_t i) { return units_[i]->type_name(); }
121  );
122 
123  // Select the first entry on sort if no previous selection was provided.
124  list.set_active_sorter("sort_0", sort_order::type::ascending, choice_.empty());
125 
127 }
128 
130 {
131  listbox& list = find_widget<listbox>("unit_type_list");
132 
133  choice_ = "";
134 
135  if(get_retval() != retval::OK) {
136  return;
137  }
138 
139  const int selected_row = list.get_selected_row();
140  if(selected_row < 0) {
141  return;
142  } else if(static_cast<std::size_t>(selected_row) >= units_.size()) {
143  // FIXME: maybe assert?
144  ERR_GUI_G << "unit create dialog has more list items than known unit "
145  "types; not good";
146  return;
147  }
148 
149  last_chosen_type_id = choice_ = units_[selected_row]->id();
152 }
153 
155 {
156  const int selected_row
157  = find_widget<listbox>("unit_type_list").get_selected_row();
158 
159  if(selected_row == -1) {
160  return;
161  }
162 
163  const unit_type* ut = &units_[selected_row]->get_gender_unit_type(gender_);
164 
165  if(!variation_.empty()) {
166  // This effectively translates to `ut = ut` if somehow variation_ does
167  // not refer to a variation that the unit type supports.
168  ut = &ut->get_variation(variation_);
169  }
170 
171  find_widget<unit_preview_pane>("unit_details").set_display_data(*ut);
172 }
173 
175 {
176  const int selected_row
177  = find_widget<listbox>("unit_type_list").get_selected_row();
178 
179  if(selected_row == -1) {
180  return;
181  }
182 
184 
186  return units_[selected_row]->has_gender_variation(gender);
187  });
188 
189  menu_button& var_box = find_widget<menu_button>("variation_box");
190  std::vector<config> var_box_values;
191  var_box_values.emplace_back("label", _("unit_variation^Default Variation"), "variation_id", "");
192 
193  const auto& ut = *units_[selected_row];
194  const auto& uvars = ut.variation_types();
195 
196  var_box.set_active(!uvars.empty());
197 
198  unsigned n = 0, selection = 0;
199 
200  for(const auto& pair : uvars) {
201  ++n;
202 
203  const std::string& uv_id = pair.first;
204  const unit_type& uv = pair.second;
205 
206  std::string uv_label;
207  if(!uv.variation_name().empty()) {
208  uv_label = uv.variation_name() + " (" + uv_id + ")";
209  } else if(!uv.type_name().empty() && uv.type_name() != ut.type_name()) {
210  uv_label = uv.type_name() + " (" + uv_id + ")";
211  } else {
212  uv_label = uv_id;
213  }
214 
215  var_box_values.emplace_back("label", uv_label, "variation_id", uv_id);
216 
217  if(uv_id == variation_) {
218  selection = n;
219  }
220  }
221 
222  // If we didn't find the variation selection again then the new selected
223  // unit type doesn't have that variation id.
224  if(!selection) {
225  variation_.clear();
226  }
227 
228  var_box.set_values(var_box_values, selection);
229 }
230 
231 void unit_create::filter_text_changed(const std::string& text)
232 {
233  find_widget<listbox>("unit_type_list")
234  .filter_rows_by([this, match = translation::make_ci_matcher(text)](std::size_t row) {
235  return match(
236  units_[row]->type_name(),
237  units_[row]->race()->plural_name(),
238  units_[row]->id()
239  );
240  });
241 }
242 
244 {
245  gender_ = val;
246 
248 }
249 
251 {
252  menu_button& var_box = find_widget<menu_button>("variation_box");
253  variation_ = var_box.get_value_config()["variation_id"].str();
254 
256 }
257 
258 } // 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.
Definition: unit_create.cpp:52
unit_race::GENDER gender_
Definition: unit_create.hpp:66
group< unit_race::GENDER > gender_toggle
Definition: unit_create.hpp:86
unit_race::GENDER gender()
Gender choice from the user.
Definition: unit_create.hpp:52
void list_item_clicked()
Callbacks.
void filter_text_changed(const std::string &text)
virtual void post_show() override
Actions to be taken after the window has been shown.
void gender_toggle_callback(const unit_race::GENDER val)
std::vector< const unit_type * > units_
Definition: unit_create.hpp:64
void add_member(selectable_item *w, const T &value)
Adds a widget/value pair to the group map.
Definition: group.hpp:42
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:111
void set_callback_on_value_change(std::function< void(widget &, const T)> func)
Sets a common callback function for all members.
Definition: group.hpp:121
void set_members_enabled(std::function< bool(const T &)> predicate)
Wrapper for enabling or disabling member widgets.
Definition: group.hpp:150
The listbox class.
Definition: listbox.hpp:41
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list's last row.
Definition: listbox.hpp:186
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:92
void set_active_sorter(std::string_view id, sort_order::type order, bool select_first=false)
Sorts the listbox by a pre-set sorting option.
Definition: listbox.cpp:617
void set_sorters(Args &&... functors)
Registers sorting controls using magic index IDs.
Definition: listbox.hpp:306
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:153
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:305
const ::config & get_value_config() const
Returns the entire config object for the selected row.
Definition: menu_button.hpp:70
void set_values(const std::vector<::config > &values, unsigned selected=0)
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: menu_button.cpp:74
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
bool empty() const
Definition: tstring.hpp:194
@ FEMALE
Definition: race.hpp:28
@ MALE
Definition: race.hpp:28
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.cpp:1257
const unit_type_map & types() const
Definition: types.hpp:396
A single unit type that the player may recruit.
Definition: types.hpp:43
const unit_type & get_variation(const std::string &id) const
Definition: types.cpp:476
@ FULL
Definition: types.hpp:74
const t_string & variation_name() const
Definition: types.hpp:174
const t_string & type_name() const
The name of the unit in the current language setting.
Definition: types.hpp:138
std::size_t i
Definition: function.cpp:1029
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define ERR_GUI_G
Definition: log.hpp:44
This file contains the window object, this object is a top level container which has the event manage...
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
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
auto make_ci_matcher(std::string_view filter_text)
Returns a function which performs locale-aware case-insensitive search.
Definition: ci_searcher.hpp:24
static map_location::direction n
unit_type_data unit_types
Definition: types.cpp:1500
static unit_race::GENDER last_gender
Definition: unit_create.cpp:37
static std::string last_chosen_type_id
Definition: unit_create.cpp:35
static std::string last_variation
Definition: unit_create.cpp:36