BsInput.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. if(mRawInputHandler == nullptr)
  59. {
  60. LOGERR("Raw input handler not initialized!");
  61. return;
  62. }
  63. else
  64. mRawInputHandler->_update();
  65. if(mOSInputHandler == nullptr)
  66. {
  67. LOGERR("OS input handler not initialized!");
  68. return;
  69. }
  70. else
  71. mOSInputHandler->_update();
  72. }
  73. void Input::inputWindowChanged(RenderWindow& win)
  74. {
  75. if(mRawInputHandler != nullptr)
  76. mRawInputHandler->_inputWindowChanged(win);
  77. if(mOSInputHandler != nullptr)
  78. mOSInputHandler->_inputWindowChanged(win);
  79. }
  80. void Input::buttonDown(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp)
  81. {
  82. while (deviceIdx >= (UINT32)mDevices.size())
  83. mDevices.push_back(DeviceData());
  84. mDevices[deviceIdx].keyStates[code & 0x0000FFFF] = ButtonState::ToggledOn;
  85. if(!onButtonDown.empty())
  86. {
  87. ButtonEvent btnEvent;
  88. btnEvent.buttonCode = code;
  89. btnEvent.timestamp = timestamp;
  90. btnEvent.deviceIdx = deviceIdx;
  91. onButtonDown(btnEvent);
  92. }
  93. }
  94. void Input::buttonUp(UINT32 deviceIdx, ButtonCode code, UINT64 timestamp)
  95. {
  96. while (deviceIdx >= (UINT32)mDevices.size())
  97. mDevices.push_back(DeviceData());
  98. mDevices[deviceIdx].keyStates[code & 0x0000FFFF] = ButtonState::ToggledOff;
  99. if(!onButtonUp.empty())
  100. {
  101. ButtonEvent btnEvent;
  102. btnEvent.buttonCode = code;
  103. btnEvent.timestamp = timestamp;
  104. btnEvent.deviceIdx = deviceIdx;
  105. onButtonUp(btnEvent);
  106. }
  107. }
  108. void Input::axisMoved(UINT32 deviceIdx, const RawAxisState& state, UINT32 axis)
  109. {
  110. while (deviceIdx >= (UINT32)mDevices.size())
  111. mDevices.push_back(DeviceData());
  112. Vector<RawAxisState>& axes = mDevices[deviceIdx].axes;
  113. while (axis >= (UINT32)axes.size())
  114. axes.push_back(RawAxisState());
  115. mDevices[deviceIdx].axes[axis] = state;
  116. }
  117. void Input::cursorMoved(const PointerEvent& event)
  118. {
  119. if(!onPointerMoved.empty())
  120. onPointerMoved(event);
  121. if (mLastPositionSet)
  122. mPointerDelta = event.screenPos - mPointerPosition;
  123. mPointerPosition = event.screenPos;
  124. mLastPositionSet = true;
  125. }
  126. void Input::cursorPressed(const PointerEvent& event)
  127. {
  128. if(!onPointerPressed.empty())
  129. onPointerPressed(event);
  130. }
  131. void Input::cursorReleased(const PointerEvent& event)
  132. {
  133. if(!onPointerReleased.empty())
  134. onPointerReleased(event);
  135. }
  136. void Input::cursorDoubleClick(const PointerEvent& event)
  137. {
  138. if(!onPointerDoubleClick.empty())
  139. onPointerDoubleClick(event);
  140. }
  141. void Input::inputCommandEntered(InputCommandType commandType)
  142. {
  143. if(!onInputCommand.empty())
  144. onInputCommand(commandType);
  145. }
  146. void Input::charInput(UINT32 chr)
  147. {
  148. if(!onCharInput.empty())
  149. {
  150. TextInputEvent textInputEvent;
  151. textInputEvent.textChar = chr;
  152. onCharInput(textInputEvent);
  153. }
  154. }
  155. float Input::getAxisValue(UINT32 type, UINT32 deviceIdx) const
  156. {
  157. if (deviceIdx >= (UINT32)mDevices.size())
  158. return 0.0f;
  159. const Vector<RawAxisState>& axes = mDevices[deviceIdx].axes;
  160. if (type >= (UINT32)axes.size())
  161. return 0.0f;
  162. return axes[type].abs;
  163. }
  164. bool Input::isButtonHeld(ButtonCode button, UINT32 deviceIdx) const
  165. {
  166. if (deviceIdx >= (UINT32)mDevices.size())
  167. return false;
  168. return mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::On ||
  169. mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::ToggledOn;
  170. }
  171. bool Input::isButtonUp(ButtonCode button, UINT32 deviceIdx) const
  172. {
  173. if (deviceIdx >= (UINT32)mDevices.size())
  174. return false;
  175. return mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::ToggledOff;
  176. }
  177. bool Input::isButtonDown(ButtonCode button, UINT32 deviceIdx) const
  178. {
  179. if (deviceIdx >= (UINT32)mDevices.size())
  180. return false;
  181. return mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::ToggledOn;
  182. }
  183. Vector2I Input::getPointerPosition() const
  184. {
  185. return mPointerPosition;
  186. }
  187. void Input::setMouseSmoothing(bool enable)
  188. {
  189. mRawInputHandler->setMouseSmoothing(enable);
  190. }
  191. Input& gInput()
  192. {
  193. return Input::instance();
  194. }
  195. }