-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
command_executor.cpp
715 lines (662 loc) · 17.7 KB
/
command_executor.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
/*
Copyright (C) 2003 - 2016 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.
*/
#include "command_executor.hpp"
#include "hotkey_item.hpp"
#include <boost/foreach.hpp>
#include "gui/dialogs/lua_interpreter.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/screenshot_notification.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/widgets/window.hpp"
#include "wml_separators.hpp"
#include "filesystem.hpp"
#include "construct_dialog.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "preferences.hpp"
#include "game_end_exceptions.hpp"
#include "display.hpp"
#include "quit_confirmation.hpp"
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <cassert>
static lg::log_domain log_config("config");
#define ERR_G LOG_STREAM(err, lg::general)
#define WRN_G LOG_STREAM(warn, lg::general)
#define LOG_G LOG_STREAM(info, lg::general)
#define DBG_G LOG_STREAM(debug, lg::general)
#define ERR_CF LOG_STREAM(err, log_config)
namespace {
bool screenshot(const std::string& filename, CVideo& video)
{
return image::save_image(video.getSurface(), filename);
}
template<typename TFunc>
void make_screenshot(const std::string& name, CVideo& video, const TFunc& func)
{
std::string filename = filesystem::get_screenshot_dir() + "/" + name + "_";
#ifdef HAVE_LIBPNG
const std::string ext = ".png";
#else
const std::string ext = ".bmp";
#endif
filename = filesystem::get_next_filename(filename, ext);
const bool res = func(filename);
if (res) {
gui2::tscreenshot_notification::display(filename, video);
} else {
gui2::show_error_message(video,
_("Screenshot creation failed.\n\n"
"Make sure there is enough space on the drive holding Wesnoth’s player resource files and that file permissions are set up correctly."));
}
}
}
namespace hotkey {
static void event_execute(const SDL_Event& event, command_executor* executor);
bool command_executor::execute_command(const hotkey_command& cmd, int /*index*/)
{
switch(cmd.id) {
case HOTKEY_CYCLE_UNITS:
cycle_units();
break;
case HOTKEY_CYCLE_BACK_UNITS:
cycle_back_units();
break;
case HOTKEY_ENDTURN:
end_turn();
break;
case HOTKEY_UNIT_HOLD_POSITION:
unit_hold_position();
break;
case HOTKEY_END_UNIT_TURN:
end_unit_turn();
break;
case HOTKEY_LEADER:
goto_leader();
break;
case HOTKEY_UNDO:
undo();
break;
case HOTKEY_REDO:
redo();
break;
case HOTKEY_TERRAIN_DESCRIPTION:
terrain_description();
break;
case HOTKEY_UNIT_DESCRIPTION:
unit_description();
break;
case HOTKEY_RENAME_UNIT:
rename_unit();
break;
case HOTKEY_SAVE_GAME:
save_game();
break;
case HOTKEY_SAVE_REPLAY:
save_replay();
break;
case HOTKEY_SAVE_MAP:
save_map();
break;
case HOTKEY_LOAD_GAME:
load_game();
break;
case HOTKEY_TOGGLE_ELLIPSES:
toggle_ellipses();
break;
case HOTKEY_TOGGLE_GRID:
toggle_grid();
break;
case HOTKEY_STATUS_TABLE:
status_table();
break;
case HOTKEY_RECALL:
recall();
break;
case HOTKEY_LABEL_SETTINGS:
label_settings();
break;
case HOTKEY_RECRUIT:
recruit();
break;
case hotkey::HOTKEY_REPEAT_RECRUIT:
repeat_recruit();
break;
case HOTKEY_SPEAK:
speak();
break;
case HOTKEY_SPEAK_ALLY:
whisper();
break;
case HOTKEY_SPEAK_ALL:
shout();
break;
case HOTKEY_CREATE_UNIT:
create_unit();
break;
case HOTKEY_CHANGE_SIDE:
change_side();
break;
case HOTKEY_KILL_UNIT:
kill_unit();
break;
case HOTKEY_PREFERENCES:
preferences();
break;
case HOTKEY_OBJECTIVES:
objectives();
break;
case HOTKEY_UNIT_LIST:
unit_list();
break;
case HOTKEY_STATISTICS:
show_statistics();
break;
case HOTKEY_STOP_NETWORK:
stop_network();
break;
case HOTKEY_START_NETWORK:
start_network();
break;
case HOTKEY_LABEL_TEAM_TERRAIN:
label_terrain(true);
break;
case HOTKEY_LABEL_TERRAIN:
label_terrain(false);
break;
case HOTKEY_CLEAR_LABELS:
clear_labels();
break;
case HOTKEY_SHOW_ENEMY_MOVES:
show_enemy_moves(false);
break;
case HOTKEY_BEST_ENEMY_MOVES:
show_enemy_moves(true);
break;
case HOTKEY_DELAY_SHROUD:
toggle_shroud_updates();
break;
case HOTKEY_UPDATE_SHROUD:
update_shroud_now();
break;
case HOTKEY_CONTINUE_MOVE:
continue_move();
break;
case HOTKEY_SEARCH:
search();
break;
case HOTKEY_HELP:
show_help();
break;
case HOTKEY_CHAT_LOG:
show_chat_log();
break;
case HOTKEY_USER_CMD:
user_command();
break;
case HOTKEY_CUSTOM_CMD:
custom_command();
break;
case HOTKEY_AI_FORMULA:
ai_formula();
break;
case HOTKEY_CLEAR_MSG:
clear_messages();
break;
case HOTKEY_LANGUAGE:
change_language();
break;
case HOTKEY_REPLAY_PLAY:
play_replay();
break;
case HOTKEY_REPLAY_RESET:
reset_replay();
break;
case HOTKEY_REPLAY_STOP:
stop_replay();
break;
case HOTKEY_REPLAY_NEXT_TURN:
replay_next_turn();
break;
case HOTKEY_REPLAY_NEXT_SIDE:
replay_next_side();
break;
case HOTKEY_REPLAY_NEXT_MOVE:
replay_next_move();
break;
case HOTKEY_REPLAY_SHOW_EVERYTHING:
replay_show_everything();
break;
case HOTKEY_REPLAY_SHOW_EACH:
replay_show_each();
break;
case HOTKEY_REPLAY_SHOW_TEAM1:
replay_show_team1();
break;
case HOTKEY_REPLAY_SKIP_ANIMATION:
replay_skip_animation();
break;
case HOTKEY_REPLAY_EXIT:
replay_exit();
break;
case HOTKEY_WB_TOGGLE:
whiteboard_toggle();
break;
case HOTKEY_WB_EXECUTE_ACTION:
whiteboard_execute_action();
break;
case HOTKEY_WB_EXECUTE_ALL_ACTIONS:
whiteboard_execute_all_actions();
break;
case HOTKEY_WB_DELETE_ACTION:
whiteboard_delete_action();
break;
case HOTKEY_WB_BUMP_UP_ACTION:
whiteboard_bump_up_action();
break;
case HOTKEY_WB_BUMP_DOWN_ACTION:
whiteboard_bump_down_action();
break;
case HOTKEY_WB_SUPPOSE_DEAD:
whiteboard_suppose_dead();
break;
case HOTKEY_SELECT_HEX:
select_hex();
break;
case HOTKEY_DESELECT_HEX:
deselect_hex();
break;
case HOTKEY_MOVE_ACTION:
move_action();
break;
case HOTKEY_SELECT_AND_ACTION:
select_and_action();
break;
case HOTKEY_ACCELERATED:
toggle_accelerated_speed();
break;
case LUA_CONSOLE:
lua_console();
break;
case HOTKEY_ZOOM_IN:
zoom_in();
break;
case HOTKEY_ZOOM_OUT:
zoom_out();
break;
case HOTKEY_ZOOM_DEFAULT:
zoom_default();
break;
case HOTKEY_MAP_SCREENSHOT:
map_screenshot();
break;
case HOTKEY_QUIT_TO_DESKTOP:
quit_to_desktop();
break;
case HOTKEY_QUIT_GAME:
quit_to_main_menu();
break;
default:
return false;
}
return true;
}
void command_executor::show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool /*context_menu*/, display& gui)
{
std::vector<std::string> items = items_arg;
if (items.empty()) return;
std::vector<std::string> menu = get_menu_images(gui, items);
int res = 0;
{
gui::dialog mmenu = gui::dialog(gui.video(),"","",
gui::MESSAGE, gui::dialog::hotkeys_style);
mmenu.set_menu(menu);
res = mmenu.show(xloc, yloc);
} // This will kill the dialog.
if (res < 0 || size_t(res) >= items.size()) return;
const theme::menu* submenu = gui.get_theme().get_menu_item(items[res]);
if (submenu) {
int y,x;
SDL_GetMouseState(&x,&y);
this->show_menu(submenu->items(), x, y, submenu->is_context(), gui);
} else {
const hotkey::hotkey_command& cmd = hotkey::get_hotkey_command(items[res]);
hotkey::execute_command(cmd,this,res);
set_button_state();
}
}
void command_executor::execute_action(const std::vector<std::string>& items_arg, int /*xloc*/, int /*yloc*/, bool /*context_menu*/, display&)
{
std::vector<std::string> items = items_arg;
if (items.empty()) {
return;
}
std::vector<std::string>::iterator i = items.begin();
while(i != items.end()) {
const hotkey_command &command = hotkey::get_hotkey_command(*i);
if (can_execute_command(command)) {
hotkey::execute_command(command, this);
set_button_state();
}
++i;
}
}
std::string command_executor::get_menu_image(display& disp, const std::string& command, int index) const {
const std::string base_image_name = "icons/action/" + command + "_25.png";
const std::string pressed_image_name = "icons/action/" + command + "_25-pressed.png";
const hotkey::HOTKEY_COMMAND hk = hotkey::get_id(command);
const hotkey::ACTION_STATE state = get_action_state(hk, index);
const theme::menu* menu = disp.get_theme().get_menu_item(command);
if (menu) {
return "icons/arrows/short_arrow_right_25.png~CROP(3,3,18,18)"; // TODO should not be hardcoded
}
if (filesystem::file_exists(game_config::path + "/images/" + base_image_name)) {
switch (state) {
case ACTION_ON:
case ACTION_SELECTED:
return pressed_image_name + "~CROP(3,3,18,18)";
default:
return base_image_name + "~CROP(3,3,18,18)";
}
}
switch (get_action_state(hk, index)) {
case ACTION_ON:
return game_config::images::checked_menu;
case ACTION_OFF:
return game_config::images::unchecked_menu;
case ACTION_SELECTED:
return game_config::images::selected_menu;
case ACTION_DESELECTED:
return game_config::images::deselected_menu;
default: return get_action_image(hk, index);
}
}
std::vector<std::string> command_executor::get_menu_images(display& disp, const std::vector<std::string>& items) {
std::vector<std::string> result;
bool has_image = false;
for (size_t i = 0; i < items.size(); ++i) {
std::string const& item = items[i];
const hotkey::HOTKEY_COMMAND hk = hotkey::get_id(item);
std::stringstream str;
//see if this menu item has an associated image
std::string img(get_menu_image(disp, item, i));
if (img.empty() == false) {
has_image = true;
str << IMAGE_PREFIX << img << COLUMN_SEPARATOR;
}
const theme::menu* menu = disp.get_theme().get_menu_item(item);
if (hk == hotkey::HOTKEY_NULL) {
if (menu)
str << menu->title();
else
str << item.substr(0, item.find_last_not_of(' ') + 1) << COLUMN_SEPARATOR;
} else {
if (menu)
str << menu->title();
else {
std::string desc = hotkey::get_description(item);
if (hk == HOTKEY_ENDTURN) {
const theme::action *b = disp.get_theme().get_action_item("button-endturn");
if (b) {
desc = b->title();
}
}
str << desc << COLUMN_SEPARATOR << hotkey::get_names(item);
}
}
result.push_back(str.str());
}
//If any of the menu items have an image, create an image column
if (has_image) {
for (std::vector<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
if (*(i->begin()) != IMAGE_PREFIX) {
i->insert(i->begin(), COLUMN_SEPARATOR);
}
}
}
return result;
}
basic_handler::basic_handler(command_executor* exec) : exec_(exec) {}
void basic_handler::handle_event(const SDL_Event& event)
{
switch (event.type) {
case SDL_KEYDOWN:
// If we're in a dialog we only want to handle items that are explicitly
// handled by the executor.
// If we're not in a dialog we can call the regular key event handler.
if (!gui::in_dialog()) {
key_event(event,exec_);
} else if (exec_ != NULL) {
event_execute(event,exec_);
}
break;
case SDL_JOYBUTTONDOWN:
if (!gui::in_dialog()) {
jbutton_event(event,exec_);
} else if (exec_ != NULL) {
event_execute(event,exec_);
}
break;
case SDL_MOUSEBUTTONDOWN:
if (!gui::in_dialog()) {
mbutton_event(event,exec_);
} else if (exec_ != NULL) {
event_execute(event,exec_);
}
break;
}
}
void mbutton_event(const SDL_Event& event, command_executor* executor)
{
event_execute(event, executor);
}
void jbutton_event(const SDL_Event& event, command_executor* executor)
{
event_execute(event, executor);
}
void jhat_event(const SDL_Event& event, command_executor* executor)
{
event_execute(event, executor);
}
void key_event(const SDL_Event& event, command_executor* executor)
{
if (!executor) return;
event_execute(event,executor);
}
static void event_execute( const SDL_Event& event, command_executor* executor)
{
if (!executor) return;
const hotkey_ptr hk = get_hotkey(event);
if (!hk->active()) {
return;
}
execute_command(hotkey::get_hotkey_command(hk->get_command()), executor);
executor->set_button_state();
}
void execute_command(const hotkey_command& command, command_executor* executor, int index)
{
if (executor != NULL) {
if (!executor->can_execute_command(command, index)
|| executor->execute_command(command, index)) {
return;
}
}
switch (command.id) {
case HOTKEY_MINIMAP_DRAW_TERRAIN:
preferences::toggle_minimap_draw_terrain();
executor->recalculate_minimap();
break;
case HOTKEY_MINIMAP_CODING_TERRAIN:
preferences::toggle_minimap_terrain_coding();
executor->recalculate_minimap();
break;
case HOTKEY_MINIMAP_CODING_UNIT:
preferences::toggle_minimap_movement_coding();
executor->recalculate_minimap();
break;
case HOTKEY_MINIMAP_DRAW_UNITS:
preferences::toggle_minimap_draw_units();
executor->recalculate_minimap();
break;
case HOTKEY_MINIMAP_DRAW_VILLAGES:
preferences::toggle_minimap_draw_villages();
executor->recalculate_minimap();
break;
case HOTKEY_FULLSCREEN:
executor->get_video().set_fullscreen(!preferences::fullscreen());
break;
case HOTKEY_SCREENSHOT:
make_screenshot(_("Screenshot"), executor->get_video(), boost::bind(&::screenshot, _1, boost::ref(executor->get_video())));
break;
case HOTKEY_ANIMATE_MAP:
preferences::set_animate_map(!preferences::animate_map());
break;
case HOTKEY_MOUSE_SCROLL:
preferences::enable_mouse_scroll(!preferences::mouse_scroll_enabled());
break;
case HOTKEY_MUTE:
{
// look if both is not playing
static struct before_muted_s
{
bool playing_sound,playing_music;
before_muted_s() : playing_sound(false),playing_music(false){}
} before_muted;
if (preferences::music_on() || preferences::sound_on())
{
// then remember settings and mute both
before_muted.playing_sound = preferences::sound_on();
before_muted.playing_music = preferences::music_on();
preferences::set_sound(false);
preferences::set_music(false);
}
else
{
// then set settings before mute
preferences::set_sound(before_muted.playing_sound);
preferences::set_music(before_muted.playing_music);
}
}
break;
default:
DBG_G << "command_executor: unknown command number " << command.id << ", ignoring.\n";
break;
}
}
void command_executor_default::set_button_state()
{
display& disp = get_display();
BOOST_FOREACH(const theme::menu& menu, disp.get_theme().menus()) {
gui::button* button = disp.find_menu_button(menu.get_id());
if (!button) continue;
bool enabled = false;
BOOST_FOREACH(const std::string& command, menu.items()) {
const hotkey::hotkey_command& command_obj = hotkey::get_hotkey_command(command);
bool can_execute = can_execute_command(command_obj);
if (can_execute) {
enabled = true;
break;
}
}
button->enable(enabled);
}
BOOST_FOREACH(const theme::action& action, disp.get_theme().actions()) {
gui::button* button = disp.find_action_button(action.get_id());
if (!button) continue;
bool enabled = false;
int i = 0;
BOOST_FOREACH(const std::string& command, action.items()) {
const hotkey::hotkey_command& command_obj = hotkey::get_hotkey_command(command);
std::string tooltip = action.tooltip(i);
if (filesystem::file_exists(game_config::path + "/images/icons/action/" + command + "_25.png" ))
button->set_overlay("icons/action/" + command);
if (!tooltip.empty())
button->set_tooltip_string(tooltip);
bool can_execute = can_execute_command(command_obj);
i++;
if (!can_execute) continue;
enabled = true;
ACTION_STATE state = get_action_state(command_obj.id, -1);
switch (state) {
case ACTION_SELECTED:
case ACTION_ON:
button->set_check(true);
break;
case ACTION_OFF:
case ACTION_DESELECTED:
button->set_check(false);
break;
case ACTION_STATELESS:
break;
default:
break;
}
break;
}
button->enable(enabled);
}
}
void command_executor_default::recalculate_minimap()
{
get_display().recalculate_minimap();
}
CVideo& command_executor_default::get_video()
{
return get_display().video();
}
void command_executor_default::lua_console()
{
if (get_display().in_game()) {
gui2::tlua_interpreter::display(get_video(), gui2::tlua_interpreter::GAME);
//WRN_G << "caution: attempting to interface console with game lua kernel when we are not in game...\n";
gui2::tlua_interpreter::display(get_video(), gui2::tlua_interpreter::APP);
} else {
command_executor::lua_console();
}
}
void command_executor::lua_console()
{
gui2::tlua_interpreter::display(get_video(), gui2::tlua_interpreter::APP);
}
void command_executor_default::zoom_in()
{
get_display().set_zoom(zoom_amount);
}
void command_executor_default::zoom_out()
{
get_display().set_zoom(-zoom_amount);
}
void command_executor_default::zoom_default()
{
get_display().set_default_zoom();
}
void command_executor_default::map_screenshot()
{
make_screenshot(_("Map-Screenshot"), get_video(), boost::bind(&display::screenshot, &get_display(), _1, true));
}
void command_executor_default::quit_to_desktop()
{
if(quit_confirmation::default_promt()) {
throw CVideo::quit();
}
}
void command_executor::quit_to_desktop()
{
throw CVideo::quit();
}
void command_executor_default::quit_to_main_menu()
{
if(quit_confirmation::default_promt()) {
throw_quit_game_exception();
}
}
}