BsOSInputHandler.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsPlatform.h"
  4. #include "BsEvent.h"
  5. #include "BsVector2I.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Represents a specific way of acquiring OS input. InputManager (which provides a higher level input)
  10. * must have at least one OSInputHandler attached. Attach events handler to the provided signals to handle input.
  11. *
  12. * @note Unlike RawInputHandler this class receives input from the operating system, and is used for receiving
  13. * text input, cursor position and similar.
  14. */
  15. class BS_CORE_EXPORT OSInputHandler
  16. {
  17. /**
  18. * @brief Contains information regarding a button state change event.
  19. */
  20. struct ButtonStateChange
  21. {
  22. Vector2I cursorPos;
  23. OSPointerButtonStates btnStates;
  24. OSMouseButton button;
  25. bool pressed;
  26. };
  27. /**
  28. * @brief Contains information regarding a double click event.
  29. */
  30. struct DoubleClick
  31. {
  32. Vector2I cursorPos;
  33. OSPointerButtonStates btnStates;
  34. };
  35. public:
  36. OSInputHandler();
  37. virtual ~OSInputHandler();
  38. /**
  39. * @brief Triggers when user inputs a character. The character might be a result of pressing
  40. * multiple keys, so character input will not necessarily correspond with button presses.
  41. * Provide character code of the input character.
  42. */
  43. Event<void(UINT32)> onCharInput;
  44. /**
  45. * @brief Triggers whenever user scrolls the mouse wheel. Returns the screen
  46. * position of the mouse cursor and delta amount of mouse scroll (can be negative or positive).
  47. */
  48. Event<void(const Vector2I&, float)> onMouseWheelScrolled;
  49. /**
  50. * @brief Triggers whenever user moves the mouse cursor.
  51. */
  52. Event<void(const PointerEvent&)> onCursorMoved;
  53. /**
  54. * @brief Triggers whenever user presses one of the mouse buttons.
  55. */
  56. Event<void(const PointerEvent&)> onCursorPressed;
  57. /**
  58. * @brief Triggers whenever user releases one of the mouse buttons.
  59. */
  60. Event<void(const PointerEvent&)> onCursorReleased;
  61. /**
  62. * @brief Triggers when user clicks a mouse button quickly twice in a row.
  63. */
  64. Event<void(const PointerEvent&)> onDoubleClick;
  65. /**
  66. * @brief Triggers when user inputa a special input command, like commands user
  67. * for manipulating text input.
  68. */
  69. Event<void(InputCommandType)> onInputCommand;
  70. /**
  71. * @brief Called once per frame. Capture input here if needed.
  72. *
  73. * @note Internal method.
  74. */
  75. virtual void _update();
  76. /**
  77. * @brief Called whenever the active window changes.
  78. *
  79. * @param win Newly active window.
  80. *
  81. * @note Internal method.
  82. */
  83. virtual void _inputWindowChanged(const RenderWindow& win) { }
  84. private:
  85. BS_MUTEX(mOSInputMutex);
  86. Vector2I mLastCursorPos;
  87. Vector2I mCursorPosition;
  88. Vector2I mDelta;
  89. bool mLastCursorPosSet;
  90. float mMouseScroll;
  91. WString mInputString;
  92. Queue<ButtonStateChange> mButtonStates;
  93. Queue<DoubleClick> mDoubleClicks;
  94. Queue<InputCommandType> mInputCommands;
  95. OSPointerButtonStates mMouseMoveBtnState;
  96. HEvent mCharInputConn;
  97. HEvent mCursorMovedConn;
  98. HEvent mCursorPressedConn;
  99. HEvent mCursorReleasedConn;
  100. HEvent mCursorDoubleClickConn;
  101. HEvent mInputCommandConn;
  102. HEvent mMouseWheelScrolledConn;
  103. /**
  104. * @brief Called from the message loop to notify user has entered a character.
  105. *
  106. * @see onCharInput
  107. */
  108. void charInput(UINT32 character);
  109. /**
  110. * @brief Called from the message loop to notify user has moved the cursor.
  111. *
  112. * @see onCursorMoved
  113. */
  114. void cursorMoved(const Vector2I& cursorPos, OSPointerButtonStates& btnStates);
  115. /**
  116. * @brief Called from the message loop to notify user has pressed a mouse button.
  117. *
  118. * @see onCursorPressed
  119. */
  120. void cursorPressed(const Vector2I& cursorPos, OSMouseButton button, OSPointerButtonStates& btnStates);
  121. /**
  122. * @brief Called from the message loop to notify user has released a mouse button.
  123. *
  124. * @see onCursorReleased
  125. */
  126. void cursorReleased(const Vector2I& cursorPos, OSMouseButton button, OSPointerButtonStates& btnStates);
  127. /**
  128. * @brief Called from the message loop to notify user has double-clicked a mouse button.
  129. *
  130. * @see onDoubleClick
  131. */
  132. void cursorDoubleClick(const Vector2I& cursorPos, OSPointerButtonStates& btnStates);
  133. /**
  134. * @brief Called from the message loop to notify user has entered an input command.
  135. *
  136. * @see onInputCommand
  137. */
  138. void inputCommandEntered(InputCommandType commandType);
  139. /**
  140. * @brief Called from the message loop to notify user has scrolled the mouse wheel.
  141. *
  142. * @see onMouseWheelScrolled
  143. */
  144. void mouseWheelScrolled(float scrollPos);
  145. };
  146. }