BsInputHandlerOIS.h 4.1 KB

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