DeviceView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using AdaptUI;
  2. using AppUI.Bluetooth;
  3. using AppUI.Manager.View;
  4. using AppUI.View.Component;
  5. using SmartBowSDK;
  6. using System;
  7. using System.Collections.Generic;
  8. using TMPro;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using BLEAimhandler = AppUI.Bluetooth.AimHandler;
  12. using SdkAimDeviceInfo = SmartBowSDK.AimDeviceInfo;
  13. namespace AppUI.View.Home.DeviceView
  14. {
  15. /// <summary>content-has 下各玩家 ItemShow;显隐由 AimDeviceInfo 是否已记录决定。</summary>
  16. [Serializable]
  17. public class DeviceViewPlayerSlot
  18. {
  19. public BluetoothPlayer player = BluetoothPlayer.FIRST_PLAYER;
  20. public DeviceView_ItemShow itemShow;
  21. }
  22. public class DeviceView : MonoBehaviour, MenuBackInterface
  23. {
  24. [Header("连接态 UI(未连接 content-not / 已连接 content-has)")]
  25. [SerializeField] GameObject ConnectNotParent;
  26. [SerializeField] GameObject ConnectHasParent;
  27. [Header("1P 设备信息(已连接态下刷新)")]
  28. [SerializeField] DeviceView_ItemShow itemShowP1;
  29. [Header("各玩家连接态 ItemShow(连接成功后显示对应子物体)")]
  30. [SerializeField] List<DeviceViewPlayerSlot> playerSlots = new List<DeviceViewPlayerSlot>();
  31. [SerializeField] List<Button> smartConnect1Buttons;
  32. [SerializeField] List<Sprite> smartArcheryBg;
  33. [SerializeField] List<DeviceView_ItemShow> deviceViewItems;
  34. SmartBowDeviceHub _hub;
  35. bool _lastP1HasRecord;
  36. bool _lastP2HasRecord;
  37. bool _hubEventsBound;
  38. bool _deviceSelectionFlowActive;
  39. /// <summary>当前激活的 DeviceView(用于子页面结束临时选型流程)。</summary>
  40. public static DeviceView Active { get; private set; }
  41. public bool IsDeviceSelectionFlowActive => _deviceSelectionFlowActive;
  42. void Awake()
  43. {
  44. EnsurePlayerSlots();
  45. }
  46. public bool OnMenuBack()
  47. {
  48. ViewManager.HideView(UIViewType.DeviceView);
  49. return true;
  50. }
  51. public void OnClick_Back()
  52. {
  53. AudioMgr.ins.PlayBtn();
  54. if (_deviceSelectionFlowActive) {
  55. EndDeviceSelectionFlow();
  56. }
  57. else {
  58. ViewManager.HideView(UIViewType.DeviceView);
  59. }
  60. }
  61. void OnEnable()
  62. {
  63. Active = this;
  64. EnterManualConnectFlow();
  65. TryBindHubEvents();
  66. RefreshConnectContent(force: true);
  67. }
  68. void OnDisable()
  69. {
  70. ExitManualConnectFlow();
  71. EndDeviceSelectionFlow();
  72. if (Active == this)
  73. Active = null;
  74. UnbindHubEvents();
  75. }
  76. void Start()
  77. {
  78. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  79. for (int i = 0; i < smartConnect1Buttons.Count; i++)
  80. {
  81. int temp = i;
  82. smartConnect1Buttons[i].onClick.AddListener(() =>
  83. {
  84. OnChangeSmartConnect1Button(temp);
  85. });
  86. }
  87. SmartBowDeviceHub.ins?.Aim?.onCreateAimDeviceInfoById();
  88. TryBindHubEvents();
  89. RefreshConnectContent(force: true);
  90. }
  91. void OnDestroy()
  92. {
  93. UnbindHubEvents();
  94. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  95. SmartBowDeviceHub.ins?.CancelConnecting();
  96. }
  97. void Update()
  98. {
  99. if (!_hubEventsBound)
  100. TryBindHubEvents();
  101. }
  102. void TryBindHubEvents()
  103. {
  104. SmartBowDeviceHub hub = SmartBowDeviceHub.ins;
  105. if (hub == null || _hubEventsBound) return;
  106. _hub = hub;
  107. _hub.OnStateChanged += OnHubStateChanged;
  108. if (_hub.Aim != null)
  109. _hub.Aim.aimDeviceInfoChangeEvent += OnDeviceInfoChanged;
  110. _hubEventsBound = true;
  111. }
  112. void UnbindHubEvents()
  113. {
  114. if (!_hubEventsBound || _hub == null) return;
  115. _hub.OnStateChanged -= OnHubStateChanged;
  116. if (_hub.Aim != null)
  117. _hub.Aim.aimDeviceInfoChangeEvent -= OnDeviceInfoChanged;
  118. _hub = null;
  119. _hubEventsBound = false;
  120. }
  121. void OnHubStateChanged(SmartBowDeviceState state)
  122. {
  123. // 连接态文案仍随 SDK 状态刷新;ItemShow 显隐只看 AimDeviceInfo 是否已记录。
  124. RefreshPlayerItemShows(updateVisibility: false);
  125. }
  126. void OnDeviceInfoChanged()
  127. {
  128. if (!isActiveAndEnabled) return;
  129. RefreshConnectContent(force: true);
  130. }
  131. /// <summary>
  132. /// 是否已在本地记录过该玩家的设备信息(选过类型且连通过写入 MAC)。
  133. /// 与 <see cref="AimDeviceInfo.bInitMac"/> / type 一致,不表示当前在线。
  134. /// </summary>
  135. static bool HasRecordedDeviceInfo(BLEAimhandler aim, BluetoothPlayer player)
  136. {
  137. if (aim == null) return false;
  138. SdkAimDeviceInfo info = aim.GetAimDeviceInfo(player);
  139. if (info == null) return false;
  140. return info.bInitMac && aim.GetDeviceType(player) != -1;
  141. }
  142. void EnsurePlayerSlots()
  143. {
  144. if (playerSlots != null && playerSlots.Count > 0)
  145. return;
  146. playerSlots = new List<DeviceViewPlayerSlot>();
  147. if (deviceViewItems != null)
  148. {
  149. foreach (DeviceView_ItemShow item in deviceViewItems)
  150. {
  151. if (item == null) continue;
  152. playerSlots.Add(new DeviceViewPlayerSlot
  153. {
  154. player = item.Player,
  155. itemShow = item
  156. });
  157. }
  158. }
  159. if (itemShowP1 != null && !ContainsSlot(playerSlots, itemShowP1))
  160. {
  161. playerSlots.Insert(0, new DeviceViewPlayerSlot
  162. {
  163. player = BluetoothPlayer.FIRST_PLAYER,
  164. itemShow = itemShowP1
  165. });
  166. }
  167. }
  168. static bool ContainsSlot(List<DeviceViewPlayerSlot> slots, DeviceView_ItemShow item)
  169. {
  170. for (int i = 0; i < slots.Count; i++)
  171. {
  172. if (slots[i].itemShow == item)
  173. return true;
  174. }
  175. return false;
  176. }
  177. /// <summary>切换设备时临时回到 content-not,走弓箭/枪选型入口。</summary>
  178. public void BeginDeviceSelectionFlow()
  179. {
  180. EnterManualConnectFlow();
  181. _deviceSelectionFlowActive = true;
  182. BLEAimhandler aim = SmartBowDeviceHub.ins?.Aim;
  183. bool p1HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.FIRST_PLAYER);
  184. bool p2HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.SECOND_PLAYER);
  185. ApplyConnectParentVisibility(p1HasRecord, p2HasRecord);
  186. }
  187. /// <summary>结束临时选型流程,恢复 content-not / content-has 正常显隐。</summary>
  188. public void EndDeviceSelectionFlow()
  189. {
  190. if (!_deviceSelectionFlowActive)
  191. return;
  192. _deviceSelectionFlowActive = false;
  193. RefreshConnectContent(force: true);
  194. }
  195. /// <summary>按 AimDeviceInfo 是否已记录设备信息切换 content-not / content-has,并显示对应 ItemShow。</summary>
  196. public void RefreshConnectContent(bool force = false)
  197. {
  198. BLEAimhandler aim = SmartBowDeviceHub.ins?.Aim;
  199. bool p1HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.FIRST_PLAYER);
  200. bool p2HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.SECOND_PLAYER);
  201. if (!force && !_deviceSelectionFlowActive
  202. && p1HasRecord == _lastP1HasRecord && p2HasRecord == _lastP2HasRecord)
  203. return;
  204. _lastP1HasRecord = p1HasRecord;
  205. _lastP2HasRecord = p2HasRecord;
  206. ApplyConnectParentVisibility(p1HasRecord, p2HasRecord);
  207. RefreshPlayerItemShows(updateVisibility: true);
  208. }
  209. void ApplyConnectParentVisibility(bool p1HasRecord, bool p2HasRecord)
  210. {
  211. if (_deviceSelectionFlowActive)
  212. {
  213. if (ConnectNotParent != null)
  214. ConnectNotParent.SetActive(true);
  215. if (ConnectHasParent != null)
  216. ConnectHasParent.SetActive(false);
  217. return;
  218. }
  219. bool anyHasRecord = p1HasRecord || p2HasRecord;
  220. if (ConnectNotParent != null)
  221. ConnectNotParent.SetActive(!anyHasRecord);
  222. if (ConnectHasParent != null)
  223. ConnectHasParent.SetActive(anyHasRecord);
  224. }
  225. /// <summary>按 AimDeviceInfo 激活 ItemShow;updateVisibility 为 false 时只刷新已显示项的状态文案。</summary>
  226. public void RefreshPlayerItemShows(bool updateVisibility = true)
  227. {
  228. EnsurePlayerSlots();
  229. BLEAimhandler aim = SmartBowDeviceHub.ins?.Aim;
  230. if (playerSlots == null) return;
  231. foreach (DeviceViewPlayerSlot slot in playerSlots)
  232. {
  233. if (slot.itemShow == null) continue;
  234. bool show = HasRecordedDeviceInfo(aim, slot.player);
  235. if (updateVisibility)
  236. {
  237. if (slot.itemShow.gameObject.activeSelf != show)
  238. slot.itemShow.gameObject.SetActive(show);
  239. }
  240. else if (!slot.itemShow.gameObject.activeSelf)
  241. {
  242. continue;
  243. }
  244. if (!show) continue;
  245. slot.itemShow.onShowDeviceInfo();
  246. slot.itemShow.onUpdateStatusInfo();
  247. slot.itemShow.RefreshActionButtons();
  248. }
  249. }
  250. public void RefreshItemShowP1()
  251. {
  252. RefreshPlayerItemShows();
  253. }
  254. DeviceView_ItemShow GetItemShowP1()
  255. {
  256. if (itemShowP1 != null) return itemShowP1;
  257. if (playerSlots != null)
  258. {
  259. foreach (DeviceViewPlayerSlot slot in playerSlots)
  260. {
  261. if (slot.player == BluetoothPlayer.FIRST_PLAYER && slot.itemShow != null)
  262. return slot.itemShow;
  263. }
  264. }
  265. if (deviceViewItems != null && deviceViewItems.Count > 0)
  266. return deviceViewItems[0];
  267. return null;
  268. }
  269. public void OnChangeSmartConnect1Button(int index)
  270. {
  271. bool _selected = false;
  272. for (int i = 0; i < smartConnect1Buttons.Count; i++)
  273. {
  274. Button _button = smartConnect1Buttons[i];
  275. Color32 _white;
  276. if (index == i)
  277. {
  278. _white = new Color32(255, 255, 255, 255);
  279. _selected = true;
  280. _button.GetComponent<Image>().sprite = smartArcheryBg[1];
  281. }
  282. else
  283. {
  284. _white = new Color32(59, 59, 59, 255);
  285. _button.GetComponent<Image>().sprite = smartArcheryBg[0];
  286. }
  287. _button.transform.Find("icon").GetComponent<Image>().color = _white;
  288. _button.transform.Find("title").GetComponent<TMP_Text>().color = _white;
  289. }
  290. if (_selected)
  291. {
  292. AudioMgr.ins.PlayBtn();
  293. if (getEnabelPanelStatus())
  294. {
  295. EnterManualConnectFlow();
  296. if (index == 0)
  297. {
  298. Debug.Log("archery");
  299. ViewManager.ShowView(UIViewType.DeviceArcheryView);
  300. }
  301. else if (index == 1)
  302. {
  303. Debug.Log("Gun");
  304. ViewManager.ShowView(UIViewType.DeviceGunView);
  305. }
  306. SmartBowDeviceHub.ins.Aim.onCreateTempAimDeviceInfo();
  307. }
  308. }
  309. }
  310. public bool getEnabelPanelStatus() => true;
  311. /// <summary>进入 DeviceView 手动连接:抑制 Home 自动连接,并取消进行中的自动连。</summary>
  312. void EnterManualConnectFlow()
  313. {
  314. SmartBowDeviceHub hub = SmartBowDeviceHub.ins;
  315. if (hub == null) return;
  316. hub.SetSuppressHomeAutoConnect(true);
  317. if (hub.IsConnecting(BluetoothPlayer.FIRST_PLAYER) || hub.IsConnecting(BluetoothPlayer.SECOND_PLAYER))
  318. hub.CancelConnecting(global::BluetoothStatusEnum.Connect);
  319. }
  320. void ExitManualConnectFlow()
  321. {
  322. SmartBowDeviceHub.ins?.SetSuppressHomeAutoConnect(false);
  323. }
  324. }
  325. }