The Battle for Wesnoth  1.17.0-dev
hotkey_command.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 <list>
23 #include <map>
24 #include <vector>
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 
60 
61  // Replay
67 
68  // Controls
71 
72  // Camera movement
74 
75  // Dialog control
77 
78  // Whiteboard commands
84 
85  // Misc.
91 
92  // Minimap
95 
96  /* Gui2 specific hotkeys. */
110 
112 
113  /* Editor commands */
116 
117  // Palette
120 
126 
127  // Unit
130 
131  // Brushes
134 
135  // Tools
140 
141  // Select
143  // Clipboard
147  // Selection
153 
154  // Map
163 
164  // Transitions
167 
168  // Refresh
171 
172  // Draw
174 
175  // Side
179 
180  // Area
185 
186  // Scenario
190 
191  /* This item must stay at the end since it is used as terminator for iterating. */
193 };
194 
209  HKCAT_PLACEHOLDER // Keep this one last
210 };
211 
212 using category_name_map_t = std::map<HOTKEY_CATEGORY, std::string>;
213 
214 /**
215  * Returns the map of hotkey categories and their display names.
216  *
217  * These aren't translated and need be converted to a t_string before
218  * being displayed to the player.
219  */
221 
222 /** Returns a list of all the hotkeys belonging to the given category. */
223 std::list<HOTKEY_COMMAND> get_hotkeys_by_category(HOTKEY_CATEGORY category);
224 
225 typedef std::bitset<SCOPE_COUNT> hk_scopes;
226 
227 /**
228  * Do not use this outside hotkeys.cpp.
229  * hotkey_command uses t_string which might cause bugs when used at program startup,
230  * so use this for the master hotkey list (and only there).
231  */
233 {
235 
236  std::string command;
237 
238  /** description, tooltip are untranslated */
239  std::string description;
240 
241  bool hidden;
242 
243  hk_scopes scope;
245 
246  std::string tooltip;
247 };
248 
249 /**
250  * Stores all information related to functions that can be bound to hotkeys.
251  * this is currently a semi struct: it haves a constructor, but only const-public members.
252  */
254 {
255  hotkey_command() = delete;
256 
257  /** Constuct a new command from a temporary static hotkey object. */
258  hotkey_command(const hotkey_command_temp& temp_command);
259 
260  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);
261 
262  hotkey_command(const hotkey_command&) = default;
263  hotkey_command& operator=(const hotkey_command&) = default;
264 
265  /**
266  * the names are strange: the "hotkey::HOTKEY_COMMAND" is named id, and the string to identify the object is called "command"
267  * there is some inconstancy with that names in this file.
268  * This binds the command to a function. Does not need to be unique.
269  */
271 
272  /** The command is unique. */
273  std::string command;
274 
275  // since the wml_menu hotkey_command s can have different textdomains we need t_string now.
277 
278  /** If hidden then don't show the command in the hotkey preferences. */
279  bool hidden;
280 
281  /**
282  * Toggle hotkeys have some restrictions on what can be bound to them.
283  * They require a binding that has two states, "pressed" and "released"
284  */
285  bool toggle;
286 
287  /** The visibility scope of the command. */
288  hk_scopes scope;
289 
290  /** The category of the command. */
292 
294 
295  /** checks weather this is the null hotkey_command */
296  bool null() const;
297 
298  /** returns the command that is treated as null */
299  static const hotkey_command& null_command();
300 
301  /**
302  * the execute_command argument was changed from HOTKEY_COMMAND to hotkey_command,
303  * to be able to call it with HOTKEY_COMMAND, this function was created
304  */
305  static const hotkey_command& get_command_by_command(HOTKEY_COMMAND command);
306 };
307 
309 public:
310  scope_changer();
311  ~scope_changer();
312 private:
314 };
315 
316 /**
317  * returns a container that contains all currently active hotkey_commands.
318  * everything that wants a hotkey, must be in this container
319  */
320 const std::vector<hotkey_command>& get_hotkey_commands();
321 
322 /** returns the hotkey_command with the given name */
323 const hotkey_command& get_hotkey_command(const std::string& command);
324 
325 /** returns the hotkey_command that is treated as null. */
327 
328 void deactivate_all_scopes();
329 void set_scope_active(scope s, bool set = true);
330 void set_active_scopes(hk_scopes s);
331 bool is_scope_active(scope s);
332 bool is_scope_active(hk_scopes s);
333 
334 bool has_hotkey_command(const std::string& id);
335 
336 /**
337  * adds a new wml hotkey to the list, but only if there is no hotkey with that id yet on the list.
338  * the object that is created here will be deleted in "delete_all_wml_hotkeys()"
339  */
340 void add_wml_hotkey(const std::string& id, const t_string& description, const config& default_hotkey);
341 
342 /** deletes all wml hotkeys, should be called after a game has ended */
344 /** removes a wml hotkey with the given id, returns true if the deletion was successful */
345 bool remove_wml_hotkey(const std::string& id);
346 
347 const std::string& get_description(const std::string& command);
348 const std::string& get_tooltip(const std::string& command);
349 
350 void init_hotkey_commands();
351 
352 void clear_hotkey_commands();
353 
354 /** returns get_hotkey_command(command).id */
355 HOTKEY_COMMAND get_id(const std::string& command);
356 }
HOTKEY_COMMAND id
the names are strange: the "hotkey::HOTKEY_COMMAND" is named id, and the string to identify the objec...
bool is_scope_active(scope s)
scope
Available hotkey scopes.
void set_scope_active(scope s, bool set)
void add_wml_hotkey(const std::string &id, const t_string &description, const config &default_hotkey)
adds a new wml hotkey to the list, but only if there is no hotkey with that id yet on the list...
Stores all information related to functions that can be bound to hotkeys.
std::list< HOTKEY_COMMAND > get_hotkeys_by_category(HOTKEY_CATEGORY category)
Returns a list of all the hotkeys belonging to the given category.
void clear_hotkey_commands()
void set_active_scopes(hk_scopes s)
Keyboard shortcuts for game actions.
const std::string & get_tooltip(const std::string &command)
std::bitset< SCOPE_COUNT > hk_scopes
void init_hotkey_commands()
hk_scopes scope
The visibility scope of the command.
void deactivate_all_scopes()
Do not use this outside hotkeys.cpp.
std::map< HOTKEY_CATEGORY, std::string > category_name_map_t
const category_name_map_t & get_category_names()
Returns the map of hotkey categories and their display names.
const std::vector< hotkey_command > & get_hotkey_commands()
returns a container that contains all currently active hotkey_commands.
std::string command
The command is unique.
bool toggle
Toggle hotkeys have some restrictions on what can be bound to them.
static map_location::DIRECTION s
const std::string & get_description(const std::string &command)
const hotkey_command & get_hotkey_null()
returns the hotkey_command that is treated as null.
bool has_hotkey_command(const std::string &id)
std::string description
description, tooltip are untranslated
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:61
const hotkey_command & get_hotkey_command(const std::string &command)
returns the hotkey_command with the given name
void delete_all_wml_hotkeys()
deletes all wml hotkeys, should be called after a game has ended
bool remove_wml_hotkey(const std::string &id)
removes a wml hotkey with the given id, returns true if the deletion was successful ...
HOTKEY_CATEGORY category
The category of the command.
HOTKEY_COMMAND get_id(const std::string &command)
returns get_hotkey_command(command).id