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

110 lines
3.0 KiB
C#
Raw Normal View History

2024-08-16 14:45:44 +08:00
using AxibugEmuOnline.Client.UI;
using System.Collections.Generic;
using UnityEngine;
namespace AxibugEmuOnline.Client
{
2024-09-12 15:08:48 +08:00
public abstract class MenuItemController : CommandExecuter
2024-08-16 14:45:44 +08:00
{
2024-08-30 16:23:27 +08:00
[SerializeField]
protected Transform m_menuItemRoot;
protected List<MenuItem> m_runtimeMenuUI = new List<MenuItem>();
protected MenuItem m_enteredItem = null;
2024-08-16 17:26:28 +08:00
2024-08-30 16:23:27 +08:00
protected int m_selectIndex = -1;
public virtual int SelectIndex
2024-08-16 14:45:44 +08:00
{
get => m_selectIndex;
set
{
value = Mathf.Clamp(value, 0, m_runtimeMenuUI.Count - 1);
2024-08-21 16:28:23 +08:00
if (m_selectIndex == value) return;
2024-08-16 14:45:44 +08:00
m_selectIndex = value;
OnSelectMenuChanged();
}
}
protected virtual void Start()
{
2024-08-30 16:23:27 +08:00
if (m_menuItemRoot != null)
2024-08-16 14:45:44 +08:00
{
2024-08-30 16:23:27 +08:00
for (int i = 0; i < m_menuItemRoot.childCount; i++)
{
Transform child = m_menuItemRoot.GetChild(i);
m_runtimeMenuUI.Add(child.GetComponent<MenuItem>());
}
2024-08-16 14:45:44 +08:00
}
Canvas.ForceUpdateCanvases();
SelectIndex = 0;
}
2024-09-12 15:08:48 +08:00
protected virtual MenuItem GetItemUIByIndex(int index)
2024-08-16 17:26:28 +08:00
{
2024-09-12 15:08:48 +08:00
return m_runtimeMenuUI[SelectIndex];
2024-08-16 17:26:28 +08:00
}
2024-08-16 14:45:44 +08:00
2024-09-12 15:08:48 +08:00
protected override bool OnCmdEnter()
2024-08-16 17:26:28 +08:00
{
2024-09-12 15:08:48 +08:00
if (m_enteredItem == null)
2024-08-16 17:26:28 +08:00
{
2024-09-12 15:08:48 +08:00
var willEnterItem = GetItemUIByIndex(SelectIndex);
bool res = willEnterItem.OnEnterItem();
if (res)
2024-08-16 17:26:28 +08:00
{
2024-09-12 15:08:48 +08:00
m_enteredItem = willEnterItem;
2024-08-16 17:26:28 +08:00
}
2024-09-12 15:08:48 +08:00
return res;
2024-08-16 17:26:28 +08:00
}
2024-09-12 15:08:48 +08:00
return false;
2024-08-16 17:26:28 +08:00
}
2024-09-12 15:08:48 +08:00
protected override void OnCmdBack()
2024-09-11 16:33:48 +08:00
{
2024-09-12 15:08:48 +08:00
if (m_enteredItem != null)
{
m_enteredItem.OnExitItem();
m_enteredItem = null;
}
2024-09-11 16:33:48 +08:00
}
2024-09-12 17:47:05 +08:00
protected abstract void OnSelectMenuChanged();
2024-08-30 16:23:27 +08:00
}
public abstract class MenuItemController<T> : MenuItemController
{
public override bool Enable => enabled && ListenControlAction;
2024-08-30 16:23:27 +08:00
private bool m_listenControlAction;
public bool ListenControlAction
2024-08-16 17:26:28 +08:00
{
2024-08-30 16:23:27 +08:00
get => m_listenControlAction;
set
{
m_listenControlAction = value;
if (value)
CommandDispatcher.Instance.RegistController(this);
else
CommandDispatcher.Instance.UnRegistController(this);
if(!value)
ResetPulsInvoker();
2024-08-30 16:23:27 +08:00
}
}
public abstract void Init(List<T> menuDataList);
private void OnDestroy()
{
if (CommandDispatcher.Instance != null)
CommandDispatcher.Instance.UnRegistController(this);
2024-08-16 17:26:28 +08:00
}
2024-08-16 14:45:44 +08:00
}
}