This commit is contained in:
2026-04-25 18:20:16 +07:00
parent 62cdf7c754
commit 034b9599f3
24 changed files with 1359 additions and 201 deletions

View File

@@ -23,7 +23,6 @@ namespace UI
var root = hudDocument.rootVisualElement;
// Tìm các thành phần UI theo Name (Bạn cần đặt tên này trong UXML)
_healthFill = root.Q<VisualElement>("health-fill");
_staminaFill = root.Q<VisualElement>("stamina-fill");
_healthText = root.Q<Label>("health-text");

View File

@@ -0,0 +1,50 @@
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
public class LobbyController : MonoBehaviour
{
private UIDocument _doc;
private Button _btnLeave;
private Button _btnStart;
private ScrollView _playerList;
private void OnEnable()
{
_doc = GetComponent<UIDocument>();
var root = _doc.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");
// ĐĂNG KÝ SỰ KIỆN:
if (_btnLeave != null)
_btnLeave.clicked += OnLeaveClicked;
if (_btnStart != null)
_btnStart.clicked += OnStartClicked;
}
private void OnLeaveClicked()
{
Debug.Log("User clicked LEAVE room!");
UIManager.Instance.ShowScreen("MainMenu");
}
private void OnStartClicked()
{
Debug.Log("Host clicked START GAME!");
UIManager.Instance.ShowScreen("HUD"); // Chuyển vào HUD khi game bắt đầu
}
private void OnDisable()
{
// Hủy đăng ký để tránh memory leak
if (_btnLeave != null) _btnLeave.clicked -= OnLeaveClicked;
if (_btnStart != null) _btnStart.clicked -= OnStartClicked;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9c37c552a9c18a242bcc8860a0a5212f

View File

@@ -0,0 +1,69 @@
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;
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;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5c17a3f09ee49ff48a0e3e2b45080257

View File

@@ -0,0 +1,23 @@
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
public class MainMenuController : MonoBehaviour
{
private UIDocument _doc;
private void OnEnable()
{
_doc = GetComponent<UIDocument>();
var root = _doc.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());
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 691980524acfc544f9660cfc35ce3616

View File

@@ -0,0 +1,14 @@
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
public class ProfileController : MonoBehaviour
{
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("MainMenu"));
}
}
}

View File

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

View File

@@ -0,0 +1,110 @@
using UnityEngine;
using UnityEngine.UIElements;
using OnlyScove.Scripts;
namespace UI
{
public class SettingsController : MonoBehaviour
{
private UIDocument _doc;
private CameraController _cameraController;
// Tabs Content
private VisualElement _contentGeneral, _contentGraphics, _contentAudio;
private Button _tabGeneral, _tabGraphics, _tabAudio, _tabControls;
private void OnEnable()
{
_doc = GetComponent<UIDocument>();
_cameraController = Object.FindFirstObjectByType<CameraController>();
var root = _doc.rootVisualElement;
// Query 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
_contentGeneral = root.Q<VisualElement>("content-general");
_contentGraphics = root.Q<VisualElement>("content-graphics");
_contentAudio = root.Q<VisualElement>("content-audio");
// Events
_tabGeneral.clicked += () => SwitchTab(_contentGeneral, _tabGeneral);
_tabGraphics.clicked += () => SwitchTab(_contentGraphics, _tabGraphics);
_tabAudio.clicked += () => SwitchTab(_contentAudio, _tabAudio);
root.Q<Button>("btn-back").clicked += () => UIManager.Instance.ToggleSettings();
// Camera Binding (FOV)
var fovSlider = root.Q<Slider>("setting-fov");
if (fovSlider != null)
{
fovSlider.RegisterValueChangedCallback(evt => {
// Cần expose hoặc tạo hàm SetFOV trong CameraController
Debug.Log($"Setting FOV to: {evt.newValue}");
});
}
// 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!");
}
}
private void SwitchTab(VisualElement targetContent, Button targetTab)
{
if (targetContent == null || targetTab == null) return;
// Ẩ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;
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
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

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5534bcf4869df944883c6fd2a17a6a5a

View File

@@ -1,92 +1,144 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace UI
{
[ExecuteAlways]
public class UIManager : MonoBehaviour
{
[Serializable]
public static UIManager Instance { get; private set; }
[System.Serializable]
public class ScreenData
{
public string screenName;
public UIDocument document;
public bool isActive;
public bool isActive; // Thêm lại để tương thích với Editor gốc
public bool isOverlay;
public Texture2D customCursor;
}
[Header("Screens Management")]
public List<ScreenData> screens = new List<ScreenData>();
[Header("Live Preview (Editor Only)")]
[Range(0, 1)] public float globalOpacity = 1f;
[Header("Default Settings")]
public Texture2D defaultCursor;
private VisualElement _currentScreenRoot;
private string _currentScreenName;
private void Awake()
{
Instance = this;
SyncScreens(); // Đồng bộ trạng thái ban đầu
foreach (var screen in screens)
{
if (screen.document != null && screen.screenName == "Settings")
screen.document.sortingOrder = 999;
}
}
private void Start()
{
if (!Application.isPlaying) return;
ShowScreen("MainMenu");
}
private void OnValidate()
{
// Tự động cập nhật giao diện ngay khi thay đổi thông số trong Inspector (không cần Play)
SyncScreens();
// Chạy trong Editor để cập nhật UI ngay lập tức khi check vào ô isActive
if (!Application.isPlaying) SyncScreens();
}
private void OnEnable()
private void Update()
{
if (Application.isPlaying)
if (Input.GetKeyDown(KeyCode.Escape))
{
SetupEvents();
ToggleSettings();
}
SyncScreens();
}
public void SyncScreens()
{
foreach (var screen in screens)
{
if (screen.document == null) continue;
var root = screen.document.rootVisualElement;
if (root == null) continue;
// Bật tắt display dựa trên biến isActive
root.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
root.style.opacity = globalOpacity;
if (screen != null && screen.document != null && screen.document.rootVisualElement != null)
{
screen.document.rootVisualElement.style.display = screen.isActive ? DisplayStyle.Flex : DisplayStyle.None;
}
}
}
// Hàm tiện ích để bật duy nhất 1 màn hình từ Code hoặc Button
public void ShowOnly(string name)
{
foreach (var screen in screens)
ShowScreen(name);
}
public void ShowScreen(string name)
{
if (_currentScreenName == name) return;
StartCoroutine(TransitionRoutine(name));
}
private IEnumerator TransitionRoutine(string nextScreenName)
{
// Tắt các màn hình khác
foreach (var s in screens)
{
screen.isActive = (screen.screenName == name);
if (s.screenName != nextScreenName && !s.isOverlay && s.isActive)
{
var root = s.document.rootVisualElement.Q<VisualElement>();
root.AddToClassList("hidden");
s.isActive = false;
}
}
yield return new WaitForSeconds(0.3f);
SyncScreens();
}
private void SetupEvents()
{
// Logic đăng ký event thông minh ở đây (tương tự như trước nhưng linh hoạt hơn)
var mainMenu = GetDocument("MainMenu");
if (mainMenu != null)
// Bật màn hình mới
var nextData = screens.Find(s => s.screenName == nextScreenName);
if (nextData != null && nextData.document != null)
{
mainMenu.rootVisualElement.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(e => ShowOnly("Settings"));
mainMenu.rootVisualElement.Q<Button>("btn-create")?.RegisterCallback<ClickEvent>(e => ShowOnly("Lobby"));
nextData.isActive = true;
_currentScreenRoot = nextData.document.rootVisualElement.Q<VisualElement>();
_currentScreenName = nextScreenName;
_currentScreenRoot.style.display = DisplayStyle.Flex;
_currentScreenRoot.AddToClassList("hidden");
yield return null;
_currentScreenRoot.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;
var settings = GetDocument("Settings");
settings?.rootVisualElement.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(e => ShowOnly("MainMenu"));
if (settingsData.isActive)
{
UnityEngine.Cursor.visible = true;
UnityEngine.Cursor.lockState = CursorLockMode.None;
ApplyCursor(settingsData.customCursor != null ? settingsData.customCursor : defaultCursor);
}
else
{
UnityEngine.Cursor.visible = false;
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
}
}
public UIDocument GetDocument(string name)
private void ApplyCursor(Texture2D texture)
{
return screens.Find(s => s.screenName == name)?.document;
}
// Thêm hàm để Update Text/Image nhanh từ Inspector hoặc Script khác
public void SetElementText(string screenName, string elementName, string newText)
{
var doc = GetDocument(screenName);
var label = doc?.rootVisualElement.Q<Label>(elementName);
if (label != null) label.text = newText;
UnityEngine.Cursor.SetCursor(texture, Vector2.zero, CursorMode.Auto);
}
}
}