| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- using AdaptUI;
- using AppUI.Bluetooth;
- using AppUI.Manager.View;
- using AppUI.View.Component;
- using SmartBowSDK;
- using System;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- using BLEAimhandler = AppUI.Bluetooth.AimHandler;
- using SdkAimDeviceInfo = SmartBowSDK.AimDeviceInfo;
- namespace AppUI.View.Home.DeviceView
- {
- /// <summary>content-has 下各玩家 ItemShow;显隐由 AimDeviceInfo 是否已记录决定。</summary>
- [Serializable]
- public class DeviceViewPlayerSlot
- {
- public BluetoothPlayer player = BluetoothPlayer.FIRST_PLAYER;
- public DeviceView_ItemShow itemShow;
- }
- public class DeviceView : MonoBehaviour, MenuBackInterface
- {
- [Header("连接态 UI(未连接 content-not / 已连接 content-has)")]
- [SerializeField] GameObject ConnectNotParent;
- [SerializeField] GameObject ConnectHasParent;
- [Header("1P 设备信息(已连接态下刷新)")]
- [SerializeField] DeviceView_ItemShow itemShowP1;
- [Header("各玩家连接态 ItemShow(连接成功后显示对应子物体)")]
- [SerializeField] List<DeviceViewPlayerSlot> playerSlots = new List<DeviceViewPlayerSlot>();
- [SerializeField] List<Button> smartConnect1Buttons;
- [SerializeField] List<Sprite> smartArcheryBg;
- [SerializeField] List<DeviceView_ItemShow> deviceViewItems;
- SmartBowDeviceHub _hub;
- bool _lastP1HasRecord;
- bool _lastP2HasRecord;
- bool _hubEventsBound;
- bool _deviceSelectionFlowActive;
- /// <summary>当前激活的 DeviceView(用于子页面结束临时选型流程)。</summary>
- public static DeviceView Active { get; private set; }
- public bool IsDeviceSelectionFlowActive => _deviceSelectionFlowActive;
- void Awake()
- {
- EnsurePlayerSlots();
- }
- public bool OnMenuBack()
- {
- ViewManager.HideView(UIViewType.DeviceView);
- return true;
- }
- public void OnClick_Back()
- {
- AudioMgr.ins.PlayBtn();
- if (_deviceSelectionFlowActive) {
- EndDeviceSelectionFlow();
- }
- else {
- ViewManager.HideView(UIViewType.DeviceView);
- }
- }
- void OnEnable()
- {
- Active = this;
- EnterManualConnectFlow();
- TryBindHubEvents();
- RefreshConnectContent(force: true);
- }
- void OnDisable()
- {
- ExitManualConnectFlow();
- EndDeviceSelectionFlow();
- if (Active == this)
- Active = null;
- UnbindHubEvents();
- }
- void Start()
- {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- for (int i = 0; i < smartConnect1Buttons.Count; i++)
- {
- int temp = i;
- smartConnect1Buttons[i].onClick.AddListener(() =>
- {
- OnChangeSmartConnect1Button(temp);
- });
- }
- SmartBowDeviceHub.ins?.Aim?.onCreateAimDeviceInfoById();
- TryBindHubEvents();
- RefreshConnectContent(force: true);
- }
- void OnDestroy()
- {
- UnbindHubEvents();
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- SmartBowDeviceHub.ins?.CancelConnecting();
- }
- void Update()
- {
- if (!_hubEventsBound)
- TryBindHubEvents();
- }
- void TryBindHubEvents()
- {
- SmartBowDeviceHub hub = SmartBowDeviceHub.ins;
- if (hub == null || _hubEventsBound) return;
- _hub = hub;
- _hub.OnStateChanged += OnHubStateChanged;
- if (_hub.Aim != null)
- _hub.Aim.aimDeviceInfoChangeEvent += OnDeviceInfoChanged;
- _hubEventsBound = true;
- }
- void UnbindHubEvents()
- {
- if (!_hubEventsBound || _hub == null) return;
- _hub.OnStateChanged -= OnHubStateChanged;
- if (_hub.Aim != null)
- _hub.Aim.aimDeviceInfoChangeEvent -= OnDeviceInfoChanged;
- _hub = null;
- _hubEventsBound = false;
- }
- void OnHubStateChanged(SmartBowDeviceState state)
- {
- // 连接态文案仍随 SDK 状态刷新;ItemShow 显隐只看 AimDeviceInfo 是否已记录。
- RefreshPlayerItemShows(updateVisibility: false);
- }
- void OnDeviceInfoChanged()
- {
- if (!isActiveAndEnabled) return;
- RefreshConnectContent(force: true);
- }
- /// <summary>
- /// 是否已在本地记录过该玩家的设备信息(选过类型且连通过写入 MAC)。
- /// 与 <see cref="AimDeviceInfo.bInitMac"/> / type 一致,不表示当前在线。
- /// </summary>
- static bool HasRecordedDeviceInfo(BLEAimhandler aim, BluetoothPlayer player)
- {
- if (aim == null) return false;
- SdkAimDeviceInfo info = aim.GetAimDeviceInfo(player);
- if (info == null) return false;
- return info.bInitMac && aim.GetDeviceType(player) != -1;
- }
- void EnsurePlayerSlots()
- {
- if (playerSlots != null && playerSlots.Count > 0)
- return;
- playerSlots = new List<DeviceViewPlayerSlot>();
- if (deviceViewItems != null)
- {
- foreach (DeviceView_ItemShow item in deviceViewItems)
- {
- if (item == null) continue;
- playerSlots.Add(new DeviceViewPlayerSlot
- {
- player = item.Player,
- itemShow = item
- });
- }
- }
- if (itemShowP1 != null && !ContainsSlot(playerSlots, itemShowP1))
- {
- playerSlots.Insert(0, new DeviceViewPlayerSlot
- {
- player = BluetoothPlayer.FIRST_PLAYER,
- itemShow = itemShowP1
- });
- }
- }
- static bool ContainsSlot(List<DeviceViewPlayerSlot> slots, DeviceView_ItemShow item)
- {
- for (int i = 0; i < slots.Count; i++)
- {
- if (slots[i].itemShow == item)
- return true;
- }
- return false;
- }
- /// <summary>切换设备时临时回到 content-not,走弓箭/枪选型入口。</summary>
- public void BeginDeviceSelectionFlow()
- {
- EnterManualConnectFlow();
- _deviceSelectionFlowActive = true;
- BLEAimhandler aim = SmartBowDeviceHub.ins?.Aim;
- bool p1HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.FIRST_PLAYER);
- bool p2HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.SECOND_PLAYER);
- ApplyConnectParentVisibility(p1HasRecord, p2HasRecord);
- }
- /// <summary>结束临时选型流程,恢复 content-not / content-has 正常显隐。</summary>
- public void EndDeviceSelectionFlow()
- {
- if (!_deviceSelectionFlowActive)
- return;
- _deviceSelectionFlowActive = false;
- RefreshConnectContent(force: true);
- }
- /// <summary>按 AimDeviceInfo 是否已记录设备信息切换 content-not / content-has,并显示对应 ItemShow。</summary>
- public void RefreshConnectContent(bool force = false)
- {
- BLEAimhandler aim = SmartBowDeviceHub.ins?.Aim;
- bool p1HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.FIRST_PLAYER);
- bool p2HasRecord = HasRecordedDeviceInfo(aim, BluetoothPlayer.SECOND_PLAYER);
- if (!force && !_deviceSelectionFlowActive
- && p1HasRecord == _lastP1HasRecord && p2HasRecord == _lastP2HasRecord)
- return;
- _lastP1HasRecord = p1HasRecord;
- _lastP2HasRecord = p2HasRecord;
- ApplyConnectParentVisibility(p1HasRecord, p2HasRecord);
- RefreshPlayerItemShows(updateVisibility: true);
- }
- void ApplyConnectParentVisibility(bool p1HasRecord, bool p2HasRecord)
- {
- if (_deviceSelectionFlowActive)
- {
- if (ConnectNotParent != null)
- ConnectNotParent.SetActive(true);
- if (ConnectHasParent != null)
- ConnectHasParent.SetActive(false);
- return;
- }
- bool anyHasRecord = p1HasRecord || p2HasRecord;
- if (ConnectNotParent != null)
- ConnectNotParent.SetActive(!anyHasRecord);
- if (ConnectHasParent != null)
- ConnectHasParent.SetActive(anyHasRecord);
- }
- /// <summary>按 AimDeviceInfo 激活 ItemShow;updateVisibility 为 false 时只刷新已显示项的状态文案。</summary>
- public void RefreshPlayerItemShows(bool updateVisibility = true)
- {
- EnsurePlayerSlots();
- BLEAimhandler aim = SmartBowDeviceHub.ins?.Aim;
- if (playerSlots == null) return;
- foreach (DeviceViewPlayerSlot slot in playerSlots)
- {
- if (slot.itemShow == null) continue;
- bool show = HasRecordedDeviceInfo(aim, slot.player);
- if (updateVisibility)
- {
- if (slot.itemShow.gameObject.activeSelf != show)
- slot.itemShow.gameObject.SetActive(show);
- }
- else if (!slot.itemShow.gameObject.activeSelf)
- {
- continue;
- }
- if (!show) continue;
- slot.itemShow.onShowDeviceInfo();
- slot.itemShow.onUpdateStatusInfo();
- slot.itemShow.RefreshActionButtons();
- }
- }
- public void RefreshItemShowP1()
- {
- RefreshPlayerItemShows();
- }
- DeviceView_ItemShow GetItemShowP1()
- {
- if (itemShowP1 != null) return itemShowP1;
- if (playerSlots != null)
- {
- foreach (DeviceViewPlayerSlot slot in playerSlots)
- {
- if (slot.player == BluetoothPlayer.FIRST_PLAYER && slot.itemShow != null)
- return slot.itemShow;
- }
- }
- if (deviceViewItems != null && deviceViewItems.Count > 0)
- return deviceViewItems[0];
- return null;
- }
- public void OnChangeSmartConnect1Button(int index)
- {
- bool _selected = false;
- for (int i = 0; i < smartConnect1Buttons.Count; i++)
- {
- Button _button = smartConnect1Buttons[i];
- Color32 _white;
- if (index == i)
- {
- _white = new Color32(255, 255, 255, 255);
- _selected = true;
- _button.GetComponent<Image>().sprite = smartArcheryBg[1];
- }
- else
- {
- _white = new Color32(59, 59, 59, 255);
- _button.GetComponent<Image>().sprite = smartArcheryBg[0];
- }
- _button.transform.Find("icon").GetComponent<Image>().color = _white;
- _button.transform.Find("title").GetComponent<TMP_Text>().color = _white;
- }
- if (_selected)
- {
- AudioMgr.ins.PlayBtn();
- if (getEnabelPanelStatus())
- {
- EnterManualConnectFlow();
- if (index == 0)
- {
- Debug.Log("archery");
- ViewManager.ShowView(UIViewType.DeviceArcheryView);
- }
- else if (index == 1)
- {
- Debug.Log("Gun");
- ViewManager.ShowView(UIViewType.DeviceGunView);
- }
- SmartBowDeviceHub.ins.Aim.onCreateTempAimDeviceInfo();
- }
- }
- }
- public bool getEnabelPanelStatus() => true;
- /// <summary>进入 DeviceView 手动连接:抑制 Home 自动连接,并取消进行中的自动连。</summary>
- void EnterManualConnectFlow()
- {
- SmartBowDeviceHub hub = SmartBowDeviceHub.ins;
- if (hub == null) return;
- hub.SetSuppressHomeAutoConnect(true);
- if (hub.IsConnecting(BluetoothPlayer.FIRST_PLAYER) || hub.IsConnecting(BluetoothPlayer.SECOND_PLAYER))
- hub.CancelConnecting(global::BluetoothStatusEnum.Connect);
- }
- void ExitManualConnectFlow()
- {
- SmartBowDeviceHub.ins?.SetSuppressHomeAutoConnect(false);
- }
- }
- }
|