Starshatter_Open
Open source Starshatter engine
Main Page
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
NetLobbyDlg.cpp
Go to the documentation of this file.
1
/* Project Starshatter 4.5
2
Destroyer Studios LLC
3
Copyright © 1997-2004. All Rights Reserved.
4
5
SUBSYSTEM: Stars.exe
6
FILE: NetLobbyDlg.cpp
7
AUTHOR: John DiCamillo
8
9
10
OVERVIEW
11
========
12
Main Menu Dialog Active Window class
13
*/
14
15
#include "
MemDebug.h
"
16
#include "
NetLobbyDlg.h
"
17
#include "
NetUnitDlg.h
"
18
#include "
NetClientConfig.h
"
19
#include "
MenuScreen.h
"
20
#include "
Starshatter.h
"
21
#include "
Ship.h
"
22
#include "
Player.h
"
23
#include "
Campaign.h
"
24
25
#include "
NetAddr.h
"
26
#include "
NetLobbyClient.h
"
27
#include "
NetLobbyServer.h
"
28
#include "
NetUser.h
"
29
#include "
NetChat.h
"
30
31
#include "
DataLoader.h
"
32
#include "
Video.h
"
33
#include "
Keyboard.h
"
34
#include "
MachineInfo.h
"
35
36
// +--------------------------------------------------------------------+
37
// DECLARE MAPPING FUNCTIONS:
38
39
DEF_MAP_CLIENT
(
NetLobbyDlg
, OnCampaignSelect);
40
DEF_MAP_CLIENT
(
NetLobbyDlg
, OnMissionSelect);
41
DEF_MAP_CLIENT
(
NetLobbyDlg
, OnApply);
42
DEF_MAP_CLIENT
(
NetLobbyDlg
, OnCancel);
43
44
// +--------------------------------------------------------------------+
45
46
NetLobbyDlg::NetLobbyDlg
(
Screen
* s,
FormDef
& def,
MenuScreen
* mgr)
47
:
FormWindow
(s, 0, 0, s->Width(), s->Height()), manager(mgr),
48
net_lobby(0)
49
{
50
selected_campaign
= 0;
51
selected_mission
= 0;
52
last_chat
= 0;
53
host_mode
=
false
;
54
55
Init
(def);
56
}
57
58
NetLobbyDlg::~NetLobbyDlg
()
59
{
60
}
61
62
// +--------------------------------------------------------------------+
63
64
void
65
NetLobbyDlg::RegisterControls
()
66
{
67
lst_campaigns
= (
ComboBox
*)
FindControl
(200);
68
REGISTER_CLIENT
(
EID_SELECT
,
lst_campaigns
,
NetLobbyDlg
,
OnCampaignSelect
);
69
70
lst_missions
= (
ListBox
*)
FindControl
(201);
71
REGISTER_CLIENT
(
EID_SELECT
,
lst_missions
,
NetLobbyDlg
,
OnMissionSelect
);
72
73
txt_desc
=
FindControl
(202);
74
lst_players
= (
ListBox
*)
FindControl
(210);
75
lst_chat
= (
ListBox
*)
FindControl
(211);
76
edt_chat
= (
EditBox
*)
FindControl
(212);
77
78
if
(
edt_chat
)
79
edt_chat
->
SetText
(
""
);
80
81
apply
= (
Button
*)
FindControl
(1);
82
REGISTER_CLIENT
(
EID_CLICK
,
apply
,
NetLobbyDlg
,
OnApply
);
83
84
cancel
= (
Button
*)
FindControl
(2);
85
REGISTER_CLIENT
(
EID_CLICK
,
cancel
,
NetLobbyDlg
,
OnCancel
);
86
}
87
88
// +--------------------------------------------------------------------+
89
90
void
91
NetLobbyDlg::Show
()
92
{
93
if
(!
IsShown
()) {
94
// clear server data:
95
if
(
lst_chat
)
lst_chat
->
ClearItems
();
96
if
(
lst_campaigns
)
lst_campaigns
->
ClearItems
();
97
if
(
lst_missions
)
lst_missions
->
ClearItems
();
98
if
(
txt_desc
)
txt_desc
->
SetText
(
""
);
99
if
(
apply
)
apply
->
SetEnabled
(
false
);
100
101
if
(
lst_missions
) {
102
lst_missions
->
SetSelectedStyle
(
ListBox::LIST_ITEM_STYLE_FILLED_BOX
);
103
lst_missions
->
SetLeading
(2);
104
}
105
106
selected_campaign
= 0;
107
selected_mission
= 0;
108
last_chat
= 0;
109
110
FormWindow::Show
();
111
112
net_lobby
=
NetLobby::GetInstance
();
113
114
if
(!
net_lobby
) {
115
Starshatter
* stars =
Starshatter::GetInstance
();
116
if
(stars)
117
stars->
StartLobby
();
118
119
net_lobby
=
NetLobby::GetInstance
();
120
}
121
122
if
(
net_lobby
) {
123
if
(
net_lobby
->
IsServer
()) {
124
host_mode
=
true
;
125
126
NetUser
* user =
net_lobby
->
GetLocalUser
();
127
128
if
(!user) {
129
Player
* player =
Player::GetCurrentPlayer
();
130
if
(player) {
131
user =
new
(__FILE__,__LINE__)
NetUser
(player);
132
user->
SetHost
(
true
);
133
}
134
else
{
135
::Print
(
"NetLobbyDlg::Show() Host mode - no current player?\n"
);
136
}
137
}
138
139
net_lobby
->
SetLocalUser
(user);
140
}
141
142
SelectMission
();
143
}
144
}
145
}
146
147
// +--------------------------------------------------------------------+
148
149
void
150
NetLobbyDlg::ExecFrame
()
151
{
152
ExecLobbyFrame
();
153
154
if
(
Keyboard::KeyDown
(VK_RETURN)) {
155
if
(
edt_chat
&&
edt_chat
->
GetText
().
length
() > 0) {
156
SendChat
(
edt_chat
->
GetText
());
157
edt_chat
->
SetText
(
""
);
158
}
159
}
160
161
GetPlayers
();
162
GetChat
();
163
164
if
(
lst_campaigns
) {
165
if
(
lst_campaigns
->
NumItems
() < 1)
166
GetMissions
();
167
else
168
GetSelectedMission
();
169
}
170
}
171
172
// +--------------------------------------------------------------------+
173
174
void
175
NetLobbyDlg::ExecLobbyFrame
()
176
{
177
if
(
net_lobby
&&
net_lobby
->
GetLastError
() != 0) {
178
if
(
net_lobby
->
IsClient
()) {
179
Starshatter
* stars =
Starshatter::GetInstance
();
180
if
(stars)
181
stars->
StopLobby
();
182
183
net_lobby
= 0;
184
manager
->
ShowNetClientDlg
();
185
}
186
}
187
}
188
189
// +--------------------------------------------------------------------+
190
191
void
192
NetLobbyDlg::GetPlayers
()
193
{
194
if
(!
lst_players
)
return
;
195
196
if
(
net_lobby
) {
197
lst_players
->
ClearItems
();
198
199
NetUser
* u =
net_lobby
->
GetLocalUser
();
200
if
(u) {
201
Text
name =
Player::RankAbrv
(u->
Rank
());
202
name +=
" "
;
203
name += u->
Name
();
204
205
int
count =
lst_players
->
AddItem
(u->
IsHost
() ?
"*"
:
" "
);
206
lst_players
->
SetItemText
(count-1, 1, name);
207
host_mode
=
true
;
208
}
209
210
ListIter<NetUser>
iter =
net_lobby
->
GetUsers
();
211
while
(++iter) {
212
NetUser
* u = iter.
value
();
213
int
count =
lst_players
->
AddItem
(u->
IsHost
() ?
"*"
:
" "
);
214
215
Text
name =
Player::RankAbrv
(u->
Rank
());
216
name +=
" "
;
217
name += u->
Name
();
218
219
lst_players
->
SetItemText
(count-1, 1, name);
220
221
if
(
Player::GetCurrentPlayer
()->Name() == u->
Name
())
222
host_mode
= u->
IsHost
();
223
}
224
}
225
}
226
227
// +--------------------------------------------------------------------+
228
229
void
230
NetLobbyDlg::GetChat
()
231
{
232
if
(!
lst_chat
)
return
;
233
234
if
(
net_lobby
) {
235
int
last_item =
lst_chat
->
NumItems
() - 1;
236
int
count = 0;
237
bool
added =
false
;
238
239
ListIter<NetChatEntry>
iter =
net_lobby
->
GetChat
();
240
while
(++iter) {
241
NetChatEntry
* c = iter.
value
();
242
243
if
(count++ > last_item) {
244
int
n =
lst_chat
->
AddItem
(c->
GetUser
());
245
lst_chat
->
SetItemText
(n-1, 1, c->
GetMessage
());
246
added =
true
;
247
}
248
}
249
250
if
(added)
251
lst_chat
->
EnsureVisible
(
lst_chat
->
NumItems
()+1);
252
}
253
}
254
255
void
256
NetLobbyDlg::SendChat
(
Text
msg)
257
{
258
if
(msg.
length
() < 1)
return
;
259
260
Player
* player =
Player::GetCurrentPlayer
();
261
262
if
(msg[0] >=
'0'
&& msg[0] <=
'9'
) {
263
if
(player) {
264
Text
macro = player->
ChatMacro
(msg[0] -
'0'
);
265
266
if
(macro.
length
())
267
msg = macro;
268
}
269
}
270
271
if
(
net_lobby
)
272
net_lobby
->
AddChat
(
net_lobby
->
GetLocalUser
(), msg);
273
}
274
275
// +--------------------------------------------------------------------+
276
277
void
278
NetLobbyDlg::GetMissions
()
279
{
280
if
(!
lst_campaigns
|| !
lst_missions
)
281
return
;
282
283
if
(
net_lobby
) {
284
lst_campaigns
->
ClearItems
();
285
286
List<NetCampaignInfo>
& campaigns =
net_lobby
->
GetCampaigns
();
287
288
if
(campaigns.
size
()) {
289
ListIter<NetCampaignInfo>
c_iter = campaigns;
290
while
(++c_iter) {
291
NetCampaignInfo
* c = c_iter.
value
();
292
lst_campaigns
->
AddItem
(c->
name
);
293
}
294
295
lst_campaigns
->
SetSelection
(0);
296
NetCampaignInfo
* c = campaigns[0];
297
298
lst_missions
->
ClearItems
();
299
300
ListIter<MissionInfo>
m_iter = c->
missions
;
301
while
(++m_iter) {
302
MissionInfo
* m = m_iter.
value
();
303
lst_missions
->
AddItem
(m->
name
);
304
}
305
}
306
}
307
}
308
309
// +--------------------------------------------------------------------+
310
311
void
312
NetLobbyDlg::GetSelectedMission
()
313
{
314
if
(!
lst_campaigns
|| !
lst_missions
)
return
;
315
316
if
(
net_lobby
) {
317
if
(
net_lobby
->
GetSelectedMissionID
()) {
318
int
id
=
net_lobby
->
GetSelectedMissionID
();
319
320
selected_campaign
=
id
>>
NET_CAMPAIGN_SHIFT
;
321
selected_mission
=
id
&
NET_MISSION_MASK
;
322
323
List<NetCampaignInfo>
& campaigns =
net_lobby
->
GetCampaigns
();
324
325
for
(
int
i = 0; i < campaigns.
size
(); i++) {
326
NetCampaignInfo
* c = campaigns[i];
327
328
if
(c->
id
==
selected_campaign
) {
329
lst_campaigns
->
SetSelection
(i);
330
OnCampaignSelect
(0);
331
332
for
(
int
j = 0; j < c->
missions
.
size
(); j++) {
333
MissionInfo
* m = c->
missions
[j];
334
335
if
(m->
id
==
selected_mission
) {
336
lst_missions
->
SetSelected
(j);
337
OnMissionSelect
(0);
338
}
339
}
340
}
341
}
342
}
343
else
if
(
selected_campaign
) {
344
selected_campaign
= 0;
345
selected_mission
= 0;
346
}
347
348
lst_campaigns
->
SetEnabled
(
selected_campaign
== 0);
349
lst_missions
->
SetEnabled
(
selected_mission
== 0);
350
351
if
(!
host_mode
)
352
apply
->
SetEnabled
(
selected_mission
!= 0);
353
}
354
}
355
356
// +--------------------------------------------------------------------+
357
358
void
359
NetLobbyDlg::SelectMission
()
360
{
361
if
(!
lst_campaigns
|| !
lst_missions
)
return
;
362
363
bool
selected =
false
;
364
365
if
(
net_lobby
) {
366
int
c_index =
lst_campaigns
->
GetSelectedIndex
();
367
int
m_index =
lst_missions
->
GetSelection
();
368
369
List<NetCampaignInfo>
& campaigns =
net_lobby
->
GetCampaigns
();
370
371
if
(c_index >= 0 && c_index < campaigns.
size
() && m_index >= 0) {
372
NetCampaignInfo
* c = campaigns[c_index];
373
374
if
(m_index < c->missions.size()) {
375
MissionInfo
* m = c->
missions
[m_index];
376
377
DWORD
id
= (c->
id
<<
NET_CAMPAIGN_SHIFT
) +
378
(m->
id
&
NET_MISSION_MASK
);
379
380
net_lobby
->
SelectMission
(
id
);
381
selected =
true
;
382
}
383
}
384
385
if
(!selected)
386
net_lobby
->
SelectMission
(0);
387
}
388
}
389
390
// +--------------------------------------------------------------------+
391
392
void
393
NetLobbyDlg::OnCampaignSelect
(
AWEvent
* event)
394
{
395
if
(
net_lobby
) {
396
List<NetCampaignInfo>
& campaigns =
net_lobby
->
GetCampaigns
();
397
398
if
(
lst_campaigns
&&
lst_missions
&& campaigns.
size
()) {
399
int
index =
lst_campaigns
->
GetSelectedIndex
();
400
401
if
(index >= 0 && index < campaigns.
size
()) {
402
NetCampaignInfo
* c = campaigns[index];
403
404
lst_missions
->
ClearItems
();
405
txt_desc
->
SetText
(
""
);
406
407
ListIter<MissionInfo>
iter = c->
missions
;
408
while
(++iter) {
409
MissionInfo
* m = iter.
value
();
410
lst_missions
->
AddItem
(m->
name
);
411
}
412
}
413
}
414
}
415
}
416
417
void
418
NetLobbyDlg::OnMissionSelect
(
AWEvent
* event)
419
{
420
if
(
net_lobby
) {
421
List<NetCampaignInfo>
& campaigns =
net_lobby
->
GetCampaigns
();
422
423
if
(
lst_campaigns
&&
lst_missions
&&
txt_desc
&& campaigns.
size
()) {
424
txt_desc
->
SetText
(
""
);
425
426
if
(
host_mode
&&
apply
)
427
apply
->
SetEnabled
(
false
);
428
429
int
c_index =
lst_campaigns
->
GetSelectedIndex
();
430
int
m_index =
lst_missions
->
GetSelection
();
431
432
if
(c_index >= 0 && c_index < campaigns.
size
()) {
433
NetCampaignInfo
* c = campaigns[c_index];
434
435
if
(m_index >= 0 && m_index < c->missions.size()) {
436
MissionInfo
* m = c->
missions
[m_index];
437
txt_desc
->
SetText
(m->
description
);
438
439
if
(
host_mode
&&
apply
)
440
apply
->
SetEnabled
(
true
);
441
}
442
}
443
}
444
}
445
}
446
447
// +--------------------------------------------------------------------+
448
449
void
450
NetLobbyDlg::OnApply
(
AWEvent
* event)
451
{
452
if
(
host_mode
)
453
SelectMission
();
454
455
manager
->
ShowNetUnitDlg
();
456
457
NetUnitDlg
* unit_dlg =
manager
->
GetNetUnitDlg
();
458
if
(unit_dlg)
459
unit_dlg->
SetHostMode
(
host_mode
);
460
}
461
462
void
463
NetLobbyDlg::OnCancel
(
AWEvent
* event)
464
{
465
if
(
net_lobby
) {
466
net_lobby
->
SelectMission
(0);
467
net_lobby
= 0;
468
469
Starshatter
* stars =
Starshatter::GetInstance
();
470
if
(stars)
471
stars->
StopLobby
();
472
}
473
474
manager
->
ShowNetClientDlg
();
475
}
Stars45
NetLobbyDlg.cpp
Generated on Tue Jun 5 2012 20:47:05 for Starshatter_Open by
1.8.1