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