| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- using AppUI.Bluetooth;
- using AppUI.Localization;
- using AppUI.Manager;
- using AppUI.Manager.View;
- using AppUI.View.Home.Main;
- using InfraredManager;
- using SmartBowSDK;
- using System;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- using BLEAimhandler = AppUI.Bluetooth.AimHandler;
- using SdkAimDeviceInfo = SmartBowSDK.AimDeviceInfo;
- namespace AppUI.View.Home.DeviceView
- {
- public class DeviceView_ItemShow : MonoBehaviour
- {
- [Tooltip("当前选择的用户设备")]
- [SerializeField] BluetoothPlayer bluetoothPlayer = BluetoothPlayer.FIRST_PLAYER;
- [SerializeField] Image selectPanel;
- [SerializeField] Button btnBg;
- [SerializeField] TMP_Text titleText;
- [Header("操作按钮(连接/屏幕定位 + 切换设备)")]
- [SerializeField] Button btnPrimaryAction;
- [SerializeField] Image btnPrimaryActionBg;
- [SerializeField] TMP_Text btnPrimaryActionLabel;
- [SerializeField] Button btnSwitchDevice;
- [SerializeField] Image btnSwitchDeviceBg;
- [SerializeField] TMP_Text btnSwitchDeviceLabel;
- [SerializeField] GameObject bettery;
- [SerializeField] GameObject betteryBar;
- [SerializeField] GameObject betteryValue;
- [SerializeField] Image bowBg;
- float countingTime1 = 5;
- [SerializeField] int deviceIndex;
- bool bShowInfo;
- string omitText;
- int dotCount;
- float interval = 0.5f;
- float timer;
- [SerializeField] TMP_Text curConnectStatus;
- [SerializeField] TMP_Text curScreenPositioningStatus;
- const string KeyScreenPositioningDone = "appui-sideslip-screen-positioning-done";
- const string KeyScreenPositioningIncomplete = "appui-sideslip-screen-positioning-incomplete";
- const string KeyStatusNotConnected = "appui-device-status-not-connected";
- const string KeyStatusConnecting = "appui-device-status-connecting";
- const string KeyStatusConnected = "appui-device-status-connected";
- const string KeyBtnConnect = "appui-device-btn-connect";
- const string KeyBtnDisconnect = "appui-device-btn-disconnect";
- const string KeyBtnSwitch = "appui-device-btn-switch";
- const string KeyBtnScreenPositioning = "appui-device-btn-screen-positioning";
- const string KeyTipInfraredConnect = "appui-device-tip-infrared-connect";
- static readonly Color32 BlackBg = new Color32(0, 0, 0, 255);
- static readonly Color32 WhiteBg = new Color32(255, 255, 255, 255);
- static readonly Color32 WhiteText = new Color32(255, 255, 255, 255);
- static readonly Color32 BlackText = new Color32(50, 50, 50, 255);
- static readonly Color32 StatusCompleteColor = new Color32(0, 0, 0, 255);
- static readonly Color32 StatusIncompleteColor = new Color32(145, 144, 144, 255);
- SmartBowDeviceHub Hub => SmartBowDeviceHub.ins;
- public BluetoothPlayer Player => bluetoothPlayer;
- /// <summary>连接 / 屏幕定位按钮节点。</summary>
- public Button BtnPrimaryAction => btnPrimaryAction;
- /// <summary>切换设备按钮节点。</summary>
- public Button BtnSwitchDevice => btnSwitchDevice;
- public GameObject BtnPrimaryActionNode => btnPrimaryAction != null ? btnPrimaryAction.gameObject : null;
- public GameObject BtnSwitchDeviceNode => btnSwitchDevice != null ? btnSwitchDevice.gameObject : null;
- enum PrimaryActionKind
- {
- ConnectSideSlip,
- InfraredView,
- Disconnect,
- }
- PrimaryActionKind _primaryActionKind = PrimaryActionKind.ConnectSideSlip;
- bool IsConnectSuccess()
- {
- return Hub != null && Hub.IsConnected(bluetoothPlayer);
- }
- bool IsConnecting()
- {
- return Hub != null && Hub.IsConnecting(bluetoothPlayer);
- }
- bool IsScreenPositioned()
- {
- InfraredCameraHelper helper = InfraredCameraHelper.GetInstance();
- return helper != null && helper.IsScreenLoateOK();
- }
- void Awake()
- {
- ResolveUiReferences();
- if (Hub?.Aim != null)
- Hub.Aim.aimDeviceInfoChangeEvent += onShowDeviceInfo;
- }
- void ResolveUiReferences()
- {
- if (titleText == null)
- {
- Transform title = transform.Find("Panel/title");
- if (title != null)
- titleText = title.GetComponent<TMP_Text>();
- }
- ResolveActionButtonReferences();
- }
- void ResolveActionButtonReferences()
- {
- if (btnPrimaryAction == null)
- {
- Transform screen = transform.Find("connected/bottom/Buttons/Screen");
- if (screen != null)
- {
- btnPrimaryAction = screen.GetComponent<Button>();
- btnPrimaryActionBg = screen.GetComponent<Image>();
- btnPrimaryActionLabel = screen.GetComponentInChildren<TMP_Text>(true);
- }
- }
- if (btnSwitchDevice == null)
- {
- Transform switchBtn = transform.Find("connected/bottom/Buttons/Switch");
- if (switchBtn != null)
- {
- btnSwitchDevice = switchBtn.GetComponent<Button>();
- btnSwitchDeviceBg = switchBtn.GetComponent<Image>();
- btnSwitchDeviceLabel = switchBtn.GetComponentInChildren<TMP_Text>(true);
- }
- }
- }
- void SetDeviceTitle(string text)
- {
- if (titleText != null)
- titleText.text = text;
- }
- void SetDeviceSprite(Sprite sprite)
- {
- if (bowBg != null && sprite != null)
- bowBg.sprite = sprite;
- }
- void OnEnable()
- {
- AppUILocalization.OnLanguageChanged += HandleLanguageChanged;
- }
- void OnDisable()
- {
- AppUILocalization.OnLanguageChanged -= HandleLanguageChanged;
- }
- void HandleLanguageChanged(LanguageEnum _)
- {
- onUpdateStatusInfo();
- RefreshActionButtons();
- }
- void Start()
- {
- ResolveUiReferences();
- btnBg?.onClick.AddListener(OnSelectEvent);
- btnPrimaryAction?.onClick.AddListener(OnClickPrimaryAction);
- btnSwitchDevice?.onClick.AddListener(OnClickSwitchDevice);
- if (Hub != null)
- {
- Hub.OnStateChanged += OnHubStateChanged;
- if (IsConnectSuccess())
- {
- int battery = Hub.GetBattery(bluetoothPlayer);
- if (battery > 0)
- RenderBattery(-1, battery);
- }
- }
- onUpdateStatusInfo();
- RefreshActionButtons();
- //开始选择一次
- if(bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
- {
- OnSelectEvent();
- }
- }
- void OnDestroy()
- {
- if (Hub != null)
- Hub.OnStateChanged -= OnHubStateChanged;
- if (Hub?.Aim != null)
- Hub.Aim.aimDeviceInfoChangeEvent -= onShowDeviceInfo;
- }
- void OnHubStateChanged(SmartBowDeviceState state)
- {
- onUpdateStatusInfo();
- RefreshActionButtons();
- }
- void Update()
- {
- if (Hub == null) return;
- if (IsConnectSuccess())
- {
- if (countingTime1 < 5)
- countingTime1 += Time.deltaTime;
- else
- {
- countingTime1 = 0;
- RenderBattery(-1, Hub.GetBattery(bluetoothPlayer));
- }
- }
- else if (!IsConnecting())
- {
- onShowDeviceInfo();
- RenderBattery(-1, 0);
- }
- else
- {
- RenderBattery(-1, 0);
- onShowDeviceInfo();
- }
- onUpdateStatusInfo();
- }
- void UpdateLoadingText()
- {
- timer += Time.deltaTime;
- if (timer >= interval)
- {
- dotCount = (dotCount + 1) % 4;
- omitText = new string('.', dotCount);
- timer = 0f;
- }
- }
- public void StopLoading()
- {
- dotCount = 0;
- omitText = "";
- }
- public void RenderBattery(int deviceID, int value)
- {
- if (betteryBar == null || betteryValue == null) return;
- Image img = betteryBar.GetComponent<Image>();
- Text txt = betteryValue.GetComponent<Text>();
- if (img == null || txt == null) return;
- img.fillAmount = value / 100f;
- txt.text = value + "%";
- }
- bool TryGetDeviceInfo(out int type, out bool bInitMac)
- {
- type = -1;
- bInitMac = false;
- var aim = Hub?.Aim;
- if (aim == null) return false;
- var player = (BluetoothPlayer)deviceIndex;
- SdkAimDeviceInfo info = aim.GetAimDeviceInfo(player);
- if (info == null) return false;
- type = aim.GetDeviceType(player);
- bInitMac = info.bInitMac;
- return true;
- }
- public void onShowDeviceInfo()
- {
- if (!TryGetDeviceInfo(out int deviceType, out bool bInitMac) || deviceType == -1 || !bInitMac)
- {
- bShowInfo = false;
- RefreshActionButtons();
- return;
- }
- bShowInfo = true;
- var deviceTypeEnum = (AimDeviceType)deviceType;
- if (Hub == null || !Hub.TryGetDeviceDisplay(deviceTypeEnum, out Sprite sprite, out string displayName))
- {
- bShowInfo = false;
- RefreshActionButtons();
- return;
- }
- SetDeviceTitle(displayName);
- SetDeviceSprite(sprite);
- RefreshActionButtons();
- }
- public void onUpdateStatusInfo()
- {
- if (Hub == null || curConnectStatus == null) return;
- if (IsConnecting())
- UpdateLoadingText();
- else
- StopLoading();
- string statusKey = ResolveStatusTextKey(Hub.GetStatusTextKey(bluetoothPlayer));
- curConnectStatus.text = AppUILocalization.GetTextByKey(statusKey, curConnectStatus) + omitText;
- curConnectStatus.color = IsConnectSuccess() ? StatusCompleteColor : StatusIncompleteColor;
- RefreshScreenPositioningStatusText();
- }
- void RefreshScreenPositioningStatusText()
- {
- if (curScreenPositioningStatus == null) return;
- if (!bInfraredDevices())
- {
- curScreenPositioningStatus.gameObject.SetActive(false);
- return;
- }
- curScreenPositioningStatus.gameObject.SetActive(true);
- bool positioned = IsScreenPositioned();
- curScreenPositioningStatus.text = AppUILocalization.GetTextByKey(positioned ? KeyScreenPositioningDone : KeyScreenPositioningIncomplete, curScreenPositioningStatus);
- curScreenPositioningStatus.color = positioned ? StatusCompleteColor : StatusIncompleteColor;
- }
- /// <summary>刷新主操作按钮与切换按钮的文案、样式与可见性。</summary>
- public void RefreshActionButtons()
- {
- ResolveActionButtonReferences();
- RefreshSwitchButton();
- RefreshPrimaryActionButton();
- RefreshScreenPositioningStatusText();
- }
- void RefreshSwitchButton()
- {
- if (btnSwitchDevice == null) return;
- bool show = bShowInfo;
- if (btnSwitchDevice.gameObject.activeSelf != show)
- btnSwitchDevice.gameObject.SetActive(show);
- if (!show) return;
- ApplyButtonStyle(btnSwitchDeviceBg, btnSwitchDeviceLabel, false, KeyBtnSwitch);
- }
- void RefreshPrimaryActionButton()
- {
- if (btnPrimaryAction == null) return;
- bool connected = IsConnectSuccess();
- bool screenPositioned = IsScreenPositioned();
- bool isInfrared = bInfraredDevices();
- bool isNineAxis = bNineAxisDevices();
- if (!bShowInfo)
- {
- btnPrimaryAction.gameObject.SetActive(false);
- return;
- }
- btnPrimaryAction.gameObject.SetActive(true);
- if (isInfrared)
- {
- if (connected)
- {
- _primaryActionKind = PrimaryActionKind.InfraredView;
- ApplyButtonStyle(
- btnPrimaryActionBg,
- btnPrimaryActionLabel,
- !screenPositioned,
- KeyBtnScreenPositioning);
- }
- else
- {
- _primaryActionKind = PrimaryActionKind.ConnectSideSlip;
- ApplyButtonStyle(btnPrimaryActionBg, btnPrimaryActionLabel, true, KeyBtnConnect);
- }
- return;
- }
- if (isNineAxis && connected)
- {
- _primaryActionKind = PrimaryActionKind.Disconnect;
- ApplyButtonStyle(btnPrimaryActionBg, btnPrimaryActionLabel, false, KeyBtnDisconnect);
- return;
- }
- _primaryActionKind = PrimaryActionKind.ConnectSideSlip;
- ApplyButtonStyle(btnPrimaryActionBg, btnPrimaryActionLabel, true, KeyBtnConnect);
- }
- static string ResolveStatusTextKey(string legacyKey)
- {
- return legacyKey switch
- {
- "Connect_BLE_NotConnected" => KeyStatusNotConnected,
- "Connect_BLE_Connecting" => KeyStatusConnecting,
- "Connect_BLE_Connected" => KeyStatusConnected,
- _ => legacyKey
- };
- }
- static void ApplyButtonStyle(Image bg, TMP_Text label, bool blackBackground, string textKey)
- {
- if (bg != null)
- bg.color = blackBackground ? BlackBg : WhiteBg;
- if (label != null)
- {
- label.text = AppUILocalization.GetTextByKey(textKey);
- label.color = blackBackground ? WhiteText : BlackText;
- }
- }
- void OnClickPrimaryAction()
- {
- AudioMgr.ins.PlayBtn();
- if (!getPanelStatus()) return;
- Hub?.SetActivePlayer(bluetoothPlayer);
- Hub?.Aim?.SetAimDeviceSelectIndex(deviceIndex);
- if (_primaryActionKind == PrimaryActionKind.InfraredView)
- {
- if (!IsConnectSuccess())
- {
- PopupMgr.ins.ShowTipTop(AppUILocalization.GetTextByKey(KeyTipInfraredConnect));
- return;
- }
- ViewManager.ShowView(UIViewType.InfraredView);
- return;
- }
- if (_primaryActionKind == PrimaryActionKind.Disconnect)
- {
- OnDisConnectedEvent();
- return;
- }
- Hub?.Aim?.PrepareDirectSideSlipConnect(deviceIndex);
- ModuleViewMgr.ins.ShowSideSlip(guideDeviceType: ResolveGuideDeviceTypeForSideSlip());
- }
- AimDeviceType? ResolveGuideDeviceTypeForSideSlip()
- {
- BLEAimhandler aim = Hub?.Aim;
- if (aim == null)
- return null;
- int deviceType = aim.GetDeviceType(bluetoothPlayer);
- if (deviceType == (int)AimDeviceType.NONE)
- return null;
- return (AimDeviceType)deviceType;
- }
- void OnClickSwitchDevice()
- {
- AudioMgr.ins.PlayBtn();
- if (!getPanelStatus()) return;
- Hub?.SetActivePlayer(bluetoothPlayer);
- Hub?.Aim?.SetAimDeviceSelectIndex(deviceIndex);
- if (IsConnectSuccess())
- OnDisConnectedEvent();
- Hub?.CancelConnecting();
- Hub?.Aim?.onCreateTempAimDeviceInfo();
- DeviceView deviceView = GetComponentInParent<DeviceView>();
- if (deviceView != null)
- {
- deviceView.BeginDeviceSelectionFlow();
- return;
- }
- if (GlobalData.MyDeviceMode == DeviceMode.Gun)
- ViewManager.ShowView(UIViewType.DeviceGunView);
- else
- ViewManager.ShowView(UIViewType.DeviceArcheryView);
- }
- void CloseAllPanelStatus()
- {
- DeviceView root = GetComponentInParent<DeviceView>();
- if (root == null) return;
- foreach (DeviceView_ItemShow item in root.GetComponentsInChildren<DeviceView_ItemShow>(true))
- {
- if (item.selectPanel != null)
- item.selectPanel.enabled = false;
- }
- }
- public void setPanelStatus(bool active)
- {
- if (selectPanel != null)
- selectPanel.enabled = active;
- if (!active) return;
- Hub?.SetActivePlayer(bluetoothPlayer);
- Hub?.Aim?.SetAimDeviceSelectIndex(deviceIndex);
- Debug.Log("当前选择的操作蓝牙用户 deviceSelectIndex:" + deviceIndex);
- }
- public bool getPanelStatus()
- {
- bool enable = selectPanel != null && selectPanel.enabled;
- CloseAllPanelStatus();
- setPanelStatus(true);
- return enable;
- }
- public bool getCurrentPanelEnable() => selectPanel != null && selectPanel.enabled;
- public bool getBLEConnectState() => IsConnectSuccess();
- public void OnConnectedEvent()
- {
- if (!getPanelStatus()) return;
- Hub?.Aim?.SetAimDeviceSelectIndex(deviceIndex);
- Hub?.Aim?.onCreateAimDeviceInfoById();
- if (IsConnectSuccess())
- {
- Hub?.ConnectActivePlayer();
- return;
- }
- if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
- Hub?.Connect1P();
- else
- Hub?.Connect2P();
- }
- public void OnDisConnectedEvent()
- {
- if (!getPanelStatus()) return;
- Hub?.Aim?.SetAimDeviceSelectIndex(deviceIndex);
- Hub?.Aim?.onCreateAimDeviceInfoById();
- if (bluetoothPlayer == BluetoothPlayer.FIRST_PLAYER)
- {
- if (Hub == null) return;
- if (IsConnectSuccess())
- Hub.Connect1P();
- else
- Hub.SetConnectCanceled(true);
- }
- else if (IsConnectSuccess())
- {
- Hub?.Set2PNoNeedToReconnect(false);
- Hub?.Connect2P();
- }
- }
- public void OnSelectEvent()
- {
- if (getCurrentPanelEnable()) return;
- AudioMgr.ins.PlayBtn();
- Hub?.Aim?.SetAimDeviceSelectIndex(deviceIndex);
- CloseAllPanelStatus();
- setPanelStatus(true);
- }
- public bool getCurrentItemVisible() => selectPanel.enabled && bShowInfo;
- public bool bInfraredDevices()
- {
- if (!TryGetDeviceInfo(out int deviceType, out bool bInitMac) || deviceType == -1 || !bInitMac)
- return false;
- switch ((AimDeviceType)deviceType)
- {
- case AimDeviceType.HOUYIPRO:
- case AimDeviceType.Gun:
- case AimDeviceType.ARTEMISPRO:
- case AimDeviceType.PistolM17:
- case AimDeviceType.RifleM416:
- return true;
- default:
- return false;
- }
- }
- public bool bNineAxisDevices()
- {
- if (!TryGetDeviceInfo(out int deviceType, out bool bInitMac) || deviceType == -1 || !bInitMac)
- return false;
- return deviceType == (int)AimDeviceType.HOUYI
- || deviceType == (int)AimDeviceType.HOUYI2
- || deviceType == (int)AimDeviceType.ARTEMIS;
- }
- }
- }
|