The Battle for Wesnoth  1.17.17+dev
faction_select.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2023
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 
19 #include "formatter.hpp"
20 #include "game_config_manager.hpp"
21 #include "gettext.hpp"
23 #include "gui/core/log.hpp"
24 #include "gui/widgets/button.hpp"
25 #include "gui/widgets/drawing.hpp"
26 #include "gui/widgets/label.hpp"
27 #include "gui/widgets/listbox.hpp"
29 #include "gui/widgets/settings.hpp"
31 #include "gui/widgets/window.hpp"
32 #include "help/help.hpp"
33 #include "preferences/game.hpp" // for encountered_units
34 #include "units/types.hpp"
35 
36 #include <functional>
37 
38 namespace gui2::dialogs
39 {
40 
41 REGISTER_DIALOG(faction_select)
42 
43 faction_select::faction_select(ng::flg_manager& flg_manager, const std::string& color, const int side)
44  : modal_dialog(window_id())
45  , flg_manager_(flg_manager)
46  , tc_color_(color)
47  , side_(side)
48  , last_faction_(flg_manager.current_faction_index())
49  , last_leader_(flg_manager.current_leader_index())
50  , last_gender_(flg_manager.current_gender_index())
51 {
52 }
53 
55 {
56  find_widget<label>(&window, "starting_pos", false).set_label(std::to_string(side_));
57 
58  //
59  // Set up gender radio buttons
60  //
61  toggle_button& gender_rand = find_widget<toggle_button>(&window, "gender_random", false);
62  toggle_button& gender_male = find_widget<toggle_button>(&window, "gender_male", false);
63  toggle_button& gender_female = find_widget<toggle_button>(&window, "gender_female", false);
64 
65  gender_toggle_.add_member(&gender_rand, "random");
68 
70 
72  std::bind(&faction_select::on_gender_select, this, std::placeholders::_2));
73 
74  //
75  // Set up leader menu button
76  //
77  connect_signal_notify_modified(find_widget<menu_button>(&window, "leader_menu", false),
78  std::bind(&faction_select::on_leader_select, this));
79 
80  // Leader's profile button
81  find_widget<button>(&window, "type_profile", false).connect_click_handler(
82  std::bind(&faction_select::profile_button_callback, this));
83 
84  //
85  // Set up faction list
86  //
87  listbox& list = find_widget<listbox>(&window, "faction_list", false);
88 
89  window.keyboard_capture(&list);
90 
92  std::bind(&faction_select::on_faction_select, this));
93 
94  for(const config *s : flg_manager_.choosable_factions()) {
95  const config& side = *s;
96 
99 
100  const std::string name = side["name"].str();
101  // flag_rgb here is unrelated to any handling in the unit class
102  const std::string flag_rgb = !side["flag_rgb"].empty() ? side["flag_rgb"].str() : "magenta";
103 
104  item["label"] = (formatter() << side["image"] << "~RC(" << flag_rgb << ">" << tc_color_ << ")").str();
105  data.emplace("faction_image", item);
106 
107  item["label"] = name;
108  data.emplace("faction_name", item);
109 
110  list.add_row(data);
111  }
112 
114 
116 }
117 
119 {
120  const int selected_row = find_widget<listbox>(get_window(), "faction_list", false).get_selected_row();
121 
122  if(selected_row == -1) {
123  return;
124  }
125 
126  // Since set_current_faction overrides the current leader, save a copy of the previous leader index so the
127  // leader dropdown can be set to the appropriate initial selection.
128  const int previous_leader_selection = flg_manager_.current_leader_index();
129 
130  flg_manager_.set_current_faction(selected_row);
131 
132  std::vector<config> leaders;
133 
134  for(const std::string& leader : flg_manager_.choosable_leaders()) {
135  const unit_type* unit = unit_types.find(leader);
136 
137  if(unit) {
138  const std::string icon = formatter() << unit->image() << "~RC(" << unit->flag_rgb() << ">" << tc_color_ << ")";
139  leaders.emplace_back("label", unit->type_name(), "icon", icon);
140  } else if(leader == "random") {
141  leaders.emplace_back("label", _("Random"), "icon", ng::random_enemy_picture);
142  } else if(leader == "null") {
143  leaders.emplace_back("label", font::unicode_em_dash);
144  } else {
145  leaders.emplace_back("label", "?");
146  }
147  }
148 
149  menu_button& leader_dropdown = find_widget<menu_button>(get_window(), "leader_menu", false);
150 
151  leader_dropdown.set_values(leaders, std::min<int>(leaders.size() - 1, previous_leader_selection));
152  leader_dropdown.set_active(leaders.size() > 1 && !flg_manager_.is_saved_game());
153 
155 
156  // Print recruits
157  const std::vector<std::string> recruit_list = utils::split(flg_manager_.current_faction()["recruit"]);
158  std::vector<t_string> recruit_names;
159 
160  for(const auto& recruit : recruit_list) {
161  if(const unit_type* rt = unit_types.find(recruit)) {
162  recruit_names.push_back(font::unicode_bullet + " " + rt->type_name());
163  }
164  }
165 
166  std::sort(recruit_names.begin(), recruit_names.end(), [](const std::string& s1, const std::string& s2) {
167  return translation::compare(s1, s2) < 0;
168  });
169 
170  find_widget<styled_widget>(get_window(), "recruits", false).set_label(utils::join(recruit_names, "\n"));
171 }
172 
174 {
175  flg_manager_.set_current_leader(find_widget<menu_button>(get_window(), "leader_menu", false).get_value());
176 
177  // TODO: should we decouple this from the flg manager and instead just check the unit type directly?
178  // If that's done so, we'd need to come up with a different check for Random availability.
179  gender_toggle_.set_members_enabled([this](const std::string& gender)->bool {
180  const std::vector<std::string>& genders = flg_manager_.choosable_genders();
181  return std::find(genders.begin(), genders.end(), gender) != genders.end();
182  });
183 
185 
186  // Disable the profile button if leader_type is dash or "Random"
187  button& profile_button = find_widget<button>(get_window(), "type_profile", false);
188  const std::string& leader_type = find_widget<menu_button>(get_window(), "leader_menu", false).get_value_string();
189  profile_button.set_active(unit_types.find(leader_type) != nullptr);
190 }
191 
193 {
194  const std::string& leader_type = find_widget<menu_button>(get_window(), "leader_menu", false).get_value_string();
195  const unit_type* ut = unit_types.find(leader_type);
196  if(ut != nullptr) {
197  preferences::encountered_units().insert(ut->id());
199  }
200 }
201 
202 void faction_select::on_gender_select(const std::string val)
203 {
205 
207 }
208 
210 {
211  std::string leader_image = ng::random_enemy_picture;
212 
215  leader_image = formatter() << utg.image() << "~RC(" << utg.flag_rgb() << ">" << tc_color_ << ")";
216  }
217 
218  find_widget<drawing>(get_window(), "leader_image", false).set_label(leader_image);
219 }
220 
222 {
223  //
224  // If we're canceling, restore the previous selections. It might be worth looking
225  // into only making selections at all here in post_show, but that would require a
226  // refactor of the flg_manager class.
227  //
228  // Also, note it's important to set these in the order of faction -> leader -> gender
229  // or the saved indices will be invalid!
230  //
231  // -- vultraz, 2018-06-16
232  //
233  if(get_retval() != retval::OK) {
237  }
238 }
239 
240 } // namespace dialogs
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
bool empty() const
Definition: config.cpp:856
std::ostringstream wrapper.
Definition: formatter.hpp:40
Simple push button.
Definition: button.hpp:37
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:65
void on_gender_select(const std::string val)
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
group< std::string > gender_toggle_
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
window * get_window()
Returns a pointer to the dialog's window.
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:46
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:62
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:246
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:62
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:77
Class for a toggle button.
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
void keyboard_capture(widget *widget)
Definition: window.cpp:1130
const std::vector< const config * > & choosable_factions() const
Definition: flg_manager.hpp:61
const std::string & current_gender() const
Definition: flg_manager.hpp:71
void set_current_faction(const unsigned index)
const std::vector< std::string > & choosable_genders() const
Definition: flg_manager.hpp:65
bool is_saved_game() const
Definition: flg_manager.hpp:52
int current_leader_index() const
Definition: flg_manager.hpp:79
const std::vector< std::string > & choosable_leaders() const
Definition: flg_manager.hpp:63
void set_current_leader(const unsigned index)
int current_faction_index() const
void set_current_gender(const unsigned index)
const config & current_faction() const
Definition: flg_manager.hpp:67
const std::string & current_leader() const
Definition: flg_manager.hpp:69
static const std::string s_female
Standard string id (not translatable) for FEMALE.
Definition: race.hpp:28
static const std::string s_male
Standard string id (not translatable) for MALE.
Definition: race.hpp:29
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
Definition: types.cpp:1246
A single unit type that the player may recruit.
Definition: types.hpp:46
const std::string & image() const
Definition: types.hpp:179
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:144
const std::string & flag_rgb() const
Definition: types.cpp:720
const unit_type & get_gender_unit_type(std::string gender) const
Returns a gendered variant of this unit_type.
Definition: types.cpp:453
This class represents a single unit of a specific type.
Definition: unit.hpp:134
static std::string _(const char *str)
Definition: gettext.hpp:93
const t_string & type_name() const
Gets the translatable name of this unit's type.
Definition: unit.hpp:370
const std::string & flag_rgb() const
Get the source color palette to use when recoloring the unit's image.
Definition: unit.cpp:1055
Define the common log macros for the gui toolkit.
This file contains the window object, this object is a top level container which has the event manage...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
const std::string unicode_em_dash
Definition: constants.cpp:44
const std::string unicode_bullet
Definition: constants.cpp:47
std::string flag_rgb
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
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
std::map< std::string, t_string > widget_item
Definition: widget.hpp:32
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:414
void show_unit_description(const unit &u)
Definition: help.cpp:75
const std::string random_enemy_picture("units/random-dice.png")
std::set< std::string > & encountered_units()
Definition: game.cpp:916
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
std::string_view data
Definition: picture.cpp:199
This file contains the settings handling of the widget library.
static map_location::DIRECTION s
unit_type_data unit_types
Definition: types.cpp:1463