The Battle for Wesnoth  1.15.2+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
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 /// 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).
231 {
233 
234  std::string command;
235 
236  /// description, tooltip are untranslated
237  std::string description;
238 
239  bool hidden;
240 
241  hk_scopes scope;
243 
244  std::string tooltip;
245 };
246 
247 /// Stores all information related to functions that can be bound to hotkeys.
248 /// this is currently a semi struct: it haves a constructor, but only const-public members.
250 {
251  hotkey_command() = delete;
252 
253  /** Constuct a new command from a temporary static hotkey object. */
254  hotkey_command(const hotkey_command_temp& temp_command);
255 
256  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);
257 
258  hotkey_command(const hotkey_command&) = default;
259  hotkey_command& operator=(const hotkey_command&) = default;
260 
261  /// the names are strange: the "hotkey::HOTKEY_COMMAND" is named id, and the string to identify the object is called "command"
262  /// there is some inconstancy with that names in this file.
263  /// This binds the command to a function. Does not need to be unique.
265 
266  /// The command is unique.
267  std::string command;
268 
269  // since the wml_menu hotkey_command s can have different textdomains we need t_string now.
271 
272  /// If hidden then don't show the command in the hotkey preferences.
273  bool hidden;
274 
275  /// Toggle hotkeys have some restrictions on what can be bound to them.
276  /// They require a binding that has two states, "pressed" and "released".
277  bool toggle;
278 
279  /// The visibility scope of the command.
280  hk_scopes scope;
281 
282  /// The category of the command.
284 
286 
287  /// checks weather this is the null hotkey_command
288  bool null() const;
289 
290  /// returns the command that is treated as null
291  static const hotkey_command& null_command();
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  static const hotkey_command& get_command_by_command(HOTKEY_COMMAND command);
296 };
297 
299 public:
300  scope_changer();
301  ~scope_changer();
302 private:
304 };
305 
306 /// returns a container that contains all currently active hotkey_commands.
307 /// everything that wants a hotkey, must be in this container.
308 const std::vector<hotkey_command>& get_hotkey_commands();
309 
310 /// returns the hotkey_command with the given name
311 const hotkey_command& get_hotkey_command(const std::string& command);
312 
313 /// returns the hotkey_command that is treated as null.
315 
316 void deactivate_all_scopes();
317 void set_scope_active(scope s, bool set = true);
318 void set_active_scopes(hk_scopes s);
319 bool is_scope_active(scope s);
320 bool is_scope_active(hk_scopes s);
321 
322 ///
323 bool has_hotkey_command(const std::string& id);
324 
325 /// adds a new wml hotkey to the list, but only if there is no hotkey with that id yet on the list.
326 /// the object that is created here will be deleted in "delete_all_wml_hotkeys()"
327 void add_wml_hotkey(const std::string& id, const t_string& description, const config& default_hotkey);
328 
329 /// deletes all wml hotkeys, should be called after a game has ended
331 ///removes a wml hotkey with the given id, returns true if the deletion was successful
332 bool remove_wml_hotkey(const std::string& id);
333 
334 const std::string& get_description(const std::string& command);
335 const std::string& get_tooltip(const std::string& command);
336 
337 void init_hotkey_commands();
338 
339 void clear_hotkey_commands();
340 
341 /// returns get_hotkey_command(command).id
342 HOTKEY_COMMAND get_id(const std::string& command);
343 }
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:68
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