Organize custom scripts under Assets/Baba_yaga and merge Opsive folders to Assets root
This commit is contained in:
309
Assets/Baba_yaga/UI/MainMenuController.cs
Normal file
309
Assets/Baba_yaga/UI/MainMenuController.cs
Normal file
@@ -0,0 +1,309 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using PrimeTween;
|
||||
using System.Threading.Tasks;
|
||||
using DG.Tweening;
|
||||
using Tween = PrimeTween.Tween;
|
||||
using Ease = PrimeTween.Ease;
|
||||
|
||||
namespace Baba_yaga.UI
|
||||
{
|
||||
[UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "Hallucinate.UI", sourceAssembly: "Opsive.UltimateCharacterController")]
|
||||
public class MainMenuController : BaseUIController
|
||||
{
|
||||
[UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "Hallucinate.UI", sourceAssembly: "Opsive.UltimateCharacterController")]
|
||||
public enum MenuState { Idle, Ribbon }
|
||||
private MenuState _currentState = MenuState.Idle;
|
||||
|
||||
private VisualElement _logo;
|
||||
private VisualElement _ribbon;
|
||||
private VisualElement _logoSpace;
|
||||
|
||||
private float _lastInteractionTime;
|
||||
private const float IDLE_TIMEOUT = 5.0f;
|
||||
private bool _isFirstLoad = true;
|
||||
|
||||
private Tween _pulseTween;
|
||||
private Tween _rotationTween;
|
||||
private Texture2D _currentIcon;
|
||||
|
||||
private bool _isBusy = false;
|
||||
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
|
||||
_logo = root.Q<VisualElement>("Logo");
|
||||
_ribbon = root.Q<VisualElement>("Ribbon");
|
||||
_logoSpace = root.Q<VisualElement>("LogoSpace");
|
||||
|
||||
if (_logo == null)
|
||||
{
|
||||
Debug.LogError($"[MainMenuController] Element 'Logo' not found in UXML!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Lắng nghe sự kiện thay đổi kích thước toàn màn hình
|
||||
root.RegisterCallback<GeometryChangedEvent>(OnScreenResize);
|
||||
|
||||
/*_logo.RegisterCallback<ClickEvent>(OnLogoClicked);*/
|
||||
|
||||
var settingsBtn = root.Q<Button>("SettingsBtn");
|
||||
/*if (settingsBtn != null) settingsBtn.clicked += () => { if (_isBusy) return; uiManager.ToggleSettings(); };
|
||||
|
||||
root.Q<Button>("JoinBtn").clicked += async () => { if (_isBusy) return; _isBusy = true; await uiManager.Push<LobbyController>(); };
|
||||
root.Q<Button>("CreateBtn").clicked += async () => { if (_isBusy) return; _isBusy = true; await uiManager.Push<LobbyController>(); };
|
||||
root.Q<Button>("ProfileBtn").clicked += async () => { if (_isBusy) return; _isBusy = true; await uiManager.Push<ProfileController>(); };
|
||||
root.Q<Button>("ExitBtn").clicked += () => Application.Quit();*/
|
||||
|
||||
// Đăng ký Localization
|
||||
if (LocalizationManager.Instance != null)
|
||||
{
|
||||
LocalizationManager.Instance.OnLanguageChanged += ApplyLocalization;
|
||||
}
|
||||
ApplyLocalization();
|
||||
|
||||
ResetLogoPosition();
|
||||
StartPulse();
|
||||
_lastInteractionTime = Time.time;
|
||||
}
|
||||
|
||||
private void ApplyLocalization()
|
||||
{
|
||||
if (LocalizationManager.Instance == null) return;
|
||||
|
||||
var joinBtn = root.Q<Button>("JoinBtn");
|
||||
if (joinBtn != null) joinBtn.text = LocalizationManager.Instance.GetLocalizedString("MENU_JOIN");
|
||||
|
||||
var createBtn = root.Q<Button>("CreateBtn");
|
||||
if (createBtn != null) createBtn.text = LocalizationManager.Instance.GetLocalizedString("MENU_CREATE");
|
||||
|
||||
var settingsBtn = root.Q<Button>("SettingsBtn");
|
||||
if (settingsBtn != null) settingsBtn.text = LocalizationManager.Instance.GetLocalizedString("MENU_SETTINGS");
|
||||
|
||||
var profileBtn = root.Q<Button>("ProfileBtn");
|
||||
if (profileBtn != null) profileBtn.text = LocalizationManager.Instance.GetLocalizedString("MENU_PROFILE");
|
||||
|
||||
var exitBtn = root.Q<Button>("ExitBtn");
|
||||
if (exitBtn != null) exitBtn.text = LocalizationManager.Instance.GetLocalizedString("MENU_EXIT");
|
||||
|
||||
var playLabel = _logo.Q<Label>();
|
||||
if (playLabel != null) playLabel.text = LocalizationManager.Instance.GetLocalizedString("MENU_PLAY");
|
||||
}
|
||||
|
||||
private void OnScreenResize(GeometryChangedEvent evt)
|
||||
{
|
||||
// Khi màn hình thay đổi kích thước, nếu đang ở Ribbon thì cập nhật theo LogoSpace
|
||||
if (_currentState == MenuState.Ribbon)
|
||||
{
|
||||
UpdateLogoToSpace();
|
||||
}
|
||||
else
|
||||
{
|
||||
ResetLogoPosition();
|
||||
}
|
||||
}
|
||||
|
||||
private void ResetLogoPosition()
|
||||
{
|
||||
if (_logo == null) return;
|
||||
|
||||
// Sử dụng phần trăm để luôn ở giữa bất kể độ phân giải
|
||||
_logo.style.left = Length.Percent(50);
|
||||
_logo.style.top = Length.Percent(50);
|
||||
_logo.style.translate = new StyleTranslate(new Translate(Length.Percent(-50), Length.Percent(-50)));
|
||||
|
||||
_logo.style.width = 200;
|
||||
_logo.style.height = 200;
|
||||
}
|
||||
|
||||
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));
|
||||
_logo.style.borderTopLeftRadius = radius;
|
||||
_logo.style.borderTopRightRadius = radius;
|
||||
_logo.style.borderBottomLeftRadius = radius;
|
||||
_logo.style.borderBottomRightRadius = radius;
|
||||
_logo.style.overflow = Overflow.Hidden;
|
||||
|
||||
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,
|
||||
onValueChange: val => _logo.style.rotate = new StyleRotate(new Rotate(Angle.Degrees(val))),
|
||||
cycles: -1,
|
||||
ease: Ease.Linear);*/
|
||||
}
|
||||
|
||||
/*public override async Task PlayTransitionIn()
|
||||
{
|
||||
_isBusy = false;
|
||||
_lastInteractionTime = Time.time;
|
||||
_currentState = MenuState.Idle;
|
||||
ResetLogoPosition();
|
||||
|
||||
if (_ribbon != null)
|
||||
{
|
||||
_ribbon.style.display = DisplayStyle.None;
|
||||
_ribbon.style.opacity = 0;
|
||||
}
|
||||
|
||||
StartRotation();
|
||||
await base.PlayTransitionIn();
|
||||
|
||||
if (!_isFirstLoad) TransitionToRibbon();
|
||||
else _isFirstLoad = false;
|
||||
}
|
||||
|
||||
public override async Task PlayTransitionOut()
|
||||
{
|
||||
/*if (_rotationTween.isAlive) _rotationTween.Stop();
|
||||
await base.PlayTransitionOut();#1#
|
||||
}*/
|
||||
|
||||
/*private async void OnLogoClicked(ClickEvent evt)
|
||||
{
|
||||
if (_isBusy) return;
|
||||
_lastInteractionTime = Time.time;
|
||||
if (_currentState == MenuState.Idle) TransitionToRibbon();
|
||||
else
|
||||
{
|
||||
_isBusy = true;
|
||||
await uiManager.Push<LobbyController>();
|
||||
}
|
||||
}*/
|
||||
|
||||
private void TransitionToRibbon()
|
||||
{
|
||||
if (_currentState == MenuState.Ribbon && _ribbon.resolvedStyle.display == DisplayStyle.Flex) return;
|
||||
_currentState = MenuState.Ribbon;
|
||||
_lastInteractionTime = Time.time;
|
||||
|
||||
_ribbon.style.display = DisplayStyle.Flex;
|
||||
/*
|
||||
Tween.Custom(0f, 1f, duration: 0.3f, onValueChange: val => _ribbon.style.opacity = val);
|
||||
*/
|
||||
|
||||
// Đợi một frame để Ribbon layout xong rồi mới lấy vị trí LogoSpace
|
||||
_logoSpace.RegisterCallback<GeometryChangedEvent>(OnLogoSpaceReady);
|
||||
}
|
||||
|
||||
private void OnLogoSpaceReady(GeometryChangedEvent evt)
|
||||
{
|
||||
_logoSpace.UnregisterCallback<GeometryChangedEvent>(OnLogoSpaceReady);
|
||||
UpdateLogoToSpace(true);
|
||||
}
|
||||
|
||||
private void UpdateLogoToSpace(bool animate = false)
|
||||
{
|
||||
Rect targetBounds = _logoSpace.worldBound;
|
||||
if (targetBounds.width <= 0) return;
|
||||
|
||||
// Chuyển đổi tọa độ world của LogoSpace sang tọa độ local của root
|
||||
Vector2 localPos = root.WorldToLocal(new Vector2(targetBounds.x, targetBounds.y));
|
||||
|
||||
float targetX = localPos.x + (targetBounds.width / 2f);
|
||||
float targetY = localPos.y + (targetBounds.height / 2f);
|
||||
|
||||
// Khi ở Ribbon, chúng ta bỏ translate -50% để tính toán chính xác tâm
|
||||
_logo.style.translate = new StyleTranslate(new Translate(Length.Percent(-50), Length.Percent(-50)));
|
||||
|
||||
if (animate)
|
||||
{
|
||||
/*Tween.Custom(_logo.resolvedStyle.left, targetX, duration: 0.5f,
|
||||
onValueChange: val => _logo.style.left = val,
|
||||
ease: Ease.OutQuad);
|
||||
|
||||
Tween.Custom(_logo.resolvedStyle.top, targetY, duration: 0.5f,
|
||||
onValueChange: val => _logo.style.top = val,
|
||||
ease: Ease.OutQuad);
|
||||
|
||||
Tween.Custom(_logo.resolvedStyle.width, 100f, duration: 0.5f,
|
||||
onValueChange: val => _logo.style.width = val,
|
||||
ease: Ease.OutQuad);
|
||||
|
||||
Tween.Custom(_logo.resolvedStyle.height, 100f, duration: 0.5f,
|
||||
onValueChange: val => _logo.style.height = val,
|
||||
ease: Ease.OutQuad);*/
|
||||
}
|
||||
else
|
||||
{
|
||||
_logo.style.left = targetX;
|
||||
_logo.style.top = targetY;
|
||||
_logo.style.width = 100;
|
||||
_logo.style.height = 100;
|
||||
}
|
||||
|
||||
_lastInteractionTime = Time.time;
|
||||
}
|
||||
|
||||
private void TransitionToIdle()
|
||||
{
|
||||
if (_currentState == MenuState.Idle) return;
|
||||
_currentState = MenuState.Idle;
|
||||
|
||||
// Quay lại dùng phần trăm để tự động căn giữa
|
||||
/*Tween.Custom(_logo.resolvedStyle.width, 200f, duration: 0.5f, onValueChange: val => _logo.style.width = val, ease: Ease.OutQuad);
|
||||
Tween.Custom(_logo.resolvedStyle.height, 200f, duration: 0.5f, onValueChange: val => _logo.style.height = val, ease: Ease.OutQuad);
|
||||
|
||||
// Animate left/top về 50%
|
||||
float startLeft = _logo.resolvedStyle.left;
|
||||
float startTop = _logo.resolvedStyle.top;
|
||||
float targetLeft = root.resolvedStyle.width / 2f;
|
||||
float targetTop = root.resolvedStyle.height / 2f;
|
||||
|
||||
Tween.Custom(0f, 1f, duration: 0.5f, ease: Ease.OutQuad, onValueChange: t => {
|
||||
_logo.style.left = Mathf.Lerp(startLeft, targetLeft, t);
|
||||
_logo.style.top = Mathf.Lerp(startTop, targetTop, t);
|
||||
}).OnComplete(() => {
|
||||
ResetLogoPosition(); // Đảm bảo cuối cùng dùng đơn vị Percent
|
||||
});
|
||||
|
||||
Tween.Custom(1f, 0f, duration: 0.5f, onValueChange: val => _ribbon.style.opacity = val)
|
||||
.OnComplete(() => _ribbon.style.display = DisplayStyle.None);*/
|
||||
}
|
||||
|
||||
public override 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();
|
||||
}
|
||||
}
|
||||
|
||||
private void StartPulse()
|
||||
{
|
||||
/*if (_pulseTween.isAlive) _pulseTween.Stop();
|
||||
_pulseTween = Tween.Custom(Vector3.one, Vector3.one * 1.1f, duration: 0.8f,
|
||||
onValueChange: val => _logo.style.scale = new StyleScale(new Scale(val)),
|
||||
cycles: -1,
|
||||
cycleMode: CycleMode.Yoyo,
|
||||
ease: Ease.InOutSine);*/
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (LocalizationManager.Instance != null)
|
||||
{
|
||||
LocalizationManager.Instance.OnLanguageChanged -= ApplyLocalization;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user