2
0

BsInputHandlerOIS.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsOISPrerequisites.h"
  6. #include "BsRawInputHandler.h"
  7. #include <OIS/OISEvents.h>
  8. #include <OIS/OISInputManager.h>
  9. #include <OIS/OISKeyboard.h>
  10. #include <OIS/OISMouse.h>
  11. #include <OIS/OISJoystick.h>
  12. namespace BansheeEngine
  13. {
  14. class InputHandlerOIS;
  15. /**
  16. * @brief Listens for events from a specific OIS joystick device.
  17. */
  18. class BS_OIS_EXPORT GamepadEventListener : public OIS::JoyStickListener
  19. {
  20. public:
  21. GamepadEventListener(InputHandlerOIS* parentHandler, UINT32 joystickIdx);
  22. /**
  23. * @brief Called by OIS whenever a gamepad/joystick button is pressed.
  24. */
  25. virtual bool buttonPressed(const OIS::JoyStickEvent& arg, int button);
  26. /**
  27. * @brief Called by OIS whenever a gamepad/joystick button is released.
  28. */
  29. virtual bool buttonReleased(const OIS::JoyStickEvent& arg, int button);
  30. /**
  31. * @brief Called by OIS whenever a gamepad/joystick axis is moved.
  32. */
  33. virtual bool axisMoved(const OIS::JoyStickEvent& arg, int axis);
  34. private:
  35. UINT32 mGamepadIdx;
  36. InputHandlerOIS* mParentHandler;
  37. };
  38. /**
  39. * @brief Raw input handler using OIS library for acquiring input.
  40. */
  41. class BS_OIS_EXPORT InputHandlerOIS : public RawInputHandler, public OIS::KeyListener,
  42. public OIS::MouseListener
  43. {
  44. /**
  45. * @brief Holding data about an active gamepad object.
  46. */
  47. struct GamepadData
  48. {
  49. OIS::JoyStick* gamepad;
  50. GamepadEventListener* listener;
  51. };
  52. public:
  53. InputHandlerOIS(unsigned int hWnd);
  54. virtual ~InputHandlerOIS();
  55. private:
  56. /**
  57. * @brief Called by OIS whenever a keyboard button is pressed.
  58. */
  59. virtual bool keyPressed(const OIS::KeyEvent& arg);
  60. /**
  61. * @brief Called by OIS whenever a keyboard button is released.
  62. */
  63. virtual bool keyReleased(const OIS::KeyEvent& arg);
  64. /**
  65. * @brief Called by OIS whenever mouse is moved.
  66. */
  67. virtual bool mouseMoved(const OIS::MouseEvent& arg);
  68. /**
  69. * @brief Called by OIS whenever is a mouse button pressed.
  70. */
  71. virtual bool mousePressed(const OIS::MouseEvent& arg, OIS::MouseButtonID id);
  72. /**
  73. * @brief Called by OIS whenever is a mouse button released
  74. */
  75. virtual bool mouseReleased(const OIS::MouseEvent& arg, OIS::MouseButtonID id);
  76. /**
  77. * @brief Called once per frame.
  78. *
  79. * @note Internal method.
  80. */
  81. virtual void _update();
  82. /**
  83. * @brief Called whenever the currently focused window changes.
  84. *
  85. * @note Internal method.
  86. */
  87. virtual void _inputWindowChanged(const RenderWindow& win);
  88. /**
  89. * @brief Converts an OIS key code into engine button code.
  90. */
  91. static ButtonCode keyCodeToButtonCode(OIS::KeyCode keyCode);
  92. /**
  93. * @brief Converts an OIS mouse button code into engine button code.
  94. */
  95. static ButtonCode mouseButtonToButtonCode(OIS::MouseButtonID mouseBtn);
  96. /**
  97. * @brief Converts an OIS gamepad button code into engine button code.
  98. */
  99. static ButtonCode gamepadButtonToButtonCode(INT32 joystickCode);
  100. private:
  101. friend class GamepadEventListener;
  102. /**
  103. * @brief Smooths the input mouse axis value. Smoothing makes the changes to
  104. * the axis more gradual depending on previous values.
  105. *
  106. * @param value Value to smooth.
  107. * @param idx Index of the mouse axis to smooth, 0 - horizontal, 1 - vertical.
  108. *
  109. * @returns Smoothed value.
  110. */
  111. float smoothMouse(float value, UINT32 idx);
  112. /**
  113. * @brief Default dots per inch reported by the mouse.
  114. *
  115. * @note This should be retrieved from the mouse driver but I am not aware of any decent
  116. * way of doing it. What this means is that if the user has a mouse with a different
  117. * DPI then he will need to adjust sensitivity.
  118. */
  119. static const UINT32 MOUSE_DPI;
  120. /**
  121. * @brief How much does the user need to move the mouse in order to max out the mouse axis
  122. * (either positive or negative), in inches.
  123. */
  124. static const float MOUSE_MAX;
  125. /**
  126. * @brief Number of seconds the mouse needs to reach the MOUSE_MAX value in, in order to
  127. * max out the axis.
  128. */
  129. static const float MOUSE_MAX_TIME;
  130. /**
  131. * @brief Minimum number of milliseconds that need to pass before mouse axes are updated again.
  132. *
  133. * @note At extremely high frame rates sampling the mouse too often will introduce jitter. This is because
  134. * mouse will be sampled by the application faster than the hardware reports the samples. This means some
  135. * of the samples will be reported as 0, while in truth mouse could be moving but it just hasn't been sampled.
  136. * So we cannot tell if mouse is actually still, or moving but sample hasn't been updated, and must assume mouse
  137. * is still, which causes jitter as one frame reports mouse as moving and another as still.
  138. *
  139. * We could get around this if we knew the exact hardware sampling rate, but we don't.
  140. */
  141. static const float MOUSE_MAX_SAMPLING_RATE;
  142. OIS::InputManager* mInputManager;
  143. OIS::Mouse* mMouse;
  144. OIS::Keyboard* mKeyboard;
  145. Vector<GamepadData> mGamepads;
  146. float mTotalMouseSamplingTime[2];
  147. UINT32 mTotalMouseNumSamples[2];
  148. float mMouseZeroTime[2];
  149. INT32 mMouseSampleAccumulator[2];
  150. float mMouseSmoothedAxis[2];
  151. UINT32 mLastMouseUpdateFrame;
  152. float mMouseSampleCounter;
  153. UINT64 mTimestampClockOffset;
  154. };
  155. }