The Battle for Wesnoth  1.19.0-dev
command_executor.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 
20 #include <SDL2/SDL_events.h>
21 
22 class display;
23 
24 namespace hotkey {
25 
27 
28 /// Used as the main paramneter for can_execute_command/do_execute_command
29 /// These functions are used to execute hotkeys but also to execute menu items,
30 /// (Most menu items point to the same action as a hotkey but not all)
31 struct ui_command
32 {
33  /// The hotkey::HOTKEY_COMMAND associated with this action, HOTKEY_NULL for actions that don't allow hotkey binding.
34  /// different actions of the ame type might have the same HOTKEY_COMMAND (like different wml menu items that allow
35  // hotkey bindings.). This is prefered to be used for comparision over id (for example being an enum makes it
36  /// imposible to make typos in the id and its faster, plus c++ unfortunateley doesn't allow switch statements with
37  // strings)
39  /// The string command, never empty, describes the action uniquely. when the action is the result of a menu click
40  // this matches the id element of the clicked item (the id paraemter of show_menu)
41  std::string id;
42  /// When this action was the result of a menu click, this is the index of the clicked item in the menu.
43  int index;
44  ui_command(hotkey::HOTKEY_COMMAND hotkey_command, std::string_view id, int index = -1)
46  , id(id)
47  , index(index)
48  { }
49  explicit ui_command(const hotkey::hotkey_command& cmd, int index = -1)
50  : ui_command(cmd.command, cmd.id, index)
51  { }
52  // the string @param id references must live longer than this object.
53  explicit ui_command(std::string_view id, int index = -1)
55  {
57  }
58 };
59 
60 // Abstract base class for objects that implement the ability
61 // to execute hotkey commands.
63 {
64 
65 protected:
66  virtual ~command_executor() {}
67 
68 public:
69  virtual void cycle_units() {}
70  virtual void cycle_back_units() {}
71  virtual void end_turn() {}
72  virtual void goto_leader() {}
73  virtual void unit_hold_position() {}
74  virtual void end_unit_turn() {}
75  virtual void undo() {}
76  virtual void redo() {}
77  virtual void terrain_description() {}
78  virtual void unit_description() {}
79  virtual void rename_unit() {}
80  virtual void save_game() {}
81  virtual void save_replay() {}
82  virtual void save_map() {}
83  virtual void load_game() {}
84  virtual void toggle_ellipses() {}
85  virtual void toggle_grid() {}
86  virtual void status_table() {}
87  virtual void recall() {}
88  virtual void recruit() {}
89  virtual void repeat_recruit() {}
90  virtual void speak() {}
91  virtual void whisper() {}
92  virtual void shout() {}
93  virtual void create_unit() {}
94  virtual void change_side() {}
95  virtual void kill_unit() {}
96  virtual void preferences() {}
97  virtual void objectives() {}
98  virtual void unit_list() {}
99  virtual void show_statistics() {}
100  virtual void stop_network() {}
101  virtual void start_network() {}
102  virtual void label_terrain(bool /*team_only*/) {}
103  virtual void clear_labels() {}
104  virtual void label_settings() {}
105  virtual void show_enemy_moves(bool /*ignore_units*/) {}
106  virtual void toggle_shroud_updates() {}
107  virtual void update_shroud_now() {}
108  virtual void continue_move() {}
109  virtual void search() {}
110  virtual void show_help() {}
111  virtual void show_chat_log() {}
112  virtual void user_command() {}
113  virtual void custom_command() {}
114  virtual void ai_formula() {}
115  virtual void clear_messages() {}
116  virtual void change_language() {}
117  virtual void play_replay() { }
118  virtual void reset_replay() {}
119  virtual void stop_replay() {}
120  virtual void replay_next_turn() { }
121  virtual void replay_next_side() { }
122  virtual void replay_next_move() { }
123  virtual void replay_show_everything() {}
124  virtual void replay_show_each() {}
125  virtual void replay_show_team1() {}
126  virtual void replay_skip_animation() {}
127  virtual void replay_exit() {}
128  virtual void whiteboard_toggle() {}
129  virtual void whiteboard_execute_action() {}
131  virtual void whiteboard_delete_action() {}
132  virtual void whiteboard_bump_up_action() {}
133  virtual void whiteboard_bump_down_action() {}
134  virtual void whiteboard_suppose_dead() {}
135  virtual void select_hex() {}
136  virtual void deselect_hex() {}
137  virtual void move_action() {}
138  virtual void select_and_action() {}
139  virtual void select_teleport() {}
140  virtual void touch_hex() {}
141  virtual void left_mouse_click() {}
142  virtual void right_mouse_click() {}
143  virtual void toggle_accelerated_speed() {}
144  virtual void scroll_up(bool /*on*/) {}
145  virtual void scroll_down(bool /*on*/) {}
146  virtual void scroll_left(bool /*on*/) {}
147  virtual void scroll_right(bool /*on*/) {}
148  virtual void lua_console();
149  virtual void zoom_in() {}
150  virtual void zoom_out() {}
151  virtual void zoom_default() {}
152  virtual void map_screenshot() {}
153  virtual void surrender_quit_game() {}
154 
155  virtual void set_button_state() {}
156  virtual void recalculate_minimap() {}
157 
158  // execute_command's parameter is changed to "hotkey_command& command" and this not maybe that is too inconsistent.
159  // Gets the action's image (if any). Displayed left of the action text in menus.
160  virtual std::string get_action_image(const hotkey::ui_command&) const { return ""; }
161  // Does the action control a toggle switch? If so, return the state of the action (on or off).
163  // Returns the appropriate menu image. Checkable items will get a checked/unchecked image.
164  std::string get_menu_image(display& disp, const std::string& command, int index=-1) const;
165  // Returns a vector of images for a given menu.
166  void get_menu_images(display &, std::vector<config>& items);
167  void surrender_game();
168  // @a items_arg the items in the menus to be shows, each item can have the folliwng attributes:
169  // 'id': The id describing the action, will be passed to do_execute_commnd and can_execute_commnd,
170  // If 'id' specifies a known hotkey command or theme item the other attributes can be generated from it.
171  // 'label': The label for this menu entry.
172  // 'icon': The icon for this menu entry.
173  virtual void show_menu(const std::vector<config>& items_arg, int xloc, int yloc, bool context_menu, display& gui);
174  // @a items_arg the actions to be exceuted, exceutes all of the actions, it looks like the idea is to associate
175  // multiple actions with a single menu button, not sure whether it is actually used.
176  void execute_action(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& gui);
177 
178  virtual bool can_execute_command(const hotkey::ui_command& command) const = 0;
179  void queue_command(const SDL_Event& event, int index = -1);
180  bool run_queued_commands();
182  {
184  do_execute_command(ui_command(quit_hotkey));
185  }
186 
188  {
189  press_event_sent_ = false;
190  }
191 
192 protected:
193  virtual bool do_execute_command(const hotkey::ui_command& command, bool press=true, bool release=false);
194 
195 private:
197  {
198  queued_command(const hotkey_command& command_, int index_, bool press_, bool release_)
199  : command(&command_), index(index_), press(press_), release(release_)
200  {}
201 
203  int index;
204  bool press;
205  bool release;
206  };
207 
208  void execute_command_wrap(const queued_command& command);
209  std::vector<queued_command> filter_command_queue();
210 
211  bool press_event_sent_ = false;
212  std::vector<queued_command> command_queue_;
213 };
215 {
216 protected:
217  virtual display& get_display() = 0;
218 public:
219  void set_button_state();
220  void recalculate_minimap();
221  void lua_console();
222  void zoom_in();
223  void zoom_out();
224  void zoom_default();
225  void map_screenshot();
227 };
228 /* Functions to be called every time a event is intercepted.
229  * Will call the relevant function in executor if the event is not nullptr.
230  * Also handles some events in the function itself,
231  * and so is still meaningful to call with executor=nullptr
232  */
233 void jbutton_event(const SDL_Event& event, command_executor* executor);
234 void jhat_event(const SDL_Event& event, command_executor* executor);
235 void key_event(const SDL_Event& event, command_executor* executor);
236 void keyup_event(const SDL_Event& event, command_executor* executor);
237 void mbutton_event(const SDL_Event& event, command_executor* executor);
238 // Function to call to process the events.
239 void run_events(command_executor* executor);
240 
241 }
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:88
virtual display & get_display()=0
virtual void scroll_right(bool)
virtual void replay_skip_animation()
virtual void scroll_up(bool)
virtual void scroll_left(bool)
virtual void whiteboard_execute_all_actions()
void get_menu_images(display &, std::vector< config > &items)
virtual bool do_execute_command(const hotkey::ui_command &command, bool press=true, bool release=false)
virtual void whiteboard_execute_action()
void execute_action(const std::vector< std::string > &items_arg, int xloc, int yloc, bool context_menu, display &gui)
virtual void show_enemy_moves(bool)
std::vector< queued_command > command_queue_
std::string get_menu_image(display &disp, const std::string &command, int index=-1) const
virtual ACTION_STATE get_action_state(const hotkey::ui_command &) const
virtual void toggle_shroud_updates()
virtual void whiteboard_bump_down_action()
void execute_command_wrap(const queued_command &command)
virtual void whiteboard_delete_action()
virtual void replay_show_everything()
virtual void toggle_accelerated_speed()
virtual void whiteboard_suppose_dead()
virtual void show_menu(const std::vector< config > &items_arg, int xloc, int yloc, bool context_menu, display &gui)
virtual void scroll_down(bool)
virtual void surrender_quit_game()
virtual void label_terrain(bool)
virtual void whiteboard_bump_up_action()
virtual void unit_hold_position()
virtual void terrain_description()
virtual void recalculate_minimap()
virtual std::string get_action_image(const hotkey::ui_command &) const
virtual bool can_execute_command(const hotkey::ui_command &command) const =0
std::vector< queued_command > filter_command_queue()
void queue_command(const SDL_Event &event, int index=-1)
General purpose widgets.
Keyboard shortcuts for game actions.
void jhat_event(const SDL_Event &event, command_executor *executor)
void key_event(const SDL_Event &event, command_executor *executor)
void mbutton_event(const SDL_Event &event, command_executor *executor)
void run_events(command_executor *executor)
void jbutton_event(const SDL_Event &event, command_executor *executor)
void keyup_event(const SDL_Event &, command_executor *executor)
const hotkey_command & get_hotkey_command(const std::string &command)
returns the hotkey_command with the given name
@ HOTKEY_QUIT_GAME
const std::vector< std::string > items
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:70
queued_command(const hotkey_command &command_, int index_, bool press_, bool release_)
Stores all information related to functions that can be bound to hotkeys.
HOTKEY_COMMAND command
The command associated with this hotkey.
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...
Used as the main paramneter for can_execute_command/do_execute_command These functions are used to ex...
ui_command(hotkey::HOTKEY_COMMAND hotkey_command, std::string_view id, int index=-1)
ui_command(const hotkey::hotkey_command &cmd, int index=-1)
hotkey::HOTKEY_COMMAND hotkey_command
The hotkey::HOTKEY_COMMAND associated with this action, HOTKEY_NULL for actions that don't allow hotk...
int index
When this action was the result of a menu click, this is the index of the clicked item in the menu.
std::string id
The string command, never empty, describes the action uniquely. when the action is the result of a me...
ui_command(std::string_view id, int index=-1)