Module_InputSystem.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. using SC.XR.Unity.Module_Device;
  9. using System.IO;
  10. namespace SC.XR.Unity.Module_InputSystem
  11. {
  12. public class Module_InputSystem : SCModuleMono
  13. {
  14. public static Module_InputSystem instance { get; private set; }
  15. public event Action initializeCallBack;
  16. public bool initialize { get; private set; }
  17. public bool IsRunning { get; private set; }
  18. public event Action<InputDeviceBase, bool> InputDeviceChangeCallBack;
  19. /// <summary>
  20. /// 所有支持的InputDevice,支持然后register后则启用
  21. /// </summary>
  22. private List<InputDeviceBase> _inputDeviceSupportList;
  23. public List<InputDeviceBase> inputDeviceSupportList
  24. {
  25. get
  26. {
  27. if (_inputDeviceSupportList == null)
  28. {
  29. _inputDeviceSupportList = new List<InputDeviceBase>();
  30. InputDeviceBase[] inputDeviceList = GetComponentsInChildren<InputDeviceBase>(true);
  31. if (API_Module_Device.Current != null)
  32. {
  33. foreach (var inputDevice in inputDeviceList)
  34. {
  35. if (API_Module_Device.Current.SupportInputDevices.Contains(inputDevice.inputDeviceType))
  36. {
  37. DebugMy.Log("Support InputDevice:" + inputDevice.inputDeviceType, this, true);
  38. _inputDeviceSupportList.Add(inputDevice);
  39. }
  40. }
  41. }
  42. else
  43. {
  44. _inputDeviceSupportList = inputDeviceList.ToList();
  45. }
  46. if (_inputDeviceSupportList.Count == 0)
  47. {
  48. DebugMy.Log("No Support InputDevice !", this, true);
  49. }
  50. }
  51. return _inputDeviceSupportList;
  52. }
  53. }
  54. //[Header("Enable Head")]
  55. //[SerializeField]
  56. private bool activeHead = true;
  57. //[Header("Enable BT3Dof")]
  58. //[SerializeField]
  59. private bool activeBT3Dof = false;
  60. //[Header("Enable KS")]
  61. //[SerializeField]
  62. private bool activeKS = false;
  63. //[Header("Enable GreyHand")]
  64. //[SerializeField]
  65. private bool activeGGT26Dof = false;
  66. public override void OnSCAwake()
  67. {
  68. base.OnSCAwake();
  69. if (instance != null)
  70. {
  71. DestroyImmediate(gameObject);
  72. return;
  73. }
  74. //DontDestroyOnLoad(gameObject);
  75. instance = this;
  76. DebugMy.Log("Awake", this, true);
  77. foreach (var inputDevice in inputDeviceSupportList)
  78. {
  79. AddModule(inputDevice);
  80. }
  81. initialize = true;
  82. IsRunning = true;
  83. initializeCallBack?.Invoke();
  84. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ActiveHead"))
  85. {
  86. activeHead = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ActiveHead", 0);
  87. }
  88. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ActiveBT3Dof"))
  89. {
  90. activeBT3Dof = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ActiveBT3Dof", 0);
  91. }
  92. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ActiveKS"))
  93. {
  94. activeKS = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ActiveKS", 0);
  95. // activeKS = false;
  96. }
  97. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ActiveGGT26Dof"))
  98. {
  99. activeGGT26Dof = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ActiveGGT26Dof", 0);
  100. }
  101. activeKS = false;
  102. activeHead = true;
  103. }
  104. private void Update()
  105. {
  106. if (Input.GetKeyDown(KeyCode.P))
  107. {
  108. SetActiveInputDevice(InputDeviceType.KS, true);
  109. SetActiveInputDevice(InputDeviceType.Head, false);
  110. }
  111. if (Input.GetKeyDown(KeyCode.O))
  112. {
  113. SetActiveInputDevice(InputDeviceType.KS, false);
  114. SetActiveInputDevice(InputDeviceType.Head, true);
  115. }
  116. }
  117. public override void OnSCStart()
  118. {
  119. base.OnSCStart();
  120. SetActiveInputDevice(InputDeviceType.Head, activeHead);
  121. SetActiveInputDevice(InputDeviceType.BT3Dof, activeBT3Dof);
  122. SetActiveInputDevice(InputDeviceType.KS, activeKS);
  123. SetActiveInputDevice(InputDeviceType.GGT26Dof, activeGGT26Dof);
  124. initialize = true;
  125. IsRunning = true;
  126. }
  127. public override void OnSCDisable()
  128. {
  129. base.OnSCDisable();
  130. initialize = false;
  131. IsRunning = false;
  132. }
  133. public T GetInputDevice<T>(InputDeviceType type) where T : InputDeviceBase
  134. {
  135. foreach (var inputDevice in inputDeviceSupportList)
  136. {
  137. if (type == inputDevice.inputDeviceType)
  138. {
  139. return (T)inputDevice;
  140. }
  141. }
  142. return null;
  143. }
  144. public void SetActiveInputDevice(InputDeviceType type, bool active)
  145. {
  146. InputDeviceBase inputDevice = GetInputDevice<InputDeviceBase>(type);
  147. if (inputDevice == null)
  148. return;
  149. SetFlag(type, active);
  150. if (active)
  151. {
  152. inputDevice.ModuleStart();
  153. }
  154. else
  155. {
  156. inputDevice.ModuleStop();
  157. }
  158. InputDeviceChangeCallBack?.Invoke(inputDevice, active);
  159. }
  160. private void SetFlag(InputDeviceType type, bool active)
  161. {
  162. if (type == InputDeviceType.Head)
  163. {
  164. activeHead = active;
  165. }
  166. else if (type == InputDeviceType.BT3Dof)
  167. {
  168. activeBT3Dof = active;
  169. }
  170. else if (type == InputDeviceType.KS)
  171. {
  172. activeKS = active;
  173. }
  174. else if (type == InputDeviceType.GGT26Dof)
  175. {
  176. activeGGT26Dof = active;
  177. }
  178. }
  179. InputDeviceBase _inputDevice;
  180. public bool GetInputDeviceStatus(InputDeviceType type)
  181. {
  182. _inputDevice = GetInputDevice<InputDeviceBase>(type);
  183. if (_inputDevice)
  184. {
  185. foreach (var part in _inputDevice.inputDevicePartList)
  186. {
  187. if (part.inputDataBase.isVaild == true)
  188. {
  189. return true;
  190. }
  191. }
  192. }
  193. return false;
  194. }
  195. public bool IsSomeDeviceActiveWithoutHead
  196. {
  197. get
  198. {
  199. if (GetInputDeviceStatus(InputDeviceType.GGT26Dof) ||
  200. GetInputDeviceStatus(InputDeviceType.KS) ||
  201. GetInputDeviceStatus(InputDeviceType.BT3Dof)
  202. )
  203. {
  204. return true;
  205. }
  206. return false;
  207. }
  208. }
  209. }
  210. }