GyrGuidanceView.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. public class GyrGuidanceView : MonoBehaviour
  7. {
  8. [SerializeField] Button btnGyrCalibrate;
  9. TextAutoLanguage2 _TextAutoLanguage2;
  10. [SerializeField] List<GameObject> layouts;
  11. bool bAutoNext = false;
  12. bool bGyrCompleted = false;
  13. bool bCanNavTo = false;
  14. void Start()
  15. {
  16. btnGyrCalibrate.GetComponent<Button>().onClick.AddListener(()=> {
  17. if (BluetoothAim.ins.getBLEPlayer() == BluetoothPlayer.FIRST_PLAYER)
  18. {
  19. ClickGyrCalibrate();
  20. }
  21. else if (BluetoothAim.ins.getBLEPlayer() == BluetoothPlayer.SECONDE_PLAYER)
  22. {
  23. ClickGyroCalibrate2P();
  24. }
  25. });
  26. transform.Find("BtnNext").GetComponent<Button>().onClick.AddListener(OnClick_Next);
  27. _TextAutoLanguage2 = btnGyrCalibrate.transform.Find("Text").GetComponent<TextAutoLanguage2>();
  28. if (BluetoothAim.ins.getBLEPlayer() == BluetoothPlayer.FIRST_PLAYER)
  29. {
  30. if (!AimHandler.ins.IsGyrCompleted())
  31. {
  32. _TextAutoLanguage2.SetTextKey("Gyro_Initialization");
  33. }
  34. else
  35. {
  36. _TextAutoLanguage2.SetTextKey("Gyro_Reinitialize");
  37. }
  38. }
  39. else if (BluetoothAim.ins.getBLEPlayer() == BluetoothPlayer.SECONDE_PLAYER)
  40. {
  41. if (!BluetoothAim.ins.getSmartBowHelper2P().IsGyrCompleted())
  42. {
  43. _TextAutoLanguage2.SetTextKey("Gyro_Initialization");
  44. }
  45. else
  46. {
  47. _TextAutoLanguage2.SetTextKey("Gyro_Reinitialize");
  48. }
  49. }
  50. if (AimHandler.ins.aimDeviceInfo.type == (int)AimDeviceType.ARTEMIS)
  51. {
  52. ShowDeviceLayout(1);
  53. }
  54. else
  55. {
  56. ShowDeviceLayout(0);
  57. }
  58. }
  59. void ShowDeviceLayout(int index)
  60. {
  61. for (int i = 0; i < layouts.Count; i++)
  62. {
  63. GameObject _button = layouts[i];
  64. _button.SetActive(index == i);
  65. }
  66. }
  67. IEnumerator SetCanNavTo() {
  68. yield return new WaitForSeconds(1.0f);
  69. bCanNavTo = true;
  70. }
  71. void Update()
  72. {
  73. if (BluetoothAim.ins.getBLEPlayer() == BluetoothPlayer.FIRST_PLAYER)
  74. {
  75. if (AimHandler.ins.IsGyrCompleted() && !bGyrCompleted && flag_GyrCalibarateOperateAndFinish != 0)
  76. {
  77. bGyrCompleted = true;
  78. //检测到已经初始化陀螺仪
  79. StartCoroutine(SetCanNavTo());
  80. }
  81. UpdateForGyr();
  82. }
  83. else if (BluetoothAim.ins.getBLEPlayer() == BluetoothPlayer.SECONDE_PLAYER) {
  84. if (BluetoothAim.ins.getSmartBowHelper2P().IsGyrCompleted() && !bGyrCompleted && flag_GyrCalibarateOperateAndFinish != 0)
  85. {
  86. bGyrCompleted = true;
  87. //检测到已经初始化陀螺仪
  88. StartCoroutine(SetCanNavTo());
  89. }
  90. UpdateCalibrateGyr2P();
  91. }
  92. }
  93. void OnClick_Next()
  94. {
  95. if (!bCanNavTo) return;
  96. if (BluetoothAim.ins.getBLEPlayer() == BluetoothPlayer.FIRST_PLAYER)
  97. {
  98. if (AimHandler.ins.IsGyrCompleted() && !gyrCalibrating)
  99. {
  100. ViewManager2.HideView(ViewManager2.Path_GyrGuidanceView);
  101. ViewManager2.ShowView(ViewManager2.Path_MagGuidanceView);
  102. }
  103. }
  104. else if (BluetoothAim.ins.getBLEPlayer() == BluetoothPlayer.SECONDE_PLAYER)
  105. {
  106. if (BluetoothAim.ins.getSmartBowHelper2P().IsGyrCompleted() && !gyrCalibrating)
  107. {
  108. ViewManager2.HideView(ViewManager2.Path_GyrGuidanceView);
  109. ViewManager2.ShowView(ViewManager2.Path_MagGuidanceView);
  110. }
  111. }
  112. }
  113. public void OnClick_Back()
  114. {
  115. AudioMgr.ins.PlayBtn();
  116. ViewManager2.HideView(ViewManager2.Path_GyrGuidanceView);
  117. }
  118. #region 陀螺仪校准逻辑
  119. [NonSerialized] public int flag_GyrCalibarateOperateAndFinish = -1;
  120. public Action action_GyrCalibarateOperateAndFinish;
  121. public Func<bool> action_OnClickGyrCalibrateInterceptor;
  122. [NonSerialized] public bool gyrCalibrating = false;
  123. void ClickGyrCalibrate()
  124. {
  125. if (action_OnClickGyrCalibrateInterceptor != null)
  126. {
  127. if (action_OnClickGyrCalibrateInterceptor.Invoke()) return;
  128. }
  129. if (!gyrCalibrating && BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess)
  130. {
  131. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
  132. return;
  133. }
  134. gyrCalibrating = !gyrCalibrating;
  135. if (gyrCalibrating)
  136. {
  137. bAutoNext = true;
  138. AimHandler.ins.gyrCalibrateCompleteCount = 0;
  139. canUpdateGyrCalibrateProgress = true;
  140. flag_GyrCalibarateOperateAndFinish = 0;
  141. }
  142. else
  143. {
  144. bAutoNext = false;
  145. canUpdateGyrCalibrateProgress = false;
  146. _TextAutoLanguage2.SetTextKey("Gyro_Reinitialize");
  147. }
  148. AimHandler.ins.ResetGyr();
  149. AimHandler.ins.CalibrateGyr(gyrCalibrating);
  150. }
  151. void FinishGyrCalibrate()
  152. {
  153. gyrCalibrating = false;
  154. canUpdateGyrCalibrateProgress = false;
  155. AimHandler.ins.CalibrateGyr(false);
  156. StartCoroutine(AimHandler.ins.SaveGyr());
  157. }
  158. bool canUpdateGyrCalibrateProgress = false;
  159. void UpdateForGyr()
  160. {
  161. if (canUpdateGyrCalibrateProgress)
  162. {
  163. int progress = AimHandler.ins.gyrCalibrateCompleteCount * 100 / AimHandler.ins.gyrCalibrateTotalCount;
  164. _TextAutoLanguage2.textFormatArgs = new object[] { progress };
  165. _TextAutoLanguage2.SetTextKey("Gyro_Initializing");
  166. if (progress >= 100)
  167. {
  168. FinishGyrCalibrate();
  169. if (flag_GyrCalibarateOperateAndFinish == 0)
  170. {
  171. flag_GyrCalibarateOperateAndFinish = 1;
  172. action_GyrCalibarateOperateAndFinish?.Invoke();
  173. if (AimHandler.ins.IsGyrCompleted())
  174. {
  175. _TextAutoLanguage2.SetTextKey("Gyro_Success");
  176. //如果是点击连接的,判断成功后自动跳转
  177. if (bAutoNext)
  178. {
  179. bAutoNext = false;
  180. OnClick_Next();
  181. }
  182. }
  183. else
  184. {
  185. //_TextAutoLanguage2.textFormatArgs = new object[] { };
  186. _TextAutoLanguage2.SetTextKey("Gyro_Initialization");
  187. }
  188. }
  189. }
  190. }
  191. }
  192. void ClickGyroCalibrate2P()
  193. {
  194. gyrCalibrating = !gyrCalibrating;
  195. if (gyrCalibrating)
  196. {
  197. bAutoNext = true;
  198. canUpdateGyrCalibrateProgress = true;
  199. flag_GyrCalibarateOperateAndFinish = 0;
  200. }
  201. else
  202. {
  203. bAutoNext = false;
  204. canUpdateGyrCalibrateProgress = false;
  205. _TextAutoLanguage2.SetTextKey("Gyro_Reinitialize");
  206. }
  207. BluetoothAim.ins.OnCalibrateGyr2P();
  208. }
  209. void UpdateCalibrateGyr2P()
  210. {
  211. //string act = BluetoothAim.ins.getSmartBowHelper2P().IsGyrCalibrating() ? "停止" : "开始";
  212. //text.text = $"点击{act}陀螺仪校准({progress}%)";
  213. if (canUpdateGyrCalibrateProgress)
  214. {
  215. int progress = BluetoothAim.ins.smartBowHelper2_Progress;
  216. _TextAutoLanguage2.textFormatArgs = new object[] { progress };
  217. _TextAutoLanguage2.SetTextKey("Gyro_Initializing");
  218. if (progress >= 100)
  219. {
  220. gyrCalibrating = false;
  221. canUpdateGyrCalibrateProgress = false;
  222. if (flag_GyrCalibarateOperateAndFinish == 0)
  223. {
  224. flag_GyrCalibarateOperateAndFinish = 1;
  225. action_GyrCalibarateOperateAndFinish?.Invoke();
  226. if (BluetoothAim.ins.getSmartBowHelper2P().IsGyrCompleted())
  227. {
  228. _TextAutoLanguage2.SetTextKey("Gyro_Success");
  229. //如果是点击连接的,判断成功后自动跳转
  230. if (bAutoNext)
  231. {
  232. bAutoNext = false;
  233. OnClick_Next();
  234. }
  235. }
  236. else
  237. {
  238. //_TextAutoLanguage2.textFormatArgs = new object[] { };
  239. _TextAutoLanguage2.SetTextKey("Gyro_Initialization");
  240. }
  241. }
  242. }
  243. }
  244. }
  245. #endregion
  246. }