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

93 lines
2.9 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;
2024-08-16 17:26:28 +08:00
using System;
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-08-16 14:45:44 +08:00
private void Awake()
{
m_select = false;
m_progress = 0f;
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);
}
public void SetData(MenuData data)
{
2024-08-22 17:25:00 +08:00
SetBaseInfo(data.Name, data.Description);
SetIcon(data.Icon);
2024-08-22 15:16:58 +08:00
if (SubMenuItemGroup != null) SubMenuItemGroup.Init(data.SubMenus);
}
2024-08-16 14:45:44 +08:00
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);
if (SubMenuItemGroup != null) SubMenuItemGroup.SetSelect(selected);
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-08-22 15:16:58 +08:00
public virtual void OnEnterItem() { }
2024-08-21 16:28:23 +08:00
2024-08-22 15:16:58 +08:00
public virtual void OnExitItem() { }
}
}