DeviceView_ItemShow.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. 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. string name = BluetoothStatus.GetStatusTextKey(BluetoothAim.ins.status);
  327. if (name != _TextAutoLanguage2.GetTextKey())
  328. _TextAutoLanguage2.SetTextKey(name);
  329. if (BluetoothAim.ins.status == BluetoothStatusEnum.Connecting)
  330. {
  331. omitText.gameObject.SetActive(true);
  332. UpdateLoadingText();
  333. }
  334. else
  335. {
  336. StopLoading();
  337. }
  338. }
  339. else if (bluetoothPlayer == BluetoothPlayer.SECONDE_PLAYER)
  340. {
  341. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  342. if (smartBowHelper == null) return;
  343. string name = BluetoothStatus.GetBLE2StatusTextKey(smartBowHelper.GetBluetoothStatus());
  344. if (name != _TextAutoLanguage2.GetTextKey())
  345. _TextAutoLanguage2.SetTextKey(name);
  346. if (smartBowHelper.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connecting)
  347. {
  348. omitText.gameObject.SetActive(true);
  349. UpdateLoadingText();
  350. }
  351. else
  352. {
  353. StopLoading();
  354. }
  355. }
  356. }
  357. //设置一个面板选择状态
  358. public void setPanelStatus(bool _active)
  359. {
  360. selectPanel.enabled = _active;
  361. if (_active)
  362. {
  363. //赋值一下当前选择的对象
  364. DeviceViewInfrared.ins.selectDeviceViewItem = this;
  365. //选择框选择到的当前蓝牙用户
  366. BluetoothAim.ins.setBLEPlayer(bluetoothPlayer);
  367. Debug.Log("当前选择的操作蓝牙用户:" + BluetoothAim.ins.getBLEPlayer());
  368. //true的情况下设置index
  369. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  370. Debug.Log("当前选择的操作蓝牙用户 deviceSelectIndex:" + deviceIndex);
  371. }
  372. }
  373. public bool getPanelStatus()
  374. {
  375. bool _enable = selectPanel.enabled;
  376. //设置一次
  377. DeviceViewInfrared.ins.OnCloseAllPanelStatus();
  378. setPanelStatus(true);
  379. return _enable;
  380. }
  381. public bool getCurrentPanelEnable()
  382. {
  383. return selectPanel.enabled;
  384. }
  385. public bool getBLEConnectState() {
  386. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER) {
  387. return BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess;
  388. }
  389. else if (bluetoothPlayer == BluetoothPlayer.SECONDE_PLAYER)
  390. {
  391. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  392. if (smartBowHelper == null) return false;
  393. SmartBowSDK.BluetoothStatusEnum bluetoothStatusEnum = smartBowHelper.GetBluetoothStatus();
  394. return bluetoothStatusEnum == SmartBowSDK.BluetoothStatusEnum.Connected;
  395. }else return false;
  396. }
  397. /// <summary>
  398. /// 蓝牙连接事件,连接和断开连接
  399. /// </summary>
  400. public void OnConnectedEvent()
  401. {
  402. //要选中情况下,才能进行下一步操作
  403. if (!getPanelStatus()) return;
  404. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  405. AimHandler.ins.onCreateAimDeviceInfoById();
  406. Debug.Log("DeviceView_ItemShow bluetoothPlayer:" + bluetoothPlayer);
  407. if (BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  408. {
  409. //断开连接
  410. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  411. {
  412. BluetoothAim.ins.DoConnect();
  413. }
  414. else
  415. {
  416. BluetoothAim.ins.DoConnect2P();
  417. }
  418. return;
  419. }
  420. if (HomeView.ShowProminentBeforeConnectBLE()) return;
  421. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  422. {
  423. BluetoothAim.ins.DoConnect();
  424. }
  425. else
  426. {
  427. BluetoothAim.ins.DoConnect2P();
  428. }
  429. }
  430. /// <summary>
  431. /// 断开蓝牙连接
  432. /// </summary>
  433. public void OnDisConnectedEvent()
  434. {
  435. //要选中情况下,才能进行下一步操作
  436. if (!getPanelStatus()) return;
  437. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  438. AimHandler.ins.onCreateAimDeviceInfoById();
  439. Debug.Log("OnDisConnectedEvent bluetoothPlayer:" + bluetoothPlayer);
  440. //断开连接
  441. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  442. {
  443. if (BluetoothAim.ins) {
  444. if(BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  445. BluetoothAim.ins.DoConnect();
  446. else BluetoothAim.ins.connectCanceled = true; // 退出自动连接,即时正在连接
  447. }
  448. }
  449. else
  450. {
  451. if (BluetoothAim.ins && BluetoothAim.ins.getSmartBowHelper2P()
  452. && BluetoothAim.ins.getSmartBowHelper2P().GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected) {
  453. BluetoothAim.ins.bNoNeedToReconnect = false;
  454. BluetoothAim.ins.DoConnect2P();
  455. }
  456. }
  457. }
  458. /// <summary>
  459. /// 选择事件
  460. /// </summary>
  461. public void OnSelectEvent() {
  462. //如果是选中的状态,不再触发
  463. if (getCurrentPanelEnable()) return;
  464. AudioMgr.ins.PlayBtn();
  465. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  466. DeviceViewInfrared.ins.OnCloseAllPanelStatus();
  467. setPanelStatus(true);
  468. }
  469. /// <summary>
  470. /// 如果有选择到并且显示的话
  471. /// </summary>
  472. /// <returns></returns>
  473. public bool getCurrentItemVisible()
  474. {
  475. return selectPanel.enabled && bShowInfo;
  476. }
  477. /// <summary>
  478. /// 判断是不是红外设备
  479. /// </summary>
  480. /// <returns></returns>
  481. public bool bInfraredDevices() {
  482. bool bInfrared = false;
  483. List<AimDeviceInfo> allDeviceInfo = AimHandler.ins.aimDeviceInfos.arry;
  484. AimDeviceInfo aimDeviceInfo = allDeviceInfo.Find(obj => obj.id == deviceIndex);
  485. if (aimDeviceInfo == null || aimDeviceInfo.type == -1 || !aimDeviceInfo.bInitMac)//AimHandler.ins.aimDeviceInfos.arry.Count <= deviceIndex
  486. {
  487. return bInfrared;
  488. }
  489. switch ((AimDeviceType)aimDeviceInfo.type)
  490. {
  491. case AimDeviceType.HOUYI:
  492. case AimDeviceType.HOUYI2:
  493. case AimDeviceType.ARTEMIS:
  494. bInfrared = false;
  495. break;
  496. case AimDeviceType.HOUYIPRO:
  497. case AimDeviceType.Gun:
  498. case AimDeviceType.ARTEMISPRO:
  499. case AimDeviceType.PistolM17:
  500. case AimDeviceType.RifleM416:
  501. bInfrared = true;
  502. break;
  503. default:
  504. bInfrared = false;
  505. break;
  506. }
  507. return bInfrared;
  508. }
  509. }