BsOSInputHandler.h 4.8 KB

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