Files
BABA_YAGA/Assets/Scripts/UI/UIManager.cs

247 lines
9.3 KiB
C#
Raw Normal View History

2026-04-25 18:20:16 +07:00
using System.Collections;
2026-04-23 23:09:54 +07:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
2026-04-26 04:39:59 +07:00
using System.Linq;
2026-04-23 23:09:54 +07:00
namespace UI
{
public class UIManager : MonoBehaviour
{
2026-04-25 18:20:16 +07:00
public static UIManager Instance { get; private set; }
[System.Serializable]
2026-04-23 23:09:54 +07:00
public class ScreenData
{
2026-04-26 04:39:59 +07:00
public string screenName;
2026-04-23 23:09:54 +07:00
public UIDocument document;
2026-04-25 18:20:16 +07:00
public bool isOverlay;
2026-04-26 05:02:49 +07:00
public bool isActive;
2026-04-23 23:09:54 +07:00
}
public List<ScreenData> screens = new List<ScreenData>();
public string initialScreen = "MainMenu";
2026-04-26 05:02:49 +07:00
2026-04-27 15:48:17 +07:00
[Header("Cursor & Trail Settings")]
public Sprite trailSprite;
public float trailFadeSpeed = 3f;
public int trailCount = 15;
public float focusRadius = 500f;
2026-04-26 05:02:49 +07:00
private VisualElement _customCursor;
private List<VisualElement> _trailPool = new List<VisualElement>();
private int _trailIndex = 0;
[Header("Editor Preview")]
2026-04-26 04:39:59 +07:00
[Range(0f, 1f)]
public float globalOpacity = 1f;
2026-04-25 18:20:16 +07:00
2026-04-26 04:39:59 +07:00
private Stack<string> _navigationStack = new Stack<string>();
2026-04-25 18:20:16 +07:00
private string _currentScreenName;
2026-04-26 04:39:59 +07:00
private VisualElement _lastHoveredElement;
2026-04-27 13:27:40 +07:00
public bool isMainMenuActive = false;
2026-04-26 05:02:49 +07:00
private bool _isSettingsOpen = false;
2026-04-25 18:20:16 +07:00
private void Awake()
{
if (Instance == null) Instance = this;
2026-04-26 04:39:59 +07:00
else { Destroy(gameObject); return; }
2026-04-27 15:48:17 +07:00
var myDoc = GetComponent<UIDocument>();
if (myDoc != null) myDoc.sortingOrder = 1000;
2026-04-26 05:02:49 +07:00
SetupCursor();
foreach (var s in screens)
2026-04-25 18:20:16 +07:00
{
2026-04-26 05:02:49 +07:00
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
2026-04-25 18:20:16 +07:00
}
2026-04-26 04:39:59 +07:00
ShowScreen(initialScreen);
2026-04-25 18:20:16 +07:00
}
2026-04-26 05:02:49 +07:00
private void SetupCursor()
2026-04-25 18:20:16 +07:00
{
2026-04-26 05:02:49 +07:00
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();
2026-04-27 13:27:40 +07:00
_customCursor.style.width = 25; _customCursor.style.height = 25;
2026-04-26 05:02:49 +07:00
_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();
2026-04-27 15:48:17 +07:00
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;
}
2026-04-26 05:02:49 +07:00
trail.style.position = Position.Absolute;
trail.pickingMode = PickingMode.Ignore;
root.Add(trail);
_trailPool.Add(trail);
}
_customCursor.BringToFront();
2026-04-25 18:20:16 +07:00
}
2026-04-23 23:09:54 +07:00
2026-04-26 05:02:49 +07:00
private void Update()
2026-04-23 23:09:54 +07:00
{
2026-04-26 05:02:49 +07:00
Vector2 mousePos = Input.mousePosition;
2026-04-26 05:20:47 +07:00
bool isMainMenu = (_currentScreenName == "MainMenu");
2026-04-27 13:27:40 +07:00
bool restrictY = (isMainMenu && isMainMenuActive && !_isSettingsOpen);
2026-04-26 05:02:49 +07:00
float targetY = restrictY ? Screen.height / 2f : mousePos.y;
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - targetY);
2026-04-27 15:48:17 +07:00
bool showCursor = !isMainMenu || _isSettingsOpen;
2026-04-26 05:20:47 +07:00
DisplayStyle cursorDisplay = showCursor ? DisplayStyle.Flex : DisplayStyle.None;
2026-04-26 05:02:49 +07:00
if (_customCursor != null)
2026-04-23 23:09:54 +07:00
{
2026-04-26 05:20:47 +07:00
_customCursor.style.display = cursorDisplay;
2026-04-26 05:02:49 +07:00
_customCursor.style.left = uiPos.x - 12.5f;
_customCursor.style.top = uiPos.y - 12.5f;
2026-04-23 23:09:54 +07:00
}
2026-04-25 18:20:16 +07:00
2026-04-26 05:02:49 +07:00
if (_trailPool.Count > 0)
2026-04-26 04:39:59 +07:00
{
2026-04-26 05:02:49 +07:00
var currentTrail = _trailPool[_trailIndex];
2026-04-27 15:48:17 +07:00
currentTrail.style.display = cursorDisplay;
currentTrail.style.left = uiPos.x - 10;
currentTrail.style.top = uiPos.y - 10;
currentTrail.style.opacity = 0.6f;
2026-04-26 05:20:47 +07:00
foreach(var t in _trailPool)
{
2026-04-27 15:48:17 +07:00
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;
2026-04-26 05:20:47 +07:00
}
2026-04-26 05:02:49 +07:00
_trailIndex = (_trailIndex + 1) % _trailPool.Count;
2026-04-26 04:39:59 +07:00
}
2026-04-25 18:20:16 +07:00
2026-04-26 05:02:49 +07:00
HandleVirtualInput(uiPos);
2026-04-26 04:39:59 +07:00
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
ToggleSettings();
}
2026-04-25 18:20:16 +07:00
2026-04-26 05:02:49 +07:00
private void HandleVirtualInput(Vector2 uiPos)
2026-04-26 04:39:59 +07:00
{
2026-04-26 05:02:49 +07:00
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;
2026-04-26 04:39:59 +07:00
VisualElement bestElement = null;
float minDistance = float.MaxValue;
2026-04-26 05:02:49 +07:00
var interactables = activeDoc.rootVisualElement.Query<VisualElement>()
.Where(e => e.focusable && e.pickingMode != PickingMode.Ignore).ToList();
2026-04-26 04:39:59 +07:00
2026-04-26 05:02:49 +07:00
foreach (var element in interactables)
2026-04-26 04:39:59 +07:00
{
Rect worldBounds = element.worldBound;
2026-04-26 05:02:49 +07:00
float dist = Vector2.Distance(uiPos, worldBounds.center);
if (dist < minDistance && dist < focusRadius) {
2026-04-26 04:39:59 +07:00
minDistance = dist;
bestElement = element;
}
}
if (bestElement != _lastHoveredElement)
2026-04-23 23:09:54 +07:00
{
2026-04-26 04:39:59 +07:00
_lastHoveredElement?.RemoveFromClassList("hover");
bestElement?.AddToClassList("hover");
_lastHoveredElement = bestElement;
2026-04-23 23:09:54 +07:00
}
2026-04-26 04:39:59 +07:00
if (Input.GetMouseButtonDown(0) && _lastHoveredElement != null)
{
2026-04-26 05:02:49 +07:00
using (var clickEvent = ClickEvent.GetPooled()) {
2026-04-26 04:39:59 +07:00
clickEvent.target = _lastHoveredElement;
_lastHoveredElement.SendEvent(clickEvent);
}
}
2026-04-23 23:09:54 +07:00
}
2026-04-27 13:27:40 +07:00
// --- Editor Support Methods (Restored) ---
2026-04-26 05:02:49 +07:00
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 ---
2026-04-26 04:39:59 +07:00
public void ShowScreen(string name)
2026-04-23 23:09:54 +07:00
{
2026-04-26 05:02:49 +07:00
var screen = screens.Find(s => s.screenName == name);
if (screen == null) return;
2026-04-25 18:20:16 +07:00
2026-04-26 05:02:49 +07:00
if (!screen.isOverlay)
2026-04-25 18:20:16 +07:00
{
2026-04-27 13:27:40 +07:00
foreach(var s in screens) if(!s.isOverlay && s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
2026-04-26 05:02:49 +07:00
_navigationStack.Push(name);
_currentScreenName = name;
2026-04-25 18:20:16 +07:00
}
2026-04-26 04:39:59 +07:00
2026-04-26 05:02:49 +07:00
screen.document.rootVisualElement.style.display = DisplayStyle.Flex;
screen.isActive = true;
UnityEngine.Cursor.visible = false;
2026-04-26 04:39:59 +07:00
}
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();
2026-04-27 13:27:40 +07:00
var prevData = screens.Find(s => s.screenName == _currentScreenName);
2026-04-27 15:48:17 +07:00
if (prevData != null) prevData.document.rootVisualElement.style.display = DisplayStyle.Flex;
}
2026-04-26 04:39:59 +07:00
public void ToggleSettings()
{
2026-04-26 04:39:59 +07:00
var settings = screens.Find(s => s.screenName == "Settings");
if (settings == null) return;
2026-04-26 05:02:49 +07:00
_isSettingsOpen = settings.document.rootVisualElement.style.display == DisplayStyle.None;
settings.document.rootVisualElement.style.display = _isSettingsOpen ? DisplayStyle.Flex : DisplayStyle.None;
2026-04-27 13:27:40 +07:00
if (_isSettingsOpen) settings.document.sortingOrder = 999;
2026-04-23 23:09:54 +07:00
}
}
}