InputDeviceKSPartStatus.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 && SvrManager.Instance.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.OneGCActive;
  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.TwoGCActive;
  29. if(isConnected) {
  30. InputDataKS.StatusDataList.Add(new InputDataKS.StatusData() { isConnected = inputDeviceKSPart.inputDeviceKS.TwoGCActive, deviceID = (int)KSIndex.Right });
  31. }
  32. }
  33. }
  34. } else if(Application.platform == RuntimePlatform.Android) {
  35. if(inputDeviceKSPart.PartType == InputDevicePartType.KSLeft) {
  36. isConnected = SvrPlugin.Instance.HandShank_GetConnectState(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 = SvrPlugin.Instance.HandShank_GetConnectState(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. } else {
  67. inputDeviceKSPart.inputDataBase.isVaild = UpdateDeviceInfo(inputDeviceKSPart,true);
  68. }
  69. }
  70. protected virtual bool UpdateDeviceInfo(InputDeviceKSPart part,bool isClear=false) {
  71. if(isClear == true) {
  72. part.inputDataKS.GCType = GCType.Null;
  73. part.inputDataKS.GCName = "";
  74. part.inputDataKS.SoftVesion = -1;
  75. part.inputDataKS.BatteryPower = -1;
  76. DebugMy.LogError("UpdateDeviceInfo Clear!", this);
  77. return false;
  78. }
  79. KSIndex index = part.inputDataKS.ksIndex;
  80. if(index != KSIndex.Left && index != KSIndex.Right) {
  81. DebugMy.LogError("UpdateDeviceInfo Error:" + index, this);
  82. return false;
  83. }
  84. try {
  85. int typeFlag = SvrPlugin.Instance.HandShank_Getbond((int)index);
  86. if(index == KSIndex.Left) {
  87. if((int)KSTypeFlag.K11 == (typeFlag & 0xF)) {
  88. part.inputDataKS.GCType = GCType.K11;
  89. } else if((int)KSTypeFlag.K101 == (typeFlag & 0xF)) {
  90. part.inputDataKS.GCType = GCType.K101;
  91. }
  92. } else if(part.inputDataKS.ksIndex == KSIndex.Right) {
  93. if((int)KSTypeFlag.K11 == ((typeFlag & 0xF0) >> 4)) {
  94. part.inputDataKS.GCType = GCType.K11;
  95. } else if((int)KSTypeFlag.K101 == ((typeFlag & 0xF0) >> 4)) {
  96. part.inputDataKS.GCType = GCType.K101;
  97. }
  98. }
  99. if(part.inputDataKS.GCType != GCType.K11 && part.inputDataKS.GCType != GCType.K101) {
  100. DebugMy.LogError("UpdateDeviceInfo Error:" + part.inputDataKS.GCType, this);
  101. return false;
  102. }
  103. part.inputDataKS.GCName = part.inputDataKS.GCType.ToString();
  104. part.inputDataKS.SoftVesion = SvrPlugin.Instance.HandShank_GetVersion((int)index);
  105. part.inputDataKS.BatteryPower = SvrPlugin.Instance.HandShank_GetBattery((int)index);
  106. DebugMy.Log("UpdateDeviceInfo : "
  107. + " isConnected: "+part.inputDataKS.isConnected
  108. + " GCName: " + part.inputDataKS.GCName
  109. + " SoftVesion: " + part.inputDataKS.SoftVesion
  110. + " BatteryPower " + part.inputDataKS.BatteryPower
  111. , this, true);
  112. } catch(Exception e) {
  113. Debug.Log(e);
  114. }
  115. return true;
  116. }
  117. }
  118. }