AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/UI/MenuItem.cs

106 lines
3.2 KiB
C#
Raw Normal View History

2024-08-16 14:45:44 +08:00
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using UnityEngine;
using UnityEngine.UI;
namespace AxibugEmuOnline.Client.UI
{
public class MenuItem : MonoBehaviour
{
[SerializeField]
2024-08-30 16:23:27 +08:00
protected Image Icon;
[SerializeField]
2024-08-30 16:23:27 +08:00
protected Text Txt;
2024-08-16 10:53:43 +08:00
[SerializeField]
2024-08-30 16:23:27 +08:00
protected Text Descript;
2024-08-16 14:45:44 +08:00
[SerializeField]
2024-08-30 16:23:27 +08:00
protected Transform Root;
2024-08-16 14:45:44 +08:00
[SerializeField]
2024-08-30 16:23:27 +08:00
protected Image ShadowIcon;
2024-08-16 14:45:44 +08:00
[SerializeField]
2024-08-30 16:23:27 +08:00
protected CanvasGroup InfoNode;
2024-08-22 15:16:58 +08:00
[SerializeField]
2024-08-30 16:23:27 +08:00
protected SubMenuItemGroup SubMenuItemGroup;
2024-08-16 10:53:43 +08:00
public float SelectScale = 1f;
public float UnSelectScale = 0.85f;
public RectTransform Rect => transform as RectTransform;
2024-08-30 16:23:27 +08:00
protected bool m_select;
protected TweenerCore<float, float, FloatOptions> progressTween;
protected float m_progress;
2024-09-11 16:33:48 +08:00
2024-09-12 11:02:30 +08:00
protected virtual void Awake()
{
Reset();
}
2024-09-11 16:33:48 +08:00
public void SetData(MenuData data)
{
Reset();
SetBaseInfo(data.Name, data.Description);
SetIcon(data.Icon);
if (SubMenuItemGroup != null) SubMenuItemGroup.Init(data.SubMenus);
}
protected void Reset()
2024-08-16 14:45:44 +08:00
{
m_select = false;
m_progress = 0f;
2024-09-11 16:33:48 +08:00
Root.localScale = Vector3.one * UnSelectScale;
if (progressTween != null) { progressTween.Kill(); progressTween = null; }
2024-08-16 14:45:44 +08:00
if (ShadowIcon != null) ShadowIcon.gameObject.SetActive(false);
2024-08-22 17:25:00 +08:00
if (InfoNode != null) InfoNode.alpha = 0;
2024-08-16 14:45:44 +08:00
if (ShadowIcon != null) ShadowIcon.gameObject.SetActiveEx(false);
if (SubMenuItemGroup != null) SubMenuItemGroup.SetSelect(false);
}
2024-08-22 17:25:00 +08:00
protected void SetBaseInfo(string name, string descript)
2024-08-22 15:16:58 +08:00
{
this.name = name;
2024-08-16 14:45:44 +08:00
2024-08-22 15:16:58 +08:00
if (Txt != null) Txt.text = name;
if (Descript != null) Descript.text = descript;
2024-08-22 17:25:00 +08:00
}
2024-08-16 14:45:44 +08:00
2024-08-22 17:25:00 +08:00
protected void SetIcon(Sprite icon)
{
if (Icon != null) Icon.sprite = icon;
if (ShadowIcon != null) ShadowIcon.sprite = icon;
}
2024-08-30 16:23:27 +08:00
public virtual void SetSelectState(bool selected)
{
2024-08-16 14:45:44 +08:00
if (m_select == selected) return;
m_select = selected;
if (ShadowIcon != null) ShadowIcon.gameObject.SetActiveEx(selected);
2024-09-12 11:19:40 +08:00
if (SubMenuItemGroup != null)
{
SubMenuItemGroup.SetSelect(selected);
}
2024-08-16 14:45:44 +08:00
if (progressTween != null) { progressTween.Kill(); progressTween = null; }
progressTween = DOTween.To(() => m_progress, (x) => m_progress = x, m_select ? 1 : 0, 5)
.SetSpeedBased().OnUpdate(() =>
{
2024-08-22 17:25:00 +08:00
if (InfoNode != null) InfoNode.alpha = m_progress;
2024-08-16 14:45:44 +08:00
Root.localScale = Vector3.one * Mathf.Lerp(UnSelectScale, SelectScale, m_progress);
});
}
2024-08-16 17:26:28 +08:00
2024-09-11 16:33:48 +08:00
public virtual bool OnEnterItem() => true;
2024-08-21 16:28:23 +08:00
2024-09-11 16:33:48 +08:00
public virtual bool OnExitItem() => true;
}
}