DeviceCalibrateView.cs 13 KB

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