-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
mp_connect.cpp
194 lines (153 loc) · 4.11 KB
/
mp_connect.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
/*
Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of 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/mp_connect.hpp"
#include "preferences/game.hpp"
#include "gui/auxiliary/field.hpp"
#include "gui/dialogs/modal_dialog.hpp"
#include "gui/widgets/button.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/settings.hpp"
#include "utils/functional.hpp"
namespace gui2
{
namespace dialogs
{
namespace
{
/*WIKI
* @page = GUIWindowDefinitionWML
* @order = 2_mp_server_list
*
* == Multiplayer server list ==
*
* This shows the dialog with a list of predefined multiplayer servers.
*
* @begin{table}{dialog_widgets}
*
* server_list & & listbox & m &
* Listbox with the predefined servers to connect to. $
*
* -name & & styled_widget & o &
* Widget which shows the name of the server. $
*
* -address & & styled_widget & m &
* The address/host_name of the server. $
*
* @end{table}
*/
class mp_server_list : public modal_dialog
{
public:
mp_server_list() : host_name_()
{
}
const std::string& host_name() const
{
return host_name_;
}
private:
std::string host_name_;
/** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const override;
/** Inherited from modal_dialog. */
virtual void pre_show(window& window) override;
/** Inherited from modal_dialog. */
virtual void post_show(window& window) override;
};
REGISTER_DIALOG(mp_server_list)
void mp_server_list::pre_show(window& window)
{
set_restore(true);
listbox& list = find_widget<listbox>(&window, "server_list", false);
window.keyboard_capture(&list);
const std::vector<game_config::server_info>& pref_servers
= preferences::server_list();
for(const auto & server : pref_servers)
{
std::map<std::string, string_map> data;
string_map item;
item["label"] = server.name;
data.emplace("name", item);
item["label"] = server.address;
data.emplace("address", item);
list.add_row(data);
}
}
void mp_server_list::post_show(window& window)
{
if(get_retval() == window::OK) {
const listbox& list
= find_widget<const listbox>(&window, "server_list", false);
const grid* row = list.get_row_grid(list.get_selected_row());
assert(row);
host_name_ = find_widget<const styled_widget>(row, "address", false).get_label();
}
}
} // namespace
/*WIKI
* @page = GUIWindowDefinitionWML
* @order = 2_mp_connect
*
* == Multiplayer connect ==
*
* This shows the dialog to the MP server to connect to.
*
* @begin{table}{dialog_widgets}
*
* start_table & & text_box & m &
* The name of the server to connect to. $
*
* list & & button & o &
* Shows a dialog with a list of predefined servers to connect to. $
*
* @end{table}
*/
REGISTER_DIALOG(mp_connect)
static void
show_server_list(window& window, field_text* host_name)
{
assert(host_name);
mp_server_list dlg;
dlg.show();
if(dlg.get_retval() == window::OK) {
host_name->set_widget_value(window, dlg.host_name());
}
}
mp_connect::mp_connect()
: host_name_(register_text("host_name",
true,
preferences::network_host,
preferences::set_network_host,
true))
{
set_restore(true);
}
void mp_connect::pre_show(window& win)
{
assert(host_name_);
// Set view list callback button.
if(button* btn = find_widget<button>(&win, "list", false, false)) {
connect_signal_mouse_left_click(*btn,
std::bind(show_server_list, std::ref(win), host_name_));
}
}
modal_dialog* mp_connect::mp_server_list_for_unit_test()
{
return new mp_server_list();
}
} // namespace dialogs
} // namespace gui2