yyl
2025-11-20 9e89e605d5429babb4b33df2e47ea86dff9d2ba7
Main/System/Redpoint/RedpointCenter.cs
@@ -3,132 +3,187 @@
using UnityEngine;
using System;
namespace vnxbqy.UI
public class RedpointCenter : Singleton<RedpointCenter>
{
    public class RedpointCenter : Singleton<RedpointCenter>
    public event Action<int> redpointValueChangeEvent;
    Dictionary<int, Redpoint> redpoints = new Dictionary<int, Redpoint>();
    Dictionary<int, List<int>> parentChildren = new Dictionary<int, List<int>>();
    public RedpointCenter()
    {
        public event Action<int> redpointValueChangeEvent;
        Dictionary<int, Redpoint> redpoints = new Dictionary<int, Redpoint>();
        Dictionary<int, List<int>> parentChildren = new Dictionary<int, List<int>>();
        public RedpointCenter()
        {
        }
        public void ResetAllRedpointState()
        {
            foreach (var redpoint in redpoints.Values)
            {
                redpoint.state = RedPointState.None;
            }
        }
        public void Register(Redpoint _redpoint)
        {
            var id = _redpoint.id;
            if (redpoints.ContainsKey(id))
            {
                Debug.LogErrorFormat("重复注册红点:id->{0}", id);
            }
            redpoints[id] = _redpoint;
            var parentId = _redpoint.parent;
            if (parentId > 0)
            {
                List<int> children = null;
                if (!parentChildren.TryGetValue(parentId, out children))
                {
                    parentChildren[parentId] = children = new List<int>();
                }
                if (!children.Contains(id))
                {
                    children.Add(id);
                }
            }
        }
        public void Report(Redpoint _redpoint)
        {
            var parentId = _redpoint.parent;
            if (redpointValueChangeEvent != null)
            {
                redpointValueChangeEvent(_redpoint.id);
            }
            if (parentId > 0)
            {
                TestParentValue(_redpoint.parent);
            }
        }
        public void ReportWithoutParent(Redpoint _redpoint)
        {
            if (redpointValueChangeEvent != null)
            {
                redpointValueChangeEvent(_redpoint.id);
            }
        }
        public RedPointState GetRedpointState(int _id)
        {
            Redpoint redpoint = null;
            if (this.redpoints.TryGetValue(_id, out redpoint))
            {
                return redpoint.state;
            }
            else
            {
                return RedPointState.None;
            }
        }
        public int GetRedpointCount(int _id)
        {
            Redpoint redpoint = null;
            if (this.redpoints.TryGetValue(_id, out redpoint))
            {
                return redpoint.count;
            }
            else
            {
                return 0;
            }
        }
        void TestParentValue(int _parentId)
        {
            List<int> children = null;
            Redpoint parent = null;
            if (redpoints.TryGetValue(_parentId, out parent) && parentChildren.TryGetValue(_parentId, out children))
            {
                var parentState = RedPointState.None;
                for (int i = 0; i < children.Count; i++)
                {
                    Redpoint child = null;
                    if (redpoints.TryGetValue(children[i], out child))
                    {
                        if (child.state > parentState)
                        {
                            parentState = child.state;
                        }
                    }
                    if (parentState == RedPointState.Full)
                    {
                        break;
                    }
                }
                parent.state = parentState;
            }
        }
    }
}
    public void ResetAllRedpointState()
    {
        foreach (var redpoint in redpoints.Values)
        {
            redpoint.state = RedPointState.None;
        }
        RemoverOtherParent();
    }
    public void Register(Redpoint _redpoint)
    {
        var id = _redpoint.id;
        if (redpoints.ContainsKey(id))
        {
            Debug.LogErrorFormat("重复注册红点:id->{0}", id);
        }
        redpoints[id] = _redpoint;
        var parentId = _redpoint.parent;
        if (parentId > 0)
        {
            List<int> children = null;
            if (!parentChildren.TryGetValue(parentId, out children))
            {
                parentChildren[parentId] = children = new List<int>();
            }
            if (!children.Contains(id))
            {
                children.Add(id);
            }
        }
    }
    //额外添加父红点用,会屏蔽重复添加,外层可直接重复调用
    public void AddParent(int _parentId, int _childId)
    {
        Redpoint child = null;
        if (!redpoints.TryGetValue(_childId, out child))
        {
            Debug.LogErrorFormat("添加父子关系失败,子节点不存在:id->{0}", _childId);
            return;
        }
        if (child.otherParent > 0)
        {
            //不重复添加
            return;
        }
        child.otherParent = _parentId;
        List<int> children = null;
        if (!parentChildren.TryGetValue(_parentId, out children))
        {
            parentChildren[_parentId] = children = new List<int>();
        }
        if (!children.Contains(_childId))
        {
            children.Add(_childId);
        }
    }
    public void RemoverOtherParent()
    {
        List<int> otherParents = new List<int>();
        foreach (var redpoint in redpoints.Values)
        {
            if (redpoint.otherParent > 0)
            {
                otherParents.Add(redpoint.otherParent);
            }
            redpoint.otherParent = 0;
        }
        foreach (var parentId in otherParents)
        {
            parentChildren.Remove(parentId);
        }
    }
    public void Report(Redpoint _redpoint)
    {
        if (redpointValueChangeEvent != null)
        {
            redpointValueChangeEvent(_redpoint.id);
        }
        if (_redpoint.parent > 0)
        {
            TestParentValue(_redpoint.parent);
        }
        if (_redpoint.otherParent > 0)
        {
            TestParentValue(_redpoint.otherParent);
        }
    }
    public void ReportWithoutParent(Redpoint _redpoint)
    {
        if (redpointValueChangeEvent != null)
        {
            redpointValueChangeEvent(_redpoint.id);
        }
    }
    public RedPointState GetRedpointState(int _id)
    {
        Redpoint redpoint = null;
        if (this.redpoints.TryGetValue(_id, out redpoint))
        {
            return redpoint.state;
        }
        else
        {
            return RedPointState.None;
        }
    }
    public int GetRedpointCount(int _id)
    {
        Redpoint redpoint = null;
        if (this.redpoints.TryGetValue(_id, out redpoint))
        {
            return redpoint.count;
        }
        else
        {
            return 0;
        }
    }
    void TestParentValue(int _parentId)
    {
        List<int> children = null;
        Redpoint parent = null;
        if (redpoints.TryGetValue(_parentId, out parent) && parentChildren.TryGetValue(_parentId, out children))
        {
            var parentState = RedPointState.None;
            for (int i = 0; i < children.Count; i++)
            {
                Redpoint child = null;
                if (redpoints.TryGetValue(children[i], out child))
                {
                    if (child.state > parentState)
                    {
                        parentState = child.state;
                    }
                }
                if (parentState == RedPointState.Full)
                {
                    break;
                }
            }
            parent.state = parentState;
        }
    }
    public Redpoint GetRedpoint(int _id)
    {
        Redpoint redpoint = null;
        if (this.redpoints.TryGetValue(_id, out redpoint))
        {
            return redpoint;
        }
        return null;
    }
}