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

180 lines
5.5 KiB
C#
Raw Permalink Normal View History

using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using System;
using System.Collections.Generic;
2024-08-21 16:28:23 +08:00
using System.Linq;
2024-08-22 15:16:58 +08:00
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace AxibugEmuOnline.Client.UI
{
2024-08-30 16:23:27 +08:00
public class MainMenuController : MenuItemController<MenuData>
{
[SerializeField]
HorizontalLayoutGroup GroupRoot;
[SerializeField]
MenuItem Template;
[SerializeField]
2024-08-16 14:45:44 +08:00
List<MenuData> MenuSetting;
[SerializeField]
float HoriRollSpd = 1f;
2024-09-18 15:53:58 +08:00
[SerializeField]
int InitSelect = -1;
2024-08-16 14:45:44 +08:00
private RectTransform groupRootRect => m_menuItemRoot as RectTransform;
private TweenerCore<Vector2, Vector2, VectorOptions> rollTween;
2024-08-21 16:28:23 +08:00
private List<CanvasGroup> m_runtimeMenuUICanvas;
private Sequence seq;
2024-09-18 15:53:58 +08:00
2024-08-21 16:28:23 +08:00
protected override void Start()
{
base.Start();
m_runtimeMenuUICanvas = m_runtimeMenuUI.Select(menu => menu.gameObject.AddComponent<CanvasGroup>()).ToList();
2024-08-22 17:25:00 +08:00
m_runtimeMenuUICanvas.ForEach(canv => canv.gameObject.AddComponent<AutoRaycastCanvasGroup>());
2024-09-18 15:53:58 +08:00
if (InitSelect != -1)
{
m_selectIndex = InitSelect;
PlaySelectItemAnim(false);
}
2024-08-21 16:28:23 +08:00
}
2024-09-18 15:53:58 +08:00
public override void Init(List<MenuData> menuDataList) { }
2024-08-21 16:28:23 +08:00
public void EnterDetailState()
{
if (seq != null)
{
seq.Kill();
seq = null;
}
var selectItem = m_runtimeMenuUICanvas[SelectIndex];
var hideItem = m_runtimeMenuUICanvas.Where(i => i != selectItem).ToList();
seq = DOTween.Sequence();
seq.Append(
DOTween.To(() => selectItem.alpha, (x) => selectItem.alpha = x, 1, 0.2f)
)
.Join(
DOTween.To(() => hideItem[0].alpha, (x) => hideItem.ForEach(i => i.alpha = x), 0, 0.2f)
);
seq.Play();
}
public void ExitDetailState()
{
if (seq != null)
{
seq.Kill();
seq = null;
}
var selectItem = m_runtimeMenuUICanvas[SelectIndex];
var hideItem = m_runtimeMenuUICanvas.Where(i => i != selectItem).ToList();
seq = DOTween.Sequence();
seq.Append(
DOTween.To(() => selectItem.alpha, (x) => selectItem.alpha = x, 1, 0.2f)
)
.Join(
DOTween.To(() => hideItem[0].alpha, (x) => hideItem.ForEach(i => i.alpha = x), 1, 0.2f)
);
seq.Play();
}
2024-08-16 14:45:44 +08:00
protected override void OnSelectMenuChanged()
2024-09-18 15:53:58 +08:00
{
PlaySelectItemAnim(true);
}
private void PlaySelectItemAnim(bool useAnim)
{
2024-08-16 14:45:44 +08:00
var step = GroupRoot.spacing;
var needSelectItem = m_runtimeMenuUI[SelectIndex];
var offset = needSelectItem.Rect.anchoredPosition.x;
var targetPosition = groupRootRect.anchoredPosition;
targetPosition.x = -offset;
if (rollTween != null) { rollTween.Kill(); rollTween = null; }
2024-08-21 16:28:23 +08:00
for (var i = 0; i < m_runtimeMenuUI.Count; i++)
{
var item = m_runtimeMenuUI[i];
item.SetSelectState(i == SelectIndex);
}
2024-09-18 15:53:58 +08:00
if (useAnim)
{
rollTween = DOTween.To(
() => groupRootRect.anchoredPosition,
(x) => groupRootRect.anchoredPosition = x,
targetPosition,
HoriRollSpd)
.SetSpeedBased();
}
else
{
groupRootRect.anchoredPosition = targetPosition;
}
}
2024-08-16 17:26:28 +08:00
protected override void OnCmdSelectItemLeft()
{
SelectIndex--;
}
protected override void OnCmdSelectItemRight()
{
2024-08-16 17:26:28 +08:00
SelectIndex++;
}
#if UNITY_EDITOR
[ContextMenu("UpdateMenuUI")]
public void UpdateMenuUI()
{
while (GroupRoot.transform.childCount > 0)
{
DestroyImmediate(GroupRoot.transform.GetChild(GroupRoot.transform.childCount - 1).gameObject);
}
for (int i = 0; i < MenuSetting.Count; i++)
{
2024-08-22 15:16:58 +08:00
var settingData = MenuSetting[i];
var templatePrefab = settingData.OverrideTemplate != null ? settingData.OverrideTemplate.gameObject : Template.gameObject;
MenuItem itemScript = null;
2024-08-22 15:16:58 +08:00
var prefabClone = UnityEditor.PrefabUtility.InstantiatePrefab(templatePrefab) as GameObject;
itemScript = prefabClone.GetComponent<MenuItem>();
itemScript.gameObject.SetActive(true);
itemScript.transform.SetParent(GroupRoot.transform);
2024-08-22 15:16:58 +08:00
itemScript.SetData(settingData);
2024-11-11 18:18:59 +08:00
itemScript.transform.localScale = Vector3.one;
}
UnityEditor.EditorUtility.SetDirty(this);
}
#endif
}
[Serializable]
2024-08-16 14:45:44 +08:00
public class MenuData
{
public Sprite Icon;
public string Name;
2024-11-15 10:53:19 +08:00
public string SubTitle;
2024-08-16 14:45:44 +08:00
public string Description;
2024-08-22 15:16:58 +08:00
public MenuItem OverrideTemplate;
2024-08-16 14:45:44 +08:00
public List<MenuData> SubMenus;
}
}