2
0

CmOSInputHandler.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmPlatform.h"
  4. #include <boost/signal.hpp>
  5. #include "CmVector2I.h"
  6. namespace CamelotFramework
  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 CM_EXPORT OSInputHandler
  16. {
  17. struct ButtonStateChange
  18. {
  19. Vector2I cursorPos;
  20. OSPositionalInputButtonStates btnStates;
  21. OSMouseButton button;
  22. bool pressed;
  23. };
  24. struct DoubleClick
  25. {
  26. Vector2I cursorPos;
  27. OSPositionalInputButtonStates btnStates;
  28. };
  29. public:
  30. OSInputHandler();
  31. virtual ~OSInputHandler();
  32. boost::signal<void(UINT32)> onCharInput;
  33. boost::signal<void(const Vector2I&, float)> onMouseWheelScrolled;
  34. boost::signal<void(const PositionalInputEvent&)> onCursorMoved;
  35. boost::signal<void(const PositionalInputEvent&)> onCursorPressed;
  36. boost::signal<void(const PositionalInputEvent&)> onCursorReleased;
  37. boost::signal<void(const PositionalInputEvent&)> onDoubleClick;
  38. boost::signal<void(InputCommandType)> onInputCommand;
  39. /**
  40. * @brief Called every frame by InputManager. Capture input here if needed.
  41. */
  42. virtual void update();
  43. /**
  44. * @brief Called by InputManager whenever window in focus changes.
  45. */
  46. virtual void inputWindowChanged(const RenderWindow& win) {}
  47. private:
  48. CM_MUTEX(mOSInputMutex);
  49. Vector2I mLastCursorPos;
  50. Vector2I mCursorPosition;
  51. float mMouseScroll;
  52. WString mInputString;
  53. Queue<ButtonStateChange>::type mButtonStates;
  54. Queue<DoubleClick>::type mDoubleClicks;
  55. Queue<InputCommandType>::type mInputCommands;
  56. OSPositionalInputButtonStates mMouseMoveBtnState;
  57. boost::signals::connection mCharInputConn;
  58. boost::signals::connection mCursorMovedConn;
  59. boost::signals::connection mCursorPressedConn;
  60. boost::signals::connection mCursorReleasedConn;
  61. boost::signals::connection mCursorDoubleClickConn;
  62. boost::signals::connection mInputCommandConn;
  63. boost::signals::connection mMouseWheelScrolledConn;
  64. /**
  65. * @brief Called from the message loop.
  66. */
  67. void charInput(UINT32 character);
  68. /**
  69. * @brief Called from the message loop.
  70. */
  71. void cursorMoved(const Vector2I& cursorPos, OSPositionalInputButtonStates& btnStates);
  72. /**
  73. * @brief Called from the message loop.
  74. */
  75. void cursorPressed(const Vector2I& cursorPos, OSMouseButton button, OSPositionalInputButtonStates& btnStates);
  76. /**
  77. * @brief Called from the message loop.
  78. */
  79. void cursorReleased(const Vector2I& cursorPos, OSMouseButton button, OSPositionalInputButtonStates& btnStates);
  80. /**
  81. * @brief Called from the message loop.
  82. */
  83. void cursorDoubleClick(const Vector2I& cursorPos, OSPositionalInputButtonStates& btnStates);
  84. /**
  85. * @brief Called from the message loop.
  86. */
  87. void inputCommandEntered(InputCommandType commandType);
  88. /**
  89. * @brief Called from the message loop.
  90. */
  91. void mouseWheelScrolled(float scrollPos);
  92. };
  93. }