BsVirtualInput.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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)VButtonModifier::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<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. for (auto& deviceData : mDevices)
  81. {
  82. for (auto& state : deviceData.cachedStates)
  83. {
  84. if (state.second.state == ButtonState::ToggledOff)
  85. state.second.state = ButtonState::Off;
  86. else if (state.second.state == ButtonState::ToggledOn)
  87. state.second.state = ButtonState::On;
  88. }
  89. }
  90. bool hasEvents = true;
  91. UINT64 repeatInternal = mInputConfiguration->getRepeatInterval();
  92. UINT64 currentTime = gTime().getTimeMs();
  93. // Trigger all events
  94. while(hasEvents)
  95. {
  96. while(!mEvents.empty())
  97. {
  98. VirtualButtonEvent& event = mEvents.front();
  99. if(event.state == ButtonState::On)
  100. {
  101. if(!onButtonDown.empty())
  102. onButtonDown(event.button, event.deviceIdx);
  103. }
  104. else if(event.state == ButtonState::Off)
  105. {
  106. if(!onButtonUp.empty())
  107. onButtonUp(event.button, event.deviceIdx);
  108. }
  109. mEvents.pop();
  110. }
  111. // Queue up any repeatable events
  112. hasEvents = false;
  113. for (auto& deviceData : mDevices)
  114. {
  115. for (auto& state : deviceData.cachedStates)
  116. {
  117. if (state.second.state != ButtonState::On)
  118. continue;
  119. if (!state.second.allowRepeat)
  120. continue;
  121. UINT64 diff = currentTime - state.second.timestamp;
  122. if (diff >= repeatInternal)
  123. {
  124. state.second.timestamp += repeatInternal;
  125. VirtualButtonEvent event;
  126. event.button = state.second.button;
  127. event.state = ButtonState::On;
  128. event.deviceIdx = 0;
  129. mEvents.push(event);
  130. hasEvents = true;
  131. }
  132. }
  133. break; // Only repeat the first device. Repeat only makes sense for keyboard which there is only one of.
  134. }
  135. }
  136. }
  137. void VirtualInput::buttonDown(const ButtonEvent& event)
  138. {
  139. if(event.buttonCode == BC_LSHIFT || event.buttonCode == BC_RSHIFT)
  140. mActiveModifiers |= (UINT32)VButtonModifier::Shift;
  141. else if(event.buttonCode == BC_LCONTROL || event.buttonCode == BC_RCONTROL)
  142. mActiveModifiers |= (UINT32)VButtonModifier::Ctrl;
  143. else if(event.buttonCode == BC_LMENU || event.buttonCode == BC_RMENU)
  144. mActiveModifiers |= (UINT32)VButtonModifier::Alt;
  145. else
  146. {
  147. VirtualButton btn;
  148. VIRTUAL_BUTTON_DESC btnDesc;
  149. if(mInputConfiguration->_getButton(event.buttonCode, mActiveModifiers, btn, btnDesc))
  150. {
  151. while (event.deviceIdx >= (UINT32)mDevices.size())
  152. mDevices.push_back(DeviceData());
  153. Map<UINT32, ButtonData>& cachedStates = mDevices[event.deviceIdx].cachedStates;
  154. ButtonData& data = cachedStates[btn.buttonIdentifier];
  155. data.button = btn;
  156. data.state = ButtonState::ToggledOn;
  157. data.timestamp = event.timestamp;
  158. data.allowRepeat = btnDesc.repeatable;
  159. VirtualButtonEvent virtualEvent;
  160. virtualEvent.button = btn;
  161. virtualEvent.state = ButtonState::On;
  162. virtualEvent.deviceIdx = event.deviceIdx;
  163. mEvents.push(virtualEvent);
  164. }
  165. }
  166. }
  167. void VirtualInput::buttonUp(const ButtonEvent& event)
  168. {
  169. if(event.buttonCode == BC_LSHIFT || event.buttonCode == BC_RSHIFT)
  170. mActiveModifiers &= ~(UINT32)VButtonModifier::Shift;
  171. else if(event.buttonCode == BC_LCONTROL || event.buttonCode == BC_RCONTROL)
  172. mActiveModifiers &= ~(UINT32)VButtonModifier::Ctrl;
  173. else if(event.buttonCode == BC_LMENU || event.buttonCode == BC_RMENU)
  174. mActiveModifiers &= ~(UINT32)VButtonModifier::Alt;
  175. else
  176. {
  177. VirtualButton btn;
  178. VIRTUAL_BUTTON_DESC btnDesc;
  179. if(mInputConfiguration->_getButton(event.buttonCode, mActiveModifiers, btn, btnDesc))
  180. {
  181. while (event.deviceIdx >= (UINT32)mDevices.size())
  182. mDevices.push_back(DeviceData());
  183. Map<UINT32, ButtonData>& cachedStates = mDevices[event.deviceIdx].cachedStates;
  184. ButtonData& data = cachedStates[btn.buttonIdentifier];
  185. data.button = btn;
  186. data.state = ButtonState::ToggledOff;
  187. data.timestamp = event.timestamp;
  188. data.allowRepeat = btnDesc.repeatable;
  189. VirtualButtonEvent virtualEvent;
  190. virtualEvent.button = btn;
  191. virtualEvent.state = ButtonState::Off;
  192. virtualEvent.deviceIdx = event.deviceIdx;
  193. mEvents.push(virtualEvent);
  194. }
  195. }
  196. }
  197. VirtualInput& gVirtualInput()
  198. {
  199. return VirtualInput::instance();
  200. }
  201. }