InputDeviceKSPartStatus.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. namespace SC.XR.Unity.Module_InputSystem.InputDeviceGC.KS {
  9. public class InputDeviceKSPartStatus : InputDeviceGCPartStatus {
  10. public InputDeviceKSPart inputDeviceKSPart;
  11. public InputDeviceKSPartStatus(InputDeviceKSPart inputDeviceKSPart) : base(inputDeviceKSPart) {
  12. this.inputDeviceKSPart = inputDeviceKSPart;
  13. }
  14. bool invokeOnce = false;
  15. public override void OnSCLateUpdate() {
  16. base.OnSCLateUpdate();
  17. if(invokeOnce == false && API_GSXR_Slam.SlamManager.IsRunning) {
  18. invokeOnce = true;
  19. bool isConnected = false;
  20. if(Application.platform != RuntimePlatform.Android) {
  21. if(inputDeviceKSPart.inputDeviceKS.SimulateInEditorMode == true) {
  22. if(inputDeviceKSPart.PartType == InputDevicePartType.KSLeft) {
  23. isConnected = inputDeviceKSPart.inputDeviceKS.LeftActive;
  24. if(isConnected) {
  25. InputDataKS.StatusDataList.Add(new InputDataKS.StatusData() { isConnected = isConnected, deviceID = (int)KSIndex.Left });
  26. }
  27. } else if(inputDeviceKSPart.PartType == InputDevicePartType.KSRight) {
  28. isConnected = inputDeviceKSPart.inputDeviceKS.RightActive;
  29. if(isConnected) {
  30. InputDataKS.StatusDataList.Add(new InputDataKS.StatusData() { isConnected = isConnected, deviceID = (int)KSIndex.Right });
  31. }
  32. }
  33. }
  34. } else if(Application.platform == RuntimePlatform.Android) {
  35. if(inputDeviceKSPart.PartType == InputDevicePartType.KSLeft) {
  36. isConnected = API_GSXR_Slam.SlamManager.plugin.GSXR_Is_ControllerConnect(0);
  37. if(isConnected) {
  38. InputDataKS.StatusDataList.Add(new InputDataKS.StatusData() { isConnected = isConnected, deviceID = (int)KSIndex.Left });
  39. }
  40. } else if(inputDeviceKSPart.PartType == InputDevicePartType.KSRight) {
  41. isConnected = API_GSXR_Slam.SlamManager.plugin.GSXR_Is_ControllerConnect(1);
  42. if(isConnected) {
  43. InputDataKS.StatusDataList.Add(new InputDataKS.StatusData() { isConnected = isConnected, deviceID = (int)KSIndex.Right });
  44. }
  45. }
  46. }
  47. }
  48. if(InputDataKS.StatusDataList.Count > 0) {
  49. if((inputDeviceKSPart.PartType == InputDevicePartType.KSLeft && InputDataKS.StatusDataList[0].deviceID == (int)KSIndex.Left)
  50. ||
  51. (inputDeviceKSPart.PartType == InputDevicePartType.KSRight && InputDataKS.StatusDataList[0].deviceID == (int)KSIndex.Right)) {
  52. inputDeviceKSPart.inputDataKS.isConnected = InputDataKS.StatusDataList[0].isConnected;
  53. InputDataKS.StatusDataList.RemoveAt(0);
  54. DebugMy.Log(inputDeviceKSPart.PartType + " StatusChange:" + inputDeviceKSPart.inputDataKS.isConnected, this, true);
  55. StatusChangeCallBack();
  56. }
  57. }
  58. }
  59. public override void OnSCDisable() {
  60. base.OnSCDisable();
  61. invokeOnce = false;
  62. }
  63. protected void StatusChangeCallBack() {
  64. if(inputDeviceKSPart.inputDataKS.isConnected && inputDeviceKSPart.inputDataBase.isVaild == false) {
  65. inputDeviceKSPart.inputDataBase.isVaild = UpdateDeviceInfo(inputDeviceKSPart);
  66. DebugMy.Log("Normal Connect !", this, true);
  67. } else if (inputDeviceKSPart.inputDataKS.isConnected && inputDeviceKSPart.inputDataBase.isVaild) {
  68. DebugMy.Log("Server Restart !",this,true);
  69. } else {
  70. inputDeviceKSPart.inputDataBase.isVaild = UpdateDeviceInfo(inputDeviceKSPart,true);
  71. DebugMy.Log("Normal DisConnect !", this, true);
  72. }
  73. }
  74. protected virtual bool UpdateDeviceInfo(InputDeviceKSPart part,bool isClear=false) {
  75. if(isClear == true) {
  76. part.inputDataKS.GCType = GCType.Null;
  77. part.inputDataKS.GCName = "";
  78. part.inputDataKS.SoftVesion = -1;
  79. part.inputDataKS.BatteryPower = -1;
  80. DebugMy.LogError("UpdateDeviceInfo Clear!", this);
  81. return false;
  82. }
  83. KSIndex index = part.inputDataKS.ksIndex;
  84. if(index != KSIndex.Left && index != KSIndex.Right) {
  85. DebugMy.LogError("UpdateDeviceInfo Error:" + index, this);
  86. return false;
  87. }
  88. try {
  89. int typeFlag = API_GSXR_Slam.SlamManager.plugin.GSXR_Get_ControllerList((int)index);
  90. if(index == KSIndex.Left) {
  91. if((int)KSTypeFlag.K11 == (typeFlag & 0xF)) {
  92. part.inputDataKS.GCType = GCType.K11;
  93. } else if((int)KSTypeFlag.K101 == (typeFlag & 0xF)) {
  94. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "UseK102Model")) {
  95. part.inputDataKS.GCType = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "UseK102Model", 0) ? GCType.K102 : GCType.K101;
  96. } else {
  97. part.inputDataKS.GCType = GCType.K101;
  98. }
  99. }
  100. } else if(part.inputDataKS.ksIndex == KSIndex.Right) {
  101. if((int)KSTypeFlag.K11 == ((typeFlag & 0xF0) >> 4)) {
  102. part.inputDataKS.GCType = GCType.K11;
  103. } else if((int)KSTypeFlag.K101 == ((typeFlag & 0xF0) >> 4)) {
  104. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "UseK102Model")) {
  105. part.inputDataKS.GCType = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "UseK102Model", 0) ? GCType.K102 : GCType.K101;
  106. } else {
  107. part.inputDataKS.GCType = GCType.K101;
  108. }
  109. }
  110. }
  111. if(part.inputDataKS.GCType != GCType.K11 && part.inputDataKS.GCType != GCType.K101 && part.inputDataKS.GCType != GCType.K102) {
  112. DebugMy.LogError("UpdateDeviceInfo Error:" + part.inputDataKS.GCType, this);
  113. return false;
  114. }
  115. part.inputDataKS.GCName = part.inputDataKS.GCType.ToString();
  116. part.inputDataKS.SoftVesion = API_GSXR_Slam.SlamManager.plugin.GSXR_Get_ControllerVersion((int)index);
  117. part.inputDataKS.BatteryPower = API_GSXR_Slam.SlamManager.plugin.GSXR_Get_ControllerBattery((int)index);
  118. if (part.PostureType == PostureType.UnKown) {
  119. if (part.inputDataKS.GCType == GCType.K11) {
  120. part.PostureType = PostureType._3Dof;
  121. DebugMy.LogError("K11 Use 3Dof:" + part.inputDataKS.GCType, this);
  122. } else if (part.inputDataKS.GCType == GCType.K102 || part.inputDataKS.GCType == GCType.K101) {
  123. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "KSMode6Dof")) {
  124. part.PostureType = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "KSMode6Dof", 0) ? PostureType._6Dof : PostureType._3Dof;
  125. }
  126. }
  127. }
  128. DebugMy.Log("UpdateDeviceInfo : "
  129. + " isConnected="+part.inputDataKS.isConnected
  130. + " GCName=" + part.inputDataKS.GCName
  131. + " PostureType=" + part.PostureType
  132. + " SoftVesion=" + part.inputDataKS.SoftVesion
  133. + " BatteryPower=" + part.inputDataKS.BatteryPower
  134. , this, true);
  135. } catch(Exception e) {
  136. Debug.Log(e);
  137. }
  138. return true;
  139. }
  140. }
  141. }