DeviceView_ItemShow.cs 15 KB

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