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

219 lines
6.6 KiB
C#
Raw Normal View History

using Cysharp.Threading.Tasks;
using Framework;
using Gameplay.FunctionLock;
2025-04-11 12:47:46 +08:00
using PhxhSDK;
using System.Collections.Generic;
namespace Gameplay.PopUp
{
/// <summary>
/// 弹窗管理器
/// </summary>
public class PopUpManager : Singlenton<PopUpManager>, IInitable
{
2025-04-11 13:47:41 +08:00
/// <summary>
2025-04-16 18:10:58 +08:00
/// 所有等待弹窗集合
2025-04-11 13:47:41 +08:00
/// </summary>
private HashSet<IPopUp> setPopUp;
2025-04-11 12:47:46 +08:00
/// <summary>
2025-04-16 18:10:58 +08:00
/// 弹窗队列字典,key:触发的UI名,value:弹窗队列
2025-04-11 12:47:46 +08:00
/// </summary>
private Dictionary<string, LinkedList<IPopUp>> dicPopUpQueue;
2025-04-22 15:28:05 +08:00
/// <summary>
/// 记录当前触发的弹窗UI名
/// </summary>
private string curUIName;
/// <summary>
2025-04-16 18:10:58 +08:00
/// 缓存等待移除的弹窗(触发成功的弹窗缓存到这里等待移除)
/// </summary>
private HashSet<IPopUp> setCacheNeedRemovePopUp;
2025-04-11 12:47:46 +08:00
public void Init()
{
2025-04-11 13:47:41 +08:00
setPopUp = new HashSet<IPopUp>();
dicPopUpQueue = new Dictionary<string, LinkedList<IPopUp>>();
setCacheNeedRemovePopUp = new HashSet<IPopUp>();
EventManager.Instance.Register<string>(EventManager.EventName.UIOpenByAnimComplete, OnUIOpenByaAnimComplte);
2025-04-22 15:14:20 +08:00
EventManager.Instance.Register<string>(EventManager.EventName.UIClose, OnUIClose);
2025-04-11 12:47:46 +08:00
}
public void Release()
{
2025-04-22 15:14:20 +08:00
EventManager.Instance.Unregister<string>(EventManager.EventName.UIClose, OnUIClose);
2025-04-22 14:51:24 +08:00
EventManager.Instance.Unregister<string>(EventManager.EventName.UIOpenByAnimComplete, OnUIOpenByaAnimComplte);
2025-04-11 12:47:46 +08:00
if (null != dicPopUpQueue)
{
foreach (var i in dicPopUpQueue.Values)
{
i.Clear();
}
dicPopUpQueue.Clear();
}
2025-04-11 13:47:41 +08:00
setPopUp?.Clear();
setCacheNeedRemovePopUp?.Clear();
2025-04-11 12:47:46 +08:00
}
/// <summary>
/// 添加弹窗
/// </summary>
2025-04-11 13:47:41 +08:00
public void AddPopUp(IPopUp newPopUp)
2025-04-11 12:47:46 +08:00
{
if (null == newPopUp)
return;
2025-04-11 13:47:41 +08:00
if (setPopUp.Contains(newPopUp))
return;
setPopUp.Add(newPopUp);
foreach (string triggerUIName in newPopUp.TriggerUINames)
{
AddPopUpByUIName(triggerUIName, newPopUp);
}
2025-04-22 16:41:10 +08:00
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))
2025-04-11 12:47:46 +08:00
{
linkPopUp = new LinkedList<IPopUp>();
dicPopUpQueue.Add(uiName, linkPopUp);
2025-04-11 12:47:46 +08:00
}
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);
}
2025-04-11 12:47:46 +08:00
/// <summary>
/// 触发弹窗
/// </summary>
/// <param name="triggerType">触发类型</param>
private async void TriggerPopUp(string uiName)
2025-04-11 12:47:46 +08:00
{
if (!dicPopUpQueue.TryGetValue(uiName, out LinkedList<IPopUp> linkPopUp)) // 不存在弹窗
2025-04-11 12:47:46 +08:00
return;
if (linkPopUp.Count <= 0) // 无弹窗
2025-04-11 12:47:46 +08:00
return;
2025-04-22 15:14:20 +08:00
if (setCacheNeedRemovePopUp.Count > 0)
return;
2025-04-22 14:51:24 +08:00
UIWindow uiWindow = UIManager.Instance.GetOpenedWindowByPath(uiName);
if (null == uiWindow)
return;
if (uiWindow.IsHide())
2025-04-16 18:04:51 +08:00
return;
foreach (IPopUp popUp in linkPopUp)
2025-04-11 17:38:09 +08:00
{
2025-04-14 15:16:05 +08:00
if (!setPopUp.Contains(popUp)) // 不存在该弹窗,或者已经弹窗过
continue;
2025-04-11 17:38:09 +08:00
if (!await popUp.PopUp()) // 开始弹窗
2025-04-11 17:38:09 +08:00
continue;
setCacheNeedRemovePopUp.Add(popUp); // 记录需要移除的弹窗
2025-04-22 15:28:05 +08:00
curUIName = uiName;
2025-04-11 17:38:09 +08:00
if (!popUp.IsSameTimePopUp) // 不能同时弹窗则跳出
break;
}
2025-04-11 17:38:09 +08:00
foreach (IPopUp popUp in setCacheNeedRemovePopUp)
{
setPopUp.Remove(popUp);
linkPopUp.Remove(popUp);
2025-04-11 17:38:09 +08:00
}
2025-04-11 12:47:46 +08:00
}
/// <summary>
/// UI打开完成
/// </summary>
/// <param name="uiName"></param>
private async void OnUIOpenByaAnimComplte(string uiName)
{
await UniTask.WaitUntil(() => { return !FunctionLockManager.Instance.IsPlayingUnlockAnim; });
TriggerPopUp(uiName);
}
2025-04-22 15:14:20 +08:00
private void OnUIClose(string uiName)
{
IPopUp popUp = null;
foreach (IPopUp temp in setCacheNeedRemovePopUp)
{
if (temp.PopUpUIName == uiName)
{
popUp = temp;
break;
}
}
setCacheNeedRemovePopUp.Remove(popUp);
2025-04-22 15:28:05 +08:00
if (setCacheNeedRemovePopUp.Count < 0)
TriggerPopUp(curUIName);
2025-04-22 15:14:20 +08:00
}
#endregion
2025-04-11 12:47:46 +08:00
}
}