The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
hotkey_item.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 
16 #ifndef HOTKEY_ITEM_HPP_INCLUDED
17 #define HOTKEY_ITEM_HPP_INCLUDED
18 
19 #include <SDL_events.h>
20 #include <SDL.h>
21 #include <boost/shared_ptr.hpp>
22 #include <vector>
23 
24 
25 class config;
26 class CVideo;
27 namespace hotkey {
28 
29 /* forward declarations */
30 class hotkey_base;
31 class hotkey_mouse;
36 
37 typedef std::vector<hotkey::hotkey_ptr> hotkey_list;
39 
40 /**
41  * This is the base class for hotkey event matching.
42  */
44 {
45 public:
46  /**
47  * Initialises a new empty hotkey that will be disabled
48  */
49  hotkey_base() : command_("null"), is_default_(true), is_disabled_(false), mod_(0)
50  {}
51 
52  void set_command(const std::string& command)
53  {
54  command_ = command;
55  }
56 
57  /**
58  * Set keyboard modifiers.
59  * @param mods Bitmask of SDLMod.
60  */
61  void set_mods(unsigned int mods)
62  {
63  mod_ = mods;
64  }
65 
66  /**
67  * Returns the string name of the HOTKEY_COMMAND
68  * @return The unique string for a hotkey command.
69  **/
70  const std::string& get_command() const
71  {
72  return command_;
73  }
74 
75  /**
76  * Returns the translated description
77  * @todo unused
78  * @return internationalised description of the command.
79  **/
80  const std::string get_description() const;
81 
82  /**
83  * This controls whether the item should appear in the hotkey preferences.
84  * @return true if the item should be hidden
85  **/
86  virtual bool hidden() const
87  {
88  return false;
89  }
90 
91  /**
92  * This indicates whether a hotkey is from the default config or if it's
93  * from the user preferences.
94  * @return true if from the default configurations, false otherwise
95  */
96  bool is_default() const
97  {
98  return is_default_;
99  }
100 
101  /**
102  * Used to indicate that a hotkey is overriden and should be treated as
103  * a user-set hotkey.
104  */
106  {
107  is_default_ = false;
108  }
109 
110  bool is_disabled() const
111  {
112  return is_disabled_;
113  }
114  void disable()
115  {
116  is_disabled_ = true;
117  }
118  void enable()
119  {
120  is_disabled_ = false;
121  }
122 
123  /**
124  * Unbind this hotkey by linking it to the null-command
125  */
126  void clear()
127  {
128  command_ = "null";
129  }
130 
131  /**
132  * Returns whether this hotkey points to the null-command
133  * @return true if it points to the null-command, false otherwise.
134  */
135  bool null() const
136  {
137  return command_ == "null";
138  }
139 
140  /*
141  * Returns whether there is a associated hotkey_command.
142  * If the none of the hotkey_commands fits this hotkey_item then
143  * @param get_hotkey_command will return the hotkey_command::null_command().
144  * @return true if the hotkey is not bound to the null-command.
145  */
146  bool active() const
147  {
148  return command_ != "null";
149  }
150 
151  /**
152  * Evaluates whether the hotkey bindings are valid.
153  * @return true if they are valid, false otherwise.
154  */
155  virtual bool valid() const = 0;
156 
157  /**
158  * Save the hotkey into the configuration object.
159  * @param cfg The configuration object to save into.
160  */
161  void save(config& cfg) const;
162 
163  /**
164  * Return "name" of hotkey. Example :"ctrl+alt+g"
165  * @return The string representation of the keybindings
166  */
167  const std::string get_name() const;
168 
169  /**
170  * Used to evaluate whether:
171  * 1. The hotkey is valid in the current scope.
172  * 2. The Keyboard modifiers and SDL_Event mathes this hotkey.
173  *
174  * @param event The SDL_Event that has triggered and is being evaluated.
175  */
176  bool matches(const SDL_Event& event) const;
177 
178  /**
179  * Checks whether the hotkey bindings and scope are equal.
180  * @param other the hokey bindings to compare against.
181  * @return true if %other has same scope and bindings.
182  */
183  virtual bool bindings_equal(hotkey_ptr other);
184 
185  virtual ~hotkey_base()
186  {}
187 
188 protected:
189  /**
190  * This is invoked by hotkey_base::get_name and must be implemented by subclasses.
191  * Keyboard modifiers are handled in this class, other hotkeys in the respective classes
192  */
193  virtual const std::string get_name_helper() const = 0;
194  /**
195  * This is invoked by hotkey_base::matches as a helper for the concrete classes.
196  * Implementing classes should only check their parts of the hotkey.
197  * @param event The SDL_Event being generated.
198  * @returns true if they match, false otherwise.
199  */
200  virtual bool matches_helper(const SDL_Event &event) const = 0;
201  virtual void save_helper(config& cfg) const = 0;
202  /**
203  * This is invoked by hotkey_base::bindings_equal as a helper for the concrete classes.
204  * Implementing classes should only check their parts of the hotkey.
205  * @param other The other hotkey the check against. Not guaranteed to be the same subclass.
206  * @returns true if they match, false otherwise.
207  */
208  virtual bool bindings_equal_helper(hotkey_ptr other) const = 0;
209 
210  /**
211  * The command that should be executed, or "null".
212  */
214 
215  /**
216  * is_default_ is true if the hot-key is part of the default hot-key list defined in data/core/hotkeys.cfg.
217  * is_default_ is false if it is not, in which case it would be defined in the user's preferences file.
218  */
220 
221  /*
222  * The original design of using a "null" command to indicate a disabled hot-key is ambiguous with regards
223  * to when to save a user hot-key to preferences as well as when a default hot-key should be flagged as
224  * disabled. So introduce a separate disabled flag to resolve the ambiguity.
225  * Where the flag is true, the hot-key should not be written to preferences unless it is a default hot-key.
226  */
228 
229  /*
230  * Keyboard modifiers. Treat as opaque, only do comparisons.
231  */
232  unsigned int mod_;
233 };
234 
235 /**
236  * This class is responsible for handling keys, not modifiers.
237  */
239 {
240 public:
241  /**
242  * Initialise new instance of this class that has no key associated with is.
243  */
244  hotkey_keyboard() : hotkey_base(), scancode_(SDL_SCANCODE_UNKNOWN)
245  {}
246 
247  /**
248  * Set the scancode associated with this class.
249  * @param scancode The SDL_Scancode that this hotkey should be associated with
250  */
251  void set_scancode(SDL_Scancode scancode)
252  {
253  scancode_ = scancode;
254  }
255 
256  /**
257  * Checks whether this hotkey has been set to a sensible value.
258  * @ return true if it is a known key
259  */
260  virtual bool valid() const
261  {
262  return scancode_ != SDL_SCANCODE_UNKNOWN;
263  }
264 
265 protected:
266  SDL_Scancode scancode_;
267 
268  virtual void save_helper(config& cfg) const;
269  virtual const std::string get_name_helper() const;
270  virtual bool matches_helper(const SDL_Event &event) const;
271  virtual bool bindings_equal_helper (hotkey_ptr other) const;
272 };
273 
274 /**
275  * This class is used to return non-valid results in order to save
276  * other people from null checks.
277  */
279 {
280 public:
282  {}
283  virtual bool valid() const
284  {
285  return false;
286  }
287 protected:
288  virtual void save_helper(config&) const
289  {}
290  virtual const std::string get_name_helper() const
291  {
292  return "";
293  }
294  virtual bool matches_helper(const SDL_Event&) const
295  {
296  return false;
297  }
298  virtual bool bindings_equal_helper(hotkey_ptr) const
299  {
300  return false;
301  }
302 };
303 
304 /**
305  * This class is responsible for handling mouse button presses.
306  */
308 {
309 public:
310  /**
311  * Initialise new instance of this class that has no button associated with is.
312  */
314  {}
315 
316  /**
317  * Returns true if the hotkey has a valid mouse button associated with it.
318  * @return true if a mouse button is set, false otherwise.
319  */
320  virtual bool valid() const
321  {
322  return button_ != 0;
323  }
324 
325  /* new functionality for this class */
326  void set_button(int button)
327  {
328  button_ = button;
329  }
330 protected:
331  int button_;
332 
333  virtual void save_helper(config& cfg) const;
334  virtual const std::string get_name_helper() const;
335  virtual bool matches_helper(const SDL_Event &event) const;
336  virtual bool bindings_equal_helper (hotkey_ptr other) const;
337 };
338 
339 /**
340  * @todo not implemented
341  */
343 {
344 protected:
345  int button_;
346 };
347 
348 /**
349  * Create and instantiate a hotkey from a config element.
350  * @param cfg The config element to read for data.
351  * @return The new instance of the hotkey item.
352  */
353 hotkey_ptr load_from_config(const config& cfg);
354 
355 /*
356  * Scans the list of hotkeys to see if one has been bound to the command.
357  * @param command The command that is searched for
358  * @return true if there is a hotkey item that has the command bound.
359  */
360 bool has_hotkey_item(const std::string& command);
361 
362 /**
363  * Add a hotkey to the list of hotkeys.
364  * @param item The item to add.
365  */
366 void add_hotkey(const hotkey_ptr item);
367 
368 /**
369  * Remove a hotkey from the list of hotkeys
370  * @todo unusued?
371  */
372 void del_hotkey(const hotkey_ptr item);
373 
374 /**
375  * Create a new hotkey item for a command from an SDL_Event.
376  * @param id The command to bind to.
377  * @param event The SDL_Event to base the creation on.
378  */
379 hotkey_ptr create_hotkey(const std::string &id, SDL_Event &event);
380 
381 /**
382  * Iterate through the list of hotkeys and return a hotkey that matches
383  * the SDL_Event and the current keyboard modifier state.
384  * @param event The SDL_Event to use as a template.
385  * @return The newly created hotkey item.
386  */
387 const hotkey_ptr get_hotkey(const SDL_Event &event);
388 
389 /**
390  * Iterates through all hotkeys present in the config struct and creates and adds
391  * them to the hotkey list.
392  * @param cfg The config struct to load from.
393  * @param set_as_default Indicates whether the config struct should be treated as the
394  * default game settings.
395  */
396 void load_hotkeys(const config& cfg, bool set_as_default = false);
397 
398 /**
399  * Reset all hotkeys to the defaults.
400  */
401 void reset_default_hotkeys();
402 
403 /**
404  * Returns the list of hotkeys.
405  */
406 const hotkey_list& get_hotkeys();
407 
408 /**
409  * Unset the command bindings for all hotkeys matching the command.
410  *
411  * @param command The binding to be unset
412  */
413 void clear_hotkeys(const std::string& command);
414 
415 /**
416  * Unset the bindings for all hotkeys.
417  */
418 void clear_hotkeys();
419 
420 /**
421  * Returns a comma-separated string of hotkey names. A hotkey name is in the form of
422  * "ctrl+l" or "n" or "mouse 1". The comman separated string is of the form "ctrl+l,n,mouse 1".
423  * @return The comma separated string of hotkey names.
424  */
426 
427 /**
428  * Save the non-default hotkeys to the config.
429  * @param cfg The config to save to.
430  */
431 void save_hotkeys(config& cfg);
432 
433 hotkey_ptr show_binding_dialog(CVideo& video, const std::string& id);
434 
435 }
436 
437 #endif
void save(config &cfg) const
Save the hotkey into the configuration object.
hotkey_mouse()
Initialise new instance of this class that has no button associated with is.
bool is_default_
is_default_ is true if the hot-key is part of the default hot-key list defined in data/core/hotkeys...
std::vector< hotkey::hotkey_ptr >::iterator hotkey_list_iter
Definition: hotkey_item.hpp:38
hotkey_ptr load_from_config(const config &cfg)
Create and instantiate a hotkey from a config element.
boost::shared_ptr< hotkey_keyboard > hotkey_keyboard_ptr
Definition: hotkey_item.hpp:35
void save_hotkeys(config &cfg)
Save the non-default hotkeys to the config.
Definition: video.hpp:58
void set_scancode(SDL_Scancode scancode)
Set the scancode associated with this class.
bool matches(const SDL_Event &event) const
Used to evaluate whether:
virtual bool bindings_equal_helper(hotkey_ptr other) const =0
This is invoked by hotkey_base::bindings_equal as a helper for the concrete classes.
virtual const std::string get_name_helper() const
This is invoked by hotkey_base::get_name and must be implemented by subclasses.
void del_hotkey(hotkey_ptr item)
Remove a hotkey from the list of hotkeys.
void clear_hotkeys(const std::string &command)
Unset the command bindings for all hotkeys matching the command.
hotkey_ptr show_binding_dialog(CVideo &video, const std::string &id)
bool has_hotkey_item(const std::string &command)
virtual bool valid() const
Evaluates whether the hotkey bindings are valid.
virtual bool matches_helper(const SDL_Event &event) const
This is invoked by hotkey_base::matches as a helper for the concrete classes.
virtual void save_helper(config &cfg) const
virtual bool matches_helper(const SDL_Event &event) const =0
This is invoked by hotkey_base::matches as a helper for the concrete classes.
Keyboard shortcuts for game actions.
virtual void save_helper(config &) const
const hotkey_list & get_hotkeys()
Returns the list of hotkeys.
void unset_default()
Used to indicate that a hotkey is overriden and should be treated as a user-set hotkey.
hotkey_keyboard()
Initialise new instance of this class that has no key associated with is.
virtual bool valid() const
Returns true if the hotkey has a valid mouse button associated with it.
virtual void save_helper(config &cfg) const =0
const hotkey_ptr get_hotkey(const SDL_Event &event)
Iterate through the list of hotkeys and return a hotkey that matches the SDL_Event and the current ke...
void set_button(int button)
hotkey_ptr create_hotkey(const std::string &id, SDL_Event &event)
Create a new hotkey item for a command from an SDL_Event.
void add_hotkey(const hotkey_ptr item)
Add a hotkey to the list of hotkeys.
This class is used to return non-valid results in order to save other people from null checks...
virtual bool matches_helper(const SDL_Event &) const
This is invoked by hotkey_base::matches as a helper for the concrete classes.
std::vector< hotkey::hotkey_ptr > hotkey_list
Definition: hotkey_item.hpp:37
virtual bool hidden() const
This controls whether the item should appear in the hotkey preferences.
Definition: hotkey_item.hpp:86
bool active() const
virtual const std::string get_name_helper() const =0
This is invoked by hotkey_base::get_name and must be implemented by subclasses.
virtual bool matches_helper(const SDL_Event &event) const
This is invoked by hotkey_base::matches as a helper for the concrete classes.
boost::shared_ptr< hotkey_mouse > hotkey_mouse_ptr
Definition: hotkey_item.hpp:34
virtual const std::string get_name_helper() const
This is invoked by hotkey_base::get_name and must be implemented by subclasses.
std::string get_names(std::string id)
Returns a comma-separated string of hotkey names.
bool is_default() const
This indicates whether a hotkey is from the default config or if it's from the user preferences...
Definition: hotkey_item.hpp:96
boost::shared_ptr< hotkey_base > hotkey_ptr
Definition: hotkey_item.hpp:32
void reset_default_hotkeys()
Reset all hotkeys to the defaults.
virtual const std::string get_name_helper() const
This is invoked by hotkey_base::get_name and must be implemented by subclasses.
virtual bool valid() const =0
Evaluates whether the hotkey bindings are valid.
This class is responsible for handling mouse button presses.
void set_command(const std::string &command)
Definition: hotkey_item.hpp:52
void set_mods(unsigned int mods)
Set keyboard modifiers.
Definition: hotkey_item.hpp:61
const std::string & get_command() const
Returns the string name of the HOTKEY_COMMAND.
Definition: hotkey_item.hpp:70
This is the base class for hotkey event matching.
Definition: hotkey_item.hpp:43
virtual bool bindings_equal_helper(hotkey_ptr other) const
This is invoked by hotkey_base::bindings_equal as a helper for the concrete classes.
std::string command_
The command that should be executed, or "null".
cl_event event
Definition: glew.h:3070
hotkey_base()
Initialises a new empty hotkey that will be disabled.
Definition: hotkey_item.hpp:49
void load_hotkeys(const config &cfg, bool set_as_default)
Iterates through all hotkeys present in the config struct and creates and adds them to the hotkey lis...
This class is responsible for handling keys, not modifiers.
virtual bool bindings_equal_helper(hotkey_ptr other) const
This is invoked by hotkey_base::bindings_equal as a helper for the concrete classes.
virtual bool bindings_equal(hotkey_ptr other)
Checks whether the hotkey bindings and scope are equal.
const std::string get_description() const
Returns the translated description.
void clear()
Unbind this hotkey by linking it to the null-command.
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual void save_helper(config &cfg) const
virtual bool bindings_equal_helper(hotkey_ptr) const
This is invoked by hotkey_base::bindings_equal as a helper for the concrete classes.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool is_disabled() const
bool null() const
Returns whether this hotkey points to the null-command.
virtual bool valid() const
Checks whether this hotkey has been set to a sensible value.
const std::string get_name() const
Return "name" of hotkey.
Definition: hotkey_item.cpp:73