BsOSInputHandler.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. float mMouseScroll;
  89. WString mInputString;
  90. Queue<ButtonStateChange> mButtonStates;
  91. Queue<DoubleClick> mDoubleClicks;
  92. Queue<InputCommandType> mInputCommands;
  93. OSPointerButtonStates mMouseMoveBtnState;
  94. HEvent mCharInputConn;
  95. HEvent mCursorMovedConn;
  96. HEvent mCursorPressedConn;
  97. HEvent mCursorReleasedConn;
  98. HEvent mCursorDoubleClickConn;
  99. HEvent mInputCommandConn;
  100. HEvent mMouseWheelScrolledConn;
  101. /**
  102. * @brief Called from the message loop to notify user has entered a character.
  103. *
  104. * @see onCharInput
  105. */
  106. void charInput(UINT32 character);
  107. /**
  108. * @brief Called from the message loop to notify user has moved the cursor.
  109. *
  110. * @see onCursorMoved
  111. */
  112. void cursorMoved(const Vector2I& cursorPos, OSPointerButtonStates& btnStates);
  113. /**
  114. * @brief Called from the message loop to notify user has pressed a mouse button.
  115. *
  116. * @see onCursorPressed
  117. */
  118. void cursorPressed(const Vector2I& cursorPos, OSMouseButton button, OSPointerButtonStates& btnStates);
  119. /**
  120. * @brief Called from the message loop to notify user has released a mouse button.
  121. *
  122. * @see onCursorReleased
  123. */
  124. void cursorReleased(const Vector2I& cursorPos, OSMouseButton button, OSPointerButtonStates& btnStates);
  125. /**
  126. * @brief Called from the message loop to notify user has double-clicked a mouse button.
  127. *
  128. * @see onDoubleClick
  129. */
  130. void cursorDoubleClick(const Vector2I& cursorPos, OSPointerButtonStates& btnStates);
  131. /**
  132. * @brief Called from the message loop to notify user has entered an input command.
  133. *
  134. * @see onInputCommand
  135. */
  136. void inputCommandEntered(InputCommandType commandType);
  137. /**
  138. * @brief Called from the message loop to notify user has scrolled the mouse wheel.
  139. *
  140. * @see onMouseWheelScrolled
  141. */
  142. void mouseWheelScrolled(float scrollPos);
  143. };
  144. }