diff --git a/ItemInfoPanel.cs b/ItemInfoPanel.cs index 9a8a60c..0c0402d 100644 --- a/ItemInfoPanel.cs +++ b/ItemInfoPanel.cs @@ -1,11 +1,12 @@ +using System.Collections.Generic; using System.Linq; using Godot; -using Godot.Collections; using Learn.Models; public partial class ItemInfoPanel : Tree { - private ItemGroup _item; + private IEnumerable _nodes; + private ItemGroup _itemGroup; #region Columns Definitions private const int EmptyColumn = 0; @@ -31,7 +32,7 @@ public partial class ItemInfoPanel : Tree ItemEdited += OnItemEdited; } - private readonly Dictionary _nodeKeys = new (); + private readonly Godot.Collections.Dictionary _nodeKeys = new (); private void OnItemEdited() { @@ -41,14 +42,23 @@ public partial class ItemInfoPanel : Tree var value = node.GetText(ValueColumn); if (prevKey != currentKey) { - _item.Remove(prevKey); + _itemGroup.Remove(prevKey); } - _item.SetValue(currentKey, value); + _itemGroup.SetValue(currentKey, value); } - public void SetCurrentItem(ItemGroup item) + public void SetSelectedNodes(IEnumerable nodes) { - _item = item; + if (nodes == null) + { + _nodes = null; + _itemGroup = null; + } + else + { + _nodes = nodes; + _itemGroup = new ItemGroup(_nodes.Select(node => node.Info).ToList()); + } OnCurrentItemChanged(); } @@ -56,11 +66,11 @@ public partial class ItemInfoPanel : Tree { Clear(); _nodeKeys.Clear(); - if (_item == null) return; + if (_itemGroup == null) return; var root = CreateItem(); - foreach (var key in _item.GetKeys()) + foreach (var key in _itemGroup.GetKeys()) { - if(!_item.TryGetValue(key, out var value, out var isSame)) continue; + if(!_itemGroup.TryGetValue(key, out var value, out var isSame)) continue; var node = CreateItem(root); node.SetText(KeyColumn, key); @@ -73,10 +83,10 @@ public partial class ItemInfoPanel : Tree private void AddKeyValue() { - if(_item == null) return; + if(_itemGroup == null) return; var newFieldName = "newField"; int index = 0; - while (!_item.TryAdd(newFieldName + (index == 0 ? "" : index.ToString()), "")) + while (!_itemGroup.TryAdd(newFieldName + (index == 0 ? "" : index.ToString()), "")) { index += 1; } @@ -85,13 +95,13 @@ public partial class ItemInfoPanel : Tree private void RemoveKeyValue() { - if(_item == null) return; + if(_itemGroup == null) return; var node = GetSelected(); var root = GetRoot(); while (node != null) { - _item.Remove(_nodeKeys[node]); + _itemGroup.Remove(_nodeKeys[node]); _nodeKeys.Remove(node); var next = GetNextSelected(node); root.RemoveChild(node); diff --git a/LearnTree.cs b/LearnTree.cs index e5b1544..a13330b 100644 --- a/LearnTree.cs +++ b/LearnTree.cs @@ -12,8 +12,8 @@ public partial class LearnTree : Tree private ItemInfoPanel _itemInfoPanel; private bool _updateItemInfoPanel; - private readonly HashSet _selectingItems = new (); - private readonly Dictionary _mapper = new(); + private readonly HashSet _selectingNodes = new (); + private readonly Dictionary _mapper = new(); public override void _Ready() { @@ -25,14 +25,14 @@ public partial class LearnTree : Tree if (_updateItemInfoPanel) { _updateItemInfoPanel = false; - _itemInfoPanel.SetCurrentItem(new ItemGroup(_selectingItems)); + _itemInfoPanel.SetSelectedNodes(_selectingNodes); } } private void OnMultiSelected(TreeItem item, long column, bool selected) { - if(selected) _selectingItems.Add(_mapper[item]); - else _selectingItems.Remove(_mapper[item]); + if(selected) _selectingNodes.Add(_mapper[item]); + else _selectingNodes.Remove(_mapper[item]); _updateItemInfoPanel = true; } @@ -51,30 +51,38 @@ public partial class LearnTree : Tree var item = new Item(); item.Info["Name"] = name; item.Info["IsDir"] = isDir ? "True" : "False"; + item.Info["Path"] = path; return item; } private void BuildTree(TreeItem root, string path) { var nodes = new Stack<(TreeItem node, string path)>(); + _mapper[root] = new TreeNode(null); nodes.Push((root, path)); while (nodes.Count > 0) { var node = nodes.Pop(); + var father = node.node; + var fatherNode = _mapper[father]; foreach (var filePath in Directory.GetFiles(node.path)) { var item = InitItem(filePath, false); - var chNode = CreateNode(node.node, item); - _mapper[chNode] = item; + var childNode = new TreeNode(item); + fatherNode.AddNode(childNode); + var child = CreateNode(father, item); + _mapper[child] = childNode; } foreach (var subDirPath in Directory.GetDirectories(node.path)) { var item = InitItem(subDirPath, true); - var chNode = CreateNode(node.node, item); - _mapper[chNode] = item; - nodes.Push((chNode, subDirPath)); + var childNode = new TreeNode(item); + fatherNode.AddNode(childNode); + var child = CreateNode(father, item); + _mapper[child] = childNode; + nodes.Push((child, subDirPath)); } } } @@ -85,7 +93,7 @@ public partial class LearnTree : Tree if (string.IsNullOrEmpty(path)) return; Clear(); _mapper.Clear(); - _selectingItems.Clear(); + _selectingNodes.Clear(); TreeItem root = CreateItem(); BuildTree(root, path); diff --git a/Models/TreeNode.cs b/Models/TreeNode.cs new file mode 100644 index 0000000..ddfc786 --- /dev/null +++ b/Models/TreeNode.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; + +namespace Learn.Models; + +public class TreeNode(Item item) +{ + public TreeNode Parent => _parent; + public IEnumerable Children => _children; + public Item Info => item; + + private TreeNode _parent; + private readonly List _children = new(); + + public void AddNode(TreeNode node) + { + if (_children.Contains(node)) return; + _children.Add(node); + node._parent = this; + } +} \ No newline at end of file diff --git a/Models/TreeNode.cs.uid b/Models/TreeNode.cs.uid new file mode 100644 index 0000000..28d34d8 --- /dev/null +++ b/Models/TreeNode.cs.uid @@ -0,0 +1 @@ +uid://ddqh11wqftpvt