Merge branch 'main' of https://scove-vault.duckdns.org/scove/HALLUCINATION
This commit is contained in:
@@ -7,6 +7,7 @@ namespace UI
|
||||
{
|
||||
public class MainMenuController : MonoBehaviour
|
||||
{
|
||||
private VisualElement _root;
|
||||
private VisualElement _logoContainer;
|
||||
private VisualElement _logo;
|
||||
private VisualElement _ribbon;
|
||||
@@ -20,53 +21,94 @@ namespace UI
|
||||
public float pulseAmount = 0.05f;
|
||||
|
||||
private float _lastInteractionTime;
|
||||
private int _lastClickFrame = -1;
|
||||
private Coroutine _currentTransition;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
var root = GetComponent<UIDocument>().rootVisualElement;
|
||||
_root = GetComponent<UIDocument>().rootVisualElement;
|
||||
|
||||
_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");
|
||||
_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);
|
||||
|
||||
// Register interactions to reset idle timer
|
||||
root.RegisterCallback<MouseMoveEvent>(evt => ResetIdleTimer());
|
||||
var buttons = root.Query<Button>().ToList();
|
||||
_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());
|
||||
_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()
|
||||
{
|
||||
// 1. Logic Pulsing (Luôn chạy)
|
||||
float baseScale = _isActive ? 0.38f : 1.0f;
|
||||
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));
|
||||
|
||||
// 2. Logic Idle Timeout
|
||||
if (_isActive)
|
||||
if (_isActive && _currentTransition == null)
|
||||
{
|
||||
if (Time.time - _lastInteractionTime > idleTimeout)
|
||||
{
|
||||
StartCoroutine(TransitionToIdle());
|
||||
}
|
||||
_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>();
|
||||
@@ -74,63 +116,77 @@ namespace UI
|
||||
UIManager.Instance.ShowScreen("Lobby");
|
||||
}
|
||||
|
||||
private void OnLogoClicked(ClickEvent evt)
|
||||
{
|
||||
ResetIdleTimer();
|
||||
if (!_isActive) {
|
||||
// Chỉ chuyển từ Idle sang Active
|
||||
StartCoroutine(TransitionToActive());
|
||||
} else {
|
||||
// Khi đã trong dải Ribbon, nhấn để vào Create Room
|
||||
NavigateToLobby(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetIdleTimer()
|
||||
{
|
||||
_lastInteractionTime = Time.time;
|
||||
}
|
||||
|
||||
private void ResetToIdleState()
|
||||
{
|
||||
_isActive = false;
|
||||
UIManager.Instance.isMainMenuActive = false;
|
||||
_ribbon.style.display = DisplayStyle.None;
|
||||
_logoContainer.style.translate = new Translate(0, 0);
|
||||
_logoContainer.pickingMode = PickingMode.Position;
|
||||
|
||||
_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) };
|
||||
|
||||
yield return null;
|
||||
|
||||
// Di chuyển logo sang vị trí thứ 2 (-20%)
|
||||
_logoContainer.style.translate = new Translate(Length.Percent(-20f), 0);
|
||||
// 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;
|
||||
|
||||
_logoContainer.style.translate = new Translate(0, 0);
|
||||
_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,12 +22,15 @@ namespace UI
|
||||
public List<ScreenData> screens = new List<ScreenData>();
|
||||
public string initialScreen = "MainMenu";
|
||||
|
||||
[Header("Cursor Settings")]
|
||||
[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;
|
||||
public int trailCount = 15;
|
||||
public float focusRadius = 500f;
|
||||
|
||||
[Header("Editor Preview")]
|
||||
[Range(0f, 1f)]
|
||||
@@ -36,6 +39,8 @@ namespace UI
|
||||
private Stack<string> _navigationStack = new Stack<string>();
|
||||
private string _currentScreenName;
|
||||
private VisualElement _lastHoveredElement;
|
||||
|
||||
public bool isMainMenuActive = false;
|
||||
private bool _isSettingsOpen = false;
|
||||
|
||||
private void Awake()
|
||||
@@ -43,6 +48,9 @@ namespace UI
|
||||
if (Instance == null) Instance = this;
|
||||
else { Destroy(gameObject); return; }
|
||||
|
||||
var myDoc = GetComponent<UIDocument>();
|
||||
if (myDoc != null) myDoc.sortingOrder = 1000;
|
||||
|
||||
SetupCursor();
|
||||
foreach (var s in screens)
|
||||
{
|
||||
@@ -60,8 +68,7 @@ namespace UI
|
||||
var root = doc.rootVisualElement;
|
||||
|
||||
_customCursor = new VisualElement();
|
||||
_customCursor.style.width = 25;
|
||||
_customCursor.style.height = 25;
|
||||
_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;
|
||||
@@ -72,10 +79,18 @@ namespace UI
|
||||
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.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);
|
||||
@@ -86,14 +101,12 @@ namespace UI
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Calculate Virtual Mouse Position
|
||||
Vector2 mousePos = Input.mousePosition;
|
||||
bool isMainMenu = (_currentScreenName == "MainMenu");
|
||||
bool restrictY = (isMainMenu && !_isSettingsOpen);
|
||||
bool restrictY = (isMainMenu && isMainMenuActive && !_isSettingsOpen);
|
||||
float targetY = restrictY ? Screen.height / 2f : mousePos.y;
|
||||
Vector2 uiPos = new Vector2(mousePos.x, Screen.height - targetY);
|
||||
|
||||
// Visibility Logic for Cursor & Trail
|
||||
bool showCursor = !isMainMenu || _isSettingsOpen;
|
||||
DisplayStyle cursorDisplay = showCursor ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
|
||||
@@ -107,20 +120,20 @@ namespace UI
|
||||
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;
|
||||
currentTrail.style.display = cursorDisplay;
|
||||
currentTrail.style.left = uiPos.x - 10;
|
||||
currentTrail.style.top = uiPos.y - 10;
|
||||
currentTrail.style.opacity = 0.6f;
|
||||
|
||||
foreach(var t in _trailPool)
|
||||
{
|
||||
t.style.display = cursorDisplay;
|
||||
t.style.opacity = Mathf.Max(0, t.style.opacity.value - Time.deltaTime * 4f);
|
||||
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;
|
||||
}
|
||||
|
||||
// Handle Focus & Clicks using virtual coordinates
|
||||
HandleVirtualInput(uiPos);
|
||||
|
||||
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.O))
|
||||
@@ -133,12 +146,10 @@ namespace UI
|
||||
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();
|
||||
|
||||
@@ -168,7 +179,7 @@ namespace UI
|
||||
}
|
||||
}
|
||||
|
||||
// --- Editor Support Methods ---
|
||||
// --- Editor Support Methods (Restored) ---
|
||||
|
||||
public void SyncScreens()
|
||||
{
|
||||
@@ -201,7 +212,7 @@ namespace UI
|
||||
|
||||
if (!screen.isOverlay)
|
||||
{
|
||||
foreach(var s in screens) if(!s.isOverlay) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
foreach(var s in screens) if(!s.isOverlay && s.document != null) s.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
_navigationStack.Push(name);
|
||||
_currentScreenName = name;
|
||||
}
|
||||
@@ -219,8 +230,8 @@ namespace UI
|
||||
if (currentData != null) currentData.document.rootVisualElement.style.display = DisplayStyle.None;
|
||||
|
||||
_currentScreenName = _navigationStack.Peek();
|
||||
var prev = screens.Find(s => s.screenName == _currentScreenName);
|
||||
if (prev != null) prev.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
var prevData = screens.Find(s => s.screenName == _currentScreenName);
|
||||
if (prevData != null) prevData.document.rootVisualElement.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
public void ToggleSettings()
|
||||
@@ -229,7 +240,7 @@ namespace UI
|
||||
if (settings == null) return;
|
||||
_isSettingsOpen = settings.document.rootVisualElement.style.display == DisplayStyle.None;
|
||||
settings.document.rootVisualElement.style.display = _isSettingsOpen ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
settings.isActive = _isSettingsOpen;
|
||||
if (_isSettingsOpen) settings.document.sortingOrder = 999;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user