Files
BABA_YAGA/Assets/Scripts/UI/BaseUIController.cs

76 lines
2.2 KiB
C#
Raw Permalink Normal View History

2026-04-28 00:07:42 +07:00
using UnityEngine;
using UnityEngine.UIElements;
using PrimeTween;
using System.Threading.Tasks;
namespace Hallucinate.UI
{
2026-04-29 01:04:28 +07:00
public abstract class BaseUIController : ScriptableObject
2026-04-28 00:07:42 +07:00
{
protected VisualElement root;
protected UIManager uiManager;
2026-04-28 18:49:05 +07:00
public VisualElement Root => root; // Thêm thuộc tính này
2026-04-28 00:07:42 +07:00
public virtual void Initialize(VisualElement uxmlRoot, UIManager manager)
{
root = uxmlRoot;
uiManager = manager;
2026-04-28 10:21:28 +07:00
// Đảm bảo ban đầu ẩn hết
2026-04-28 00:07:42 +07:00
Hide();
2026-04-30 17:47:29 +07:00
// Tự động gán âm thanh phản hồi cho các UI elements
UIAudioHelper.BindFeedback(root);
2026-04-28 00:07:42 +07:00
}
public virtual void Show()
{
if (root != null)
2026-04-28 10:21:28 +07:00
{
2026-04-28 00:07:42 +07:00
root.style.display = DisplayStyle.Flex;
2026-04-28 10:21:28 +07:00
root.style.opacity = 1;
}
2026-04-28 00:07:42 +07:00
}
public virtual void Hide()
{
if (root != null)
2026-04-28 10:21:28 +07:00
{
2026-04-28 00:07:42 +07:00
root.style.display = DisplayStyle.None;
2026-04-28 10:21:28 +07:00
}
2026-04-28 00:07:42 +07:00
}
2026-04-29 02:31:15 +07:00
protected string GetLoc(string key)
{
if (LocalizationManager.Instance != null)
return LocalizationManager.Instance.GetLocalizedString(key);
return key;
}
2026-04-29 13:10:00 +07:00
public virtual void Update() { }
2026-04-28 00:07:42 +07:00
public virtual async Task PlayTransitionIn()
{
if (root == null) return;
Show();
2026-04-28 10:21:28 +07:00
// Reset vị trí mặc định để tránh lỗi trôi màn hình
2026-04-28 00:07:42 +07:00
root.style.translate = new StyleTranslate(new Translate(Length.Percent(100), 0));
2026-04-28 10:21:28 +07:00
2026-04-28 00:07:42 +07:00
await Tween.Custom(100f, 0f, duration: 0.5f, ease: Ease.OutBack,
onValueChange: val => root.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
}
public virtual async Task PlayTransitionOut()
{
if (root == null) return;
await Tween.Custom(0f, -100f, duration: 0.5f, ease: Ease.InBack,
onValueChange: val => root.style.translate = new StyleTranslate(new Translate(Length.Percent(val), 0)));
2026-04-28 10:21:28 +07:00
2026-04-28 00:07:42 +07:00
Hide();
}
}
}