BsInputHandlerOIS.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include "BsOISPrerequisites.h"
  3. #include "BsRawInputHandler.h"
  4. #include <OIS/OISEvents.h>
  5. #include <OIS/OISInputManager.h>
  6. #include <OIS/OISKeyboard.h>
  7. #include <OIS/OISMouse.h>
  8. #include <OIS/OISJoystick.h>
  9. namespace BansheeEngine
  10. {
  11. class InputHandlerOIS;
  12. /**
  13. * @brief Listens for events from a specific OIS joystick device.
  14. */
  15. class BS_OIS_EXPORT GamepadEventListener : public OIS::JoyStickListener
  16. {
  17. public:
  18. GamepadEventListener(InputHandlerOIS* parentHandler, UINT32 joystickIdx);
  19. /**
  20. * @brief Called by OIS whenever a gamepad/joystick button is pressed.
  21. */
  22. virtual bool buttonPressed(const OIS::JoyStickEvent& arg, int button) override;
  23. /**
  24. * @brief Called by OIS whenever a gamepad/joystick button is released.
  25. */
  26. virtual bool buttonReleased(const OIS::JoyStickEvent& arg, int button) override;
  27. /**
  28. * @brief Called by OIS whenever a gamepad/joystick axis is moved.
  29. */
  30. virtual bool axisMoved(const OIS::JoyStickEvent& arg, int axis) override;
  31. private:
  32. UINT32 mGamepadIdx;
  33. InputHandlerOIS* mParentHandler;
  34. };
  35. /**
  36. * @brief Raw input handler using OIS library for acquiring input.
  37. */
  38. class BS_OIS_EXPORT InputHandlerOIS : public RawInputHandler, public OIS::KeyListener,
  39. public OIS::MouseListener
  40. {
  41. /**
  42. * @brief Holding data about an active gamepad object.
  43. */
  44. struct GamepadData
  45. {
  46. OIS::JoyStick* gamepad;
  47. GamepadEventListener* listener;
  48. };
  49. public:
  50. InputHandlerOIS(unsigned int hWnd);
  51. virtual ~InputHandlerOIS();
  52. private:
  53. /**
  54. * @brief Called by OIS whenever a keyboard button is pressed.
  55. */
  56. virtual bool keyPressed(const OIS::KeyEvent& arg) override;
  57. /**
  58. * @brief Called by OIS whenever a keyboard button is released.
  59. */
  60. virtual bool keyReleased(const OIS::KeyEvent& arg) override;
  61. /**
  62. * @brief Called by OIS whenever mouse is moved.
  63. */
  64. virtual bool mouseMoved(const OIS::MouseEvent& arg) override;
  65. /**
  66. * @brief Called by OIS whenever is a mouse button pressed.
  67. */
  68. virtual bool mousePressed(const OIS::MouseEvent& arg, OIS::MouseButtonID id) override;
  69. /**
  70. * @brief Called by OIS whenever is a mouse button released
  71. */
  72. virtual bool mouseReleased(const OIS::MouseEvent& arg, OIS::MouseButtonID id) override;
  73. /**
  74. * @brief Called once per frame.
  75. *
  76. * @note Internal method.
  77. */
  78. virtual void _update() override;
  79. /**
  80. * @brief Called whenever the currently focused window changes.
  81. *
  82. * @note Internal method.
  83. */
  84. virtual void _inputWindowChanged(const RenderWindow& win) override;
  85. /**
  86. * @brief Converts an OIS key code into engine button code.
  87. */
  88. static ButtonCode keyCodeToButtonCode(OIS::KeyCode keyCode);
  89. /**
  90. * @brief Converts an OIS mouse button code into engine button code.
  91. */
  92. static ButtonCode mouseButtonToButtonCode(OIS::MouseButtonID mouseBtn);
  93. /**
  94. * @brief Converts an OIS gamepad button code into engine button code.
  95. */
  96. static ButtonCode gamepadButtonToButtonCode(INT32 joystickCode);
  97. private:
  98. friend class GamepadEventListener;
  99. /**
  100. * @brief Smooths the input mouse axis value. Smoothing makes the changes to
  101. * the axis more gradual depending on previous values.
  102. *
  103. * @param value Value to smooth.
  104. * @param idx Index of the mouse axis to smooth, 0 - horizontal, 1 - vertical.
  105. *
  106. * @returns Smoothed value.
  107. */
  108. float smoothMouse(float value, UINT32 idx);
  109. OIS::InputManager* mInputManager;
  110. OIS::Mouse* mMouse;
  111. OIS::Keyboard* mKeyboard;
  112. Vector<GamepadData> mGamepads;
  113. float mTotalMouseSamplingTime[2];
  114. UINT32 mTotalMouseNumSamples[2];
  115. float mMouseZeroTime[2];
  116. INT32 mMouseSampleAccumulator[2];
  117. float mMouseSmoothedAxis[2];
  118. UINT64 mLastMouseUpdateFrame;
  119. UINT64 mTimestampClockOffset;
  120. };
  121. }