247 lines
9.3 KiB
C#
247 lines
9.3 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 bool isActive;
|
|
}
|
|
|
|
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 VisualElement _customCursor;
|
|
private List<VisualElement> _trailPool = new List<VisualElement>();
|
|
private int _trailIndex = 0;
|
|
|
|
[Header("Editor Preview")]
|
|
[Range(0f, 1f)]
|
|
public float globalOpacity = 1f;
|
|
|
|
private Stack<string> _navigationStack = new Stack<string>();
|
|
private string _currentScreenName;
|
|
private VisualElement _lastHoveredElement;
|
|
|
|
public bool isMainMenuActive = false;
|
|
private bool _isSettingsOpen = false;
|
|
|
|
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 (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
|
}
|
|
ShowScreen(initialScreen);
|
|
}
|
|
|
|
private void SetupCursor()
|
|
{
|
|
UIDocument doc = GetComponent<UIDocument>();
|
|
if (doc == null && screens.Count > 0) doc = screens[0].document;
|
|
if (doc == null) return;
|
|
|
|
var root = doc.rootVisualElement;
|
|
|
|
_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);
|
|
|
|
for (int i = 0; i < trailCount; i++)
|
|
{
|
|
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);
|
|
}
|
|
_customCursor.BringToFront();
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
private void HandleVirtualInput(Vector2 uiPos)
|
|
{
|
|
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)
|
|
{
|
|
Rect worldBounds = element.worldBound;
|
|
float dist = Vector2.Distance(uiPos, worldBounds.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);
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Editor Support Methods (Restored) ---
|
|
|
|
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 ---
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|