The Battle for Wesnoth  1.19.0-dev
mp_change_control.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2024
3  by Lukasz Dobrogowski <lukasz.dobrogowski@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 "font/text_formatting.hpp"
21 #include "formatter.hpp"
22 #include "formula/string_utils.hpp"
23 #include "game_board.hpp"
24 #include "game_display.hpp"
27 #include "gui/widgets/label.hpp"
28 #include "gui/widgets/listbox.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  : modal_dialog(window_id())
49  , menu_handler_(mh)
50  , selected_side_(0)
51  , selected_nick_(0)
52  , sides_()
53  , nicks_()
54 {
55 }
56 
58 {
59  listbox& sides_list = find_widget<listbox>(&window, "sides_list", false);
60  listbox& nicks_list = find_widget<listbox>(&window, "nicks_list", false);
61 
64 
67 
68  //
69  // Initialize sides list
70  //
71  const unsigned int num_sides = menu_handler_.board().teams().size();
72 
73  for(unsigned int side = 1; side <= num_sides; ++side) {
74  if(menu_handler_.board().get_team(side).hidden()) {
75  continue;
76  }
77 
78  sides_.push_back(side);
79 
82 
83  std::string side_str = VGETTEXT("Side $side", {{"side", std::to_string(side)}});
84  side_str = font::span_color(team::get_side_color(side)) + side_str + "</span>";
85 
86  item["id"] = (formatter() << "side_" << side).str();
87  item["label"] = side_str;
88  item["use_markup"] = "true";
89  data.emplace("side", item);
90 
91  sides_list.add_row(data);
92  }
93 
94  //
95  // Prepare set of available nicknames
96  //
97  std::set<std::string> temp_nicks;
98  for(const auto& team : menu_handler_.board().teams()) {
99  if(!team.is_local_ai() && !team.is_network_ai() && !team.is_idle()
100  && !team.is_empty() && !team.current_player().empty())
101  {
102  temp_nicks.insert(team.current_player());
103  }
104  }
105 
106  const std::set<std::string>& observers = game_display::get_singleton()->observers();
107  temp_nicks.insert(observers.begin(), observers.end());
108 
109  // In case we are an observer, it isn't in the observers set and has to be added manually.
110  temp_nicks.insert(preferences::login());
111 
112  //
113  // Initialize nick list
114  //
115  for(const std::string& nick : temp_nicks) {
116  nicks_.push_back(nick);
117 
120 
121  item["id"] = nick;
122  item["label"] = nick;
123  item["use_markup"] = "true";
124  data.emplace("nick", item);
125 
126  nicks_list.add_row(data);
127  }
128 
131 }
132 
134 {
135  selected_side_ = find_widget<listbox>(get_window(), "sides_list", false).get_selected_row();
136 
138 }
139 
141 {
142  selected_nick_ = find_widget<listbox>(get_window(), "nicks_list", false).get_selected_row();
143 }
144 
146 {
147  listbox& nicks_list = find_widget<listbox>(get_window(), "nicks_list", false);
148  const auto& teams = menu_handler_.board().teams();
149 
150  int i = 0;
151  for(const std::string& nick : nicks_) {
152  std::string label_str = "";
153 
154  if(selected_side_ <= static_cast<unsigned int>(teams.size()) && teams.at(selected_side_).current_player() == nick) {
155  label_str = formatter() << "<b>" << nick << "</b>";
156  } else {
157  label_str = nick;
158  }
159 
160  grid* row_grid = nicks_list.get_row_grid(i);
161  find_widget<label>(row_grid, nick, false).set_label(label_str);
162 
163  ++i;
164  }
165 }
166 
168 {
169  if(window.get_retval() == retval::OK) {
170  DBG_GUI << "Main: changing control of side "
171  << sides_[selected_side_] << " to nick "
173 
177  );
178  }
179 }
180 
181 } // namespace dialogs
game_board & board() const
void request_control_change(int side_num, const std::string &player)
std::ostringstream wrapper.
Definition: formatter.hpp:40
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:79
team & get_team(int i)
Definition: game_board.hpp:91
virtual const std::set< std::string > & observers() const override
static game_display * get_singleton()
Abstract base class for all modal dialogs.
window * get_window()
Returns a pointer to the dialog's window.
This shows the multiplayer change control dialog.
std::vector< std::string > nicks_
events::menu_handler & menu_handler_
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.
Base container class.
Definition: grid.hpp:32
The listbox class.
Definition: listbox.hpp:43
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:59
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:230
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
int get_retval()
Definition: window.hpp:406
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
const std::string & current_player() const
Definition: team.hpp:220
bool is_network_ai() const
Definition: team.hpp:256
bool is_local_ai() const
Definition: team.hpp:254
bool is_idle() const
Definition: team.hpp:270
static color_t get_side_color(int side)
Definition: team.cpp:959
bool hidden() const
Definition: team.hpp:333
bool is_empty() const
Definition: team.hpp:245
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
std::size_t i
Definition: function.cpp:968
This file contains the window object, this object is a top level container which has the event manage...
Standard logging facilities (interface).
#define DBG_GUI
static lg::log_domain log_gui("gui/dialogs/mp_change_control")
Handling of system events.
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
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:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
std::string login()
std::string_view data
Definition: picture.cpp:194