Update
This commit is contained in:
55
Assets/Scripts/UI/BaseUIController.cs
Normal file
55
Assets/Scripts/UI/BaseUIController.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using PrimeTween;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
public abstract class BaseUIController
|
||||
{
|
||||
protected VisualElement root;
|
||||
protected UIManager uiManager;
|
||||
|
||||
public virtual void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
root = uxmlRoot;
|
||||
uiManager = manager;
|
||||
|
||||
// Default to hidden
|
||||
Hide();
|
||||
}
|
||||
|
||||
public virtual void Show()
|
||||
{
|
||||
if (root != null)
|
||||
root.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
public virtual void Hide()
|
||||
{
|
||||
if (root != null)
|
||||
root.style.display = DisplayStyle.None;
|
||||
}
|
||||
|
||||
public virtual async Task PlayTransitionIn()
|
||||
{
|
||||
if (root == null) return;
|
||||
|
||||
Show();
|
||||
// Fly-in from right using Custom tween for style.translate
|
||||
root.style.translate = new StyleTranslate(new Translate(Length.Percent(100), 0));
|
||||
await Tween.Custom(100f, 0f, duration: 0.5f, ease: Ease.OutBack,
|
||||
onValueChange: val => root.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
|
||||
}
|
||||
|
||||
public virtual async Task PlayTransitionOut()
|
||||
{
|
||||
if (root == null) return;
|
||||
|
||||
// Fly-out to left
|
||||
await Tween.Custom(0f, -100f, duration: 0.5f, ease: Ease.InBack,
|
||||
onValueChange: val => root.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
|
||||
Hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/BaseUIController.cs.meta
Normal file
2
Assets/Scripts/UI/BaseUIController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 872f8bfaee91588488a3393579135de9
|
||||
@@ -1,197 +1,80 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using OnlyScove.Scripts;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.InputSystem;
|
||||
using PrimeTween;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UI
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
public class HUDController : MonoBehaviour
|
||||
public class HUDController : BaseUIController
|
||||
{
|
||||
[Header("UI Document")]
|
||||
public UIDocument hudDocument;
|
||||
private VisualElement _topLeft;
|
||||
private VisualElement _bottomLeft;
|
||||
|
||||
private ProgressBar _healthBar;
|
||||
private ProgressBar _staminaBar;
|
||||
|
||||
private float _lastActionTime;
|
||||
private const float FADE_TIMEOUT = 5.0f;
|
||||
private bool _isFaded = false;
|
||||
|
||||
private VisualElement _healthFill;
|
||||
private VisualElement _staminaFill;
|
||||
private Label _healthText;
|
||||
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()
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
if (hudDocument == null)
|
||||
hudDocument = GetComponent<UIDocument>();
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
|
||||
var root = hudDocument.rootVisualElement;
|
||||
_topLeft = root.Q<VisualElement>("TopLeft");
|
||||
_bottomLeft = root.Q<VisualElement>("BottomLeft");
|
||||
_healthBar = root.Q<ProgressBar>("HealthBar");
|
||||
_staminaBar = root.Q<ProgressBar>("StaminaBar");
|
||||
|
||||
_healthFill = root.Q<VisualElement>("health-fill");
|
||||
_staminaFill = root.Q<VisualElement>("stamina-fill");
|
||||
_healthText = root.Q<Label>("health-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;
|
||||
_lastActionTime = Time.time;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
public void UpdateHUD(float health, float stamina)
|
||||
{
|
||||
if (PlayerStateMachine.Local != null)
|
||||
{
|
||||
SubscribeToPlayer(PlayerStateMachine.Local);
|
||||
}
|
||||
|
||||
HandleAutoHide();
|
||||
HandleInventoryInput();
|
||||
_healthBar.value = health;
|
||||
_staminaBar.value = stamina;
|
||||
WakeUpHUD();
|
||||
}
|
||||
|
||||
private void HandleAutoHide()
|
||||
public void UpdateStats(int ping, int fps)
|
||||
{
|
||||
bool inputDetected = false;
|
||||
root.Q<Label>("PingLabel").text = $"PING: {ping}ms";
|
||||
root.Q<Label>("FPSLabel").text = $"FPS: {fps}";
|
||||
}
|
||||
|
||||
// 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)
|
||||
public void WakeUpHUD()
|
||||
{
|
||||
_lastActionTime = Time.time;
|
||||
if (_isFaded)
|
||||
{
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
else if (Time.time - _lastInputTime > autoHideDelay)
|
||||
{
|
||||
SetHUDVisibility(false);
|
||||
_isFaded = false;
|
||||
Tween.Custom(_topLeft.style.opacity.value, 1f, duration: 0.3f, onValueChange: val => _topLeft.style.opacity = val);
|
||||
Tween.Custom(_bottomLeft.style.opacity.value, 1f, duration: 0.3f, onValueChange: val => _bottomLeft.style.opacity = val);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetHUDVisibility(bool visible)
|
||||
public void Update()
|
||||
{
|
||||
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++)
|
||||
if (!_isFaded && Time.time - _lastActionTime > FADE_TIMEOUT)
|
||||
{
|
||||
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;
|
||||
|
||||
private void SubscribeToPlayer(PlayerStateMachine player)
|
||||
{
|
||||
if (_currentPlayer == player) return;
|
||||
|
||||
if (_currentPlayer != null)
|
||||
{
|
||||
_currentPlayer.OnHealthChanged -= UpdateHealth;
|
||||
_currentPlayer.OnStaminaChanged -= UpdateStamina;
|
||||
_currentPlayer.OnInteractableTargetChanged -= UpdateInteraction;
|
||||
}
|
||||
|
||||
_currentPlayer = player;
|
||||
|
||||
_currentPlayer.OnHealthChanged += UpdateHealth;
|
||||
_currentPlayer.OnStaminaChanged += UpdateStamina;
|
||||
_currentPlayer.OnInteractableTargetChanged += UpdateInteraction;
|
||||
|
||||
UpdateHealth(_currentPlayer.Health);
|
||||
UpdateStamina(_currentPlayer.Stamina);
|
||||
}
|
||||
|
||||
private void UpdateHealth(float health)
|
||||
{
|
||||
if (_healthFill != null) _healthFill.style.width = Length.Percent(health);
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
|
||||
private void UpdateStamina(float stamina)
|
||||
{
|
||||
if (_staminaFill != null) _staminaFill.style.width = Length.Percent(stamina);
|
||||
if (stamina < 99f) // Only wake up HUD if stamina is being used
|
||||
{
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
_isFaded = true;
|
||||
Tween.Custom(_topLeft.style.opacity.value, 0.2f, duration: 1.0f, onValueChange: val => _topLeft.style.opacity = val);
|
||||
Tween.Custom(_bottomLeft.style.opacity.value, 0.2f, duration: 1.0f, onValueChange: val => _bottomLeft.style.opacity = val);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateInteraction(IInteractable interactable)
|
||||
public override Task PlayTransitionIn()
|
||||
{
|
||||
if (_interactionPrompt == null) return;
|
||||
Show();
|
||||
_topLeft.style.opacity = 1;
|
||||
_bottomLeft.style.opacity = 1;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (interactable != null)
|
||||
{
|
||||
_interactionPrompt.style.display = DisplayStyle.Flex;
|
||||
if (_interactionLabel != null) _interactionLabel.text = interactable.InteractionPrompt;
|
||||
_lastInputTime = Time.time;
|
||||
SetHUDVisibility(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
_interactionPrompt.style.display = DisplayStyle.None;
|
||||
}
|
||||
public override Task PlayTransitionOut()
|
||||
{
|
||||
Hide();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,75 +1,73 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UI
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
public class LobbyController : MonoBehaviour
|
||||
public class LobbyController : BaseUIController
|
||||
{
|
||||
private VisualElement _joinView;
|
||||
private VisualElement _createView;
|
||||
|
||||
private float _lastInteractionTime;
|
||||
private bool _isCreateMode = false;
|
||||
private const float AutoReturnDelay = 5f;
|
||||
private VisualElement _joinContainer;
|
||||
private VisualElement _createContainer;
|
||||
private VisualElement _loungeContainer;
|
||||
|
||||
private void OnEnable()
|
||||
private Button _backBtn;
|
||||
private Button _createFinalBtn;
|
||||
private Button _startGameBtn;
|
||||
|
||||
private Toggle _hostReady;
|
||||
private Toggle _guestReady;
|
||||
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
var root = GetComponent<UIDocument>().rootVisualElement;
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
|
||||
_joinView = root.Q<VisualElement>("join-view");
|
||||
_createView = root.Q<VisualElement>("create-view");
|
||||
_joinContainer = root.Q<VisualElement>("JoinContainer");
|
||||
_createContainer = root.Q<VisualElement>("CreateContainer");
|
||||
_loungeContainer = root.Q<VisualElement>("LoungeContainer");
|
||||
|
||||
// Back button
|
||||
root.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.GoBack());
|
||||
root.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ToggleSettings());
|
||||
_backBtn = root.Q<Button>("BackToMenuBtn");
|
||||
_createFinalBtn = root.Q<Button>("CreateRoomFinalBtn");
|
||||
_startGameBtn = root.Q<Button>("StartGameBtn");
|
||||
|
||||
_hostReady = root.Q<Toggle>("HostReady");
|
||||
_guestReady = root.Q<Toggle>("GuestReady");
|
||||
|
||||
_backBtn.clicked += () => uiManager.Pop();
|
||||
_createFinalBtn.clicked += () => ShowLounge();
|
||||
|
||||
// Create confirm -> Lounge
|
||||
root.Q<Button>("btn-create-confirm")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ShowScreen("Lounge"));
|
||||
_hostReady.RegisterValueChangedCallback(evt => UpdateStartButton());
|
||||
_guestReady.RegisterValueChangedCallback(evt => UpdateStartButton());
|
||||
|
||||
// Register Interaction Resetters
|
||||
var textFields = root.Query<TextField>().ToList();
|
||||
foreach (var field in textFields)
|
||||
field.RegisterValueChangedCallback(evt => ResetInteractionTimer());
|
||||
|
||||
var toggles = root.Query<Toggle>().ToList();
|
||||
foreach (var t in toggles)
|
||||
t.RegisterValueChangedCallback(evt => ResetInteractionTimer());
|
||||
|
||||
// Password Toggle Logic
|
||||
var passToggle = root.Q<Toggle>("toggle-password");
|
||||
var passField = root.Q<TextField>("field-password");
|
||||
passToggle?.RegisterValueChangedCallback(evt => {
|
||||
if(passField != null) passField.style.display = evt.newValue ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
});
|
||||
|
||||
ResetInteractionTimer();
|
||||
UpdateStartButton();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
public void ShowJoin()
|
||||
{
|
||||
if (_isCreateMode)
|
||||
_joinContainer.style.display = DisplayStyle.Flex;
|
||||
_createContainer.style.display = DisplayStyle.None;
|
||||
_loungeContainer.style.display = DisplayStyle.None;
|
||||
}
|
||||
|
||||
public void ShowCreate()
|
||||
{
|
||||
_joinContainer.style.display = DisplayStyle.None;
|
||||
_createContainer.style.display = DisplayStyle.Flex;
|
||||
_loungeContainer.style.display = DisplayStyle.None;
|
||||
}
|
||||
|
||||
public void ShowLounge()
|
||||
{
|
||||
_joinContainer.style.display = DisplayStyle.None;
|
||||
_createContainer.style.display = DisplayStyle.None;
|
||||
_loungeContainer.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
private void UpdateStartButton()
|
||||
{
|
||||
if (_startGameBtn != null)
|
||||
{
|
||||
if (Time.time - _lastInteractionTime > AutoReturnDelay)
|
||||
{
|
||||
SetMode(false); // Auto return to Stage 1
|
||||
}
|
||||
_startGameBtn.SetEnabled(_hostReady.value && _guestReady.value);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetMode(bool isCreate)
|
||||
{
|
||||
_isCreateMode = isCreate;
|
||||
if (_joinView == null) return;
|
||||
|
||||
_joinView.style.display = isCreate ? DisplayStyle.None : DisplayStyle.Flex;
|
||||
_createView.style.display = isCreate ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
|
||||
if (isCreate) ResetInteractionTimer();
|
||||
}
|
||||
|
||||
private void ResetInteractionTimer()
|
||||
{
|
||||
_lastInteractionTime = Time.time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LocalizationManager : MonoBehaviour
|
||||
{
|
||||
public static LocalizationManager Instance { get; private set; }
|
||||
|
||||
private Dictionary<string, string> _localizedText;
|
||||
private string _currentLanguage = "en";
|
||||
|
||||
public event Action OnLanguageChanged;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
if (transform.parent == null)
|
||||
DontDestroyOnLoad(gameObject);
|
||||
LoadLanguage(_currentLanguage);
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void LoadLanguage(string langCode)
|
||||
{
|
||||
TextAsset targetFile = Resources.Load<TextAsset>($"Localization/{langCode}");
|
||||
if (targetFile != null)
|
||||
{
|
||||
// Simple JSON parsing (For production, consider using a proper JSON library like Newtonsoft)
|
||||
string json = targetFile.text;
|
||||
_localizedText = ParseJson(json);
|
||||
_currentLanguage = langCode;
|
||||
OnLanguageChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public string Get(string key)
|
||||
{
|
||||
if (_localizedText != null && _localizedText.ContainsKey(key))
|
||||
return _localizedText[key];
|
||||
return $"[{key}]";
|
||||
}
|
||||
|
||||
private Dictionary<string, string> ParseJson(string json)
|
||||
{
|
||||
// Dummy parser for demonstration, replace with JsonUtility if using wrapper class
|
||||
// or Newtonsoft for direct dictionary parsing
|
||||
var dict = new Dictionary<string, string>();
|
||||
string[] lines = json.Split(new[] { ',', '{', '}', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
string[] parts = line.Split(':');
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
string key = parts[0].Trim(' ', '"');
|
||||
string val = parts[1].Trim(' ', '"');
|
||||
dict[key] = val;
|
||||
}
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c17a3f09ee49ff48a0e3e2b45080257
|
||||
@@ -1,36 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac201603ede0899488995be3d88ea0dc
|
||||
@@ -1,192 +1,146 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using PrimeTween;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UI
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
public class MainMenuController : MonoBehaviour
|
||||
public class MainMenuController : BaseUIController
|
||||
{
|
||||
private VisualElement _root;
|
||||
private VisualElement _logoContainer;
|
||||
public enum MenuState { Idle, Ribbon }
|
||||
private MenuState _currentState = MenuState.Idle;
|
||||
|
||||
private VisualElement _logo;
|
||||
private VisualElement _ribbon;
|
||||
private VisualElement _logoPlaceholder;
|
||||
private bool _isActive = false;
|
||||
|
||||
[Header("Animation Settings")]
|
||||
public float transitionDuration = 0.5f;
|
||||
public float idleTimeout = 5f;
|
||||
public float pulseSpeed = 2f;
|
||||
public float pulseAmount = 0.05f;
|
||||
private VisualElement _virtualCursor;
|
||||
|
||||
private float _lastInteractionTime;
|
||||
private int _lastClickFrame = -1;
|
||||
private Coroutine _currentTransition;
|
||||
private const float IDLE_TIMEOUT = 5.0f;
|
||||
|
||||
private void OnEnable()
|
||||
private Tween _pulseTween;
|
||||
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
_root = GetComponent<UIDocument>().rootVisualElement;
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
|
||||
_logoContainer = _root.Q<VisualElement>("beat-logo-container");
|
||||
_logo = _root.Q<VisualElement>("beat-logo");
|
||||
_ribbon = _root.Q<VisualElement>("menu-ribbon");
|
||||
_logoPlaceholder = _root.Q<VisualElement>("logo-placeholder");
|
||||
_logo = root.Q<VisualElement>("Logo");
|
||||
_ribbon = root.Q<VisualElement>("Ribbon");
|
||||
_virtualCursor = root.Q<VisualElement>("VirtualCursor");
|
||||
|
||||
// Đảm bảo Logo luôn có thể nhấn được
|
||||
_logoContainer.pickingMode = PickingMode.Position;
|
||||
_logo.pickingMode = PickingMode.Position;
|
||||
_logoContainer.RegisterCallback<ClickEvent>(OnLogoClicked);
|
||||
if (_logo == null)
|
||||
{
|
||||
Debug.LogError($"[MainMenuController] Element 'Logo' not found in UXML! Root children: {root.childCount}");
|
||||
return;
|
||||
}
|
||||
|
||||
_root.RegisterCallback<MouseMoveEvent>(evt => ResetIdleTimer());
|
||||
_logo.RegisterCallback<PointerDownEvent>(OnLogoClicked);
|
||||
|
||||
// Bind Buttons with null checks
|
||||
var settingsBtn = root.Q<Button>("SettingsBtn");
|
||||
if (settingsBtn != null) settingsBtn.clicked += () => uiManager.Push<SettingsController>();
|
||||
|
||||
var buttons = _root.Query<Button>().ToList();
|
||||
foreach (var btn in buttons)
|
||||
{
|
||||
btn.RegisterCallback<PointerDownEvent>(evt => ApplyDrumHit(btn, true));
|
||||
btn.RegisterCallback<PointerUpEvent>(evt => ApplyDrumHit(btn, false));
|
||||
btn.RegisterCallback<ClickEvent>(evt => ResetIdleTimer());
|
||||
}
|
||||
var joinBtn = root.Q<Button>("JoinBtn");
|
||||
if (joinBtn != null) joinBtn.clicked += () => uiManager.Push<LobbyController>();
|
||||
|
||||
var createBtn = root.Q<Button>("CreateBtn");
|
||||
if (createBtn != null) createBtn.clicked += () => uiManager.Push<LobbyController>();
|
||||
|
||||
var profileBtn = root.Q<Button>("ProfileBtn");
|
||||
if (profileBtn != null) profileBtn.clicked += () => uiManager.Push<ProfileController>();
|
||||
|
||||
var exitBtn = root.Q<Button>("ExitBtn");
|
||||
if (exitBtn != null) exitBtn.clicked += () => Application.Quit();
|
||||
|
||||
_logoContainer.RegisterCallback<PointerDownEvent>(evt => ApplyDrumHit(_logoContainer, true));
|
||||
_logoContainer.RegisterCallback<PointerUpEvent>(evt => ApplyDrumHit(_logoContainer, false));
|
||||
|
||||
// Routing
|
||||
_root.Q<Button>("btn-create")?.RegisterCallback<ClickEvent>(ev => NavigateToLobby(true));
|
||||
_root.Q<Button>("btn-join")?.RegisterCallback<ClickEvent>(ev => NavigateToLobby(false));
|
||||
_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());
|
||||
|
||||
ResetToIdleState();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
float baseScale = _isActive ? 0.35f : 1.0f;
|
||||
float pulse = Mathf.Sin(Time.time * pulseSpeed) * pulseAmount;
|
||||
_logo.style.scale = new Scale(new Vector3(baseScale + pulse, baseScale + pulse, 1f));
|
||||
|
||||
if (_isActive && _currentTransition == null)
|
||||
{
|
||||
if (Time.time - _lastInteractionTime > idleTimeout)
|
||||
_currentTransition = StartCoroutine(TransitionToIdle());
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLogoClicked(ClickEvent evt)
|
||||
{
|
||||
if (Time.frameCount == _lastClickFrame) return;
|
||||
_lastClickFrame = Time.frameCount;
|
||||
|
||||
ResetIdleTimer();
|
||||
|
||||
// QUAN TRỌNG: Chỉ vào Lobby nếu ĐÃ Active và KHÔNG đang chuyển cảnh
|
||||
if (!_isActive) {
|
||||
if (_currentTransition != null) StopCoroutine(_currentTransition);
|
||||
_currentTransition = StartCoroutine(TransitionToActive());
|
||||
} else if (_currentTransition == null) {
|
||||
NavigateToLobby(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyDrumHit(VisualElement element, bool isDown)
|
||||
{
|
||||
if (isDown)
|
||||
{
|
||||
element.style.scale = new Scale(new Vector3(0.85f, 0.85f, 1f));
|
||||
element.style.transitionDuration = new List<TimeValue> { new TimeValue(0.05f, TimeUnit.Second) };
|
||||
}
|
||||
else
|
||||
{
|
||||
element.style.scale = new Scale(Vector3.one);
|
||||
element.style.transitionDuration = new List<TimeValue> { new TimeValue(0.15f, TimeUnit.Second) };
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetIdleTimer()
|
||||
{
|
||||
StartPulse();
|
||||
_lastInteractionTime = Time.time;
|
||||
}
|
||||
|
||||
private void NavigateToLobby(bool isCreate)
|
||||
public override async Task PlayTransitionIn()
|
||||
{
|
||||
var lobby = Object.FindFirstObjectByType<LobbyController>();
|
||||
lobby?.SetMode(isCreate);
|
||||
UIManager.Instance.ShowScreen("Lobby");
|
||||
await base.PlayTransitionIn();
|
||||
UnityEngine.Cursor.visible = false;
|
||||
}
|
||||
|
||||
private void ResetToIdleState()
|
||||
public override async Task PlayTransitionOut()
|
||||
{
|
||||
_isActive = false;
|
||||
UIManager.Instance.isMainMenuActive = false;
|
||||
_ribbon.style.display = DisplayStyle.None;
|
||||
UnityEngine.Cursor.visible = true;
|
||||
await base.PlayTransitionOut();
|
||||
}
|
||||
|
||||
private void StartPulse()
|
||||
{
|
||||
// Use Vector3.one * 1.1f for target scale
|
||||
_pulseTween = Tween.Scale(_logo.transform, Vector3.one * 1.1f, duration: 0.8f, cycles: -1, cycleMode: CycleMode.Yoyo, ease: Ease.InOutSine);
|
||||
}
|
||||
|
||||
private void OnLogoClicked(PointerDownEvent evt)
|
||||
{
|
||||
_lastInteractionTime = Time.time;
|
||||
|
||||
if (_currentState == MenuState.Idle)
|
||||
{
|
||||
TransitionToRibbon();
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = uiManager.Push<LobbyController>();
|
||||
}
|
||||
}
|
||||
|
||||
private void TransitionToRibbon()
|
||||
{
|
||||
_currentState = MenuState.Ribbon;
|
||||
|
||||
_root.Add(_logoContainer);
|
||||
_logoContainer.style.position = Position.Absolute;
|
||||
_logoContainer.style.width = 300; _logoContainer.style.height = 300;
|
||||
_logoContainer.style.left = Length.Percent(50);
|
||||
_logoContainer.style.top = Length.Percent(50);
|
||||
_logoContainer.style.translate = new Translate(Length.Percent(-50), Length.Percent(-50));
|
||||
_currentTransition = null;
|
||||
}
|
||||
|
||||
private IEnumerator TransitionToActive()
|
||||
{
|
||||
_isActive = true;
|
||||
UIManager.Instance.isMainMenuActive = true;
|
||||
ResetIdleTimer();
|
||||
// Transition Logo using Custom tween for offset
|
||||
Tween.Custom(0f, -300f, duration: 0.5f, ease: Ease.OutQuad,
|
||||
onValueChange: val => _logo.style.left = val);
|
||||
|
||||
_ribbon.style.display = DisplayStyle.Flex;
|
||||
_ribbon.style.opacity = 0;
|
||||
|
||||
yield return null;
|
||||
|
||||
_logoContainer.style.transitionProperty = new List<StylePropertyName> { "translate", "opacity" };
|
||||
_logoContainer.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) };
|
||||
_ribbon.style.transitionProperty = new List<StylePropertyName> { "opacity" };
|
||||
_ribbon.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) };
|
||||
|
||||
// Trượt Logo từ tâm sang vị trí ribbon (#2)
|
||||
_logoContainer.style.translate = new Translate(Length.Percent(-75f), Length.Percent(-50f));
|
||||
_ribbon.style.opacity = 1;
|
||||
|
||||
yield return new WaitForSeconds(transitionDuration);
|
||||
|
||||
// Gán chặt vào placeholder và khóa kích thước để chống giãn
|
||||
_logoPlaceholder.Add(_logoContainer);
|
||||
_logoContainer.style.position = Position.Relative;
|
||||
_logoContainer.style.left = StyleKeyword.Auto;
|
||||
_logoContainer.style.top = StyleKeyword.Auto;
|
||||
_logoContainer.style.translate = new Translate(0, 0);
|
||||
|
||||
// Khóa kích thước nhỏ để fit vào Ribbon
|
||||
_logoContainer.style.width = 100;
|
||||
_logoContainer.style.height = 100;
|
||||
|
||||
_currentTransition = null;
|
||||
// Fade in Ribbon
|
||||
Tween.Custom(0f, 1f, duration: 0.5f, onValueChange: val => _ribbon.style.opacity = val);
|
||||
}
|
||||
|
||||
private IEnumerator TransitionToIdle()
|
||||
private void TransitionToIdle()
|
||||
{
|
||||
_isActive = false;
|
||||
UIManager.Instance.isMainMenuActive = false;
|
||||
_currentState = MenuState.Idle;
|
||||
|
||||
Tween.Custom(-300f, 0f, duration: 0.5f, ease: Ease.OutQuad,
|
||||
onValueChange: val => _logo.style.left = val);
|
||||
|
||||
_root.Add(_logoContainer);
|
||||
_logoContainer.style.position = Position.Absolute;
|
||||
_logoContainer.style.width = 300; _logoContainer.style.height = 300;
|
||||
_logoContainer.style.left = Length.Percent(50);
|
||||
_logoContainer.style.top = Length.Percent(50);
|
||||
_logoContainer.style.translate = new Translate(Length.Percent(-75f), Length.Percent(-50f));
|
||||
Tween.Custom(1f, 0f, duration: 0.5f, onValueChange: val => _ribbon.style.opacity = val)
|
||||
.OnComplete(() => _ribbon.style.display = DisplayStyle.None);
|
||||
}
|
||||
|
||||
yield return null;
|
||||
public void Update()
|
||||
{
|
||||
UpdateVirtualCursor();
|
||||
|
||||
_logoContainer.style.translate = new Translate(Length.Percent(-50f), Length.Percent(-50f));
|
||||
_ribbon.style.opacity = 0;
|
||||
if (_currentState == MenuState.Ribbon)
|
||||
{
|
||||
if (Time.time - _lastInteractionTime > IDLE_TIMEOUT)
|
||||
{
|
||||
TransitionToIdle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(transitionDuration);
|
||||
_ribbon.style.display = DisplayStyle.None;
|
||||
_currentTransition = null;
|
||||
private void UpdateVirtualCursor()
|
||||
{
|
||||
if (_virtualCursor == null) return;
|
||||
|
||||
Vector2 mousePos = Input.mousePosition;
|
||||
float x = mousePos.x;
|
||||
float y = Screen.height - mousePos.y;
|
||||
|
||||
if (_currentState == MenuState.Ribbon)
|
||||
{
|
||||
y = Screen.height / 2f + 50;
|
||||
}
|
||||
|
||||
_virtualCursor.style.left = x - _virtualCursor.layout.width / 2;
|
||||
_virtualCursor.style.top = y - _virtualCursor.layout.height / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
using OnlyScove.Scripts;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class MyUIDisplay : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
public PlayerDebugProvider playerDebugProvider;
|
||||
|
||||
[Header("UI Prompt")]
|
||||
public GameObject interactionPromptContainer;
|
||||
|
||||
public UnityEngine.UI.Text interactionPromptText;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
//if (playerDebugProvider == null)
|
||||
//playerDebugProvider = FindFirstObjectByType<PlayerDebugProvider>();
|
||||
|
||||
// Luôn ẩn lúc bắt đầu
|
||||
if (interactionPromptContainer != null)
|
||||
interactionPromptContainer.SetActive(false);
|
||||
|
||||
TryFindPlayer();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (playerDebugProvider == null)
|
||||
{
|
||||
TryFindPlayer();
|
||||
if (playerDebugProvider == null) return;
|
||||
}
|
||||
if (interactionPromptContainer == null) return;
|
||||
|
||||
IInteractable interactable = playerDebugProvider.GetActiveInteractable();
|
||||
|
||||
if (interactable != null)
|
||||
{
|
||||
// Hiện UI tại vị trí cố định bạn đã đặt trong Canvas
|
||||
interactionPromptContainer.SetActive(true);
|
||||
|
||||
if (!interactionPromptContainer.activeSelf)
|
||||
{
|
||||
interactionPromptContainer.SetActive(true);
|
||||
}
|
||||
|
||||
if (interactionPromptText != null)
|
||||
interactionPromptText.text = interactable.InteractionPrompt;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (interactionPromptContainer.activeSelf)
|
||||
interactionPromptContainer.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void TryFindPlayer()
|
||||
{
|
||||
if (PlayerStateMachine.Local != null)
|
||||
{
|
||||
playerDebugProvider = PlayerStateMachine.Local.GetComponent<PlayerDebugProvider>();
|
||||
}
|
||||
|
||||
if (playerDebugProvider == null)
|
||||
{
|
||||
playerDebugProvider = Object.FindFirstObjectByType<PlayerDebugProvider>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cd13c5c96000414397dd7d41a73edd62
|
||||
timeCreated: 1773383951
|
||||
@@ -1,14 +1,37 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UI
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
public class ProfileController : MonoBehaviour
|
||||
public class ProfileController : BaseUIController
|
||||
{
|
||||
private void OnEnable()
|
||||
private Label _username;
|
||||
private Label _rank;
|
||||
private ProgressBar _winRateBar;
|
||||
private Label _winRateText;
|
||||
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
var root = GetComponent<UIDocument>().rootVisualElement;
|
||||
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.GoBack());
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
|
||||
_username = root.Q<Label>("Username");
|
||||
_rank = root.Q<Label>("Rank");
|
||||
_winRateBar = root.Q<ProgressBar>("WinRateBar");
|
||||
_winRateText = root.Q<Label>("WinRateText");
|
||||
|
||||
root.Q<Button>("BackBtn").clicked += () => uiManager.Pop();
|
||||
|
||||
LoadProfileData();
|
||||
}
|
||||
|
||||
private void LoadProfileData()
|
||||
{
|
||||
// Dummy data for now
|
||||
_username.text = "GamerPro_2026";
|
||||
_rank.text = "DIAMOND II";
|
||||
_winRateBar.value = 72;
|
||||
_winRateText.text = "72%";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,63 +1,52 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Collections.Generic;
|
||||
using PrimeTween;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UI
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
public class SettingsController : MonoBehaviour
|
||||
public class SettingsController : BaseUIController
|
||||
{
|
||||
private VisualElement _contentGeneral;
|
||||
private VisualElement _contentGraphics;
|
||||
private VisualElement _contentAudio;
|
||||
private VisualElement _contentControls;
|
||||
private VisualElement _sidebar;
|
||||
private Label _tabTitle;
|
||||
private ScrollView _content;
|
||||
|
||||
private Button _tabGeneral;
|
||||
private Button _tabGraphics;
|
||||
private Button _tabAudio;
|
||||
private Button _tabControls;
|
||||
|
||||
private void OnEnable()
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
var root = GetComponent<UIDocument>().rootVisualElement;
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
|
||||
// Tabs
|
||||
_tabGeneral = root.Q<Button>("tab-general");
|
||||
_tabGraphics = root.Q<Button>("tab-graphics");
|
||||
_tabAudio = root.Q<Button>("tab-audio");
|
||||
_tabControls = root.Q<Button>("tab-controls");
|
||||
_sidebar = root.Q<VisualElement>("Sidebar");
|
||||
_tabTitle = root.Q<Label>("TabTitle");
|
||||
_content = root.Q<ScrollView>("SettingsContent");
|
||||
|
||||
// Content
|
||||
_contentGeneral = root.Q<VisualElement>("content-general");
|
||||
_contentGraphics = root.Q<VisualElement>("content-graphics");
|
||||
_contentAudio = root.Q<VisualElement>("content-audio");
|
||||
_contentControls = root.Q<VisualElement>("content-controls");
|
||||
|
||||
// Register Tab Events
|
||||
_tabGeneral?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentGeneral, _tabGeneral));
|
||||
_tabGraphics?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentGraphics, _tabGraphics));
|
||||
_tabAudio?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentAudio, _tabAudio));
|
||||
_tabControls?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentControls, _tabControls));
|
||||
|
||||
// Close
|
||||
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ToggleSettings());
|
||||
root.Q<Button>("GeneralTab").clicked += () => SwitchTab("GENERAL");
|
||||
root.Q<Button>("VideoTab").clicked += () => SwitchTab("VIDEO");
|
||||
root.Q<Button>("SoundTab").clicked += () => SwitchTab("SOUND");
|
||||
root.Q<Button>("ControlTab").clicked += () => SwitchTab("CONTROL");
|
||||
root.Q<Button>("CloseSettingsBtn").clicked += () => uiManager.Pop();
|
||||
}
|
||||
|
||||
private void SwitchTab(VisualElement targetContent, Button targetTab)
|
||||
private void SwitchTab(string title)
|
||||
{
|
||||
// Hide all
|
||||
_contentGeneral.style.display = DisplayStyle.None;
|
||||
if(_contentGraphics != null) _contentGraphics.style.display = DisplayStyle.None;
|
||||
if(_contentAudio != null) _contentAudio.style.display = DisplayStyle.None;
|
||||
if(_contentControls != null) _contentControls.style.display = DisplayStyle.None;
|
||||
_tabTitle.text = title;
|
||||
// Clear and add specific settings content
|
||||
_content.Clear();
|
||||
_content.Add(new Label($"Settings for {title} coming soon..."));
|
||||
}
|
||||
|
||||
_tabGeneral.RemoveFromClassList("active-tab");
|
||||
_tabGraphics.RemoveFromClassList("active-tab");
|
||||
_tabAudio.RemoveFromClassList("active-tab");
|
||||
_tabControls.RemoveFromClassList("active-tab");
|
||||
public override async Task PlayTransitionIn()
|
||||
{
|
||||
Show();
|
||||
_sidebar.style.translate = new StyleTranslate(new Translate(Length.Percent(-100), 0));
|
||||
await Tween.Custom(-100f, 0f, duration: 0.4f, ease: Ease.OutQuad,
|
||||
onValueChange: val => _sidebar.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
|
||||
}
|
||||
|
||||
// Show target
|
||||
targetContent.style.display = DisplayStyle.Flex;
|
||||
targetTab.AddToClassList("active-tab");
|
||||
public override async Task PlayTransitionOut()
|
||||
{
|
||||
await Tween.Custom(0f, -100f, duration: 0.4f, ease: Ease.InQuad,
|
||||
onValueChange: val => _sidebar.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
|
||||
Hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,246 +1,126 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Linq;
|
||||
|
||||
namespace UI
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
[RequireComponent(typeof(UIDocument))]
|
||||
public class UIManager : MonoBehaviour
|
||||
{
|
||||
public static UIManager Instance { get; private set; }
|
||||
|
||||
[System.Serializable]
|
||||
public class ScreenData
|
||||
{
|
||||
public string screenName;
|
||||
public UIDocument document;
|
||||
public bool isOverlay;
|
||||
public bool isActive;
|
||||
}
|
||||
private UIDocument _uiDocument;
|
||||
private VisualElement _rootElement;
|
||||
|
||||
public List<ScreenData> screens = new List<ScreenData>();
|
||||
public string initialScreen = "MainMenu";
|
||||
|
||||
[Header("Cursor & Trail Settings")]
|
||||
public Sprite trailSprite;
|
||||
public float trailFadeSpeed = 3f;
|
||||
public int trailCount = 15;
|
||||
public float focusRadius = 500f;
|
||||
private readonly Dictionary<Type, BaseUIController> _controllers = new Dictionary<Type, BaseUIController>();
|
||||
private readonly Stack<BaseUIController> _history = new Stack<BaseUIController>();
|
||||
|
||||
private VisualElement _customCursor;
|
||||
private List<VisualElement> _trailPool = new List<VisualElement>();
|
||||
private int _trailIndex = 0;
|
||||
[Header("UI Templates")]
|
||||
[SerializeField] private VisualTreeAsset mainMenuTemplate;
|
||||
[SerializeField] private VisualTreeAsset lobbyTemplate;
|
||||
[SerializeField] private VisualTreeAsset profileTemplate;
|
||||
[SerializeField] private VisualTreeAsset settingsTemplate;
|
||||
[SerializeField] private VisualTreeAsset hudTemplate;
|
||||
|
||||
[Header("Editor Preview")]
|
||||
[Range(0f, 1f)]
|
||||
public float globalOpacity = 1f;
|
||||
[Header("Debug Settings")]
|
||||
[SerializeField] private bool showDebugInfo = true;
|
||||
|
||||
private Stack<string> _navigationStack = new Stack<string>();
|
||||
private string _currentScreenName;
|
||||
private VisualElement _lastHoveredElement;
|
||||
|
||||
public bool isMainMenuActive = false;
|
||||
private bool _isSettingsOpen = false;
|
||||
private MainMenuController _mainMenuController;
|
||||
private LobbyController _lobbyController;
|
||||
private ProfileController _profileController;
|
||||
private SettingsController _settingsController;
|
||||
private HUDController _hudController;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null) Instance = this;
|
||||
else { Destroy(gameObject); return; }
|
||||
|
||||
var myDoc = GetComponent<UIDocument>();
|
||||
if (myDoc != null) myDoc.sortingOrder = 1000;
|
||||
|
||||
SetupCursor();
|
||||
foreach (var s in screens)
|
||||
if (Instance == null)
|
||||
{
|
||||
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
ShowScreen(initialScreen);
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
_uiDocument = GetComponent<UIDocument>();
|
||||
_rootElement = _uiDocument.rootVisualElement;
|
||||
|
||||
InitializeControllers();
|
||||
}
|
||||
|
||||
private void SetupCursor()
|
||||
private void InitializeControllers()
|
||||
{
|
||||
UIDocument doc = GetComponent<UIDocument>();
|
||||
if (doc == null && screens.Count > 0) doc = screens[0].document;
|
||||
if (doc == null) return;
|
||||
|
||||
var root = doc.rootVisualElement;
|
||||
_mainMenuController = RegisterController<MainMenuController>(mainMenuTemplate);
|
||||
_lobbyController = RegisterController<LobbyController>(lobbyTemplate);
|
||||
_profileController = RegisterController<ProfileController>(profileTemplate);
|
||||
_settingsController = RegisterController<SettingsController>(settingsTemplate);
|
||||
_hudController = RegisterController<HUDController>(hudTemplate);
|
||||
|
||||
_customCursor = new VisualElement();
|
||||
_customCursor.style.width = 25; _customCursor.style.height = 25;
|
||||
_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);
|
||||
// Start with Main Menu
|
||||
_ = Push<MainMenuController>();
|
||||
}
|
||||
|
||||
for (int i = 0; i < trailCount; i++)
|
||||
private T RegisterController<T>(VisualTreeAsset template) where T : BaseUIController, new()
|
||||
{
|
||||
if (template == null)
|
||||
{
|
||||
var trail = new VisualElement();
|
||||
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;
|
||||
}
|
||||
trail.style.position = Position.Absolute;
|
||||
trail.pickingMode = PickingMode.Ignore;
|
||||
root.Add(trail);
|
||||
_trailPool.Add(trail);
|
||||
Debug.LogWarning($"Template for {typeof(T).Name} is missing!");
|
||||
return null;
|
||||
}
|
||||
_customCursor.BringToFront();
|
||||
|
||||
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);
|
||||
|
||||
var controller = new T();
|
||||
controller.Initialize(instance, this);
|
||||
_controllers[typeof(T)] = controller;
|
||||
|
||||
return controller;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Vector2 mousePos = Input.mousePosition;
|
||||
bool isMainMenu = (_currentScreenName == "MainMenu");
|
||||
bool restrictY = (isMainMenu && isMainMenuActive && !_isSettingsOpen);
|
||||
float targetY = restrictY ? Screen.height / 2f : mousePos.y;
|
||||
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - targetY);
|
||||
|
||||
bool showCursor = !isMainMenu || _isSettingsOpen;
|
||||
DisplayStyle cursorDisplay = showCursor ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
|
||||
if (_customCursor != null)
|
||||
{
|
||||
_customCursor.style.display = cursorDisplay;
|
||||
_customCursor.style.left = uiPos.x - 12.5f;
|
||||
_customCursor.style.top = uiPos.y - 12.5f;
|
||||
}
|
||||
|
||||
if (_trailPool.Count > 0)
|
||||
{
|
||||
var currentTrail = _trailPool[_trailIndex];
|
||||
currentTrail.style.display = cursorDisplay;
|
||||
currentTrail.style.left = uiPos.x - 10;
|
||||
currentTrail.style.top = uiPos.y - 10;
|
||||
currentTrail.style.opacity = 0.6f;
|
||||
|
||||
foreach(var t in _trailPool)
|
||||
{
|
||||
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;
|
||||
}
|
||||
_trailIndex = (_trailIndex + 1) % _trailPool.Count;
|
||||
}
|
||||
|
||||
HandleVirtualInput(uiPos);
|
||||
|
||||
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
|
||||
ToggleSettings();
|
||||
_mainMenuController?.Update();
|
||||
_hudController?.Update();
|
||||
}
|
||||
|
||||
private void HandleVirtualInput(Vector2 uiPos)
|
||||
public async Task Push<T>() where T : BaseUIController
|
||||
{
|
||||
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;
|
||||
|
||||
VisualElement bestElement = null;
|
||||
float minDistance = float.MaxValue;
|
||||
var interactables = activeDoc.rootVisualElement.Query<VisualElement>()
|
||||
.Where(e => e.focusable && e.pickingMode != PickingMode.Ignore).ToList();
|
||||
|
||||
foreach (var element in interactables)
|
||||
if (!_controllers.TryGetValue(typeof(T), out var newScreen))
|
||||
{
|
||||
Rect worldBounds = element.worldBound;
|
||||
float dist = Vector2.Distance(uiPos, worldBounds.center);
|
||||
if (dist < minDistance && dist < focusRadius) {
|
||||
minDistance = dist;
|
||||
bestElement = element;
|
||||
}
|
||||
Debug.LogError($"Controller of type {typeof(T)} not registered!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (bestElement != _lastHoveredElement)
|
||||
if (_history.Count > 0)
|
||||
{
|
||||
_lastHoveredElement?.RemoveFromClassList("hover");
|
||||
bestElement?.AddToClassList("hover");
|
||||
_lastHoveredElement = bestElement;
|
||||
var currentScreen = _history.Peek();
|
||||
await currentScreen.PlayTransitionOut();
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0) && _lastHoveredElement != null)
|
||||
{
|
||||
using (var clickEvent = ClickEvent.GetPooled()) {
|
||||
clickEvent.target = _lastHoveredElement;
|
||||
_lastHoveredElement.SendEvent(clickEvent);
|
||||
}
|
||||
}
|
||||
_history.Push(newScreen);
|
||||
await newScreen.PlayTransitionIn();
|
||||
}
|
||||
|
||||
// --- Editor Support Methods (Restored) ---
|
||||
|
||||
public void SyncScreens()
|
||||
public async Task Pop()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (_history.Count <= 1) return;
|
||||
|
||||
var currentScreen = _history.Pop();
|
||||
await currentScreen.PlayTransitionOut();
|
||||
|
||||
var previousScreen = _history.Peek();
|
||||
await previousScreen.PlayTransitionIn();
|
||||
}
|
||||
|
||||
public void ShowOnly(string name)
|
||||
{
|
||||
foreach (var screen in screens)
|
||||
{
|
||||
screen.isActive = (screen.screenName == name);
|
||||
}
|
||||
SyncScreens();
|
||||
}
|
||||
|
||||
// --- Runtime Logic ---
|
||||
|
||||
public void ShowScreen(string name)
|
||||
{
|
||||
var screen = screens.Find(s => s.screenName == name);
|
||||
if (screen == null) return;
|
||||
|
||||
if (!screen.isOverlay)
|
||||
{
|
||||
foreach(var s in screens) if(!s.isOverlay && s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
_navigationStack.Push(name);
|
||||
_currentScreenName = name;
|
||||
}
|
||||
|
||||
screen.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
screen.isActive = true;
|
||||
UnityEngine.Cursor.visible = false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void ToggleSettings()
|
||||
{
|
||||
var settings = screens.Find(s => s.screenName == "Settings");
|
||||
if (settings == null) return;
|
||||
_isSettingsOpen = settings.document.rootVisualElement.style.display == DisplayStyle.None;
|
||||
settings.document.rootVisualElement.style.display = _isSettingsOpen ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
if (_isSettingsOpen) settings.document.sortingOrder = 999;
|
||||
}
|
||||
// Custom Inspector features can be added here with [ContextMenu] or CustomEditor
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user