1 /*
2    Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3    Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY.
11 
12    See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "hotkey_command.hpp"
18 #include "game_end_exceptions.hpp"
19 
20 class display;
21 class CVideo;
22 
23 namespace hotkey {
24 
25 enum ACTION_STATE { ACTION_STATELESS, ACTION_ON, ACTION_OFF, ACTION_SELECTED, ACTION_DESELECTED };
26 
27 // Abstract base class for objects that implement the ability
28 // to execute hotkey commands.
29 class command_executor
30 {
31 
32 protected:
~command_executor()33 	virtual ~command_executor() {}
34 
35 public:
cycle_units()36 	virtual void cycle_units() {}
cycle_back_units()37 	virtual void cycle_back_units() {}
end_turn()38 	virtual void end_turn() {}
goto_leader()39 	virtual void goto_leader() {}
unit_hold_position()40 	virtual void unit_hold_position() {}
end_unit_turn()41 	virtual void end_unit_turn() {}
undo()42 	virtual void undo() {}
redo()43 	virtual void redo() {}
terrain_description()44 	virtual void terrain_description() {}
unit_description()45 	virtual void unit_description() {}
rename_unit()46 	virtual void rename_unit() {}
save_game()47 	virtual void save_game() {}
save_replay()48 	virtual void save_replay() {}
save_map()49 	virtual void save_map() {}
load_game()50 	virtual void load_game() {}
toggle_ellipses()51 	virtual void toggle_ellipses() {}
toggle_grid()52 	virtual void toggle_grid() {}
status_table()53 	virtual void status_table() {}
recall()54 	virtual void recall() {}
recruit()55 	virtual void recruit() {}
repeat_recruit()56 	virtual void repeat_recruit() {}
speak()57 	virtual void speak() {}
whisper()58 	virtual void whisper() {}
shout()59 	virtual void shout() {}
create_unit()60 	virtual void create_unit() {}
change_side()61 	virtual void change_side() {}
kill_unit()62 	virtual void kill_unit() {}
preferences()63 	virtual void preferences() {}
objectives()64 	virtual void objectives() {}
unit_list()65 	virtual void unit_list() {}
show_statistics()66 	virtual void show_statistics() {}
stop_network()67 	virtual void stop_network() {}
start_network()68 	virtual void start_network() {}
label_terrain(bool)69 	virtual void label_terrain(bool /*team_only*/) {}
clear_labels()70 	virtual void clear_labels() {}
label_settings()71 	virtual void label_settings() {}
show_enemy_moves(bool)72 	virtual void show_enemy_moves(bool /*ignore_units*/) {}
toggle_shroud_updates()73 	virtual void toggle_shroud_updates() {}
update_shroud_now()74 	virtual void update_shroud_now() {}
continue_move()75 	virtual void continue_move() {}
search()76 	virtual void search() {}
show_help()77 	virtual void show_help() {}
show_chat_log()78 	virtual void show_chat_log() {}
user_command()79 	virtual void user_command() {}
custom_command()80 	virtual void custom_command() {}
ai_formula()81 	virtual void ai_formula() {}
clear_messages()82 	virtual void clear_messages() {}
change_language()83 	virtual void change_language() {}
play_replay()84 	virtual void play_replay() {  }
reset_replay()85 	virtual void reset_replay() {}
stop_replay()86 	virtual void stop_replay() {}
replay_next_turn()87 	virtual void replay_next_turn() {  }
replay_next_side()88 	virtual void replay_next_side() {  }
replay_next_move()89 	virtual void replay_next_move() {  }
replay_show_everything()90 	virtual void replay_show_everything() {}
replay_show_each()91 	virtual void replay_show_each() {}
replay_show_team1()92 	virtual void replay_show_team1() {}
replay_skip_animation()93 	virtual void replay_skip_animation() {}
replay_exit()94 	virtual void replay_exit() {}
whiteboard_toggle()95 	virtual void whiteboard_toggle() {}
whiteboard_execute_action()96 	virtual void whiteboard_execute_action() {}
whiteboard_execute_all_actions()97 	virtual void whiteboard_execute_all_actions() {}
whiteboard_delete_action()98 	virtual void whiteboard_delete_action() {}
whiteboard_bump_up_action()99 	virtual void whiteboard_bump_up_action() {}
whiteboard_bump_down_action()100 	virtual void whiteboard_bump_down_action() {}
whiteboard_suppose_dead()101 	virtual void whiteboard_suppose_dead() {}
select_hex()102 	virtual void select_hex() {}
deselect_hex()103 	virtual void deselect_hex() {}
move_action()104 	virtual void move_action() {}
select_and_action()105 	virtual void select_and_action() {}
touch_hex()106 	virtual void touch_hex() {}
left_mouse_click()107 	virtual void left_mouse_click() {}
right_mouse_click()108 	virtual void right_mouse_click() {}
toggle_accelerated_speed()109 	virtual void toggle_accelerated_speed() {}
scroll_up(bool)110 	virtual void scroll_up(bool /*on*/) {}
scroll_down(bool)111 	virtual void scroll_down(bool /*on*/) {}
scroll_left(bool)112 	virtual void scroll_left(bool /*on*/) {}
scroll_right(bool)113 	virtual void scroll_right(bool /*on*/) {}
114 	virtual void lua_console();
zoom_in()115 	virtual void zoom_in() {}
zoom_out()116 	virtual void zoom_out() {}
zoom_default()117 	virtual void zoom_default() {}
map_screenshot()118 	virtual void map_screenshot() {}
surrender_quit_game()119 	virtual void surrender_quit_game() {}
120 
set_button_state()121 	virtual void set_button_state() {}
recalculate_minimap()122 	virtual void recalculate_minimap() {}
123 
124 	// execute_command's parameter is changed to "hotkey_command& command" and this not maybe that is too inconsistent.
125 	// Gets the action's image (if any). Displayed left of the action text in menus.
get_action_image(hotkey::HOTKEY_COMMAND,int) const126 	virtual std::string get_action_image(hotkey::HOTKEY_COMMAND /*command*/, int /*index*/) const { return ""; }
127 	// Does the action control a toggle switch? If so, return the state of the action (on or off).
get_action_state(hotkey::HOTKEY_COMMAND,int) const128 	virtual ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND /*command*/, int /*index*/) const { return ACTION_STATELESS; }
129 	// Returns the appropriate menu image. Checkable items will get a checked/unchecked image.
130 	std::string get_menu_image(display& disp, const std::string& command, int index=-1) const;
131 	// Returns a vector of images for a given menu.
132 	void get_menu_images(display &, std::vector<config>& items);
133 	void surrender_game();
134 	virtual void show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& gui);
135 	void execute_action(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& gui);
136 
137 	virtual bool can_execute_command(const hotkey_command& command, int index=-1) const = 0;
138 	void queue_command(const SDL_Event& event, int index = -1);
139 	bool run_queued_commands();
execute_quit_command()140 	void execute_quit_command()
141 	{
142 		const hotkey_command& quit_hotkey = hotkey_command::get_command_by_command(hotkey::HOTKEY_QUIT_GAME);
143 		do_execute_command(quit_hotkey);
144 	}
145 
handle_keyup()146 	void handle_keyup()
147 	{
148 		press_event_sent_ = false;
149 	}
150 
151 protected:
152 	virtual bool do_execute_command(const hotkey_command& command, int index=-1, bool press=true, bool release=false);
153 
154 private:
155 	struct queued_command
156 	{
queued_commandhotkey::command_executor::queued_command157 		queued_command(const hotkey_command& command_, int index_, bool press_, bool release_)
158 			: command(&command_), index(index_), press(press_), release(release_)
159 		{}
160 
161 		const hotkey_command* command;
162 		int index;
163 		bool press;
164 		bool release;
165 	};
166 
167 	void execute_command_wrap(const queued_command& command);
168 	std::vector<queued_command> filter_command_queue();
169 
170 	bool press_event_sent_ = false;
171 	std::vector<queued_command> command_queue_;
172 };
173 class command_executor_default : public command_executor
174 {
175 protected:
176 	virtual display& get_display() = 0;
177 public:
178 	void set_button_state();
179 	void recalculate_minimap();
180 	void lua_console();
181 	void zoom_in();
182 	void zoom_out();
183 	void zoom_default();
184 	void map_screenshot();
185 	void quit_to_main_menu();
186 };
187 /* Functions to be called every time a event is intercepted.
188  * Will call the relevant function in executor if the event is not nullptr.
189  * Also handles some events in the function itself,
190  * and so is still meaningful to call with executor=nullptr
191  */
192 void jbutton_event(const SDL_Event& event, command_executor* executor);
193 void jhat_event(const SDL_Event& event, command_executor* executor);
194 void key_event(const SDL_Event& event, command_executor* executor);
195 void keyup_event(const SDL_Event& event, command_executor* executor);
196 void mbutton_event(const SDL_Event& event, command_executor* executor);
197 // Function to call to process the events.
198 void run_events(command_executor* executor);
199 
200 }
201