Gorgon Game Engine
KeyRepeater.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <set>
5 
6 #include "../Event.h"
7 #include "../Animation.h"
8 #include "Keyboard.h"
9 
10 namespace Gorgon { namespace Input {
11 
12  namespace internal {
14  virtual ~eventunregisterhelper() { }
15  virtual void unregister() = 0;
16  };
17 
18  template<class T_>
21  }
22 
23  virtual void unregister() override {
24  event.Unregister(token);
25  }
26 
27  T_ &event;
29  };
30  }
31 
54  class KeyRepeater : public Animation::Base {
55  public:
56 
58  KeyRepeater() : Base(true) { }
59 
61  template<class E_>
62  KeyRepeater(E_ &event, const std::initializer_list<Key> &keys, int delay = 100);
63 
65  KeyRepeater(const std::initializer_list<Key> &keys, int delay = 100);
66 
68  KeyRepeater(const KeyRepeater &) = delete;
69 
72  if(token)
73  token->unregister();
74 
75  delete token;
76  }
77 
83  template<class E_>
84  void RegisterTo(E_ &event, bool ignoremodified = false) {
86 
88  event,
89  event.Register(this, &KeyRepeater::KeyEvent)
90  );
91  }
92 
94  void UnregisterFrom() {
95  if(token)
96  token->unregister();
97 
98  delete token;
99  token = nullptr;
100  }
101 
104  void Register(Key key) {
105  registeredkeys.insert(key);
106  }
107 
110  template<class ... T_>
111  void Register(Key key, T_ &&... args) {
112  registeredkeys.insert(key);
113 
114  this->Register(std::forward<T_>(args)...);
115  }
116 
117 
122  void Unregister(Key key) {
123  Release(key);
124  registeredkeys.erase(key);
125  }
126 
128  bool IsKeyRegistered(Key key) const {
129  return registeredkeys.count(key) != 0;
130  }
131 
134  void Press(Key key);
135 
137  void Release(Key key);
138 
140  bool IsPressed(Key key) const {
141  return pressedkeys.count(key) != 0;
142  }
143 
145  void SetRepeatOnPress(bool value) {
146  repeatonpress = value;
147  }
148 
150  bool GetRepeatOnPress() const {
151  return repeatonpress;
152  }
153 
155  void SetRepeatOnRelease(bool value) {
156  repeatonrelease = value;
157  }
158 
160  bool GetRepeatOnRelease() const {
161  return repeatonrelease;
162  }
163 
166  void SetInitialDelay(int value) {
167  initialdelay = value;
168  }
169 
172  bool GetInitialDelay() const {
173  return initialdelay;
174  }
175 
178  void SetDelay(int value) {
179  delay = value;
180  }
181 
183  int GetDelay() const {
184  return delay;
185  }
186 
189  void SetAcceleration(int value) {
190  acceleration = value;
191  }
192 
195  int GetAcceleration() const {
196  return acceleration;
197  }
198 
201  void SetAccelerationStart(int value) {
202  accelerationstart = value;
203  }
204 
207  int GetAccelerationStart() const {
208  return accelerationstart;
209  }
210 
213  void SetAccelerationCount(int value) {
214  accelerationcount = value;
215  }
216 
219  int GetAccelerationCount() const {
220  return accelerationcount;
221  }
222 
224  int GetFinalDelay() const {
225  return delay - accelerationcount * acceleration;
226  }
227 
231  void SetupAcceleration(int startdelay, int finaldelay, int rampup);
232 
233  virtual bool Progress(unsigned &) override;
234 
235  virtual int GetDuration() const override { return 0; }
236 
239 
240  lastprogress = controller.GetProgress();
241  }
242 
244  bool KeyEvent(Key &key, float amount);
245 
248 
249  private:
250 
251  struct repeatinfo {
252  int delay = 0;
253  int count = 0;
254  };
255 
256  internal::eventunregisterhelper *token = nullptr;
257 
258  std::map<Key, repeatinfo> pressedkeys;
259 
260  std::set<Key> registeredkeys;
261 
262 
263  bool repeatonpress = true;
264 
265  bool repeatonrelease = false;
266 
267  int initialdelay = 500;
268 
269  int delay = 100;
270 
271  int acceleration = 0;
272 
273  int accelerationcount = 0;
274 
275  int accelerationstart = 0;
276 
277  unsigned lastprogress = 0;
278  };
279 
280  template<class E_>
281  KeyRepeater::KeyRepeater(E_ &event, const std::initializer_list<Key> &keys, int delay) : Base(true) {
282  RegisterTo(event);
283 
284  for(auto key : keys)
285  Register(key);
286 
287  SetDelay(delay);
288  }
289 
290 
291 } }
292 
Gorgon::EventToken
intptr_t EventToken
Generic type to store event tokens.
Definition: Event.h:121
Gorgon::Input::KeyRepeater::SetAccelerationCount
void SetAccelerationCount(int value)
Set how many times acceleration can be applied.
Definition: KeyRepeater.h:213
Gorgon::Input::KeyRepeater::~KeyRepeater
~KeyRepeater()
Destructor.
Definition: KeyRepeater.h:71
Gorgon::Animation::Base::Base
Base(ControllerBase &controller)
Sets the controller for this animation to the given controller.
Definition: Animation.h:310
Gorgon::Input::KeyRepeater::Release
void Release(Key key)
Releases a key may cause a repeat.
Definition: KeyRepeater.cpp:80
Gorgon::Input::KeyRepeater::Register
void Register(Key key)
Registers the given key to be repeated.
Definition: KeyRepeater.h:104
Gorgon::Input::KeyRepeater::Press
void Press(Key key)
Presses a key, effectively simulating keydown.
Definition: KeyRepeater.cpp:57
Gorgon::Event
This class provides event mechanism.
Definition: Event.h:134
Gorgon::Animation::ControllerBase::GetProgress
virtual unsigned GetProgress() const =0
Returns the current progress of the timer.
Gorgon::Animation::ControllerBase
Controllers are required to progress animations.
Definition: Animation.h:65
Gorgon::Input::KeyRepeater::Progress
virtual bool Progress(unsigned &) override
This function should progress the animation.
Definition: KeyRepeater.cpp:14
Gorgon::Input::KeyRepeater::SetAccelerationStart
void SetAccelerationStart(int value)
Sets the number of repeats after which the acceleration will start excluding first press if repeat on...
Definition: KeyRepeater.h:201
Gorgon::Input::internal::eventunregisterer::unregister
virtual void unregister() override
Definition: KeyRepeater.h:23
Gorgon::Input::KeyRepeater::GetAccelerationCount
int GetAccelerationCount() const
Returns how many times acceleration can be applied.
Definition: KeyRepeater.h:219
Gorgon::Input::KeyRepeater::KeyRepeater
KeyRepeater(const KeyRepeater &)=delete
Disable copy constructor.
Gorgon::Input::KeyRepeater::KeyRepeater
KeyRepeater()
Default constructor.
Definition: KeyRepeater.h:58
Gorgon::Input::KeyRepeater::IsPressed
bool IsPressed(Key key) const
Checks if a key is pressed.
Definition: KeyRepeater.h:140
Gorgon::Input::KeyRepeater::SetRepeatOnRelease
void SetRepeatOnRelease(bool value)
Sets if the key should be repeated right after the key is released.
Definition: KeyRepeater.h:155
Gorgon::Input::KeyRepeater::Register
void Register(Key key, T_ &&... args)
Registers the given keys to be repeated.
Definition: KeyRepeater.h:111
Gorgon::Input::Key
int Key
A type to represent an input key.
Definition: Input.h:14
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Animation::Base
This is the base class for all animations.
Definition: Animation.h:306
Gorgon::Input::KeyRepeater::SetInitialDelay
void SetInitialDelay(int value)
Sets the initial delay before the first (or second if instant repeat is on) key is repeated in millis...
Definition: KeyRepeater.h:166
Gorgon::Input::KeyRepeater::RegisterTo
void RegisterTo(E_ &event, bool ignoremodified=false)
Registers this repeater to the given event to obtain press and release actions.
Definition: KeyRepeater.h:84
Gorgon::Input::KeyRepeater::GetRepeatOnPress
bool GetRepeatOnPress() const
Returns whether the key will be repeated instantly when pressed.
Definition: KeyRepeater.h:150
Gorgon::Input::KeyRepeater::SetupAcceleration
void SetupAcceleration(int startdelay, int finaldelay, int rampup)
This function allows easy setup for acceleration by supplying starting delay, final delay and the tim...
Definition: KeyRepeater.cpp:94
Gorgon::Input::KeyRepeater::SetAcceleration
void SetAcceleration(int value)
Change in repeat delay per repeat in milliseconds, positive values will reduce delay by the given amo...
Definition: KeyRepeater.h:189
Gorgon::Input::internal::eventunregisterer::token
EventToken token
Definition: KeyRepeater.h:28
Gorgon::Input::KeyRepeater::GetDelay
int GetDelay() const
Returns the delay between successive repeats in milliseconds.
Definition: KeyRepeater.h:183
Gorgon::Input::KeyRepeater::SetDelay
void SetDelay(int value)
Repeat delay between successive repeat events in milliseconds.
Definition: KeyRepeater.h:178
Gorgon::Input::internal::eventunregisterhelper::~eventunregisterhelper
virtual ~eventunregisterhelper()
Definition: KeyRepeater.h:14
Gorgon::Input::KeyRepeater::SetController
virtual void SetController(Animation::ControllerBase &controller) override
Sets the controller to the given controller.
Definition: KeyRepeater.h:237
Gorgon::Input::KeyRepeater::GetDuration
virtual int GetDuration() const override
Returns the duration of the animation if it is a known apriori.
Definition: KeyRepeater.h:235
Gorgon::Input::KeyRepeater::SetRepeatOnPress
void SetRepeatOnPress(bool value)
Sets whether the key will be repeated instantly when pressed.
Definition: KeyRepeater.h:145
Gorgon::Input::internal::eventunregisterer
Definition: KeyRepeater.h:19
Gorgon::Input::internal::eventunregisterhelper
Definition: KeyRepeater.h:13
Gorgon::Input::KeyRepeater::GetAccelerationStart
int GetAccelerationStart() const
Returns the number of repeats after which the acceleration will start excluding first press if repeat...
Definition: KeyRepeater.h:207
Gorgon::Input::KeyRepeater
This class simplifies the use of repeated keystrokes when a key is pressed.
Definition: KeyRepeater.h:54
Gorgon::Input::KeyRepeater::Unregister
void Unregister(Key key)
Unregisters a key from this repeater.
Definition: KeyRepeater.h:122
Gorgon::Animation::Base::SetController
virtual void SetController(ControllerBase &controller)
Sets the controller to the given controller.
Definition: Animation.h:334
Gorgon::Input::KeyRepeater::KeyEvent
bool KeyEvent(Key &key, float amount)
This function is used to handle key events.
Definition: KeyRepeater.cpp:104
Gorgon::Input::internal::eventunregisterer::eventunregisterer
eventunregisterer(T_ &event, EventToken token)
Definition: KeyRepeater.h:20
KeyRepeater.h
Gorgon::Input::KeyRepeater::IsKeyRegistered
bool IsKeyRegistered(Key key) const
Returns whether a given is registered for automatic management.
Definition: KeyRepeater.h:128
Gorgon::Animation::Base::controller
ControllerBase * controller
Controller of this animation.
Definition: Animation.h:388
Gorgon::Input::internal::eventunregisterhelper::unregister
virtual void unregister()=0
Gorgon::Input::KeyRepeater::GetInitialDelay
bool GetInitialDelay() const
Returns the initial delay before the first (or second if instant repeat is on) key is repeated in mil...
Definition: KeyRepeater.h:172
Gorgon::Input::KeyRepeater::UnregisterFrom
void UnregisterFrom()
Unregisters this repeater from its registered event.
Definition: KeyRepeater.h:94
Gorgon::Input::KeyRepeater::GetRepeatOnRelease
bool GetRepeatOnRelease() const
Returns if the key will be repeated right after the key is released.
Definition: KeyRepeater.h:160
Gorgon::Input::KeyRepeater::GetFinalDelay
int GetFinalDelay() const
Returns the final delay between repeat events in milliseconds after acceleration is completed.
Definition: KeyRepeater.h:224
Keyboard.h
Gorgon::Input::KeyRepeater::GetAcceleration
int GetAcceleration() const
Returns the change in repeat delay per repeat in milliseconds, positive values will reduce delay by t...
Definition: KeyRepeater.h:195
Gorgon::Input::KeyRepeater::Repeat
Event< KeyRepeater, Key > Repeat
Press event that is called everytime a key is pressed.
Definition: KeyRepeater.h:247
Gorgon::Input::internal::eventunregisterer::event
T_ & event
Definition: KeyRepeater.h:27