CmInput.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "CmRect.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&)> onKeyDown;
  16. boost::signal<void(const ButtonEvent&)> onKeyUp;
  17. boost::signal<void(const TextInputEvent&)> onCharInput;
  18. boost::signal<void(const MouseEvent&)> onMouseMoved;
  19. void registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler);
  20. /**
  21. * @brief Called every frame. Dispatches any callbacks resulting from input by the user. Should only be called by Application.
  22. */
  23. void update();
  24. /**
  25. * @brief Returns smoothed mouse/joystick input in the horizontal axis.
  26. *
  27. * @return The horizontal axis value ranging [-1.0f, 1.0f].
  28. */
  29. float getHorizontalAxis() const;
  30. /**
  31. * @brief Returns smoothed mouse/joystick input in the vertical axis.
  32. *
  33. * @return The vertical axis value ranging [-1.0f, 1.0f].
  34. */
  35. float getVerticalAxis() const;
  36. bool isButtonDown(ButtonCode keyCode) const;
  37. Int2 getMousePosition() const { return mMouseAbsPos; }
  38. private:
  39. std::shared_ptr<RawInputHandler> mRawInputHandler;
  40. std::shared_ptr<OSInputHandler> mOSInputHandler;
  41. float mSmoothHorizontalAxis;
  42. float mSmoothVerticalAxis;
  43. float* mHorizontalHistoryBuffer;
  44. float* mVerticalHistoryBuffer;
  45. float* mTimesHistoryBuffer;
  46. int mCurrentBufferIdx;
  47. Int2 mMouseLastRel;
  48. Int2 mMouseAbsPos;
  49. RawAxisState mAxes[RawInputAxis::Count];
  50. bool mKeyState[BC_Count];
  51. void buttonDown(ButtonCode code);
  52. void buttonUp(ButtonCode code);
  53. void charInput(UINT32 chr);
  54. /**
  55. * @brief Raw mouse/joystick axis input.
  56. */
  57. void axisMoved(const RawAxisState& state, RawInputAxis axis);
  58. /**
  59. * @brief Mouse movement as OS reports it. Used for screen cursor position.
  60. */
  61. void mouseMoved(const Int2& screenPos);
  62. /**
  63. * @brief Updates the axis input values that need smoothing.
  64. */
  65. void updateSmoothInput();
  66. /**
  67. * @brief Called when window in focus changes, as reported by the OS.
  68. */
  69. void inputWindowChanged(RenderWindow& win);
  70. /************************************************************************/
  71. /* STATICS */
  72. /************************************************************************/
  73. static const int HISTORY_BUFFER_SIZE; // Size of buffer used for input smoothing
  74. static const float WEIGHT_MODIFIER;
  75. };
  76. CM_EXPORT Input& gInput();
  77. }