Input.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef ANKI_INPUT_INPUT_H
  2. #define ANKI_INPUT_INPUT_H
  3. #include "anki/Math.h"
  4. #include "anki/util/Singleton.h"
  5. #include "anki/util/Array.h"
  6. #include "anki/util/StdTypes.h"
  7. #include "anki/input/KeyCode.h"
  8. #include <memory>
  9. namespace anki {
  10. struct InputImpl;
  11. class NativeWindow;
  12. /// Handle the input and other events
  13. ///
  14. /// @note All positions are in NDC space
  15. class Input
  16. {
  17. public:
  18. enum Event
  19. {
  20. WINDOW_FOCUS_LOST_EVENT,
  21. WINDOW_FOCUS_GAINED_EVENT,
  22. WINDOW_CLOSED_EVENT,
  23. EVENTS_COUNT
  24. };
  25. Input()
  26. {
  27. reset();
  28. }
  29. ~Input();
  30. /// @name Acessors
  31. /// @{
  32. U getKey(U32 i) const
  33. {
  34. return keys[i];
  35. }
  36. U getMouseButton(U32 i) const
  37. {
  38. return mouseBtns[i];
  39. }
  40. const Vec2& getMousePosition() const
  41. {
  42. return mousePosNdc;
  43. }
  44. /// Get the times an event was triggered and resets the counter
  45. U getEvent(Event eventId) const
  46. {
  47. return events[eventId];
  48. }
  49. /// @}
  50. /// Initialize the platform's input system
  51. void init(NativeWindow* nativeWindow);
  52. /// Reset the keys and mouse buttons
  53. void reset();
  54. /// Populate the key and button with the new state
  55. void handleEvents();
  56. /// Move the mouse cursor to a position inside the window. Useful for
  57. /// locking the cursor into a fixed location (eg in the center of the
  58. /// screen)
  59. void moveCursor(const Vec2& posNdc);
  60. /// Hide the mouse cursor
  61. void hideCursor(Bool hide);
  62. /// Lock mouse to (0, 0)
  63. void lockCursor(Bool lock)
  64. {
  65. lockCurs = lock;
  66. }
  67. /// Add a new event
  68. void addEvent(Event eventId)
  69. {
  70. ++events[eventId];
  71. }
  72. private:
  73. NativeWindow* nativeWindow = nullptr;
  74. /// @name Keys and btns
  75. /// @{
  76. /// Shows the current key state
  77. /// - 0 times: unpressed
  78. /// - 1 times: pressed once
  79. /// - >1 times: Kept pressed 'n' times continuously
  80. Array<U32, KC_COUNT> keys;
  81. /// Mouse btns. Supporting 3 btns & wheel. @see keys
  82. Array<U32, 8> mouseBtns;
  83. /// @}
  84. Vec2 mousePosNdc = Vec2(2.0); ///< The coords are in the NDC space
  85. Array<U8, EVENTS_COUNT> events;
  86. std::shared_ptr<InputImpl> impl;
  87. Bool8 lockCurs = false;
  88. };
  89. typedef Singleton<Input> InputSingleton;
  90. } // end namespace anki
  91. #endif