This commit is contained in:
2026-04-26 05:02:49 +07:00
parent 5b22d31259
commit a6891ab5b8
6 changed files with 207 additions and 275 deletions

View File

@@ -1,113 +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-close").clicked += () => UIManager.Instance.ToggleSettings();
// If we want a hard back button to MainMenu:
// root.Q<Button>("btn-back").clicked += () => UIManager.Instance.GoBack();
// Camera Binding (FOV)
var fovSlider = root.Q<Slider>("setting-fov");
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!");
}
// 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");
}
}
}