BsVirtualInput.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "BsVirtualInput.h"
  2. #include "BsInput.h"
  3. #include "BsMath.h"
  4. #include "BsTime.h"
  5. using namespace std::placeholders;
  6. namespace BansheeEngine
  7. {
  8. VirtualInput::VirtualInput()
  9. :mActiveModifiers((UINT32)ButtonModifier::None)
  10. {
  11. mInputConfiguration = createConfiguration();
  12. Input::instance().onButtonDown.connect(std::bind(&VirtualInput::buttonDown, this, _1));
  13. Input::instance().onButtonUp.connect(std::bind(&VirtualInput::buttonUp, this, _1));
  14. }
  15. std::shared_ptr<InputConfiguration> VirtualInput::createConfiguration()
  16. {
  17. return bs_shared_ptr_new<InputConfiguration>();
  18. }
  19. void VirtualInput::setConfiguration(const std::shared_ptr<InputConfiguration>& input)
  20. {
  21. mInputConfiguration = input;
  22. // Note: Technically this is slightly wrong as it will
  23. // "forget" any buttons currently held down, but shouldn't matter much in practice.
  24. for (auto& deviceData : mDevices)
  25. deviceData.cachedStates.clear();
  26. }
  27. bool VirtualInput::isButtonDown(const VirtualButton& button, UINT32 deviceIdx) const
  28. {
  29. if (deviceIdx >= (UINT32)mDevices.size())
  30. return false;
  31. const Map<UINT32, ButtonData>& cachedStates = mDevices[deviceIdx].cachedStates;
  32. auto iterFind = cachedStates.find(button.buttonIdentifier);
  33. if (iterFind != cachedStates.end())
  34. return iterFind->second.state == ButtonState::ToggledOn;
  35. return false;
  36. }
  37. bool VirtualInput::isButtonUp(const VirtualButton& button, UINT32 deviceIdx) const
  38. {
  39. if (deviceIdx >= (UINT32)mDevices.size())
  40. return false;
  41. const Map<UINT32, ButtonData>& cachedStates = mDevices[deviceIdx].cachedStates;
  42. auto iterFind = cachedStates.find(button.buttonIdentifier);
  43. if (iterFind != cachedStates.end())
  44. return iterFind->second.state == ButtonState::ToggledOff;
  45. return false;
  46. }
  47. bool VirtualInput::isButtonHeld(const VirtualButton& button, UINT32 deviceIdx) const
  48. {
  49. if (deviceIdx >= (UINT32)mDevices.size())
  50. return false;
  51. const Map<UINT32, ButtonData>& cachedStates = mDevices[deviceIdx].cachedStates;
  52. auto iterFind = cachedStates.find(button.buttonIdentifier);
  53. if (iterFind != cachedStates.end())
  54. return iterFind->second.state == ButtonState::On || iterFind->second.state == ButtonState::ToggledOn;
  55. return false;
  56. }
  57. float VirtualInput::getAxisValue(const VirtualAxis& axis, UINT32 deviceIdx) const
  58. {
  59. VIRTUAL_AXIS_DESC axisDesc;
  60. if (mInputConfiguration->_getAxis(axis, axisDesc))
  61. {
  62. float axisValue = gInput().getAxisValue((UINT32)axisDesc.type, deviceIdx);
  63. if (axisDesc.deadZone > 0.0f)
  64. {
  65. // Scale to [-1, 1] range after removing the dead zone
  66. if (axisValue > 0)
  67. axisValue = std::max(0.f, axisValue - axisDesc.deadZone) / (1.0f - axisDesc.deadZone);
  68. else
  69. axisValue = -std::max(0.f, -axisValue - axisDesc.deadZone) / (1.0f - axisDesc.deadZone);
  70. }
  71. axisValue = Math::clamp(axisValue * axisDesc.sensitivity, -1.0f, 1.0f);
  72. if (axisDesc.invert)
  73. axisValue = -axisValue;
  74. return axisValue;
  75. }
  76. return 0.0f;
  77. }
  78. void VirtualInput::_update()
  79. {
  80. UINT64 frameIdx = gTime().getFrameIdx();
  81. for (auto& deviceData : mDevices)
  82. {
  83. for (auto& state : deviceData.cachedStates)
  84. {
  85. // We need to stay in toggled state for one frame.
  86. if (state.second.updateFrameIdx == frameIdx)
  87. continue;
  88. if (state.second.state == ButtonState::ToggledOff)
  89. state.second.state = ButtonState::Off;
  90. else if (state.second.state == ButtonState::ToggledOn)
  91. state.second.state = ButtonState::On;
  92. }
  93. }
  94. bool hasEvents = true;
  95. UINT64 repeatInternal = mInputConfiguration->getRepeatInterval();
  96. UINT64 currentTime = gTime().getTimeMs();
  97. // Trigger all events
  98. while(hasEvents)
  99. {
  100. while(!mEvents.empty())
  101. {
  102. VirtualButtonEvent& event = mEvents.front();
  103. if(event.state == ButtonState::On)
  104. {
  105. if(!onButtonDown.empty())
  106. onButtonDown(event.button, event.deviceIdx);
  107. }
  108. else if(event.state == ButtonState::Off)
  109. {
  110. if(!onButtonUp.empty())
  111. onButtonUp(event.button, event.deviceIdx);
  112. }
  113. mEvents.pop();
  114. }
  115. // Queue up any repeatable events
  116. hasEvents = false;
  117. for (auto& deviceData : mDevices)
  118. {
  119. for (auto& state : deviceData.cachedStates)
  120. {
  121. if (state.second.state != ButtonState::On)
  122. continue;
  123. if (!state.second.allowRepeat)
  124. continue;
  125. UINT64 diff = currentTime - state.second.timestamp;
  126. if (diff >= repeatInternal)
  127. {
  128. state.second.timestamp += repeatInternal;
  129. VirtualButtonEvent event;
  130. event.button = state.second.button;
  131. event.state = ButtonState::On;
  132. event.deviceIdx = 0;
  133. mEvents.push(event);
  134. hasEvents = true;
  135. }
  136. }
  137. break; // Only repeat the first device. Repeat only makes sense for keyboard which there is only one of.
  138. }
  139. }
  140. }
  141. void VirtualInput::buttonDown(const ButtonEvent& event)
  142. {
  143. if(event.buttonCode == BC_LSHIFT || event.buttonCode == BC_RSHIFT)
  144. mActiveModifiers |= (UINT32)ButtonModifier::Shift;
  145. else if(event.buttonCode == BC_LCONTROL || event.buttonCode == BC_RCONTROL)
  146. mActiveModifiers |= (UINT32)ButtonModifier::Ctrl;
  147. else if(event.buttonCode == BC_LMENU || event.buttonCode == BC_RMENU)
  148. mActiveModifiers |= (UINT32)ButtonModifier::Alt;
  149. else
  150. {
  151. tempButtons.clear();
  152. tempBtnDescs.clear();
  153. if (mInputConfiguration->_getButtons(event.buttonCode, mActiveModifiers, tempButtons, tempBtnDescs))
  154. {
  155. while (event.deviceIdx >= (UINT32)mDevices.size())
  156. mDevices.push_back(DeviceData());
  157. Map<UINT32, ButtonData>& cachedStates = mDevices[event.deviceIdx].cachedStates;
  158. UINT32 numButtons = (UINT32)tempButtons.size();
  159. for (UINT32 i = 0; i < numButtons; i++)
  160. {
  161. const VirtualButton& btn = tempButtons[i];
  162. const VIRTUAL_BUTTON_DESC& btnDesc = tempBtnDescs[i];
  163. ButtonData& data = cachedStates[btn.buttonIdentifier];
  164. data.button = btn;
  165. data.state = ButtonState::ToggledOn;
  166. data.timestamp = event.timestamp;
  167. data.updateFrameIdx = gTime().getFrameIdx();
  168. data.allowRepeat = btnDesc.repeatable;
  169. VirtualButtonEvent virtualEvent;
  170. virtualEvent.button = btn;
  171. virtualEvent.state = ButtonState::On;
  172. virtualEvent.deviceIdx = event.deviceIdx;
  173. mEvents.push(virtualEvent);
  174. }
  175. }
  176. }
  177. }
  178. void VirtualInput::buttonUp(const ButtonEvent& event)
  179. {
  180. if(event.buttonCode == BC_LSHIFT || event.buttonCode == BC_RSHIFT)
  181. mActiveModifiers &= ~(UINT32)ButtonModifier::Shift;
  182. else if(event.buttonCode == BC_LCONTROL || event.buttonCode == BC_RCONTROL)
  183. mActiveModifiers &= ~(UINT32)ButtonModifier::Ctrl;
  184. else if(event.buttonCode == BC_LMENU || event.buttonCode == BC_RMENU)
  185. mActiveModifiers &= ~(UINT32)ButtonModifier::Alt;
  186. else
  187. {
  188. tempButtons.clear();
  189. tempBtnDescs.clear();
  190. if (mInputConfiguration->_getButtons(event.buttonCode, mActiveModifiers, tempButtons, tempBtnDescs))
  191. {
  192. while (event.deviceIdx >= (UINT32)mDevices.size())
  193. mDevices.push_back(DeviceData());
  194. Map<UINT32, ButtonData>& cachedStates = mDevices[event.deviceIdx].cachedStates;
  195. UINT32 numButtons = (UINT32)tempButtons.size();
  196. for (UINT32 i = 0; i < numButtons; i++)
  197. {
  198. const VirtualButton& btn = tempButtons[i];
  199. const VIRTUAL_BUTTON_DESC& btnDesc = tempBtnDescs[i];
  200. ButtonData& data = cachedStates[btn.buttonIdentifier];
  201. data.button = btn;
  202. data.state = ButtonState::ToggledOff;
  203. data.timestamp = event.timestamp;
  204. data.updateFrameIdx = gTime().getFrameIdx();
  205. data.allowRepeat = btnDesc.repeatable;
  206. VirtualButtonEvent virtualEvent;
  207. virtualEvent.button = btn;
  208. virtualEvent.state = ButtonState::Off;
  209. virtualEvent.deviceIdx = event.deviceIdx;
  210. mEvents.push(virtualEvent);
  211. }
  212. }
  213. }
  214. }
  215. VirtualInput& gVirtualInput()
  216. {
  217. return VirtualInput::instance();
  218. }
  219. }