This commit is contained in:
2026-04-28 10:44:22 +07:00
parent 0ac5256b40
commit f925c61277
7 changed files with 180 additions and 62 deletions

View File

@@ -16,9 +16,11 @@ namespace Hallucinate.UI
private float _lastInteractionTime;
private const float IDLE_TIMEOUT = 5.0f;
private bool _isFirstLoad = true; // Biến cờ để nhận biết lần đầu vào game
private Tween _pulseTween;
private Tween _rotationTween;
private Texture2D _currentIcon;
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
{
@@ -34,7 +36,6 @@ namespace Hallucinate.UI
return;
}
ResetLogoPosition();
_logo.RegisterCallback<ClickEvent>(OnLogoClicked);
// Bind Buttons
@@ -44,6 +45,7 @@ namespace Hallucinate.UI
root.Q<Button>("ProfileBtn").clicked += () => uiManager.Push<ProfileController>();
root.Q<Button>("ExitBtn").clicked += () => Application.Quit();
ResetLogoPosition();
StartPulse();
_lastInteractionTime = Time.time;
}
@@ -51,6 +53,7 @@ namespace Hallucinate.UI
private void ResetLogoPosition()
{
if (_logo == null) return;
_logo.style.translate = new StyleTranslate(new Translate(0, 0));
_logo.style.left = (Screen.width / 2f) - 100;
_logo.style.top = (Screen.height / 2f) - 100;
_logo.style.width = 200;
@@ -60,6 +63,7 @@ namespace Hallucinate.UI
public void SetGameIcon(Texture2D icon)
{
if (icon == null || _logo == null) return;
_currentIcon = icon;
_logo.style.backgroundImage = icon;
var radius = new StyleLength(new Length(50, LengthUnit.Percent));
@@ -72,28 +76,45 @@ namespace Hallucinate.UI
var label = _logo.Q<Label>();
if (label != null) label.style.display = DisplayStyle.None;
StartRotation();
}
private void StartRotation()
{
if (_currentIcon == null) return;
if (_rotationTween.isAlive) _rotationTween.Stop();
_rotationTween = Tween.Custom(0f, 360f, duration: 4f, cycles: -1, ease: Ease.Linear,
onValueChange: val => _logo.style.rotate = new StyleRotate(new Rotate(Angle.Degrees(val))));
}
public override async Task PlayTransitionIn()
{
// 1. Đưa về trạng thái Idle và reset vị trí Logo về giữa trước khi hiện
_lastInteractionTime = Time.time;
_currentState = MenuState.Idle;
ResetLogoPosition();
if (_ribbon != null)
{
_ribbon.style.display = DisplayStyle.None;
_ribbon.style.opacity = 0;
}
// 2. Chạy hiệu ứng bay vào từ bên phải của Base class
// Hàm này sẽ gọi Show() và chạy Tween di chuyển toàn bộ root
// Khởi động lại rotation nếu có icon
StartRotation();
UnityEngine.Cursor.visible = true;
await base.PlayTransitionIn();
// 3. Sau khi bay vào xong, tự động kích hoạt Ribbon
TransitionToRibbon();
// Nếu không phải lần đầu load (tức là quay lại bằng nút Back), tự động bung Ribbon
if (!_isFirstLoad)
{
TransitionToRibbon();
}
else
{
_isFirstLoad = false; // Đã xong lần đầu, các lần sau sẽ tự động bung
}
}
public override async Task PlayTransitionOut()
@@ -109,33 +130,34 @@ namespace Hallucinate.UI
else _ = uiManager.Push<LobbyController>();
}
private async void TransitionToRibbon()
private void TransitionToRibbon()
{
// Tránh chạy đè nếu đang ở Ribbon rồi
if (_currentState == MenuState.Ribbon) return;
if (_currentState == MenuState.Ribbon && _ribbon.resolvedStyle.display == DisplayStyle.Flex) return;
_currentState = MenuState.Ribbon;
_lastInteractionTime = Time.time;
// Hiện Ribbon
_ribbon.style.display = DisplayStyle.Flex;
Tween.Custom(0f, 1f, duration: 0.3f, onValueChange: val => _ribbon.style.opacity = val);
// Đợi UI Toolkit cập nhật layout
await Task.Yield();
_logoSpace.RegisterCallback<GeometryChangedEvent>(OnLogoSpaceReady);
}
if (_logoSpace == null) return;
private void OnLogoSpaceReady(GeometryChangedEvent evt)
{
_logoSpace.UnregisterCallback<GeometryChangedEvent>(OnLogoSpaceReady);
Rect targetBounds = _logoSpace.worldBound;
// Di chuyển Logo vào vị trí trong Ribbon
Tween.Custom(_logo.style.left.value.value, targetBounds.x, duration: 0.5f, ease: Ease.OutQuad,
if (targetBounds.width <= 0) return;
Tween.Custom(_logo.resolvedStyle.left, targetBounds.x, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.left = val);
Tween.Custom(_logo.style.top.value.value, targetBounds.y - 35, duration: 0.5f, ease: Ease.OutQuad,
Tween.Custom(_logo.resolvedStyle.top, targetBounds.y - 35, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.top = val);
Tween.Custom(_logo.style.width.value.value, 120f, duration: 0.5f, ease: Ease.OutQuad,
Tween.Custom(_logo.resolvedStyle.width, 120f, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.width = val);
Tween.Custom(_logo.style.height.value.value, 120f, duration: 0.5f, ease: Ease.OutQuad,
Tween.Custom(_logo.resolvedStyle.height, 120f, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.height = val);
_lastInteractionTime = Time.time;
@@ -149,13 +171,13 @@ namespace Hallucinate.UI
float targetX = (Screen.width / 2f) - 100;
float targetY = (Screen.height / 2f) - 100;
Tween.Custom(_logo.style.left.value.value, targetX, duration: 0.5f, ease: Ease.OutQuad,
Tween.Custom(_logo.resolvedStyle.left, targetX, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.left = val);
Tween.Custom(_logo.style.top.value.value, targetY, duration: 0.5f, ease: Ease.OutQuad,
Tween.Custom(_logo.resolvedStyle.top, targetY, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.top = val);
Tween.Custom(_logo.style.width.value.value, 200f, duration: 0.5f, ease: Ease.OutQuad,
Tween.Custom(_logo.resolvedStyle.width, 200f, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.width = val);
Tween.Custom(_logo.style.height.value.value, 200f, duration: 0.5f, ease: Ease.OutQuad,
Tween.Custom(_logo.resolvedStyle.height, 200f, duration: 0.5f, ease: Ease.OutQuad,
onValueChange: val => _logo.style.height = val);
Tween.Custom(1f, 0f, duration: 0.5f, onValueChange: val => _ribbon.style.opacity = val)
@@ -164,6 +186,11 @@ namespace Hallucinate.UI
public void Update()
{
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0 || Input.anyKey)
{
_lastInteractionTime = Time.time;
}
if (_currentState == MenuState.Ribbon && Time.time - _lastInteractionTime > IDLE_TIMEOUT)
{
TransitionToIdle();