Update
This commit is contained in:
@@ -1,246 +1,126 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Linq;
|
||||
|
||||
namespace UI
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
[RequireComponent(typeof(UIDocument))]
|
||||
public class UIManager : MonoBehaviour
|
||||
{
|
||||
public static UIManager Instance { get; private set; }
|
||||
|
||||
[System.Serializable]
|
||||
public class ScreenData
|
||||
{
|
||||
public string screenName;
|
||||
public UIDocument document;
|
||||
public bool isOverlay;
|
||||
public bool isActive;
|
||||
}
|
||||
private UIDocument _uiDocument;
|
||||
private VisualElement _rootElement;
|
||||
|
||||
public List<ScreenData> screens = new List<ScreenData>();
|
||||
public string initialScreen = "MainMenu";
|
||||
|
||||
[Header("Cursor & Trail Settings")]
|
||||
public Sprite trailSprite;
|
||||
public float trailFadeSpeed = 3f;
|
||||
public int trailCount = 15;
|
||||
public float focusRadius = 500f;
|
||||
private readonly Dictionary<Type, BaseUIController> _controllers = new Dictionary<Type, BaseUIController>();
|
||||
private readonly Stack<BaseUIController> _history = new Stack<BaseUIController>();
|
||||
|
||||
private VisualElement _customCursor;
|
||||
private List<VisualElement> _trailPool = new List<VisualElement>();
|
||||
private int _trailIndex = 0;
|
||||
[Header("UI Templates")]
|
||||
[SerializeField] private VisualTreeAsset mainMenuTemplate;
|
||||
[SerializeField] private VisualTreeAsset lobbyTemplate;
|
||||
[SerializeField] private VisualTreeAsset profileTemplate;
|
||||
[SerializeField] private VisualTreeAsset settingsTemplate;
|
||||
[SerializeField] private VisualTreeAsset hudTemplate;
|
||||
|
||||
[Header("Editor Preview")]
|
||||
[Range(0f, 1f)]
|
||||
public float globalOpacity = 1f;
|
||||
[Header("Debug Settings")]
|
||||
[SerializeField] private bool showDebugInfo = true;
|
||||
|
||||
private Stack<string> _navigationStack = new Stack<string>();
|
||||
private string _currentScreenName;
|
||||
private VisualElement _lastHoveredElement;
|
||||
|
||||
public bool isMainMenuActive = false;
|
||||
private bool _isSettingsOpen = false;
|
||||
private MainMenuController _mainMenuController;
|
||||
private LobbyController _lobbyController;
|
||||
private ProfileController _profileController;
|
||||
private SettingsController _settingsController;
|
||||
private HUDController _hudController;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null) Instance = this;
|
||||
else { Destroy(gameObject); return; }
|
||||
|
||||
var myDoc = GetComponent<UIDocument>();
|
||||
if (myDoc != null) myDoc.sortingOrder = 1000;
|
||||
|
||||
SetupCursor();
|
||||
foreach (var s in screens)
|
||||
if (Instance == null)
|
||||
{
|
||||
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
ShowScreen(initialScreen);
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
_uiDocument = GetComponent<UIDocument>();
|
||||
_rootElement = _uiDocument.rootVisualElement;
|
||||
|
||||
InitializeControllers();
|
||||
}
|
||||
|
||||
private void SetupCursor()
|
||||
private void InitializeControllers()
|
||||
{
|
||||
UIDocument doc = GetComponent<UIDocument>();
|
||||
if (doc == null && screens.Count > 0) doc = screens[0].document;
|
||||
if (doc == null) return;
|
||||
|
||||
var root = doc.rootVisualElement;
|
||||
_mainMenuController = RegisterController<MainMenuController>(mainMenuTemplate);
|
||||
_lobbyController = RegisterController<LobbyController>(lobbyTemplate);
|
||||
_profileController = RegisterController<ProfileController>(profileTemplate);
|
||||
_settingsController = RegisterController<SettingsController>(settingsTemplate);
|
||||
_hudController = RegisterController<HUDController>(hudTemplate);
|
||||
|
||||
_customCursor = new VisualElement();
|
||||
_customCursor.style.width = 25; _customCursor.style.height = 25;
|
||||
_customCursor.style.backgroundColor = Color.white;
|
||||
_customCursor.style.borderTopLeftRadius = 13; _customCursor.style.borderTopRightRadius = 13;
|
||||
_customCursor.style.borderBottomLeftRadius = 13; _customCursor.style.borderBottomRightRadius = 13;
|
||||
_customCursor.style.position = Position.Absolute;
|
||||
_customCursor.pickingMode = PickingMode.Ignore;
|
||||
root.Add(_customCursor);
|
||||
// Start with Main Menu
|
||||
_ = Push<MainMenuController>();
|
||||
}
|
||||
|
||||
for (int i = 0; i < trailCount; i++)
|
||||
private T RegisterController<T>(VisualTreeAsset template) where T : BaseUIController, new()
|
||||
{
|
||||
if (template == null)
|
||||
{
|
||||
var trail = new VisualElement();
|
||||
trail.style.width = 20; trail.style.height = 20;
|
||||
if (trailSprite != null)
|
||||
{
|
||||
trail.style.backgroundImage = new StyleBackground(trailSprite);
|
||||
trail.style.backgroundColor = Color.clear;
|
||||
}
|
||||
else
|
||||
{
|
||||
trail.style.backgroundColor = new Color(1, 1, 1, 0.4f);
|
||||
trail.style.borderTopLeftRadius = 10; trail.style.borderTopRightRadius = 10;
|
||||
trail.style.borderBottomLeftRadius = 10; trail.style.borderBottomRightRadius = 10;
|
||||
}
|
||||
trail.style.position = Position.Absolute;
|
||||
trail.pickingMode = PickingMode.Ignore;
|
||||
root.Add(trail);
|
||||
_trailPool.Add(trail);
|
||||
Debug.LogWarning($"Template for {typeof(T).Name} is missing!");
|
||||
return null;
|
||||
}
|
||||
_customCursor.BringToFront();
|
||||
|
||||
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);
|
||||
_rootElement.Add(instance);
|
||||
|
||||
var controller = new T();
|
||||
controller.Initialize(instance, this);
|
||||
_controllers[typeof(T)] = controller;
|
||||
|
||||
return controller;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Vector2 mousePos = Input.mousePosition;
|
||||
bool isMainMenu = (_currentScreenName == "MainMenu");
|
||||
bool restrictY = (isMainMenu && isMainMenuActive && !_isSettingsOpen);
|
||||
float targetY = restrictY ? Screen.height / 2f : mousePos.y;
|
||||
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - targetY);
|
||||
|
||||
bool showCursor = !isMainMenu || _isSettingsOpen;
|
||||
DisplayStyle cursorDisplay = showCursor ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
|
||||
if (_customCursor != null)
|
||||
{
|
||||
_customCursor.style.display = cursorDisplay;
|
||||
_customCursor.style.left = uiPos.x - 12.5f;
|
||||
_customCursor.style.top = uiPos.y - 12.5f;
|
||||
}
|
||||
|
||||
if (_trailPool.Count > 0)
|
||||
{
|
||||
var currentTrail = _trailPool[_trailIndex];
|
||||
currentTrail.style.display = cursorDisplay;
|
||||
currentTrail.style.left = uiPos.x - 10;
|
||||
currentTrail.style.top = uiPos.y - 10;
|
||||
currentTrail.style.opacity = 0.6f;
|
||||
|
||||
foreach(var t in _trailPool)
|
||||
{
|
||||
float currentOp = t.style.opacity.value;
|
||||
if (currentOp > 0) t.style.opacity = Mathf.Max(0, currentOp - Time.deltaTime * trailFadeSpeed);
|
||||
else t.style.display = DisplayStyle.None;
|
||||
}
|
||||
_trailIndex = (_trailIndex + 1) % _trailPool.Count;
|
||||
}
|
||||
|
||||
HandleVirtualInput(uiPos);
|
||||
|
||||
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
|
||||
ToggleSettings();
|
||||
_mainMenuController?.Update();
|
||||
_hudController?.Update();
|
||||
}
|
||||
|
||||
private void HandleVirtualInput(Vector2 uiPos)
|
||||
public async Task Push<T>() where T : BaseUIController
|
||||
{
|
||||
UIDocument activeDoc = null;
|
||||
var settings = screens.Find(s => s.screenName == "Settings");
|
||||
if (_isSettingsOpen) activeDoc = settings.document;
|
||||
else activeDoc = screens.Find(s => s.screenName == _currentScreenName)?.document;
|
||||
if (activeDoc == null) return;
|
||||
|
||||
VisualElement bestElement = null;
|
||||
float minDistance = float.MaxValue;
|
||||
var interactables = activeDoc.rootVisualElement.Query<VisualElement>()
|
||||
.Where(e => e.focusable && e.pickingMode != PickingMode.Ignore).ToList();
|
||||
|
||||
foreach (var element in interactables)
|
||||
if (!_controllers.TryGetValue(typeof(T), out var newScreen))
|
||||
{
|
||||
Rect worldBounds = element.worldBound;
|
||||
float dist = Vector2.Distance(uiPos, worldBounds.center);
|
||||
if (dist < minDistance && dist < focusRadius) {
|
||||
minDistance = dist;
|
||||
bestElement = element;
|
||||
}
|
||||
Debug.LogError($"Controller of type {typeof(T)} not registered!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (bestElement != _lastHoveredElement)
|
||||
if (_history.Count > 0)
|
||||
{
|
||||
_lastHoveredElement?.RemoveFromClassList("hover");
|
||||
bestElement?.AddToClassList("hover");
|
||||
_lastHoveredElement = bestElement;
|
||||
var currentScreen = _history.Peek();
|
||||
await currentScreen.PlayTransitionOut();
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0) && _lastHoveredElement != null)
|
||||
{
|
||||
using (var clickEvent = ClickEvent.GetPooled()) {
|
||||
clickEvent.target = _lastHoveredElement;
|
||||
_lastHoveredElement.SendEvent(clickEvent);
|
||||
}
|
||||
}
|
||||
_history.Push(newScreen);
|
||||
await newScreen.PlayTransitionIn();
|
||||
}
|
||||
|
||||
// --- Editor Support Methods (Restored) ---
|
||||
|
||||
public void SyncScreens()
|
||||
public async Task Pop()
|
||||
{
|
||||
foreach (var screen in screens)
|
||||
{
|
||||
if (screen.document != null && screen.document.rootVisualElement != null)
|
||||
{
|
||||
screen.document.rootVisualElement.style.display =
|
||||
screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
screen.document.rootVisualElement.style.opacity = globalOpacity;
|
||||
}
|
||||
}
|
||||
if (_history.Count <= 1) return;
|
||||
|
||||
var currentScreen = _history.Pop();
|
||||
await currentScreen.PlayTransitionOut();
|
||||
|
||||
var previousScreen = _history.Peek();
|
||||
await previousScreen.PlayTransitionIn();
|
||||
}
|
||||
|
||||
public void ShowOnly(string name)
|
||||
{
|
||||
foreach (var screen in screens)
|
||||
{
|
||||
screen.isActive = (screen.screenName == name);
|
||||
}
|
||||
SyncScreens();
|
||||
}
|
||||
|
||||
// --- Runtime Logic ---
|
||||
|
||||
public void ShowScreen(string name)
|
||||
{
|
||||
var screen = screens.Find(s => s.screenName == name);
|
||||
if (screen == null) return;
|
||||
|
||||
if (!screen.isOverlay)
|
||||
{
|
||||
foreach(var s in screens) if(!s.isOverlay && s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
_navigationStack.Push(name);
|
||||
_currentScreenName = name;
|
||||
}
|
||||
|
||||
screen.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
screen.isActive = true;
|
||||
UnityEngine.Cursor.visible = false;
|
||||
}
|
||||
|
||||
public void GoBack()
|
||||
{
|
||||
if (_navigationStack.Count <= 1) return;
|
||||
string current = _navigationStack.Pop();
|
||||
var currentData = screens.Find(s => s.screenName == current);
|
||||
if (currentData != null) currentData.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
|
||||
_currentScreenName = _navigationStack.Peek();
|
||||
var prevData = screens.Find(s => s.screenName == _currentScreenName);
|
||||
if (prevData != null) prevData.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
public void ToggleSettings()
|
||||
{
|
||||
var settings = screens.Find(s => s.screenName == "Settings");
|
||||
if (settings == null) return;
|
||||
_isSettingsOpen = settings.document.rootVisualElement.style.display == DisplayStyle.None;
|
||||
settings.document.rootVisualElement.style.display = _isSettingsOpen ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
if (_isSettingsOpen) settings.document.sortingOrder = 999;
|
||||
}
|
||||
// Custom Inspector features can be added here with [ContextMenu] or CustomEditor
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user