| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using AppUI.Bluetooth;
- using AppUI.Manager.View;
- using SmartBowSDK;
- using UnityEngine;
- using UnityEngine.UI;
- using BLEAimhandler = AppUI.Bluetooth.AimHandler;
- namespace AppUI.View.NineAxis
- {
- /// <summary>九轴陀螺仪校准引导(AppUI);调用方式对齐 DemoStarter。</summary>
- public class GyrGuidanceView : MonoBehaviour
- {
- const float ModuleInitWaitSeconds = 12f;
- [SerializeField] Button btnGyrCalibrate;
- [SerializeField] List<GameObject> layouts;
- TextAutoLanguage2 _textAutoLanguage2;
- BLEAimhandler _aim;
- SmartBowDeviceHub _hub;
- bool _autoNext;
- bool _gyrCompleted;
- bool _canNavTo;
- bool _canUpdateGyrProgress;
- bool _sensorsReady;
- [NonSerialized] public int flag_GyrCalibarateOperateAndFinish = -1;
- public Action action_GyrCalibarateOperateAndFinish;
- public Func<bool> action_OnClickGyrCalibrateInterceptor;
- void Start()
- {
- SmartBowDeviceHub.EnsureReady();
- _hub = SmartBowDeviceHub.ins;
- _aim = _hub?.Aim;
- btnGyrCalibrate.onClick.AddListener(OnClickGyrCalibrate);
- transform.Find("BtnNext").GetComponent<Button>().onClick.AddListener(OnClick_Next);
- _textAutoLanguage2 = btnGyrCalibrate.transform.Find("Text").GetComponent<TextAutoLanguage2>();
- RefreshButtonLabel();
- ShowDeviceLayout(_aim != null && _aim.GetActivePlayerDeviceType() == (int)AimDeviceType.ARTEMIS ? 1 : 0);
- if (_aim != null && _aim.IsSdkGyrCompleted())
- {
- _gyrCompleted = true;
- _canNavTo = true;
- }
- StartCoroutine(WaitForSensorsReady());
- }
- IEnumerator WaitForSensorsReady()
- {
- float elapsed = 0f;
- while (elapsed < ModuleInitWaitSeconds)
- {
- if (_hub != null && _hub.EnsureNineAxisSensorsStarted())
- {
- _sensorsReady = true;
- yield break;
- }
- elapsed += Time.unscaledDeltaTime;
- yield return null;
- }
- Debug.LogWarning("[GyrGuidanceView] 等待模块初始化/九轴传感开启超时。");
- }
- void RefreshButtonLabel()
- {
- if (_textAutoLanguage2 == null || _aim == null)
- return;
- _textAutoLanguage2.SetTextKey(_aim.IsSdkGyrCompleted()
- ? "Gyro_Reinitialize"
- : "Gyro_Initialization");
- }
- void ShowDeviceLayout(int index)
- {
- for (int i = 0; i < layouts.Count; i++)
- layouts[i].SetActive(index == i);
- }
- void Update()
- {
- if (_aim == null)
- return;
- if (!_sensorsReady && _hub != null)
- _sensorsReady = _hub.EnsureNineAxisSensorsStarted();
- if (_aim.IsSdkGyrCompleted() && !_gyrCompleted && flag_GyrCalibarateOperateAndFinish != 0)
- {
- _gyrCompleted = true;
- _canNavTo = true;
- }
- UpdateGyrProgress();
- }
- void OnClickGyrCalibrate()
- {
- if (action_OnClickGyrCalibrateInterceptor != null
- && action_OnClickGyrCalibrateInterceptor.Invoke())
- return;
- if (!IsDeviceConnected())
- {
- PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
- return;
- }
- if (!_sensorsReady && (_hub == null || !_hub.EnsureNineAxisSensorsStarted()))
- {
- PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
- return;
- }
- // DemoStarter:IsGyrCalibrating ? Stop : Start
- if (_aim == null || !_aim.TryToggleGyrCalibration())
- {
- PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
- return;
- }
- bool calibrating = _aim.IsSdkGyrCalibrating();
- if (calibrating)
- {
- _autoNext = true;
- _canUpdateGyrProgress = true;
- flag_GyrCalibarateOperateAndFinish = 0;
- _canNavTo = false;
- }
- else
- {
- _autoNext = false;
- _canUpdateGyrProgress = false;
- _textAutoLanguage2.SetTextKey("Gyro_Reinitialize");
- }
- }
- bool IsDeviceConnected()
- {
- if (_hub == null)
- return false;
- return _hub.IsConnected(_hub.GetActivePlayer());
- }
- void FinishGyrCalibrate()
- {
- _canUpdateGyrProgress = false;
- if (_aim != null && _aim.IsSdkGyrCalibrating())
- _aim.TryStopGyrCalibration();
- StartCoroutine(_aim.SaveGyr());
- }
- void UpdateGyrProgress()
- {
- if (!_canUpdateGyrProgress || _aim == null)
- return;
- int progress = _aim.GetSdkGyrProgressPercent();
- _textAutoLanguage2.textFormatArgs = new object[] { progress };
- _textAutoLanguage2.SetTextKey("Gyro_Initializing");
- if (!_aim.IsSdkGyrCompleted())
- return;
- FinishGyrCalibrate();
- if (flag_GyrCalibarateOperateAndFinish != 0)
- return;
- flag_GyrCalibarateOperateAndFinish = 1;
- action_GyrCalibarateOperateAndFinish?.Invoke();
- _textAutoLanguage2.SetTextKey("Gyro_Success");
- _gyrCompleted = true;
- _canNavTo = true;
- if (_autoNext)
- {
- _autoNext = false;
- NavigateToMag();
- }
- }
- void NavigateToMag()
- {
- if (_aim == null || !_aim.IsSdkGyrCompleted())
- return;
- _hub?.EnsureNineAxisSensorsStarted();
- ViewManager.HideView(UIViewType.GyrGuidanceView);
- ViewManager.ShowView(UIViewType.MagGuidanceView);
- }
- void OnClick_Next()
- {
- if (!_canNavTo || _aim == null)
- return;
- NavigateToMag();
- }
- public void OnClick_Back()
- {
- AudioMgr.ins.PlayBtn();
- ViewManager.HideView(UIViewType.GyrGuidanceView);
- }
- }
- }
|