The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
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 <SDL3/SDL_events.h>
21
22class display;
23
24namespace hotkey
25{
27
28/** Returns action_state::on if @a condition is true, else action_state::off. */
29inline 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. */
35inline 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)
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 explicit ui_command(std::string_view id, int index = -1)
68 {
69 // Only set the command from the associated hotkey_command, not the ID.
70 // There are cases (specifically, autoload items) with which no command
71 // is associated, yet whose ID should be stored unmodified.
73 }
74};
75
76// Abstract base class for objects that implement the ability
77// to execute hotkey commands.
79{
80protected:
81 virtual ~command_executor() {}
82
83public:
84 virtual void cycle_units() {}
85 virtual void cycle_back_units() {}
86 virtual void end_turn() {}
87 virtual void goto_leader() {}
88 virtual void unit_hold_position() {}
89 virtual void end_unit_turn() {}
90 virtual void undo() {}
91 virtual void redo() {}
92 virtual void terrain_description() {}
93 virtual void unit_description() {}
94 virtual void rename_unit() {}
95 virtual void save_game() {}
96 virtual void save_replay() {}
97 virtual void save_map() {}
98 virtual void load_game() {}
99 virtual void toggle_ellipses() {}
100 virtual void toggle_grid() {}
101 virtual void status_table() {}
102 virtual void recall() {}
103 virtual void recruit() {}
104 virtual void repeat_recruit() {}
105 virtual void speak() {}
106 virtual void whisper() {}
107 virtual void shout() {}
108 virtual void create_unit() {}
109 virtual void change_side() {}
110 virtual void kill_unit() {}
111 virtual void preferences() {}
112 virtual void objectives() {}
113 virtual void unit_list() {}
114 virtual void show_statistics() {}
115 virtual void stop_network() {}
116 virtual void start_network() {}
117 virtual void label_terrain(bool /*team_only*/) {}
118 virtual void clear_labels() {}
119 virtual void label_settings() {}
120 virtual void show_enemy_moves(bool /*ignore_units*/) {}
121 virtual void toggle_shroud_updates() {}
122 virtual void update_shroud_now() {}
123 virtual void continue_move() {}
124 virtual void search() {}
125 virtual void show_help() {}
126 virtual void show_chat_log() {}
127 virtual void user_command() {}
128 virtual void custom_command() {}
129 virtual void ai_formula() {}
130 virtual void clear_messages() {}
131 virtual void change_language() {}
132 virtual void play_replay() { }
133 virtual void reset_replay() {}
134 virtual void stop_replay() {}
135 virtual void replay_next_turn() { }
136 virtual void replay_next_side() { }
137 virtual void replay_next_move() { }
138 virtual void replay_show_everything() {}
139 virtual void replay_show_each() {}
140 virtual void replay_show_team1() {}
141 virtual void replay_skip_animation() {}
142 virtual void replay_exit() {}
143 virtual void whiteboard_toggle() {}
146 virtual void whiteboard_delete_action() {}
149 virtual void whiteboard_suppose_dead() {}
150 virtual void select_hex() {}
151 virtual void deselect_hex() {}
152 virtual void move_action() {}
153 virtual void select_and_action() {}
154 virtual void select_teleport() {}
155 virtual void touch_hex() {}
156 virtual void toggle_accelerated_speed() {}
157 virtual void scroll_up(bool /*on*/) {}
158 virtual void scroll_down(bool /*on*/) {}
159 virtual void scroll_left(bool /*on*/) {}
160 virtual void scroll_right(bool /*on*/) {}
161 virtual void lua_console();
162 virtual void zoom_in() {}
163 virtual void zoom_out() {}
164 virtual void zoom_default() {}
165 virtual void map_screenshot() {}
166 virtual void surrender_quit_game() {}
167
168 virtual void set_button_state() {}
169 virtual void recalculate_minimap() {}
170
171 void surrender_game();
173
174 // @a items_arg the items in the menus to be shows, each item can have the following attributes:
175 // 'id': The id describing the action, will be passed to do_execute_command and can_execute_command,
176 // If 'id' specifies a known hotkey command or theme item the other attributes can be generated from it.
177 // 'label': The label for this menu entry.
178 // 'icon': The icon for this menu entry.
179 virtual void show_menu(const std::vector<config>& items_arg, const point& menu_loc, bool context_menu);
180
181 // @a items_arg the actions to be executed, executes all of the actions, it looks like the idea is to associate
182 // multiple actions with a single menu button, not sure whether it is actually used.
183 void execute_action(const std::vector<std::string>& items_arg);
184
185protected:
186 virtual bool can_execute_command(const hotkey::ui_command& command) const = 0;
187 virtual bool do_execute_command(const hotkey::ui_command& command, bool press = true, bool release = false);
188
189 // Does the action control a toggle switch? If so, return the state of the action (on or off).
191 {
193 }
194
195 /**
196 * Determines whether the command should be in the context menu or not.
197 * Independent of whether or not we can actually execute the command.
198 */
199 virtual bool in_context_menu(const hotkey::ui_command&) const
200 {
201 return true;
202 }
203
204private:
205 void populate_menu_controls(config& item, int index) const;
206 void populate_menu_item_info(config& item, int index) const;
207
208 /** If true, the menu will remain open after an item has been selected. */
209 virtual bool keep_menu_open() const
210 {
211 return false;
212 }
213
215 {
216 queued_command(const hotkey_command& command_, int index_, bool press_, bool release_)
217 : command(&command_), index(index_), press(press_), release(release_)
218 {}
219
221 int index;
222 bool press;
224 };
225
226 void execute_command_wrap(const queued_command& command);
227 std::vector<queued_command> filter_command_queue();
228
229 bool press_event_sent_ = false;
230 std::vector<queued_command> command_queue_;
231
232public:
233 void queue_command(const SDL_Event& event, int index = -1);
234 bool run_queued_commands();
235
237 {
238 press_event_sent_ = false;
239 }
240};
241
243{
244protected:
245 virtual display& get_display() = 0;
246public:
247 void set_button_state();
248 void recalculate_minimap();
249 void lua_console();
250 void zoom_in();
251 void zoom_out();
252 void zoom_default();
253 void map_screenshot();
254};
255
256/* Functions to be called every time a event is intercepted.
257 * Will call the relevant function in executor if the event is not nullptr.
258 * Also handles some events in the function itself,
259 * and so is still meaningful to call with executor=nullptr
260 */
261void jbutton_event(const SDL_Event& event, command_executor* executor);
262void jhat_event(const SDL_Event& event, command_executor* executor);
263void key_event(const SDL_Event& event, command_executor* executor);
264void keyup_event(const SDL_Event& event, command_executor* executor);
265void mbutton_event(const SDL_Event& event, command_executor* executor);
266// Function to call to process the events.
267void run_events(command_executor* executor);
268
269}
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
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 show_menu(const std::vector< config > &items_arg, const point &menu_loc, bool context_menu)
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 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()
void populate_menu_item_info(config &item, int index) const
virtual void scroll_down(bool)
virtual bool in_context_menu(const hotkey::ui_command &) const
Determines whether the command should be in the context menu or not.
virtual void label_terrain(bool)
virtual void whiteboard_bump_up_action()
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)
void populate_menu_controls(config &item, int index) const
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 id.
void keyup_event(const SDL_Event &, command_executor *executor)
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.
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)
Holds a 2D point.
Definition point.hpp:25