DeviceCalibrateView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. /* 设备校准界面,公用(设备界面的校准、校准教程) */
  7. public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
  8. {
  9. [SerializeField] Button btnGyrCalibrate;
  10. [SerializeField] Text progressGyrCalibrate;
  11. [SerializeField] Text progressMagCalibrate;
  12. [SerializeField] GameObject prefab_MagInterferenceTipView;
  13. [SerializeField] GameObject magReset; //地磁校准按钮
  14. [SerializeField] GameObject magTipOk;
  15. [SerializeField] GameObject magTipOk2;
  16. [SerializeField] Button btnBack;
  17. [SerializeField] Button btnNext;
  18. [SerializeField] Button btnFinish;
  19. public Action action_OnDestroy;
  20. public DeviceCalibrateItem deviceCalibrateItem = DeviceCalibrateItem.Guide;
  21. DeviceCalibrateItem[] deviceCalibrateItemForGuide = {
  22. DeviceCalibrateItem.Gyr,
  23. DeviceCalibrateItem.Mag
  24. };
  25. int guideIndex = 0;
  26. public bool guide = true;
  27. public static DeviceCalibrateView ins;
  28. public static void Create(DeviceCalibrateItem itemType = DeviceCalibrateItem.Guide) {
  29. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/DeviceCalibrateView"));
  30. o.GetComponent<DeviceCalibrateView>().deviceCalibrateItem = itemType;
  31. o.GetComponent<DeviceCalibrateView>().guide = itemType == DeviceCalibrateItem.Guide;
  32. }
  33. void Awake() {
  34. ins = this;
  35. try {
  36. GlobalEventCenter.ins.onDeviceCalibrateViewAwakeChanged?.Invoke(true);
  37. } catch (System.Exception e) { Debug.LogError(e.Message); }
  38. RenderModuleCamera.Load();
  39. }
  40. void Start()
  41. {
  42. btnGyrCalibrate.GetComponent<Button>().onClick.AddListener(ClickGyrCalibrate);
  43. magReset.GetComponent<Button>().onClick.AddListener(ClickResetMag);
  44. btnBack.GetComponent<Button>().onClick.AddListener(delegate() {
  45. guideIndex--;
  46. RefreshPage();
  47. });
  48. btnNext.GetComponent<Button>().onClick.AddListener(delegate() {
  49. guideIndex++;
  50. RefreshPage();
  51. });
  52. btnFinish.GetComponent<Button>().onClick.AddListener(delegate() {
  53. Destroy(this.gameObject);
  54. if (guide && guideIndex == deviceCalibrateItemForGuide.Length - 1) {
  55. GameMgr.ins.FinishDeviceCalibrateGuide();
  56. }
  57. });
  58. if (guide) { //看看哪项没校准,就引导校准
  59. List<DeviceCalibrateItem> guideList = new List<DeviceCalibrateItem>();
  60. //九轴的需要校准引导,判断是否初始化陀螺仪和地磁计
  61. if (AimHandler.ins.bRuning9Axis())
  62. {
  63. if (!AimHandler.ins.IsGyrCompleted())
  64. {
  65. guideList.Add(DeviceCalibrateItem.Gyr);
  66. }
  67. if (!AimHandler.ins.IsMagCompleted())
  68. {
  69. guideList.Add(DeviceCalibrateItem.Mag);
  70. }
  71. }
  72. deviceCalibrateItemForGuide = guideList.ToArray();
  73. if (deviceCalibrateItemForGuide.Length == 0) {
  74. guideIndex = -1;
  75. btnFinish.GetComponent<Button>().onClick.Invoke();
  76. return;
  77. }
  78. }
  79. RefreshPage();
  80. if (!guide) {
  81. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  82. }
  83. }
  84. void OnDestroy()
  85. {
  86. if (ins == this) ins = null;
  87. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  88. try {
  89. GlobalEventCenter.ins.onDeviceCalibrateViewAwakeChanged?.Invoke(false);
  90. } catch (System.Exception e) { Debug.LogError(e.Message); }
  91. RenderModuleCamera.Unload();
  92. action_OnDestroy?.Invoke();
  93. }
  94. public bool OnMenuBack() {
  95. var is_interactable = btnFinish.interactable;
  96. if (is_interactable) Destroy(gameObject);
  97. return is_interactable;
  98. }
  99. void Update()
  100. {
  101. UpdateForGyr();
  102. UpdateForMag();
  103. }
  104. // HashSet<object> magerDebugSet = new HashSet<object>();
  105. // void OnGUI()
  106. // {
  107. // if (!AimHandler.ins) return;
  108. // GUIStyle labelFont = new GUIStyle();
  109. // labelFont.normal.textColor = new Color(1, 0.6f, 0.6f);
  110. // labelFont.fontSize = 40;
  111. // var m = ((Axis9Handler)AimHandler.ins.m_axisHandler)._9Axis.Attitude.MagCalibrater;
  112. // if (!magerDebugSet.Contains(m)) magerDebugSet.Add(m);
  113. // int mid = magerDebugSet.Count;
  114. // string str = $"地磁计ID:{mid}\nComplete:{m.Complete}\nVariance:{m.Variance}\nEllipsoidFitting==null?:{m.EllipsoidFitting==null}";
  115. // GUI.Label(new Rect(Screen.width/20,Screen.height/20,0,0), str, labelFont);
  116. // }
  117. void RefreshPage() {
  118. if (guide) {
  119. deviceCalibrateItem = deviceCalibrateItemForGuide[guideIndex];
  120. if (deviceCalibrateItemForGuide.Length == 1) {
  121. btnBack.gameObject.SetActive(false);
  122. btnNext.gameObject.SetActive(false);
  123. btnFinish.gameObject.SetActive(true);
  124. } else {
  125. if (guideIndex == 0) {
  126. btnBack.gameObject.SetActive(false);
  127. btnNext.gameObject.SetActive(true);
  128. btnFinish.gameObject.SetActive(false);
  129. } else if (guideIndex == deviceCalibrateItemForGuide.Length - 1) {
  130. btnBack.gameObject.SetActive(true);
  131. btnNext.gameObject.SetActive(false);
  132. btnFinish.gameObject.SetActive(true);
  133. } else {
  134. btnBack.gameObject.SetActive(true);
  135. btnNext.gameObject.SetActive(true);
  136. btnFinish.gameObject.SetActive(false);
  137. }
  138. }
  139. } else {
  140. btnBack.gameObject.SetActive(false);
  141. btnNext.gameObject.SetActive(false);
  142. btnFinish.gameObject.SetActive(true);
  143. }
  144. this.transform.Find("Gyr").gameObject.SetActive(deviceCalibrateItem == DeviceCalibrateItem.Gyr);
  145. this.transform.Find("Mag").gameObject.SetActive(deviceCalibrateItem == DeviceCalibrateItem.Mag);
  146. RefreshResetMagBtn();
  147. RefreshGyrBtn();
  148. if (deviceCalibrateItem == DeviceCalibrateItem.Gyr)
  149. RenderModuleCamera.ins?.SetMode(0);
  150. else
  151. RenderModuleCamera.ins?.SetMode(1);
  152. }
  153. /* ------ 新地磁计校准(2022-10-3) ------ */
  154. [NonSerialized] public bool calibrateMagDoing = false;
  155. float calibrateMagStartTime = 0; //地磁校准开始时刻
  156. void RefreshResetMagBtn()
  157. {
  158. if (calibrateMagDoing) {
  159. magReset.GetComponentInChildren<Text>().color = Color.red;
  160. magReset.GetComponentInChildren<TextAutoLanguage>().SetText(121);
  161. return;
  162. }
  163. Color outColor;
  164. ColorUtility.TryParseHtmlString("#005AB6", out outColor);
  165. magReset.GetComponentInChildren<Text>().color = outColor;
  166. if (AimHandler.ins.IsMagCompleted()) {
  167. magReset.GetComponentInChildren<TextAutoLanguage>().SetText(116);
  168. } else {
  169. magReset.GetComponentInChildren<TextAutoLanguage>().SetText(85);
  170. }
  171. }
  172. //在当前页面中,用户是否点击了开始校准并校准成功;有则flag=1
  173. [NonSerialized] public int flag_MagCalibarateOperateAndFinish = -1;
  174. public Action action_MagCalibarateOperateAndFinish;
  175. public Func<bool> action_OnClickMagCalibrateInterceptor;
  176. void ClickResetMag()
  177. {
  178. if (action_OnClickMagCalibrateInterceptor != null) {
  179. if (action_OnClickMagCalibrateInterceptor.Invoke()) return;
  180. }
  181. if (!calibrateMagDoing && BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess) {
  182. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
  183. return;
  184. }
  185. calibrateMagDoing = !calibrateMagDoing;
  186. if (calibrateMagDoing) {
  187. calibrateMagStartTime = Time.realtimeSinceStartup;
  188. AimHandler.ins.ResetMag();
  189. flag_MagCalibarateOperateAndFinish = 0;
  190. } else {
  191. Axis9CalibrateRecord.ResumeCalibrateRecord(LoginMgr.myUserInfo.mac);
  192. Axis9CalibrateRecord.SetCalibrateOkRecord(LoginMgr.myUserInfo.mac, false);
  193. }
  194. interactableAllSkipBtns(!calibrateMagDoing);
  195. RefreshResetMagBtn();
  196. }
  197. //取消地磁计校准(仅用在UpdateForMag中)
  198. void CancelResetMag() {
  199. calibrateMagDoing = false;
  200. interactableAllSkipBtns(!calibrateMagDoing);
  201. RefreshResetMagBtn();
  202. }
  203. void UpdateForMag() {
  204. if (deviceCalibrateItem == DeviceCalibrateItem.Mag) {
  205. if (!AimHandler.ins.IsMagCompleted()) {
  206. progressMagCalibrate.gameObject.SetActive(true);
  207. magTipOk.SetActive(false);
  208. magTipOk2.SetActive(false);
  209. //地磁校准超时提示
  210. if (calibrateMagDoing) {
  211. float doingTime = Time.realtimeSinceStartup - calibrateMagStartTime;
  212. float progress = Mathf.FloorToInt(doingTime / 20 * 100);
  213. if (progress >= 100) {
  214. CancelResetMag();
  215. //地磁干扰提示页面-start
  216. GameObject o = Instantiate(prefab_MagInterferenceTipView);
  217. var nug = FindObjectOfType<NewUserGuider>(); //判断是否处于引导界面
  218. if (nug && nug.gameObject && nug.gameObject.activeSelf) {
  219. JCUnityLib.CanvasUtils.PlusSortOrder(nug.gameObject, o, 1);
  220. } else {
  221. JCUnityLib.CanvasUtils.PlusSortOrder(gameObject, o, 1);
  222. }
  223. //地磁干扰提示页面-end
  224. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_please-leave-interfere"));
  225. } else {
  226. progressMagCalibrate.text = progress + "%";
  227. }
  228. } else {
  229. progressMagCalibrate.text = "0%";
  230. }
  231. } else {
  232. progressMagCalibrate.gameObject.SetActive(false);
  233. magTipOk.SetActive(true);
  234. //bool hasOkRecord = Axis9CalibrateRecord.HasCalibrateOkRecord(LoginMgr.myUserInfo.mac);
  235. //magTipOk.SetActive(hasOkRecord);
  236. //magTipOk2.SetActive(!hasOkRecord);
  237. if (calibrateMagDoing) {
  238. CancelResetMag();
  239. }
  240. if (flag_MagCalibarateOperateAndFinish == 0) {
  241. flag_MagCalibarateOperateAndFinish = 1;
  242. action_MagCalibarateOperateAndFinish?.Invoke();
  243. }
  244. }
  245. }
  246. }
  247. // ------ 陀螺仪校准 ------
  248. //在当前页面中,用户是否点击了开始校准并校准成功;有则flag=1
  249. [NonSerialized] public int flag_GyrCalibarateOperateAndFinish = -1;
  250. public Action action_GyrCalibarateOperateAndFinish;
  251. public Func<bool> action_OnClickGyrCalibrateInterceptor;
  252. [NonSerialized] public bool gyrCalibrating = false;
  253. void ClickGyrCalibrate()
  254. {
  255. if (action_OnClickGyrCalibrateInterceptor != null) {
  256. if (action_OnClickGyrCalibrateInterceptor.Invoke()) return;
  257. }
  258. if (!gyrCalibrating && BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess) {
  259. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
  260. return;
  261. }
  262. gyrCalibrating = !gyrCalibrating;
  263. interactableAllSkipBtns(!gyrCalibrating);
  264. if (gyrCalibrating) {
  265. AimHandler.ins.gyrCalibrateCompleteCount = 0;
  266. canUpdateGyrCalibrateProgress = true;
  267. flag_GyrCalibarateOperateAndFinish = 0;
  268. } else {
  269. canUpdateGyrCalibrateProgress = false;
  270. }
  271. AimHandler.ins.ResetGyr();
  272. AimHandler.ins.CalibrateGyr(gyrCalibrating);
  273. RefreshGyrBtn();
  274. }
  275. void FinishGyrCalibrate()
  276. {
  277. gyrCalibrating = false;
  278. interactableAllSkipBtns(true);
  279. canUpdateGyrCalibrateProgress = false;
  280. AimHandler.ins.CalibrateGyr(false);
  281. StartCoroutine(AimHandler.ins.SaveGyr());
  282. RefreshGyrBtn();
  283. }
  284. bool canUpdateGyrCalibrateProgress = false;
  285. void UpdateForGyr()
  286. {
  287. if (deviceCalibrateItem != DeviceCalibrateItem.Gyr) return;
  288. if (canUpdateGyrCalibrateProgress) {
  289. int progress = AimHandler.ins.gyrCalibrateCompleteCount * 100 / AimHandler.ins.gyrCalibrateTotalCount;
  290. progressGyrCalibrate.text = progress + "%";
  291. if (progress >= 100) {
  292. FinishGyrCalibrate();
  293. if (flag_GyrCalibarateOperateAndFinish == 0) {
  294. flag_GyrCalibarateOperateAndFinish = 1;
  295. action_GyrCalibarateOperateAndFinish?.Invoke();
  296. }
  297. }
  298. } else {
  299. if (AimHandler.ins.IsGyrCompleted()) progressGyrCalibrate.text = "100%";
  300. else progressGyrCalibrate.text = "0%";
  301. }
  302. }
  303. void RefreshGyrBtn() {
  304. Button btn = btnGyrCalibrate.GetComponent<Button>();
  305. if (guide) {
  306. if (AimHandler.ins.IsGyrCompleted()) {
  307. btn.enabled = false;
  308. btn.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGray");
  309. btn.GetComponentInChildren<Text>().color = Color.gray;
  310. }
  311. } else {
  312. if (gyrCalibrating) {
  313. btn.GetComponentInChildren<TextAutoLanguage>().SetText(121);
  314. btn.GetComponentInChildren<Text>().color = Color.red;
  315. } else {
  316. btn.GetComponentInChildren<TextAutoLanguage>().SetText(AimHandler.ins.IsGyrCompleted() ? 116 : 85);
  317. Color outColor;
  318. ColorUtility.TryParseHtmlString("#005AB6", out outColor);
  319. btn.GetComponentInChildren<Text>().color = outColor;
  320. }
  321. }
  322. }
  323. //设置所有跳转按钮是否可交互
  324. private void interactableAllSkipBtns(bool value) {
  325. this.btnBack.interactable = value;
  326. this.btnNext.interactable = value;
  327. this.btnFinish.interactable = value;
  328. }
  329. }
  330. public enum DeviceCalibrateItem
  331. {
  332. Guide, Gyr, Mag
  333. }