| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- using AdaptUI;
- using AppUI.Bluetooth;
- using AppUI.Manager.Role;
- using AppUI.Manager.View;
- using AppUI.View.Home.DeviceView;
- using BestHTTP.SignalR.Hubs;
- using InfraredManager;
- using SmartBowSDK;
- using System;
- using System.Collections;
- using SdkAimDeviceInfo = SmartBowSDK.AimDeviceInfo;
- using TMPro;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- namespace AppUI.View.Home.Main
- {
- /**
- * 新处理HomeView,包含主页的主要功能,如显示用户信息、连接蓝牙设备、显示挑战选项等。
- 1. 显示用户头像和昵称。
- 2. 显示游戏版本号(目前隐藏)。
- 3. 提供连接蓝牙设备的按钮,并根据连接状态更新按钮显示。
- 4. 显示好友请求提示。
- 5. 显示电池状态图标。
- 6. 包含挑战选项和顶部栏视图,并处理它们之间的交互。
- */
- public class HomeView : MonoBehaviour
- {
- public static bool bOpenOtherView = false;
- public static string openName = "";
- [SerializeField] Image myAvatarSprite;
- [SerializeField] TMP_Text nickNameText;
- [SerializeField] Text gameVersion;
- //[SerializeField] GameObject friendBarTipTop;
- [SerializeField] GameObject friendBarTipBottom;
- [SerializeField] HomeViewChallengeOption challengeOption;
- public HomeViewChallengeOption MyChallengeOption => challengeOption;
- [SerializeField] HomeViewTopBar topBarView;
- public HomeViewTopBar MyTopBarView => topBarView;
- public static HomeView ins;
- SmartBowDeviceHub Hub => SmartBowDeviceHub.ins;
- [SerializeField] TMP_Text curConnectStatus;
- [SerializeField] TMP_Text curScreenPositioningStatus;
- void Awake()
- {
- ins = this;
- gameVersion.text = "V" + Application.version;
- gameVersion.gameObject.SetActive(false); //隐藏版本号
- if (CommonConfig.needToExamine)
- {
- transform.Find("LeftPanel/Item (1)").gameObject.SetActive(false);
- transform.Find("RightPanel/Item/Text").GetComponent<TextAutoLanguage>().SetText(1234562);
- transform.Find("RightPanel/Item (1)/Text").GetComponent<TextAutoLanguage>().SetText(1234563);
- transform.Find("RightPanel/Item (3)").gameObject.SetActive(false);
- }
- //动态添加信息
- challengeOption.AddLevelInfo();
- if (topBarView && challengeOption) topBarView.onChangeTypeEvent.AddListener(challengeOption.refreshList);
- //设置相机视野角度,根据设备类型
- // 25 iPhone 40 iPad
- var deviceType = DeviceTypeHelper.DetectDeviceType();
- switch (deviceType)
- {
- case DeviceTypeHelper.DeviceType.iPhone:
- UserSettings.ins.bowRotateConvert.fieldOfView = 25;
- break;
- case DeviceTypeHelper.DeviceType.iPad:
- UserSettings.ins.bowRotateConvert.fieldOfView = 40;
- break;
- default:
- UserSettings.ins.bowRotateConvert.fieldOfView = 25;
- break;
- }
- }
- IEnumerator Start()
- {
- transform.SetSiblingIndex(0); //放在ui最底层
- SmartBowDeviceHub.EnsureReady();
- if (SmartBowDeviceHub.ins?.IsConnected(BluetoothPlayer.FIRST_PLAYER) == true)
- SmartBowDeviceHub.ins.SetMainConnectDeviceType();
- AudioMgr.Init();
- if (LoginMgr.myUserInfo.id > 0 || (CommonConfig.StandaloneMode && UserPlayer.ins != null && UserPlayer.ins.hasGetUserInfo))
- {
- RenderNameOrGender();
- RenderMyAvatarSprite();
- }
- //如果需要初始化跳转页面,这里处理
- Debug.Log("bOpenOtherView:" + bOpenOtherView);
- if (bOpenOtherView)
- {
- bOpenOtherView = false;
- switch (openName)
- {
- case "DeviceView":
- // ViewMgr.Instance.ShowView<DeviceView>();
- break;
- default:
- break;
- }
- }
- yield return new WaitForSeconds(1.0f);
- // 登录/认证成功后再自动连蓝牙(与 AuthLoginMask 重连互不干扰)
- UserPlayer.TryHomeAutoConnectBle();
- }
- void OnDestroy()
- {
- if (ins == this) ins = null;
- }
- void Update()
- {
- UpdateCheckRenderFriendTip();
- onUpdateStatusInfo();
- RefreshScreenPositioningStatusText();
- }
- public static bool ShowProminentBeforeConnectBLE(Action onAgree = null)
- {
- if (CommonConfig.StandaloneMode) return false;
- if (SceneManager.GetActiveScene().name.Equals("HeartRateBand")) return false;
- if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0)
- {
- //ModalView m = ModalView.Show();
- ModalConfirmView m = ModalConfirmView.Show();
- string pName = Application.productName;
- m.textKey = "Model_Location-Prominent";
- //m.onAgreeTextKey = "common_next";
- m.onConfirmTextKey = "common_next";
- //m.onRejectTextKey = "common_cancel";
- m.textFormatArgs = new object[] { pName };
- //m.onAgree = () => {
- m.onConfirm = () =>
- {
- PlayerPrefs.SetInt("Location-Prominent", 1);
- try
- {
- onAgree?.Invoke();
- }
- catch (System.Exception e)
- {
- Debug.LogError(e.Message + "\n" + e.StackTrace);
- }
- };
- return true;
- }
- return false;
- }
-
- public void OnClick_ConnectBLE()
- {
- if (ShowProminentBeforeConnectBLE()) return;
- SmartBowDeviceHub.ins?.Connect1P();
- }
- public void OnClick_ShowDeviceView()
- {
- AudioMgr.ins.PlayBtn();
- //ViewManager.ShowView<DeviceView>();
- }
- public void RenderNameOrGender()
- {
- nickNameText.text = LoginMgr.myUserInfo.nickname;
- //genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true);
- //genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false);
- LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent<RectTransform>());
- }
- public void RenderMyAvatarSprite()
- {
- RoleManager.SetAvatarToImage(myAvatarSprite, LoginMgr.myUserInfo.avatarID, LoginMgr.myUserInfo.avatarUrl);
- }
- int flag_SwapFriendAndRank = 0;
- //检测是否需要提示有人加我好友
- bool _old_hasFriendRequest = false;
- int _old_flag_SwapFriendAndRank = 0;
- void UpdateCheckRenderFriendTip()
- {
- if (UserPlayer.ins == null) return;
- if (
- UserPlayer.ins.tempData.hasFriendRequest == _old_hasFriendRequest &&
- flag_SwapFriendAndRank == _old_flag_SwapFriendAndRank
- ) return;
- _old_hasFriendRequest = UserPlayer.ins.tempData.hasFriendRequest;
- _old_flag_SwapFriendAndRank = flag_SwapFriendAndRank;
- //friendBarTipTop.SetActive(
- // flag_SwapFriendAndRank == 1 &&
- // UserPlayer.ins.tempData.hasFriendRequest);
- friendBarTipBottom.SetActive(
- flag_SwapFriendAndRank == 0 &&
- UserPlayer.ins.tempData.hasFriendRequest);
- }
- [SerializeField] BluetoothPlayer bluetoothPlayer = BluetoothPlayer.FIRST_PLAYER;
- readonly Color32 StatusCompleteColor = new Color32(0, 0, 0, 255);
- readonly Color32 StatusIncompleteColor = new Color32(145, 144, 144, 255);
- bool IsConnecting()
- {
- return Hub != null && Hub.IsConnecting(bluetoothPlayer);
- }
- public bool IsConnectSuccess()
- {
- return Hub != null && Hub.IsConnected(bluetoothPlayer);
- }
- public bool IsScreenPositioned()
- {
- InfraredCameraHelper helper = InfraredCameraHelper.GetInstance();
- return helper != null && helper.IsScreenLoateOK();
- }
- bool TryGetDeviceInfo(out int deviceType, out bool bInitMac)
- {
- deviceType = -1;
- bInitMac = false;
- var aim = Hub?.Aim;
- if (aim == null) return false;
- SdkAimDeviceInfo info = aim.GetAimDeviceInfo(bluetoothPlayer);
- if (info == null) return false;
- deviceType = aim.GetDeviceType(bluetoothPlayer);
- bInitMac = info.bInitMac;
- return true;
- }
- bool HasRecordedDeviceInfo()
- {
- return TryGetDeviceInfo(out int deviceType, out bool bInitMac)
- && deviceType != -1
- && bInitMac;
- }
- bool TryGetDeviceDisplayName(out string displayName)
- {
- displayName = null;
- if (!TryGetDeviceInfo(out int deviceType, out _) || deviceType == -1)
- return false;
- return Hub != null
- && Hub.TryGetDeviceDisplay((AimDeviceType)deviceType, out _, out displayName)
- && !string.IsNullOrEmpty(displayName);
- }
- bool IsInfraredDevice()
- {
- 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;
- }
- }
- bool IsInfraredCameraReady() => InfraredDemo.IsUVCReady;
- public void onUpdateStatusInfo()
- {
- if (Hub == null || curConnectStatus == null) return;
- if (!HasRecordedDeviceInfo())
- {
- if (curConnectStatus.gameObject.activeSelf)
- curConnectStatus.gameObject.SetActive(false);
- return;
- }
- if (!curConnectStatus.gameObject.activeSelf)
- curConnectStatus.gameObject.SetActive(true);
- string statusText = TextAutoLanguage2.GetTextByKey(Hub.GetStatusTextKey(bluetoothPlayer));
- if (TryGetDeviceDisplayName(out string displayName))
- curConnectStatus.text = $"{displayName} {statusText}";
- else
- curConnectStatus.text = statusText;
- curConnectStatus.color = IsConnectSuccess() ? StatusCompleteColor : StatusIncompleteColor;
- }
- void RefreshScreenPositioningStatusText()
- {
- if (curScreenPositioningStatus == null) return;
- if (!IsInfraredDevice() || !IsInfraredCameraReady())
- {
- if (curScreenPositioningStatus.gameObject.activeSelf)
- curScreenPositioningStatus.gameObject.SetActive(false);
- return;
- }
- if (!curScreenPositioningStatus.gameObject.activeSelf)
- curScreenPositioningStatus.gameObject.SetActive(true);
- bool positioned = IsScreenPositioned();
- curScreenPositioningStatus.text = positioned ? "屏幕定位已完成" : "屏幕定位未完成";
- curScreenPositioningStatus.color = positioned ? StatusCompleteColor : StatusIncompleteColor;
- }
- }
- }
|