DeviceView_ItemShow.cs 18 KB

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