CmInput.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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&)> onButtonDown;
  16. boost::signal<void(const ButtonEvent&)> onButtonUp;
  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. // Thread safe. Will only be processed on next "update".
  39. void simulateButtonDown(ButtonCode code);
  40. // Thread safe. Will only be processed on next "update".
  41. void simulateButtonUp(ButtonCode code);
  42. private:
  43. std::shared_ptr<RawInputHandler> mRawInputHandler;
  44. std::shared_ptr<OSInputHandler> mOSInputHandler;
  45. CM_MUTEX(mSimulatedInputMutex);
  46. Vector<ButtonCode>::type mSimulatedButtonUp;
  47. Vector<ButtonCode>::type mSimulatedButtonDown;
  48. float mSmoothHorizontalAxis;
  49. float mSmoothVerticalAxis;
  50. float* mHorizontalHistoryBuffer;
  51. float* mVerticalHistoryBuffer;
  52. float* mTimesHistoryBuffer;
  53. int mCurrentBufferIdx;
  54. Int2 mMouseLastRel;
  55. Int2 mMouseAbsPos;
  56. RawAxisState mAxes[RawInputAxis::Count];
  57. bool mKeyState[BC_Count];
  58. void buttonDown(ButtonCode code);
  59. void buttonUp(ButtonCode code);
  60. void charInput(UINT32 chr);
  61. /**
  62. * @brief Raw mouse/joystick axis input.
  63. */
  64. void axisMoved(const RawAxisState& state, RawInputAxis axis);
  65. /**
  66. * @brief Mouse movement as OS reports it. Used for screen cursor position.
  67. */
  68. void mouseMoved(const Int2& screenPos, float mouseWheelScrollAmount);
  69. /**
  70. * @brief Updates the axis input values that need smoothing.
  71. */
  72. void updateSmoothInput();
  73. /**
  74. * @brief Called when window in focus changes, as reported by the OS.
  75. */
  76. void inputWindowChanged(RenderWindow& win);
  77. /************************************************************************/
  78. /* STATICS */
  79. /************************************************************************/
  80. static const int HISTORY_BUFFER_SIZE; // Size of buffer used for input smoothing
  81. static const float WEIGHT_MODIFIER;
  82. };
  83. CM_EXPORT Input& gInput();
  84. }