/
lobby_main.cpp
1873 lines (1690 loc) · 53.8 KB
/
lobby_main.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2009 - 2014 by Tomasz Sniatowski <kailoran@gmail.com>
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/lobby_main.hpp"
#include "gui/dialogs/lobby_player_info.hpp"
#include "gui/dialogs/field.hpp"
#include "gui/dialogs/helper.hpp"
#include "gui/auxiliary/log.hpp"
#include "gui/auxiliary/timer.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/image.hpp"
#include "gui/widgets/label.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/minimap.hpp"
#include "gui/widgets/multi_page.hpp"
#include "gui/widgets/scroll_label.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/text_box.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/toggle_panel.hpp"
#include "gui/widgets/tree_view_node.hpp"
#include "formula_string_utils.hpp"
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "lobby_preferences.hpp"
#include "log.hpp"
#include "network.hpp"
#include "playmp_controller.hpp"
#include "preferences_display.hpp"
#include "mp_ui_alerts.hpp"
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
static lg::log_domain log_network("network");
#define DBG_NW LOG_STREAM(debug, log_network)
#define LOG_NW LOG_STREAM(info, log_network)
#define ERR_NW LOG_STREAM(err, log_network)
static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
#define ERR_NG LOG_STREAM(err, log_engine)
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
static lg::log_domain log_lobby("lobby");
#define DBG_LB LOG_STREAM(debug, log_lobby)
#define LOG_LB LOG_STREAM(info, log_lobby)
#define ERR_LB LOG_STREAM(err, log_lobby)
#define SCOPE_LB log_scope2(log_lobby, __func__)
namespace gui2
{
REGISTER_DIALOG(lobby_main)
void tsub_player_list::init(gui2::twindow& w, const std::string& id)
{
list = find_widget<tlistbox>(&w, id, false, true);
show_toggle
= find_widget<ttoggle_button>(&w, id + "_show_toggle", false, true);
show_toggle->set_icon_name("lobby/group-expanded.png");
show_toggle->set_callback_state_change(
boost::bind(&tsub_player_list::show_toggle_callback, this, _1));
count = find_widget<tlabel>(&w, id + "_count", false, true);
label = find_widget<tlabel>(&w, id + "_label", false, true);
ttree_view& parent_tree = find_widget<ttree_view>(&w, "player_tree", false);
string_map tree_group_field;
std::map<std::string, string_map> tree_group_item;
tree_group_field["label"] = id;
tree_group_item["tree_view_node_label"] = tree_group_field;
tree = &parent_tree.add_node("player_group", tree_group_item);
tree_label = find_widget<tlabel>(tree, "tree_view_node_label", false, true);
tree_label->set_label(label->label());
}
void tsub_player_list::show_toggle_callback(gui2::twidget& /*widget*/)
{
if(show_toggle->get_value()) {
list->set_visible(twidget::tvisible::invisible);
show_toggle->set_icon_name("lobby/group-folded.png");
} else {
list->set_visible(twidget::tvisible::visible);
show_toggle->set_icon_name("lobby/group-expanded.png");
}
}
void tsub_player_list::auto_hide()
{
assert(tree);
assert(tree_label);
if(tree->empty()) {
/**
* @todo Make sure setting visible resizes the widget.
*
* It doesn't work here since invalidate_layout is blocked, but the
* widget should also be able to handle it itself. Once done the
* setting of the label text can also be removed.
*/
assert(label);
tree_label->set_label(label->label() + " (0)");
// tree_label->set_visible(twidget::tvisible::invisible);
} else {
assert(label);
std::stringstream ss;
ss << label->label() << " (" << tree->size() << ")";
tree_label->set_label(ss.str());
// tree_label->set_visible(twidget::tvisible::visible);
}
}
void tplayer_list::init(gui2::twindow& w)
{
active_game.init(w, "active_game");
active_room.init(w, "active_room");
other_rooms.init(w, "other_rooms");
other_games.init(w, "other_games");
sort_by_name = find_widget<ttoggle_button>(
&w, "player_list_sort_name", false, true);
sort_by_relation = find_widget<ttoggle_button>(
&w, "player_list_sort_relation", false, true);
tree = find_widget<ttree_view>(&w, "player_tree", false, true);
find_widget<twidget>(&w, "old_player_list", false)
.set_visible(twidget::tvisible::invisible);
/**
* @todo This is a hack to fold the items.
*
* This hack can be removed when folding is properly implemented.
*/
assert(active_room.tree);
find_widget<ttoggle_button>(active_room.tree, "tree_view_node_icon", false)
.set_value(true);
assert(other_rooms.tree);
find_widget<ttoggle_button>(other_rooms.tree, "tree_view_node_icon", false)
.set_value(true);
assert(other_games.tree);
find_widget<ttoggle_button>(other_games.tree, "tree_view_node_icon", false)
.set_value(true);
}
void tplayer_list::update_sort_icons()
{
if(sort_by_name->get_value()) {
sort_by_name->set_icon_name("lobby/sort-az.png");
} else {
sort_by_name->set_icon_name("lobby/sort-az-off.png");
}
if(sort_by_relation->get_value()) {
sort_by_relation->set_icon_name("lobby/sort-friend.png");
} else {
sort_by_relation->set_icon_name("lobby/sort-friend-off.png");
}
}
void tlobby_main::send_chat_message(const std::string& message,
bool /*allies_only*/)
{
config data, msg;
msg["message"] = message;
msg["sender"] = preferences::login();
data.add_child("message", msg);
add_chat_message(time(NULL), preferences::login(), 0, message); // local
// echo
network::send_data(data, 0);
}
void tlobby_main::user_relation_changed(const std::string& /*name*/)
{
player_list_dirty_ = true;
}
void tlobby_main::add_chat_message(const time_t& /*time*/,
const std::string& speaker,
int /*side*/,
const std::string& message,
events::chat_handler::MESSAGE_TYPE /*type*/)
{
std::stringstream ss;
ss << "<" << speaker << "> ";
ss << message;
append_to_chatbox(ss.str());
}
void tlobby_main::add_whisper_sent(const std::string& receiver,
const std::string& message)
{
if(whisper_window_active(receiver)) {
add_active_window_message(preferences::login(), message);
} else if(tlobby_chat_window* t = whisper_window_open(
receiver, preferences::auto_open_whisper_windows())) {
switch_to_window(t);
add_active_window_message(preferences::login(), message);
} else {
utils::string_map symbols;
symbols["receiver"] = receiver;
add_active_window_whisper(VGETTEXT("whisper to $receiver", symbols),
message);
}
lobby_info_.get_whisper_log(receiver)
.add_message(preferences::login(), message);
}
void tlobby_main::add_whisper_received(const std::string& sender,
const std::string& message)
{
bool can_go_to_active = !preferences::whisper_friends_only()
|| preferences::is_friend(sender);
bool can_open_new = preferences::auto_open_whisper_windows()
&& can_go_to_active;
lobby_info_.get_whisper_log(sender).add_message(sender, message);
if(whisper_window_open(sender, can_open_new)) {
if(whisper_window_active(sender)) {
add_active_window_message(sender, message);
do_notify(NOTIFY_WHISPER, sender, message);
} else {
add_whisper_window_whisper(sender, message);
increment_waiting_whsipers(sender);
do_notify(NOTIFY_WHISPER_OTHER_WINDOW, sender, message);
}
} else if(can_go_to_active) {
add_active_window_whisper(sender, message);
do_notify(NOTIFY_WHISPER, sender, message);
} else {
LOG_LB << "Ignoring whisper from " << sender << "\n";
}
}
void tlobby_main::add_chat_room_message_sent(const std::string& room,
const std::string& message)
{
// do not open room window here, player should be in the room before sending
// messages yo it should be allowed to happen
if(tlobby_chat_window* t = room_window_open(room, false)) {
room_info* ri = lobby_info_.get_room(room);
assert(ri);
if(!room_window_active(room)) {
switch_to_window(t);
}
ri->log().add_message(preferences::login(), message);
add_active_window_message(preferences::login(), message);
} else {
LOG_LB << "Cannot add sent message to ui for room " << room
<< ", player not in the room\n";
}
}
void tlobby_main::add_chat_room_message_received(const std::string& room,
const std::string& speaker,
const std::string& message)
{
if(room_info* ri = lobby_info_.get_room(room)) {
t_notify_mode notify_mode = NOTIFY_NONE;
ri->log().add_message(speaker, message);
if(room_window_active(room)) {
add_active_window_message(speaker, message);
notify_mode = NOTIFY_MESSAGE;
} else {
add_room_window_message(room, speaker, message);
increment_waiting_messages(room);
notify_mode = NOTIFY_MESSAGE_OTHER_WINDOW;
}
if(speaker == "server") {
notify_mode = NOTIFY_SERVER_MESSAGE;
} else if(utils::word_match(message, preferences::login())) {
notify_mode = NOTIFY_OWN_NICK;
} else if(preferences::is_friend(speaker)) {
notify_mode = NOTIFY_FRIEND_MESSAGE;
}
do_notify(notify_mode, speaker, message);
} else {
LOG_LB << "Discarding message to room " << room << " from " << speaker
<< " (room not open)\n";
}
}
void tlobby_main::append_to_chatbox(const std::string& text)
{
append_to_chatbox(text, active_window_);
}
void tlobby_main::append_to_chatbox(const std::string& text, size_t id)
{
tgrid& grid = chat_log_container_->page_grid(id);
tscroll_label& log = find_widget<tscroll_label>(&grid, "log_text", false);
log.set_label(log.label() + "\n" + preferences::get_chat_timestamp(time(0))
+ text);
log.scroll_vertical_scrollbar(tscrollbar_::END);
}
void tlobby_main::do_notify(t_notify_mode mode, const std::string & sender, const std::string & message)
{
switch(mode) {
case NOTIFY_WHISPER:
case NOTIFY_WHISPER_OTHER_WINDOW:
case NOTIFY_OWN_NICK:
mp_ui_alerts::private_message(true, sender, message);
break;
case NOTIFY_FRIEND_MESSAGE:
mp_ui_alerts::friend_message(true, sender, message);
break;
case NOTIFY_SERVER_MESSAGE:
mp_ui_alerts::server_message(true, sender, message);
break;
case NOTIFY_LOBBY_QUIT:
mp_ui_alerts::player_leaves(true);
break;
case NOTIFY_LOBBY_JOIN:
mp_ui_alerts::player_joins(true);
break;
case NOTIFY_MESSAGE:
mp_ui_alerts::public_message(true, sender, message);
break;
default:
break;
}
}
tlobby_main::tlobby_main(const config& game_config,
lobby_info& info,
display& disp)
: legacy_result_(QUIT)
, game_config_(game_config)
, gamelistbox_(NULL)
, userlistbox_(NULL)
, roomlistbox_(NULL)
, chat_log_container_(NULL)
, chat_input_(NULL)
, window_(NULL)
, lobby_info_(info)
, preferences_callback_(NULL)
, open_windows_()
, active_window_(0)
, filter_friends_(NULL)
, filter_ignored_(NULL)
, filter_slots_(NULL)
, filter_invert_(NULL)
, filter_text_(NULL)
, selected_game_id_()
, player_list_()
, player_list_dirty_(false)
, gamelist_dirty_(false)
, last_gamelist_update_(0)
, gamelist_diff_update_(true)
, disp_(disp)
, lobby_update_timer_(0)
, preferences_wrapper_()
, gamelist_id_at_row_()
, delay_playerlist_update_(false)
, delay_gamelist_update_(false)
{
}
struct lobby_delay_gamelist_update_guard
{
lobby_delay_gamelist_update_guard(tlobby_main& l) : l(l)
{
l.delay_gamelist_update_ = true;
}
~lobby_delay_gamelist_update_guard()
{
l.delay_gamelist_update_ = false;
}
tlobby_main& l;
};
void tlobby_main::set_preferences_callback(boost::function<void()> cb)
{
preferences_callback_ = cb;
}
tlobby_main::~tlobby_main()
{
if(lobby_update_timer_) {
remove_timer(lobby_update_timer_);
}
}
static void signal_handler_sdl_video_resize(const event::tevent event,
bool& handled,
bool& halt,
const tpoint& new_size,
CVideo& video)
{
DBG_GUI_E << __func__ << ": " << event << ".\n";
if(new_size.x < preferences::min_allowed_width()
|| new_size.y < preferences::min_allowed_height()) {
DBG_GUI_E << __func__ << ": resize aborted, too small.\n";
handled = true;
halt = true;
return;
}
if(new_size.x == static_cast<int>(settings::screen_width)
&& new_size.y == static_cast<int>(settings::screen_height)) {
DBG_GUI_E << __func__ << ": resize not needed.\n";
handled = true;
return;
}
if(!preferences::set_resolution(video, new_size.x, new_size.y)) {
LOG_GUI_E << __func__ << ": resize aborted, resize failed.\n";
}
}
static bool fullscreen(CVideo& video)
{
preferences::set_fullscreen(video, !preferences::fullscreen());
// Setting to fullscreen doesn't seem to generate a resize event.
const SDL_Rect& rect = screen_area();
SDL_Event event;
#if SDL_VERSION_ATLEAST(2, 0, 0)
event.type = SDL_WINDOWEVENT;
event.window.event = SDL_WINDOWEVENT_RESIZED;
event.window.data1 = rect.w;
event.window.data2 = rect.h;
#else
event.type = SDL_VIDEORESIZE;
event.resize.type = SDL_VIDEORESIZE;
event.resize.w = rect.w;
event.resize.h = rect.h;
#endif
SDL_PushEvent(&event);
return true;
}
void tlobby_main::post_build(CVideo& video, twindow& window)
{
/** @todo Should become a global hotkey after 1.8, then remove it here. */
window.register_hotkey(hotkey::HOTKEY_FULLSCREEN,
boost::bind(fullscreen, boost::ref(video)));
/** @todo Remove this code once the resizing in twindow is finished. */
window.connect_signal<event::SDL_VIDEO_RESIZE>(
boost::bind(&signal_handler_sdl_video_resize,
_2,
_3,
_4,
_5,
boost::ref(video)),
event::tdispatcher::front_child);
/*** Local hotkeys. ***/
preferences_wrapper_
= boost::bind(&tlobby_main::show_preferences_button_callback,
this,
boost::ref(window));
window.register_hotkey(
hotkey::HOTKEY_PREFERENCES,
boost::bind(function_wrapper<bool, boost::function<void()> >,
true,
boost::cref(preferences_wrapper_)));
}
namespace
{
void add_label_data(std::map<std::string, string_map>& map,
const std::string& key,
const std::string& label)
{
string_map item;
item["label"] = label;
map.insert(std::make_pair(key, item));
}
void add_tooltip_data(std::map<std::string, string_map>& map,
const std::string& key,
const std::string& label)
{
string_map item;
item["tooltip"] = label;
map.insert(std::make_pair(key, item));
}
void modify_grid_with_data(tgrid* grid,
const std::map<std::string, string_map>& map)
{
FOREACH(const AUTO & v, map)
{
const std::string& key = v.first;
const string_map& strmap = v.second;
twidget* w = grid->find(key, false);
if(w == NULL)
continue;
tcontrol* c = dynamic_cast<tcontrol*>(w);
if(c == NULL)
continue;
FOREACH(const AUTO & vv, strmap)
{
if(vv.first == "label") {
c->set_label(vv.second);
} else if(vv.first == "tooltip") {
c->set_tooltip(vv.second);
}
}
}
}
void set_visible_if_exists(tgrid* grid, const char* id, bool visible)
{
twidget* w = grid->find(id, false);
if(w) {
w->set_visible(visible ? twidget::tvisible::visible
: twidget::tvisible::invisible);
}
}
std::string colorize(const std::string& str, const std::string& color)
{
return "<span color=\"" + color + "\">" + str + "</span>";
}
std::string tag(const std::string& str, const std::string& tag)
{
return "<" + tag + ">" + str + "</" + tag + ">";
}
} // end anonymous namespace
void tlobby_main::update_gamelist()
{
SCOPE_LB;
gamelistbox_->clear();
gamelist_id_at_row_.clear();
lobby_info_.make_games_vector();
int select_row = -1;
for(unsigned i = 0; i < lobby_info_.games().size(); ++i) {
const game_info& game = *lobby_info_.games()[i];
if(game.id == selected_game_id_) {
select_row = i;
}
gamelist_id_at_row_.push_back(game.id);
LOG_LB << "Adding game to listbox (1)" << game.id << "\n";
gamelistbox_->add_row(make_game_row_data(game));
tgrid* grid = gamelistbox_->get_row_grid(gamelistbox_->get_item_count()
- 1);
adjust_game_row_contents(
game, gamelistbox_->get_item_count() - 1, grid);
}
if(select_row >= 0 && select_row != gamelistbox_->get_selected_row()) {
gamelistbox_->select_row(select_row);
}
update_selected_game();
gamelist_dirty_ = false;
last_gamelist_update_ = SDL_GetTicks();
lobby_info_.sync_games_display_status();
lobby_info_.apply_game_filter();
update_gamelist_header();
gamelistbox_->set_row_shown(lobby_info_.games_visibility());
}
void tlobby_main::update_gamelist_diff()
{
SCOPE_LB;
lobby_info_.make_games_vector();
int select_row = -1;
unsigned list_i = 0;
int list_rows_deleted = 0;
std::vector<int> next_gamelist_id_at_row;
for(unsigned i = 0; i < lobby_info_.games().size(); ++i) {
const game_info& game = *lobby_info_.games()[i];
if(game.display_status == game_info::NEW) {
LOG_LB << "Adding game to listbox " << game.id << "\n";
if(list_i != gamelistbox_->get_item_count()) {
gamelistbox_->add_row(make_game_row_data(game), list_i);
DBG_LB << "Added a game listbox row not at the end" << list_i
<< " " << gamelistbox_->get_item_count() << "\n";
list_rows_deleted--;
} else {
gamelistbox_->add_row(make_game_row_data(game));
}
tgrid* grid = gamelistbox_->get_row_grid(
gamelistbox_->get_item_count() - 1);
adjust_game_row_contents(
game, gamelistbox_->get_item_count() - 1, grid);
list_i++;
next_gamelist_id_at_row.push_back(game.id);
} else {
if(list_i >= gamelistbox_->get_item_count()) {
ERR_LB << "Ran out of listbox items -- triggering a full "
"refresh\n";
network::send_data(config("refresh_lobby"), 0);
return;
}
if(list_i + list_rows_deleted >= gamelist_id_at_row_.size()) {
ERR_LB << "gamelist_id_at_row_ overflow! " << list_i << " + "
<< list_rows_deleted
<< " >= " << gamelist_id_at_row_.size()
<< " -- triggering a full refresh\n";
network::send_data(config("refresh_lobby"), 0);
return;
}
int listbox_game_id
= gamelist_id_at_row_[list_i + list_rows_deleted];
if(game.id != listbox_game_id) {
ERR_LB << "Listbox game id does not match expected id "
<< listbox_game_id << " " << game.id << " (row "
<< list_i << ")\n";
network::send_data(config("refresh_lobby"), 0);
return;
}
if(game.display_status == game_info::UPDATED) {
LOG_LB << "Modifying game in listbox " << game.id << " (row "
<< list_i << ")\n";
tgrid* grid = gamelistbox_->get_row_grid(list_i);
modify_grid_with_data(grid, make_game_row_data(game));
adjust_game_row_contents(game, list_i, grid);
++list_i;
next_gamelist_id_at_row.push_back(game.id);
} else if(game.display_status == game_info::DELETED) {
LOG_LB << "Deleting game from listbox " << game.id << " (row "
<< list_i << ")\n";
gamelistbox_->remove_row(list_i);
++list_rows_deleted;
} else {
// clean
LOG_LB << "Clean game in listbox " << game.id << " (row "
<< list_i << ")\n";
next_gamelist_id_at_row.push_back(game.id);
++list_i;
}
}
}
for(unsigned i = 0; i < next_gamelist_id_at_row.size(); ++i) {
if(next_gamelist_id_at_row[i] == selected_game_id_) {
select_row = i;
}
}
next_gamelist_id_at_row.swap(gamelist_id_at_row_);
if(select_row >= static_cast<int>(gamelistbox_->get_item_count())) {
ERR_LB << "Would select a row beyond the listbox" << select_row << " "
<< gamelistbox_->get_item_count() << "\n";
select_row = gamelistbox_->get_item_count() - 1;
}
if(select_row >= 0 && select_row != gamelistbox_->get_selected_row()) {
gamelistbox_->select_row(select_row);
}
update_selected_game();
gamelist_dirty_ = false;
last_gamelist_update_ = SDL_GetTicks();
lobby_info_.sync_games_display_status();
lobby_info_.apply_game_filter();
update_gamelist_header();
gamelistbox_->set_row_shown(lobby_info_.games_visibility());
}
void tlobby_main::update_gamelist_header()
{
#ifndef GUI2_EXPERIMENTAL_LISTBOX
utils::string_map symbols;
symbols["num_shown"]
= lexical_cast<std::string>(lobby_info_.games_filtered().size());
symbols["num_total"]
= lexical_cast<std::string>(lobby_info_.games().size());
std::string games_string
= VGETTEXT("Games: showing $num_shown out of $num_total", symbols);
find_widget<tlabel>(gamelistbox_, "map", false).set_label(games_string);
#endif
}
std::map<std::string, string_map>
tlobby_main::make_game_row_data(const game_info& game)
{
std::map<std::string, string_map> data;
const char* color_string;
if(game.vacant_slots > 0) {
if(game.reloaded || game.started) {
color_string = "yellow";
} else {
color_string = "green";
}
} else {
if(game.observers) {
color_string = "#ddd";
} else {
color_string = "red";
}
}
if(!game.have_era && (game.vacant_slots > 0 || game.observers)) {
color_string = "#444";
}
add_label_data(data, "status", colorize(game.status, color_string));
add_label_data(data, "name", colorize(game.name, color_string));
add_label_data(data, "era", game.era);
add_label_data(data, "era_short", game.era_short);
add_label_data(data, "map_info", game.map_info);
add_label_data(data, "scenario", game.scenario);
add_label_data(data, "map_size_text", game.map_size_info);
add_label_data(data, "time_limit", game.time_limit);
add_label_data(data, "gold_text", game.gold);
add_label_data(data, "xp_text", game.xp);
add_label_data(data, "vision_text", game.vision);
add_label_data(data, "time_limit_text", game.time_limit);
add_label_data(data, "status", game.status);
if(game.observers) {
add_label_data(data, "observer_icon", "misc/eye.png");
add_tooltip_data(data, "observer_icon", _("Observers allowed"));
} else {
add_label_data(data, "observer_icon", "misc/no_observer.png");
add_tooltip_data(data, "observer_icon", _("Observers not allowed"));
}
const char* vision_icon;
if(game.fog) {
if(game.shroud) {
vision_icon = "misc/vision-fog-shroud.png";
} else {
vision_icon = "misc/vision-fog.png";
}
} else {
if(game.shroud) {
vision_icon = "misc/vision-shroud.png";
} else {
vision_icon = "misc/vision-none.png";
}
}
add_label_data(data, "vision_icon", vision_icon);
add_tooltip_data(data, "vision_icon", game.vision);
return data;
}
void tlobby_main::adjust_game_row_contents(const game_info& game,
int idx,
tgrid* grid)
{
find_widget<tcontrol>(grid, "name", false).set_use_markup(true);
find_widget<tcontrol>(grid, "status", false).set_use_markup(true);
ttoggle_panel& row_panel = find_widget<ttoggle_panel>(grid, "panel", false);
row_panel.set_callback_mouse_left_double_click(
boost::bind(&tlobby_main::join_or_observe, this, idx));
set_visible_if_exists(grid, "time_limit_icon", !game.time_limit.empty());
set_visible_if_exists(grid, "vision_fog", game.fog);
set_visible_if_exists(grid, "vision_shroud", game.shroud);
set_visible_if_exists(grid, "vision_none", !(game.fog || game.shroud));
set_visible_if_exists(grid, "observers_yes", game.observers);
set_visible_if_exists(grid, "shuffle_sides_icon", game.shuffle_sides);
set_visible_if_exists(grid, "observers_no", !game.observers);
set_visible_if_exists(grid, "needs_password", game.password_required);
set_visible_if_exists(grid, "reloaded", game.reloaded);
set_visible_if_exists(grid, "started", game.started);
set_visible_if_exists(grid, "use_map_settings", game.use_map_settings);
set_visible_if_exists(grid, "no_era", !game.have_era);
tbutton* join_button = dynamic_cast<tbutton*>(grid->find("join", false));
if(join_button) {
connect_signal_mouse_left_click(
*join_button,
boost::bind(&tlobby_main::join_button_callback,
this,
boost::ref(*window_)));
join_button->set_active(game.can_join());
}
tbutton* observe_button
= dynamic_cast<tbutton*>(grid->find("observe", false));
if(observe_button) {
connect_signal_mouse_left_click(
*observe_button,
boost::bind(&tlobby_main::observe_button_callback,
this,
boost::ref(*window_)));
observe_button->set_active(game.can_observe());
}
tminimap* minimap = dynamic_cast<tminimap*>(grid->find("minimap", false));
if(minimap) {
minimap->set_config(&game_config_);
minimap->set_map_data(game.map_data);
}
}
void tlobby_main::update_gamelist_filter()
{
DBG_LB << "tlobby_main::update_gamelist_filter\n";
lobby_info_.apply_game_filter();
DBG_LB << "Games in lobby_info: " << lobby_info_.games().size()
<< ", games in listbox: " << gamelistbox_->get_item_count() << "\n";
assert(lobby_info_.games().size() == gamelistbox_->get_item_count());
gamelistbox_->set_row_shown(lobby_info_.games_visibility());
}
void tlobby_main::update_playerlist()
{
if(delay_playerlist_update_)
return;
SCOPE_LB;
DBG_LB << "Playerlist update: " << lobby_info_.users().size() << "\n";
lobby_info_.update_user_statuses(selected_game_id_, active_window_room());
lobby_info_.sort_users(player_list_.sort_by_name->get_value(),
player_list_.sort_by_relation->get_value());
bool lobby = false;
if(room_info* ri = active_window_room()) {
if(ri->name() == "lobby") {
lobby = true;
}
}
player_list_.active_game.list->clear();
player_list_.active_room.list->clear();
player_list_.other_rooms.list->clear();
player_list_.other_games.list->clear();
assert(player_list_.active_game.tree);
assert(player_list_.active_room.tree);
assert(player_list_.other_games.tree);
assert(player_list_.other_rooms.tree);
player_list_.active_game.tree->clear();
player_list_.active_room.tree->clear();
player_list_.other_games.tree->clear();
player_list_.other_rooms.tree->clear();
FOREACH(AUTO userptr, lobby_info_.users_sorted())
{
user_info& user = *userptr;
tsub_player_list* target_list(NULL);
std::map<std::string, string_map> data;
std::stringstream icon_ss;
std::string name = user.name;
icon_ss << "lobby/status";
switch(user.state) {
case user_info::SEL_ROOM:
icon_ss << "-lobby";
target_list = &player_list_.active_room;
if(lobby) {
target_list = &player_list_.other_rooms;
}
break;
case user_info::LOBBY:
icon_ss << "-lobby";
target_list = &player_list_.other_rooms;
break;
case user_info::SEL_GAME:
name = colorize(name, "cyan");
icon_ss << (user.observing ? "-obs" : "-playing");
target_list = &player_list_.active_game;
break;
case user_info::GAME:
name = colorize(name, "red");
icon_ss << (user.observing ? "-obs" : "-playing");
target_list = &player_list_.other_games;
break;
default:
ERR_LB << "Bad user state in lobby: " << user.name << ": "
<< user.state << "\n";
continue;
}
switch(user.relation) {
case user_info::ME:
/* fall through */
case user_info::NEUTRAL:
icon_ss << "-n";
break;
case user_info::FRIEND:
icon_ss << "-f";
break;
case user_info::IGNORED:
icon_ss << "-i";
break;
default:
ERR_LB << "Bad user relation in lobby: " << user.relation
<< "\n";
}
if(user.registered) {
name = tag(name, "b");
}
icon_ss << ".png";
add_label_data(data, "player", name);
add_label_data(data, "main_icon", icon_ss.str());
if(!preferences::playerlist_group_players()) {
target_list = &player_list_.other_rooms;
}
assert(target_list->tree);
string_map tree_group_field;
std::map<std::string, string_map> tree_group_item;
/*** Add tree item ***/
tree_group_field["label"] = icon_ss.str();
tree_group_item["icon"] = tree_group_field;
tree_group_field["label"] = name;
tree_group_field["use_markup"] = "true";
tree_group_item["name"] = tree_group_field;
ttree_view_node& player
= target_list->tree->add_child("player", tree_group_item);
find_widget<ttoggle_panel>(&player, "tree_view_node_label", false)
.set_callback_mouse_left_double_click(boost::bind(
&tlobby_main::user_dialog_callback, this, userptr));
}
player_list_.active_game.auto_hide();
player_list_.active_room.auto_hide();
player_list_.other_rooms.auto_hide();
player_list_.other_games.auto_hide();
player_list_dirty_ = false;
}
void tlobby_main::update_selected_game()
{
int idx = gamelistbox_->get_selected_row();
bool can_join = false, can_observe = false;
if(idx >= 0) {
const game_info& game = *lobby_info_.games()[idx];
can_observe = game.can_observe();
can_join = game.can_join();
selected_game_id_ = game.id;
} else {
selected_game_id_ = 0;
}
find_widget<tbutton>(window_, "observe_global", false)
.set_active(can_observe);
find_widget<tbutton>(window_, "join_global", false).set_active(can_join);
player_list_dirty_ = true;
}
void tlobby_main::pre_show(CVideo& /*video*/, twindow& window)
{
SCOPE_LB;
roomlistbox_ = find_widget<tlistbox>(&window, "room_list", false, true);
#ifdef GUI2_EXPERIMENTAL_LISTBOX
connect_signal_notify_modified(
*roomlistbox_,
boost::bind(&tlobby_main::room_switch_callback,
*this,
boost::ref(window)));
#else
roomlistbox_->set_callback_value_change(
dialog_callback<tlobby_main, &tlobby_main::room_switch_callback>);
#endif
gamelistbox_ = find_widget<tlistbox>(&window, "game_list", false, true);
#ifdef GUI2_EXPERIMENTAL_LISTBOX
connect_signal_notify_modified(
*gamelistbox_,
boost::bind(&tlobby_main::gamelist_change_callback,
*this,
boost::ref(window)));
#else
gamelistbox_->set_callback_value_change(
dialog_callback<tlobby_main,
&tlobby_main::gamelist_change_callback>);
#endif
player_list_.init(window);
player_list_.sort_by_name->set_value(preferences::playerlist_sort_name());
player_list_.sort_by_relation->set_value(
preferences::playerlist_sort_relation());