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