The Battle for Wesnoth  1.17.12+dev
hotkey_command.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 "tooltips.hpp"
19 #include "tstring.hpp"
20 
21 #include <bitset>
22 #include <functional>
23 #include <list>
24 #include <map>
25 #include <vector>
26 
27 class config;
28 
29 namespace hotkey {
30 
31 /**
32  * Available hotkey scopes. The scope is used to allow command from
33  * non-overlapping areas of the game share the same key
34  */
35 enum scope {
40 };
41 
42 // For some reason std::bitset::operator| is not constexpr, so we'll construct the bitset with these values
43 // FIXME: unify these with the enum above. Right now these are the proper bitmasks to initialize a bitset,
44 // while the values above are used as indices to access the bits of the bitset.
45 constexpr uint32_t scope_game = 1 << SCOPE_GAME;
46 constexpr uint32_t scope_editor = 1 << SCOPE_EDITOR;
47 constexpr uint32_t scope_main = 1 << SCOPE_MAIN_MENU;
48 
68 
69  // Replay
75 
76  // Controls
79 
80  // Camera movement
82 
83  // Dialog control
85 
86  // Whiteboard commands
92 
93  // Misc.
100 
101  // Minimap
104 
105  /* Gui2 specific hotkeys. */
118 
120 
121  /* Editor commands */
124 
125  // Palette
128 
134 
135  // Unit
138 
139  // Brushes
142 
143  // Tools
148 
149  // Select
151  // Clipboard
155  // Selection
161 
162  // Map
171 
172  // Transitions
175 
176  // Refresh
179 
180  // Draw
182 
183  // Side
187 
188  // Area
193 
194  // Scenario
198 
199  /* This item must stay at the end since it is used as terminator for iterating. */
201 };
202 
217  HKCAT_PLACEHOLDER // Keep this one last
218 };
219 
220 /**
221  * Returns the map of hotkey categories and their display names.
222  *
223  * These aren't translated and need be converted to a t_string before
224  * being displayed to the player.
225  */
226 const std::map<HOTKEY_CATEGORY, std::string>& get_category_names();
227 
228 typedef std::bitset<SCOPE_COUNT> hk_scopes;
229 
230 /**
231  * hotkey_command uses t_string which might cause bugs when used at program startup,
232  * so use this for the master hotkey list (and only there).
233  */
234 struct hotkey_command_temp;
235 
236 /**
237  * Stores all information related to functions that can be bound to hotkeys.
238  * this is currently a semi struct: it haves a constructor, but only const-public members.
239  */
241 {
242  hotkey_command() = delete;
243 
244  /** Constructs a new command from a temporary static hotkey object. */
245  hotkey_command(const hotkey_command_temp& temp_command);
246 
247  /** @todo: see if we can remove this with c++20. Aggregate initialization with try_emplace?*/
248  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);
249 
250  hotkey_command(const hotkey_command&) = default;
251  hotkey_command& operator=(const hotkey_command&) = default;
252 
253  /** The command associated with this hotkey. Does not need to be unique. */
255 
256  /** The unique ID. */
257  std::string id;
258 
259  // since the wml_menu hotkey_command s can have different textdomains we need t_string now.
261 
262  /** If hidden then don't show the command in the hotkey preferences. */
263  bool hidden;
264 
265  /**
266  * Toggle hotkeys have some restrictions on what can be bound to them.
267  * They require a binding that has two states, "pressed" and "released"
268  */
269  bool toggle;
270 
271  /** The visibility scope of the command. */
272  hk_scopes scope;
273 
274  /** The category of the command. */
276 
278 
279  /** checks weather this is the null hotkey_command */
280  bool null() const;
281 
282  /** returns the command that is treated as null */
283  static const hotkey_command& null_command();
284 
285  /**
286  * the execute_command argument was changed from HOTKEY_COMMAND to hotkey_command,
287  * to be able to call it with HOTKEY_COMMAND, this function was created
288  */
290 };
291 
293 {
294 public:
295  scope_changer();
296  explicit scope_changer(hk_scopes new_scopes, bool restore = true);
297  ~scope_changer();
298 private:
300  const bool restore_;
301 };
302 
303 /**
304  * returns a container that contains all currently active hotkey_commands.
305  * everything that wants a hotkey, must be in this container
306  */
307 const std::map<std::string_view, hotkey::hotkey_command>& get_hotkey_commands();
308 
309 /** returns the hotkey_command with the given name */
310 const hotkey_command& get_hotkey_command(const std::string& command);
311 
312 bool is_scope_active(scope s);
313 bool is_scope_active(hk_scopes s);
314 
315 bool has_hotkey_command(const std::string& id);
316 
317 /**
318  * RAII helper class to control the lifetime of a WML hotkey_command.
319  */
321 {
322 public:
323  wml_hotkey_record() = default;
324 
325  /** Don't allow copying so objects don't get erased early. */
326  wml_hotkey_record(const wml_hotkey_record&) = delete;
327  const wml_hotkey_record& operator=(const wml_hotkey_record&) = delete;
328 
329  /** Registers a hotkey_command for a WML hotkey with the given ID if one does not already exist. */
330  wml_hotkey_record(const std::string& id, const t_string& description, const config& default_hotkey);
331 
333 
334 private:
335  /** Handles removing the associated hotkey_command on this object's destruction. */
336  std::function<void()> cleanup_{};
337 };
338 
339 void init_hotkey_commands();
340 }
bool is_scope_active(scope s)
scope
Available hotkey scopes.
const std::map< HOTKEY_CATEGORY, std::string > & get_category_names()
Returns the map of hotkey categories and their display names.
static const hotkey_command & null_command()
returns the command that is treated as null
Stores all information related to functions that can be bound to hotkeys.
HOTKEY_COMMAND command
The command associated with this hotkey.
std::string id
The unique ID.
bool null() const
checks weather this is the null hotkey_command
Keyboard shortcuts for game actions.
constexpr uint32_t scope_main
RAII helper class to control the lifetime of a WML hotkey_command.
constexpr uint32_t scope_editor
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
void init_hotkey_commands()
constexpr uint32_t scope_game
hk_scopes scope
The visibility scope of the command.
bool toggle
Toggle hotkeys have some restrictions on what can be bound to them.
static map_location::DIRECTION s
bool has_hotkey_command(const std::string &id)
hotkey_command & operator=(const hotkey_command &)=default
bool hidden
If hidden then don&#39;t show the command in the hotkey preferences.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
const hotkey_command & get_hotkey_command(const std::string &command)
returns the hotkey_command with the given name
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...