BsInput.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "BsInput.h"
  2. #include "BsTime.h"
  3. #include "BsMath.h"
  4. #include "BsRectI.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. {
  19. mOSInputHandler = bs_shared_ptr<OSInputHandler>();
  20. mOSInputHandler->onCharInput.connect(std::bind(&Input::charInput, this, _1));
  21. mOSInputHandler->onCursorMoved.connect(std::bind(&Input::cursorMoved, this, _1));
  22. mOSInputHandler->onCursorPressed.connect(std::bind(&Input::cursorPressed, this, _1));
  23. mOSInputHandler->onCursorReleased.connect(std::bind(&Input::cursorReleased, this, _1));
  24. mOSInputHandler->onDoubleClick.connect(std::bind(&Input::cursorDoubleClick, this, _1));
  25. mOSInputHandler->onInputCommand.connect(std::bind(&Input::inputCommandEntered, this, _1));
  26. RenderWindowManager::instance().onFocusGained.connect(std::bind(&Input::inputWindowChanged, this, _1));
  27. }
  28. Input::~Input()
  29. { }
  30. void Input::_registerRawInputHandler(std::shared_ptr<RawInputHandler> inputHandler)
  31. {
  32. if(mRawInputHandler != inputHandler)
  33. {
  34. mRawInputHandler = inputHandler;
  35. if(mRawInputHandler != nullptr)
  36. {
  37. mRawInputHandler->onButtonDown.connect(std::bind(&Input::buttonDown, this, _1, _2, _3));
  38. mRawInputHandler->onButtonUp.connect(std::bind(&Input::buttonUp, this, _1, _2, _3));
  39. mRawInputHandler->onAxisMoved.connect(std::bind(&Input::axisMoved, this, _1, _2, _3));
  40. }
  41. }
  42. }
  43. void Input::_update()
  44. {
  45. // Toggle states only remain active for a single frame before they are transitioned
  46. // into permanent state
  47. for (auto& deviceData : mDevices)
  48. {
  49. for (UINT32 i = 0; i < BC_Count; i++)
  50. {
  51. if (deviceData.keyStates[i] == ButtonState::ToggledOff)
  52. deviceData.keyStates[i] = ButtonState::Off;
  53. else if (deviceData.keyStates[i] == ButtonState::ToggledOn)
  54. deviceData.keyStates[i] = ButtonState::On;
  55. }
  56. }
  57. if(mRawInputHandler == nullptr)
  58. {
  59. LOGERR("Raw input handler not initialized!");
  60. return;
  61. }
  62. else
  63. mRawInputHandler->_update();
  64. if(mOSInputHandler == nullptr)
  65. {
  66. LOGERR("OS input handler not initialized!");
  67. return;
  68. }
  69. else
  70. mOSInputHandler->_update();
  71. // TODO - Perform smoothing
  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. }
  122. void Input::cursorPressed(const PointerEvent& event)
  123. {
  124. if(!onPointerPressed.empty())
  125. onPointerPressed(event);
  126. }
  127. void Input::cursorReleased(const PointerEvent& event)
  128. {
  129. if(!onPointerReleased.empty())
  130. onPointerReleased(event);
  131. }
  132. void Input::cursorDoubleClick(const PointerEvent& event)
  133. {
  134. if(!onPointerDoubleClick.empty())
  135. onPointerDoubleClick(event);
  136. }
  137. void Input::inputCommandEntered(InputCommandType commandType)
  138. {
  139. if(!onInputCommand.empty())
  140. onInputCommand(commandType);
  141. }
  142. void Input::charInput(UINT32 chr)
  143. {
  144. if(!onCharInput.empty())
  145. {
  146. TextInputEvent textInputEvent;
  147. textInputEvent.textChar = chr;
  148. onCharInput(textInputEvent);
  149. }
  150. }
  151. float Input::getAxisValue(UINT32 type, UINT32 deviceIdx, bool smooth) const
  152. {
  153. if (deviceIdx >= (UINT32)mDevices.size())
  154. return 0.0f;
  155. // TODO - Smooth parameter is ignored
  156. const Vector<RawAxisState>& axes = mDevices[deviceIdx].axes;
  157. if (type >= (UINT32)axes.size())
  158. return 0.0f;
  159. return axes[type].abs;
  160. }
  161. bool Input::isButtonHeld(ButtonCode button, UINT32 deviceIdx) const
  162. {
  163. if (deviceIdx >= (UINT32)mDevices.size())
  164. return false;
  165. return mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::On ||
  166. mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::ToggledOn;
  167. }
  168. bool Input::isButtonUp(ButtonCode button, UINT32 deviceIdx) const
  169. {
  170. if (deviceIdx >= (UINT32)mDevices.size())
  171. return false;
  172. return mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::ToggledOff;
  173. }
  174. bool Input::isButtonDown(ButtonCode button, UINT32 deviceIdx) const
  175. {
  176. if (deviceIdx >= (UINT32)mDevices.size())
  177. return false;
  178. return mDevices[deviceIdx].keyStates[button & 0x0000FFFF] == ButtonState::ToggledOn;
  179. }
  180. Input& gInput()
  181. {
  182. return Input::instance();
  183. }
  184. }