Upd
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,72 @@ 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");
|
||||
|
||||
_logoContainer.RegisterCallback<ClickEvent>(OnLogoClicked);
|
||||
|
||||
// Register interactions to reset idle timer
|
||||
root.RegisterCallback<MouseMoveEvent>(evt => ResetIdleTimer());
|
||||
var buttons = root.Query<Button>().ToList();
|
||||
foreach (var btn in buttons)
|
||||
{
|
||||
btn.RegisterCallback<ClickEvent>(evt => ResetIdleTimer());
|
||||
}
|
||||
// Tracking interactions
|
||||
_root.RegisterCallback<MouseMoveEvent>(evt => ResetIdleTimer());
|
||||
var buttons = _root.Query<Button>().ToList();
|
||||
foreach (var btn in buttons) btn.RegisterCallback<ClickEvent>(evt => ResetIdleTimer());
|
||||
|
||||
// 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)
|
||||
// Pulsing logic
|
||||
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));
|
||||
|
||||
// 2. Logic Idle Timeout
|
||||
if (_isActive)
|
||||
// Idle timer logic
|
||||
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();
|
||||
|
||||
if (!_isActive) {
|
||||
if (_currentTransition != null) StopCoroutine(_currentTransition);
|
||||
_currentTransition = StartCoroutine(TransitionToActive());
|
||||
} else {
|
||||
if (_currentTransition == null) NavigateToLobby(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetIdleTimer()
|
||||
{
|
||||
_lastInteractionTime = Time.time;
|
||||
}
|
||||
|
||||
private void NavigateToLobby(bool isCreate)
|
||||
{
|
||||
var lobby = Object.FindFirstObjectByType<LobbyController>();
|
||||
@@ -74,63 +94,79 @@ 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;
|
||||
|
||||
// Ensure logo is back in root
|
||||
if (_logoContainer.parent != _root) _root.Add(_logoContainer);
|
||||
|
||||
_logoContainer.style.position = Position.Absolute;
|
||||
_logoContainer.style.translate = new Translate(0, 0);
|
||||
_logoContainer.pickingMode = PickingMode.Position;
|
||||
_currentTransition = null;
|
||||
}
|
||||
|
||||
private IEnumerator TransitionToActive()
|
||||
{
|
||||
_isActive = true;
|
||||
UIManager.Instance.isMainMenuActive = true;
|
||||
ResetIdleTimer();
|
||||
|
||||
_ribbon.style.display = DisplayStyle.Flex;
|
||||
_ribbon.style.opacity = 0;
|
||||
|
||||
// Wait for layout update to get correct placeholder position
|
||||
yield return null;
|
||||
|
||||
// Animation properties
|
||||
_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%)
|
||||
// Move to placeholder (-20% of screen width)
|
||||
_logoContainer.style.translate = new Translate(Length.Percent(-20f), 0);
|
||||
_ribbon.style.opacity = 1;
|
||||
|
||||
yield return new WaitForSeconds(transitionDuration);
|
||||
|
||||
// --- 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;
|
||||
}
|
||||
|
||||
private IEnumerator TransitionToIdle()
|
||||
{
|
||||
_isActive = false;
|
||||
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
|
||||
|
||||
_logoContainer.style.translate = new Translate(0, 0);
|
||||
_ribbon.style.opacity = 0;
|
||||
|
||||
yield return new WaitForSeconds(transitionDuration);
|
||||
_ribbon.style.display = DisplayStyle.None;
|
||||
_currentTransition = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user