2026-04-25 18:20:16 +07:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UIElements;
|
2026-04-26 04:39:59 +07:00
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2026-04-25 18:20:16 +07:00
|
|
|
|
|
|
|
|
namespace UI
|
|
|
|
|
{
|
|
|
|
|
public class MainMenuController : MonoBehaviour
|
|
|
|
|
{
|
2026-04-27 13:27:40 +07:00
|
|
|
private VisualElement _root;
|
2026-04-26 04:39:59 +07:00
|
|
|
private VisualElement _logoContainer;
|
|
|
|
|
private VisualElement _logo;
|
|
|
|
|
private VisualElement _ribbon;
|
2026-04-26 05:02:49 +07:00
|
|
|
private VisualElement _logoPlaceholder;
|
2026-04-26 04:39:59 +07:00
|
|
|
private bool _isActive = false;
|
|
|
|
|
|
2026-04-26 05:20:47 +07:00
|
|
|
[Header("Animation Settings")]
|
2026-04-26 04:39:59 +07:00
|
|
|
public float transitionDuration = 0.5f;
|
2026-04-26 05:20:47 +07:00
|
|
|
public float idleTimeout = 5f;
|
|
|
|
|
public float pulseSpeed = 2f;
|
|
|
|
|
public float pulseAmount = 0.05f;
|
|
|
|
|
|
|
|
|
|
private float _lastInteractionTime;
|
2026-04-27 13:27:40 +07:00
|
|
|
private int _lastClickFrame = -1;
|
|
|
|
|
private Coroutine _currentTransition;
|
2026-04-25 18:20:16 +07:00
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
2026-04-27 13:27:40 +07:00
|
|
|
_root = GetComponent<UIDocument>().rootVisualElement;
|
2026-04-26 04:39:59 +07:00
|
|
|
|
2026-04-27 13:27:40 +07:00
|
|
|
_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");
|
2026-04-26 04:39:59 +07:00
|
|
|
|
|
|
|
|
_logoContainer.RegisterCallback<ClickEvent>(OnLogoClicked);
|
2026-04-25 18:20:16 +07:00
|
|
|
|
2026-04-27 13:27:40 +07:00
|
|
|
// Tracking interactions
|
|
|
|
|
_root.RegisterCallback<MouseMoveEvent>(evt => ResetIdleTimer());
|
|
|
|
|
var buttons = _root.Query<Button>().ToList();
|
|
|
|
|
foreach (var btn in buttons) btn.RegisterCallback<ClickEvent>(evt => ResetIdleTimer());
|
2026-04-26 05:20:47 +07:00
|
|
|
|
2026-04-26 05:02:49 +07:00
|
|
|
// Routing
|
2026-04-27 13:27:40 +07:00
|
|
|
_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());
|
2026-04-26 05:20:47 +07:00
|
|
|
|
|
|
|
|
ResetToIdleState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
2026-04-27 13:27:40 +07:00
|
|
|
// Pulsing logic
|
2026-04-26 05:20:47 +07:00
|
|
|
float baseScale = _isActive ? 0.38f : 1.0f;
|
|
|
|
|
float pulse = Mathf.Sin(Time.time * pulseSpeed) * pulseAmount;
|
|
|
|
|
_logo.style.scale = new Scale(new Vector3(baseScale + pulse, baseScale + pulse, 1f));
|
|
|
|
|
|
2026-04-27 13:27:40 +07:00
|
|
|
// Idle timer logic
|
|
|
|
|
if (_isActive && _currentTransition == null)
|
2026-04-26 05:20:47 +07:00
|
|
|
{
|
|
|
|
|
if (Time.time - _lastInteractionTime > idleTimeout)
|
|
|
|
|
{
|
2026-04-27 13:27:40 +07:00
|
|
|
_currentTransition = StartCoroutine(TransitionToIdle());
|
2026-04-26 05:20:47 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 04:39:59 +07:00
|
|
|
private void OnLogoClicked(ClickEvent evt)
|
|
|
|
|
{
|
2026-04-27 13:27:40 +07:00
|
|
|
if (Time.frameCount == _lastClickFrame) return;
|
|
|
|
|
_lastClickFrame = Time.frameCount;
|
|
|
|
|
|
2026-04-26 05:20:47 +07:00
|
|
|
ResetIdleTimer();
|
2026-04-27 13:27:40 +07:00
|
|
|
|
2026-04-26 05:02:49 +07:00
|
|
|
if (!_isActive) {
|
2026-04-27 13:27:40 +07:00
|
|
|
if (_currentTransition != null) StopCoroutine(_currentTransition);
|
|
|
|
|
_currentTransition = StartCoroutine(TransitionToActive());
|
2026-04-26 05:02:49 +07:00
|
|
|
} else {
|
2026-04-27 13:27:40 +07:00
|
|
|
if (_currentTransition == null) NavigateToLobby(true);
|
2026-04-26 05:02:49 +07:00
|
|
|
}
|
2026-04-26 04:39:59 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-26 05:20:47 +07:00
|
|
|
private void ResetIdleTimer()
|
|
|
|
|
{
|
|
|
|
|
_lastInteractionTime = Time.time;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 13:27:40 +07:00
|
|
|
private void NavigateToLobby(bool isCreate)
|
|
|
|
|
{
|
|
|
|
|
var lobby = Object.FindFirstObjectByType<LobbyController>();
|
|
|
|
|
lobby?.SetMode(isCreate);
|
|
|
|
|
UIManager.Instance.ShowScreen("Lobby");
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 05:20:47 +07:00
|
|
|
private void ResetToIdleState()
|
|
|
|
|
{
|
|
|
|
|
_isActive = false;
|
2026-04-27 13:27:40 +07:00
|
|
|
UIManager.Instance.isMainMenuActive = false;
|
2026-04-26 05:20:47 +07:00
|
|
|
_ribbon.style.display = DisplayStyle.None;
|
2026-04-27 13:27:40 +07:00
|
|
|
|
|
|
|
|
// Ensure logo is back in root
|
|
|
|
|
if (_logoContainer.parent != _root) _root.Add(_logoContainer);
|
|
|
|
|
|
|
|
|
|
_logoContainer.style.position = Position.Absolute;
|
2026-04-26 05:20:47 +07:00
|
|
|
_logoContainer.style.translate = new Translate(0, 0);
|
|
|
|
|
_logoContainer.pickingMode = PickingMode.Position;
|
2026-04-27 13:27:40 +07:00
|
|
|
_currentTransition = null;
|
2026-04-26 05:20:47 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-26 04:39:59 +07:00
|
|
|
private IEnumerator TransitionToActive()
|
|
|
|
|
{
|
|
|
|
|
_isActive = true;
|
2026-04-27 13:27:40 +07:00
|
|
|
UIManager.Instance.isMainMenuActive = true;
|
2026-04-26 05:20:47 +07:00
|
|
|
ResetIdleTimer();
|
|
|
|
|
|
2026-04-26 05:02:49 +07:00
|
|
|
_ribbon.style.display = DisplayStyle.Flex;
|
|
|
|
|
_ribbon.style.opacity = 0;
|
|
|
|
|
|
2026-04-27 13:27:40 +07:00
|
|
|
// Wait for layout update to get correct placeholder position
|
|
|
|
|
yield return null;
|
|
|
|
|
|
|
|
|
|
// Animation properties
|
2026-04-26 05:20:47 +07:00
|
|
|
_logoContainer.style.transitionProperty = new List<StylePropertyName> { "translate", "opacity" };
|
2026-04-26 04:39:59 +07:00
|
|
|
_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) };
|
2026-04-26 05:20:47 +07:00
|
|
|
|
2026-04-27 13:27:40 +07:00
|
|
|
// Move to placeholder (-20% of screen width)
|
2026-04-26 05:20:47 +07:00
|
|
|
_logoContainer.style.translate = new Translate(Length.Percent(-20f), 0);
|
2026-04-26 04:39:59 +07:00
|
|
|
_ribbon.style.opacity = 1;
|
2026-04-26 05:02:49 +07:00
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(transitionDuration);
|
2026-04-27 13:27:40 +07:00
|
|
|
|
|
|
|
|
// --- REPARENTING LOGIC ---
|
|
|
|
|
// Move logo container INSIDE the placeholder for perfect positioning
|
|
|
|
|
_logoPlaceholder.Add(_logoContainer);
|
|
|
|
|
|
|
|
|
|
// Reset translation because it's now relative to the placeholder
|
|
|
|
|
_logoContainer.style.translate = new Translate(0, 0);
|
|
|
|
|
_logoContainer.style.position = Position.Relative; // Follow flex layout
|
|
|
|
|
_logoContainer.style.alignSelf = Align.Center;
|
|
|
|
|
|
|
|
|
|
_currentTransition = null;
|
2026-04-26 04:39:59 +07:00
|
|
|
}
|
2026-04-26 05:20:47 +07:00
|
|
|
|
|
|
|
|
private IEnumerator TransitionToIdle()
|
|
|
|
|
{
|
|
|
|
|
_isActive = false;
|
2026-04-27 13:27:40 +07:00
|
|
|
UIManager.Instance.isMainMenuActive = false;
|
|
|
|
|
|
|
|
|
|
// --- UN-PARENTING LOGIC ---
|
|
|
|
|
// Move back to root for animation
|
|
|
|
|
Vector3 worldPos = _logoContainer.worldBound.center;
|
|
|
|
|
_root.Add(_logoContainer);
|
|
|
|
|
_logoContainer.style.position = Position.Absolute;
|
|
|
|
|
|
|
|
|
|
// Keep it at current position visually before animating back
|
|
|
|
|
_logoContainer.style.translate = new Translate(Length.Percent(-20f), 0);
|
|
|
|
|
|
|
|
|
|
yield return null; // wait for frame
|
2026-04-26 05:20:47 +07:00
|
|
|
|
|
|
|
|
_logoContainer.style.translate = new Translate(0, 0);
|
|
|
|
|
_ribbon.style.opacity = 0;
|
|
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(transitionDuration);
|
|
|
|
|
_ribbon.style.display = DisplayStyle.None;
|
2026-04-27 13:27:40 +07:00
|
|
|
_currentTransition = null;
|
2026-04-26 05:20:47 +07:00
|
|
|
}
|
2026-04-25 18:20:16 +07:00
|
|
|
}
|
|
|
|
|
}
|