BsInput.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsInput.h"
  5. #include "BsTime.h"
  6. #include "BsMath.h"
  7. #include "BsRectI.h"
  8. #include "BsDebug.h"
  9. #include "BsRenderWindowManager.h"
  10. using namespace std::placeholders;
  11. namespace BansheeEngine
  12. {
  13. const int Input::HISTORY_BUFFER_SIZE = 10; // Size of buffer used for input smoothing
  14. const float Input::WEIGHT_MODIFIER = 0.5f;
  15. Input::DeviceData::DeviceData()
  16. {
  17. for (int i = 0; i < BC_Count; i++)
  18. keyStates[i] = ButtonState::Off;
  19. }
  20. Input::Input()
  21. {
  22. mOSInputHandler = bs_shared_ptr<OSInputHandler>();
  23. mOSInputHandler->onCharInput.connect(std::bind(&Input::charInput, this, _1));
  24. mOSInputHandler->onCursorMoved.connect(std::bind(&Input::cursorMoved, this, _1));
  25. mOSInputHandler->onCursorPressed.connect(std::bind(&Input::cursorPressed, this, _1));
  26. mOSInputHandler->onCursorReleased.connect(std::bind(&Input::cursorReleased, this, _1));
  27. mOSInputHandler->onDoubleClick.connect(std::bind(&Input::cursorDoubleClick, this, _1));
  28. mOSInputHandler->onInputCommand.connect(std::bind(&Input::inputCommandEntered, this, _1));
  29. RenderWindowManager::instance().onFocusGained.connect(std::bind(&Input::inputWindowChanged, this, _1));
  30. }
  31. Input::~Input()
  32. { }
  33. void Input::_registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler)
  34. {
  35. if(mRawInputHandler != inputHandler)
  36. {
  37. mRawInputHandler = inputHandler;
  38. if(mRawInputHandler != nullptr)
  39. {
  40. mRawInputHandler->onButtonDown.connect(std::bind(&Input::buttonDown, this, _1, _2, _3));
  41. mRawInputHandler->onButtonUp.connect(std::bind(&Input::buttonUp, this, _1, _2, _3));
  42. mRawInputHandler->onAxisMoved.connect(std::bind(&Input::axisMoved, this, _1, _2, _3));
  43. }
  44. }
  45. }
  46. void Input::_update()
  47. {
  48. // Toggle states only remain active for a single frame before they are transitioned
  49. // into permanent state
  50. for (auto& deviceData : mDevices)
  51. {
  52. for (UINT32 i = 0; i < BC_Count; i++)
  53. {
  54. if (deviceData.keyStates[i] == ButtonState::ToggledOff)
  55. deviceData.keyStates[i] = ButtonState::Off;
  56. else if (deviceData.keyStates[i] == ButtonState::ToggledOn)
  57. deviceData.keyStates[i] = ButtonState::On;
  58. }
  59. }
  60. if(mRawInputHandler == nullptr)
  61. {
  62. LOGERR("Raw input handler not initialized!");
  63. return;
  64. }
  65. else
  66. mRawInputHandler->_update();
  67. if(mOSInputHandler == nullptr)
  68. {
  69. LOGERR("OS input handler not initialized!");
  70. return;
  71. }
  72. else
  73. mOSInputHandler->_update();
  74. // TODO - Perform smoothing
  75. }
  76. void Input::inputWindowChanged(RenderWindow& win)
  77. {
  78. if(mRawInputHandler != nullptr)
  79. mRawInputHandler->_inputWindowChanged(win);
  80. if(mOSInputHandler != nullptr)
  81. mOSInputHandler->_inputWindowChanged(win);
  82. }
  83. void Input::buttonDown(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp)
  84. {
  85. while (deviceIdx >= (UINT32)mDevices.size())
  86. mDevices.push_back(DeviceData());
  87. mDevices[deviceIdx].keyStates[code & 0x0000FFFF] = ButtonState::ToggledOn;
  88. if(!onButtonDown.empty())
  89. {
  90. ButtonEvent btnEvent;
  91. btnEvent.buttonCode = code;
  92. btnEvent.timestamp = timestamp;
  93. btnEvent.deviceIdx = deviceIdx;
  94. onButtonDown(btnEvent);
  95. }
  96. }
  97. void Input::buttonUp(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp)
  98. {
  99. while (deviceIdx >= (UINT32)mDevices.size())
  100. mDevices.push_back(DeviceData());
  101. mDevices[deviceIdx].keyStates[code & 0x0000FFFF] = ButtonState::ToggledOff;
  102. if(!onButtonUp.empty())
  103. {
  104. ButtonEvent btnEvent;
  105. btnEvent.buttonCode = code;
  106. btnEvent.timestamp = timestamp;
  107. btnEvent.deviceIdx = deviceIdx;
  108. onButtonUp(btnEvent);
  109. }
  110. }
  111. void Input::axisMoved(UINT32 deviceIdx, const RawAxisState& state, UINT32 axis)
  112. {
  113. while (deviceIdx >= (UINT32)mDevices.size())
  114. mDevices.push_back(DeviceData());
  115. Vector<RawAxisState>& axes = mDevices[deviceIdx].axes;
  116. while (axis >= (UINT32)axes.size())
  117. axes.push_back(RawAxisState());
  118. mDevices[deviceIdx].axes[axis] = state;
  119. }
  120. void Input::cursorMoved(const PointerEvent& event)
  121. {
  122. if(!onPointerMoved.empty())
  123. onPointerMoved(event);
  124. }
  125. void Input::cursorPressed(const PointerEvent& event)
  126. {
  127. if(!onPointerPressed.empty())
  128. onPointerPressed(event);
  129. }
  130. void Input::cursorReleased(const PointerEvent& event)
  131. {
  132. if(!onPointerReleased.empty())
  133. onPointerReleased(event);
  134. }
  135. void Input::cursorDoubleClick(const PointerEvent& event)
  136. {
  137. if(!onPointerDoubleClick.empty())
  138. onPointerDoubleClick(event);
  139. }
  140. void Input::inputCommandEntered(InputCommandType commandType)
  141. {
  142. if(!onInputCommand.empty())
  143. onInputCommand(commandType);
  144. }
  145. void Input::charInput(UINT32 chr)
  146. {
  147. if(!onCharInput.empty())
  148. {
  149. TextInputEvent textInputEvent;
  150. textInputEvent.textChar = chr;
  151. onCharInput(textInputEvent);
  152. }
  153. }
  154. float Input::getAxisValue(UINT32 type, UINT32 deviceIdx) const
  155. {
  156. if (deviceIdx >= (UINT32)mDevices.size())
  157. return 0.0f;
  158. const Vector<RawAxisState>& axes = mDevices[deviceIdx].axes;
  159. if (type >= (UINT32)axes.size())
  160. return 0.0f;
  161. return axes[type].abs;
  162. }
  163. bool Input::isButtonHeld(ButtonCode button, UINT32 deviceIdx) const
  164. {
  165. if (deviceIdx >= (UINT32)mDevices.size())
  166. return false;
  167. return mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::On ||
  168. mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::ToggledOn;
  169. }
  170. bool Input::isButtonUp(ButtonCode button, UINT32 deviceIdx) const
  171. {
  172. if (deviceIdx >= (UINT32)mDevices.size())
  173. return false;
  174. return mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::ToggledOff;
  175. }
  176. bool Input::isButtonDown(ButtonCode button, UINT32 deviceIdx) const
  177. {
  178. if (deviceIdx >= (UINT32)mDevices.size())
  179. return false;
  180. return mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::ToggledOn;
  181. }
  182. void Input::setMouseSmoothing(bool enable)
  183. {
  184. mRawInputHandler->setMouseSmoothing(enable);
  185. }
  186. Input& gInput()
  187. {
  188. return Input::instance();
  189. }
  190. }