DeviceView_ItemShow.cs 17 KB

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