GameKit  0.0.1a
C++ gamedev tools
InputHandler.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: InputHandler.hpp
5  *
6  * Description:
7  *
8  * Created: 17/01/2018 19:09:57
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_INPUTHANDLER_HPP_
15 #define GK_INPUTHANDLER_HPP_
16 
17 #include <map>
18 
19 #include "gk/core/IntTypes.hpp"
20 
21 namespace gk {
22 
23 using GameKey = u32;
24 
29 class InputHandler {
30  public:
31  virtual ~InputHandler() = default;
32 
41  virtual bool isKeyPressed(GameKey key) { return m_keysPressed[key]; }
42 
51  virtual bool isKeyPressedOnce(GameKey key);
52 
62  virtual bool isKeyPressedWithDelay(GameKey key, u16 delay);
63 
70  const std::map<GameKey, bool> &keysPressed() const { return m_keysPressed; }
71 
72  protected:
79  void addKey(GameKey key);
80 
82  // Member data
84  std::map<GameKey, bool> m_keysPressed;
85  std::map<GameKey, bool> m_keysPressedOnce;
86  std::map<GameKey, u32> m_lastTimePressed;
87 };
88 
89 } // namespace gk
90 
91 #endif // GK_INPUTHANDLER_HPP_
92 
Give access to the real time state of an input device (keyboard, gamepad, etc...) ...
virtual ~InputHandler()=default
unsigned short u16
Definition: IntTypes.hpp:22
virtual bool isKeyPressedWithDelay(GameKey key, u16 delay)
Check if a key is pressed with delay.
std::map< GameKey, bool > m_keysPressed
Keys state.
virtual bool isKeyPressedOnce(GameKey key)
Check if a key is pressed only one time.
virtual bool isKeyPressed(GameKey key)
Check if a key is pressed.
const std::map< GameKey, bool > & keysPressed() const
Get current keys state.
unsigned int u32
Definition: IntTypes.hpp:23
std::map< GameKey, u32 > m_lastTimePressed
Keys state for isKeyPressedWithDelay.
std::map< GameKey, bool > m_keysPressedOnce
Keys state for isKeyPressedOnce.
u32 GameKey
Alias for game key type.
void addKey(GameKey key)
Add a new key to this input handler.