Input.h 2.4 KB

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