/// <summary> /// 标记脏节点 /// </summary> public void MarkDirtyNode(RedDotTreeNode node) { if (node == null || node.Name == Root.Name) { return; } _dirtyNodes.Add(node); }
public override void OnSingletonInit() { base.OnSingletonInit(); _allNodes = new Dictionary <string, RedDotTreeNode>(); _dirtyNodes = new HashSet <RedDotTreeNode>(); _tempDirtyNodes = new List <RedDotTreeNode>(); CachedStringBuilder = new StringBuilder(); Root = new RedDotTreeNode("Root"); Separator = '/'; }
/// <summary> /// 获取子节点,如果不存在则添加 /// </summary> public RedDotTreeNode GetOrAddChild(RangeString key) { RedDotTreeNode child = GetChild(key); if (child == null) { child = AddChild(key); } return(child); }
/// <summary> /// 移除节点值监听 /// </summary> public void RemoveListener(string path, Action <int> callback) { if (callback == null) { return; } RedDotTreeNode node = GetTreeNode(path); node.RemoveListener(callback); }
/// <summary> /// 获取节点值 /// </summary> public int GetValue(string path) { RedDotTreeNode node = GetTreeNode(path); if (node == null) { return(0); } return(node.Value); }
/// <summary> /// 添加节点值监听 /// </summary> public RedDotTreeNode AddListener(string path, Action <int> callback) { if (callback == null) { return(null); } RedDotTreeNode node = GetTreeNode(path); node.AddListener(callback); return(node); }
/// <summary> /// 移除节点 /// </summary> public bool RemoveTreeNode(string path) { if (!_allNodes.ContainsKey(path)) { return(false); } RedDotTreeNode node = GetTreeNode(path); _allNodes.Remove(path); return(node.Parent.RemoveChild(new RangeString(node.Name, 0, node.Name.Length - 1))); }
/// <summary> /// 添加子节点 /// </summary> public RedDotTreeNode AddChild(RangeString key) { if (_children == null) { _children = new Dictionary <RangeString, RedDotTreeNode>(); } else if (_children.ContainsKey(key)) { throw new Exception("子节点添加失败,不允许重复添加:" + FullPath); } RedDotTreeNode child = new RedDotTreeNode(key.ToString(), this); _children.Add(key, child); RedDotMananger.Instance.onNodeNumChangedCallback?.Invoke(); return(child); }
/// <summary> /// 获取节点 /// </summary> public RedDotTreeNode GetTreeNode(string path) { if (string.IsNullOrEmpty(path)) { throw new Exception("路径不合法,不能为空"); } if (_allNodes.TryGetValue(path, out RedDotTreeNode node)) { return(node); } RedDotTreeNode current = Root; int length = path.Length; int startIndex = 0; for (int i = 0; i < length; i++) { if (path[i] == Separator) { if (i == length - 1) { throw new Exception("路径不合法,不能以路径分隔符结尾:" + path); } int endIndex = i - 1; if (endIndex < startIndex) { throw new Exception("路径不合法,不能存在连续的路径分隔符或以路径分隔符开头:" + path); } RedDotTreeNode child = current.GetOrAddChild(new RangeString(path, startIndex, endIndex)); //更新startIndex startIndex = i + 1; current = child; } } //最后一个节点(直接用length - 1作为endIndex) RedDotTreeNode target = current.GetOrAddChild(new RangeString(path, startIndex, length - 1)); _allNodes.Add(path, target); return(target); }
/// <summary> /// 移除子节点 /// </summary> public bool RemoveChild(RangeString key) { if (_children == null || _children.Count == 0) { return(false); } RedDotTreeNode child = GetChild(key); if (child != null) { //子节点被删除 需要进行一次父节点刷新 RedDotMananger.Instance.MarkDirtyNode(this); _children.Remove(key); RedDotMananger.Instance.onNodeNumChangedCallback?.Invoke(); return(true); } return(false); }
public RedDotTreeNode(string name, RedDotTreeNode parent) : this(name) { Parent = parent; }
/// <summary> /// 改变节点值 /// </summary> public void ChangeValue(string path, int newValue) { RedDotTreeNode node = GetTreeNode(path); node.ChangeValue(newValue); }
/// <summary> /// 移除所有节点值监听 /// </summary> public void RemoveAllListener(string path) { RedDotTreeNode node = GetTreeNode(path); node.RemoveAllListener(); }