The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
mp_change_control.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2011 - 2025
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
21#include "formatter.hpp"
23#include "game_board.hpp"
24#include "game_display.hpp"
26#include "gui/widgets/label.hpp"
29#include "log.hpp"
30#include "menu_events.hpp"
32#include "team.hpp"
33
34#include <functional>
35
36static 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
42namespace gui2::dialogs
43{
44
45REGISTER_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>("sides_list");
60 listbox& nicks_list = find_widget<listbox>("nicks_list");
61
62 connect_signal_notify_modified(sides_list,
64
65 connect_signal_notify_modified(nicks_list,
67
68 //
69 // Initialize sides list
70 //
71 for(const team& t : menu_handler_.board().teams()) {
72 if(t.hidden()) {
73 continue;
74 }
75
76 const int side = sides_.emplace_back(t.side());
77
78 std::string side_str = VGETTEXT("Side $side", {{"side", std::to_string(side)}});
79 side_str = markup::span_color(team::get_font_color(side), side_str);
80
81 sides_list.add_row(widget_data{
82 { "side", {
83 { "id", (formatter() << "side_" << side).str() }, // TODO: need?
84 { "label", side_str },
85 { "use_markup", "true" }
86 }}
87 });
88 }
89
90 //
91 // Prepare set of available nicknames
92 //
93 std::set<std::string> temp_nicks;
94 for(const auto& team : menu_handler_.board().teams()) {
96 && !team.is_empty() && !team.current_player().empty())
97 {
98 temp_nicks.insert(team.current_player());
99 }
100 }
101
102 const std::set<std::string>& observers = game_display::get_singleton()->observers();
103 temp_nicks.insert(observers.begin(), observers.end());
104
105 // In case we are an observer, it isn't in the observers set and has to be added manually.
106 temp_nicks.insert(prefs::get().login());
107
108 //
109 // Initialize nick list
110 //
111 for(const std::string& nick : temp_nicks) {
112 nicks_.push_back(nick);
113
114 nicks_list.add_row(widget_data{
115 { "nick", {
116 { "id", nick },
117 { "label", nick },
118 { "use_markup", "true" },
119 }}
120 });
121 }
122
125}
126
128{
129 selected_side_ = find_widget<listbox>("sides_list").get_selected_row();
130
132}
133
135{
136 selected_nick_ = find_widget<listbox>("nicks_list").get_selected_row();
137}
138
140{
141 listbox& nicks_list = find_widget<listbox>("nicks_list");
142 const auto& teams = menu_handler_.board().teams();
143
144 int i = 0;
145 for(const std::string& nick : nicks_) {
146 std::string label_str = "";
147
148 if(selected_side_ <= static_cast<unsigned int>(teams.size()) && teams.at(selected_side_).current_player() == nick) {
149 label_str = markup::bold(nick);
150 } else {
151 label_str = nick;
152 }
153
154 grid* row_grid = nicks_list.get_row_grid(i);
155 row_grid->find_widget<label>(nick).set_label(label_str);
156
157 ++i;
158 }
159}
160
162{
163 if(get_retval() == retval::OK) {
164 DBG_GUI << "Main: changing control of side "
165 << sides_[selected_side_] << " to nick "
167
171 );
172 }
173}
174
175} // namespace dialogs
double t
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
virtual const std::set< std::string > & observers() const override
static game_display * get_singleton()
Abstract base class for all modal dialogs.
std::vector< std::string > nicks_
virtual void pre_show() override
Actions to be taken before showing the window.
virtual void post_show() override
Actions to be taken after the window has been shown.
Base container class.
Definition grid.hpp:32
The listbox class.
Definition listbox.hpp:41
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
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition listbox.cpp:256
virtual void set_label(const t_string &text)
T * find_widget(const std::string_view id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition widget.hpp:747
int get_retval()
Definition window.hpp:394
static prefs & get()
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:261
bool is_network_ai() const
Definition team.hpp:297
static color_t get_font_color(int side)
Definition team.cpp:955
bool is_local_ai() const
Definition team.hpp:295
bool is_idle() const
Definition team.hpp:311
bool is_empty() const
Definition team.hpp:286
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
This file contains the window object, this object is a top level container which has the event manage...
Standard logging facilities (interface).
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
#define DBG_GUI
static lg::log_domain log_gui("gui/dialogs/mp_change_control")
Handling of system events.
std::map< std::string, widget_item > widget_data
Definition widget.hpp:36
@ OK
Dialog was closed with the OK button.
Definition retval.hpp:35
std::string bold(Args &&... data)
Applies bold Pango markup to the input.
Definition markup.hpp:161
std::string span_color(const color_t &color, Args &&... data)
Applies Pango markup to the input specifying its display color.
Definition markup.hpp:110
#define i