Input.h 2.4 KB

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