The Battle for Wesnoth  1.15.12+dev
hotkey_command.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "tooltips.hpp"
18 #include "tstring.hpp"
19 
20 #include <bitset>
21 #include <list>
22 #include <map>
23 #include <vector>
24 
25 class config;
26 
27 namespace hotkey {
28 
29 /**
30  * Available hotkey scopes. The scope is used to allow command from
31  * non-overlapping areas of the game share the same key
32  */
33 enum scope {
38 };
39 
59 
60  // Replay
66 
67  // Controls
70 
71  // Camera movement
73 
74  // Dialog control
76 
77  // Whiteboard commands
83 
84  // Misc.
90 
91  // Minimap
94 
95  /* Gui2 specific hotkeys. */
109 
111 
112  /* Editor commands */
115 
116  // Palette
119 
125 
126  // Unit
129 
130  // Brushes
133 
134  // Tools
139 
140  // Select
142  // Clipboard
146  // Selection
152 
153  // Map
162 
163  // Transitions
166 
167  // Refresh
170 
171  // Draw
173 
174  // Side
178 
179  // Area
184 
185  // Scenario
189 
190  /* This item must stay at the end since it is used as terminator for iterating. */
192 };
193 
208  HKCAT_PLACEHOLDER // Keep this one last
209 };
210 
211 using category_name_map_t = std::map<HOTKEY_CATEGORY, std::string>;
212 
213 /**
214  * Returns the map of hotkey categories and their display names.
215  *
216  * These aren't translated and need be converted to a t_string before
217  * being displayed to the player.
218  */
220 
221 /** Returns a list of all the hotkeys belonging to the given category. */
222 std::list<HOTKEY_COMMAND> get_hotkeys_by_category(HOTKEY_CATEGORY category);
223 
224 typedef std::bitset<SCOPE_COUNT> hk_scopes;
225 
226 /**
227  * Do not use this outside hotkeys.cpp.
228  * hotkey_command uses t_string which might cause bugs when used at program startup,
229  * so use this for the master hotkey list (and only there).
230  */
232 {
234 
235  std::string command;
236 
237  /** description, tooltip are untranslated */
238  std::string description;
239 
240  bool hidden;
241 
242  hk_scopes scope;
244 
245  std::string tooltip;
246 };
247 
248 /**
249  * Stores all information related to functions that can be bound to hotkeys.
250  * this is currently a semi struct: it haves a constructor, but only const-public members.
251  */
253 {
254  hotkey_command() = delete;
255 
256  /** Constuct a new command from a temporary static hotkey object. */
257  hotkey_command(const hotkey_command_temp& temp_command);
258 
259  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);
260 
261  hotkey_command(const hotkey_command&) = default;
262  hotkey_command& operator=(const hotkey_command&) = default;
263 
264  /**
265  * the names are strange: the "hotkey::HOTKEY_COMMAND" is named id, and the string to identify the object is called "command"
266  * there is some inconstancy with that names in this file.
267  * This binds the command to a function. Does not need to be unique.
268  */
270 
271  /** The command is unique. */
272  std::string command;
273 
274  // since the wml_menu hotkey_command s can have different textdomains we need t_string now.
276 
277  /** If hidden then don't show the command in the hotkey preferences. */
278  bool hidden;
279 
280  /**
281  * Toggle hotkeys have some restrictions on what can be bound to them.
282  * They require a binding that has two states, "pressed" and "released"
283  */
284  bool toggle;
285 
286  /** The visibility scope of the command. */
287  hk_scopes scope;
288 
289  /** The category of the command. */
291 
293 
294  /** checks weather this is the null hotkey_command */
295  bool null() const;
296 
297  /** returns the command that is treated as null */
298  static const hotkey_command& null_command();
299 
300  /**
301  * the execute_command argument was changed from HOTKEY_COMMAND to hotkey_command,
302  * to be able to call it with HOTKEY_COMMAND, this function was created
303  */
304  static const hotkey_command& get_command_by_command(HOTKEY_COMMAND command);
305 };
306 
308 public:
309  scope_changer();
310  ~scope_changer();
311 private:
313 };
314 
315 /**
316  * returns a container that contains all currently active hotkey_commands.
317  * everything that wants a hotkey, must be in this container
318  */
319 const std::vector<hotkey_command>& get_hotkey_commands();
320 
321 /** returns the hotkey_command with the given name */
322 const hotkey_command& get_hotkey_command(const std::string& command);
323 
324 /** returns the hotkey_command that is treated as null. */
326 
327 void deactivate_all_scopes();
328 void set_scope_active(scope s, bool set = true);
329 void set_active_scopes(hk_scopes s);
330 bool is_scope_active(scope s);
331 bool is_scope_active(hk_scopes s);
332 
333 bool has_hotkey_command(const std::string& id);
334 
335 /**
336  * adds a new wml hotkey to the list, but only if there is no hotkey with that id yet on the list.
337  * the object that is created here will be deleted in "delete_all_wml_hotkeys()"
338  */
339 void add_wml_hotkey(const std::string& id, const t_string& description, const config& default_hotkey);
340 
341 /** deletes all wml hotkeys, should be called after a game has ended */
343 /** removes a wml hotkey with the given id, returns true if the deletion was successful */
344 bool remove_wml_hotkey(const std::string& id);
345 
346 const std::string& get_description(const std::string& command);
347 const std::string& get_tooltip(const std::string& command);
348 
349 void init_hotkey_commands();
350 
351 void clear_hotkey_commands();
352 
353 /** returns get_hotkey_command(command).id */
354 HOTKEY_COMMAND get_id(const std::string& command);
355 }
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:59
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