CG_Labs  2021.2
InputHandler.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <cstdint>
5 #include <unordered_map>
6 
7 #define GLFW_INCLUDE_NONE
8 #include <GLFW/glfw3.h>
9 #include <glm/vec2.hpp>
10 
11 /*----------------------------------------------------------------------------*/
12 
13 #define PRESSED (1 << 0)
14 #define RELEASED (1 << 1)
15 #define JUST_PRESSED (1 << 2)
16 #define JUST_RELEASED (1 << 3)
17 
19 {
20 public:
21  struct IState {
22  std::uint64_t mDownTick{ std::numeric_limits<std::uint64_t>::max() };
23  std::uint64_t mUpTick{ std::numeric_limits<std::uint64_t>::max() };
24  bool mIsDown{ false };
25  };
26 
27 public:
28  InputHandler();
29 
30 public:
31  void FeedKeyboard(int key, int scancode, int action);
32  void FeedMouseButtons(int button, int action);
33  void FeedMouseMotion(glm::vec2 const& position);
34  void Advance();
35  std::uint32_t GetScancodeState(int scancode);
36  std::uint32_t GetKeycodeState(int key);
37  std::uint32_t GetMouseState(std::uint32_t button);
38  glm::vec2 GetMousePositionAtStateShift(std::uint32_t button);
39  glm::vec2 GetMousePosition();
40  bool IsMouseCapturedByUI() const;
41  bool IsKeyboardCapturedByUI() const;
42  void SetUICapture(bool mouseCapture, bool keyboardCapture);
43 
44 private:
45  using InputStateMap = std::unordered_map<size_t, IState>;
46 
47  void DownEvent(InputStateMap& map, size_t loc);
48  void UpEvent(InputStateMap& map, size_t loc);
49  std::uint32_t GetState(InputStateMap const& map, size_t loc);
50 
54 
55  glm::vec2 mMousePosition{ -1.0f };
56  std::array<glm::vec2, GLFW_MOUSE_BUTTON_LAST> mMousePositionSwitched;
57 
58  bool mMouseCapturedByUI{ false };
59  bool mKeyboardCapturedByUI{ false };
60 
61  std::uint64_t mTick{ 0ULL };
62 
63 };
64 
Definition: InputHandler.h:19
glm::vec2 mMousePosition
Definition: InputHandler.h:55
InputStateMap mKeycodeMap
Definition: InputHandler.h:52
std::uint32_t GetMouseState(std::uint32_t button)
Definition: InputHandler.cpp:100
void UpEvent(InputStateMap &map, size_t loc)
Definition: InputHandler.cpp:28
std::array< glm::vec2, GLFW_MOUSE_BUTTON_LAST > mMousePositionSwitched
Definition: InputHandler.h:56
std::uint32_t GetKeycodeState(int key)
Definition: InputHandler.cpp:95
std::uint32_t GetScancodeState(int scancode)
Definition: InputHandler.cpp:90
InputHandler()
Definition: InputHandler.cpp:5
bool IsMouseCapturedByUI() const
Definition: InputHandler.cpp:115
std::uint64_t mTick
Definition: InputHandler.h:61
bool mMouseCapturedByUI
Definition: InputHandler.h:58
InputStateMap mScancodeMap
Definition: InputHandler.h:51
void DownEvent(InputStateMap &map, size_t loc)
Definition: InputHandler.cpp:18
void FeedMouseButtons(int button, int action)
Definition: InputHandler.cpp:60
glm::vec2 GetMousePosition()
Definition: InputHandler.cpp:110
void SetUICapture(bool mouseCapture, bool keyboardCapture)
Definition: InputHandler.cpp:125
void FeedMouseMotion(glm::vec2 const &position)
Definition: InputHandler.cpp:55
void FeedKeyboard(int key, int scancode, int action)
Definition: InputHandler.cpp:38
std::unordered_map< size_t, IState > InputStateMap
Definition: InputHandler.h:45
InputStateMap mMouseMap
Definition: InputHandler.h:53
bool mKeyboardCapturedByUI
Definition: InputHandler.h:59
std::uint32_t GetState(InputStateMap const &map, size_t loc)
Definition: InputHandler.cpp:76
glm::vec2 GetMousePositionAtStateShift(std::uint32_t button)
Definition: InputHandler.cpp:105
bool IsKeyboardCapturedByUI() const
Definition: InputHandler.cpp:120
void Advance()
Definition: InputHandler.cpp:13
Definition: InputHandler.h:21
std::uint64_t mDownTick
Definition: InputHandler.h:22
std::uint64_t mUpTick
Definition: InputHandler.h:23
bool mIsDown
Definition: InputHandler.h:24