339 lines
13 KiB
C#
339 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using PrimeTween;
|
|
using OnlyScove.Scripts;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
namespace Hallucinate.UI
|
|
{
|
|
[RequireComponent(typeof(UIDocument))]
|
|
public class UIManager : MonoBehaviour
|
|
{
|
|
public static UIManager Instance { get; private set; }
|
|
|
|
private UIDocument _uiDocument;
|
|
private VisualElement _rootElement;
|
|
private VisualElement _cursorLayer;
|
|
private VisualElement _mainCursor;
|
|
|
|
private readonly Dictionary<Type, BaseUIController> _controllers = new Dictionary<Type, BaseUIController>();
|
|
private readonly Stack<BaseUIController> _history = new Stack<BaseUIController>();
|
|
|
|
[Header("References")]
|
|
[SerializeField] private InputReader inputReader;
|
|
|
|
[Header("Game Metadata")]
|
|
[SerializeField] private Texture2D gameIcon;
|
|
|
|
[Header("Cursor & Effects Settings")]
|
|
[SerializeField] private Sprite cursorSprite;
|
|
[SerializeField] private Sprite cursorTrailSprite;
|
|
[SerializeField, Range(10f, 150f)] private float cursorSize = 40f;
|
|
[SerializeField, Range(5, 50)] private int trailLength = 20;
|
|
[SerializeField, Range(1, 10)] private int trailSpacing = 2;
|
|
[SerializeField] private bool enableRipples = true;
|
|
[SerializeField] private Color rippleColor = new Color(1, 1, 1, 0.4f);
|
|
|
|
[Header("UI Templates")]
|
|
[SerializeField] private VisualTreeAsset loginTemplate;
|
|
[SerializeField] private VisualTreeAsset mainMenuTemplate;
|
|
[SerializeField] private VisualTreeAsset lobbyTemplate;
|
|
[SerializeField] private VisualTreeAsset roomItemTemplate; // Template cho dòng phòng
|
|
[SerializeField] private VisualTreeAsset profileTemplate;
|
|
[SerializeField] private VisualTreeAsset settingsTemplate;
|
|
[SerializeField] private VisualTreeAsset hudTemplate;
|
|
|
|
private LoginController _loginController;
|
|
private MainMenuController _mainMenuController;
|
|
private LobbyController _lobbyController;
|
|
private SettingsController _settingsController;
|
|
private List<VisualElement> _trailSegments = new List<VisualElement>();
|
|
private List<Vector2> _posHistory = new List<Vector2>();
|
|
|
|
private Vector2 _lastMousePos;
|
|
private float _trailOpacity = 1f;
|
|
private bool _isSettingsOpen = false;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
_uiDocument = GetComponent<UIDocument>();
|
|
UnityEngine.Cursor.visible = false;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (_uiDocument == null) return;
|
|
_rootElement = _uiDocument.rootVisualElement;
|
|
|
|
_cursorLayer = new VisualElement();
|
|
_cursorLayer.name = "CursorLayer";
|
|
_cursorLayer.style.position = Position.Absolute;
|
|
_cursorLayer.style.width = Length.Percent(100);
|
|
_cursorLayer.style.height = Length.Percent(100);
|
|
_cursorLayer.pickingMode = PickingMode.Ignore;
|
|
_rootElement.Add(_cursorLayer);
|
|
|
|
SetupVirtualCursor();
|
|
|
|
_rootElement.RegisterCallback<PointerDownEvent>(OnGlobalClick, TrickleDown.TrickleDown);
|
|
|
|
if (inputReader != null)
|
|
{
|
|
inputReader.OnToggleSettingsEvent += ToggleSettings;
|
|
inputReader.OnCancelEvent += HandleCancel;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
if (gameIcon == null)
|
|
{
|
|
var icons = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Unknown);
|
|
if (icons != null && icons.Length > 0) gameIcon = icons[0];
|
|
}
|
|
#endif
|
|
InitializeControllers();
|
|
CheckLoginStatus();
|
|
}
|
|
|
|
private void CheckLoginStatus()
|
|
{
|
|
string savedName = PlayerPrefs.GetString("Username", "");
|
|
if (string.IsNullOrEmpty(savedName))
|
|
{
|
|
_ = Push<LoginController>();
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"[UIManager] Welcome back, {savedName}!");
|
|
_ = Push<MainMenuController>();
|
|
}
|
|
}
|
|
|
|
public void OnLoginSuccess()
|
|
{
|
|
_ = Push<MainMenuController>();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (inputReader != null)
|
|
{
|
|
inputReader.OnToggleSettingsEvent -= ToggleSettings;
|
|
inputReader.OnCancelEvent -= HandleCancel;
|
|
}
|
|
}
|
|
|
|
private void HandleCancel()
|
|
{
|
|
if (_isSettingsOpen) ToggleSettings();
|
|
}
|
|
|
|
public async void ToggleSettings()
|
|
{
|
|
if (_settingsController == null) return;
|
|
|
|
if (!_isSettingsOpen)
|
|
{
|
|
_isSettingsOpen = true;
|
|
_settingsController.Root.BringToFront();
|
|
_cursorLayer.BringToFront();
|
|
await _settingsController.PlayTransitionIn();
|
|
}
|
|
else
|
|
{
|
|
_isSettingsOpen = false;
|
|
await _settingsController.PlayTransitionOut();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_mainMenuController != null) _mainMenuController.Update();
|
|
UpdateCursorAndTrail();
|
|
}
|
|
|
|
private void SetupVirtualCursor()
|
|
{
|
|
_cursorLayer.Clear();
|
|
_trailSegments.Clear();
|
|
_posHistory.Clear();
|
|
|
|
if (cursorTrailSprite != null)
|
|
{
|
|
for (int i = 0; i < trailLength; i++)
|
|
{
|
|
var segment = new VisualElement();
|
|
segment.style.position = Position.Absolute;
|
|
segment.style.width = cursorSize;
|
|
segment.style.height = cursorSize;
|
|
segment.style.backgroundImage = new StyleBackground(Background.FromSprite(cursorTrailSprite));
|
|
|
|
float ratio = 1f - ((float)i / trailLength);
|
|
segment.style.opacity = 0f;
|
|
segment.style.scale = new StyleScale(new Scale(new Vector3(ratio, ratio, 1f)));
|
|
segment.style.translate = new StyleTranslate(new Translate(Length.Percent(-50), Length.Percent(-50)));
|
|
segment.pickingMode = PickingMode.Ignore;
|
|
|
|
_cursorLayer.Add(segment);
|
|
_trailSegments.Add(segment);
|
|
}
|
|
}
|
|
|
|
_mainCursor = new VisualElement();
|
|
_mainCursor.style.position = Position.Absolute;
|
|
_mainCursor.style.width = cursorSize;
|
|
_mainCursor.style.height = cursorSize;
|
|
_mainCursor.style.backgroundImage = new StyleBackground(Background.FromSprite(cursorSprite != null ? cursorSprite : cursorTrailSprite));
|
|
_mainCursor.style.translate = new StyleTranslate(new Translate(Length.Percent(-50), Length.Percent(-50)));
|
|
_mainCursor.pickingMode = PickingMode.Ignore;
|
|
_cursorLayer.Add(_mainCursor);
|
|
|
|
Vector2 startPos = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
|
|
_lastMousePos = startPos;
|
|
for (int i = 0; i < trailLength * trailSpacing + 1; i++) _posHistory.Add(startPos);
|
|
}
|
|
|
|
private void OnGlobalClick(PointerDownEvent evt)
|
|
{
|
|
if (!enableRipples) return;
|
|
|
|
var ripple = new VisualElement();
|
|
ripple.style.position = Position.Absolute;
|
|
ripple.style.width = cursorSize;
|
|
ripple.style.height = cursorSize;
|
|
ripple.style.translate = new StyleTranslate(new Translate(Length.Percent(-50), Length.Percent(-50)));
|
|
|
|
var radius = new StyleLength(new Length(50, LengthUnit.Percent));
|
|
ripple.style.borderTopLeftRadius = radius;
|
|
ripple.style.borderTopRightRadius = radius;
|
|
ripple.style.borderBottomLeftRadius = radius;
|
|
ripple.style.borderBottomRightRadius = radius;
|
|
|
|
ripple.style.borderTopColor = rippleColor;
|
|
ripple.style.borderBottomColor = rippleColor;
|
|
ripple.style.borderLeftColor = rippleColor;
|
|
ripple.style.borderRightColor = rippleColor;
|
|
ripple.style.borderTopWidth = 2;
|
|
ripple.style.borderBottomWidth = 2;
|
|
ripple.style.borderLeftWidth = 2;
|
|
ripple.style.borderRightWidth = 2;
|
|
|
|
ripple.style.left = evt.localPosition.x;
|
|
ripple.style.top = evt.localPosition.y;
|
|
ripple.pickingMode = PickingMode.Ignore;
|
|
|
|
_cursorLayer.Add(ripple);
|
|
|
|
Tween.Scale(ripple.transform, Vector3.one * 2.5f, duration: 0.4f, ease: Ease.OutQuad);
|
|
Tween.Custom(1f, 0f, duration: 0.4f, onValueChange: val => ripple.style.opacity = val)
|
|
.OnComplete(() => ripple.RemoveFromHierarchy());
|
|
}
|
|
|
|
private void UpdateCursorAndTrail()
|
|
{
|
|
if (!Application.isFocused)
|
|
{
|
|
if (_cursorLayer != null) _cursorLayer.style.display = DisplayStyle.None;
|
|
return;
|
|
}
|
|
|
|
Vector3 mousePos = Input.mousePosition;
|
|
bool isMouseInWindow = mousePos.x >= 0 && mousePos.x <= Screen.width && mousePos.y >= 0 && mousePos.y <= Screen.height;
|
|
|
|
if (!isMouseInWindow)
|
|
{
|
|
if (_cursorLayer != null) _cursorLayer.style.display = DisplayStyle.None;
|
|
return;
|
|
}
|
|
|
|
if (_cursorLayer != null) _cursorLayer.style.display = DisplayStyle.Flex;
|
|
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - mousePos.y);
|
|
|
|
float mouseSpeed = Vector2.Distance(uiPos, _lastMousePos);
|
|
_lastMousePos = uiPos;
|
|
|
|
if (mouseSpeed > 0.1f) _trailOpacity = Mathf.MoveTowards(_trailOpacity, 1f, Time.deltaTime * 5f);
|
|
else _trailOpacity = Mathf.MoveTowards(_trailOpacity, 0f, Time.deltaTime * 3f);
|
|
|
|
_posHistory.Insert(0, uiPos);
|
|
if (_posHistory.Count > trailLength * trailSpacing + 1)
|
|
_posHistory.RemoveAt(_posHistory.Count - 1);
|
|
|
|
if (_mainCursor != null)
|
|
{
|
|
_mainCursor.style.left = uiPos.x;
|
|
_mainCursor.style.top = uiPos.y;
|
|
}
|
|
|
|
for (int i = 0; i < _trailSegments.Count; i++)
|
|
{
|
|
int historyIndex = (i + 1) * trailSpacing;
|
|
if (historyIndex < _posHistory.Count)
|
|
{
|
|
_trailSegments[i].style.left = _posHistory[historyIndex].x;
|
|
_trailSegments[i].style.top = _posHistory[historyIndex].y;
|
|
float baseRatio = 1f - ((float)i / trailLength);
|
|
_trailSegments[i].style.opacity = baseRatio * 0.5f * _trailOpacity;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void InitializeControllers()
|
|
{
|
|
_mainMenuController = RegisterController<MainMenuController>(mainMenuTemplate);
|
|
if (_mainMenuController != null && gameIcon != null) _mainMenuController.SetGameIcon(gameIcon);
|
|
|
|
_lobbyController = RegisterController<LobbyController>(lobbyTemplate);
|
|
if (_lobbyController != null) _lobbyController.SetRoomTemplate(roomItemTemplate);
|
|
|
|
RegisterController<ProfileController>(profileTemplate);
|
|
_settingsController = RegisterController<SettingsController>(settingsTemplate);
|
|
RegisterController<HUDController>(hudTemplate);
|
|
|
|
_loginController = RegisterController<LoginController>(loginTemplate);
|
|
}
|
|
|
|
private T RegisterController<T>(VisualTreeAsset template) where T : BaseUIController, new()
|
|
{
|
|
if (template == null) return null;
|
|
var instance = template.Instantiate();
|
|
instance.style.flexGrow = 1;
|
|
instance.style.position = Position.Absolute;
|
|
instance.style.width = Length.Percent(100);
|
|
instance.style.height = Length.Percent(100);
|
|
instance.style.display = DisplayStyle.None;
|
|
_rootElement.Add(instance);
|
|
if (_cursorLayer != null) _cursorLayer.BringToFront();
|
|
|
|
var controller = new T();
|
|
controller.Initialize(instance, this);
|
|
_controllers[typeof(T)] = controller;
|
|
return controller;
|
|
}
|
|
|
|
public async Task Push<T>() where T : BaseUIController
|
|
{
|
|
if (!_controllers.TryGetValue(typeof(T), out var newScreen)) return;
|
|
if (_history.Count > 0 && _history.Peek() == newScreen) return;
|
|
if (_history.Count > 0) await _history.Peek().PlayTransitionOut();
|
|
|
|
_history.Push(newScreen);
|
|
await newScreen.PlayTransitionIn();
|
|
if (_cursorLayer != null) _cursorLayer.BringToFront();
|
|
}
|
|
|
|
public async Task Pop()
|
|
{
|
|
if (_history.Count <= 1) return;
|
|
await _history.Pop().PlayTransitionOut();
|
|
if (_history.Count > 0) await _history.Peek().PlayTransitionIn();
|
|
if (_cursorLayer != null) _cursorLayer.BringToFront();
|
|
}
|
|
}
|
|
}
|