The Battle for Wesnoth  1.15.2+dev
command_executor.hpp
Go to the documentation of this file.
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 
26 
27 // Abstract base class for objects that implement the ability
28 // to execute hotkey commands.
30 {
31 
32 protected:
33  virtual ~command_executor() {}
34 
35 public:
36  virtual void cycle_units() {}
37  virtual void cycle_back_units() {}
38  virtual void end_turn() {}
39  virtual void goto_leader() {}
40  virtual void unit_hold_position() {}
41  virtual void end_unit_turn() {}
42  virtual void undo() {}
43  virtual void redo() {}
44  virtual void terrain_description() {}
45  virtual void unit_description() {}
46  virtual void rename_unit() {}
47  virtual void save_game() {}
48  virtual void save_replay() {}
49  virtual void save_map() {}
50  virtual void load_game() {}
51  virtual void toggle_ellipses() {}
52  virtual void toggle_grid() {}
53  virtual void status_table() {}
54  virtual void recall() {}
55  virtual void recruit() {}
56  virtual void repeat_recruit() {}
57  virtual void speak() {}
58  virtual void whisper() {}
59  virtual void shout() {}
60  virtual void create_unit() {}
61  virtual void change_side() {}
62  virtual void kill_unit() {}
63  virtual void preferences() {}
64  virtual void objectives() {}
65  virtual void unit_list() {}
66  virtual void show_statistics() {}
67  virtual void stop_network() {}
68  virtual void start_network() {}
69  virtual void label_terrain(bool /*team_only*/) {}
70  virtual void clear_labels() {}
71  virtual void label_settings() {}
72  virtual void show_enemy_moves(bool /*ignore_units*/) {}
73  virtual void toggle_shroud_updates() {}
74  virtual void update_shroud_now() {}
75  virtual void continue_move() {}
76  virtual void search() {}
77  virtual void show_help() {}
78  virtual void show_chat_log() {}
79  virtual void user_command() {}
80  virtual void custom_command() {}
81  virtual void ai_formula() {}
82  virtual void clear_messages() {}
83  virtual void change_language() {}
84  virtual void play_replay() { }
85  virtual void reset_replay() {}
86  virtual void stop_replay() {}
87  virtual void replay_next_turn() { }
88  virtual void replay_next_side() { }
89  virtual void replay_next_move() { }
90  virtual void replay_show_everything() {}
91  virtual void replay_show_each() {}
92  virtual void replay_show_team1() {}
93  virtual void replay_skip_animation() {}
94  virtual void replay_exit() {}
95  virtual void whiteboard_toggle() {}
96  virtual void whiteboard_execute_action() {}
98  virtual void whiteboard_delete_action() {}
99  virtual void whiteboard_bump_up_action() {}
100  virtual void whiteboard_bump_down_action() {}
101  virtual void whiteboard_suppose_dead() {}
102  virtual void select_hex() {}
103  virtual void deselect_hex() {}
104  virtual void move_action() {}
105  virtual void select_and_action() {}
106  virtual void touch_hex() {}
107  virtual void left_mouse_click() {}
108  virtual void right_mouse_click() {}
109  virtual void toggle_accelerated_speed() {}
110  virtual void scroll_up(bool /*on*/) {}
111  virtual void scroll_down(bool /*on*/) {}
112  virtual void scroll_left(bool /*on*/) {}
113  virtual void scroll_right(bool /*on*/) {}
114  virtual void lua_console();
115  virtual void zoom_in() {}
116  virtual void zoom_out() {}
117  virtual void zoom_default() {}
118  virtual void map_screenshot() {}
119  virtual void surrender_quit_game() {}
120 
121  virtual void set_button_state() {}
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.
126  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).
128  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();
141  {
143  do_execute_command(quit_hotkey);
144  }
145 
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:
156  {
157  queued_command(const hotkey_command& command_, int index_, bool press_, bool release_)
158  : command(&command_), index(index_), press(press_), release(release_)
159  {}
160 
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 };
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 }
virtual void toggle_shroud_updates()
queued_command(const hotkey_command &command_, int index_, bool press_, bool release_)
Definition: video.hpp:31
General purpose widgets.
Stores all information related to functions that can be bound to hotkeys.
virtual void terrain_description()
void mbutton_event(const SDL_Event &event, command_executor *executor)
const std::vector< std::string > items
virtual void recalculate_minimap()
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
virtual void whiteboard_execute_action()
virtual void scroll_up(bool)
virtual void surrender_quit_game()
Keyboard shortcuts for game actions.
virtual void whiteboard_delete_action()
virtual bool can_execute_command(const hotkey_command &command, int index=-1) const =0
virtual void whiteboard_bump_up_action()
virtual void label_terrain(bool)
void run_events(command_executor *executor)
void get_menu_images(display &, std::vector< config > &items)
virtual void replay_show_team1()
void jbutton_event(const SDL_Event &event, command_executor *executor)
virtual void unit_hold_position()
virtual void scroll_right(bool)
virtual void show_menu(const std::vector< config > &items_arg, int xloc, int yloc, bool context_menu, display &gui)
void keyup_event(const SDL_Event &, command_executor *executor)
virtual void show_enemy_moves(bool)
void jhat_event(const SDL_Event &event, command_executor *executor)
void queue_command(const SDL_Event &event, int index=-1)
std::string get_menu_image(display &disp, const std::string &command, int index=-1) const
void execute_command_wrap(const queued_command &command)
virtual void scroll_left(bool)
std::vector< queued_command > command_queue_
virtual void update_shroud_now()
void execute_action(const std::vector< std::string > &items_arg, int xloc, int yloc, bool context_menu, display &gui)
virtual void replay_show_everything()
virtual void scroll_down(bool)
virtual std::string get_action_image(hotkey::HOTKEY_COMMAND, int) const
virtual bool do_execute_command(const hotkey_command &command, int index=-1, bool press=true, bool release=false)
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
virtual void whiteboard_suppose_dead()
void key_event(const SDL_Event &event, command_executor *executor)
virtual void whiteboard_bump_down_action()
virtual void whiteboard_execute_all_actions()
virtual ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND, int) const
std::vector< queued_command > filter_command_queue()
virtual void replay_skip_animation()
virtual void whiteboard_toggle()
static const hotkey_command & get_command_by_command(HOTKEY_COMMAND command)
the execute_command argument was changed from HOTKEY_COMMAND to hotkey_command, to be able to call it...
virtual void toggle_accelerated_speed()