Input.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (C) 2009-2022, 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. // Forward
  13. class InputImpl;
  14. class NativeWindow;
  15. enum class InputEvent : U8
  16. {
  17. WINDOW_FOCUS_LOST,
  18. WINDOW_FOCUS_GAINED,
  19. WINDOW_CLOSED,
  20. COUNT
  21. };
  22. /// Handle the input and other events
  23. /// @note All positions are in NDC space
  24. class Input
  25. {
  26. ANKI_FRIEND_ALLOCATOR
  27. public:
  28. static Error newInstance(AllocAlignedCallback allocCallback, void* allocCallbackUserData,
  29. NativeWindow* nativeWindow, Input*& input);
  30. static void deleteInstance(Input* input);
  31. U32 getKey(KeyCode i) const
  32. {
  33. return m_keys[i];
  34. }
  35. U32 getMouseButton(MouseButton i) const
  36. {
  37. return m_mouseBtns[i];
  38. }
  39. const Vec2& getMousePosition() const
  40. {
  41. return m_mousePosNdc;
  42. }
  43. const UVec2& getWindowMousePosition() const
  44. {
  45. return m_mousePosWin;
  46. }
  47. /// Get the times an event was triggered and resets the counter
  48. U32 getEvent(InputEvent eventId) const
  49. {
  50. return m_events[eventId];
  51. }
  52. /// Reset the keys and mouse buttons
  53. void reset()
  54. {
  55. zeroMemory(m_keys);
  56. zeroMemory(m_mouseBtns);
  57. m_mousePosNdc = Vec2(-1.0f);
  58. m_mousePosWin = UVec2(0u);
  59. zeroMemory(m_events);
  60. zeroMemory(m_textInput);
  61. zeroMemory(m_touchPointers);
  62. zeroMemory(m_touchPointerPosNdc);
  63. zeroMemory(m_touchPointerPosWin);
  64. }
  65. /// Populate the key and button with the new state
  66. 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[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. U32 getTouchPointer(TouchPointer p) const
  99. {
  100. return m_touchPointers[p];
  101. }
  102. Vec2 getTouchPointerNdcPosition(TouchPointer p) const
  103. {
  104. return m_touchPointerPosNdc[p];
  105. }
  106. UVec2 getTouchPointerWindowPosition(TouchPointer p) const
  107. {
  108. return m_touchPointerPosWin[p];
  109. }
  110. Bool hasTouchDevice() const;
  111. protected:
  112. NativeWindow* m_nativeWindow = nullptr;
  113. HeapAllocator<U8> m_alloc;
  114. /// Shows the current key state
  115. /// - 0 times: unpressed
  116. /// - 1 times: pressed once
  117. /// - >1 times: Kept pressed 'n' times continuously
  118. Array<U32, U(KeyCode::COUNT)> m_keys;
  119. /// Mouse btns. Supporting 3 btns & wheel. @see keys
  120. Array<U32, U(MouseButton::COUNT)> m_mouseBtns;
  121. Vec2 m_mousePosNdc;
  122. UVec2 m_mousePosWin;
  123. Array<U32, U(TouchPointer::COUNT)> m_touchPointers;
  124. Array<Vec2, U(TouchPointer::COUNT)> m_touchPointerPosNdc;
  125. Array<UVec2, U(TouchPointer::COUNT)> m_touchPointerPosWin;
  126. Array<U8, U(InputEvent::COUNT)> m_events;
  127. /// The keybord input as ascii.
  128. Array<char, U(KeyCode::COUNT)> m_textInput;
  129. Bool m_lockCurs = false;
  130. Input()
  131. {
  132. reset();
  133. }
  134. ~Input()
  135. {
  136. }
  137. };
  138. } // end namespace anki