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 
74  = find_widget<text_box>("filter_box", false, true);
75 
76  filter->on_modified([this](const auto& box) { filter_text_changed(box.text()); });
77 
79  add_to_keyboard_chain(&list);
80 
82 
83  list.clear();
84 
85  for(const auto& i : unit_types.types())
86  {
87  // Make sure this unit type is built with the data we need.
89 
90  units_.push_back(&i.second);
91 
92  widget_data row_data;
93  widget_item column;
94 
95  column["label"] = units_.back()->race()->plural_name();
96  row_data.emplace("race", column);
97 
98  column["label"] = units_.back()->type_name();
99  if(units_.back()->type_name().str() != units_.back()->id()) {
100  column["label"] += " (" + units_.back()->id() + ")";
101  }
102  row_data.emplace("unit_type", column);
103 
104  list.add_row(row_data);
105 
106  // Select the previous choice, if any.
107  if(!choice_.empty() && choice_ == i.first) {
108  list.select_last_row();
109  }
110  }
111 
112  if(units_.empty()) {
113  ERR_GUI_G << "no unit types found for unit create dialog; not good"
114  << std::endl;
115  }
116 
117  list.set_sorters(
118  [this](const std::size_t i) { return units_[i]->race()->plural_name(); },
119  [this](const std::size_t i) { return units_[i]->type_name(); }
120  );
121 
122  // Select the first entry on sort if no previous selection was provided.
123  list.set_active_sorter("sort_0", sort_order::type::ascending, choice_.empty());
124 
126 }
127 
129 {
130  listbox& list = find_widget<listbox>("unit_type_list");
131 
132  choice_ = "";
133 
134  if(get_retval() != retval::OK) {
135  return;
136  }
137 
138  const int selected_row = list.get_selected_row();
139  if(selected_row < 0) {
140  return;
141  } else if(static_cast<std::size_t>(selected_row) >= units_.size()) {
142  // FIXME: maybe assert?
143  ERR_GUI_G << "unit create dialog has more list items than known unit "
144  "types; not good";
145  return;
146  }
147 
148  last_chosen_type_id = choice_ = units_[selected_row]->id();
151 }
152 
154 {
155  const int selected_row
156  = find_widget<listbox>("unit_type_list").get_selected_row();
157 
158  if(selected_row == -1) {
159  return;
160  }
161 
162  const unit_type* ut = &units_[selected_row]->get_gender_unit_type(gender_);
163 
164  if(!variation_.empty()) {
165  // This effectively translates to `ut = ut` if somehow variation_ does
166  // not refer to a variation that the unit type supports.
167  ut = &ut->get_variation(variation_);
168  }
169 
170  find_widget<unit_preview_pane>("unit_details").set_display_data(*ut);
171 }
172 
174 {
175  const int selected_row
176  = find_widget<listbox>("unit_type_list").get_selected_row();
177 
178  if(selected_row == -1) {
179  return;
180  }
181 
183 
185  return units_[selected_row]->has_gender_variation(gender);
186  });
187 
188  menu_button& var_box = find_widget<menu_button>("variation_box");
189  std::vector<config> var_box_values;
190  var_box_values.emplace_back("label", _("unit_variation^Default Variation"), "variation_id", "");
191 
192  const auto& ut = *units_[selected_row];
193  const auto& uvars = ut.variation_types();
194 
195  var_box.set_active(!uvars.empty());
196 
197  unsigned n = 0, selection = 0;
198 
199  for(const auto& pair : uvars) {
200  ++n;
201 
202  const std::string& uv_id = pair.first;
203  const unit_type& uv = pair.second;
204 
205  std::string uv_label;
206  if(!uv.variation_name().empty()) {
207  uv_label = uv.variation_name() + " (" + uv_id + ")";
208  } else if(!uv.type_name().empty() && uv.type_name() != ut.type_name()) {
209  uv_label = uv.type_name() + " (" + uv_id + ")";
210  } else {
211  uv_label = uv_id;
212  }
213 
214  var_box_values.emplace_back("label", uv_label, "variation_id", uv_id);
215 
216  if(uv_id == variation_) {
217  selection = n;
218  }
219  }
220 
221  // If we didn't find the variation selection again then the new selected
222  // unit type doesn't have that variation id.
223  if(!selection) {
224  variation_.clear();
225  }
226 
227  var_box.set_values(var_box_values, selection);
228 }
229 
230 void unit_create::filter_text_changed(const std::string& text)
231 {
232  find_widget<listbox>("unit_type_list")
233  .filter_rows_by([this, match = translation::make_ci_matcher(text)](std::size_t row) {
234  return match(
235  units_[row]->type_name(),
236  units_[row]->race()->plural_name(),
237  units_[row]->id()
238  );
239  });
240 }
241 
243 {
244  gender_ = val;
245 
247 }
248 
250 {
251  menu_button& var_box = find_widget<menu_button>("variation_box");
252  variation_ = var_box.get_value_config()["variation_id"].str();
253 
255 }
256 
257 } // 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:621
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
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
constexpr auto filter
Definition: ranges.hpp:38
static map_location::direction n
unit_type_data unit_types
Definition: types.cpp:1504
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