DeviceView_ItemShow.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class DeviceView_ItemShow : MonoBehaviour
  7. {
  8. [Tooltip("当前选择的用户设备")]
  9. [SerializeField]
  10. BluetoothPlayer bluetoothPlayer = BluetoothPlayer.FIRST_PLAYER;
  11. [SerializeField] Image selectPanel;
  12. [SerializeField] Button btnBg;
  13. [SerializeField] Button btnConnectBow;
  14. [SerializeField] Button ConnectButton;
  15. [SerializeField] Image ConnectButtonImage;
  16. [SerializeField] Sprite[] ConnectButtonImages;
  17. [SerializeField] GameObject process;
  18. [SerializeField] GameObject connected;
  19. [SerializeField] GameObject unConnected;
  20. [SerializeField] Text titleText;
  21. [SerializeField] Text statusText;
  22. [SerializeField] GameObject initStatus;
  23. [SerializeField] GameObject bettery;
  24. [SerializeField] GameObject betteryBar;
  25. [SerializeField] GameObject betteryValue;
  26. [SerializeField] Image pointImage;
  27. [SerializeField] Sprite[] pointImages;
  28. [SerializeField] Image rectangle;
  29. [SerializeField] Sprite[] rectangles;
  30. [SerializeField] Text initStatusText;
  31. [SerializeField] Image bowBg;
  32. [SerializeField] Sprite[] bowBgs;
  33. [SerializeField] Sprite[] gunBgs;
  34. float countingTime1 = 5;
  35. BluetoothStatusEnum bowStatus;
  36. [SerializeField] TextAutoLanguage2 _TextAutoLanguage2;
  37. //这个是处理设备下标
  38. [SerializeField] int deviceIndex = 0;
  39. //是否存在连接信息。
  40. bool bShowInfo = false;
  41. // Start is called before the first frame update
  42. // bool bProcess = false;
  43. [SerializeField] Text omitText;
  44. private int dotCount = 0;
  45. private float interval = 0.5f; // 标点更新间隔
  46. private float timer = 0f;
  47. private void Awake()
  48. {
  49. AimHandler.ins.aimDeviceInfoChangeEvent += onShowDeviceInfo;
  50. }
  51. void Start()
  52. { //panel 按钮,选择状态。主要控制getPanelStatus
  53. btnBg.onClick.AddListener(OnSelectEvent);
  54. //addBtn 按钮
  55. //btnConnectBow.onClick.AddListener(delegate ()
  56. //{
  57. // AudioMgr.ins.PlayBtn();
  58. // //要选中情况下,才能进行下一步操作
  59. // if (!getPanelStatus()) return;
  60. // AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  61. // AimHandler.ins.onCreateTempAimDeviceInfo();
  62. // //ViewMgr.Instance.ShowView<SmartArcheryView>();
  63. // if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  64. // {
  65. // ViewMgr.Instance.ShowView<SmartArcheryView>();
  66. // }
  67. // else if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  68. // {
  69. // ViewMgr.Instance.ShowView<SmartGunView>();
  70. // }
  71. // else
  72. // {
  73. // Debug.LogError("btnConnectBow 没有设置页面!!");
  74. // }
  75. //});
  76. //init 按钮(有硬件信息才显示initStatus按钮)
  77. //initStatus.GetComponent<Button>().onClick.AddListener(delegate ()
  78. //{
  79. // AudioMgr.ins.PlayBtn();
  80. // //要选中情况下,才能进行下一步操作
  81. // if (!getPanelStatus()) return;
  82. // //选择此device index 跳转页面
  83. // AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  84. // AimHandler.ins.onCreateTempAimDeviceInfo();
  85. // if (GlobalData.MyDeviceMode == DeviceMode.Archery)
  86. // {
  87. // //判断当前的信息是否是 HOUYI PRO,是的话直接跳转到pro初始化?
  88. // if (AimHandler.ins.isHOUYIPRO(bluetoothPlayer))
  89. // {
  90. // //直接进入红外引导界面
  91. // AimHandler.ins.SetTempAimDeviceType(AimDeviceType.HOUYIPRO);
  92. // ViewManager2.ShowView(ViewManager2.Path_InfraredView);
  93. // } else if (AimHandler.ins.isARTEMISPro(bluetoothPlayer)) {
  94. // //直接进入红外引导界面
  95. // AimHandler.ins.SetTempAimDeviceType(AimDeviceType.ARTEMISPRO);
  96. // ViewManager2.ShowView(ViewManager2.Path_InfraredView);
  97. // }
  98. // else
  99. // {
  100. // ViewMgr.Instance.ShowView<SmartArcheryView>();
  101. // }
  102. // }
  103. // else if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  104. // {
  105. // ViewMgr.Instance.ShowView<SmartGunView>();
  106. // }
  107. // else {
  108. // Debug.LogError("initStatus 没有设置页面!!");
  109. // }
  110. //});
  111. //ConnectButton.onClick.AddListener(delegate ()
  112. //{
  113. // AudioMgr.ins.PlayBtn();
  114. // OnConnectedEvent();
  115. //});
  116. //进入页面更新一次
  117. if (BluetoothAim.ins)
  118. {
  119. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  120. {
  121. if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  122. {
  123. //连接后,更新电池状态
  124. RenderBattery(DeviceBatteryView.ins.batteryDeviceID, DeviceBatteryView.ins.batteryValue);
  125. if (unConnected.activeSelf) unConnected.SetActive(false);
  126. }
  127. }
  128. else
  129. {
  130. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  131. if (smartBowHelper != null &&
  132. smartBowHelper.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected &&
  133. BluetoothAim.ins.get2PBattery() > 0)
  134. {
  135. //连接后,更新电池状态
  136. RenderBattery(-1, BluetoothAim.ins.get2PBattery());
  137. }
  138. }
  139. }
  140. onUpdateStatusInfo();
  141. }
  142. private void OnDestroy()
  143. {
  144. AimHandler.ins.aimDeviceInfoChangeEvent -= onShowDeviceInfo;
  145. }
  146. // Update is called once per frame
  147. void Update()
  148. {
  149. if (BluetoothAim.ins)
  150. {
  151. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  152. {
  153. if (BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  154. {
  155. //if (!connected.activeSelf) connected.SetActive(true);
  156. //if (unConnected.activeSelf) unConnected.SetActive(false);
  157. //连接后,更新电池状态
  158. if (countingTime1 < 5)
  159. {
  160. countingTime1 += Time.deltaTime;
  161. }
  162. else
  163. {
  164. countingTime1 = 0;
  165. RenderBattery(DeviceBatteryView.ins.batteryDeviceID, DeviceBatteryView.ins.batteryValue);
  166. }
  167. }
  168. else if (BluetoothAim.ins.status != BluetoothStatusEnum.Connecting) //BluetoothAim.ins.status == BluetoothStatusEnum.ConnectFail
  169. {
  170. //如果连接失败,重置一下状态
  171. onShowDeviceInfo();
  172. RenderBattery(-1, 0);
  173. }
  174. else
  175. {
  176. //连接状态或者正在连接等
  177. RenderBattery(-1, 0);
  178. onShowDeviceInfo();
  179. }
  180. }
  181. else if (bluetoothPlayer == BluetoothPlayer.SECONDE_PLAYER)
  182. {
  183. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  184. if (smartBowHelper == null) return;
  185. SmartBowSDK.BluetoothStatusEnum bluetoothStatusEnum = smartBowHelper.GetBluetoothStatus();
  186. //Debug.Log(bluetoothStatusEnum);
  187. if (bluetoothStatusEnum == SmartBowSDK.BluetoothStatusEnum.Connected)
  188. {
  189. //if (!connected.activeSelf) connected.SetActive(true);
  190. //if (unConnected.activeSelf) unConnected.SetActive(false);
  191. //连接后,更新电池状态
  192. if (countingTime1 < 5)
  193. {
  194. countingTime1 += Time.deltaTime;
  195. }
  196. else
  197. {
  198. countingTime1 = 0;
  199. RenderBattery(-1, BluetoothAim.ins.getSmartBowHelper2P().GetBattery());
  200. }
  201. }
  202. else if (bluetoothStatusEnum != SmartBowSDK.BluetoothStatusEnum.Connecting)//bluetoothStatusEnum == SmartBowSDK.BluetoothStatusEnum.None
  203. {
  204. //如果连接失败,重置一下状态
  205. onShowDeviceInfo();
  206. RenderBattery(-1, 0);
  207. }
  208. else
  209. {
  210. //连接状态或者正在连接等
  211. RenderBattery(-1, 0);
  212. onShowDeviceInfo();
  213. }
  214. }
  215. if (connected.activeSelf) onUpdateStatusInfo();
  216. }
  217. //如果存在连接信息
  218. if (bShowInfo)
  219. {
  220. if (unConnected.activeSelf) unConnected.SetActive(false);
  221. if (!connected.activeSelf) connected.SetActive(true);
  222. }
  223. else
  224. {
  225. if (!unConnected.activeSelf) unConnected.SetActive(true);
  226. if (connected.activeSelf) connected.SetActive(false);
  227. }
  228. }
  229. void UpdateLoadingText()
  230. {
  231. timer += Time.deltaTime;
  232. // 每隔 `interval` 秒更新一次标点符号
  233. if (timer >= interval)
  234. {
  235. dotCount = (dotCount + 1) % 4; // 标点数量从 0 到 3 循环
  236. omitText.text = new string('.', dotCount); // 添加相应数量的点
  237. timer = 0f; // 重置计时器
  238. }
  239. }
  240. // 停止加载效果
  241. public void StopLoading()
  242. {
  243. dotCount = 0;
  244. omitText.text = ""; // 停止时恢复为基础文本
  245. }
  246. void RequestBatteryForBow()
  247. {
  248. try
  249. {
  250. if (BluetoothAim.ins.hasData && JCUnityLib.TimeUtils.GetTimestamp() - BluetoothAim.ins.hasDataTime > 5000)
  251. {
  252. BluetoothAim.ins.RequestBattery();
  253. }
  254. }
  255. catch (Exception) { }
  256. }
  257. public void RenderBattery(int deviceID, int value)
  258. {
  259. //if (!bettery.activeSelf) bettery.SetActive(true);
  260. Image img = betteryBar.GetComponent<Image>();
  261. Text txt = betteryValue.GetComponent<Text>();
  262. img.fillAmount = value / 100f;
  263. txt.text = value + "%";
  264. }
  265. public void onShowDeviceInfo()
  266. {
  267. //判断是否存在当前对象
  268. List<AimDeviceInfo> allDeviceInfo = AimHandler.ins.aimDeviceInfos.arry;
  269. AimDeviceInfo aimDeviceInfo = allDeviceInfo.Find(obj => obj.id == deviceIndex);
  270. //对象不存在
  271. if (aimDeviceInfo == null || aimDeviceInfo.type == -1 || !aimDeviceInfo.bInitMac)//AimHandler.ins.aimDeviceInfos.arry.Count <= deviceIndex
  272. {
  273. if (!unConnected.activeSelf) unConnected.SetActive(true);
  274. if (connected.activeSelf) connected.SetActive(false);
  275. return;
  276. }
  277. //Debug.Log("show aimDeviceInfo:" + aimDeviceInfo.id + ", deviceSelectIndex:" + deviceIndex);
  278. //AimDeviceInfo aimDeviceInfo = AimHandler.ins.aimDeviceInfos.arry[deviceIndex];
  279. bShowInfo = true;
  280. switch ((AimDeviceType)aimDeviceInfo.type)
  281. {
  282. case AimDeviceType.HOUYI:
  283. titleText.text = "HOUYI";
  284. bowBg.sprite = bowBgs[0];
  285. break;
  286. case AimDeviceType.HOUYI2:
  287. titleText.text = "HOUYI 2";
  288. bowBg.sprite = bowBgs[0];
  289. break;
  290. case AimDeviceType.ARTEMIS:
  291. titleText.text = "ARTEMIS";
  292. bowBg.sprite = bowBgs[1];
  293. break;
  294. case AimDeviceType.HOUYIPRO:
  295. titleText.text = "HOUYI Pro";
  296. bowBg.sprite = bowBgs[0];
  297. break;
  298. case AimDeviceType.Gun:
  299. titleText.text = "Pistol M9";
  300. bowBg.sprite = gunBgs[0];
  301. break;
  302. case AimDeviceType.ARTEMISPRO:
  303. titleText.text = "ARTEMIS Pro";
  304. bowBg.sprite = bowBgs[1];
  305. break;
  306. default:
  307. bShowInfo = false;
  308. // Debug.LogWarning("onShowDeviceInfo 没有对应设备 AimDeviceType:" + ((AimDeviceType)aimDeviceInfo.type).ToString());
  309. break;
  310. }
  311. //onUpdateStatusInfo();
  312. }
  313. //刷新状态
  314. public void onUpdateStatusInfo()
  315. {
  316. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  317. {
  318. string name = BluetoothStatus.GetStatusTextKey(BluetoothAim.ins.status);
  319. if (name != _TextAutoLanguage2.GetTextKey())
  320. _TextAutoLanguage2.SetTextKey(name);
  321. if (BluetoothAim.ins.status == BluetoothStatusEnum.Connecting)
  322. {
  323. omitText.gameObject.SetActive(true);
  324. UpdateLoadingText();
  325. }
  326. else
  327. {
  328. StopLoading();
  329. }
  330. }
  331. else if (bluetoothPlayer == BluetoothPlayer.SECONDE_PLAYER)
  332. {
  333. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  334. if (smartBowHelper == null) return;
  335. string name = BluetoothStatus.GetBLE2StatusTextKey(smartBowHelper.GetBluetoothStatus());
  336. if (name != _TextAutoLanguage2.GetTextKey())
  337. _TextAutoLanguage2.SetTextKey(name);
  338. if (smartBowHelper.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connecting)
  339. {
  340. omitText.gameObject.SetActive(true);
  341. UpdateLoadingText();
  342. }
  343. else
  344. {
  345. StopLoading();
  346. }
  347. }
  348. }
  349. //设置一个面板选择状态
  350. public void setPanelStatus(bool _active)
  351. {
  352. selectPanel.enabled = _active;
  353. if (_active)
  354. {
  355. //赋值一下当前选择的对象
  356. DeviceViewInfrared.ins.selectDeviceViewItem = this;
  357. //选择框选择到的当前蓝牙用户
  358. BluetoothAim.ins.setBLEPlayer(bluetoothPlayer);
  359. Debug.Log("当前选择的操作蓝牙用户:" + BluetoothAim.ins.getBLEPlayer());
  360. //true的情况下设置index
  361. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  362. Debug.Log("当前选择的操作蓝牙用户 deviceSelectIndex:" + deviceIndex);
  363. }
  364. }
  365. public bool getPanelStatus()
  366. {
  367. bool _enable = selectPanel.enabled;
  368. //设置一次
  369. DeviceViewInfrared.ins.OnCloseAllPanelStatus();
  370. setPanelStatus(true);
  371. return _enable;
  372. }
  373. public bool getCurrentPanelEnable()
  374. {
  375. return selectPanel.enabled;
  376. }
  377. public bool getBLEConnectState() {
  378. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER) {
  379. return BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess;
  380. }
  381. else if (bluetoothPlayer == BluetoothPlayer.SECONDE_PLAYER)
  382. {
  383. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  384. if (smartBowHelper == null) return false;
  385. SmartBowSDK.BluetoothStatusEnum bluetoothStatusEnum = smartBowHelper.GetBluetoothStatus();
  386. return bluetoothStatusEnum == SmartBowSDK.BluetoothStatusEnum.Connected;
  387. }else return false;
  388. }
  389. /// <summary>
  390. /// 蓝牙连接事件,连接和断开连接
  391. /// </summary>
  392. public void OnConnectedEvent()
  393. {
  394. //要选中情况下,才能进行下一步操作
  395. if (!getPanelStatus()) return;
  396. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  397. AimHandler.ins.onCreateAimDeviceInfoById();
  398. Debug.Log("DeviceView_ItemShow bluetoothPlayer:" + bluetoothPlayer);
  399. if (BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  400. {
  401. //断开连接
  402. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  403. {
  404. BluetoothAim.ins.DoConnect();
  405. }
  406. else
  407. {
  408. BluetoothAim.ins.DoConnect2P();
  409. }
  410. return;
  411. }
  412. if (HomeView.ShowProminentBeforeConnectBLE()) return;
  413. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  414. {
  415. BluetoothAim.ins.DoConnect();
  416. }
  417. else
  418. {
  419. BluetoothAim.ins.DoConnect2P();
  420. }
  421. }
  422. /// <summary>
  423. /// 断开蓝牙连接
  424. /// </summary>
  425. public void OnDisConnectedEvent()
  426. {
  427. //要选中情况下,才能进行下一步操作
  428. if (!getPanelStatus()) return;
  429. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  430. AimHandler.ins.onCreateAimDeviceInfoById();
  431. Debug.Log("OnDisConnectedEvent bluetoothPlayer:" + bluetoothPlayer);
  432. //断开连接
  433. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  434. {
  435. if (BluetoothAim.ins) {
  436. if(BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  437. BluetoothAim.ins.DoConnect();
  438. else BluetoothAim.ins.connectCanceled = true; // 退出自动连接,即时正在连接
  439. }
  440. }
  441. else
  442. {
  443. if (BluetoothAim.ins && BluetoothAim.ins.getSmartBowHelper2P().GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected) {
  444. BluetoothAim.ins.bNoNeedToReconnect = false;
  445. BluetoothAim.ins.DoConnect2P();
  446. }
  447. }
  448. }
  449. /// <summary>
  450. /// 选择事件
  451. /// </summary>
  452. public void OnSelectEvent() {
  453. //如果是选中的状态,不再触发
  454. if (getCurrentPanelEnable()) return;
  455. AudioMgr.ins.PlayBtn();
  456. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  457. DeviceViewInfrared.ins.OnCloseAllPanelStatus();
  458. setPanelStatus(true);
  459. }
  460. /// <summary>
  461. /// 如果有选择到并且显示的话
  462. /// </summary>
  463. /// <returns></returns>
  464. public bool getCurrentItemVisible()
  465. {
  466. return selectPanel.enabled && bShowInfo;
  467. }
  468. /// <summary>
  469. /// 判断是不是红外设备
  470. /// </summary>
  471. /// <returns></returns>
  472. public bool bInfraredDevices() {
  473. bool bInfrared = false;
  474. List<AimDeviceInfo> allDeviceInfo = AimHandler.ins.aimDeviceInfos.arry;
  475. AimDeviceInfo aimDeviceInfo = allDeviceInfo.Find(obj => obj.id == deviceIndex);
  476. if (aimDeviceInfo == null || aimDeviceInfo.type == -1 || !aimDeviceInfo.bInitMac)//AimHandler.ins.aimDeviceInfos.arry.Count <= deviceIndex
  477. {
  478. return bInfrared;
  479. }
  480. switch ((AimDeviceType)aimDeviceInfo.type)
  481. {
  482. case AimDeviceType.HOUYI:
  483. case AimDeviceType.HOUYI2:
  484. case AimDeviceType.ARTEMIS:
  485. bInfrared = false;
  486. break;
  487. case AimDeviceType.HOUYIPRO:
  488. case AimDeviceType.Gun:
  489. case AimDeviceType.ARTEMISPRO:
  490. bInfrared = true;
  491. break;
  492. default:
  493. bInfrared = false;
  494. break;
  495. }
  496. return bInfrared;
  497. }
  498. }