Input.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  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/String.h>
  10. #include <anki/input/KeyCode.h>
  11. namespace anki
  12. {
  13. // Forward
  14. class InputImpl;
  15. class NativeWindow;
  16. enum class InputEvent : U8
  17. {
  18. WINDOW_FOCUS_LOST,
  19. WINDOW_FOCUS_GAINED,
  20. WINDOW_CLOSED,
  21. COUNT
  22. };
  23. /// Handle the input and other events
  24. /// @note All positions are in NDC space
  25. class Input
  26. {
  27. public:
  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 init(NativeWindow* nativeWindow)
  38. {
  39. reset();
  40. return initInternal(nativeWindow);
  41. }
  42. U32 getKey(KeyCode i) const
  43. {
  44. return m_keys[i];
  45. }
  46. U32 getMouseButton(MouseButton i) const
  47. {
  48. return m_mouseBtns[i];
  49. }
  50. const Vec2& getMousePosition() const
  51. {
  52. return m_mousePosNdc;
  53. }
  54. const UVec2& getWindowMousePosition() const
  55. {
  56. return m_mousePosWin;
  57. }
  58. /// Get the times an event was triggered and resets the counter
  59. U getEvent(InputEvent eventId) const
  60. {
  61. return m_events[static_cast<U>(eventId)];
  62. }
  63. /// Reset the keys and mouse buttons
  64. void reset();
  65. /// Populate the key and button with the new state
  66. ANKI_USE_RESULT Error handleEvents();
  67. /// Move the mouse cursor to a position inside the window. Useful for locking the cursor into a fixed location (eg
  68. /// in the center of the screen)
  69. void moveCursor(const Vec2& posNdc);
  70. /// Hide the mouse cursor
  71. void hideCursor(Bool hide);
  72. /// Lock mouse to (0, 0)
  73. void lockCursor(Bool lock)
  74. {
  75. m_lockCurs = lock;
  76. }
  77. /// Add a new event
  78. void addEvent(InputEvent eventId)
  79. {
  80. ++m_events[static_cast<U>(eventId)];
  81. }
  82. template<typename TFunc>
  83. void iteratePressedKeys(TFunc func) const
  84. {
  85. for(KeyCode i = KeyCode::FIRST; i < KeyCode::COUNT; ++i)
  86. {
  87. if(m_keys[i] > 0)
  88. {
  89. func(i, m_keys[i]);
  90. }
  91. }
  92. }
  93. /// Get some easy to digest input from the keyboard.
  94. CString getTextInput() const
  95. {
  96. return &m_textInput[0];
  97. }
  98. private:
  99. InputImpl* m_impl = nullptr;
  100. NativeWindow* m_nativeWindow = nullptr;
  101. /// @name Keys and btns
  102. /// @{
  103. /// Shows the current key state
  104. /// - 0 times: unpressed
  105. /// - 1 times: pressed once
  106. /// - >1 times: Kept pressed 'n' times continuously
  107. Array<U32, static_cast<U>(KeyCode::COUNT)> m_keys;
  108. /// Mouse btns. Supporting 3 btns & wheel. @see keys
  109. Array<U32, U(MouseButton::COUNT)> m_mouseBtns;
  110. /// @}
  111. Vec2 m_mousePosNdc = Vec2(2.0); ///< The coords are in the NDC space
  112. UVec2 m_mousePosWin = UVec2(0u);
  113. Array<U8, static_cast<U>(InputEvent::COUNT)> m_events;
  114. /// The keybord input as ascii.
  115. Array<char, static_cast<U>(KeyCode::COUNT)> m_textInput;
  116. Bool m_lockCurs = false;
  117. /// Initialize the platform's input system
  118. ANKI_USE_RESULT Error initInternal(NativeWindow* nativeWindow);
  119. /// Destroy the platform specific input system
  120. void destroy();
  121. };
  122. } // end namespace anki