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

205 lines
6.2 KiB
C#
Raw Normal View History

2024-08-16 14:45:44 +08:00
using AxibugEmuOnline.Client.UI;
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using System.Collections.Generic;
using UnityEngine;
namespace AxibugEmuOnline.Client
{
2024-08-30 16:23:27 +08:00
public class SubMenuItemGroup : MenuItemController<MenuData>
2024-08-16 14:45:44 +08:00
{
[SerializeField]
MenuItem SubMenuItemTemplate;
[SerializeField]
CanvasGroup alphaGroup;
private TweenerCore<float, float, FloatOptions> selectTween;
private bool m_selected;
private TweenerCore<int, int, NoOptions> rollTween;
2024-09-12 15:08:48 +08:00
protected override void Awake()
2024-08-16 14:45:44 +08:00
{
m_selected = false;
2024-09-11 16:33:48 +08:00
if (alphaGroup != null) alphaGroup.alpha = 0;
2024-09-12 15:08:48 +08:00
base.Awake();
2024-08-16 14:45:44 +08:00
}
2024-08-30 16:23:27 +08:00
public override void Init(List<MenuData> menuDataList)
2024-08-16 14:45:44 +08:00
{
2024-08-16 17:26:28 +08:00
#if UNITY_EDITOR
2024-08-16 14:45:44 +08:00
while (transform.childCount > 0)
{
DestroyImmediate(transform.GetChild(0).gameObject);
}
2024-08-16 17:26:28 +08:00
#else
for (int i = 0; i < transform.childCount; i++)
{
Destroy(transform.GetChild(i).gameObject);
}
#endif
2024-08-16 14:45:44 +08:00
2024-08-16 17:26:28 +08:00
m_runtimeMenuUI.Clear();
2024-08-16 14:45:44 +08:00
foreach (MenuData menuData in menuDataList)
{
2024-08-22 15:16:58 +08:00
var template = menuData.OverrideTemplate != null ? menuData.OverrideTemplate : SubMenuItemTemplate;
var item = Clone(template, transform);
2024-08-16 17:26:28 +08:00
item.SetData(menuData);
m_runtimeMenuUI.Add(item);
}
calcItemPosition();
for (var i = 0; i < m_runtimeMenuUI.Count; i++)
{
var item = m_runtimeMenuUI[i];
var needPos = m_itemUIPosition[i];
item.Rect.anchoredPosition = needPos;
2024-08-16 14:45:44 +08:00
}
}
2024-09-12 15:08:48 +08:00
protected override bool OnCmdEnter()
2024-08-21 16:28:23 +08:00
{
2024-09-12 15:08:48 +08:00
base.OnCmdEnter();
LaunchUI.Instance.ToDetailMenuLayout();
2024-09-12 15:08:48 +08:00
var item = GetItemUIByIndex(SelectIndex);
2024-08-21 16:28:23 +08:00
item.SetSelectState(false);
2024-09-11 16:33:48 +08:00
return true;
2024-08-21 16:28:23 +08:00
}
2024-09-12 15:08:48 +08:00
protected override void OnCmdBack()
2024-08-21 16:28:23 +08:00
{
2024-09-12 15:08:48 +08:00
base.OnCmdBack();
2024-08-21 16:28:23 +08:00
LaunchUI.Instance.ToMainMenuLayout();
2024-09-12 15:08:48 +08:00
var item = GetItemUIByIndex(SelectIndex);
2024-08-21 16:28:23 +08:00
item.SetSelectState(true);
}
2024-08-16 17:26:28 +08:00
protected override void OnCmdSelectItemUp()
{
if (m_enteredItem == null)
SelectIndex--;
2024-08-16 17:26:28 +08:00
}
protected override void OnCmdSelectItemDown()
{
if (m_enteredItem == null)
SelectIndex++;
2024-08-16 17:26:28 +08:00
}
2024-08-30 16:23:27 +08:00
public virtual void SetSelect(bool select)
2024-08-16 14:45:44 +08:00
{
if (m_selected == select) return;
m_selected = select;
if (selectTween != null)
{
selectTween.Kill();
selectTween = null;
}
if (select)
{
selectTween = DOTween.To(() => alphaGroup.alpha, (x) => alphaGroup.alpha = x, 1, 10).SetSpeedBased();
}
else
{
selectTween = DOTween.To(() => alphaGroup.alpha, (x) => alphaGroup.alpha = x, 0, 10).SetSpeedBased();
}
2024-08-16 17:26:28 +08:00
ListenControlAction = m_selected;
2024-08-16 14:45:44 +08:00
}
protected override void OnCmdSelectItemLeft()
{
base.OnCmdSelectItemLeft();
OnCmdBack();
}
2024-08-16 17:26:28 +08:00
2024-08-16 14:45:44 +08:00
protected override void OnSelectMenuChanged()
{
if (rollTween != null) { rollTween.Kill(); rollTween = null; }
2024-08-16 17:26:28 +08:00
float duration = 0.5f;
calcItemPosition();
2024-08-21 16:28:23 +08:00
for (var i = 0; i < m_runtimeMenuUI.Count; i++)
{
var item = m_runtimeMenuUI[i];
bool isSelectItem = i == SelectIndex;
item.SetSelectState(isSelectItem);
}
2024-08-16 17:26:28 +08:00
rollTween = DOTween.To(() => 1, (x) => { }, 1, duration).OnUpdate(() =>
2024-08-16 14:45:44 +08:00
{
2024-08-16 17:26:28 +08:00
var tweenProgress = rollTween.position / rollTween.Duration();
2024-08-16 14:45:44 +08:00
for (var i = 0; i < m_runtimeMenuUI.Count; i++)
{
var item = m_runtimeMenuUI[i];
2024-08-16 17:26:28 +08:00
var needPos = m_itemUIPosition[i];
item.Rect.anchoredPosition = Vector2.Lerp(item.Rect.anchoredPosition, needPos, tweenProgress);
2024-08-16 14:45:44 +08:00
}
2024-08-16 17:26:28 +08:00
}).OnComplete(() =>
{
for (var i = 0; i < m_runtimeMenuUI.Count; i++)
{
var item = m_runtimeMenuUI[i];
var needPos = m_itemUIPosition[i];
item.Rect.anchoredPosition = needPos;
}
2024-08-16 14:45:44 +08:00
});
}
2024-08-16 17:26:28 +08:00
[SerializeField]
Vector2 m_selectItemPosition = new Vector2(50, -51);
[SerializeField]
float step = 50f;
[SerializeField]
float splitStep = 200f;
List<Vector2> m_itemUIPosition = new List<Vector2>();
private void calcItemPosition()
{
m_itemUIPosition.Clear();
for (int i = 0; i < m_runtimeMenuUI.Count; i++)
{
var gap = SelectIndex - i;
var start = m_selectItemPosition;
start.y += step * gap;
if (i < SelectIndex) start.y += splitStep;
m_itemUIPosition.Add(start);
}
}
2024-08-22 15:16:58 +08:00
private MenuItem Clone(MenuItem template, Transform parent)
2024-08-16 14:45:44 +08:00
{
#if UNITY_EDITOR
if (Application.isPlaying)
{
2024-11-13 13:55:36 +08:00
var item = GameObject.Instantiate(template.gameObject, parent).GetComponent<MenuItem>();
2024-11-11 18:18:59 +08:00
item.transform.localPosition = Vector3.zero;
return item;
2024-08-16 14:45:44 +08:00
}
else
{
2024-08-22 15:16:58 +08:00
var clone = UnityEditor.PrefabUtility.InstantiatePrefab(template.gameObject, parent) as GameObject;
2024-11-11 18:18:59 +08:00
clone.transform.localPosition = Vector3.zero;
2024-08-16 14:45:44 +08:00
return clone.GetComponent<MenuItem>();
}
#else
return GameObject.Instantiate(SubMenuItemTemplate.gameObject, parent).GetComponent<MenuItem>();
#endif
}
}
}