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

@@ -6,20 +6,10 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="f9183c68-daf0-43b8-be4c-fad79983f91b" name="Changes" comment=""> <list default="true" id="f9183c68-daf0-43b8-be4c-fad79983f91b" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Editor/UIManagerEditor.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Editor/UIManagerEditor.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scove/UIScaleTest.unity" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scove/UIScaleTest.unity" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/HUDController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/HUDController.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/LobbyController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/LobbyController.cs" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/LobbyController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/LobbyController.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/MainMenuController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/MainMenuController.cs" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/MainMenuController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/MainMenuController.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/ProfileController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/ProfileController.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/SettingsController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/SettingsController.cs" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/SettingsController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/SettingsController.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/UIManager.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/UIManager.cs" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Assets/Scripts/UI/UIManager.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/UI/UIManager.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/UI/Documents/Lobby.uxml" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/UI/Documents/Lobby.uxml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/UI/Documents/MainGameHUD.uxml" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/UI/Documents/MainGameHUD.uxml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/UI/Documents/MainMenu.uxml" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/UI/Documents/MainMenu.uxml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/UI/Documents/Profile.uxml" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/UI/Documents/Profile.uxml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/UI/Documents/Settings.uxml" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/UI/Documents/Settings.uxml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/UI/MainPanelSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/UI/MainPanelSettings.asset" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/UI/Styles/Global.uss" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/UI/Styles/Global.uss" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Assets/UI/Styles/Global.uss" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/UI/Styles/Global.uss" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@@ -152,7 +142,7 @@
<workItem from="1776940053256" duration="13616000" /> <workItem from="1776940053256" duration="13616000" />
<workItem from="1777113431258" duration="10253000" /> <workItem from="1777113431258" duration="10253000" />
<workItem from="1777150520438" duration="58000" /> <workItem from="1777150520438" duration="58000" />
<workItem from="1777150592854" duration="2288000" /> <workItem from="1777150592854" duration="3918000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@@ -5,44 +5,36 @@ namespace UI
{ {
public class LobbyController : MonoBehaviour public class LobbyController : MonoBehaviour
{ {
private UIDocument _doc; private VisualElement _joinView;
private Button _btnLeave; private VisualElement _createView;
private Button _btnStart;
private ScrollView _playerList;
private void OnEnable() private void OnEnable()
{ {
_doc = GetComponent<UIDocument>(); var root = GetComponent<UIDocument>().rootVisualElement;
var root = _doc.rootVisualElement;
// BINDING: Tìm element theo tên (Name) đã đặt trong UXML _joinView = root.Q<VisualElement>("join-view");
_btnLeave = root.Q<Button>("btn-leave"); _createView = root.Q<VisualElement>("create-view");
_btnStart = root.Q<Button>("btn-start");
_playerList = root.Q<ScrollView>("player-list");
// ĐĂNG KÝ SỰ KIỆN: // Back button
if (_btnLeave != null) root.Q<Button>("btn-back")?.RegisterCallback<ClickEvent>(evt => UIManager.Instance.GoBack());
_btnLeave.clicked += OnLeaveClicked; 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) // Toggle password field in Create View
_btnStart.clicked += OnStartClicked; 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;
});
} }
private void OnLeaveClicked() public void SetMode(bool isCreate)
{ {
UIManager.Instance.GoBack(); if (_joinView == null) return;
} _joinView.style.display = isCreate ? DisplayStyle.None : DisplayStyle.Flex;
_createView.style.display = isCreate ? DisplayStyle.Flex : DisplayStyle.None;
private void OnStartClicked()
{
UIManager.Instance.ShowScreen("Lounge");
}
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

@@ -10,11 +10,9 @@ namespace UI
private VisualElement _logoContainer; private VisualElement _logoContainer;
private VisualElement _logo; private VisualElement _logo;
private VisualElement _ribbon; private VisualElement _ribbon;
private VisualElement _logoPlaceholder;
private bool _isActive = false; private bool _isActive = false;
[Header("Animation Settings")]
public float pulseSpeed = 2f;
public float pulseAmount = 0.1f;
public float transitionDuration = 0.5f; public float transitionDuration = 0.5f;
private void OnEnable() private void OnEnable()
@@ -24,11 +22,11 @@ namespace UI
_logoContainer = root.Q<VisualElement>("beat-logo-container"); _logoContainer = root.Q<VisualElement>("beat-logo-container");
_logo = root.Q<VisualElement>("beat-logo"); _logo = root.Q<VisualElement>("beat-logo");
_ribbon = root.Q<VisualElement>("menu-ribbon"); _ribbon = root.Q<VisualElement>("menu-ribbon");
_logoPlaceholder = root.Q<VisualElement>("logo-placeholder");
// Register logo click
_logoContainer.RegisterCallback<ClickEvent>(OnLogoClicked); _logoContainer.RegisterCallback<ClickEvent>(OnLogoClicked);
// Register button events // Routing
root.Q<Button>("btn-create")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("Lobby")); 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-join")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ShowScreen("Lobby"));
root.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ToggleSettings()); root.Q<Button>("btn-settings")?.RegisterCallback<ClickEvent>(ev => UIManager.Instance.ToggleSettings());
@@ -36,44 +34,37 @@ namespace UI
root.Q<Button>("btn-exit")?.RegisterCallback<ClickEvent>(ev => Application.Quit()); root.Q<Button>("btn-exit")?.RegisterCallback<ClickEvent>(ev => Application.Quit());
} }
private void Update()
{
if (!_isActive)
{
// Pulse Animation
float scale = 1f + Mathf.Sin(Time.time * pulseSpeed) * pulseAmount;
_logo.style.scale = new Scale(new Vector3(scale, scale, 1f));
}
}
private void OnLogoClicked(ClickEvent evt) private void OnLogoClicked(ClickEvent evt)
{ {
if (_isActive) return; if (!_isActive) {
StartCoroutine(TransitionToActive()); StartCoroutine(TransitionToActive());
} else {
UIManager.Instance.ShowScreen("Lobby");
}
} }
private IEnumerator TransitionToActive() private IEnumerator TransitionToActive()
{ {
_isActive = true; _isActive = true;
// 1. Shrink and move logo // 1. Show Ribbon (initially invisible)
_logoContainer.style.transitionProperty = new List<StylePropertyName> { "scale", "translate" };
_logoContainer.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) };
_logoContainer.style.scale = new Scale(new Vector3(0.4f, 0.4f, 1f));
// Translate is tricky in UI Toolkit relative to center, but we can use absolute positioning or a placeholder
// For now, let's just fade in the ribbon and hide the central logo
yield return new WaitForSeconds(transitionDuration * 0.5f);
_ribbon.style.display = DisplayStyle.Flex; _ribbon.style.display = DisplayStyle.Flex;
_ribbon.style.opacity = 0; _ribbon.style.opacity = 0;
// 2. Animate Logo to Ribbon
_logoContainer.style.transitionProperty = new List<StylePropertyName> { "scale", "translate", "opacity" };
_logoContainer.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) };
yield return null; // Wait for layout update
// Tính toán khoảng cách di chuyển (Nếu cần thiết, ở đây dùng scale đơn giản)
_logoContainer.style.scale = new Scale(new Vector3(0.4f, 0.4f, 1f));
_ribbon.style.transitionProperty = new List<StylePropertyName> { "opacity" }; _ribbon.style.transitionProperty = new List<StylePropertyName> { "opacity" };
_ribbon.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) }; _ribbon.style.transitionDuration = new List<TimeValue> { new TimeValue(transitionDuration, TimeUnit.Second) };
yield return null;
_ribbon.style.opacity = 1; _ribbon.style.opacity = 1;
_logoContainer.style.display = DisplayStyle.None;
yield return new WaitForSeconds(transitionDuration);
} }
} }
} }

View File

@@ -1,113 +1,63 @@
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
using OnlyScove.Scripts; using System.Collections.Generic;
namespace UI namespace UI
{ {
public class SettingsController : MonoBehaviour public class SettingsController : MonoBehaviour
{ {
private UIDocument _doc; private VisualElement _contentGeneral;
private CameraController _cameraController; private VisualElement _contentGraphics;
private VisualElement _contentAudio;
private VisualElement _contentControls;
// Tabs Content private Button _tabGeneral;
private VisualElement _contentGeneral, _contentGraphics, _contentAudio; private Button _tabGraphics;
private Button _tabGeneral, _tabGraphics, _tabAudio, _tabControls; private Button _tabAudio;
private Button _tabControls;
private void OnEnable() private void OnEnable()
{ {
_doc = GetComponent<UIDocument>(); var root = GetComponent<UIDocument>().rootVisualElement;
_cameraController = Object.FindFirstObjectByType<CameraController>();
var root = _doc.rootVisualElement; // Tabs
// Query Tabs
_tabGeneral = root.Q<Button>("tab-general"); _tabGeneral = root.Q<Button>("tab-general");
_tabGraphics = root.Q<Button>("tab-graphics"); _tabGraphics = root.Q<Button>("tab-graphics");
_tabAudio = root.Q<Button>("tab-audio"); _tabAudio = root.Q<Button>("tab-audio");
_tabControls = root.Q<Button>("tab-controls"); _tabControls = root.Q<Button>("tab-controls");
// Query Content // Content
_contentGeneral = root.Q<VisualElement>("content-general"); _contentGeneral = root.Q<VisualElement>("content-general");
_contentGraphics = root.Q<VisualElement>("content-graphics"); _contentGraphics = root.Q<VisualElement>("content-graphics");
_contentAudio = root.Q<VisualElement>("content-audio"); _contentAudio = root.Q<VisualElement>("content-audio");
_contentControls = root.Q<VisualElement>("content-controls");
// Events // Register Tab Events
_tabGeneral.clicked += () => SwitchTab(_contentGeneral, _tabGeneral); _tabGeneral?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentGeneral, _tabGeneral));
_tabGraphics.clicked += () => SwitchTab(_contentGraphics, _tabGraphics); _tabGraphics?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentGraphics, _tabGraphics));
_tabAudio.clicked += () => SwitchTab(_contentAudio, _tabAudio); _tabAudio?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentAudio, _tabAudio));
_tabControls?.RegisterCallback<ClickEvent>(evt => SwitchTab(_contentControls, _tabControls));
root.Q<Button>("btn-close").clicked += () => UIManager.Instance.ToggleSettings(); // Close
root.Q<Button>("btn-close")?.RegisterCallback<ClickEvent>(evt => 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!");
}
} }
private void SwitchTab(VisualElement targetContent, Button targetTab) 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) _tabGeneral.RemoveFromClassList("active-tab");
if (_contentGeneral != null) _contentGeneral.style.display = DisplayStyle.None; _tabGraphics.RemoveFromClassList("active-tab");
if (_contentGraphics != null) _contentGraphics.style.display = DisplayStyle.None; _tabAudio.RemoveFromClassList("active-tab");
if (_contentAudio != null) _contentAudio.style.display = DisplayStyle.None; _tabControls.RemoveFromClassList("active-tab");
if (_tabGeneral != null) _tabGeneral.RemoveFromClassList("active-tab"); // Show target
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; targetContent.style.display = DisplayStyle.Flex;
targetTab.AddToClassList("active-tab"); 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

@@ -16,49 +16,142 @@ namespace UI
public string screenName; public string screenName;
public UIDocument document; public UIDocument document;
public bool isOverlay; public bool isOverlay;
public Texture2D customCursor; public bool isActive;
public bool isActive; // For Editor and Initial state
} }
public List<ScreenData> screens = new List<ScreenData>(); public List<ScreenData> screens = new List<ScreenData>();
[Header("Settings")]
public string initialScreen = "MainMenu"; public string initialScreen = "MainMenu";
public float focusRadius = 300f;
[Header("Cursor Settings")]
private VisualElement _customCursor;
private List<VisualElement> _trailPool = new List<VisualElement>();
private int _trailIndex = 0;
public int trailCount = 15;
public float focusRadius = 500f;
[Header("Editor Preview")]
[Range(0f, 1f)] [Range(0f, 1f)]
public float globalOpacity = 1f; public float globalOpacity = 1f;
private Stack<string> _navigationStack = new Stack<string>(); private Stack<string> _navigationStack = new Stack<string>();
private string _currentScreenName; private string _currentScreenName;
private VisualElement _lastHoveredElement; private VisualElement _lastHoveredElement;
private bool _isSettingsOpen = false;
private void Awake() private void Awake()
{ {
if (Instance == null) Instance = this; if (Instance == null) Instance = this;
else { Destroy(gameObject); return; } else { Destroy(gameObject); return; }
// Initialize all screens based on isActive or hidden SetupCursor();
foreach (var screen in screens) foreach (var s in screens)
{ {
if (screen.document != null) if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
{
// Ensure the root has the screen-root class for transitions
var root = screen.document.rootVisualElement;
if (root != null)
{
root.AddToClassList("screen-root");
root.style.display = DisplayStyle.None;
}
}
} }
ShowScreen(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 = 18; trail.style.height = 18;
trail.style.backgroundColor = new Color(1, 1, 1, 0.4f);
trail.style.borderTopLeftRadius = 9; trail.style.borderTopRightRadius = 9;
trail.style.borderBottomLeftRadius = 9; trail.style.borderBottomRightRadius = 9;
trail.style.position = Position.Absolute;
trail.pickingMode = PickingMode.Ignore;
root.Add(trail);
_trailPool.Add(trail);
}
_customCursor.BringToFront();
}
private void Update() private void Update()
{ {
HandleGlobalInputs(); Vector2 mousePos = Input.mousePosition;
HandleCursorlessFocus(); bool restrictY = (_currentScreenName == "MainMenu" && !_isSettingsOpen);
float targetY = restrictY ? Screen.height / 2f : mousePos.y;
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - targetY);
if (_customCursor != null)
{
_customCursor.style.left = uiPos.x - 12.5f;
_customCursor.style.top = uiPos.y - 12.5f;
}
if (_trailPool.Count > 0)
{
var currentTrail = _trailPool[_trailIndex];
currentTrail.style.left = uiPos.x - 9;
currentTrail.style.top = uiPos.y - 9;
currentTrail.style.opacity = 0.5f;
foreach(var t in _trailPool) t.style.opacity = Mathf.Max(0, t.style.opacity.value - Time.deltaTime * 4f);
_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 --- // --- Editor Support Methods ---
@@ -87,132 +180,42 @@ namespace UI
// --- Runtime Logic --- // --- Runtime Logic ---
private void HandleGlobalInputs()
{
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
{
ToggleSettings();
}
if (Input.GetKeyDown(KeyCode.Escape))
{
if (_navigationStack.Count > 1)
{
GoBack();
}
}
}
private void HandleCursorlessFocus()
{
if (string.IsNullOrEmpty(_currentScreenName)) return;
var screen = screens.Find(s => s.screenName == _currentScreenName);
if (screen == null || screen.document == null) return;
Vector2 mousePos = Input.mousePosition;
Vector2 uiMousePos = new Vector2(mousePos.x, Screen.height - mousePos.y);
VisualElement bestElement = null;
float minDistance = float.MaxValue;
var interactiveElements = screen.document.rootVisualElement.Query<VisualElement>()
.Where(e => e.focusable && e.pickingMode == PickingMode.Position).ToList();
foreach (var element in interactiveElements)
{
Rect worldBounds = element.worldBound;
Vector2 center = worldBounds.center;
float dist = Vector2.Distance(uiMousePos, 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);
}
}
}
public void ShowScreen(string name) public void ShowScreen(string name)
{ {
var nextData = screens.Find(s => s.screenName == name); var screen = screens.Find(s => s.screenName == name);
if (nextData == null) return; if (screen == null) return;
// Hide all first for a clean state at runtime if (!screen.isOverlay)
foreach (var s in screens)
{ {
if (s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None; foreach(var s in screens) if(!s.isOverlay) s.document.rootVisualElement.style.display = DisplayStyle.None;
s.isActive = false; _navigationStack.Push(name);
_currentScreenName = name;
} }
_navigationStack.Push(name); screen.document.rootVisualElement.style.display = DisplayStyle.Flex;
_currentScreenName = name; screen.isActive = true;
nextData.isActive = true; UnityEngine.Cursor.visible = false;
nextData.document.rootVisualElement.style.display = DisplayStyle.Flex;
nextData.document.rootVisualElement.style.opacity = globalOpacity;
ApplyCursorSettings(nextData);
} }
public void GoBack() public void GoBack()
{ {
if (_navigationStack.Count <= 1) return; if (_navigationStack.Count <= 1) return;
string current = _navigationStack.Pop(); string current = _navigationStack.Pop();
var currentData = screens.Find(s => s.screenName == current); var currentData = screens.Find(s => s.screenName == current);
if (currentData != null) currentData.document.rootVisualElement.style.display = DisplayStyle.None; if (currentData != null) currentData.document.rootVisualElement.style.display = DisplayStyle.None;
_currentScreenName = _navigationStack.Peek(); _currentScreenName = _navigationStack.Peek();
var prevData = screens.Find(s => s.screenName == _currentScreenName); var prev = screens.Find(s => s.screenName == _currentScreenName);
if (prevData != null) if (prev != null) prev.document.rootVisualElement.style.display = DisplayStyle.Flex;
{
prevData.document.rootVisualElement.style.display = DisplayStyle.Flex;
ApplyCursorSettings(prevData);
}
} }
public void ToggleSettings() public void ToggleSettings()
{ {
var settings = screens.Find(s => s.screenName == "Settings"); var settings = screens.Find(s => s.screenName == "Settings");
if (settings == null) return; if (settings == null) return;
_isSettingsOpen = settings.document.rootVisualElement.style.display == DisplayStyle.None;
bool isShowing = settings.document.rootVisualElement.style.display == DisplayStyle.Flex; settings.document.rootVisualElement.style.display = _isSettingsOpen ? DisplayStyle.Flex : DisplayStyle.None;
settings.document.rootVisualElement.style.display = isShowing ? DisplayStyle.None : DisplayStyle.Flex; settings.isActive = _isSettingsOpen;
if (!isShowing)
{
settings.document.sortingOrder = 999;
}
}
private void ApplyCursorSettings(ScreenData data)
{
if (data.screenName == "HUD")
{
UnityEngine.Cursor.visible = false;
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
}
else
{
UnityEngine.Cursor.visible = false;
UnityEngine.Cursor.lockState = CursorLockMode.None;
}
} }
} }
} }

View File

@@ -110,9 +110,15 @@
/* Cursorless Focus System */ /* Cursorless Focus System */
.slanted-button.hover { .slanted-button.hover {
background-color: var(--primary-hover); background-color: var(--primary-hover);
scale: 1.1; scale: 1.15;
rotate: -2deg;
border-width: 2px; border-width: 2px;
border-color: white; border-color: white;
transition-duration: 0.15s;
}
.slanted-button.hover .slanted-button-inner {
color: #FFD700; /* Golden text on hover */
} }
/* HUD Components */ /* HUD Components */