DeviceView_ItemShow.cs 19 KB

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