BsInputHandlerOIS.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include "BsInputHandlerOIS.h"
  2. #include "BsVector2I.h"
  3. #include "OIS/OISException.h"
  4. #include "BsRenderWindow.h"
  5. #include "BsTime.h"
  6. #include "BsMath.h"
  7. #include "BsDebug.h"
  8. namespace BansheeEngine
  9. {
  10. const UINT32 InputHandlerOIS::MOUSE_DPI = 800;
  11. const float InputHandlerOIS::MOUSE_MAX = 0.05f;
  12. const float InputHandlerOIS::MOUSE_MAX_TIME = 0.020f; // 20 ms
  13. GamepadEventListener::GamepadEventListener(InputHandlerOIS* parentHandler, UINT32 joystickIdx)
  14. :mParentHandler(parentHandler), mGamepadIdx(joystickIdx)
  15. { }
  16. bool GamepadEventListener::buttonPressed(const OIS::JoyStickEvent& arg, int button)
  17. {
  18. ButtonCode bc = InputHandlerOIS::gamepadButtonToButtonCode(button);
  19. // Note: No timestamps for gamepad buttons, but they shouldn't be used for anything anyway
  20. mParentHandler->onButtonDown(mGamepadIdx, bc, 0);
  21. return true;
  22. }
  23. bool GamepadEventListener::buttonReleased(const OIS::JoyStickEvent& arg, int button)
  24. {
  25. ButtonCode bc = InputHandlerOIS::gamepadButtonToButtonCode(button);
  26. // Note: No timestamps for gamepad buttons, but they shouldn't be used for anything anyway
  27. mParentHandler->onButtonUp(mGamepadIdx, bc, 0);
  28. return true;
  29. }
  30. bool GamepadEventListener::axisMoved(const OIS::JoyStickEvent& arg, int axis)
  31. {
  32. // Move axis values into [-1.0f, 1.0f] range
  33. float axisRange = Math::abs((float)OIS::JoyStick::MAX_AXIS) + Math::abs((float)OIS::JoyStick::MIN_AXIS);
  34. INT32 axisRel = arg.state.mAxes[axis].rel;
  35. INT32 axisAbs = arg.state.mAxes[axis].abs;
  36. RawAxisState axisState;
  37. axisState.rel = ((axisRel + Math::abs((float)OIS::JoyStick::MIN_AXIS)) / axisRange) * 2.0f - 1.0f;
  38. axisState.abs = ((axisAbs + Math::abs((float)OIS::JoyStick::MIN_AXIS)) / axisRange) * 2.0f - 1.0f;
  39. mParentHandler->onAxisMoved(mGamepadIdx, axisState, (UINT32)axis);
  40. return true;
  41. }
  42. InputHandlerOIS::InputHandlerOIS(unsigned int hWnd)
  43. :mInputManager(nullptr), mKeyboard(nullptr), mMouse(nullptr), mTimestampClockOffset(0),
  44. mLastMouseUpdateFrame(0), mMouseSamplesObsolete(true)
  45. {
  46. mMouseSampleAccumulator[0] = 0;
  47. mMouseSampleAccumulator[1] = 0;
  48. mTotalMouseSamplingTime[0] = 1.0f / 125.0f; // Use 125Hz as initial pooling rate for mice
  49. mTotalMouseSamplingTime[1] = 1.0f / 125.0f;
  50. mTotalMouseNumSamples[0] = 1;
  51. mTotalMouseNumSamples[1] = 1;
  52. mMouseSmoothedAxis[0] = 0.0f;
  53. mMouseSmoothedAxis[1] = 0.0f;
  54. mMouseZeroTime[0] = 0.0f;
  55. mMouseZeroTime[1] = 0.0f;
  56. OIS::ParamList pl;
  57. std::ostringstream windowHndStr;
  58. windowHndStr << hWnd;
  59. pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
  60. #if defined BS_PLATFORM == BS_PLATFORM_WIN32
  61. pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" )));
  62. pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
  63. pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));
  64. pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
  65. #elif defined BS_PLATFORM == BS_PLATFORM_LINUX || BS_PLATFORM == BS_PLATFORM_APPLE
  66. pl.insert(std::make_pair(std::string("x11_mouse_grab"), std::string("false")));
  67. pl.insert(std::make_pair(std::string("x11_mouse_hide"), std::string("false")));
  68. pl.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false")));
  69. pl.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
  70. #endif
  71. try
  72. {
  73. mInputManager = OIS::InputManager::createInputSystem(pl);
  74. }
  75. catch(OIS::Exception &e)
  76. {
  77. std::cout << e.eText << std::endl;
  78. }
  79. if (mInputManager->getNumberOfDevices(OIS::OISKeyboard) > 0)
  80. {
  81. mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, true));
  82. mKeyboard->setEventCallback(this);
  83. }
  84. if (mInputManager->getNumberOfDevices(OIS::OISMouse) > 0)
  85. {
  86. mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, true));
  87. mMouse->setEventCallback(this);
  88. }
  89. UINT32 numGamepads = mInputManager->getNumberOfDevices(OIS::OISJoyStick);
  90. for (UINT32 i = 0; i < numGamepads; i++)
  91. {
  92. mGamepads.push_back(GamepadData());
  93. GamepadData& gamepadData = mGamepads.back();
  94. gamepadData.gamepad = static_cast<OIS::JoyStick*>(mInputManager->createInputObject(OIS::OISJoyStick, true));
  95. gamepadData.listener = bs_new<GamepadEventListener>(this, i);
  96. gamepadData.gamepad->setEventCallback(gamepadData.listener);
  97. }
  98. // OIS reports times since system start but we use time since program start
  99. mTimestampClockOffset = gTime().getStartTimeMs();
  100. }
  101. InputHandlerOIS::~InputHandlerOIS()
  102. {
  103. if(mInputManager)
  104. {
  105. if(mMouse != nullptr)
  106. mInputManager->destroyInputObject(mMouse);
  107. if(mKeyboard != nullptr)
  108. mInputManager->destroyInputObject(mKeyboard);
  109. for (auto& gamepadData : mGamepads)
  110. {
  111. mInputManager->destroyInputObject(gamepadData.gamepad);
  112. bs_delete(gamepadData.listener);
  113. }
  114. OIS::InputManager::destroyInputSystem(mInputManager);
  115. mInputManager = nullptr;
  116. }
  117. }
  118. float InputHandlerOIS::smoothMouse(float value, UINT32 idx)
  119. {
  120. UINT32 sampleCount = 1;
  121. float deltaTime = gTime().getFrameDelta();
  122. if (deltaTime < 0.25f)
  123. {
  124. float secondsPerSample = mTotalMouseSamplingTime[idx] / mTotalMouseNumSamples[idx];
  125. if (value == 0.0f)
  126. {
  127. mMouseZeroTime[idx] += deltaTime;
  128. if (mMouseZeroTime[idx] < secondsPerSample)
  129. value = mMouseSmoothedAxis[idx] * deltaTime / secondsPerSample;
  130. else
  131. mMouseSmoothedAxis[idx] = 0;
  132. }
  133. else
  134. {
  135. mMouseZeroTime[idx] = 0;
  136. if (mMouseSmoothedAxis[idx] != 0)
  137. {
  138. if (deltaTime < secondsPerSample * (sampleCount + 1))
  139. value = value * deltaTime / (secondsPerSample * sampleCount);
  140. else
  141. sampleCount = Math::roundToInt(deltaTime / secondsPerSample);
  142. }
  143. mMouseSmoothedAxis[idx] = value / sampleCount;
  144. }
  145. }
  146. else
  147. {
  148. mMouseSmoothedAxis[idx] = 0.0f;
  149. mMouseZeroTime[idx] = 0.0f;
  150. }
  151. return value;
  152. }
  153. void InputHandlerOIS::_update()
  154. {
  155. if (mMouse != nullptr)
  156. mMouse->capture();
  157. if (mKeyboard != nullptr)
  158. mKeyboard->capture();
  159. for (auto& gamepadData : mGamepads)
  160. {
  161. gamepadData.gamepad->capture();
  162. }
  163. float rawXValue = 0.0f;
  164. float rawYValue = 0.0f;
  165. // Smooth mouse axes if needed
  166. if (mMouseSmoothingEnabled)
  167. {
  168. rawXValue = smoothMouse((float)mMouseSampleAccumulator[0], 0);
  169. rawYValue = smoothMouse((float)mMouseSampleAccumulator[1], 1);
  170. }
  171. else
  172. {
  173. rawXValue = (float)mMouseSampleAccumulator[0];
  174. rawYValue = (float)mMouseSampleAccumulator[1];
  175. }
  176. mMouseSamplesObsolete = true;
  177. // Scale by time so that we are framerate independant: rawXValue = rawXValue * (MOUSE_MAX_TIME / gTime().getFrameDelta());
  178. // Scale to valid [-1.0, 1.0] range: rawXValue / (MOUSE_DPI * MOUSE_MAX)
  179. // This is just the combination of the two:
  180. float axisScale = ((MOUSE_DPI * MOUSE_MAX) / MOUSE_MAX_TIME) * gTime().getFrameDelta();
  181. RawAxisState xState;
  182. xState.rel = -Math::clamp(rawXValue / axisScale, -1.0f, 1.0f);
  183. xState.abs = xState.rel; // Abs value irrelevant for mouse
  184. onAxisMoved(0, xState, (UINT32)InputAxis::MouseX);
  185. RawAxisState yState;
  186. yState.rel = -Math::clamp(rawYValue / axisScale, -1.0f, 1.0f);
  187. yState.abs = yState.rel; // Abs value irrelevant for mouse
  188. onAxisMoved(0, yState, (UINT32)InputAxis::MouseY);
  189. }
  190. void InputHandlerOIS::_inputWindowChanged(const RenderWindow& win)
  191. {
  192. unsigned long long hWnd;
  193. win.getCustomAttribute("WINDOW", &hWnd);
  194. std::string normalString = toString((UINT64)hWnd).c_str();
  195. mKeyboard->setCaptureContext(normalString);
  196. mMouse->setCaptureContext(normalString);
  197. }
  198. bool InputHandlerOIS::keyPressed(const OIS::KeyEvent &arg)
  199. {
  200. onButtonDown(0, keyCodeToButtonCode(arg.key), arg.timestamp - mTimestampClockOffset);
  201. return true;
  202. }
  203. bool InputHandlerOIS::keyReleased(const OIS::KeyEvent& arg)
  204. {
  205. onButtonUp(0, keyCodeToButtonCode(arg.key), arg.timestamp - mTimestampClockOffset);
  206. return true;
  207. }
  208. bool InputHandlerOIS::mouseMoved(const OIS::MouseEvent& arg)
  209. {
  210. if (mMouseSamplesObsolete)
  211. {
  212. mMouseSampleAccumulator[0] = 0;
  213. mMouseSampleAccumulator[1] = 0;
  214. mMouseSamplesObsolete = false;
  215. }
  216. mMouseSampleAccumulator[0] += arg.state.X.rel;
  217. mMouseSampleAccumulator[1] += arg.state.Y.rel;
  218. mTotalMouseNumSamples[0] += Math::roundToInt(Math::abs((float)arg.state.X.rel));
  219. mTotalMouseNumSamples[1] += Math::roundToInt(Math::abs((float)arg.state.Y.rel));
  220. // Update sample times used for determining sampling rate. But only if something was
  221. // actually sampled, and only if this isn't the first non-zero sample.
  222. if (mLastMouseUpdateFrame != gTime().getCurrentFrameNumber())
  223. {
  224. if (arg.state.X.rel != 0 && !Math::approxEquals(mMouseSmoothedAxis[0], 0.0f))
  225. mTotalMouseSamplingTime[0] += gTime().getFrameDelta();
  226. if (arg.state.Y.rel != 0 && !Math::approxEquals(mMouseSmoothedAxis[1], 0.0f))
  227. mTotalMouseSamplingTime[1] += gTime().getFrameDelta();
  228. mLastMouseUpdateFrame = gTime().getCurrentFrameNumber();
  229. }
  230. RawAxisState zState;
  231. zState.abs = (float)arg.state.Z.abs;
  232. zState.rel = (float)arg.state.Z.rel;
  233. onAxisMoved(0, zState, (UINT32)InputAxis::MouseZ);
  234. return true;
  235. }
  236. bool InputHandlerOIS::mousePressed(const OIS::MouseEvent& arg, OIS::MouseButtonID id)
  237. {
  238. onButtonDown(0, mouseButtonToButtonCode(id), arg.timestamp - mTimestampClockOffset);
  239. return true;
  240. }
  241. bool InputHandlerOIS::mouseReleased(const OIS::MouseEvent& arg, OIS::MouseButtonID id)
  242. {
  243. onButtonUp(0, mouseButtonToButtonCode(id), arg.timestamp - mTimestampClockOffset);
  244. return true;
  245. }
  246. ButtonCode InputHandlerOIS::keyCodeToButtonCode(OIS::KeyCode keyCode)
  247. {
  248. return (ButtonCode)keyCode;
  249. }
  250. ButtonCode InputHandlerOIS::mouseButtonToButtonCode(OIS::MouseButtonID mouseBtn)
  251. {
  252. return (ButtonCode)(((int)mouseBtn + BC_NumKeys) | 0x80000000);
  253. }
  254. ButtonCode InputHandlerOIS::gamepadButtonToButtonCode(INT32 joystickCode)
  255. {
  256. switch (joystickCode)
  257. {
  258. case 0:
  259. return BC_GAMEPAD_DPAD_UP;
  260. case 1:
  261. return BC_GAMEPAD_DPAD_DOWN;
  262. case 2:
  263. return BC_GAMEPAD_DPAD_LEFT;
  264. case 3:
  265. return BC_GAMEPAD_DPAD_RIGHT;
  266. case 4:
  267. return BC_GAMEPAD_START;
  268. case 5:
  269. return BC_GAMEPAD_BACK;
  270. case 6:
  271. return BC_GAMEPAD_LS;
  272. case 7:
  273. return BC_GAMEPAD_RS;
  274. case 8:
  275. return BC_GAMEPAD_LB;
  276. case 9:
  277. return BC_GAMEPAD_RB;
  278. case 10:
  279. return BC_GAMEPAD_BTN1;
  280. case 11:
  281. return BC_GAMEPAD_LS;
  282. case 12:
  283. return BC_GAMEPAD_A;
  284. case 13:
  285. return BC_GAMEPAD_B;
  286. case 14:
  287. return BC_GAMEPAD_X;
  288. case 15:
  289. return BC_GAMEPAD_Y;
  290. }
  291. return (ButtonCode)(BC_GAMEPAD_BTN1 + (joystickCode - 15));
  292. }
  293. }