Input.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_INPUT_INPUT_H
  6. #define ANKI_INPUT_INPUT_H
  7. #include "anki/Math.h"
  8. #include "anki/util/Singleton.h"
  9. #include "anki/util/Array.h"
  10. #include "anki/util/StdTypes.h"
  11. #include "anki/input/KeyCode.h"
  12. namespace anki {
  13. class InputImpl;
  14. class NativeWindow;
  15. /// Handle the input and other events
  16. ///
  17. /// @note All positions are in NDC space
  18. class Input
  19. {
  20. public:
  21. enum class Event: U8
  22. {
  23. WINDOW_FOCUS_LOST,
  24. WINDOW_FOCUS_GAINED,
  25. WINDOW_CLOSED,
  26. COUNT
  27. };
  28. Input()
  29. {}
  30. ~Input()
  31. {
  32. destroy();
  33. ANKI_ASSERT(m_impl == nullptr);
  34. ANKI_ASSERT(m_nativeWindow == nullptr);
  35. }
  36. ANKI_USE_RESULT Error create(NativeWindow* nativeWindow)
  37. {
  38. reset();
  39. return init(nativeWindow);
  40. }
  41. U getKey(KeyCode i) const
  42. {
  43. return m_keys[static_cast<U>(i)];
  44. }
  45. U getMouseButton(U32 i) const
  46. {
  47. return m_mouseBtns[i];
  48. }
  49. const Vec2& getMousePosition() const
  50. {
  51. return m_mousePosNdc;
  52. }
  53. /// Get the times an event was triggered and resets the counter
  54. U getEvent(Event eventId) const
  55. {
  56. return m_events[static_cast<U>(eventId)];
  57. }
  58. /// Reset the keys and mouse buttons
  59. void reset();
  60. /// Populate the key and button with the new state
  61. ANKI_USE_RESULT Error handleEvents();
  62. /// Move the mouse cursor to a position inside the window. Useful for
  63. /// locking the cursor into a fixed location (eg in the center of the
  64. /// screen)
  65. void moveCursor(const Vec2& posNdc);
  66. /// Hide the mouse cursor
  67. void hideCursor(Bool hide);
  68. /// Lock mouse to (0, 0)
  69. void lockCursor(Bool lock)
  70. {
  71. m_lockCurs = lock;
  72. }
  73. /// Add a new event
  74. void addEvent(Event eventId)
  75. {
  76. ++m_events[static_cast<U>(eventId)];
  77. }
  78. private:
  79. InputImpl* m_impl = nullptr;
  80. NativeWindow* m_nativeWindow = nullptr;
  81. /// @name Keys and btns
  82. /// @{
  83. /// Shows the current key state
  84. /// - 0 times: unpressed
  85. /// - 1 times: pressed once
  86. /// - >1 times: Kept pressed 'n' times continuously
  87. Array<U32, static_cast<U>(KeyCode::COUNT)> m_keys;
  88. /// Mouse btns. Supporting 3 btns & wheel. @see keys
  89. Array<U32, 8> m_mouseBtns;
  90. /// @}
  91. Vec2 m_mousePosNdc = Vec2(2.0); ///< The coords are in the NDC space
  92. Array<U8, static_cast<U>(Event::COUNT)> m_events;
  93. Bool8 m_lockCurs = false;
  94. /// Initialize the platform's input system
  95. ANKI_USE_RESULT Error init(NativeWindow* nativeWindow);
  96. /// Destroy the platform specific input system
  97. void destroy();
  98. };
  99. } // end namespace anki
  100. #endif