The Battle for Wesnoth  1.19.10+dev
hotkey_command.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 "global.hpp"
19 #include "tooltips.hpp"
20 #include "tstring.hpp"
21 
22 #include <bitset>
23 #include <functional>
24 #include <map>
25 
26 class config;
27 
28 namespace hotkey {
29 
30 /**
31  * Available hotkey scopes. The scope is used to allow command from
32  * non-overlapping areas of the game share the same key
33  */
34 enum scope {
39 };
40 
41 // For some reason std::bitset::operator| is not constexpr, so we'll construct the bitset with these values
42 // FIXME: unify these with the enum above. Right now these are the proper bitmasks to initialize a bitset,
43 // while the values above are used as indices to access the bits of the bitset.
44 constexpr uint32_t scope_game = 1 << SCOPE_GAME;
45 constexpr uint32_t scope_editor = 1 << SCOPE_EDITOR;
46 constexpr uint32_t scope_main = 1 << SCOPE_MAIN_MENU;
47 
67 
68  // Replay
74 
75  // Controls
78 
79  // Camera movement
81 
82  // Dialog control
84 
85  // Whiteboard commands
91 
92  // Misc.
99 
100  // Minimap
103 
104  // Multiplayer
106 
107  /* Gui2 specific hotkeys. */
120 
122 
123  /* Editor commands */
127 
128  // Palette
131 
137 
139 
140  // Unit
143 
144  // Brushes
147 
148  // Tools
153 
154  // Select
156  // Clipboard
160  // Selection
166 
167  // Map
177 
178  // Transitions
181 
182  // Refresh
185 
186  // Draw
188 
189  // Side
193 
194  // Area
199 
200  // Addons
205 
206  // Scenario
210 
211  /* This item must stay at the end since it is used as terminator for iterating. */
213 };
214 
229  HKCAT_PLACEHOLDER // Keep this one last
230 };
231 
232 /** Gets the display name for a given hotkey category. */
234 
235 typedef std::bitset<SCOPE_COUNT> hk_scopes;
236 
237 /**
238  * hotkey_command uses t_string which might cause bugs when used at program startup,
239  * so use this for the master hotkey list (and only there).
240  */
241 struct hotkey_command_temp;
242 
243 /**
244  * Stores all information related to functions that can be bound to hotkeys.
245  * this is currently a semi struct: it haves a constructor, but only const-public members.
246  */
248 {
249  hotkey_command() = delete;
250 
251  /** Constructs a new command from a temporary static hotkey object. */
252  hotkey_command(const hotkey_command_temp& temp_command);
253 
254  /** @todo: see if we can remove this with c++20. Aggregate initialization with try_emplace?*/
255  hotkey_command(HOTKEY_COMMAND cmd, const std::string& id, const t_string& desc, bool hidden, bool toggle, hk_scopes scope, HOTKEY_CATEGORY category, const t_string& tooltip);
256 
257  hotkey_command(const hotkey_command&) = default;
259 
260  /** The command associated with this hotkey. Does not need to be unique. */
262 
263  /** The unique ID. */
264  std::string id;
265 
266  // since the wml_menu hotkey_command s can have different textdomains we need t_string now.
268 
269  /** If hidden then don't show the command in the hotkey preferences. */
270  bool hidden;
271 
272  /**
273  * Toggle hotkeys have some restrictions on what can be bound to them.
274  * They require a binding that has two states, "pressed" and "released"
275  */
276  bool toggle;
277 
278  /** The visibility scope of the command. */
280 
281  /** The category of the command. */
283 
285 
286  /** checks weather this is the null hotkey_command */
287  bool null() const;
288 
289  /** returns the command that is treated as null */
290  static const hotkey_command& null_command();
291 
292  /**
293  * the execute_command argument was changed from HOTKEY_COMMAND to hotkey_command,
294  * to be able to call it with HOTKEY_COMMAND, this function was created
295  */
297 };
298 
300 {
301 public:
302  scope_changer();
303  explicit scope_changer(hk_scopes new_scopes, bool restore = true);
304  ~scope_changer();
305 private:
307  const bool restore_;
308 };
309 
310 /**
311  * returns a container that contains all currently active hotkey_commands.
312  * everything that wants a hotkey, must be in this container
313  */
314 const std::map<std::string_view, hotkey::hotkey_command>& get_hotkey_commands();
315 
316 /** returns the hotkey_command with the given name */
317 NOT_DANGLING const hotkey_command& get_hotkey_command(const std::string& command);
318 
319 bool is_scope_active(scope s);
321 
322 bool has_hotkey_command(const std::string& id);
323 
324 /**
325  * RAII helper class to control the lifetime of a WML hotkey_command.
326  */
328 {
329 public:
330  /** Don't allow copying so objects don't get erased early. */
333 
334  /** But we *do* want move semantics. */
337 
338  /** Registers a hotkey_command for a WML hotkey with the given ID if one does not already exist. */
339  wml_hotkey_record(const std::string& id, const t_string& description, const config& default_hotkey);
340 
342 
343 private:
344  /** Handles removing the associated hotkey_command on this object's destruction. */
345  std::function<void()> cleanup_{};
346 };
347 
348 void init_hotkey_commands();
349 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
RAII helper class to control the lifetime of a WML hotkey_command.
wml_hotkey_record(const wml_hotkey_record &)=delete
Don't allow copying so objects don't get erased early.
const wml_hotkey_record & operator=(const wml_hotkey_record &)=delete
std::function< void()> cleanup_
Handles removing the associated hotkey_command on this object's destruction.
wml_hotkey_record(wml_hotkey_record &&)=default
But we do want move semantics.
wml_hotkey_record & operator=(wml_hotkey_record &&)=default
#define NOT_DANGLING
Definition: global.hpp:65
Keyboard shortcuts for game actions.
const hotkey_command & get_hotkey_command(const std::string &command)
returns the hotkey_command with the given name
bool has_hotkey_command(const std::string &id)
constexpr uint32_t scope_game
void init_hotkey_commands()
const std::map< std::string_view, hotkey::hotkey_command > & get_hotkey_commands()
returns a container that contains all currently active hotkey_commands.
std::bitset< SCOPE_COUNT > hk_scopes
bool is_scope_active(scope s)
scope
Available hotkey scopes.
constexpr uint32_t scope_main
t_string get_translatable_category_name(HOTKEY_CATEGORY category)
Gets the display name for a given hotkey category.
constexpr uint32_t scope_editor
@ HOTKEY_EDITOR_TOOL_VILLAGE
@ HOTKEY_MINIMAP_DRAW_VILLAGES
@ HOTKEY_EDITOR_BRUSH_NW_SE
@ HOTKEY_EDITOR_REFRESH
@ HOTKEY_FULLSCREEN
@ HOTKEY_EDITOR_SELECT_NONE
@ HOTKEY_OBJECTIVES
@ HOTKEY_ANIMATE_MAP
@ HOTKEY_SCREENSHOT
@ HOTKEY_TELEP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PTION
@ HOTKEY_UPDATE_SHROUD
@ HOTKEY_REPEAT_RECRUIT
@ TITLE_SCREEN__CREDITS
@ HOTKEY_REPLAY_STOP
@ HOTKEY_KILL_UNIT
@ HOTKEY_SCROLL_RIGHT
@ HOTKEY_SAVE_REPLAY
@ HOTKEY_EDITOR_SELECT_ALL
@ HOTKEY_LABEL_TEAM_TERRAIN
@ HOTKEY_EDITOR_DRAW_COORDINATES
@ HOTKEY_UNIT_HOLD_POSITION
@ HOTKEY_EDITOR_TOOL_NEXT
@ TITLE_SCREEN__EDITOR
@ HOTKEY_EDITOR_SELECTION_EXPORT
@ HOTKEY_EDITOR_PARTIAL_UPDATE_TRANSITIONS
@ HOTKEY_EDITOR_MAP_CREATE_MASK_TO
@ HOTKEY_REPLAY_NEXT_TURN
@ HOTKEY_TOGGLE_GRID
@ HOTKEY_EDITOR_SELECTION_CUT
@ HOTKEY_EDITOR_UNIT_CHANGE_ID
@ HOTKEY_SURRENDER
@ HOTKEY_SELECT_AND_ACTION
@ HOTKEY_CLEAR_MSG
@ HOTKEY_REPLAY_SHOW_EVERYTHING
@ HOTKEY_REPLAY_SHOW_TEAM1
@ HOTKEY_MINIMAP_DRAW_TERRAIN
@ HOTKEY_LABEL_TERRAIN
@ HOTKEY_EDITOR_DRAW_NUM_OF_BITMAPS
@ HOTKEY_CUSTOM_CMD
@ HOTKEY_STOP_NETWORK
@ HOTKEY_MAP_SCREENSHOT
@ HOTKEY_CLEAR_LABELS
@ HOTKEY_EDITOR_AREA_ADD
@ HOTKEY_EDITOR_BRUSH_SW_NE
@ HOTKEY_EDITOR_TOOL_UNIT
@ HOTKEY_EDITOR_BRUSH_DEFAULT
@ HOTKEY_CONTINUE_MOVE
@ HOTKEY_BEST_ENEMY_MOVES
@ HOTKEY_QUIT_TO_DESKTOP
@ HOTKEY_EDITOR_HELP_TEXT_SHOWN
@ HOTKEY_EDITOR_CHANGE_ADDON_ID
@ HOTKEY_LOAD_AUTOSAVES
@ HOTKEY_EDITOR_CUSTOM_TODS
@ HOTKEY_EDITOR_REFRESH_IMAGE_CACHE
@ HOTKEY_EDITOR_SELECTION_FILL
@ HOTKEY_TOGGLE_ELLIPSES
@ HOTKEY_EDITOR_CLIPBOARD_FLIP_VERTICAL
@ HOTKEY_EDITOR_MAP_SAVE_ALL
@ HOTKEY_EDITOR_BRUSH_1
@ HOTKEY_EDITOR_SELECTION_ROTATE
@ HOTKEY_EDITOR_MAP_LOAD
@ HOTKEY_RENAME_UNIT
@ HOTKEY_EDITOR_BRUSH_2
@ HOTKEY_EDITOR_TOOL_STARTING_POSITION
@ HOTKEY_MINIMAP_CODING_TERRAIN
@ HOTKEY_EDITOR_NO_UPDATE_TRANSITIONS
@ HOTKEY_LOAD_GAME
@ TITLE_SCREEN__TEST
@ HOTKEY_WB_EXECUTE_ACTION
@ HOTKEY_EDITOR_SELECTION_COPY
@ HOTKEY_MINIMAP_DRAW_UNITS
@ HOTKEY_CHANGE_SIDE
@ HOTKEY_EDITOR_PALETTE_DOWNSCROLL
@ HOTKEY_EDITOR_UNIT_TOGGLE_LOYAL
@ TITLE_SCREEN__PREVIOUS_TIP
@ HOTKEY_CYCLE_UNITS
@ HOTKEY_DELAY_SHROUD
@ HOTKEY_WB_BUMP_UP_ACTION
@ HOTKEY_EDITOR_MAP_APPLY_MASK
@ HOTKEY_EDITOR_LOCAL_TIME
@ HOTKEY_CREATE_UNIT
@ HOTKEY_REPLAY_EXIT
@ HOTKEY_EDITOR_UNIT_FACING
@ HOTKEY_PREFERENCES
@ HOTKEY_STATUS_TABLE
@ HOTKEY_REPLAY_NEXT_SIDE
@ HOTKEY_DELETE_UNIT
@ HOTKEY_MOVE_ACTION
@ HOTKEY_REPLAY_NEXT_MOVE
@ HOTKEY_REPLAY_RESET
@ HOTKEY_EDITOR_EDIT_UNIT
@ HOTKEY_TOUCH_HEX
@ HOTKEY_EDITOR_SIDE_NEW
@ HOTKEY_EDITOR_SELECTION_RANDOMIZE
@ HOTKEY_START_NETWORK
@ HOTKEY_WB_TOGGLE
@ HOTKEY_EDITOR_SCENARIO_NEW
@ TITLE_SCREEN__CORES
@ HOTKEY_EDITOR_MAP_SWITCH
@ HOTKEY_STATISTICS
@ HOTKEY_EDITOR_SELECT_INVERSE
@ HOTKEY_EDITOR_TOGGLE_TRANSITIONS
@ HOTKEY_UNIT_LIST
@ HOTKEY_SCROLL_DOWN
@ HOTKEY_EDITOR_AUTO_UPDATE_TRANSITIONS
@ HOTKEY_REPLAY_SKIP_ANIMATION
@ HOTKEY_ZOOM_DEFAULT
@ HOTKEY_EDITOR_MAP_INFO
@ HOTKEY_WB_BUMP_DOWN_ACTION
@ TITLE_SCREEN__CAMPAIGN
@ HOTKEY_EDITOR_AREA_RENAME
@ HOTKEY_EDITOR_MAP_RESIZE
@ HOTKEY_SCROLL_UP
@ HOTKEY_EDITOR_DRAW_TERRAIN_CODES
@ HOTKEY_MP_START_GAME
@ HOTKEY_EDITOR_MAP_TO_SCENARIO
@ HOTKEY_EDITOR_AREA_REMOVE
@ HOTKEY_SELECT_HEX
@ HOTKEY_QUIT_GAME
@ HOTKEY_EDITOR_UPDATE_TRANSITIONS
@ HOTKEY_EDITOR_TOOL_SELECT
@ HOTKEY_EDITOR_MAP_NEW
@ HOTKEY_EDITOR_MAP_SAVE
@ HOTKEY_EDITOR_AREA_SAVE
@ HOTKEY_EDITOR_REMOVE_LOCATION
@ HOTKEY_EDITOR_SIDE_EDIT
@ HOTKEY_AI_FORMULA
@ HOTKEY_EDITOR_SELECT_ADDON
@ HOTKEY_EDITOR_TOOL_ITEM
@ TITLE_SCREEN__RELOAD_WML
@ HOTKEY_REPLAY_SHOW_EACH
@ TITLE_SCREEN__NEXT_TIP
@ HOTKEY_CYCLE_BACK_UNITS
@ HOTKEY_EDITOR_UNIT_TOGGLE_RENAMEABLE
@ HOTKEY_EDITOR_SCENARIO_EDIT
@ HOTKEY_EDITOR_OPEN_ADDON
@ HOTKEY_EDITOR_MAP_REVERT
@ HOTKEY_WB_DELETE_ACTION
@ HOTKEY_EDITOR_CLIPBOARD_FLIP_HORIZONTAL
@ HOTKEY_MINIMAP_CODING_UNIT
Stores all information related to functions that can be bound to hotkeys.
bool toggle
Toggle hotkeys have some restrictions on what can be bound to them.
HOTKEY_COMMAND command
The command associated with this hotkey.
std::string id
The unique ID.
hk_scopes scope
The visibility scope of the command.
bool hidden
If hidden then don't show the command in the hotkey preferences.
static const hotkey_command & null_command()
returns the command that is treated as null
HOTKEY_CATEGORY category
The category of the command.
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...
hotkey_command(const hotkey_command &)=default
hotkey_command & operator=(const hotkey_command &)=default
static map_location::direction s