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

116 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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();
//该红点节点需要注册的事件,当事件发生时刷新数量
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)
{
_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.RefreshCount();
}
}
public IEnumerator GetEnumerator()
{
for (int i = _children.Count - 1; i >= 0; i--)
{
yield return _children[i];
}
}
}