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