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

219 lines
7.1 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;
public Texture2D customCursor;
2026-04-26 04:39:59 +07:00
public bool isActive; // For Editor and Initial state
2026-04-23 23:09:54 +07:00
}
public List<ScreenData> screens = new List<ScreenData>();
2026-04-26 04:39:59 +07:00
[Header("Settings")]
public string initialScreen = "MainMenu";
2026-04-26 04:39:59 +07:00
public float focusRadius = 300f;
[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-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-26 04:39:59 +07:00
// Initialize all screens based on isActive or hidden
2026-04-25 18:20:16 +07:00
foreach (var screen in screens)
{
2026-04-26 04:39:59 +07:00
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;
}
}
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
}
private void Update()
2026-04-25 18:20:16 +07:00
{
2026-04-26 04:39:59 +07:00
HandleGlobalInputs();
HandleCursorlessFocus();
2026-04-25 18:20:16 +07:00
}
2026-04-23 23:09:54 +07:00
2026-04-26 04:39:59 +07:00
// --- Editor Support Methods ---
2026-04-23 23:09:54 +07:00
2026-04-26 04:39:59 +07:00
public void SyncScreens()
2026-04-23 23:09:54 +07:00
{
2026-04-26 04:39:59 +07:00
foreach (var screen in screens)
2026-04-23 23:09:54 +07:00
{
2026-04-26 04:39:59 +07:00
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;
}
2026-04-23 23:09:54 +07:00
}
2026-04-26 04:39:59 +07:00
}
2026-04-25 18:20:16 +07:00
2026-04-26 04:39:59 +07:00
public void ShowOnly(string name)
{
foreach (var screen in screens)
{
screen.isActive = (screen.screenName == name);
}
SyncScreens();
2026-04-25 18:20:16 +07:00
}
2026-04-26 04:39:59 +07:00
// --- Runtime Logic ---
private void HandleGlobalInputs()
2026-04-25 18:20:16 +07:00
{
2026-04-26 04:39:59 +07:00
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
2026-04-23 23:09:54 +07:00
{
2026-04-26 04:39:59 +07:00
ToggleSettings();
}
if (Input.GetKeyDown(KeyCode.Escape))
{
if (_navigationStack.Count > 1)
2026-04-25 18:20:16 +07:00
{
2026-04-26 04:39:59 +07:00
GoBack();
2026-04-25 18:20:16 +07:00
}
2026-04-23 23:09:54 +07:00
}
2026-04-26 04:39:59 +07:00
}
2026-04-25 18:20:16 +07:00
2026-04-26 04:39:59 +07:00
private void HandleCursorlessFocus()
{
if (string.IsNullOrEmpty(_currentScreenName)) return;
2026-04-23 23:09:54 +07:00
2026-04-26 04:39:59 +07:00
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)
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)
{
using (var clickEvent = ClickEvent.GetPooled())
{
clickEvent.target = _lastHoveredElement;
_lastHoveredElement.SendEvent(clickEvent);
}
}
2026-04-23 23:09:54 +07:00
}
2026-04-26 04:39:59 +07:00
public void ShowScreen(string name)
2026-04-23 23:09:54 +07:00
{
2026-04-26 04:39:59 +07:00
var nextData = screens.Find(s => s.screenName == name);
if (nextData == null) return;
2026-04-25 18:20:16 +07:00
2026-04-26 04:39:59 +07:00
// Hide all first for a clean state at runtime
foreach (var s in screens)
2026-04-25 18:20:16 +07:00
{
2026-04-26 04:39:59 +07:00
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
s.isActive = false;
2026-04-25 18:20:16 +07:00
}
2026-04-26 04:39:59 +07:00
_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)
2026-04-25 18:20:16 +07:00
{
2026-04-26 04:39:59 +07:00
prevData.document.rootVisualElement.style.display = DisplayStyle.Flex;
ApplyCursorSettings(prevData);
}
}
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;
bool isShowing = settings.document.rootVisualElement.style.display == DisplayStyle.Flex;
settings.document.rootVisualElement.style.display = isShowing ? DisplayStyle.None : DisplayStyle.Flex;
if (!isShowing)
{
2026-04-26 04:39:59 +07:00
settings.document.sortingOrder = 999;
2026-04-25 18:20:16 +07:00
}
2026-04-23 23:09:54 +07:00
}
2026-04-26 04:39:59 +07:00
private void ApplyCursorSettings(ScreenData data)
2026-04-23 23:09:54 +07:00
{
2026-04-26 04:39:59 +07:00
if (data.screenName == "HUD")
{
UnityEngine.Cursor.visible = false;
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
}
else
{
UnityEngine.Cursor.visible = false;
UnityEngine.Cursor.lockState = CursorLockMode.None;
}
2026-04-23 23:09:54 +07:00
}
}
}