add logic treenode
This commit is contained in:
parent
81d515e40b
commit
6f1dc3f05f
@ -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<TreeNode> _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<TreeItem, string> _nodeKeys = new ();
|
||||
private readonly Godot.Collections.Dictionary<TreeItem, string> _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<TreeNode> 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);
|
||||
|
||||
30
LearnTree.cs
30
LearnTree.cs
@ -12,8 +12,8 @@ public partial class LearnTree : Tree
|
||||
private ItemInfoPanel _itemInfoPanel;
|
||||
|
||||
private bool _updateItemInfoPanel;
|
||||
private readonly HashSet<Item> _selectingItems = new ();
|
||||
private readonly Dictionary<TreeItem, Item> _mapper = new();
|
||||
private readonly HashSet<TreeNode> _selectingNodes = new ();
|
||||
private readonly Dictionary<TreeItem, TreeNode> _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);
|
||||
|
||||
20
Models/TreeNode.cs
Normal file
20
Models/TreeNode.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Learn.Models;
|
||||
|
||||
public class TreeNode(Item item)
|
||||
{
|
||||
public TreeNode Parent => _parent;
|
||||
public IEnumerable<TreeNode> Children => _children;
|
||||
public Item Info => item;
|
||||
|
||||
private TreeNode _parent;
|
||||
private readonly List<TreeNode> _children = new();
|
||||
|
||||
public void AddNode(TreeNode node)
|
||||
{
|
||||
if (_children.Contains(node)) return;
|
||||
_children.Add(node);
|
||||
node._parent = this;
|
||||
}
|
||||
}
|
||||
1
Models/TreeNode.cs.uid
Normal file
1
Models/TreeNode.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://ddqh11wqftpvt
|
||||
Loading…
x
Reference in New Issue
Block a user