BluetoothVliew.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class BluetoothVliew : PinBallUIBase
  6. {
  7. [SerializeField] GameObject btnConnect;
  8. [SerializeField] GameObject btnGyrCalibrate;
  9. [SerializeField] GameObject btnMagCalibrate;
  10. [SerializeField] Text textGyrCalibrate;
  11. [SerializeField] Text textMagCalibrate;
  12. void Start()
  13. {
  14. UIEventListener.Get(btnConnect).onClick = OnClick;
  15. UIEventListener.Get(btnGyrCalibrate).onClick = OnClick;
  16. UIEventListener.Get(btnMagCalibrate).onClick = OnClick;
  17. }
  18. void Update()
  19. {
  20. UpdateBtnForConnect();
  21. UpdateForGyrCalibrate();
  22. UpdateForMagCalibrate();
  23. }
  24. void OnDestroy()
  25. {
  26. if (gyrCalibrating && AimHandler.ins)
  27. {
  28. AimHandler.ins.CalibrateGyr(false);
  29. AimHandler.ins.ResetGyr();
  30. }
  31. }
  32. void OnClick(GameObject o)
  33. {
  34. if (o.Equals(btnConnect)) BluetoothAim.ins.DoConnect();
  35. else if (o.Equals(btnGyrCalibrate)) OnClick_BtnGyrCalibrate();
  36. else if (o.Equals(btnMagCalibrate)) OnClick_BtnMagCalibrate();
  37. }
  38. bool gyrCalibrating;
  39. void OnClick_BtnGyrCalibrate()
  40. {
  41. if (!gyrCalibrating && BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess)
  42. {
  43. PopupTip.Show("请先连接模块");
  44. return;
  45. }
  46. if (magCalibrating)
  47. {
  48. PopupTip.Show("地磁计正在校准,请勿同时操作!");
  49. return;
  50. }
  51. gyrCalibrating = !gyrCalibrating;
  52. if (gyrCalibrating)
  53. {
  54. AimHandler.ins.gyrCalibrateCompleteCount = 0;
  55. }
  56. AimHandler.ins.CalibrateGyr(gyrCalibrating);
  57. AimHandler.ins.ResetGyr();
  58. }
  59. bool magCalibrating;
  60. void OnClick_BtnMagCalibrate()
  61. {
  62. if (!magCalibrating && BluetoothAim.ins.status != BluetoothStatusEnum.ConnectSuccess)
  63. {
  64. PopupTip.Show("请先连接模块");
  65. return;
  66. }
  67. if (gyrCalibrating)
  68. {
  69. PopupTip.Show("陀螺仪正在校准,请勿同时操作!");
  70. return;
  71. }
  72. magCalibrating = !magCalibrating;
  73. if (magCalibrating)
  74. {
  75. magCalibrateStartTime = Time.realtimeSinceStartup;
  76. AimHandler.ins.ResetMag();
  77. }
  78. }
  79. BluetoothStatusEnum bowStatus;
  80. void UpdateBtnForConnect()
  81. {
  82. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status)
  83. {
  84. bowStatus = BluetoothAim.ins.status;
  85. (string textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  86. btnConnect.GetComponentInChildren<Text>().text = textID;
  87. btnConnect.GetComponentInChildren<Text>().color = color;
  88. }
  89. }
  90. void UpdateForGyrCalibrate()
  91. {
  92. int progress = 0;
  93. if (gyrCalibrating)
  94. {
  95. progress = AimHandler.ins.gyrCalibrateCompleteCount * 100 / AimHandler.ins.gyrCalibrateTotalCount;
  96. if (progress >= 100)
  97. {
  98. progress = 100;
  99. gyrCalibrating = false;
  100. AimHandler.ins.CalibrateGyr(false);
  101. StartCoroutine(AimHandler.ins.SaveGyr());
  102. PopupTip.Show("陀螺仪校准完成");
  103. }
  104. }
  105. else if (AimHandler.ins.IsGyrCompleted())
  106. {
  107. progress = 100;
  108. }
  109. textGyrCalibrate.text = progress + "%";
  110. btnGyrCalibrate.GetComponentInChildren<Text>().text = (gyrCalibrating ? "停止" : "开始") + "-陀螺仪校准";
  111. }
  112. float magCalibrateStartTime;
  113. void UpdateForMagCalibrate()
  114. {
  115. int progress = 0;
  116. if (AimHandler.ins.IsMagCompleted())
  117. {
  118. progress = 100;
  119. if (magCalibrating) magCalibrating = false;
  120. }
  121. else if (magCalibrating)
  122. {
  123. progress = Mathf.FloorToInt((Time.realtimeSinceStartup - magCalibrateStartTime) / 20 * 100);
  124. if (progress >= 100)
  125. {
  126. progress = 0;
  127. magCalibrating = false;
  128. PopupTip.Show("存在磁场干扰,请远离电子设备后再进行校准!");
  129. }
  130. }
  131. textMagCalibrate.text = $"{progress}%";
  132. btnMagCalibrate.GetComponentInChildren<Text>().text = (magCalibrating ? "停止" : "开始") + "-地磁计校准";
  133. }
  134. }