This commit is contained in:
2026-04-29 02:31:15 +07:00
parent 21c999a904
commit ed86fface3
12 changed files with 433 additions and 79 deletions

View File

@@ -6,7 +6,7 @@ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine.SceneManagement;
using OnlyScove.Scripts; // Namespace của InputReader
using OnlyScove.Scripts;
namespace Hallucinate.UI
{
@@ -28,16 +28,12 @@ namespace Hallucinate.UI
_tabTitle = root.Q<Label>("TabTitle");
_content = root.Q<ScrollView>("SettingsContent");
// Ưu tiên 1: Lấy từ InputReader đã gán trong UIManager
_inputActions = uiManager.InputReader?.InputActions;
// Ưu tiên 2: Nếu null, thử tìm PlayerInput trong scene
if (_inputActions == null)
{
_inputActions = GameObject.FindAnyObjectByType<PlayerInput>()?.actions;
}
// Click outside to close
root.RegisterCallback<PointerDownEvent>(evt => {
if (evt.target == root)
{
@@ -53,7 +49,6 @@ namespace Hallucinate.UI
var closeBtn = root.Q<Button>("CloseSettingsBtn");
if (closeBtn != null) closeBtn.clicked += () => uiManager.ToggleSettings();
// Default tab
SwitchTab("GENERAL");
}
@@ -72,7 +67,6 @@ namespace Hallucinate.UI
_activeTab = tabId;
_tabTitle.text = tabId;
// Update tab styles
foreach (var kvp in _tabButtons)
{
if (kvp.Key == tabId) kvp.Value.AddToClassList("active-tab");
@@ -99,12 +93,13 @@ namespace Hallucinate.UI
private void RenderGeneralSettings()
{
var section = CreateSection("ACCOUNT & DATA");
// --- ACCOUNT ---
_content.Add(CreateSection(GetLoc("settings_general")));
var wipeBtn = new Button { text = "WIPE ALL USER DATA (TEST ONLY)" };
wipeBtn.AddToClassList("button-spring");
wipeBtn.AddToClassList("btn-exit");
wipeBtn.style.marginTop = 20;
wipeBtn.style.marginTop = 10;
wipeBtn.style.backgroundColor = new Color(0.8f, 0.2f, 0.2f, 0.8f);
wipeBtn.clicked += () => {
@@ -116,17 +111,105 @@ namespace Hallucinate.UI
{
PlayerPrefs.DeleteAll();
PlayerPrefs.Save();
Debug.Log("[Settings] Data wiped. Restarting...");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
};
_content.Add(section);
_content.Add(wipeBtn);
_content.Add(new Label("\nNote: This will force the game to ask for your name again on next launch.") {
style = { fontSize = 12, color = new Color(0.6f, 0.6f, 0.6f), whiteSpace = WhiteSpace.Normal, marginTop = 10 }
// --- LANGUAGE ---
_content.Add(CreateSection(GetLoc("label_language")));
var langContainer = new VisualElement();
langContainer.style.flexDirection = FlexDirection.Row;
langContainer.style.alignItems = Align.Center;
langContainer.style.marginTop = 10;
var langLabel = new Label(GetLoc("label_language"));
langLabel.AddToClassList("text-body");
langLabel.style.width = Length.Percent(40);
var langDropdown = new DropdownField(new List<string> { "English", "Tiếng Việt" }, LocalizationManager.Instance?.CurrentLanguage == "vi" ? 1 : 0);
langDropdown.style.flexGrow = 1;
langDropdown.RegisterValueChangedCallback(evt => {
string code = evt.newValue == "Tiếng Việt" ? "vi" : "en";
LocalizationManager.Instance?.LoadLanguage(code);
// Refresh current tab to update text
SwitchTab("GENERAL");
});
langContainer.Add(langLabel);
langContainer.Add(langDropdown);
_content.Add(langContainer);
// --- INTERFACE ---
_content.Add(CreateSection("INTERFACE"));
float currentScale = PlayerPrefs.GetFloat("UIScale", 1.0f);
var scaleRow = CreateSliderWithInput("UI SCALE", 0.5f, 2.0f, currentScale, (val) => {
uiManager.SetUIScale(val);
});
_content.Add(scaleRow);
_content.Add(new Label("\nNote: Some elements may require restart to align perfectly.") {
style = { fontSize = 12, color = new Color(0.6f, 0.6f, 0.6f), whiteSpace = WhiteSpace.Normal, marginTop = 20 }
});
}
private VisualElement CreateSliderWithInput(string labelText, float min, float max, float startVal, Action<float> OnValueChanged)
{
var row = new VisualElement();
row.style.flexDirection = FlexDirection.Row;
row.style.alignItems = Align.Center;
row.style.marginTop = 10;
row.style.marginBottom = 10;
var label = new Label(labelText);
label.AddToClassList("text-body");
label.style.width = Length.Percent(35);
var slider = new Slider(min, max);
slider.style.flexGrow = 1;
slider.value = startVal;
var input = new TextField();
input.style.width = 60;
input.style.marginLeft = 15;
input.value = startVal.ToString("F1");
input.AddToClassList("input-field");
input.style.marginBottom = 0; // Override default margin
input.style.height = 30;
input.style.fontSize = 14;
// Sync Slider -> Input
slider.RegisterValueChangedCallback(evt => {
float val = Mathf.Round(evt.newValue * 10f) / 10f;
// Kiểm tra xem input có đang được focus không để tránh ghi đè khi người dùng đang nhập
bool isInputFocused = input.panel?.focusController?.focusedElement == input.ElementAt(0);
if (!isInputFocused) input.value = val.ToString("F1");
OnValueChanged?.Invoke(val);
});
// Sync Input -> Slider
input.RegisterValueChangedCallback(evt => {
if (float.TryParse(evt.newValue, out float val))
{
val = Mathf.Clamp(val, min, max);
slider.value = val;
OnValueChanged?.Invoke(val);
}
});
// Format on blur
input.RegisterCallback<BlurEvent>(evt => {
if (float.TryParse(input.value, out float val))
{
input.value = Mathf.Clamp(val, min, max).ToString("F1");
}
});
row.Add(label);
row.Add(slider);
row.Add(input);
return row;
}
private void RenderControlSettings()
@@ -142,7 +225,6 @@ namespace Hallucinate.UI
var playerMap = _inputActions.FindActionMap("Player");
if (playerMap == null) return;
// Categories
RenderSection("MOVEMENT", playerMap, new[] { "Move", "Jump", "Sprint", "Crouch" });
RenderSection("COMBAT", playerMap, new[] { "Attack" });
RenderSection("INTERACTION", playerMap, new[] { "Interact", "Next", "Previous" });
@@ -155,10 +237,7 @@ namespace Hallucinate.UI
foreach (var name in actionNames)
{
var action = map.FindAction(name);
if (action != null)
{
_content.Add(CreateRebindRow(action));
}
if (action != null) _content.Add(CreateRebindRow(action));
}
}
@@ -179,10 +258,7 @@ namespace Hallucinate.UI
var rebindBtn = new Button();
rebindBtn.AddToClassList("rebind-button");
// Get current binding text
UpdateBindingText(action, rebindBtn);
rebindBtn.clicked += () => StartRebind(action, rebindBtn);
row.Add(label);
@@ -201,19 +277,17 @@ namespace Hallucinate.UI
string oldText = btn.text;
btn.text = "...";
btn.style.color = Color.yellow;
action.Disable();
var rebindOperation = action.PerformInteractiveRebinding()
.WithControlsExcluding("<Mouse>/delta") // Don't bind to mouse movement
.WithControlsExcluding("<Mouse>/delta")
.WithControlsExcluding("<Mouse>/scroll")
.OnMatchWaitForAnother(0.1f)
.OnComplete(operation => {
btn.style.color = new Color(0f, 1f, 0.8f); // Reset color
btn.style.color = new Color(0f, 1f, 0.8f);
UpdateBindingText(action, btn);
action.Enable();
operation.Dispose();
// Save bindings here if you have a save system
})
.OnCancel(operation => {
btn.style.color = new Color(0f, 1f, 0.8f);
@@ -243,7 +317,6 @@ namespace Hallucinate.UI
{
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();
}
}