BsInput.cpp 6.0 KB

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