add logic treenode
This commit is contained in:
parent
81d515e40b
commit
6f1dc3f05f
@ -1,11 +1,12 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Godot;
|
using Godot;
|
||||||
using Godot.Collections;
|
|
||||||
using Learn.Models;
|
using Learn.Models;
|
||||||
|
|
||||||
public partial class ItemInfoPanel : Tree
|
public partial class ItemInfoPanel : Tree
|
||||||
{
|
{
|
||||||
private ItemGroup _item;
|
private IEnumerable<TreeNode> _nodes;
|
||||||
|
private ItemGroup _itemGroup;
|
||||||
|
|
||||||
#region Columns Definitions
|
#region Columns Definitions
|
||||||
private const int EmptyColumn = 0;
|
private const int EmptyColumn = 0;
|
||||||
@ -31,7 +32,7 @@ public partial class ItemInfoPanel : Tree
|
|||||||
ItemEdited += OnItemEdited;
|
ItemEdited += OnItemEdited;
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly Dictionary<TreeItem, string> _nodeKeys = new ();
|
private readonly Godot.Collections.Dictionary<TreeItem, string> _nodeKeys = new ();
|
||||||
|
|
||||||
private void OnItemEdited()
|
private void OnItemEdited()
|
||||||
{
|
{
|
||||||
@ -41,14 +42,23 @@ public partial class ItemInfoPanel : Tree
|
|||||||
var value = node.GetText(ValueColumn);
|
var value = node.GetText(ValueColumn);
|
||||||
if (prevKey != currentKey)
|
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();
|
OnCurrentItemChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,11 +66,11 @@ public partial class ItemInfoPanel : Tree
|
|||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
_nodeKeys.Clear();
|
_nodeKeys.Clear();
|
||||||
if (_item == null) return;
|
if (_itemGroup == null) return;
|
||||||
var root = CreateItem();
|
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);
|
var node = CreateItem(root);
|
||||||
|
|
||||||
node.SetText(KeyColumn, key);
|
node.SetText(KeyColumn, key);
|
||||||
@ -73,10 +83,10 @@ public partial class ItemInfoPanel : Tree
|
|||||||
|
|
||||||
private void AddKeyValue()
|
private void AddKeyValue()
|
||||||
{
|
{
|
||||||
if(_item == null) return;
|
if(_itemGroup == null) return;
|
||||||
var newFieldName = "newField";
|
var newFieldName = "newField";
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (!_item.TryAdd(newFieldName + (index == 0 ? "" : index.ToString()), ""))
|
while (!_itemGroup.TryAdd(newFieldName + (index == 0 ? "" : index.ToString()), ""))
|
||||||
{
|
{
|
||||||
index += 1;
|
index += 1;
|
||||||
}
|
}
|
||||||
@ -85,13 +95,13 @@ public partial class ItemInfoPanel : Tree
|
|||||||
|
|
||||||
private void RemoveKeyValue()
|
private void RemoveKeyValue()
|
||||||
{
|
{
|
||||||
if(_item == null) return;
|
if(_itemGroup == null) return;
|
||||||
|
|
||||||
var node = GetSelected();
|
var node = GetSelected();
|
||||||
var root = GetRoot();
|
var root = GetRoot();
|
||||||
while (node != null)
|
while (node != null)
|
||||||
{
|
{
|
||||||
_item.Remove(_nodeKeys[node]);
|
_itemGroup.Remove(_nodeKeys[node]);
|
||||||
_nodeKeys.Remove(node);
|
_nodeKeys.Remove(node);
|
||||||
var next = GetNextSelected(node);
|
var next = GetNextSelected(node);
|
||||||
root.RemoveChild(node);
|
root.RemoveChild(node);
|
||||||
|
|||||||
30
LearnTree.cs
30
LearnTree.cs
@ -12,8 +12,8 @@ public partial class LearnTree : Tree
|
|||||||
private ItemInfoPanel _itemInfoPanel;
|
private ItemInfoPanel _itemInfoPanel;
|
||||||
|
|
||||||
private bool _updateItemInfoPanel;
|
private bool _updateItemInfoPanel;
|
||||||
private readonly HashSet<Item> _selectingItems = new ();
|
private readonly HashSet<TreeNode> _selectingNodes = new ();
|
||||||
private readonly Dictionary<TreeItem, Item> _mapper = new();
|
private readonly Dictionary<TreeItem, TreeNode> _mapper = new();
|
||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
@ -25,14 +25,14 @@ public partial class LearnTree : Tree
|
|||||||
if (_updateItemInfoPanel)
|
if (_updateItemInfoPanel)
|
||||||
{
|
{
|
||||||
_updateItemInfoPanel = false;
|
_updateItemInfoPanel = false;
|
||||||
_itemInfoPanel.SetCurrentItem(new ItemGroup(_selectingItems));
|
_itemInfoPanel.SetSelectedNodes(_selectingNodes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnMultiSelected(TreeItem item, long column, bool selected)
|
private void OnMultiSelected(TreeItem item, long column, bool selected)
|
||||||
{
|
{
|
||||||
if(selected) _selectingItems.Add(_mapper[item]);
|
if(selected) _selectingNodes.Add(_mapper[item]);
|
||||||
else _selectingItems.Remove(_mapper[item]);
|
else _selectingNodes.Remove(_mapper[item]);
|
||||||
|
|
||||||
_updateItemInfoPanel = true;
|
_updateItemInfoPanel = true;
|
||||||
}
|
}
|
||||||
@ -51,30 +51,38 @@ public partial class LearnTree : Tree
|
|||||||
var item = new Item();
|
var item = new Item();
|
||||||
item.Info["Name"] = name;
|
item.Info["Name"] = name;
|
||||||
item.Info["IsDir"] = isDir ? "True" : "False";
|
item.Info["IsDir"] = isDir ? "True" : "False";
|
||||||
|
item.Info["Path"] = path;
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void BuildTree(TreeItem root, string path)
|
private void BuildTree(TreeItem root, string path)
|
||||||
{
|
{
|
||||||
var nodes = new Stack<(TreeItem node, string path)>();
|
var nodes = new Stack<(TreeItem node, string path)>();
|
||||||
|
_mapper[root] = new TreeNode(null);
|
||||||
nodes.Push((root, path));
|
nodes.Push((root, path));
|
||||||
while (nodes.Count > 0)
|
while (nodes.Count > 0)
|
||||||
{
|
{
|
||||||
var node = nodes.Pop();
|
var node = nodes.Pop();
|
||||||
|
var father = node.node;
|
||||||
|
var fatherNode = _mapper[father];
|
||||||
|
|
||||||
foreach (var filePath in Directory.GetFiles(node.path))
|
foreach (var filePath in Directory.GetFiles(node.path))
|
||||||
{
|
{
|
||||||
var item = InitItem(filePath, false);
|
var item = InitItem(filePath, false);
|
||||||
var chNode = CreateNode(node.node, item);
|
var childNode = new TreeNode(item);
|
||||||
_mapper[chNode] = item;
|
fatherNode.AddNode(childNode);
|
||||||
|
var child = CreateNode(father, item);
|
||||||
|
_mapper[child] = childNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var subDirPath in Directory.GetDirectories(node.path))
|
foreach (var subDirPath in Directory.GetDirectories(node.path))
|
||||||
{
|
{
|
||||||
var item = InitItem(subDirPath, true);
|
var item = InitItem(subDirPath, true);
|
||||||
var chNode = CreateNode(node.node, item);
|
var childNode = new TreeNode(item);
|
||||||
_mapper[chNode] = item;
|
fatherNode.AddNode(childNode);
|
||||||
nodes.Push((chNode, subDirPath));
|
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;
|
if (string.IsNullOrEmpty(path)) return;
|
||||||
Clear();
|
Clear();
|
||||||
_mapper.Clear();
|
_mapper.Clear();
|
||||||
_selectingItems.Clear();
|
_selectingNodes.Clear();
|
||||||
|
|
||||||
TreeItem root = CreateItem();
|
TreeItem root = CreateItem();
|
||||||
BuildTree(root, path);
|
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