using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
namespace Stary.Evo.UIFarme
{
public interface IPanelSystem : ISystem
{
///
/// UI的入栈操作,此操作会显示一个面板
///
Task PushQueue(Transform parent=null,string packageName=null) where T : IBasePanel, new();
///
/// UI的入栈操作,此操作会显示一个面板
///
Task PushStack(Transform parent=null,string packageName=null) where T : IBasePanel, new();
///
/// 执行面板的出栈操作,此操作会执行面板的OnExit方法
///
void PopQueue() where T : IBasePanel, new();
///
/// 执行面板的出栈操作,此操作会执行面板的OnExit方法
///
void PopStack();
///
/// 面板全部出栈此操作会执行面板的OnExit方法
///
void PopAll();
///
/// 事件发送
///
///
public void SendPanelEvent(TEvent key) where TEvent : IConvertible;
///
/// 事件发送
///
///
public void SendPanelEvent(TEvent key, Tvalue1 value) where TEvent : IConvertible;
///
/// 事件发送
///
///
public void SendPanelEvent(TEvent key, Tvalue1 value1, Tvalue2 value2)
where TEvent : IConvertible;
///
/// 事件发送
///
///
public void SendPanelEvent(TEvent key, Tvlue1 value1, Tvlue2 vlue2, Tvlue3 vlue3)
where TEvent : IConvertible;
///
/// 返回存储面板实例的字典
///
///
Dictionary Get_Dic();
IBasePanel GetPanel(string PanelName);
}
///
/// 面板管理器,用栈来存储UI
///
public class PanelSystem : AbstractSystem, IPanelSystem
{
///
/// 存储普通UI面板
///
private List queuePanel;
///
/// 存储堆栈UI面板
///
private Stack stackPanel;
///
/// 存储所有UI信息的字典,每个UI信息都会对应一个GameObject
///
private Dictionary dicUI;
protected override void OnInit()
{
stackPanel = new Stack();
queuePanel = new List();
dicUI = new Dictionary();
}
///
/// UI的入栈操作,此操作会显示一个面板
///
///
/// 非热更模式传null
///
public async Task PushQueue(Transform parent=null,string packageName=null) where T : IBasePanel, new()
{
string panelName = typeof(T).Name;
IBasePanel nextPanel = null;
if (!dicUI.ContainsKey(panelName))
{
nextPanel = new T();
nextPanel.Initialize(this);
nextPanel.SetPanelParent(parent);
GameObject panelGo = await nextPanel.CreatePanel($"Prefabs_{panelName}",packageName);
///生成面板后,进行初始化操作
nextPanel.Initialize(panelGo);
dicUI.Add(panelName, nextPanel);
}
else
{
for (int i = 0; i < queuePanel.Count; i++)
{
if (queuePanel[i].GetType().Name == panelName)
{
return;
}
}
nextPanel = dicUI[panelName];
}
queuePanel.Add(nextPanel);
nextPanel.OnEnter();
//TOOD
}
///
/// UI的入栈操作,此操作会显示一个面板
///
///
/// 非热更模式传null
///
public async Task PushStack(Transform parent=null,string packageName=null) where T : IBasePanel, new()
{
string panelName = typeof(T).Name;
IBasePanel nextPanel = null;
if (!dicUI.ContainsKey(panelName))
{
nextPanel = new T();
nextPanel.Initialize(this);
nextPanel.SetPanelParent(parent);
GameObject panelGo = await nextPanel.CreatePanel($"Prefabs_{panelName}",packageName);
///生成面板后,进行初始化操作
nextPanel.Initialize(panelGo);
dicUI.Add(panelName, nextPanel);
}
else
{
for (int i = 0; i < stackPanel.Count; i++)
{
if (queuePanel[i].GetType().Name == panelName)
{
return;
}
}
nextPanel = dicUI[panelName];
}
stackPanel.Push(nextPanel);
nextPanel.OnEnter();
}
public void PopQueue() where T : IBasePanel, new()
{
string panelName = typeof(T).Name;
for (int i = 0; i < queuePanel.Count; i++)
{
if (queuePanel[i].GetType().Name == panelName)
{
queuePanel[i].OnExit();
queuePanel.RemoveAt(i);
}
}
}
public void PopStack()
{
if (stackPanel.Count > 0)
{
stackPanel.Pop().OnExit();
}
if (stackPanel.Count > 0)
{
stackPanel.Peek().OnResume();
}
}
public void PopAll()
{
while (stackPanel.Count > 0)
stackPanel.Pop().OnExit();
foreach (var panel in queuePanel)
{
panel.OnExit();
}
queuePanel.Clear();
}
///
/// 事件发送
///
///
public void SendPanelEvent(TEvent key) where TEvent : IConvertible
{
this.SendEvent(key);
}
///
/// 事件发送
///
///
public void SendPanelEvent(TEvent key, Tvalue1 value) where TEvent : IConvertible
{
this.SendEvent(key,value);
}
///
/// 事件发送
///
///
public void SendPanelEvent(TEvent key, Tvalue1 value1, Tvalue2 value2)
where TEvent : IConvertible
{
this.SendEvent(key, value1, value2);
}
///
/// 事件发送
///
///
public void SendPanelEvent(TEvent key, Tvlue1 value1, Tvlue2 vlue2, Tvlue3 vlue3)
where TEvent : IConvertible
{
this.SendEvent(key, value1, vlue2, vlue3);
}
public Dictionary Get_Dic()
{
if (dicUI.Count > 0)
{
return dicUI;
}
return null;
}
public IBasePanel GetPanel(string PanelName)
{
if (dicUI.ContainsKey(PanelName))
{
return dicUI[PanelName];
}
return null;
}
public override void Dispose()
{
PopAll();
stackPanel = null;
queuePanel =null;
dicUI = null;
}
}
}