DeviceView_ItemShow.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. //这个是处理设备下标
  37. [SerializeField] int deviceIndex = 0;
  38. //是否存在连接信息。
  39. bool bShowInfo = false;
  40. // Start is called before the first frame update
  41. // bool bProcess = false;
  42. private string omitText;
  43. private int dotCount = 0;
  44. private float interval = 0.5f; // 标点更新间隔
  45. private float timer = 0f;
  46. [SerializeField] Text curConnectStatus;
  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 = new string('.', dotCount); // 添加相应数量的点
  237. timer = 0f; // 重置计时器
  238. }
  239. }
  240. // 停止加载效果
  241. public void StopLoading()
  242. {
  243. dotCount = 0;
  244. omitText = ""; // 停止时恢复为基础文本
  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. case AimDeviceType.PistolM17:
  307. titleText.text = "Pistol M17";
  308. bowBg.sprite = gunBgs[1];
  309. break;
  310. case AimDeviceType.RifleM416:
  311. titleText.text = "Rifle M416";
  312. bowBg.sprite = gunBgs[2];
  313. break;
  314. default:
  315. bShowInfo = false;
  316. // Debug.LogWarning("onShowDeviceInfo 没有对应设备 AimDeviceType:" + ((AimDeviceType)aimDeviceInfo.type).ToString());
  317. break;
  318. }
  319. //onUpdateStatusInfo();
  320. }
  321. //刷新状态
  322. public void onUpdateStatusInfo()
  323. {
  324. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  325. {
  326. if (BluetoothAim.ins.status == BluetoothStatusEnum.Connecting)
  327. {
  328. UpdateLoadingText();
  329. }
  330. else
  331. {
  332. StopLoading();
  333. }
  334. string name = BluetoothStatus.GetStatusTextKey(BluetoothAim.ins.status);
  335. curConnectStatus.text = TextAutoLanguage2.GetTextByKey(name) + omitText;
  336. }
  337. else if (bluetoothPlayer == BluetoothPlayer.SECONDE_PLAYER)
  338. {
  339. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  340. if (smartBowHelper == null) return;
  341. if (smartBowHelper.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connecting)
  342. {
  343. UpdateLoadingText();
  344. }
  345. else
  346. {
  347. StopLoading();
  348. }
  349. string name = BluetoothStatus.GetBLE2StatusTextKey(smartBowHelper.GetBluetoothStatus());
  350. curConnectStatus.text = TextAutoLanguage2.GetTextByKey(name) + omitText;
  351. }
  352. }
  353. //设置一个面板选择状态
  354. public void setPanelStatus(bool _active)
  355. {
  356. selectPanel.enabled = _active;
  357. if (_active)
  358. {
  359. //赋值一下当前选择的对象
  360. DeviceViewInfrared.ins.selectDeviceViewItem = this;
  361. //选择框选择到的当前蓝牙用户
  362. BluetoothAim.ins.setBLEPlayer(bluetoothPlayer);
  363. Debug.Log("当前选择的操作蓝牙用户:" + BluetoothAim.ins.getBLEPlayer());
  364. //true的情况下设置index
  365. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  366. Debug.Log("当前选择的操作蓝牙用户 deviceSelectIndex:" + deviceIndex);
  367. }
  368. }
  369. public bool getPanelStatus()
  370. {
  371. bool _enable = selectPanel.enabled;
  372. //设置一次
  373. DeviceViewInfrared.ins.OnCloseAllPanelStatus();
  374. setPanelStatus(true);
  375. return _enable;
  376. }
  377. public bool getCurrentPanelEnable()
  378. {
  379. return selectPanel.enabled;
  380. }
  381. public bool getBLEConnectState() {
  382. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER) {
  383. return BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess;
  384. }
  385. else if (bluetoothPlayer == BluetoothPlayer.SECONDE_PLAYER)
  386. {
  387. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  388. if (smartBowHelper == null) return false;
  389. SmartBowSDK.BluetoothStatusEnum bluetoothStatusEnum = smartBowHelper.GetBluetoothStatus();
  390. return bluetoothStatusEnum == SmartBowSDK.BluetoothStatusEnum.Connected;
  391. }else return false;
  392. }
  393. /// <summary>
  394. /// 蓝牙连接事件,连接和断开连接
  395. /// </summary>
  396. public void OnConnectedEvent()
  397. {
  398. //要选中情况下,才能进行下一步操作
  399. if (!getPanelStatus()) return;
  400. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  401. AimHandler.ins.onCreateAimDeviceInfoById();
  402. Debug.Log("DeviceView_ItemShow bluetoothPlayer:" + bluetoothPlayer);
  403. if (BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  404. {
  405. //断开连接
  406. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  407. {
  408. BluetoothAim.ins.DoConnect();
  409. }
  410. else
  411. {
  412. BluetoothAim.ins.DoConnect2P();
  413. }
  414. return;
  415. }
  416. if (HomeView.ShowProminentBeforeConnectBLE()) return;
  417. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  418. {
  419. BluetoothAim.ins.DoConnect();
  420. }
  421. else
  422. {
  423. BluetoothAim.ins.DoConnect2P();
  424. }
  425. }
  426. /// <summary>
  427. /// 断开蓝牙连接
  428. /// </summary>
  429. public void OnDisConnectedEvent()
  430. {
  431. //要选中情况下,才能进行下一步操作
  432. if (!getPanelStatus()) return;
  433. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  434. AimHandler.ins.onCreateAimDeviceInfoById();
  435. Debug.Log("OnDisConnectedEvent bluetoothPlayer:" + bluetoothPlayer);
  436. //断开连接
  437. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  438. {
  439. if (BluetoothAim.ins) {
  440. if(BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  441. BluetoothAim.ins.DoConnect();
  442. else BluetoothAim.ins.connectCanceled = true; // 退出自动连接,即时正在连接
  443. }
  444. }
  445. else
  446. {
  447. if (BluetoothAim.ins && BluetoothAim.ins.getSmartBowHelper2P()
  448. && BluetoothAim.ins.getSmartBowHelper2P().GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected) {
  449. BluetoothAim.ins.bNoNeedToReconnect = false;
  450. BluetoothAim.ins.DoConnect2P();
  451. }
  452. }
  453. }
  454. /// <summary>
  455. /// 选择事件
  456. /// </summary>
  457. public void OnSelectEvent() {
  458. //如果是选中的状态,不再触发
  459. if (getCurrentPanelEnable()) return;
  460. AudioMgr.ins.PlayBtn();
  461. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  462. DeviceViewInfrared.ins.OnCloseAllPanelStatus();
  463. setPanelStatus(true);
  464. }
  465. /// <summary>
  466. /// 如果有选择到并且显示的话
  467. /// </summary>
  468. /// <returns></returns>
  469. public bool getCurrentItemVisible()
  470. {
  471. return selectPanel.enabled && bShowInfo;
  472. }
  473. /// <summary>
  474. /// 判断是不是红外设备
  475. /// </summary>
  476. /// <returns></returns>
  477. public bool bInfraredDevices() {
  478. bool bInfrared = false;
  479. List<AimDeviceInfo> allDeviceInfo = AimHandler.ins.aimDeviceInfos.arry;
  480. AimDeviceInfo aimDeviceInfo = allDeviceInfo.Find(obj => obj.id == deviceIndex);
  481. if (aimDeviceInfo == null || aimDeviceInfo.type == -1 || !aimDeviceInfo.bInitMac)//AimHandler.ins.aimDeviceInfos.arry.Count <= deviceIndex
  482. {
  483. return bInfrared;
  484. }
  485. switch ((AimDeviceType)aimDeviceInfo.type)
  486. {
  487. case AimDeviceType.HOUYI:
  488. case AimDeviceType.HOUYI2:
  489. case AimDeviceType.ARTEMIS:
  490. bInfrared = false;
  491. break;
  492. case AimDeviceType.HOUYIPRO:
  493. case AimDeviceType.Gun:
  494. case AimDeviceType.ARTEMISPRO:
  495. case AimDeviceType.PistolM17:
  496. case AimDeviceType.RifleM416:
  497. bInfrared = true;
  498. break;
  499. default:
  500. bInfrared = false;
  501. break;
  502. }
  503. return bInfrared;
  504. }
  505. }