2023-10-19 15:00:19 +08:00
|
|
|
|
using Framework;
|
2023-11-02 20:02:10 +08:00
|
|
|
|
using System.Collections;
|
2023-10-19 15:00:19 +08:00
|
|
|
|
using System.Collections.Generic;
|
2023-11-02 20:02:10 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Framework.Condition;
|
2023-10-19 15:00:19 +08:00
|
|
|
|
using static Framework.EventManager;
|
|
|
|
|
|
|
2023-11-02 20:02:10 +08:00
|
|
|
|
public abstract class RedPointNode : IEnumerable
|
2023-10-19 15:00:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
private bool _isDisposed;
|
|
|
|
|
|
|
2023-11-02 20:02:10 +08:00
|
|
|
|
private EventName[] _eventsToRegister;
|
|
|
|
|
|
|
|
|
|
|
|
private int _reactionID;
|
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
|
protected List<RedPointNode> _children = new();
|
2024-06-28 17:16:23 +08:00
|
|
|
|
public int ChildCount => _children.Count;
|
2023-11-02 20:02:10 +08:00
|
|
|
|
|
|
|
|
|
|
//该红点节点需要注册的事件,当事件发生时刷新数量
|
2023-10-19 15:00:19 +08:00
|
|
|
|
protected abstract EventName[] eventsToRegister { get; }
|
2023-11-02 20:02:10 +08:00
|
|
|
|
|
2024-06-27 18:55:53 +08:00
|
|
|
|
//数目要override,去算各自系统对应的消息数,如果是叶子节点,直接返回消息数,如果是父节点,返回所有子节点的消息数之和
|
2023-11-02 20:02:10 +08:00
|
|
|
|
protected abstract int GetCount();
|
|
|
|
|
|
public int MessageCount { get; private set; }
|
2023-10-19 15:00:19 +08:00
|
|
|
|
public int ID { get; private set; }
|
2023-11-02 20:02:10 +08:00
|
|
|
|
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;
|
|
|
|
|
|
|
2023-11-02 20:02:10 +08:00
|
|
|
|
public object param { get; set; }
|
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
|
protected RedPointNode(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
ID = id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AddChild(RedPointNode child)
|
|
|
|
|
|
{
|
2024-06-28 17:16:23 +08:00
|
|
|
|
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()
|
|
|
|
|
|
{
|
2023-11-02 20:02:10 +08:00
|
|
|
|
_eventsToRegister = eventsToRegister;
|
|
|
|
|
|
RegisterSignal();
|
|
|
|
|
|
RefreshCount();
|
2023-10-19 15:00:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
2023-11-02 20:02:10 +08:00
|
|
|
|
if (!_isDisposed)
|
|
|
|
|
|
{
|
|
|
|
|
|
UnRegisterSignal();
|
|
|
|
|
|
_children = null;
|
|
|
|
|
|
parent = null;
|
|
|
|
|
|
_isDisposed = true;
|
|
|
|
|
|
SendChange();
|
|
|
|
|
|
}
|
2023-10-19 15:00:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-02 20:02:10 +08:00
|
|
|
|
private void RegisterSignal()
|
2023-10-19 15:00:19 +08:00
|
|
|
|
{
|
2023-11-02 20:02:10 +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
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-02 20:02:10 +08:00
|
|
|
|
private void OnRefreshFinish(bool result, object callBackParam, int id)
|
2023-10-19 15:00:19 +08:00
|
|
|
|
{
|
2023-11-02 20:02:10 +08:00
|
|
|
|
if (result)
|
2023-10-19 15:00:19 +08:00
|
|
|
|
{
|
2023-11-02 20:02:10 +08:00
|
|
|
|
SendChange();
|
2023-10-19 15:00:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-02 20:02:10 +08:00
|
|
|
|
public void RefreshCount()
|
2023-10-19 15:00:19 +08:00
|
|
|
|
{
|
2023-11-02 20:02:10 +08:00
|
|
|
|
if (Refresh())
|
2023-10-19 15:00:19 +08:00
|
|
|
|
{
|
2023-11-02 20:02:10 +08:00
|
|
|
|
SendChange();
|
2023-10-19 15:00:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-02 20:02:10 +08:00
|
|
|
|
private void SendChange()
|
2023-10-19 15:00:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
EventManager.Instance.Send(EventName.RedPointDataChange, this);
|
2024-06-27 18:55:53 +08:00
|
|
|
|
if (parent != null && parent.ID != ID)
|
2023-10-19 15:00:19 +08:00
|
|
|
|
{
|
2023-11-02 20:02:10 +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
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-06-27 18:55:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public interface IRedPointTreeNode
|
|
|
|
|
|
{
|
|
|
|
|
|
public void ConstructRedPointTree(int parentID);
|
|
|
|
|
|
|
|
|
|
|
|
public void ConstructRedPointTree(RedPointNode parent);
|
2023-11-02 20:02:10 +08:00
|
|
|
|
}
|