CmInput.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "CmRectI.h"
  5. #include "CmOSInputHandler.h"
  6. #include "CmRawInputHandler.h"
  7. #include "CmInputFwd.h"
  8. namespace CamelotFramework
  9. {
  10. class CM_EXPORT Input : public Module<Input>
  11. {
  12. enum class ButtonState
  13. {
  14. Off,
  15. On,
  16. ToggledOn,
  17. ToggledOff
  18. };
  19. public:
  20. Input();
  21. ~Input();
  22. boost::signal<void(const ButtonEvent&)> onButtonDown;
  23. boost::signal<void(const ButtonEvent&)> onButtonUp;
  24. boost::signal<void(const TextInputEvent&)> onCharInput;
  25. boost::signal<void(const PositionalInputEvent&)> onCursorMoved;
  26. boost::signal<void(const PositionalInputEvent&)> onCursorPressed;
  27. boost::signal<void(const PositionalInputEvent&)> onCursorReleased;
  28. boost::signal<void(const PositionalInputEvent&)> onDoubleClick;
  29. boost::signal<void(InputCommandType)> onInputCommand;
  30. void registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler);
  31. /**
  32. * @brief Called every frame. Dispatches any callbacks resulting from input by the user. Should only be called by Application.
  33. */
  34. void update();
  35. /**
  36. * @brief Returns smoothed mouse/joystick input in the horizontal axis.
  37. *
  38. * @return The horizontal axis value ranging [-1.0f, 1.0f].
  39. */
  40. float getHorizontalAxis() const;
  41. /**
  42. * @brief Returns smoothed mouse/joystick input in the vertical axis.
  43. *
  44. * @return The vertical axis value ranging [-1.0f, 1.0f].
  45. */
  46. float getVerticalAxis() const;
  47. bool isButtonHeld(ButtonCode keyCode) const;
  48. bool isButtonUp(ButtonCode keyCode) const;
  49. bool isButtonDown(ButtonCode keyCode) const;
  50. Vector2I getCursorPosition() const { return mMouseAbsPos; }
  51. private:
  52. std::shared_ptr<RawInputHandler> mRawInputHandler;
  53. std::shared_ptr<OSInputHandler> mOSInputHandler;
  54. float mSmoothHorizontalAxis;
  55. float mSmoothVerticalAxis;
  56. float* mHorizontalHistoryBuffer;
  57. float* mVerticalHistoryBuffer;
  58. float* mTimesHistoryBuffer;
  59. int mCurrentBufferIdx;
  60. Vector2I mMouseLastRel;
  61. Vector2I mMouseAbsPos;
  62. RawAxisState mAxes[RawInputAxis::Count];
  63. ButtonState mKeyState[BC_Count];
  64. void buttonDown(ButtonCode code, UINT64 timestamp);
  65. void buttonUp(ButtonCode code, UINT64 timestamp);
  66. void charInput(UINT32 chr);
  67. /**
  68. * @brief Raw mouse/joystick axis input.
  69. */
  70. void axisMoved(const RawAxisState& state, RawInputAxis axis);
  71. /**
  72. * @brief Cursor movement as OS reports it. Used for screen cursor position.
  73. */
  74. void cursorMoved(const PositionalInputEvent& event);
  75. /**
  76. * @brief Cursor button presses as OS reports it.
  77. */
  78. void cursorPressed(const PositionalInputEvent& event);
  79. /**
  80. * @brief Cursor button releases as OS reports it.
  81. */
  82. void cursorReleased(const PositionalInputEvent& event);
  83. /**
  84. * @brief Cursor button releases as OS reports it.
  85. */
  86. void cursorDoubleClick(const PositionalInputEvent& event);
  87. /**
  88. * @brief Input commands as OS reports them.
  89. */
  90. void inputCommandEntered(InputCommandType commandType);
  91. /**
  92. * @brief Updates the axis input values that need smoothing.
  93. */
  94. void updateSmoothInput();
  95. /**
  96. * @brief Called when window in focus changes, as reported by the OS.
  97. */
  98. void inputWindowChanged(RenderWindow& win);
  99. /************************************************************************/
  100. /* STATICS */
  101. /************************************************************************/
  102. static const int HISTORY_BUFFER_SIZE; // Size of buffer used for input smoothing
  103. static const float WEIGHT_MODIFIER;
  104. };
  105. CM_EXPORT Input& gInput();
  106. }