Update
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using OnlyScove.Scripts;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
@@ -12,9 +14,16 @@ namespace UI
|
||||
private VisualElement _healthFill;
|
||||
private VisualElement _staminaFill;
|
||||
private Label _healthText;
|
||||
private Label _noiseLabel;
|
||||
private Label _interactionLabel;
|
||||
private VisualElement _interactionPrompt;
|
||||
private Label _interactionLabel;
|
||||
|
||||
private VisualElement _statsArea;
|
||||
private VisualElement _inventoryArea;
|
||||
private VisualElement _infoArea;
|
||||
|
||||
private float _lastInputTime;
|
||||
private bool _isHUDVisible = true;
|
||||
public float autoHideDelay = 5f;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
@@ -26,18 +35,106 @@ namespace UI
|
||||
_healthFill = root.Q<VisualElement>("health-fill");
|
||||
_staminaFill = root.Q<VisualElement>("stamina-fill");
|
||||
_healthText = root.Q<Label>("health-text");
|
||||
_noiseLabel = root.Q<Label>("noise-label");
|
||||
_interactionLabel = root.Q<Label>("interaction-text");
|
||||
_interactionPrompt = root.Q<VisualElement>("interaction-prompt");
|
||||
_interactionLabel = root.Q<Label>("interaction-text");
|
||||
|
||||
_statsArea = root.Q<VisualElement>("hud-stats");
|
||||
_inventoryArea = root.Q<VisualElement>("hud-inventory");
|
||||
_infoArea = root.Q<VisualElement>("hud-info");
|
||||
|
||||
_lastInputTime = Time.time;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Kết nối với Local Player
|
||||
if (PlayerStateMachine.Local != null)
|
||||
{
|
||||
SubscribeToPlayer(PlayerStateMachine.Local);
|
||||
}
|
||||
|
||||
HandleAutoHide();
|
||||
HandleInventoryInput();
|
||||
}
|
||||
|
||||
private void HandleAutoHide()
|
||||
{
|
||||
bool inputDetected = false;
|
||||
|
||||
// Check for mouse movement
|
||||
if (Mouse.current != null && Mouse.current.delta.ReadValue().sqrMagnitude > 0.01f)
|
||||
inputDetected = true;
|
||||
|
||||
// Check for any key press (including mouse buttons)
|
||||
if (!inputDetected && Keyboard.current != null && Keyboard.current.anyKey.isPressed)
|
||||
inputDetected = true;
|
||||
|
||||
if (!inputDetected && Mouse.current != null && (Mouse.current.leftButton.isPressed || Mouse.current.rightButton.isPressed))
|
||||
inputDetected = true;
|
||||
|
||||
if (inputDetected)
|
||||
{
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
else if (Time.time - _lastInputTime > autoHideDelay)
|
||||
{
|
||||
SetHUDVisibility(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetHUDVisibility(bool visible)
|
||||
{
|
||||
if (_isHUDVisible == visible) return;
|
||||
_isHUDVisible = visible;
|
||||
|
||||
float targetOpacity = visible ? 1f : 0.2f;
|
||||
|
||||
_statsArea.style.opacity = targetOpacity;
|
||||
_inventoryArea.style.opacity = targetOpacity;
|
||||
_infoArea.style.opacity = targetOpacity;
|
||||
|
||||
_statsArea.style.transitionProperty = new List<StylePropertyName> { "opacity" };
|
||||
_statsArea.style.transitionDuration = new List<TimeValue> { new TimeValue(0.5f, TimeUnit.Second) };
|
||||
_inventoryArea.style.transitionProperty = new List<StylePropertyName> { "opacity" };
|
||||
_inventoryArea.style.transitionDuration = new List<TimeValue> { new TimeValue(0.5f, TimeUnit.Second) };
|
||||
_infoArea.style.transitionProperty = new List<StylePropertyName> { "opacity" };
|
||||
_infoArea.style.transitionDuration = new List<TimeValue> { new TimeValue(0.5f, TimeUnit.Second) };
|
||||
}
|
||||
|
||||
private void HandleInventoryInput()
|
||||
{
|
||||
if (Keyboard.current == null) return;
|
||||
|
||||
if (Keyboard.current.digit1Key.wasPressedThisFrame) SelectSlot(1);
|
||||
if (Keyboard.current.digit2Key.wasPressedThisFrame) SelectSlot(2);
|
||||
if (Keyboard.current.digit3Key.wasPressedThisFrame) SelectSlot(3);
|
||||
}
|
||||
|
||||
private void SelectSlot(int index)
|
||||
{
|
||||
// Mock logic: Highlight the selected slot
|
||||
var root = hudDocument.rootVisualElement;
|
||||
for (int i = 1; i <= 3; i++)
|
||||
{
|
||||
var slot = root.Q<VisualElement>($"slot-{i}");
|
||||
if (slot != null)
|
||||
{
|
||||
float width = (i == index) ? 2f : 1f;
|
||||
Color color = (i == index) ? Color.white : new Color(0.5f, 0.5f, 0.5f);
|
||||
|
||||
slot.style.borderTopWidth = width;
|
||||
slot.style.borderBottomWidth = width;
|
||||
slot.style.borderLeftWidth = width;
|
||||
slot.style.borderRightWidth = width;
|
||||
|
||||
slot.style.borderTopColor = color;
|
||||
slot.style.borderBottomColor = color;
|
||||
slot.style.borderLeftColor = color;
|
||||
slot.style.borderRightColor = color;
|
||||
}
|
||||
}
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
|
||||
private PlayerStateMachine _currentPlayer;
|
||||
@@ -46,43 +143,38 @@ namespace UI
|
||||
{
|
||||
if (_currentPlayer == player) return;
|
||||
|
||||
// Hủy đăng ký player cũ nếu có
|
||||
if (_currentPlayer != null)
|
||||
{
|
||||
_currentPlayer.OnHealthChanged -= UpdateHealth;
|
||||
_currentPlayer.OnStaminaChanged -= UpdateStamina;
|
||||
_currentPlayer.OnNoiseLevelChanged -= UpdateNoise;
|
||||
_currentPlayer.OnInteractableTargetChanged -= UpdateInteraction;
|
||||
}
|
||||
|
||||
_currentPlayer = player;
|
||||
|
||||
// Đăng ký player mới
|
||||
_currentPlayer.OnHealthChanged += UpdateHealth;
|
||||
_currentPlayer.OnStaminaChanged += UpdateStamina;
|
||||
_currentPlayer.OnNoiseLevelChanged += UpdateNoise;
|
||||
_currentPlayer.OnInteractableTargetChanged += UpdateInteraction;
|
||||
|
||||
// Cập nhật giá trị ban đầu
|
||||
UpdateHealth(_currentPlayer.Health);
|
||||
UpdateStamina(_currentPlayer.Stamina);
|
||||
UpdateNoise(_currentPlayer.NoiseLevel);
|
||||
}
|
||||
|
||||
private void UpdateHealth(float health)
|
||||
{
|
||||
if (_healthFill != null) _healthFill.style.width = Length.Percent(health);
|
||||
if (_healthText != null) _healthText.text = $"HEALTH: {Mathf.RoundToInt(health)}/100";
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
|
||||
private void UpdateStamina(float stamina)
|
||||
{
|
||||
if (_staminaFill != null) _staminaFill.style.width = Length.Percent(stamina);
|
||||
}
|
||||
|
||||
private void UpdateNoise(float noise)
|
||||
{
|
||||
if (_noiseLabel != null) _noiseLabel.text = $"NOISE: {Mathf.RoundToInt(noise)}%";
|
||||
if (stamina < 99f) // Only wake up HUD if stamina is being used
|
||||
{
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateInteraction(IInteractable interactable)
|
||||
@@ -93,6 +185,8 @@ namespace UI
|
||||
{
|
||||
_interactionPrompt.style.display = DisplayStyle.Flex;
|
||||
if (_interactionLabel != null) _interactionLabel.text = interactable.InteractionPrompt;
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -30,14 +30,12 @@ namespace UI
|
||||
|
||||
private void OnLeaveClicked()
|
||||
{
|
||||
Debug.Log("User clicked LEAVE room!");
|
||||
UIManager.Instance.ShowScreen("MainMenu");
|
||||
UIManager.Instance.GoBack();
|
||||
}
|
||||
|
||||
private void OnStartClicked()
|
||||
{
|
||||
Debug.Log("Host clicked START GAME!");
|
||||
UIManager.Instance.ShowScreen("HUD"); // Chuyển vào HUD khi game bắt đầu
|
||||
UIManager.Instance.ShowScreen("Lounge");
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
|
||||
36
Assets/Scripts/UI/LoungeController.cs
Normal file
36
Assets/Scripts/UI/LoungeController.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LoungeController : MonoBehaviour
|
||||
{
|
||||
private Toggle _readyHost;
|
||||
private Toggle _readyGuest;
|
||||
private Button _btnStart;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
var root = GetComponent<UIDocument>().rootVisualElement;
|
||||
|
||||
_readyHost = root.Q<Toggle>("ready-host");
|
||||
_readyGuest = root.Q<Toggle>("ready-guest");
|
||||
_btnStart = root.Q<Button>("btn-start");
|
||||
|
||||
_readyHost?.RegisterValueChangedCallback(evt => UpdateStartButton());
|
||||
_readyGuest?.RegisterValueChangedCallback(evt => UpdateStartButton());
|
||||
|
||||
_btnStart?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ShowScreen("HUD"));
|
||||
root.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.GoBack());
|
||||
|
||||
UpdateStartButton();
|
||||
}
|
||||
|
||||
private void UpdateStartButton()
|
||||
{
|
||||
if (_btnStart == null) return;
|
||||
bool bothReady = (_readyHost != null && _readyHost.value) && (_readyGuest != null && _readyGuest.value);
|
||||
_btnStart.SetEnabled(bothReady);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/LoungeController.cs.meta
Normal file
2
Assets/Scripts/UI/LoungeController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac201603ede0899488995be3d88ea0dc
|
||||
@@ -1,23 +1,79 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class MainMenuController : MonoBehaviour
|
||||
{
|
||||
private UIDocument _doc;
|
||||
private VisualElement _logoContainer;
|
||||
private VisualElement _logo;
|
||||
private VisualElement _ribbon;
|
||||
private bool _isActive = false;
|
||||
|
||||
[Header("Animation Settings")]
|
||||
public float pulseSpeed = 2f;
|
||||
public float pulseAmount = 0.1f;
|
||||
public float transitionDuration = 0.5f;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_doc = GetComponent<UIDocument>();
|
||||
var root = _doc.rootVisualElement;
|
||||
var root = GetComponent<UIDocument>().rootVisualElement;
|
||||
|
||||
// Nối dây các nút bấm
|
||||
_logoContainer = root.Q<VisualElement>("beat-logo-container");
|
||||
_logo = root.Q<VisualElement>("beat-logo");
|
||||
_ribbon = root.Q<VisualElement>("menu-ribbon");
|
||||
|
||||
// Register logo click
|
||||
_logoContainer.RegisterCallback<ClickEvent>(OnLogoClicked);
|
||||
|
||||
// Register button events
|
||||
root.Q<Button>("btn-create")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("Lobby"));
|
||||
root.Q<Button>("btn-join")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("Lobby"));
|
||||
root.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ToggleSettings());
|
||||
root.Q<Button>("btn-profile")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("Profile"));
|
||||
root.Q<Button>("btn-exit")?.RegisterCallback<ClickEvent>(ev => Application.Quit());
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_isActive)
|
||||
{
|
||||
// Pulse Animation
|
||||
float scale = 1f + Mathf.Sin(Time.time * pulseSpeed) * pulseAmount;
|
||||
_logo.style.scale = new Scale(new Vector3(scale, scale, 1f));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLogoClicked(ClickEvent evt)
|
||||
{
|
||||
if (_isActive) return;
|
||||
StartCoroutine(TransitionToActive());
|
||||
}
|
||||
|
||||
private IEnumerator TransitionToActive()
|
||||
{
|
||||
_isActive = true;
|
||||
|
||||
// 1. Shrink and move logo
|
||||
_logoContainer.style.transitionProperty = new List<StylePropertyName> { "scale", "translate" };
|
||||
_logoContainer.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) };
|
||||
|
||||
_logoContainer.style.scale = new Scale(new Vector3(0.4f, 0.4f, 1f));
|
||||
// Translate is tricky in UI Toolkit relative to center, but we can use absolute positioning or a placeholder
|
||||
// For now, let's just fade in the ribbon and hide the central logo
|
||||
|
||||
yield return new WaitForSeconds(transitionDuration * 0.5f);
|
||||
|
||||
_ribbon.style.display = DisplayStyle.Flex;
|
||||
_ribbon.style.opacity = 0;
|
||||
_ribbon.style.transitionProperty = new List<StylePropertyName> { "opacity" };
|
||||
_ribbon.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) };
|
||||
|
||||
yield return null;
|
||||
_ribbon.style.opacity = 1;
|
||||
_logoContainer.style.display = DisplayStyle.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace UI
|
||||
private void OnEnable()
|
||||
{
|
||||
var root = GetComponent<UIDocument>().rootVisualElement;
|
||||
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("MainMenu"));
|
||||
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.GoBack());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,10 @@ namespace UI
|
||||
_tabGraphics.clicked += () => SwitchTab(_contentGraphics, _tabGraphics);
|
||||
_tabAudio.clicked += () => SwitchTab(_contentAudio, _tabAudio);
|
||||
|
||||
root.Q<Button>("btn-back").clicked += () => UIManager.Instance.ToggleSettings();
|
||||
root.Q<Button>("btn-close").clicked += () => UIManager.Instance.ToggleSettings();
|
||||
|
||||
// If we want a hard back button to MainMenu:
|
||||
// root.Q<Button>("btn-back").clicked += () => UIManager.Instance.GoBack();
|
||||
|
||||
// Camera Binding (FOV)
|
||||
var fovSlider = root.Q<Slider>("setting-fov");
|
||||
|
||||
@@ -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