NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/RedPoint/RedPointNode.cs

125 lines
3.0 KiB
C#
Raw Normal View History

2023-10-19 15:00:19 +08:00
using Framework;
using System.Collections;
2023-10-19 15:00:19 +08:00
using System.Collections.Generic;
using System.Linq;
using Framework.Condition;
2023-10-19 15:00:19 +08:00
using static Framework.EventManager;
public abstract class RedPointNode : IEnumerable
2023-10-19 15:00:19 +08:00
{
private bool _isDisposed;
private EventName[] _eventsToRegister;
private int _reactionID;
2023-10-19 15:00:19 +08:00
protected List<RedPointNode> _children = new();
public int ChildCount => _children.Count;
//该红点节点需要注册的事件,当事件发生时刷新数量
2023-10-19 15:00:19 +08:00
protected abstract EventName[] eventsToRegister { get; }
//数目要override去算各自系统对应的消息数如果是叶子节点直接返回消息数如果是父节点返回所有子节点的消息数之和
protected abstract int GetCount();
public int MessageCount { get; private set; }
2023-10-19 15:00:19 +08:00
public int ID { get; private set; }
public bool IsDynamic => ID > RedPointManager.RED_POINT_NODE_DYNAMIC_ID_START;
2023-10-19 15:00:19 +08:00
public RedPointNode parent { get; set; }
public bool Valid => !_isDisposed;
public object param { get; set; }
2023-10-19 15:00:19 +08:00
protected RedPointNode(int id)
{
ID = id;
}
public void AddChild(RedPointNode child)
{
if (child.ID != ID)
_children.Add(child);
2023-10-19 15:00:19 +08:00
}
public void RemoveChild(RedPointNode child)
{
_children.Remove(child);
}
public void Init()
{
_eventsToRegister = eventsToRegister;
RegisterSignal();
RefreshCount();
2023-10-19 15:00:19 +08:00
}
public void Dispose()
{
if (!_isDisposed)
{
UnRegisterSignal();
_children = null;
parent = null;
_isDisposed = true;
SendChange();
}
2023-10-19 15:00:19 +08:00
}
private void RegisterSignal()
2023-10-19 15:00:19 +08:00
{
if (_eventsToRegister != null)
_reactionID = ConditionReactor.Instance.AddReaction(Refresh, _eventsToRegister.ToList(), OnRefreshFinish);
}
private void UnRegisterSignal()
{
if (_eventsToRegister != null)
ConditionReactor.Instance.RemoveReaction(_reactionID);
}
private bool Refresh(object arg = null)
{
var oldCount = MessageCount;
MessageCount = GetCount();
return MessageCount != oldCount;
2023-10-19 15:00:19 +08:00
}
private void OnRefreshFinish(bool result, object callBackParam, int id)
2023-10-19 15:00:19 +08:00
{
if (result)
2023-10-19 15:00:19 +08:00
{
SendChange();
2023-10-19 15:00:19 +08:00
}
}
public void RefreshCount()
2023-10-19 15:00:19 +08:00
{
if (Refresh())
2023-10-19 15:00:19 +08:00
{
SendChange();
2023-10-19 15:00:19 +08:00
}
}
private void SendChange()
2023-10-19 15:00:19 +08:00
{
EventManager.Instance.Send(EventName.RedPointDataChange, this);
if (parent != null && parent.ID != ID)
2023-10-19 15:00:19 +08:00
{
parent.RefreshCount();
}
}
public IEnumerator GetEnumerator()
{
for (int i = _children.Count - 1; i >= 0; i--)
{
yield return _children[i];
2023-10-19 15:00:19 +08:00
}
}
}
public interface IRedPointTreeNode
{
public void ConstructRedPointTree(int parentID);
public void ConstructRedPointTree(RedPointNode parent);
}