CmInput.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "CmRect.h"
  5. #include "CmInputHandler.h"
  6. namespace CamelotFramework
  7. {
  8. class CM_EXPORT Input : public Module<Input>
  9. {
  10. public:
  11. Input();
  12. ~Input();
  13. boost::signal<void(const KeyEvent&)> onKeyDown;
  14. boost::signal<void(const KeyEvent&)> onKeyUp;
  15. boost::signal<void(const MouseEvent&)> onMouseMoved;
  16. boost::signal<void(const MouseEvent&, MouseButton)> onMouseDown;
  17. boost::signal<void(const MouseEvent&, MouseButton)> onMouseUp;
  18. void initClipRect(Rect& clipRect);
  19. void registerInputHandler(InputHandlerPtr inputHandler);
  20. /**
  21. * @brief Called every frame. 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(MouseButton button) const;
  37. bool isKeyDown(KeyCode keyCode) const;
  38. private:
  39. InputHandlerPtr mInputHandler;
  40. float mSmoothHorizontalAxis;
  41. float mSmoothVerticalAxis;
  42. float* mHorizontalHistoryBuffer;
  43. float* mVerticalHistoryBuffer;
  44. float* mTimesHistoryBuffer;
  45. int mCurrentBufferIdx;
  46. Int2 mMouseLastRel;
  47. Rect mClipRect;
  48. bool mUsingClipRect;
  49. bool mMouseButtonState[MB_Count];
  50. bool mKeyState[KC_Count];
  51. void keyDown(const KeyEvent& event);
  52. void keyUp(const KeyEvent& event);
  53. void mouseMoved(const MouseEvent& event);
  54. void mouseDown(const MouseEvent& event, MouseButton buttonID);
  55. void mouseUp(const MouseEvent& event, MouseButton buttonID);
  56. /**
  57. * @brief Updates the input values that need smoothing.
  58. */
  59. void updateSmoothInput();
  60. /************************************************************************/
  61. /* STATICS */
  62. /************************************************************************/
  63. static const int HISTORY_BUFFER_SIZE; // Size of buffer used for input smoothing
  64. static const float WEIGHT_MODIFIER;
  65. };
  66. CM_EXPORT Input& gInput();
  67. }