BsVirtualInput.cpp 8.0 KB

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