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