181 lines
5.2 KiB
C#
181 lines
5.2 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using Framework;
|
|
using Gameplay.FunctionLock;
|
|
using PhxhSDK;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Gameplay.PopUp
|
|
{
|
|
/// <summary>
|
|
/// 弹窗管理器
|
|
/// </summary>
|
|
public class PopUpManager : Singlenton<PopUpManager>, IInitable
|
|
{
|
|
/// <summary>
|
|
/// 弹窗
|
|
/// </summary>
|
|
private HashSet<IPopUp> setPopUp;
|
|
/// <summary>
|
|
/// 弹窗队列字典
|
|
/// </summary>
|
|
private Dictionary<string, LinkedList<IPopUp>> dicPopUpQueue;
|
|
|
|
/// <summary>
|
|
/// 缓存触发成功的弹窗
|
|
/// </summary>
|
|
private HashSet<IPopUp> setCacheNeedRemovePopUp;
|
|
|
|
public void Init()
|
|
{
|
|
setPopUp = new HashSet<IPopUp>();
|
|
dicPopUpQueue = new Dictionary<string, LinkedList<IPopUp>>();
|
|
setCacheNeedRemovePopUp = new HashSet<IPopUp>();
|
|
|
|
EventManager.Instance.Register<string>(EventManager.EventName.UIOpenByAnimComplete, OnUIOpenByaAnimComplte);
|
|
}
|
|
|
|
public void Release()
|
|
{
|
|
if (null != dicPopUpQueue)
|
|
{
|
|
foreach (var i in dicPopUpQueue.Values)
|
|
{
|
|
i.Clear();
|
|
}
|
|
dicPopUpQueue.Clear();
|
|
}
|
|
|
|
setPopUp?.Clear();
|
|
setCacheNeedRemovePopUp?.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加弹窗
|
|
/// </summary>
|
|
public void AddPopUp(IPopUp newPopUp)
|
|
{
|
|
if (null == newPopUp)
|
|
return;
|
|
|
|
if (setPopUp.Contains(newPopUp))
|
|
return;
|
|
|
|
setPopUp.Add(newPopUp);
|
|
|
|
foreach (string triggerUIName in newPopUp.TriggerUINames)
|
|
{
|
|
AddPopUpByUIName(triggerUIName, newPopUp);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除弹窗
|
|
/// </summary>
|
|
/// <param name="removePopUp"></param>
|
|
public bool RemovePopUp(IPopUp removePopUp)
|
|
{
|
|
if (!setPopUp.Contains(removePopUp))
|
|
return false;
|
|
|
|
foreach (string uiName in removePopUp.TriggerUINames)
|
|
{
|
|
RemovePopUpUIName(uiName, removePopUp);
|
|
}
|
|
|
|
return setPopUp.Remove(removePopUp);
|
|
}
|
|
|
|
#region 私有方法
|
|
/// <summary>
|
|
/// 根据UI名添加弹窗
|
|
/// </summary>
|
|
/// <param name="popUpTrigger"></param>
|
|
/// <param name="newPopUp"></param>
|
|
private void AddPopUpByUIName(string uiName, IPopUp newPopUp)
|
|
{
|
|
if (!dicPopUpQueue.TryGetValue(uiName, out LinkedList<IPopUp> linkPopUp))
|
|
{
|
|
linkPopUp = new LinkedList<IPopUp>();
|
|
dicPopUpQueue.Add(uiName, linkPopUp);
|
|
}
|
|
|
|
if (linkPopUp.Count <= 0)
|
|
{
|
|
linkPopUp.AddLast(newPopUp);
|
|
return;
|
|
}
|
|
|
|
LinkedListNode<IPopUp> nodePopUp = linkPopUp.First;
|
|
while (null != nodePopUp && null != nodePopUp.Value)
|
|
{
|
|
if (nodePopUp.Value.Priority > newPopUp.Priority) // 根据优先级排序
|
|
{
|
|
linkPopUp.AddBefore(nodePopUp, newPopUp);
|
|
return;
|
|
}
|
|
|
|
nodePopUp = nodePopUp.Next;
|
|
}
|
|
|
|
linkPopUp.AddLast(newPopUp);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据UI名移除弹窗
|
|
/// </summary>
|
|
/// <param name="uiName"></param>
|
|
/// <param name="removePopUp"></param>
|
|
private void RemovePopUpUIName(string uiName, IPopUp removePopUp)
|
|
{
|
|
if (!dicPopUpQueue.TryGetValue(uiName, out LinkedList<IPopUp> linkPopUp))
|
|
return;
|
|
|
|
linkPopUp.Remove(removePopUp);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 触发弹窗
|
|
/// </summary>
|
|
/// <param name="triggerType">触发类型</param>
|
|
private async void TriggerPopUp(string uiName)
|
|
{
|
|
if (!dicPopUpQueue.TryGetValue(uiName, out LinkedList<IPopUp> linkPopUp)) // 不存在弹窗
|
|
return;
|
|
|
|
if (linkPopUp.Count <= 0) // 无弹窗
|
|
return;
|
|
|
|
foreach (IPopUp popUp in linkPopUp)
|
|
{
|
|
if (!setPopUp.Contains(popUp)) // 不存在该弹窗,或者已经弹窗过
|
|
continue;
|
|
|
|
if (!await popUp.PopUp()) // 开始弹窗
|
|
continue;
|
|
|
|
setCacheNeedRemovePopUp.Add(popUp); // 记录需要移除的弹窗
|
|
|
|
if (!popUp.IsSameTimePopUp) // 不能同时弹窗则跳出
|
|
break;
|
|
}
|
|
|
|
foreach (IPopUp popUp in setCacheNeedRemovePopUp)
|
|
{
|
|
setPopUp.Remove(popUp);
|
|
linkPopUp.Remove(popUp);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// UI打开完成
|
|
/// </summary>
|
|
/// <param name="uiName"></param>
|
|
private async void OnUIOpenByaAnimComplte(string uiName)
|
|
{
|
|
await UniTask.WaitUntil(() => { return !FunctionLockManager.Instance.IsPlayingUnlockAnim; });
|
|
|
|
TriggerPopUp(uiName);
|
|
}
|
|
#endregion
|
|
}
|
|
} |