The Battle for Wesnoth  1.17.0-dev
mp_change_control.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2021
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 "font/text_formatting.hpp"
20 #include "formatter.hpp"
21 #include "formula/string_utils.hpp"
22 #include "game_board.hpp"
23 #include "game_display.hpp"
26 #include "gui/widgets/label.hpp"
27 #include "gui/widgets/listbox.hpp"
28 #include "gui/widgets/settings.hpp"
29 #include "gui/widgets/window.hpp"
30 #include "log.hpp"
31 #include "menu_events.hpp"
32 #include "team.hpp"
33 
34 #include <functional>
35 
36 static lg::log_domain log_gui("gui/dialogs/mp_change_control");
37 #define ERR_GUI LOG_STREAM(err, log_gui)
38 #define WRN_GUI LOG_STREAM(warn, log_gui)
39 #define LOG_GUI LOG_STREAM(info, log_gui)
40 #define DBG_GUI LOG_STREAM(debug, log_gui)
41 
42 namespace gui2::dialogs
43 {
44 
45 REGISTER_DIALOG(mp_change_control)
46 
48  : menu_handler_(mh)
49  , selected_side_(0)
50  , selected_nick_(0)
51  , sides_()
52  , nicks_()
53 {
54 }
55 
57 {
58  listbox& sides_list = find_widget<listbox>(&window, "sides_list", false);
59  listbox& nicks_list = find_widget<listbox>(&window, "nicks_list", false);
60 
63 
66 
67  //
68  // Initialize sides list
69  //
70  const unsigned int num_sides = menu_handler_.board().teams().size();
71 
72  for(unsigned int side = 1; side <= num_sides; ++side) {
73  if(menu_handler_.board().get_team(side).hidden()) {
74  continue;
75  }
76 
77  sides_.push_back(side);
78 
79  std::map<std::string, string_map> data;
81 
82  std::string side_str = VGETTEXT("Side $side", {{"side", std::to_string(side)}});
83  side_str = font::span_color(team::get_side_color(side)) + side_str + "</span>";
84 
85  item["id"] = (formatter() << "side_" << side).str();
86  item["label"] = side_str;
87  item["use_markup"] = "true";
88  data.emplace("side", item);
89 
90  sides_list.add_row(data);
91  }
92 
93  //
94  // Prepare set of available nicknames
95  //
96  std::set<std::string> temp_nicks;
97  for(const auto& team : menu_handler_.board().teams()) {
98  if(!team.is_local_ai() && !team.is_network_ai() && !team.is_idle()
99  && !team.is_empty() && !team.current_player().empty())
100  {
101  temp_nicks.insert(team.current_player());
102  }
103  }
104 
105  const std::set<std::string>& observers = game_display::get_singleton()->observers();
106  temp_nicks.insert(observers.begin(), observers.end());
107 
108  // In case we are an observer, it isn't in the observers set and has to be added manually.
109  temp_nicks.insert(preferences::login());
110 
111  //
112  // Initialize nick list
113  //
114  for(const std::string& nick : temp_nicks) {
115  nicks_.push_back(nick);
116 
117  std::map<std::string, string_map> data;
119 
120  item["id"] = nick;
121  item["label"] = nick;
122  item["use_markup"] = "true";
123  data.emplace("nick", item);
124 
125  nicks_list.add_row(data);
126  }
127 
130 }
131 
133 {
134  selected_side_ = find_widget<listbox>(get_window(), "sides_list", false).get_selected_row();
135 
137 }
138 
140 {
141  selected_nick_ = find_widget<listbox>(get_window(), "nicks_list", false).get_selected_row();
142 }
143 
145 {
146  listbox& nicks_list = find_widget<listbox>(get_window(), "nicks_list", false);
147  const auto& teams = menu_handler_.board().teams();
148 
149  int i = 0;
150  for(const std::string& nick : nicks_) {
151  std::string label_str = "";
152 
153  if(selected_side_ <= static_cast<unsigned int>(teams.size()) && teams.at(selected_side_).current_player() == nick) {
154  label_str = formatter() << "<b>" << nick << "</b>";
155  } else {
156  label_str = nick;
157  }
158 
159  grid* row_grid = nicks_list.get_row_grid(i);
160  find_widget<label>(row_grid, nick, false).set_label(label_str);
161 
162  ++i;
163  }
164 }
165 
167 {
168  if(window.get_retval() == retval::OK) {
169  DBG_GUI << "Main: changing control of side "
170  << sides_[selected_side_] << " to nick "
171  << nicks_[selected_nick_] << std::endl;
172 
176  );
177  }
178 }
179 
180 } // namespace dialogs
bool is_local_ai() const
Definition: team.hpp:279
bool is_empty() const
Definition: team.hpp:270
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:84
This file contains the window object, this object is a top level container which has the event manage...
bool is_idle() const
Definition: team.hpp:296
void request_control_change(int side_num, const std::string &player)
bool is_network_ai() const
Definition: team.hpp:281
This shows the multiplayer change control dialog.
window * get_window() const
Returns a pointer to the dialog&#39;s window.
events::menu_handler & menu_handler_
game_board & board() const
The listbox class.
Definition: listbox.hpp:42
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
Base container class.
Definition: grid.hpp:30
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:71
team & get_team(int i)
Definition: game_board.hpp:96
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:186
This file contains the settings handling of the widget library.
std::ostringstream wrapper.
Definition: formatter.hpp:38
#define DBG_GUI
virtual const std::set< std::string > & observers() const override
std::vector< std::string > nicks_
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
bool hidden() const
Definition: team.hpp:359
int get_retval()
Definition: window.hpp:364
std::string login()
std::size_t i
Definition: function.cpp:940
const std::string & current_player() const
Definition: team.hpp:245
std::map< std::string, t_string > string_map
Definition: widget.hpp:25
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:67
static lg::log_domain log_gui("gui/dialogs/mp_change_control")
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
Handling of system events.
Definition: manager.hpp:42
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:238
static color_t get_side_color(int side)
Definition: team.cpp:960
Standard logging facilities (interface).
Dialog was closed with the OK button.
Definition: retval.hpp:34
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:409
static game_display * get_singleton()