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

144 lines
4.6 KiB
C#
Raw Normal View History

2026-04-28 00:07:42 +07:00
using System;
2026-04-23 23:09:54 +07:00
using System.Collections.Generic;
2026-04-28 00:07:42 +07:00
using System.Threading.Tasks;
2026-04-23 23:09:54 +07:00
using UnityEngine;
using UnityEngine.UIElements;
2026-04-28 10:11:28 +07:00
#if UNITY_EDITOR
using UnityEditor;
#endif
2026-04-23 23:09:54 +07:00
2026-04-28 00:07:42 +07:00
namespace Hallucinate.UI
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
[RequireComponent(typeof(UIDocument))]
2026-04-23 23:09:54 +07:00
public class UIManager : MonoBehaviour
{
2026-04-25 18:20:16 +07:00
public static UIManager Instance { get; private set; }
2026-04-28 00:07:42 +07:00
private UIDocument _uiDocument;
private VisualElement _rootElement;
2026-04-23 23:09:54 +07:00
2026-04-28 00:07:42 +07:00
private readonly Dictionary<Type, BaseUIController> _controllers = new Dictionary<Type, BaseUIController>();
private readonly Stack<BaseUIController> _history = new Stack<BaseUIController>();
2026-04-25 18:20:16 +07:00
2026-04-28 10:11:28 +07:00
[Header("Game Metadata")]
[SerializeField] private Texture2D gameIcon;
2026-04-28 00:07:42 +07:00
[Header("UI Templates")]
[SerializeField] private VisualTreeAsset mainMenuTemplate;
[SerializeField] private VisualTreeAsset lobbyTemplate;
[SerializeField] private VisualTreeAsset profileTemplate;
[SerializeField] private VisualTreeAsset settingsTemplate;
[SerializeField] private VisualTreeAsset hudTemplate;
2026-04-28 00:07:42 +07:00
[Header("Debug Settings")]
[SerializeField] private bool showDebugInfo = true;
2026-04-27 15:48:17 +07:00
2026-04-28 00:07:42 +07:00
private MainMenuController _mainMenuController;
private LobbyController _lobbyController;
private ProfileController _profileController;
private SettingsController _settingsController;
private HUDController _hudController;
2026-04-25 18:20:16 +07:00
2026-04-28 00:07:42 +07:00
private void Awake()
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
if (Instance == null)
2026-04-23 23:09:54 +07:00
{
2026-04-28 00:07:42 +07:00
Instance = this;
DontDestroyOnLoad(gameObject);
2026-04-23 23:09:54 +07:00
}
2026-04-28 00:07:42 +07:00
else
2026-04-26 04:39:59 +07:00
{
2026-04-28 00:07:42 +07:00
Destroy(gameObject);
return;
2026-04-26 04:39:59 +07:00
}
2026-04-25 18:20:16 +07:00
2026-04-28 00:07:42 +07:00
_uiDocument = GetComponent<UIDocument>();
_rootElement = _uiDocument.rootVisualElement;
2026-04-28 10:11:28 +07:00
#if UNITY_EDITOR
if (gameIcon == null)
{
var icons = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Unknown);
if (icons != null && icons.Length > 0) gameIcon = icons[0];
}
#endif
2026-04-28 00:07:42 +07:00
InitializeControllers();
}
2026-04-26 04:39:59 +07:00
2026-04-28 00:07:42 +07:00
private void InitializeControllers()
{
_mainMenuController = RegisterController<MainMenuController>(mainMenuTemplate);
2026-04-28 10:11:28 +07:00
if (_mainMenuController != null && gameIcon != null)
{
_mainMenuController.SetGameIcon(gameIcon);
}
2026-04-28 00:07:42 +07:00
_lobbyController = RegisterController<LobbyController>(lobbyTemplate);
_profileController = RegisterController<ProfileController>(profileTemplate);
_settingsController = RegisterController<SettingsController>(settingsTemplate);
_hudController = RegisterController<HUDController>(hudTemplate);
// Start with Main Menu
_ = Push<MainMenuController>();
2026-04-26 04:39:59 +07:00
}
2026-04-25 18:20:16 +07:00
2026-04-28 00:07:42 +07:00
private T RegisterController<T>(VisualTreeAsset template) where T : BaseUIController, new()
2026-04-26 04:39:59 +07:00
{
2026-04-28 00:07:42 +07:00
if (template == null)
2026-04-26 04:39:59 +07:00
{
2026-04-28 00:07:42 +07:00
Debug.LogWarning($"Template for {typeof(T).Name} is missing!");
return null;
2026-04-26 04:39:59 +07:00
}
2026-04-28 00:07:42 +07:00
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);
2026-04-28 00:07:42 +07:00
var controller = new T();
controller.Initialize(instance, this);
_controllers[typeof(T)] = controller;
return controller;
2026-04-23 23:09:54 +07:00
}
2026-04-28 00:07:42 +07:00
private void Update()
2026-04-26 05:02:49 +07:00
{
2026-04-28 00:07:42 +07:00
_mainMenuController?.Update();
_hudController?.Update();
2026-04-26 05:02:49 +07:00
}
2026-04-28 00:07:42 +07:00
public async Task Push<T>() where T : BaseUIController
2026-04-26 05:02:49 +07:00
{
2026-04-28 00:07:42 +07:00
if (!_controllers.TryGetValue(typeof(T), out var newScreen))
2026-04-26 05:02:49 +07:00
{
2026-04-28 00:07:42 +07:00
Debug.LogError($"Controller of type {typeof(T)} not registered!");
return;
2026-04-26 05:02:49 +07:00
}
2026-04-25 18:20:16 +07:00
2026-04-28 00:07:42 +07:00
if (_history.Count > 0)
2026-04-25 18:20:16 +07:00
{
2026-04-28 00:07:42 +07:00
var currentScreen = _history.Peek();
await currentScreen.PlayTransitionOut();
2026-04-25 18:20:16 +07:00
}
2026-04-26 04:39:59 +07:00
2026-04-28 00:07:42 +07:00
_history.Push(newScreen);
await newScreen.PlayTransitionIn();
2026-04-26 04:39:59 +07:00
}
2026-04-28 00:07:42 +07:00
public async Task Pop()
2026-04-26 04:39:59 +07:00
{
2026-04-28 00:07:42 +07:00
if (_history.Count <= 1) return;
2026-04-28 00:07:42 +07:00
var currentScreen = _history.Pop();
await currentScreen.PlayTransitionOut();
var previousScreen = _history.Peek();
await previousScreen.PlayTransitionIn();
2026-04-23 23:09:54 +07:00
}
}
}