Input.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (C) 2009-2021, 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. public:
  27. static ANKI_USE_RESULT Error newInstance(AllocAlignedCallback allocCallback, void* allocCallbackUserData,
  28. NativeWindow* nativeWindow, Input*& input);
  29. static void deleteInstance(Input* input);
  30. U32 getKey(KeyCode i) const
  31. {
  32. return m_keys[i];
  33. }
  34. U32 getMouseButton(MouseButton i) const
  35. {
  36. return m_mouseBtns[i];
  37. }
  38. const Vec2& getMousePosition() const
  39. {
  40. return m_mousePosNdc;
  41. }
  42. const UVec2& getWindowMousePosition() const
  43. {
  44. return m_mousePosWin;
  45. }
  46. /// Get the times an event was triggered and resets the counter
  47. U32 getEvent(InputEvent eventId) const
  48. {
  49. return m_events[eventId];
  50. }
  51. /// Reset the keys and mouse buttons
  52. void reset()
  53. {
  54. zeroMemory(m_keys);
  55. zeroMemory(m_mouseBtns);
  56. m_mousePosNdc = Vec2(-1.0f);
  57. m_mousePosWin = UVec2(0u);
  58. zeroMemory(m_events);
  59. zeroMemory(m_textInput);
  60. zeroMemory(m_touchPointers);
  61. zeroMemory(m_touchPointerPosNdc);
  62. zeroMemory(m_touchPointerPosWin);
  63. }
  64. /// Populate the key and button with the new state
  65. ANKI_USE_RESULT Error handleEvents();
  66. /// Move the mouse cursor to a position inside the window. Useful for locking the cursor into a fixed location (eg
  67. /// in the center of the screen)
  68. void moveCursor(const Vec2& posNdc);
  69. /// Hide the mouse cursor
  70. void hideCursor(Bool hide);
  71. /// Lock mouse to (0, 0)
  72. void lockCursor(Bool lock)
  73. {
  74. m_lockCurs = lock;
  75. }
  76. /// Add a new event
  77. void addEvent(InputEvent eventId)
  78. {
  79. ++m_events[eventId];
  80. }
  81. template<typename TFunc>
  82. void iteratePressedKeys(TFunc func) const
  83. {
  84. for(KeyCode i = KeyCode::FIRST; i < KeyCode::COUNT; ++i)
  85. {
  86. if(m_keys[i] > 0)
  87. {
  88. func(i, m_keys[i]);
  89. }
  90. }
  91. }
  92. /// Get some easy to digest input from the keyboard.
  93. CString getTextInput() const
  94. {
  95. return &m_textInput[0];
  96. }
  97. U32 getTouchPointer(TouchPointer p) const
  98. {
  99. return m_touchPointers[p];
  100. }
  101. Vec2 getTouchPointerNdcPosition(TouchPointer p) const
  102. {
  103. return m_touchPointerPosNdc[p];
  104. }
  105. UVec2 getTouchPointerWindowPosition(TouchPointer p) const
  106. {
  107. return m_touchPointerPosWin[p];
  108. }
  109. protected:
  110. NativeWindow* m_nativeWindow = nullptr;
  111. HeapAllocator<U8> m_alloc;
  112. /// Shows the current key state
  113. /// - 0 times: unpressed
  114. /// - 1 times: pressed once
  115. /// - >1 times: Kept pressed 'n' times continuously
  116. Array<U32, U(KeyCode::COUNT)> m_keys;
  117. /// Mouse btns. Supporting 3 btns & wheel. @see keys
  118. Array<U32, U(MouseButton::COUNT)> m_mouseBtns;
  119. Vec2 m_mousePosNdc;
  120. UVec2 m_mousePosWin;
  121. Array<U32, U(TouchPointer::COUNT)> m_touchPointers;
  122. Array<Vec2, U(TouchPointer::COUNT)> m_touchPointerPosNdc;
  123. Array<UVec2, U(TouchPointer::COUNT)> m_touchPointerPosWin;
  124. Array<U8, U(InputEvent::COUNT)> m_events;
  125. /// The keybord input as ascii.
  126. Array<char, U(KeyCode::COUNT)> m_textInput;
  127. Bool m_lockCurs = false;
  128. Input()
  129. {
  130. reset();
  131. }
  132. ~Input()
  133. {
  134. }
  135. };
  136. } // end namespace anki