This commit is contained in:
Lucastaa
2026-04-27 18:50:34 +07:00
89 changed files with 31087 additions and 1445 deletions

View File

@@ -8,7 +8,7 @@
public class _LobbyManager : MonoBehaviour
{
public GameObject lobbyPanel;
public GameObject characterSelectionPanel;
public _BasicSpawner spawner;
@@ -24,7 +24,6 @@
async void Start()
{
lobbyPanel.SetActive(false);
characterSelectionPanel.SetActive(true);
spawner = FindFirstObjectByType<_BasicSpawner>();
@@ -52,7 +51,7 @@
// đưa lên host để tạo player object, ở đây tạm thời chỉ log ra console
Debug.Log($"Player Name: {_profile.Name}, Class: {_profile.Role}");
// chuyển sang lobby panel
characterSelectionPanel.SetActive(false);
lobbyPanel.SetActive(true);
}

View File

@@ -14,7 +14,7 @@ public struct _PlayerProfile
public _Role Role;
}
public class PlayerInfo : NetworkBehaviour
public class _PlayerInfo : NetworkBehaviour
{
[Networked] public string playerName { get; set; }

View File

@@ -1,6 +1,8 @@
using UnityEngine;
using UnityEngine.UIElements;
using OnlyScove.Scripts;
using System.Collections.Generic;
using UnityEngine.InputSystem;
namespace UI
{
@@ -12,10 +14,16 @@ namespace UI
private VisualElement _healthFill;
private VisualElement _staminaFill;
private Label _healthText;
private Label _noiseLabel;
private Label _interactionLabel;
private VisualElement _interactionPrompt;
private Button _btnToggleView;
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()
{
@@ -27,27 +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");
_btnToggleView = root.Q<Button>("btn-toggle-view");
if (_btnToggleView != null)
{
_btnToggleView.clicked += () => {
var cam = Object.FindFirstObjectByType<CameraController>();
if (cam != null) cam.ToggleCameraView();
};
}
_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;
@@ -56,46 +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 - Kiểm tra xem đã Spawned chưa nếu là Network Object
if (player.Object == null || player.Object.IsValid)
{
UpdateHealth(_currentPlayer.Health);
UpdateStamina(_currentPlayer.Stamina);
UpdateNoise(_currentPlayer.NoiseLevel);
}
UpdateHealth(_currentPlayer.Health);
UpdateStamina(_currentPlayer.Stamina);
}
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)
@@ -106,6 +185,8 @@ namespace UI
{
_interactionPrompt.style.display = DisplayStyle.Flex;
if (_interactionLabel != null) _interactionLabel.text = interactable.InteractionPrompt;
_lastInputTime = Time.time;
SetHUDVisibility(true);
}
else
{

View File

@@ -5,46 +5,71 @@ namespace UI
{
public class LobbyController : MonoBehaviour
{
private UIDocument _doc;
private Button _btnLeave;
private Button _btnStart;
private ScrollView _playerList;
private VisualElement _joinView;
private VisualElement _createView;
private float _lastInteractionTime;
private bool _isCreateMode = false;
private const float AutoReturnDelay = 5f;
private void OnEnable()
{
_doc = GetComponent<UIDocument>();
var root = _doc.rootVisualElement;
var root = GetComponent<UIDocument>().rootVisualElement;
// BINDING: Tìm element theo tên (Name) đã đặt trong UXML
_btnLeave = root.Q<Button>("btn-leave");
_btnStart = root.Q<Button>("btn-start");
_playerList = root.Q<ScrollView>("player-list");
_joinView = root.Q<VisualElement>("join-view");
_createView = root.Q<VisualElement>("create-view");
// ĐĂNG KÝ SỰ KIỆN:
if (_btnLeave != null)
_btnLeave.clicked += OnLeaveClicked;
// Back button
root.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.GoBack());
root.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ToggleSettings());
// Create confirm -> Lounge
root.Q<Button>("btn-create-confirm")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ShowScreen("Lounge"));
if (_btnStart != null)
_btnStart.clicked += OnStartClicked;
// 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();
}
private void OnLeaveClicked()
private void Update()
{
Debug.Log("User clicked LEAVE room!");
UIManager.Instance.ShowScreen("MainMenu");
if (_isCreateMode)
{
if (Time.time - _lastInteractionTime > AutoReturnDelay)
{
SetMode(false); // Auto return to Stage 1
}
}
}
private void OnStartClicked()
public void SetMode(bool isCreate)
{
Debug.Log("Host clicked START GAME!");
UIManager.Instance.ShowScreen("HUD"); // Chuyển vào HUD khi game bắt đầu
_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 OnDisable()
private void ResetInteractionTimer()
{
// Hủy đăng ký để tránh memory leak
if (_btnLeave != null) _btnLeave.clicked -= OnLeaveClicked;
if (_btnStart != null) _btnStart.clicked -= OnStartClicked;
_lastInteractionTime = Time.time;
}
}
}

View File

@@ -18,7 +18,8 @@ namespace UI
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
if (transform.parent == null)
DontDestroyOnLoad(gameObject);
LoadLanguage(_currentLanguage);
}
else

View 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);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ac201603ede0899488995be3d88ea0dc

View File

@@ -1,23 +1,192 @@
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections;
using System.Collections.Generic;
namespace UI
{
public class MainMenuController : MonoBehaviour
{
private UIDocument _doc;
private VisualElement _root;
private VisualElement _logoContainer;
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 float _lastInteractionTime;
private int _lastClickFrame = -1;
private Coroutine _currentTransition;
private void OnEnable()
{
_doc = GetComponent<UIDocument>();
var root = _doc.rootVisualElement;
_root = GetComponent<UIDocument>().rootVisualElement;
// Nối dây các nút bấm
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());
_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");
// Đảm bảo Logo luôn có thể nhấn được
_logoContainer.pickingMode = PickingMode.Position;
_logo.pickingMode = PickingMode.Position;
_logoContainer.RegisterCallback<ClickEvent>(OnLogoClicked);
_root.RegisterCallback<MouseMoveEvent>(evt => ResetIdleTimer());
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());
}
_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()
{
_lastInteractionTime = Time.time;
}
private void NavigateToLobby(bool isCreate)
{
var lobby = Object.FindFirstObjectByType<LobbyController>();
lobby?.SetMode(isCreate);
UIManager.Instance.ShowScreen("Lobby");
}
private void ResetToIdleState()
{
_isActive = false;
UIManager.Instance.isMainMenuActive = false;
_ribbon.style.display = DisplayStyle.None;
_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();
_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;
}
private IEnumerator TransitionToIdle()
{
_isActive = false;
UIManager.Instance.isMainMenuActive = false;
_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));
yield return null;
_logoContainer.style.translate = new Translate(Length.Percent(-50f), Length.Percent(-50f));
_ribbon.style.opacity = 0;
yield return new WaitForSeconds(transitionDuration);
_ribbon.style.display = DisplayStyle.None;
_currentTransition = null;
}
}
}

View File

@@ -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());
}
}
}

View File

@@ -1,151 +1,63 @@
using UnityEngine;
using UnityEngine.UIElements;
using OnlyScove.Scripts;
using System.Collections.Generic;
namespace UI
{
public class SettingsController : MonoBehaviour
{
private UIDocument _doc;
private CameraController _cameraController;
private VisualElement _contentGeneral;
private VisualElement _contentGraphics;
private VisualElement _contentAudio;
private VisualElement _contentControls;
// Tabs Content
private VisualElement _contentGeneral, _contentGraphics, _contentAudio;
private Button _tabGeneral, _tabGraphics, _tabAudio, _tabControls;
private Button _tabGeneral;
private Button _tabGraphics;
private Button _tabAudio;
private Button _tabControls;
private void OnEnable()
{
_doc = GetComponent<UIDocument>();
_cameraController = Object.FindFirstObjectByType<CameraController>();
var root = GetComponent<UIDocument>().rootVisualElement;
var root = _doc.rootVisualElement;
// Query Tabs
// 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");
// Query Content
// 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");
// Events
_tabGeneral.clicked += () => SwitchTab(_contentGeneral, _tabGeneral);
_tabGraphics.clicked += () => SwitchTab(_contentGraphics, _tabGraphics);
_tabAudio.clicked += () => SwitchTab(_contentAudio, _tabAudio);
// 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));
root.Q<Button>("btn-back").clicked += () => UIManager.Instance.ToggleSettings();
// Camera Binding (FOV)
var fovSlider = root.Q<Slider>("setting-fov");
if (fovSlider != null)
{
if (SettingsManager.Instance != null) fovSlider.value = SettingsManager.Instance.Settings.fieldOfView;
fovSlider.RegisterValueChangedCallback(evt => {
if (SettingsManager.Instance != null) SettingsManager.Instance.Settings.fieldOfView = evt.newValue;
if (_cameraController != null) _cameraController.SetFOV(evt.newValue);
});
}
// Sensitivity Binding
var sensSlider = root.Q<Slider>("setting-sensitivity");
if (sensSlider != null)
{
if (SettingsManager.Instance != null) sensSlider.value = SettingsManager.Instance.Settings.sensitivity;
sensSlider.RegisterValueChangedCallback(evt => {
if (SettingsManager.Instance != null) SettingsManager.Instance.SetSensitivity(evt.newValue);
});
}
// Invert X Binding
var invertXToggle = root.Q<Toggle>("setting-invert-x");
if (invertXToggle != null)
{
if (SettingsManager.Instance != null) invertXToggle.value = SettingsManager.Instance.Settings.invertX;
invertXToggle.RegisterValueChangedCallback(evt => {
if (SettingsManager.Instance != null) SettingsManager.Instance.SetInvertX(evt.newValue);
});
}
// Invert Y Binding
var invertYToggle = root.Q<Toggle>("setting-invert-y");
if (invertYToggle != null)
{
if (SettingsManager.Instance != null) invertYToggle.value = SettingsManager.Instance.Settings.invertY;
invertYToggle.RegisterValueChangedCallback(evt => {
if (SettingsManager.Instance != null) SettingsManager.Instance.SetInvertY(evt.newValue);
});
}
// Side Bias Binding
var sideDropdown = root.Q<DropdownField>("setting-camera-side");
if (sideDropdown != null)
{
if (SettingsManager.Instance != null) sideDropdown.index = SettingsManager.Instance.Settings.sideBiasRight ? 0 : 1;
sideDropdown.RegisterValueChangedCallback(evt => {
if (SettingsManager.Instance != null) SettingsManager.Instance.SetSideBias(evt.newValue == "Right");
});
}
// Language Binding
var langDropdown = root.Q<DropdownField>("setting-language");
if (langDropdown != null)
{
langDropdown.RegisterValueChangedCallback(evt => {
string code = evt.newValue == "English" ? "en" : "vi";
LocalizationManager.Instance.LoadLanguage(code);
});
}
// Lắng nghe sự kiện đổi ngôn ngữ để cập nhật Text
if (LocalizationManager.Instance != null)
{
LocalizationManager.Instance.OnLanguageChanged += UpdateTexts;
UpdateTexts();
}
else
{
Debug.LogWarning("[SettingsController] LocalizationManager Instance not found in scene!");
}
// Close
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.ToggleSettings());
}
private void SwitchTab(VisualElement targetContent, Button targetTab)
{
if (targetContent == null || targetTab == null) return;
// 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;
// Ẩn tất cả (Thêm null check)
if (_contentGeneral != null) _contentGeneral.style.display = DisplayStyle.None;
if (_contentGraphics != null) _contentGraphics.style.display = DisplayStyle.None;
if (_contentAudio != null) _contentAudio.style.display = DisplayStyle.None;
_tabGeneral.RemoveFromClassList("active-tab");
_tabGraphics.RemoveFromClassList("active-tab");
_tabAudio.RemoveFromClassList("active-tab");
_tabControls.RemoveFromClassList("active-tab");
if (_tabGeneral != null) _tabGeneral.RemoveFromClassList("active-tab");
if (_tabGraphics != null) _tabGraphics.RemoveFromClassList("active-tab");
if (_tabAudio != null) _tabAudio.RemoveFromClassList("active-tab");
// Hiện cái được chọn
// Show target
targetContent.style.display = DisplayStyle.Flex;
targetTab.AddToClassList("active-tab");
}
private void UpdateTexts()
{
if (LocalizationManager.Instance == null) return;
var root = _doc.rootVisualElement;
// Dùng null-conditional operator (?.) để cực kỳ an toàn
var titleLabel = root.Q<Label>("title");
if (titleLabel != null) titleLabel.text = LocalizationManager.Instance.Get("settings_title");
if (_tabGeneral != null) _tabGeneral.text = LocalizationManager.Instance.Get("settings_general");
if (_tabGraphics != null) _tabGraphics.text = LocalizationManager.Instance.Get("settings_graphics");
if (_tabAudio != null) _tabAudio.text = LocalizationManager.Instance.Get("settings_audio");
if (_tabControls != null) _tabControls.text = LocalizationManager.Instance.Get("settings_controls");
var btnBack = root.Q<Button>("btn-back");
if (btnBack != null) btnBack.text = LocalizationManager.Instance.Get("settings_back");
}
}
}

View File

@@ -2,6 +2,7 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using System.Linq;
namespace UI
{
@@ -12,159 +13,234 @@ 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;
}
public List<ScreenData> screens = new List<ScreenData>();
[Header("Default Settings")]
public Texture2D defaultCursor;
public string initialScreen = "MainMenu";
[Header("Cursor & Trail Settings")]
public Sprite trailSprite;
public float trailFadeSpeed = 3f;
public int trailCount = 15;
public float focusRadius = 500f;
private VisualElement _customCursor;
private List<VisualElement> _trailPool = new List<VisualElement>();
private int _trailIndex = 0;
[Header("Editor Preview")]
[Range(0f, 1f)]
public float globalOpacity = 1f;
private Stack<string> _navigationStack = new Stack<string>();
private string _currentScreenName;
private VisualElement _lastHoveredElement;
public bool isMainMenuActive = false;
private bool _isSettingsOpen = false;
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
foreach (var screen in screens)
var myDoc = GetComponent<UIDocument>();
if (myDoc != null) myDoc.sortingOrder = 1000;
SetupCursor();
foreach (var s 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 (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
}
_currentScreenName = initialScreen;
ShowScreen(initialScreen);
}
private void SetupCursor()
{
UIDocument doc = GetComponent<UIDocument>();
if (doc == null && screens.Count > 0) doc = screens[0].document;
if (doc == null) return;
var root = doc.rootVisualElement;
_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);
for (int i = 0; i < trailCount; i++)
{
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);
}
_customCursor.BringToFront();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape)) ToggleSettings();
HandleAltCursor();
}
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);
private void HandleAltCursor()
{
// Only handle Alt cursor when we are in game (HUD is active)
// and Settings is NOT currently toggled on.
var settingsData = screens.Find(s => s.screenName == "Settings");
bool settingsActive = settingsData != null && settingsData.isActive;
bool showCursor = !isMainMenu || _isSettingsOpen;
DisplayStyle cursorDisplay = showCursor ? DisplayStyle.Flex : DisplayStyle.None;
if (_currentScreenName == "HUD" && !settingsActive)
if (_customCursor != null)
{
if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
{
UnityEngine.Cursor.visible = true;
UnityEngine.Cursor.lockState = CursorLockMode.None;
}
else
{
UnityEngine.Cursor.visible = false;
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
}
}
}
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;
_customCursor.style.display = cursorDisplay;
_customCursor.style.left = uiPos.x - 12.5f;
_customCursor.style.top = uiPos.y - 12.5f;
}
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 (_trailPool.Count > 0)
{
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);
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;
// Tùy vào game là FPS hay Menu mà ẩn chuột
if (_currentScreenName == "HUD")
foreach(var t in _trailPool)
{
UnityEngine.Cursor.visible = false;
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
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();
}
private void HandleVirtualInput(Vector2 uiPos)
{
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)
{
Rect worldBounds = element.worldBound;
float dist = Vector2.Distance(uiPos, worldBounds.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);
}
}
}
// --- Editor Support Methods (Restored) ---
public void SyncScreens()
{
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.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 ---
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;
}
}
}