| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class BluetoothVliew : PinBallUIBase
- {
- [SerializeField] GameObject btnConnect;
- [SerializeField] GameObject btnGyrCalibrate;
- [SerializeField] GameObject btnMagCalibrate;
- [SerializeField] Text textGyrCalibrate;
- [SerializeField] Text textMagCalibrate;
- void Start()
- {
- UIEventListener.Get(btnConnect).onClick = OnClick;
- UIEventListener.Get(btnGyrCalibrate).onClick = OnClick;
- UIEventListener.Get(btnMagCalibrate).onClick = OnClick;
- }
- void Update()
- {
- UpdateBtnForConnect();
- UpdateForGyrCalibrate();
- UpdateForMagCalibrate();
- }
- void OnDestroy()
- {
- if (gyrCalibrating && AimHandler.ins)
- {
- AimHandler.ins.CalibrateGyr(false);
- AimHandler.ins.ResetGyr();
- }
- }
- void OnClick(GameObject o)
- {
- if (o.Equals(btnConnect)) BluetoothAim.ins.DoConnect();
- else if (o.Equals(btnGyrCalibrate)) OnClick_BtnGyrCalibrate();
- else if (o.Equals(btnMagCalibrate)) OnClick_BtnMagCalibrate();
- }
- bool gyrCalibrating;
- void OnClick_BtnGyrCalibrate()
- {
- if (!gyrCalibrating && BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess)
- {
- PopupTip.Show("请先连接模块");
- return;
- }
- if (magCalibrating)
- {
- PopupTip.Show("地磁计正在校准,请勿同时操作!");
- return;
- }
- gyrCalibrating = !gyrCalibrating;
- if (gyrCalibrating)
- {
- AimHandler.ins.gyrCalibrateCompleteCount = 0;
- }
- AimHandler.ins.CalibrateGyr(gyrCalibrating);
- AimHandler.ins.ResetGyr();
- }
- bool magCalibrating;
- void OnClick_BtnMagCalibrate()
- {
- if (!magCalibrating && BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess)
- {
- PopupTip.Show("请先连接模块");
- return;
- }
- if (gyrCalibrating)
- {
- PopupTip.Show("陀螺仪正在校准,请勿同时操作!");
- return;
- }
- magCalibrating = !magCalibrating;
- if (magCalibrating)
- {
- magCalibrateStartTime = Time.realtimeSinceStartup;
- AimHandler.ins.ResetMag();
- }
- }
- BluetoothStatusEnum bowStatus;
- void UpdateBtnForConnect()
- {
- if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status)
- {
- bowStatus = BluetoothAim.ins.status;
- (string textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
- btnConnect.GetComponentInChildren<Text>().text = textID;
- btnConnect.GetComponentInChildren<Text>().color = color;
- }
- }
- void UpdateForGyrCalibrate()
- {
- int progress = 0;
- if (gyrCalibrating)
- {
- progress = AimHandler.ins.gyrCalibrateCompleteCount * 100 / AimHandler.ins.gyrCalibrateTotalCount;
- if (progress >= 100)
- {
- progress = 100;
- gyrCalibrating = false;
- AimHandler.ins.CalibrateGyr(false);
- StartCoroutine(AimHandler.ins.SaveGyr());
- PopupTip.Show("陀螺仪校准完成");
- }
- }
- else if (AimHandler.ins.IsGyrCompleted())
- {
- progress = 100;
- }
- textGyrCalibrate.text = progress + "%";
- btnGyrCalibrate.GetComponentInChildren<Text>().text = (gyrCalibrating ? "停止" : "开始") + "-陀螺仪校准";
- }
- float magCalibrateStartTime;
- void UpdateForMagCalibrate()
- {
- int progress = 0;
- if (AimHandler.ins.IsMagCompleted())
- {
- progress = 100;
- if (magCalibrating) magCalibrating = false;
- }
- else if (magCalibrating)
- {
- progress = Mathf.FloorToInt((Time.realtimeSinceStartup - magCalibrateStartTime) / 20 * 100);
- if (progress >= 100)
- {
- progress = 0;
- magCalibrating = false;
- PopupTip.Show("存在磁场干扰,请远离电子设备后再进行校准!");
- }
- }
- textMagCalibrate.text = $"{progress}%";
- btnMagCalibrate.GetComponentInChildren<Text>().text = (magCalibrating ? "停止" : "开始") + "-地磁计校准";
- }
- }
|