219 lines
7.1 KiB
C#
219 lines
7.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using System.Linq;
|
|
|
|
namespace UI
|
|
{
|
|
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 Texture2D customCursor;
|
|
public bool isActive; // For Editor and Initial state
|
|
}
|
|
|
|
public List<ScreenData> screens = new List<ScreenData>();
|
|
|
|
[Header("Settings")]
|
|
public string initialScreen = "MainMenu";
|
|
public float focusRadius = 300f;
|
|
[Range(0f, 1f)]
|
|
public float globalOpacity = 1f;
|
|
|
|
private Stack<string> _navigationStack = new Stack<string>();
|
|
private string _currentScreenName;
|
|
private VisualElement _lastHoveredElement;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null) Instance = this;
|
|
else { Destroy(gameObject); return; }
|
|
|
|
// Initialize all screens based on isActive or hidden
|
|
foreach (var screen in screens)
|
|
{
|
|
if (screen.document != null)
|
|
{
|
|
// Ensure the root has the screen-root class for transitions
|
|
var root = screen.document.rootVisualElement;
|
|
if (root != null)
|
|
{
|
|
root.AddToClassList("screen-root");
|
|
root.style.display = DisplayStyle.None;
|
|
}
|
|
}
|
|
}
|
|
|
|
ShowScreen(initialScreen);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
HandleGlobalInputs();
|
|
HandleCursorlessFocus();
|
|
}
|
|
|
|
// --- Editor Support Methods ---
|
|
|
|
public void SyncScreens()
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ShowOnly(string name)
|
|
{
|
|
foreach (var screen in screens)
|
|
{
|
|
screen.isActive = (screen.screenName == name);
|
|
}
|
|
SyncScreens();
|
|
}
|
|
|
|
// --- Runtime Logic ---
|
|
|
|
private void HandleGlobalInputs()
|
|
{
|
|
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
|
|
{
|
|
ToggleSettings();
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
{
|
|
if (_navigationStack.Count > 1)
|
|
{
|
|
GoBack();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void HandleCursorlessFocus()
|
|
{
|
|
if (string.IsNullOrEmpty(_currentScreenName)) return;
|
|
|
|
var screen = screens.Find(s => s.screenName == _currentScreenName);
|
|
if (screen == null || screen.document == null) return;
|
|
|
|
Vector2 mousePos = Input.mousePosition;
|
|
Vector2 uiMousePos = new Vector2(mousePos.x, Screen.height - mousePos.y);
|
|
|
|
VisualElement bestElement = null;
|
|
float minDistance = float.MaxValue;
|
|
|
|
var interactiveElements = screen.document.rootVisualElement.Query<VisualElement>()
|
|
.Where(e => e.focusable && e.pickingMode == PickingMode.Position).ToList();
|
|
|
|
foreach (var element in interactiveElements)
|
|
{
|
|
Rect worldBounds = element.worldBound;
|
|
Vector2 center = worldBounds.center;
|
|
float dist = Vector2.Distance(uiMousePos, center);
|
|
|
|
if (dist < minDistance && dist < focusRadius)
|
|
{
|
|
minDistance = dist;
|
|
bestElement = element;
|
|
}
|
|
}
|
|
|
|
if (bestElement != _lastHoveredElement)
|
|
{
|
|
_lastHoveredElement?.RemoveFromClassList("hover");
|
|
bestElement?.AddToClassList("hover");
|
|
_lastHoveredElement = bestElement;
|
|
}
|
|
|
|
if (Input.GetMouseButtonDown(0) && _lastHoveredElement != null)
|
|
{
|
|
using (var clickEvent = ClickEvent.GetPooled())
|
|
{
|
|
clickEvent.target = _lastHoveredElement;
|
|
_lastHoveredElement.SendEvent(clickEvent);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ShowScreen(string name)
|
|
{
|
|
var nextData = screens.Find(s => s.screenName == name);
|
|
if (nextData == null) return;
|
|
|
|
// Hide all first for a clean state at runtime
|
|
foreach (var s in screens)
|
|
{
|
|
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
|
s.isActive = false;
|
|
}
|
|
|
|
_navigationStack.Push(name);
|
|
_currentScreenName = name;
|
|
nextData.isActive = true;
|
|
|
|
nextData.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
|
nextData.document.rootVisualElement.style.opacity = globalOpacity;
|
|
ApplyCursorSettings(nextData);
|
|
}
|
|
|
|
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;
|
|
ApplyCursorSettings(prevData);
|
|
}
|
|
}
|
|
|
|
public void ToggleSettings()
|
|
{
|
|
var settings = screens.Find(s => s.screenName == "Settings");
|
|
if (settings == null) return;
|
|
|
|
bool isShowing = settings.document.rootVisualElement.style.display == DisplayStyle.Flex;
|
|
settings.document.rootVisualElement.style.display = isShowing ? DisplayStyle.None : DisplayStyle.Flex;
|
|
|
|
if (!isShowing)
|
|
{
|
|
settings.document.sortingOrder = 999;
|
|
}
|
|
}
|
|
|
|
private void ApplyCursorSettings(ScreenData data)
|
|
{
|
|
if (data.screenName == "HUD")
|
|
{
|
|
UnityEngine.Cursor.visible = false;
|
|
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
|
|
}
|
|
else
|
|
{
|
|
UnityEngine.Cursor.visible = false;
|
|
UnityEngine.Cursor.lockState = CursorLockMode.None;
|
|
}
|
|
}
|
|
}
|
|
}
|