DeviceView_ItemShow.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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.HOUYIPRO:
  283. titleText.text = "HOUYI Pro";
  284. bowBg.sprite = bowBgs[0];
  285. break;
  286. case AimDeviceType.Gun:
  287. titleText.text = "Pistol M9";
  288. bowBg.sprite = gunBgs[0];
  289. break;
  290. case AimDeviceType.ARTEMISPRO:
  291. titleText.text = "ARTEMIS Pro";
  292. bowBg.sprite = bowBgs[1];
  293. break;
  294. case AimDeviceType.PistolM17:
  295. titleText.text = "Pistol M17";
  296. bowBg.sprite = gunBgs[1];
  297. break;
  298. case AimDeviceType.RifleM416:
  299. titleText.text = "Rifle M416";
  300. bowBg.sprite = gunBgs[2];
  301. break;
  302. default:
  303. bShowInfo = false;
  304. // Debug.LogWarning("onShowDeviceInfo 没有对应设备 AimDeviceType:" + ((AimDeviceType)aimDeviceInfo.type).ToString());
  305. break;
  306. }
  307. //onUpdateStatusInfo();
  308. }
  309. //刷新状态
  310. public void onUpdateStatusInfo()
  311. {
  312. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  313. {
  314. string name = BluetoothStatus.GetStatusTextKey(BluetoothAim.ins.status);
  315. if (name != _TextAutoLanguage2.GetTextKey())
  316. _TextAutoLanguage2.SetTextKey(name);
  317. if (BluetoothAim.ins.status == BluetoothStatusEnum.Connecting)
  318. {
  319. omitText.gameObject.SetActive(true);
  320. UpdateLoadingText();
  321. }
  322. else
  323. {
  324. StopLoading();
  325. }
  326. }
  327. else if (bluetoothPlayer == BluetoothPlayer.SECONDE_PLAYER)
  328. {
  329. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  330. if (smartBowHelper == null) return;
  331. string name = BluetoothStatus.GetBLE2StatusTextKey(smartBowHelper.GetBluetoothStatus());
  332. if (name != _TextAutoLanguage2.GetTextKey())
  333. _TextAutoLanguage2.SetTextKey(name);
  334. if (smartBowHelper.GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connecting)
  335. {
  336. omitText.gameObject.SetActive(true);
  337. UpdateLoadingText();
  338. }
  339. else
  340. {
  341. StopLoading();
  342. }
  343. }
  344. }
  345. //设置一个面板选择状态
  346. public void setPanelStatus(bool _active)
  347. {
  348. selectPanel.enabled = _active;
  349. if (_active)
  350. {
  351. //赋值一下当前选择的对象
  352. DeviceViewInfrared.ins.selectDeviceViewItem = this;
  353. //选择框选择到的当前蓝牙用户
  354. BluetoothAim.ins.setBLEPlayer(bluetoothPlayer);
  355. Debug.Log("当前选择的操作蓝牙用户:" + BluetoothAim.ins.getBLEPlayer());
  356. //true的情况下设置index
  357. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  358. Debug.Log("当前选择的操作蓝牙用户 deviceSelectIndex:" + deviceIndex);
  359. }
  360. }
  361. public bool getPanelStatus()
  362. {
  363. bool _enable = selectPanel.enabled;
  364. //设置一次
  365. DeviceViewInfrared.ins.OnCloseAllPanelStatus();
  366. setPanelStatus(true);
  367. return _enable;
  368. }
  369. public bool getCurrentPanelEnable()
  370. {
  371. return selectPanel.enabled;
  372. }
  373. public bool getBLEConnectState() {
  374. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER) {
  375. return BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess;
  376. }
  377. else if (bluetoothPlayer == BluetoothPlayer.SECONDE_PLAYER)
  378. {
  379. SmartBowSDK.SmartBowHelper smartBowHelper = BluetoothAim.ins.getSmartBowHelper2P();
  380. if (smartBowHelper == null) return false;
  381. SmartBowSDK.BluetoothStatusEnum bluetoothStatusEnum = smartBowHelper.GetBluetoothStatus();
  382. return bluetoothStatusEnum == SmartBowSDK.BluetoothStatusEnum.Connected;
  383. }else return false;
  384. }
  385. /// <summary>
  386. /// 蓝牙连接事件,连接和断开连接
  387. /// </summary>
  388. public void OnConnectedEvent()
  389. {
  390. //要选中情况下,才能进行下一步操作
  391. if (!getPanelStatus()) return;
  392. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  393. AimHandler.ins.onCreateAimDeviceInfoById();
  394. Debug.Log("DeviceView_ItemShow bluetoothPlayer:" + bluetoothPlayer);
  395. if (BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  396. {
  397. //断开连接
  398. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  399. {
  400. BluetoothAim.ins.DoConnect();
  401. }
  402. else
  403. {
  404. BluetoothAim.ins.DoConnect2P();
  405. }
  406. return;
  407. }
  408. if (HomeView.ShowProminentBeforeConnectBLE()) return;
  409. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  410. {
  411. BluetoothAim.ins.DoConnect();
  412. }
  413. else
  414. {
  415. BluetoothAim.ins.DoConnect2P();
  416. }
  417. }
  418. /// <summary>
  419. /// 断开蓝牙连接
  420. /// </summary>
  421. public void OnDisConnectedEvent()
  422. {
  423. //要选中情况下,才能进行下一步操作
  424. if (!getPanelStatus()) return;
  425. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  426. AimHandler.ins.onCreateAimDeviceInfoById();
  427. Debug.Log("OnDisConnectedEvent bluetoothPlayer:" + bluetoothPlayer);
  428. //断开连接
  429. if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
  430. {
  431. if (BluetoothAim.ins) {
  432. if(BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  433. BluetoothAim.ins.DoConnect();
  434. else BluetoothAim.ins.connectCanceled = true; // 退出自动连接,即时正在连接
  435. }
  436. }
  437. else
  438. {
  439. if (BluetoothAim.ins && BluetoothAim.ins.getSmartBowHelper2P()
  440. && BluetoothAim.ins.getSmartBowHelper2P().GetBluetoothStatus() == SmartBowSDK.BluetoothStatusEnum.Connected) {
  441. BluetoothAim.ins.bNoNeedToReconnect = false;
  442. BluetoothAim.ins.DoConnect2P();
  443. }
  444. }
  445. }
  446. /// <summary>
  447. /// 选择事件
  448. /// </summary>
  449. public void OnSelectEvent() {
  450. //如果是选中的状态,不再触发
  451. if (getCurrentPanelEnable()) return;
  452. AudioMgr.ins.PlayBtn();
  453. AimHandler.ins.SetAimDeviceSelectIndex(deviceIndex);
  454. DeviceViewInfrared.ins.OnCloseAllPanelStatus();
  455. setPanelStatus(true);
  456. }
  457. /// <summary>
  458. /// 如果有选择到并且显示的话
  459. /// </summary>
  460. /// <returns></returns>
  461. public bool getCurrentItemVisible()
  462. {
  463. return selectPanel.enabled && bShowInfo;
  464. }
  465. ///// <summary>
  466. ///// 判断是不是红外设备
  467. ///// </summary>
  468. ///// <returns></returns>
  469. //public bool bInfraredDevices() {
  470. // bool bInfrared = false;
  471. // List<AimDeviceInfo> allDeviceInfo = AimHandler.ins.aimDeviceInfos.arry;
  472. // AimDeviceInfo aimDeviceInfo = allDeviceInfo.Find(obj => obj.id == deviceIndex);
  473. // if (aimDeviceInfo == null || aimDeviceInfo.type == -1 || !aimDeviceInfo.bInitMac)//AimHandler.ins.aimDeviceInfos.arry.Count <= deviceIndex
  474. // {
  475. // return bInfrared;
  476. // }
  477. // switch ((AimDeviceType)aimDeviceInfo.type)
  478. // {
  479. // case AimDeviceType.HOUYIPRO:
  480. // case AimDeviceType.Gun:
  481. // case AimDeviceType.ARTEMISPRO:
  482. // bInfrared = true;
  483. // break;
  484. // default:
  485. // bInfrared = false;
  486. // break;
  487. // }
  488. // return bInfrared;
  489. //}
  490. }