NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/PopUp/PopUpManager.cs

219 lines
6.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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>
/// 弹窗队列字典key触发的UI名value弹窗队列
/// </summary>
private Dictionary<string, LinkedList<IPopUp>> dicPopUpQueue;
/// <summary>
/// 记录当前触发的弹窗UI名
/// </summary>
private string curUIName;
/// <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);
EventManager.Instance.Register<string>(EventManager.EventName.UIClose, OnUIClose);
}
public void Release()
{
EventManager.Instance.Unregister<string>(EventManager.EventName.UIClose, OnUIClose);
EventManager.Instance.Unregister<string>(EventManager.EventName.UIOpenByAnimComplete, OnUIOpenByaAnimComplte);
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);
}
if (UIManager.Instance.IsWindowOpening(UINameConst.UI_MainPanel))
TriggerPopUp(UINameConst.UI_MainPanel);
}
/// <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;
if (setCacheNeedRemovePopUp.Count > 0)
return;
UIWindow uiWindow = UIManager.Instance.GetOpenedWindowByPath(uiName);
if (null == uiWindow)
return;
if (uiWindow.IsHide())
return;
foreach (IPopUp popUp in linkPopUp)
{
if (!setPopUp.Contains(popUp)) // 不存在该弹窗,或者已经弹窗过
continue;
if (!await popUp.PopUp()) // 开始弹窗
continue;
setCacheNeedRemovePopUp.Add(popUp); // 记录需要移除的弹窗
curUIName = uiName;
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);
}
private void OnUIClose(string uiName)
{
IPopUp popUp = null;
foreach (IPopUp temp in setCacheNeedRemovePopUp)
{
if (temp.PopUpUIName == uiName)
{
popUp = temp;
break;
}
}
setCacheNeedRemovePopUp.Remove(popUp);
if (setCacheNeedRemovePopUp.Count < 0)
TriggerPopUp(curUIName);
}
#endregion
}
}