| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- /* 设备校准界面,公用(设备界面的校准、校准教程) */
- public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
- {
- [SerializeField] Button btnGyrCalibrate;
- [SerializeField] Text progressGyrCalibrate;
- [SerializeField] Text progressMagCalibrate;
- [SerializeField] GameObject prefab_MagInterferenceTipView;
- [SerializeField] GameObject magReset; //地磁校准按钮
- [SerializeField] GameObject magTipOk;
- [SerializeField] GameObject magTipOk2;
- [SerializeField] Button btnBack;
- [SerializeField] Button btnNext;
- [SerializeField] Button btnFinish;
- public Action action_OnDestroy;
- public DeviceCalibrateItem deviceCalibrateItem = DeviceCalibrateItem.Guide;
- DeviceCalibrateItem[] deviceCalibrateItemForGuide = {
- DeviceCalibrateItem.Gyr,
- DeviceCalibrateItem.Mag
- };
- int guideIndex = 0;
- public bool guide = true;
- public static DeviceCalibrateView ins;
-
- public static void Create(DeviceCalibrateItem itemType = DeviceCalibrateItem.Guide) {
- GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/DeviceCalibrateView"));
- o.GetComponent<DeviceCalibrateView>().deviceCalibrateItem = itemType;
- o.GetComponent<DeviceCalibrateView>().guide = itemType == DeviceCalibrateItem.Guide;
- }
- void Awake() {
- ins = this;
- try {
- GlobalEventCenter.ins.onDeviceCalibrateViewAwakeChanged?.Invoke(true);
- } catch (System.Exception e) { Debug.LogError(e.Message); }
- RenderModuleCamera.Load();
- }
- void Start()
- {
- btnGyrCalibrate.GetComponent<Button>().onClick.AddListener(ClickGyrCalibrate);
- magReset.GetComponent<Button>().onClick.AddListener(ClickResetMag);
- btnBack.GetComponent<Button>().onClick.AddListener(delegate() {
- guideIndex--;
- RefreshPage();
- });
- btnNext.GetComponent<Button>().onClick.AddListener(delegate() {
- guideIndex++;
- RefreshPage();
- });
- btnFinish.GetComponent<Button>().onClick.AddListener(delegate() {
- Destroy(this.gameObject);
- if (guide && guideIndex == deviceCalibrateItemForGuide.Length - 1) {
- GameMgr.ins.FinishDeviceCalibrateGuide();
- }
- });
- if (guide) { //看看哪项没校准,就引导校准
- List<DeviceCalibrateItem> guideList = new List<DeviceCalibrateItem>();
- //九轴的需要校准引导,判断是否初始化陀螺仪和地磁计
- if (AimHandler.ins.bRuning9Axis())
- {
- if (!AimHandler.ins.IsGyrCompleted())
- {
- guideList.Add(DeviceCalibrateItem.Gyr);
- }
- if (!AimHandler.ins.IsMagCompleted())
- {
- guideList.Add(DeviceCalibrateItem.Mag);
- }
- }
- deviceCalibrateItemForGuide = guideList.ToArray();
- if (deviceCalibrateItemForGuide.Length == 0) {
- guideIndex = -1;
- btnFinish.GetComponent<Button>().onClick.Invoke();
- return;
- }
- }
- RefreshPage();
- if (!guide) {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- }
- }
- void OnDestroy()
- {
- if (ins == this) ins = null;
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- try {
- GlobalEventCenter.ins.onDeviceCalibrateViewAwakeChanged?.Invoke(false);
- } catch (System.Exception e) { Debug.LogError(e.Message); }
- RenderModuleCamera.Unload();
- action_OnDestroy?.Invoke();
- }
- public bool OnMenuBack() {
- var is_interactable = btnFinish.interactable;
- if (is_interactable) Destroy(gameObject);
- return is_interactable;
- }
-
-
- void Update()
- {
- UpdateForGyr();
- UpdateForMag();
- }
- // HashSet<object> magerDebugSet = new HashSet<object>();
- // void OnGUI()
- // {
- // if (!AimHandler.ins) return;
- // GUIStyle labelFont = new GUIStyle();
- // labelFont.normal.textColor = new Color(1, 0.6f, 0.6f);
- // labelFont.fontSize = 40;
- // var m = ((Axis9Handler)AimHandler.ins.m_axisHandler)._9Axis.Attitude.MagCalibrater;
- // if (!magerDebugSet.Contains(m)) magerDebugSet.Add(m);
- // int mid = magerDebugSet.Count;
- // string str = $"地磁计ID:{mid}\nComplete:{m.Complete}\nVariance:{m.Variance}\nEllipsoidFitting==null?:{m.EllipsoidFitting==null}";
- // GUI.Label(new Rect(Screen.width/20,Screen.height/20,0,0), str, labelFont);
- // }
-
- void RefreshPage() {
- if (guide) {
- deviceCalibrateItem = deviceCalibrateItemForGuide[guideIndex];
- if (deviceCalibrateItemForGuide.Length == 1) {
- btnBack.gameObject.SetActive(false);
- btnNext.gameObject.SetActive(false);
- btnFinish.gameObject.SetActive(true);
- } else {
- if (guideIndex == 0) {
- btnBack.gameObject.SetActive(false);
- btnNext.gameObject.SetActive(true);
- btnFinish.gameObject.SetActive(false);
- } else if (guideIndex == deviceCalibrateItemForGuide.Length - 1) {
- btnBack.gameObject.SetActive(true);
- btnNext.gameObject.SetActive(false);
- btnFinish.gameObject.SetActive(true);
- } else {
- btnBack.gameObject.SetActive(true);
- btnNext.gameObject.SetActive(true);
- btnFinish.gameObject.SetActive(false);
- }
- }
- } else {
- btnBack.gameObject.SetActive(false);
- btnNext.gameObject.SetActive(false);
- btnFinish.gameObject.SetActive(true);
- }
- this.transform.Find("Gyr").gameObject.SetActive(deviceCalibrateItem == DeviceCalibrateItem.Gyr);
- this.transform.Find("Mag").gameObject.SetActive(deviceCalibrateItem == DeviceCalibrateItem.Mag);
- RefreshResetMagBtn();
- RefreshGyrBtn();
- if (deviceCalibrateItem == DeviceCalibrateItem.Gyr)
- RenderModuleCamera.ins?.SetMode(0);
- else
- RenderModuleCamera.ins?.SetMode(1);
- }
- /* ------ 新地磁计校准(2022-10-3) ------ */
- [NonSerialized] public bool calibrateMagDoing = false;
- float calibrateMagStartTime = 0; //地磁校准开始时刻
- void RefreshResetMagBtn()
- {
- if (calibrateMagDoing) {
- magReset.GetComponentInChildren<Text>().color = Color.red;
- magReset.GetComponentInChildren<TextAutoLanguage>().SetText(121);
- return;
- }
- Color outColor;
- ColorUtility.TryParseHtmlString("#005AB6", out outColor);
- magReset.GetComponentInChildren<Text>().color = outColor;
- if (AimHandler.ins.IsMagCompleted()) {
- magReset.GetComponentInChildren<TextAutoLanguage>().SetText(116);
- } else {
- magReset.GetComponentInChildren<TextAutoLanguage>().SetText(85);
- }
- }
- //在当前页面中,用户是否点击了开始校准并校准成功;有则flag=1
- [NonSerialized] public int flag_MagCalibarateOperateAndFinish = -1;
- public Action action_MagCalibarateOperateAndFinish;
- public Func<bool> action_OnClickMagCalibrateInterceptor;
- void ClickResetMag()
- {
- if (action_OnClickMagCalibrateInterceptor != null) {
- if (action_OnClickMagCalibrateInterceptor.Invoke()) return;
- }
- if (!calibrateMagDoing && BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess) {
- PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
- return;
- }
- calibrateMagDoing = !calibrateMagDoing;
- if (calibrateMagDoing) {
- calibrateMagStartTime = Time.realtimeSinceStartup;
- AimHandler.ins.ResetMag();
- flag_MagCalibarateOperateAndFinish = 0;
- } else {
- Axis9CalibrateRecord.ResumeCalibrateRecord(LoginMgr.myUserInfo.mac);
- Axis9CalibrateRecord.SetCalibrateOkRecord(LoginMgr.myUserInfo.mac, false);
- }
- interactableAllSkipBtns(!calibrateMagDoing);
- RefreshResetMagBtn();
- }
- //取消地磁计校准(仅用在UpdateForMag中)
- void CancelResetMag() {
- calibrateMagDoing = false;
- interactableAllSkipBtns(!calibrateMagDoing);
- RefreshResetMagBtn();
- }
- void UpdateForMag() {
- if (deviceCalibrateItem == DeviceCalibrateItem.Mag) {
- if (!AimHandler.ins.IsMagCompleted()) {
- progressMagCalibrate.gameObject.SetActive(true);
- magTipOk.SetActive(false);
- magTipOk2.SetActive(false);
- //地磁校准超时提示
- if (calibrateMagDoing) {
- float doingTime = Time.realtimeSinceStartup - calibrateMagStartTime;
- float progress = Mathf.FloorToInt(doingTime / 20 * 100);
- if (progress >= 100) {
- CancelResetMag();
- //地磁干扰提示页面-start
- GameObject o = Instantiate(prefab_MagInterferenceTipView);
- var nug = FindObjectOfType<NewUserGuider>(); //判断是否处于引导界面
- if (nug && nug.gameObject && nug.gameObject.activeSelf) {
- JCUnityLib.CanvasUtils.PlusSortOrder(nug.gameObject, o, 1);
- } else {
- JCUnityLib.CanvasUtils.PlusSortOrder(gameObject, o, 1);
- }
- //地磁干扰提示页面-end
- PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_please-leave-interfere"));
- } else {
- progressMagCalibrate.text = progress + "%";
- }
- } else {
- progressMagCalibrate.text = "0%";
- }
- } else {
- progressMagCalibrate.gameObject.SetActive(false);
- magTipOk.SetActive(true);
- //bool hasOkRecord = Axis9CalibrateRecord.HasCalibrateOkRecord(LoginMgr.myUserInfo.mac);
- //magTipOk.SetActive(hasOkRecord);
- //magTipOk2.SetActive(!hasOkRecord);
- if (calibrateMagDoing) {
- CancelResetMag();
- }
- if (flag_MagCalibarateOperateAndFinish == 0) {
- flag_MagCalibarateOperateAndFinish = 1;
- action_MagCalibarateOperateAndFinish?.Invoke();
- }
- }
- }
- }
- // ------ 陀螺仪校准 ------
- //在当前页面中,用户是否点击了开始校准并校准成功;有则flag=1
- [NonSerialized] public int flag_GyrCalibarateOperateAndFinish = -1;
- public Action action_GyrCalibarateOperateAndFinish;
- public Func<bool> action_OnClickGyrCalibrateInterceptor;
- [NonSerialized] public bool gyrCalibrating = false;
- void ClickGyrCalibrate()
- {
- if (action_OnClickGyrCalibrateInterceptor != null) {
- if (action_OnClickGyrCalibrateInterceptor.Invoke()) return;
- }
- if (!gyrCalibrating && BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess) {
- PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
- return;
- }
- gyrCalibrating = !gyrCalibrating;
- interactableAllSkipBtns(!gyrCalibrating);
- if (gyrCalibrating) {
- AimHandler.ins.gyrCalibrateCompleteCount = 0;
- canUpdateGyrCalibrateProgress = true;
- flag_GyrCalibarateOperateAndFinish = 0;
- } else {
- canUpdateGyrCalibrateProgress = false;
- }
- AimHandler.ins.ResetGyr();
- AimHandler.ins.CalibrateGyr(gyrCalibrating);
- RefreshGyrBtn();
- }
- void FinishGyrCalibrate()
- {
- gyrCalibrating = false;
- interactableAllSkipBtns(true);
- canUpdateGyrCalibrateProgress = false;
- AimHandler.ins.CalibrateGyr(false);
- StartCoroutine(AimHandler.ins.SaveGyr());
- RefreshGyrBtn();
- }
- bool canUpdateGyrCalibrateProgress = false;
- void UpdateForGyr()
- {
- if (deviceCalibrateItem != DeviceCalibrateItem.Gyr) return;
- if (canUpdateGyrCalibrateProgress) {
- int progress = AimHandler.ins.gyrCalibrateCompleteCount * 100 / AimHandler.ins.gyrCalibrateTotalCount;
- progressGyrCalibrate.text = progress + "%";
- if (progress >= 100) {
- FinishGyrCalibrate();
- if (flag_GyrCalibarateOperateAndFinish == 0) {
- flag_GyrCalibarateOperateAndFinish = 1;
- action_GyrCalibarateOperateAndFinish?.Invoke();
- }
- }
- } else {
- if (AimHandler.ins.IsGyrCompleted()) progressGyrCalibrate.text = "100%";
- else progressGyrCalibrate.text = "0%";
- }
- }
- void RefreshGyrBtn() {
- Button btn = btnGyrCalibrate.GetComponent<Button>();
- if (guide) {
- if (AimHandler.ins.IsGyrCompleted()) {
- btn.enabled = false;
- btn.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGray");
- btn.GetComponentInChildren<Text>().color = Color.gray;
- }
- } else {
- if (gyrCalibrating) {
- btn.GetComponentInChildren<TextAutoLanguage>().SetText(121);
- btn.GetComponentInChildren<Text>().color = Color.red;
- } else {
- btn.GetComponentInChildren<TextAutoLanguage>().SetText(AimHandler.ins.IsGyrCompleted() ? 116 : 85);
- Color outColor;
- ColorUtility.TryParseHtmlString("#005AB6", out outColor);
- btn.GetComponentInChildren<Text>().color = outColor;
- }
- }
- }
- //设置所有跳转按钮是否可交互
- private void interactableAllSkipBtns(bool value) {
- this.btnBack.interactable = value;
- this.btnNext.interactable = value;
- this.btnFinish.interactable = value;
- }
- }
- public enum DeviceCalibrateItem
- {
- Guide, Gyr, Mag
- }
|