88 lines
2.0 KiB
C#
88 lines
2.0 KiB
C#
using Framework;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using static Framework.EventManager;
|
||
|
||
public abstract class RedPointNode
|
||
{
|
||
private bool _isDisposed;
|
||
|
||
protected List<RedPointNode> _children = new();
|
||
//该红点节点需要注册的事件,当事件发生时通知UI刷新数据
|
||
protected abstract EventName[] eventsToRegister { get; }
|
||
//消息数目要重载,去算各自系统对应的消息数
|
||
public abstract int MessageCount { get; }
|
||
public int ID { get; private set; }
|
||
public RedPointNode parent { get; set; }
|
||
public bool Valid => !_isDisposed;
|
||
|
||
protected RedPointNode(int id)
|
||
{
|
||
ID = id;
|
||
}
|
||
|
||
public void AddChild(RedPointNode child)
|
||
{
|
||
_children.Add(child);
|
||
}
|
||
|
||
public void RemoveChild(RedPointNode child)
|
||
{
|
||
_children.Remove(child);
|
||
}
|
||
|
||
public void Init()
|
||
{
|
||
RegisterEvents();
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
UnRegisterEvents();
|
||
_children = null;
|
||
parent = null;
|
||
_isDisposed = true;
|
||
}
|
||
|
||
//如果节点有特殊信号处理的,重写该函数
|
||
protected virtual void RegisterEvents()
|
||
{
|
||
if (eventsToRegister != null)
|
||
{
|
||
foreach (var eveName in eventsToRegister)
|
||
{
|
||
EventManager.Instance.Register(eveName, SendChange);
|
||
}
|
||
}
|
||
}
|
||
|
||
//如果节点有特殊信号处理的,重写该函数
|
||
protected virtual void UnRegisterEvents()
|
||
{
|
||
if (eventsToRegister != null)
|
||
{
|
||
foreach (var eveName in eventsToRegister)
|
||
{
|
||
EventManager.Instance.Unregister(eveName, SendChange);
|
||
}
|
||
}
|
||
}
|
||
|
||
public void ForeachChild(Action<RedPointNode> func)
|
||
{
|
||
for (int i = _children.Count - 1; i >= 0; i--)
|
||
{
|
||
func(_children[i]);
|
||
}
|
||
}
|
||
|
||
public void SendChange()
|
||
{
|
||
EventManager.Instance.Send(EventName.RedPointDataChange, this);
|
||
if (parent != null)
|
||
{
|
||
parent.SendChange();
|
||
}
|
||
}
|
||
}
|