-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
faction_select.cpp
218 lines (166 loc) · 7.07 KB
/
faction_select.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/multiplayer/faction_select.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/auxiliary/old_markup.hpp"
#include "gui/core/log.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/settings.hpp"
#include "gui/widgets/image.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/menu_button.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/window.hpp"
#include "formatter.hpp"
#include "gettext.hpp"
#include "units/types.hpp"
#include "utils/functional.hpp"
namespace gui2
{
namespace dialogs
{
REGISTER_DIALOG(faction_select)
faction_select::faction_select(ng::flg_manager& flg_manager, const std::string& color, const int side)
: flg_manager_(flg_manager)
, tc_color_(color)
, side_(side)
{
}
void faction_select::pre_show(window& window)
{
find_widget<label>(&window, "starting_pos", false).set_label(std::to_string(side_));
//
// Set up gender radio buttons
//
toggle_button& gender_rand = find_widget<toggle_button>(&window, "gender_random", false);
toggle_button& gender_male = find_widget<toggle_button>(&window, "gender_male", false);
toggle_button& gender_female = find_widget<toggle_button>(&window, "gender_female", false);
gender_toggle_.add_member(&gender_rand, "random");
gender_toggle_.add_member(&gender_male, unit_race::s_male);
gender_toggle_.add_member(&gender_female, unit_race::s_female);
gender_toggle_.set_member_states("random");
gender_toggle_.set_callback_on_value_change(
std::bind(&faction_select::on_gender_select, this, std::ref(window)));
//
// Set up leader menu button
//
connect_signal_notify_modified(find_widget<menu_button>(&window, "leader_menu", false),
std::bind(&faction_select::on_leader_select, this, std::ref(window)));
//
// Set up faction list
//
listbox& list = find_widget<listbox>(&window, "faction_list", false);
window.keyboard_capture(&list);
connect_signal_notify_modified(list,
std::bind(&faction_select::on_faction_select, this, std::ref(window)));
for(const config *s : flg_manager_.choosable_factions()) {
const config& side = *s;
std::map<std::string, string_map> data;
string_map item;
const std::string name = side["name"].str();
// flag_rgb here is unrelated to any handling in the unit class
const std::string flag_rgb = !side["flag_rgb"].empty() ? side["flag_rgb"].str() : "magenta";
// Handle legacy DescriptionWML format.
if(name.find_first_of("=") != std::string::npos) {
gui2::legacy_menu_item parsed(name, "Use separate name= and image= keys. Multiple text columns are no longer supported.");
if(!side.has_attribute("image")) {
item["label"] = (formatter() << parsed.icon() << "~RC(" << flag_rgb << ">" << tc_color_ << ")").str();
data.emplace("faction_image", item);
}
item["label"] = parsed.label();
if(!parsed.description().empty()) {
item["label"] += " " + parsed.description();
}
data.emplace("faction_name", item);
} else {
item["label"] = (formatter() << side["image"] << "~RC(" << flag_rgb << ">" << tc_color_ << ")").str();
data.emplace("faction_image", item);
item["label"] = name;
data.emplace("faction_name", item);
}
list.add_row(data);
}
list.select_row(flg_manager_.current_faction_index());
on_faction_select(window);
}
void faction_select::on_faction_select(window& window)
{
const int selected_row = find_widget<listbox>(&window, "faction_list", false).get_selected_row();
if(selected_row == -1) {
return;
}
// Since set_current_faction overrides the current leader, save a copy of the previous leader index so the
// leader dropdown can be set to the appropriate initial selection.
const int previous_leader_selection = flg_manager_.current_leader_index();
flg_manager_.set_current_faction(selected_row);
std::vector<config> leaders;
for(const std::string& leader : flg_manager_.choosable_leaders()) {
const unit_type* unit = unit_types.find(leader);
if(unit) {
const std::string icon = formatter() << unit->image() << "~RC(" << unit->flag_rgb() << ">" << tc_color_ << ")";
leaders.emplace_back("label", unit->type_name(), "icon", icon);
} else if(leader == "random") {
leaders.emplace_back("label", _("Random"), "icon", ng::random_enemy_picture);
} else if(leader == "null") {
leaders.emplace_back("label", font::unicode_em_dash);
} else {
leaders.emplace_back("label", "?");
}
}
menu_button& leader_dropdown = find_widget<menu_button>(&window, "leader_menu", false);
leader_dropdown.set_values(leaders, std::min<int>(leaders.size() - 1, previous_leader_selection));
leader_dropdown.set_active(leaders.size() > 1 && !flg_manager_.is_saved_game());
on_leader_select(window);
// Print recruits
const std::vector<std::string> recruit_list = utils::split(flg_manager_.current_faction()["recruit"]);
std::vector<t_string> recruit_names;
for(const auto& recruit : recruit_list) {
if(const unit_type* rt = unit_types.find(recruit)) {
recruit_names.push_back(font::unicode_bullet + " " + rt->type_name());
}
}
std::sort(recruit_names.begin(), recruit_names.end(), [](const std::string& s1, const std::string& s2) {
return translation::compare(s1, s2) < 0;
});
find_widget<styled_widget>(&window, "recruits", false).set_label(utils::join(recruit_names, "\n"));
}
void faction_select::on_leader_select(window& window)
{
flg_manager_.set_current_leader(find_widget<menu_button>(&window, "leader_menu", false).get_value());
// TODO: should we decouple this from the flg manager and instead just check the unit type directly?
// If that's done so, we'd need to come up with a different check for Random availability.
gender_toggle_.set_members_enabled([this](const std::string& gender)->bool {
const std::vector<std::string>& genders = flg_manager_.choosable_genders();
return std::find(genders.begin(), genders.end(), gender) != genders.end();
});
update_leader_image(window);
}
void faction_select::on_gender_select(window& window)
{
flg_manager_.set_current_gender(gender_toggle_.get_active_member_value());
update_leader_image(window);
}
void faction_select::update_leader_image(window& window)
{
std::string leader_image = ng::random_enemy_picture;
if(const unit_type* ut = unit_types.find(flg_manager_.current_leader())) {
const unit_type& utg = ut->get_gender_unit_type(flg_manager_.current_gender());
leader_image = formatter() << utg.image() << "~RC(" << utg.flag_rgb() << ">" << tc_color_ << ")" << "~SCALE_INTO_SHARP(144,144)";
}
find_widget<image>(&window, "leader_image", false).set_label(leader_image);
}
} // namespace dialogs
} // namespace gui2