CmInput.h 3.4 KB

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