InputDeviceKSPartStatus.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 GCType DefaultType = GCType.K102;
  16. public override void OnSCLateUpdate() {
  17. base.OnSCLateUpdate();
  18. if(invokeOnce == false ) {
  19. invokeOnce = true;
  20. bool isConnected = false;
  21. if(Application.platform != RuntimePlatform.Android) {
  22. if(inputDeviceKSPart.inputDeviceKS.SimulateInEditorMode == true) {
  23. if(inputDeviceKSPart.PartType == InputDevicePartType.KSLeft) {
  24. isConnected = inputDeviceKSPart.inputDeviceKS.LeftActive;
  25. if(isConnected) {
  26. InputDataKS.StatusDataList.Add(new InputDataKS.StatusData() { isConnected = isConnected, deviceID = (int)KSIndex.Left });
  27. }
  28. } else if(inputDeviceKSPart.PartType == InputDevicePartType.KSRight) {
  29. isConnected = inputDeviceKSPart.inputDeviceKS.RightActive;
  30. if(isConnected) {
  31. InputDataKS.StatusDataList.Add(new InputDataKS.StatusData() { isConnected = isConnected, deviceID = (int)KSIndex.Right });
  32. }
  33. }
  34. }
  35. } else if(Application.platform == RuntimePlatform.Android) {
  36. if(inputDeviceKSPart.PartType == InputDevicePartType.KSLeft) {
  37. isConnected = API_GSXR_Slam.plugin.GSXR_Is_ControllerConnect(0);
  38. if(isConnected) {
  39. InputDataKS.StatusDataList.Add(new InputDataKS.StatusData() { isConnected = isConnected, deviceID = (int)KSIndex.Left });
  40. }
  41. } else if(inputDeviceKSPart.PartType == InputDevicePartType.KSRight) {
  42. isConnected = API_GSXR_Slam.plugin.GSXR_Is_ControllerConnect(1);
  43. if(isConnected) {
  44. InputDataKS.StatusDataList.Add(new InputDataKS.StatusData() { isConnected = isConnected, deviceID = (int)KSIndex.Right });
  45. }
  46. }
  47. DebugMy.Log(inputDeviceKSPart.PartType + " isConnected: " + isConnected, true, true);
  48. }
  49. }
  50. if(InputDataKS.StatusDataList.Count > 0) {
  51. if((inputDeviceKSPart.PartType == InputDevicePartType.KSLeft && InputDataKS.StatusDataList[0].deviceID == (int)KSIndex.Left)
  52. ||
  53. (inputDeviceKSPart.PartType == InputDevicePartType.KSRight && InputDataKS.StatusDataList[0].deviceID == (int)KSIndex.Right)) {
  54. inputDeviceKSPart.inputDataKS.isConnected = InputDataKS.StatusDataList[0].isConnected;
  55. InputDataKS.StatusDataList.RemoveAt(0);
  56. DebugMy.Log(inputDeviceKSPart.PartType + " StatusChange:" + inputDeviceKSPart.inputDataKS.isConnected, this, true);
  57. StatusChangeCallBack();
  58. }
  59. }
  60. }
  61. public override void OnSCDisable() {
  62. base.OnSCDisable();
  63. invokeOnce = false;
  64. }
  65. protected void StatusChangeCallBack() {
  66. if(inputDeviceKSPart.inputDataKS.isConnected && inputDeviceKSPart.inputDataBase.isVaild == false) {
  67. inputDeviceKSPart.inputDataBase.isVaild = UpdateDeviceInfo(inputDeviceKSPart);
  68. DebugMy.Log("Normal Connect !", this, true);
  69. } else if (inputDeviceKSPart.inputDataKS.isConnected && inputDeviceKSPart.inputDataBase.isVaild) {
  70. DebugMy.Log("Server Restart !",this,true);
  71. } else {
  72. inputDeviceKSPart.inputDataBase.isVaild = UpdateDeviceInfo(inputDeviceKSPart,true);
  73. DebugMy.Log("Normal DisConnect !", this, true);
  74. }
  75. }
  76. protected virtual bool UpdateDeviceInfo(InputDeviceKSPart part,bool isClear=false) {
  77. if(isClear == true) {
  78. part.inputDataKS.GCType = GCType.Null;
  79. part.inputDataKS.GCName = "";
  80. part.inputDataKS.SoftVesion = -1;
  81. //part.inputDataKS.BatteryPower = -1;
  82. DebugMy.LogError("UpdateDeviceInfo Clear!", this);
  83. return false;
  84. }
  85. KSIndex index = part.inputDataKS.ksIndex;
  86. if(index != KSIndex.Left && index != KSIndex.Right) {
  87. DebugMy.LogError("UpdateDeviceInfo Error:" + index, this);
  88. return false;
  89. }
  90. try {
  91. int typeFlag = API_GSXR_Slam.plugin.GSXR_Get_ControllerList();
  92. DebugMy.Log("GSXR_Get_Controller Bond:"+index+" "+ typeFlag, this,true);
  93. if (typeFlag == (int)KSID.Nolo) {
  94. part.inputDataKS.GCType = GCType.Nolo;
  95. } else if (typeFlag == (int)KSID.Luci) {
  96. part.inputDataKS.GCType = GCType.Luci;
  97. } else {
  98. if (index == KSIndex.Left) {
  99. if ((int)KSTypeFlag.K11 == (typeFlag & 0xF)) {
  100. part.inputDataKS.GCType = GCType.K11;
  101. } else if ((int)KSTypeFlag.K101 == (typeFlag & 0xF)) {
  102. part.inputDataKS.GCType = GCType.K101;
  103. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "UseK102Model")) {
  104. part.inputDataKS.GCType = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "UseK102Model", 0) ? GCType.K102 : GCType.K101;
  105. }
  106. } else if ((int)KSTypeFlag.K07 == (typeFlag & 0xF)) {
  107. part.inputDataKS.GCType = GCType.K07;
  108. } else {
  109. part.inputDataKS.GCType = GCType.Default;
  110. }
  111. } else if (part.inputDataKS.ksIndex == KSIndex.Right) {
  112. if ((int)KSTypeFlag.K11 == ((typeFlag & 0xF0) >> 4)) {
  113. part.inputDataKS.GCType = GCType.K11;
  114. } else if ((int)KSTypeFlag.K101 == ((typeFlag & 0xF0) >> 4)) {
  115. part.inputDataKS.GCType = GCType.K101;
  116. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "UseK102Model")) {
  117. part.inputDataKS.GCType = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "UseK102Model", 0) ? GCType.K102 : GCType.K101;
  118. }
  119. } else if ((int)KSTypeFlag.K07 == ((typeFlag & 0xF0) >> 4)) {
  120. part.inputDataKS.GCType = GCType.K07;
  121. } else {
  122. part.inputDataKS.GCType = GCType.Default;
  123. }
  124. }
  125. }
  126. part.inputDataKS.GCName = part.inputDataKS.GCType.ToString();
  127. part.inputDataKS.SoftVesion = API_GSXR_Slam.plugin.GSXR_Get_ControllerHSVersion((int)index);
  128. part.inputDataKS.BatteryPower = API_GSXR_Slam.plugin.GSXR_Get_ControllerBattery((int)index);
  129. part.inputDataKS.PostureType = API_GSXR_Slam.plugin.GSXR_Get_ControllerMode() == 6 ? PostureType._6Dof : PostureType._3Dof;
  130. if (inputDeviceKSPart.inputDataGetKS.inputDataGetKSPosture.isKS_3DofMode)
  131. {
  132. API_GSXR_Slam.plugin.GSXR_Set_6Dof(false);
  133. part.inputDataKS.PostureType = PostureType._3Dof;
  134. DebugMy.Log("GSXR_Set_6Dof Mode:3Dof Index:" + index, this, true);
  135. }
  136. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "KSModeSet3Dof") && API_Module_SDKConfiguration.GetBool("Module_InputSystem", "KSModeSet3Dof", 0)) {
  137. part.inputDataKS.PostureType = PostureType._3Dof;
  138. DebugMy.Log("PostureType Config '3Dof' By SDKConfiguration: KSModeSet3Dof==true", this,true);
  139. }
  140. if (part.inputDataKS.GCType == GCType.Default) {
  141. part.inputDataKS.GCType = DefaultType;
  142. DebugMy.Log("GCType Use DefaultType : " + DefaultType, this, true);
  143. }
  144. DebugMy.Log("UpdateDeviceInfo : "
  145. + " isConnected="+part.inputDataKS.isConnected
  146. + " GCName=" + part.inputDataKS.GCName
  147. + " GCType=" + part.inputDataKS.GCType
  148. + " PostureType=" + part.inputDataKS.PostureType
  149. + " SoftVesion=" + part.inputDataKS.SoftVesion
  150. + " BatteryPower=" + part.inputDataKS.BatteryPower
  151. , this, true);
  152. } catch(Exception e) {
  153. Debug.Log(e);
  154. }
  155. return true;
  156. }
  157. }
  158. }