GyrGuidanceView.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using AppUI.Bluetooth;
  5. using AppUI.Manager.View;
  6. using SmartBowSDK;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using BLEAimhandler = AppUI.Bluetooth.AimHandler;
  10. namespace AppUI.View.NineAxis
  11. {
  12. /// <summary>九轴陀螺仪校准引导(AppUI);调用方式对齐 DemoStarter。</summary>
  13. public class GyrGuidanceView : MonoBehaviour
  14. {
  15. const float ModuleInitWaitSeconds = 12f;
  16. [SerializeField] Button btnGyrCalibrate;
  17. [SerializeField] List<GameObject> layouts;
  18. TextAutoLanguage2 _textAutoLanguage2;
  19. BLEAimhandler _aim;
  20. SmartBowDeviceHub _hub;
  21. bool _autoNext;
  22. bool _gyrCompleted;
  23. bool _canNavTo;
  24. bool _canUpdateGyrProgress;
  25. bool _sensorsReady;
  26. [NonSerialized] public int flag_GyrCalibarateOperateAndFinish = -1;
  27. public Action action_GyrCalibarateOperateAndFinish;
  28. public Func<bool> action_OnClickGyrCalibrateInterceptor;
  29. void Start()
  30. {
  31. SmartBowDeviceHub.EnsureReady();
  32. _hub = SmartBowDeviceHub.ins;
  33. _aim = _hub?.Aim;
  34. btnGyrCalibrate.onClick.AddListener(OnClickGyrCalibrate);
  35. transform.Find("BtnNext").GetComponent<Button>().onClick.AddListener(OnClick_Next);
  36. _textAutoLanguage2 = btnGyrCalibrate.transform.Find("Text").GetComponent<TextAutoLanguage2>();
  37. RefreshButtonLabel();
  38. ShowDeviceLayout(_aim != null && _aim.GetActivePlayerDeviceType() == (int)AimDeviceType.ARTEMIS ? 1 : 0);
  39. if (_aim != null && _aim.IsSdkGyrCompleted())
  40. {
  41. _gyrCompleted = true;
  42. _canNavTo = true;
  43. }
  44. StartCoroutine(WaitForSensorsReady());
  45. }
  46. IEnumerator WaitForSensorsReady()
  47. {
  48. float elapsed = 0f;
  49. while (elapsed < ModuleInitWaitSeconds)
  50. {
  51. if (_hub != null && _hub.EnsureNineAxisSensorsStarted())
  52. {
  53. _sensorsReady = true;
  54. yield break;
  55. }
  56. elapsed += Time.unscaledDeltaTime;
  57. yield return null;
  58. }
  59. Debug.LogWarning("[GyrGuidanceView] 等待模块初始化/九轴传感开启超时。");
  60. }
  61. void RefreshButtonLabel()
  62. {
  63. if (_textAutoLanguage2 == null || _aim == null)
  64. return;
  65. _textAutoLanguage2.SetTextKey(_aim.IsSdkGyrCompleted()
  66. ? "Gyro_Reinitialize"
  67. : "Gyro_Initialization");
  68. }
  69. void ShowDeviceLayout(int index)
  70. {
  71. for (int i = 0; i < layouts.Count; i++)
  72. layouts[i].SetActive(index == i);
  73. }
  74. void Update()
  75. {
  76. if (_aim == null)
  77. return;
  78. if (!_sensorsReady && _hub != null)
  79. _sensorsReady = _hub.EnsureNineAxisSensorsStarted();
  80. if (_aim.IsSdkGyrCompleted() && !_gyrCompleted && flag_GyrCalibarateOperateAndFinish != 0)
  81. {
  82. _gyrCompleted = true;
  83. _canNavTo = true;
  84. }
  85. UpdateGyrProgress();
  86. }
  87. void OnClickGyrCalibrate()
  88. {
  89. if (action_OnClickGyrCalibrateInterceptor != null
  90. && action_OnClickGyrCalibrateInterceptor.Invoke())
  91. return;
  92. if (!IsDeviceConnected())
  93. {
  94. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
  95. return;
  96. }
  97. if (!_sensorsReady && (_hub == null || !_hub.EnsureNineAxisSensorsStarted()))
  98. {
  99. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
  100. return;
  101. }
  102. // DemoStarter:IsGyrCalibrating ? Stop : Start
  103. if (_aim == null || !_aim.TryToggleGyrCalibration())
  104. {
  105. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
  106. return;
  107. }
  108. bool calibrating = _aim.IsSdkGyrCalibrating();
  109. if (calibrating)
  110. {
  111. _autoNext = true;
  112. _canUpdateGyrProgress = true;
  113. flag_GyrCalibarateOperateAndFinish = 0;
  114. _canNavTo = false;
  115. }
  116. else
  117. {
  118. _autoNext = false;
  119. _canUpdateGyrProgress = false;
  120. _textAutoLanguage2.SetTextKey("Gyro_Reinitialize");
  121. }
  122. }
  123. bool IsDeviceConnected()
  124. {
  125. if (_hub == null)
  126. return false;
  127. return _hub.IsConnected(_hub.GetActivePlayer());
  128. }
  129. void FinishGyrCalibrate()
  130. {
  131. _canUpdateGyrProgress = false;
  132. if (_aim != null && _aim.IsSdkGyrCalibrating())
  133. _aim.TryStopGyrCalibration();
  134. StartCoroutine(_aim.SaveGyr());
  135. }
  136. void UpdateGyrProgress()
  137. {
  138. if (!_canUpdateGyrProgress || _aim == null)
  139. return;
  140. int progress = _aim.GetSdkGyrProgressPercent();
  141. _textAutoLanguage2.textFormatArgs = new object[] { progress };
  142. _textAutoLanguage2.SetTextKey("Gyro_Initializing");
  143. if (!_aim.IsSdkGyrCompleted())
  144. return;
  145. FinishGyrCalibrate();
  146. if (flag_GyrCalibarateOperateAndFinish != 0)
  147. return;
  148. flag_GyrCalibarateOperateAndFinish = 1;
  149. action_GyrCalibarateOperateAndFinish?.Invoke();
  150. _textAutoLanguage2.SetTextKey("Gyro_Success");
  151. _gyrCompleted = true;
  152. _canNavTo = true;
  153. if (_autoNext)
  154. {
  155. _autoNext = false;
  156. NavigateToMag();
  157. }
  158. }
  159. void NavigateToMag()
  160. {
  161. if (_aim == null || !_aim.IsSdkGyrCompleted())
  162. return;
  163. _hub?.EnsureNineAxisSensorsStarted();
  164. ViewManager.HideView(UIViewType.GyrGuidanceView);
  165. ViewManager.ShowView(UIViewType.MagGuidanceView);
  166. }
  167. void OnClick_Next()
  168. {
  169. if (!_canNavTo || _aim == null)
  170. return;
  171. NavigateToMag();
  172. }
  173. public void OnClick_Back()
  174. {
  175. AudioMgr.ins.PlayBtn();
  176. ViewManager.HideView(UIViewType.GyrGuidanceView);
  177. }
  178. }
  179. }