/
multiplayerdialog.cpp
308 lines (256 loc) · 10.1 KB
/
multiplayerdialog.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/** @file multiplayerdialog.cpp Dialog for listing found servers and joining games.
*
* @authors Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "ui/dialogs/multiplayerdialog.h"
#include "network/serverlink.h"
#include "clientapp.h"
#include "dd_main.h"
#include "con_main.h"
#include "CommandAction"
#include "ui/widgets/taskbarwidget.h"
#include <de/charsymbols.h>
#include <de/SignalAction>
#include <de/SequentialLayout>
#include <de/ChildWidgetOrganizer>
#include <de/DocumentPopupWidget>
#include <de/ui/Item>
using namespace de;
DENG_GUI_PIMPL(MultiplayerDialog)
, DENG2_OBSERVES(ServerLink, DiscoveryUpdate)
, public ChildWidgetOrganizer::IWidgetFactory
{
static String hostId(serverinfo_t const &sv)
{
return String("%1:%2").arg(sv.address).arg(sv.port);
}
class ServerListItem : public ui::Item
{
public:
ServerListItem(serverinfo_t const &serverInfo)
{
setData(hostId(serverInfo));
std::memcpy(&_info, &serverInfo, sizeof(serverInfo));
}
serverinfo_t const &info() const
{
return _info;
}
void setInfo(serverinfo_t const &serverInfo)
{
std::memcpy(&_info, &serverInfo, sizeof(serverInfo));
notifyChange();
}
private:
serverinfo_t _info;
};
/**
* Widget representing a ServerListItem in the dialog's menu.
*/
struct ServerWidget : public GuiWidget
{
LabelWidget *title;
ButtonWidget *extra;
ButtonWidget *join;
QScopedPointer<SequentialLayout> layout;
DocumentPopupWidget *info;
struct JoinAction : public Action
{
public:
JoinAction(serverinfo_t const &sv, ButtonWidget *owner)
: _owner(owner)
{
_gameId = sv.gameIdentityKey;
_cmd = String("connect %1 %2").arg(sv.address).arg(sv.port);
}
void trigger()
{
Action::trigger();
BusyMode_FreezeGameForBusyMode();
// Closing the taskbar will cause this action to be deleted. Let's take
// ownership of the action so we can delete after we're done.
_owner->takeAction();
ClientWindow::main().taskBar().close();
App_ChangeGame(App_Games().byIdentityKey(_gameId), false /*no reload*/);
Con_Execute(CMDS_DDAY, _cmd.toLatin1(), false, false);
delete this;
}
Action *duplicate() const
{
DENG2_ASSERT(!"JoinAction: cannot duplicate");
return 0;
}
private:
ButtonWidget *_owner;
String _gameId;
String _cmd;
};
ServerWidget()
{
setBehavior(ContentClipping);
add(title = new LabelWidget);
add(extra = new ButtonWidget);
add(join = new ButtonWidget);
extra->setText(tr("..."));
join->setText(tr("Join"));
title->setSizePolicy(ui::Expand, ui::Expand);
title->setAppearanceAnimation(LabelWidget::AppearGrowVertically, 0.5);
title->setAlignment(ui::AlignTop);
title->setTextAlignment(ui::AlignRight);
title->setTextLineAlignment(ui::AlignLeft);
title->setImageAlignment(ui::AlignCenter);
title->setMaximumTextWidth(style().rules().rule("dialog.multiplayer.width").valuei());
extra->setSizePolicy(ui::Expand, ui::Expand);
join->setSizePolicy(ui::Expand, ui::Expand);
join->disable();
layout.reset(new SequentialLayout(rule().left(), rule().top(), ui::Right));
*layout << *title << *extra << *join;
rule().setSize(layout->width(), title->rule().height());
// Extra info popup.
info = new DocumentPopupWidget;
info->document().setMaximumLineWidth(style().rules().rule("dialog.multiplayer.width").valuei());
info->setAnchorAndOpeningDirection(extra->rule(), ui::Up);
add(info);
extra->setAction(new SignalAction(info, SLOT(open())));
}
void updateFromItem(ServerListItem const &item)
{
try
{
Game const &svGame = App_Games().byIdentityKey(item.info().gameIdentityKey);
if(style().images().has(svGame.logoImageId()))
{
title->setImage(style().images().image(svGame.logoImageId()));
}
serverinfo_t const &sv = item.info();
join->enable(sv.canJoin);
if(sv.canJoin)
{
join->setAction(new JoinAction(sv, join));
}
title->setText(String(_E(1) "%1 " _E(.)_E(2) "(%5/%6)" _E(.) "\n%2"
_E(D)_E(l) "\n%7 %4")
.arg(sv.name)
.arg(svGame.title())
.arg(sv.gameConfig)
.arg(sv.numPlayers)
.arg(sv.maxPlayers)
.arg(sv.map));
// Extra information.
#define TABBED(A, B) _E(Ta)_E(l) " " A _E(.) " " _E(\t) B "\n"
info->document()
.setText(String(_E(b) "%1" _E(.) "\n%2\n" _E(T`)
TABBED("Joinable:", "%5")
TABBED("Players:", "%3 / %4%13")
TABBED("Game:", "%9\n%10\n%12 %11")
TABBED("PWADs:", "%14")
TABBED("Address:", "%6:%7")
TABBED("Ping:", "%8 ms (approx)"))
.arg(sv.name)
.arg(sv.description)
.arg(sv.numPlayers)
.arg(sv.maxPlayers)
.arg(sv.canJoin? "Yes" : "No") // 5
.arg(sv.address)
.arg(sv.port)
.arg(sv.ping)
.arg(sv.plugin)
.arg(sv.gameIdentityKey) // 10
.arg(sv.gameConfig)
.arg(sv.map)
.arg(!String(sv.clientNames).isEmpty()? String(_E(2) " (%1)" _E(.)).arg(sv.clientNames) : "")
.arg(String(sv.pwads).isEmpty()? String(DENG2_CHAR_MDASH) : String(sv.pwads))); // 14
#undef TABBED
}
catch(Error const &)
{
/// @todo
}
}
};
MenuWidget *list;
Instance(Public *i) : Base(i)
{
self.area().add(list = new MenuWidget);
// Configure the server list widet.
list->setGridSize(1, ui::Expand, 0, ui::Expand);
list->organizer().setWidgetFactory(*this);
link().audienceForDiscoveryUpdate += this;
}
~Instance()
{
link().audienceForDiscoveryUpdate -= this;
}
GuiWidget *makeItemWidget(ui::Item const &item, GuiWidget const *)
{
return new ServerWidget;
}
void updateItemWidget(GuiWidget &widget, ui::Item const &item)
{
widget.as<ServerWidget>().updateFromItem(item.as<ServerListItem>());
}
void linkDiscoveryUpdate(ServerLink const &link)
{
// Remove obsolete entries.
for(ui::Data::Pos idx = 0; idx < list->items().size(); ++idx)
{
String const id = list->items().at(idx).data().toString();
if(!link.isFound(Address::parse(id)))
{
list->items().remove(idx--);
}
}
// Add new entries and update existing ones.
foreach(de::Address const &host, link.foundServers())
{
serverinfo_t info;
if(!link.foundServerInfo(host, &info)) continue;
ui::Data::Pos found = list->items().findData(hostId(info));
if(found == ui::Data::InvalidPos)
{
// Needs to be added.
list->items().append(new ServerListItem(info));
}
else
{
// Update the info.
list->items().at(found).as<ServerListItem>().setInfo(info);
}
}
}
static ServerLink &link()
{
return ClientApp::serverLink();
}
};
MultiplayerDialog::MultiplayerDialog(String const &name)
: DialogWidget(name, WithHeading), d(new Instance(this))
{
heading().setText(tr("Multiplayer"));
LabelWidget *lab = LabelWidget::newWithText(tr("Games from Master Server and local network:"), &area());
GridLayout layout(area().contentRule().left(), area().contentRule().top());
layout.setGridSize(1, 0);
//layout.setColumnAlignment(0, ui::AlignRight);
layout << *lab << *d->list;
area().setContentSize(layout.width(), layout.height());
buttons()
<< new DialogButtonItem(DialogWidget::Default | DialogWidget::Accept, tr("Close"))
<< new DialogButtonItem(DialogWidget::Action, style().images().image("gear"),
new SignalAction(this, SLOT(showSettings())));
}
void MultiplayerDialog::showSettings()
{
}