BsInputHandlerOIS.h 3.6 KB

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