Update
This commit is contained in:
@@ -2,6 +2,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Linq;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
@@ -12,121 +13,55 @@ namespace UI
|
||||
[System.Serializable]
|
||||
public class ScreenData
|
||||
{
|
||||
public string screenName; // Phải khớp với chuỗi gọi trong ShowScreen
|
||||
public string screenName;
|
||||
public UIDocument document;
|
||||
public bool isActive;
|
||||
public bool isOverlay;
|
||||
public Texture2D customCursor;
|
||||
public bool isActive; // For Editor and Initial state
|
||||
}
|
||||
|
||||
public List<ScreenData> screens = new List<ScreenData>();
|
||||
|
||||
[Header("Default Settings")]
|
||||
public Texture2D defaultCursor;
|
||||
[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);
|
||||
else { Destroy(gameObject); return; }
|
||||
|
||||
// Khởi tạo trạng thái ban đầu: Ẩn tất cả trừ màn hình mặc định
|
||||
// Initialize all screens based on isActive or hidden
|
||||
foreach (var screen in screens)
|
||||
{
|
||||
if (screen.document == null) continue;
|
||||
|
||||
if (screen.screenName == "Settings") screen.document.sortingOrder = 999;
|
||||
|
||||
screen.isActive = (screen.screenName == initialScreen);
|
||||
screen.document.rootVisualElement.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
_currentScreenName = initialScreen;
|
||||
|
||||
ShowScreen(initialScreen);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Escape)) ToggleSettings();
|
||||
HandleGlobalInputs();
|
||||
HandleCursorlessFocus();
|
||||
}
|
||||
|
||||
public void ShowOnly(string name) => ShowScreen(name);
|
||||
|
||||
public void ShowScreen(string name)
|
||||
{
|
||||
if (_currentScreenName == name) return;
|
||||
|
||||
// Kiểm tra xem màn hình mục tiêu có tồn tại không trước khi tắt cái cũ
|
||||
var nextData = screens.Find(s => s.screenName == name);
|
||||
if (nextData == null)
|
||||
{
|
||||
Debug.LogError($"[UIManager] Screen '{name}' not found in the list! Check your Inspector names.");
|
||||
return;
|
||||
}
|
||||
|
||||
StartCoroutine(TransitionRoutine(nextData));
|
||||
}
|
||||
|
||||
private IEnumerator TransitionRoutine(ScreenData nextData)
|
||||
{
|
||||
// 1. Fade Out các màn hình chính hiện tại (trừ Overlay)
|
||||
foreach (var s in screens)
|
||||
{
|
||||
if (s.isActive && !s.isOverlay)
|
||||
{
|
||||
var root = s.document.rootVisualElement.Q<VisualElement>();
|
||||
if (root != null) root.AddToClassList("hidden");
|
||||
s.isActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(0.3f);
|
||||
SyncScreens(); // Ẩn hẳn display
|
||||
|
||||
// 2. Hiện màn hình mới
|
||||
nextData.isActive = true;
|
||||
_currentScreenName = nextData.screenName;
|
||||
|
||||
var nextRoot = nextData.document.rootVisualElement.Q<VisualElement>();
|
||||
if (nextRoot != null)
|
||||
{
|
||||
nextData.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
nextRoot.AddToClassList("hidden");
|
||||
yield return null; // Chờ 1 frame để UI Toolkit cập nhật
|
||||
nextRoot.RemoveFromClassList("hidden");
|
||||
}
|
||||
|
||||
ApplyCursor(nextData.customCursor != null ? nextData.customCursor : defaultCursor);
|
||||
}
|
||||
|
||||
public void ToggleSettings()
|
||||
{
|
||||
var settingsData = screens.Find(s => s.screenName == "Settings");
|
||||
if (settingsData == null) return;
|
||||
|
||||
settingsData.isActive = !settingsData.isActive;
|
||||
settingsData.document.rootVisualElement.style.display = settingsData.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
|
||||
if (settingsData.isActive)
|
||||
{
|
||||
UnityEngine.Cursor.visible = true;
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.None;
|
||||
ApplyCursor(settingsData.customCursor != null ? settingsData.customCursor : defaultCursor);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Nếu tắt Settings, quay về trạng thái của màn hình hiện tại
|
||||
var current = screens.Find(s => s.screenName == _currentScreenName);
|
||||
if (current != null) ApplyCursor(current.customCursor != null ? current.customCursor : defaultCursor);
|
||||
|
||||
// Tùy vào game là FPS hay Menu mà ẩn chuột
|
||||
if (_currentScreenName == "HUD")
|
||||
{
|
||||
UnityEngine.Cursor.visible = false;
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
}
|
||||
}
|
||||
// --- Editor Support Methods ---
|
||||
|
||||
public void SyncScreens()
|
||||
{
|
||||
@@ -134,14 +69,150 @@ namespace UI
|
||||
{
|
||||
if (screen.document != null && screen.document.rootVisualElement != null)
|
||||
{
|
||||
screen.document.rootVisualElement.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
screen.document.rootVisualElement.style.display =
|
||||
screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
screen.document.rootVisualElement.style.opacity = globalOpacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyCursor(Texture2D texture)
|
||||
public void ShowOnly(string name)
|
||||
{
|
||||
UnityEngine.Cursor.SetCursor(texture, Vector2.zero, CursorMode.Auto);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user