-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
dialogs.cpp
1709 lines (1403 loc) · 49.9 KB
/
dialogs.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) 2003 - 2014 by David White <dave@whitevine.net>
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.
*/
/**
* @file
* Various dialogs: advance_unit, show_objectives, save+load game, network::connection.
*/
#include "global.hpp"
#include "actions/attack.hpp"
#include "actions/undo.hpp"
#include "dialogs.hpp"
#include "format_time_summary.hpp"
#include "game_display.hpp"
#include "game_preferences.hpp"
#include "gui/dialogs/game_delete.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/widgets/window.hpp"
#include "gettext.hpp"
#include "help.hpp"
#include "language.hpp"
#include "log.hpp"
#include "map.hpp"
#include "map_exception.hpp"
#include "marked-up_text.hpp"
#include "menu_events.hpp"
#include "mouse_handler_base.hpp"
#include "minimap.hpp"
#include "recall_list_manager.hpp"
#include "replay.hpp"
#include "replay_helper.hpp"
#include "resources.hpp"
#include "savegame.hpp"
#include "save_index.hpp"
#include "strftime.hpp"
#include "synced_context.hpp"
#include "thread.hpp"
#include "unit.hpp"
#include "unit_animation.hpp"
#include "unit_helper.hpp"
#include "unit_types.hpp"
#include "wml_separators.hpp"
#include "widgets/progressbar.hpp"
#include "wml_exception.hpp"
#include "formula_string_utils.hpp"
#include "gui/dialogs/game_save.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "ai/lua/unit_advancements_aspect.hpp"
#include <boost/foreach.hpp>
//#ifdef _WIN32
//#include "locale.h"
//#endif
#include <clocale>
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_display("display");
#define LOG_DP LOG_STREAM(info, log_display)
#define ERR_G LOG_STREAM(err, lg::general)
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
namespace dialogs
{
namespace
{
class delete_recall_unit : public gui::dialog_button_action
{
public:
delete_recall_unit(display& disp, gui::filter_textbox& filter, const std::vector<unit_const_ptr >& units) : disp_(disp), filter_(filter), units_(units) {}
private:
gui::dialog_button_action::RESULT button_pressed(int menu_selection);
display& disp_;
gui::filter_textbox& filter_;
const std::vector<unit_const_ptr >& units_;
};
gui::dialog_button_action::RESULT delete_recall_unit::button_pressed(int menu_selection)
{
const size_t index = size_t(filter_.get_index(menu_selection));
if(index < units_.size()) {
const unit_const_ptr & u_ptr = units_[index];
const unit & u = *u_ptr;
//If the unit is of level > 1, or is close to advancing,
//we warn the player about it
std::stringstream message;
if (u.loyal()) {
// TRANSLATORS: this string ends with a space
message << _("This unit is loyal and requires no upkeep. ") << (u.gender() == unit_race::MALE ? _("Do you really want to dismiss him?")
: _("Do you really want to dismiss her?"));
} else if(u.level() > 1) {
// TRANSLATORS: this string ends with a space
message << _("This unit is an experienced one, having advanced levels. ") << (u.gender() == unit_race::MALE ? _("Do you really want to dismiss him?")
: _("Do you really want to dismiss her?"));
} else if(u.experience() > u.max_experience()/2) {
// TRANSLATORS: this string ends with a space
message << _("This unit is close to advancing a level. ") << (u.gender() == unit_race::MALE ? _("Do you really want to dismiss him?")
: _("Do you really want to dismiss her?"));
}
if(!message.str().empty()) {
const int res = gui2::show_message(disp_.video(), _("Dismiss Unit"), message.str(), gui2::tmessage::yes_no_buttons);
if(res == gui2::twindow::CANCEL) {
return gui::CONTINUE_DIALOG;
}
}
// Remove the item from filter_textbox memory
filter_.delete_item(menu_selection);
//add dismissal to the undo stack
resources::undo_stack->add_dismissal(u_ptr);
// Find the unit in the recall list.
unit_ptr dismissed_unit = (*resources::teams)[u.side() -1].recall_list().find_if_matches_id(u.id());
assert(dismissed_unit);
// Record the dismissal, then delete the unit.
synced_context::run_in_synced_context("disband", replay_helper::get_disband(dismissed_unit->id()));
//recorder.add_disband(dismissed_unit->id());
//recall_list.erase(dismissed_unit);
return gui::DELETE_ITEM;
} else {
return gui::CONTINUE_DIALOG;
}
}
} //anon namespace
int advance_unit_dialog(const map_location &loc)
{
unit_map::iterator u = resources::units->find(loc);
const std::vector<std::string>& options = u->advances_to();
std::vector<std::string> lang_options;
std::vector<unit> sample_units;
for(std::vector<std::string>::const_iterator op = options.begin(); op != options.end(); ++op) {
sample_units.push_back(::get_advanced_unit(*u, *op));
const unit& type = sample_units.back();
#ifdef LOW_MEM
lang_options.push_back(IMAGE_PREFIX
+ static_cast<std::string const &>(type.absolute_image())
+ COLUMN_SEPARATOR + type.type_name());
#else
lang_options.push_back(IMAGE_PREFIX + type.absolute_image() + u->image_mods() + COLUMN_SEPARATOR + type.type_name());
#endif
preferences::encountered_units().insert(*op);
}
bool always_display = false;
BOOST_FOREACH(const config &mod, u->get_modification_advances())
{
if (mod["always_display"].to_bool()) always_display = true;
sample_units.push_back(::get_amla_unit(*u, mod));
const unit& type = sample_units.back();
if (!mod["image"].empty()) {
lang_options.push_back(IMAGE_PREFIX + mod["image"].str() + COLUMN_SEPARATOR + mod["description"].str());
} else {
#ifdef LOW_MEM
lang_options.push_back(IMAGE_PREFIX
+ static_cast<std::string const &>(type.absolute_image())
+ COLUMN_SEPARATOR + mod["description"].str());
#else
lang_options.push_back(IMAGE_PREFIX + type.absolute_image() + u->image_mods() + COLUMN_SEPARATOR + mod["description"].str());
#endif
}
}
assert(!lang_options.empty());
if (lang_options.size() > 1 || always_display)
{
units_list_preview_pane unit_preview(sample_units);
std::vector<gui::preview_pane*> preview_panes;
preview_panes.push_back(&unit_preview);
gui::dialog advances = gui::dialog(*resources::screen,
_("Advance Unit"),
_("What should our victorious unit become?"),
gui::OK_ONLY);
advances.set_menu(lang_options);
advances.set_panes(preview_panes);
return advances.show();
}
return 0;
}
bool animate_unit_advancement(const map_location &loc, size_t choice, const bool &fire_event, const bool animate)
{
const events::command_disabler cmd_disabler;
unit_map::iterator u = resources::units->find(loc);
if (u == resources::units->end()) {
LOG_DP << "animate_unit_advancement suppressed: invalid unit\n";
return false;
} else if (!u->advances()) {
LOG_DP << "animate_unit_advancement suppressed: unit does not advance\n";
return false;
}
const std::vector<std::string>& options = u->advances_to();
std::vector<config> mod_options = u->get_modification_advances();
if(choice >= options.size() + mod_options.size()) {
LOG_DP << "animate_unit_advancement suppressed: invalid option\n";
return false;
}
// When the unit advances, it fades to white, and then switches
// to the new unit, then fades back to the normal color
if (animate && !resources::screen->video().update_locked()) {
unit_animator animator;
bool with_bars = true;
animator.add_animation(&*u,"levelout", u->get_location(), map_location(), 0, with_bars);
animator.start_animations();
animator.wait_for_end();
}
if(choice < options.size()) {
// chosen_unit is not a reference, since the unit may disappear at any moment.
std::string chosen_unit = options[choice];
::advance_unit(loc, chosen_unit, fire_event);
} else {
const config &mod_option = mod_options[choice - options.size()];
::advance_unit(loc, "", fire_event, &mod_option);
}
u = resources::units->find(loc);
resources::screen->invalidate_unit();
if (animate && u != resources::units->end() && !resources::screen->video().update_locked()) {
unit_animator animator;
animator.add_animation(&*u, "levelin", u->get_location(), map_location(), 0, true);
animator.start_animations();
animator.wait_for_end();
animator.set_all_standing();
resources::screen->invalidate(loc);
resources::screen->draw();
events::pump();
}
resources::screen->invalidate_all();
resources::screen->draw();
return true;
}
void show_unit_list(display& gui)
{
const std::string heading = std::string(1,HEADING_PREFIX) +
_("Type") + COLUMN_SEPARATOR + // 0
_("Name") + COLUMN_SEPARATOR + // 1
_("Moves") + COLUMN_SEPARATOR + // 2
_("Status") + COLUMN_SEPARATOR + // 3
_("HP") + COLUMN_SEPARATOR + // 4
_("Level^Lvl.") + COLUMN_SEPARATOR + // 5
_("XP") + COLUMN_SEPARATOR + // 6
_("unit list^Traits"); // 7
gui::menu::basic_sorter sorter;
sorter.set_alpha_sort(0).set_alpha_sort(1).set_numeric_sort(2);
sorter.set_alpha_sort(3).set_numeric_sort(4).set_level_sort(5, 6);
sorter.set_xp_sort(6).set_alpha_sort(7);
std::vector<std::string> items;
items.push_back(heading);
std::vector<map_location> locations_list;
std::vector<unit> units_list;
int selected = 0;
const unit_map& units = gui.get_units();
for(unit_map::const_iterator i = units.begin(); i != units.end(); ++i) {
if (i->side() != gui.viewing_side())
continue;
std::stringstream row;
// If a unit is already selected on the map, we do the same in the unit list dialog
if (gui.selected_hex() == i->get_location()) {
row << DEFAULT_ITEM;
selected = units_list.size();
}
// If unit is leader, show name in special color, e.g. gold/silver
/** @todo TODO: hero just has overlay "misc/hero-icon.png" - needs an ability to query */
if (i->can_recruit() ) {
row << "<205,173,0>"; // gold3
}
row << i->type_name() << COLUMN_SEPARATOR;
if (i->can_recruit() ) {
row << "<205,173,0>"; // gold3
}
row << i->name() << COLUMN_SEPARATOR;
// display move left (0=red, moved=yellow, not moved=green)
if (i->movement_left() == 0) {
row << font::RED_TEXT;
} else if (i->movement_left() < i->total_movement() ) {
row << "<255,255,0>";
} else {
row << font::GREEN_TEXT;
}
row << i->movement_left() << '/' << i->total_movement() << COLUMN_SEPARATOR;
// show icons if unit is slowed, poisoned, petrified, invisible:
if(i->get_state(unit::STATE_PETRIFIED))
row << IMAGE_PREFIX << "misc/petrified.png" << IMG_TEXT_SEPARATOR;
if(i->get_state(unit::STATE_POISONED))
row << IMAGE_PREFIX << "misc/poisoned.png" << IMG_TEXT_SEPARATOR;
if(i->get_state(unit::STATE_SLOWED))
row << IMAGE_PREFIX << "misc/slowed.png" << IMG_TEXT_SEPARATOR;
if(i->invisible(i->get_location(),false))
row << IMAGE_PREFIX << "misc/invisible.png";
row << COLUMN_SEPARATOR;
// Display HP
// see also unit_preview_pane in dialogs.cpp
row << font::color2markup(i->hp_color());
row << i->hitpoints() << '/' << i->max_hitpoints() << COLUMN_SEPARATOR;
// Show units of level (0=gray, 1 normal, 2 bold, 2+ bold&wbright)
int level = i->level();
if(level < 1) {
row << "<150,150,150>";
} else if(level == 1) {
row << font::NORMAL_TEXT;
} else if(level == 2) {
row << font::BOLD_TEXT;
} else if(level > 2 ) {
row << font::BOLD_TEXT << "<255,255,255>";
}
row << level << COLUMN_SEPARATOR;
// Display XP
row << font::color2markup(i->xp_color());
row << i->experience() << "/";
if (i->can_advance()) {
row << i->max_experience();
} else {
row << "-";
}
row << COLUMN_SEPARATOR;
// TODO: show 'loyal' in green / xxx in red // how to handle translations ??
row << utils::join(i->trait_names(), ", ");
items.push_back(row.str());
locations_list.push_back(i->get_location());
units_list.push_back(*i);
}
{
dialogs::units_list_preview_pane unit_preview(units_list);
unit_preview.set_selection(selected);
gui::dialog umenu(gui, _("Unit List"), "", gui::NULL_DIALOG);
umenu.set_menu(items, &sorter);
umenu.add_pane(&unit_preview);
//sort by type name
umenu.get_menu().sort_by(0);
umenu.add_button(new gui::standard_dialog_button(gui.video(), _("Scroll To"), 0, false),
gui::dialog::BUTTON_STANDARD);
umenu.add_button(new gui::standard_dialog_button(gui.video(), _("Close"), 1, true),
gui::dialog::BUTTON_STANDARD);
umenu.set_basic_behavior(gui::OK_CANCEL);
selected = umenu.show();
} // this will kill the dialog before scrolling
if(selected >= 0 && selected < int(locations_list.size())) {
const map_location& loc = locations_list[selected];
gui.scroll_to_tile(loc,game_display::WARP);
gui.select_hex(loc);
}
}
void show_objectives(const config &level, const std::string &objectives)
{
static const std::string no_objectives(_("No objectives available"));
gui2::show_transient_message(resources::screen->video(), level["name"],
(objectives.empty() ? no_objectives : objectives), "", true);
}
int recruit_dialog(display& disp, std::vector< const unit_type* >& units, const std::vector< std::string >& items, int side, const std::string& title_suffix)
{
dialogs::unit_types_preview_pane unit_preview(
units, NULL, side);
std::vector<gui::preview_pane*> preview_panes;
preview_panes.push_back(&unit_preview);
gui::menu::basic_sorter sorter;
sorter.set_alpha_sort(1);
gui::dialog rmenu(disp, _("Recruit") + title_suffix,
_("Select unit:") + std::string("\n"),
gui::OK_CANCEL,
gui::dialog::default_style);
rmenu.add_button(new help::help_button(disp, "recruit_and_recall"),
gui::dialog::BUTTON_HELP);
gui::menu::imgsel_style units_display_style(gui::menu::bluebg_style);
units_display_style.scale_images(font::relative_size(72), font::relative_size(72));
gui::menu* units_menu = new gui::menu(disp.video(), items, false, -1,
gui::dialog::max_menu_width, &sorter, &units_display_style, false);
units_menu->sort_by(1); // otherwise it's unsorted by default
rmenu.set_menu(units_menu);
rmenu.set_panes(preview_panes);
return rmenu.show();
}
#ifdef LOW_MEM
int recall_dialog(display& disp, std::vector< unit_const_ptr >& units, int /*side*/, const std::string& title_suffix, const int team_recall_cost)
#else
int recall_dialog(display& disp, std::vector< unit_const_ptr >& units, int side, const std::string& title_suffix, const int team_recall_cost)
#endif
{
std::vector<std::string> options, options_to_filter;
std::ostringstream heading;
heading << HEADING_PREFIX << COLUMN_SEPARATOR << _("Type")
<< COLUMN_SEPARATOR << _("Name")
<< COLUMN_SEPARATOR << _("Level^Lvl.")
<< COLUMN_SEPARATOR << _("XP");
heading << COLUMN_SEPARATOR << _("Traits");
gui::menu::basic_sorter sorter;
sorter.set_alpha_sort(1).set_alpha_sort(2);
sorter.set_level_sort(3,4).set_xp_sort(4).set_alpha_sort(5);
options.push_back(heading.str());
options_to_filter.push_back(options.back());
BOOST_FOREACH(const unit_const_ptr & u, units)
{
std::stringstream option, option_to_filter;
std::string name = u->name();
if (name.empty()) name = utils::unicode_em_dash;
option << IMAGE_PREFIX << u->absolute_image();
#ifndef LOW_MEM
option << "~RC(" << u->team_color() << '>'
<< team::get_side_color_index(side) << ')';
if(u->can_recruit()) {
option << "~BLIT(" << unit::leader_crown() << ")";
}
BOOST_FOREACH(const std::string& overlay, u->overlays())
{
option << "~BLIT(" << overlay << ")";
}
#endif
option << COLUMN_SEPARATOR;
int cost = u->recall_cost();
if(cost < 0) {
cost = team_recall_cost;
}
option << u->type_name() << "\n";
if(cost > team_recall_cost) {
option << font::NORMAL_TEXT << "<255,0,0>";
}
else if(cost == team_recall_cost) {
option << font::NORMAL_TEXT;
}
else if(cost < team_recall_cost) {
option << font::NORMAL_TEXT << "<0,255,0>";
}
option << cost << " Gold" << COLUMN_SEPARATOR
<< name << COLUMN_SEPARATOR;
// Show units of level (0=gray, 1 normal, 2 bold, 2+ bold&wbright)
const int level = u->level();
if(level < 1) {
option << "<150,150,150>";
} else if(level == 1) {
option << font::NORMAL_TEXT;
} else if(level == 2) {
option << font::BOLD_TEXT;
} else if(level > 2 ) {
option << font::BOLD_TEXT << "<255,255,255>";
}
option << level << COLUMN_SEPARATOR;
option << font::color2markup(u->xp_color()) << u->experience() << "/";
if (u->can_advance())
option << u->max_experience();
else
option << "-";
option_to_filter << u->type_name() << " " << name << " " << u->level();
option << COLUMN_SEPARATOR;
BOOST_FOREACH(const t_string& trait, u->trait_names()) {
option << trait << '\n';
option_to_filter << " " << trait;
}
options.push_back(option.str());
options_to_filter.push_back(option_to_filter.str());
}
gui::dialog rmenu(disp, _("Recall") + title_suffix,
_("Select unit:") + std::string("\n"),
gui::OK_CANCEL, gui::dialog::default_style);
gui::menu::imgsel_style units_display_style(gui::menu::bluebg_style);
units_display_style.scale_images(font::relative_size(72), font::relative_size(72));
gui::menu* units_menu = new gui::menu(disp.video(), options, false, -1,
gui::dialog::max_menu_width, &sorter, &units_display_style, false);
rmenu.set_menu(units_menu);
gui::filter_textbox* filter = new gui::filter_textbox(disp.video(),
_("Filter: "), options, options_to_filter, 1, rmenu, 200);
rmenu.set_textbox(filter);
delete_recall_unit recall_deleter(disp, *filter, units);
gui::dialog_button_info delete_button(&recall_deleter,_("Dismiss Unit"));
rmenu.add_button(delete_button);
rmenu.add_button(new help::help_button(disp,"recruit_and_recall"),
gui::dialog::BUTTON_HELP);
dialogs::units_list_preview_pane unit_preview(units, filter);
rmenu.add_pane(&unit_preview);
//sort by level
static int sort_by = 3;
static bool sort_reversed = false;
if(sort_by >= 0) {
rmenu.get_menu().sort_by(sort_by);
// "reclick" on the sorter to reverse the order
if(sort_reversed) {
rmenu.get_menu().sort_by(sort_by);
}
}
int res = rmenu.show();
res = filter->get_index(res);
sort_by = rmenu.get_menu().get_sort_by();
sort_reversed = rmenu.get_menu().get_sort_reversed();
return res;
}
namespace {
/** Class to handle deleting a saved game. */
class delete_save : public gui::dialog_button_action
{
public:
delete_save(display& disp, gui::filter_textbox& filter, std::vector<savegame::save_info>& saves) :
disp_(disp), saves_(saves), filter_(filter) {}
private:
gui::dialog_button_action::RESULT button_pressed(int menu_selection);
display& disp_;
std::vector<savegame::save_info>& saves_;
gui::filter_textbox& filter_;
};
gui::dialog_button_action::RESULT delete_save::button_pressed(int menu_selection)
{
const size_t index = size_t(filter_.get_index(menu_selection));
if(index < saves_.size()) {
// See if we should ask the user for deletion confirmation
if(preferences::ask_delete_saves()) {
if(!gui2::tgame_delete::execute(disp_.video())) {
return gui::CONTINUE_DIALOG;
}
}
// Remove the item from filter_textbox memory
filter_.delete_item(menu_selection);
// Delete the file
savegame::delete_game(saves_[index].name());
// Remove it from the list of saves
saves_.erase(saves_.begin() + index);
return gui::DELETE_ITEM;
} else {
return gui::CONTINUE_DIALOG;
}
}
static const int save_preview_border = 10;
class save_preview_pane : public gui::preview_pane
{
public:
save_preview_pane(CVideo &video, const config& game_config, gamemap* map,
const std::vector<savegame::save_info>& info,
const gui::filter_textbox& textbox,
gui::dialog_button* difficulty_option) :
gui::preview_pane(video),
game_config_(&game_config),
map_(map), info_(&info),
index_(0),
map_cache_(),
textbox_(textbox),
difficulty_option_(difficulty_option)
{
set_measurements(std::min<int>(200,video.getx()/4),
std::min<int>(400,video.gety() * 4/5));
if (difficulty_option_ != NULL) {
difficulty_option_->enable(false);
}
}
void draw_contents();
/**
* Enables "Change difficulty" option box for start-of-scenario
* save-files only
*/
void decide_on_difficulty_option();
void set_selection(int index) {
int res = textbox_.get_index(index);
index_ = (res >= 0) ? res : index_;
decide_on_difficulty_option();
set_dirty();
}
bool left_side() const { return true; }
private:
const config* game_config_;
gamemap* map_;
const std::vector<savegame::save_info>* info_;
int index_;
std::map<std::string,surface> map_cache_;
const gui::filter_textbox& textbox_;
gui::dialog_button* difficulty_option_;
};
void save_preview_pane::decide_on_difficulty_option()
{
if (difficulty_option_ == NULL || size_t(index_) >= info_->size()) {
return;
}
const config& summary = ((*info_)[index_]).summary();
const bool start_of_scenario = summary["turn"].empty() && !summary["replay"].to_bool();
difficulty_option_->enable(start_of_scenario);
}
void save_preview_pane::draw_contents()
{
if(size_t(index_) >= info_->size()) {
return;
}
surface screen = video().getSurface();
SDL_Rect const &loc = location();
const SDL_Rect area = sdl::create_rect(loc.x + save_preview_border
, loc.y + save_preview_border
, loc.w - save_preview_border * 2
, loc.h - save_preview_border * 2);
const clip_rect_setter clipper(screen, &area);
int ypos = area.y;
bool have_leader_image = false;
const config& summary = ((*info_)[index_]).summary();
const std::string& leader_image = summary["leader_image"].str();
if(!leader_image.empty() && image::exists(leader_image))
{
#ifdef LOW_MEM
const surface& image(image::get_image(leader_image));
#else
// NOTE: assuming magenta for TC here. This is what's used in all of
// mainline, so the compromise should be good enough until we add more
// summary fields to help with this and deciding the side color range.
const surface& image(image::get_image(leader_image + "~RC(magenta>1)"));
#endif
have_leader_image = !image.null();
if(have_leader_image) {
SDL_Rect image_rect = sdl::create_rect(area.x, area.y, image->w, image->h);
ypos += image_rect.h + save_preview_border;
sdl_blit(image,NULL,screen,&image_rect);
}
}
std::string map_data = summary["map_data"];
if(map_data.empty()) {
const config &scenario = game_config_->find_child(summary["campaign_type"], "id", summary["scenario"]);
if (scenario && !scenario.find_child("side", "shroud", "yes")) {
map_data = scenario["map_data"].str();
if (map_data.empty() && scenario.has_attribute("map")) {
try {
map_data = read_map(scenario["map"]);
} catch(io_exception& e) {
ERR_G << "could not read map '" << scenario["map"] << "': " << e.what() << '\n';
}
}
}
}
surface map_surf(NULL);
if(map_data.empty() == false) {
LOG_DP << "When parsing save summary " << ((*info_)[index_]).name() << std::endl
<< "Did not find a map_data field. Looking in game config for a child [" << summary["campaign_type"] << "] with id " << summary["scenario"] << std::endl;
assert(game_config_ && "ran into a null game config pointer inside a game preview pane");
const std::map<std::string,surface>::const_iterator itor = map_cache_.find(map_data);
if(itor != map_cache_.end()) {
map_surf = itor->second;
} else if(map_ != NULL) {
try {
const int minimap_size = 100;
map_->read(map_data);
map_surf = image::getMinimap(minimap_size, minimap_size, *map_);
if(map_surf != NULL) {
map_cache_.insert(std::pair<std::string,surface>(map_data, map_surf));
}
} catch(incorrect_map_format_error& e) {
ERR_CF << "map could not be loaded: " << e.message << '\n';
} catch(twml_exception& e) {
ERR_CF << "map could not be loaded: " << e.dev_message << '\n';
}
}
}
if(map_surf != NULL) {
// Align the map to the left when the leader image is missing.
const int map_x = have_leader_image ? area.x + area.w - map_surf->w : area.x;
SDL_Rect map_rect = sdl::create_rect(map_x
, area.y
, map_surf->w
, map_surf->h);
ypos = std::max<int>(ypos,map_rect.y + map_rect.h + save_preview_border);
sdl_blit(map_surf,NULL,screen,&map_rect);
} else {
LOG_DP << "Never found a map for savefile " << (*info_)[index_].name() << std::endl;
}
char time_buf[256] = {0};
const savegame::save_info& save = (*info_)[index_];
tm* tm_l = localtime(&save.modified());
if (tm_l) {
const size_t res = util::strftime(time_buf,sizeof(time_buf),
(preferences::use_twelve_hour_clock_format() ? _("%a %b %d %I:%M %p %Y") : _("%a %b %d %H:%M %Y")),
tm_l);
if(res == 0) {
time_buf[0] = 0;
}
} else {
LOG_NG << "localtime() returned null for time " << save.modified() << ", save " << save.name();
}
std::stringstream str;
str << font::BOLD_TEXT << font::NULL_MARKUP
<< (*info_)[index_].name() << '\n' << time_buf;
const std::string& campaign_type = summary["campaign_type"];
if (summary["corrupt"].to_bool()) {
str << "\n" << _("#(Invalid)");
} else {
str << "\n";
try {
game_classification::CAMPAIGN_TYPE ct = lexical_cast<game_classification::CAMPAIGN_TYPE> (campaign_type);
switch (ct) {
case game_classification::SCENARIO:
{
const std::string campaign_id = summary["campaign"];
const config *campaign = NULL;
if (!campaign_id.empty()) {
if (const config &c = game_config_->find_child("campaign", "id", campaign_id))
campaign = &c;
}
utils::string_map symbols;
if (campaign != NULL) {
symbols["campaign_name"] = (*campaign)["name"];
} else {
// Fallback to nontranslatable campaign id.
symbols["campaign_name"] = "(" + campaign_id + ")";
}
str << vgettext("Campaign: $campaign_name", symbols);
// Display internal id for debug purposes if we didn't above
if (game_config::debug && (campaign != NULL)) {
str << '\n' << "(" << campaign_id << ")";
}
break;
}
case game_classification::MULTIPLAYER:
str << _("Multiplayer");
break;
case game_classification::TUTORIAL:
str << _("Tutorial");
break;
case game_classification::TEST:
str << _("Test scenario");
break;
}
} catch (bad_lexical_cast &) {
str << campaign_type;
}
str << "\n";
if (summary["replay"].to_bool() && !summary["snapshot"].to_bool(true)) {
str << _("Replay");
} else if (!summary["turn"].empty()) {
str << _("Turn") << " " << summary["turn"];
} else {
str << _("Scenario start");
}
str << "\n" << _("Difficulty: ") << string_table[summary["difficulty"]];
if(!summary["version"].empty()) {
str << "\n" << _("Version: ") << summary["version"];
}
}
font::draw_text(&video(), area, font::SIZE_SMALL, font::NORMAL_COLOR, str.str(), area.x, ypos, true);
}
} // end anon namespace
std::string load_game_dialog(display& disp, const config& game_config, bool* select_difficulty, bool* show_replay, bool* cancel_orders)
{
std::vector<savegame::save_info> games;
{
cursor::setter cur(cursor::WAIT);
games = savegame::get_saves_list();
}
if(games.empty()) {
gui2::show_transient_message(disp.video(),
_("No Saved Games"),
_("There are no saved games to load.\n\n(Games are saved automatically when you complete a scenario)"));
return "";
}
const events::event_context context;
std::vector<std::string> items;
std::ostringstream heading;
heading << HEADING_PREFIX << _("Name") << COLUMN_SEPARATOR << _("Date");
items.push_back(heading.str());
std::vector<savegame::save_info>::const_iterator i;
for(i = games.begin(); i != games.end(); ++i) {
std::string name = i->name();
utf8::truncate(name, 40); // truncate only acts if the name is longer
std::ostringstream str;
str << name << COLUMN_SEPARATOR << util::format_time_summary(i->modified());
items.push_back(str.str());
}
gamemap map_obj(game_config, "");
gui::dialog lmenu(disp,
_("Load Game"),
"", gui::NULL_DIALOG);
lmenu.set_basic_behavior(gui::OK_CANCEL);
gui::menu::basic_sorter sorter;
sorter.set_alpha_sort(0).set_id_sort(1);
lmenu.set_menu(items, &sorter);
gui::filter_textbox* filter = new gui::filter_textbox(disp.video(), _("Filter: "), items, items, 1, lmenu);
lmenu.set_textbox(filter);
gui::dialog_button* change_difficulty_option = NULL;
if(select_difficulty != NULL) {
// implementation of gui::dialog::add_option, needed for storing a pointer to the option-box
change_difficulty_option = new gui::dialog_button(disp.video(), _("Change difficulty"), gui::button::TYPE_CHECK);
change_difficulty_option->set_check(false);
change_difficulty_option->set_help_string(_("Change campaign difficulty before loading"));
lmenu.add_button(change_difficulty_option, gui::dialog::BUTTON_CHECKBOX);
}
save_preview_pane save_preview(disp.video(),game_config,&map_obj,games,*filter,change_difficulty_option);
lmenu.add_pane(&save_preview);
// create an option for whether the replay should be shown or not
if(show_replay != NULL) {
lmenu.add_option(_("Show replay"), false,
gui::dialog::BUTTON_CHECKBOX_LEFT,
_("Play the embedded replay from the saved game if applicable")
);
}
if(cancel_orders != NULL) {
lmenu.add_option(_("Cancel orders"), false,
gui::dialog::BUTTON_CHECKBOX_LEFT,
_("Cancel any pending unit movements in the saved game")
);
}
lmenu.add_button(new gui::standard_dialog_button(disp.video(),_("OK"),0,false), gui::dialog::BUTTON_STANDARD);
lmenu.add_button(new gui::standard_dialog_button(disp.video(),_("Cancel"),1,true), gui::dialog::BUTTON_STANDARD);
delete_save save_deleter(disp,*filter,games);
gui::dialog_button_info delete_button(&save_deleter,_("Delete Save"));
lmenu.add_button(delete_button,
// Beware load screen glitches at low res!
gui::dialog::BUTTON_HELP);
int res = lmenu.show();
if(res == -1)
return "";
res = filter->get_index(res);
int option_index = 0;
if (select_difficulty != NULL) {
*select_difficulty = lmenu.option_checked(option_index++) && change_difficulty_option->enabled();
}
if(show_replay != NULL) {
*show_replay = lmenu.option_checked(option_index++);
const config& summary = games[res].summary();
if (summary["replay"].to_bool() && !summary["snapshot"].to_bool(true)) {
*show_replay = true;
}
}
if (cancel_orders != NULL) {
*cancel_orders = lmenu.option_checked(option_index++);
}
return games[res].name();
}
namespace {
static const int unit_preview_border = 10;
}
unit_preview_pane::details::details() :
image(),
name(),
type_name(),
race(),
level(0),
alignment(),
traits(),
abilities(),