mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/PCK-Studio.git
synced 2026-05-22 11:45:10 +00:00
Core - Add TreeNodeCollectionExtensions.cs
This commit is contained in:
@@ -577,49 +577,11 @@ namespace PckStudio.Controls
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// wrapper that allows the use of <paramref name="name"/> in <code>TreeNode.Nodes.Find(<paramref name="name"/>, ...)</code> and <code>TreeNode.Nodes.ContainsKey(<paramref name="name"/>)</code>
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="tag"></param>
|
||||
/// <returns>new Created TreeNode</returns>
|
||||
private static TreeNode CreateNode(string name, object tag = null)
|
||||
{
|
||||
TreeNode node = new TreeNode(name);
|
||||
node.Name = name;
|
||||
node.Tag = tag;
|
||||
return node;
|
||||
}
|
||||
|
||||
private TreeNode BuildNodeTreeBySeperator(TreeNodeCollection root, string path, char seperator)
|
||||
{
|
||||
_ = root ?? throw new ArgumentNullException(nameof(root));
|
||||
if (!path.Contains(seperator))
|
||||
{
|
||||
TreeNode finalNode = CreateNode(path);
|
||||
root.Add(finalNode);
|
||||
return finalNode;
|
||||
}
|
||||
string nodeText = path.Substring(0, path.IndexOf(seperator));
|
||||
string subPath = path.Substring(path.IndexOf(seperator) + 1);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(nodeText))
|
||||
{
|
||||
return BuildNodeTreeBySeperator(root, subPath, seperator);
|
||||
}
|
||||
|
||||
bool alreadyExists = root.ContainsKey(nodeText);
|
||||
TreeNode subNode = alreadyExists ? root[nodeText] : CreateNode(nodeText);
|
||||
if (!alreadyExists)
|
||||
root.Add(subNode);
|
||||
return BuildNodeTreeBySeperator(subNode.Nodes, subPath, seperator);
|
||||
}
|
||||
|
||||
private void BuildPckTreeView(TreeNodeCollection root, PckFile pckFile)
|
||||
{
|
||||
foreach (PckAsset asset in pckFile.GetAssets())
|
||||
{
|
||||
TreeNode node = BuildNodeTreeBySeperator(root, asset.Filename, '/');
|
||||
TreeNode node = root.BuildNodeTreeBySeperator(asset.Filename, '/');
|
||||
node.Tag = asset;
|
||||
int nodeIconId = GetNodeIconId(asset.Type);
|
||||
node.ImageIndex = nodeIconId;
|
||||
|
||||
47
PckStudio.Core/Extensions/TreeNodeCollectionExtensions.cs
Normal file
47
PckStudio.Core/Extensions/TreeNodeCollectionExtensions.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace PckStudio.Core.Extensions
|
||||
{
|
||||
public static class TreeNodeCollectionExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// wrapper that allows the use of <paramref name="name"/> in <code>TreeNode.Nodes.Find(<paramref name="name"/>, ...)</code> and <code>TreeNode.Nodes.ContainsKey(<paramref name="name"/>)</code>
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="tag"></param>
|
||||
/// <returns>new Created TreeNode</returns>
|
||||
public static TreeNode CreateNode(this TreeNodeCollection root, string name, object tag = null)
|
||||
{
|
||||
TreeNode node = new TreeNode(name);
|
||||
node.Name = name;
|
||||
node.Tag = tag;
|
||||
root.Add(node);
|
||||
return node;
|
||||
}
|
||||
|
||||
public static TreeNode BuildNodeTreeBySeperator(this TreeNodeCollection root, string path, char seperator)
|
||||
{
|
||||
_ = root ?? throw new ArgumentNullException(nameof(root));
|
||||
if (!path.Contains(seperator))
|
||||
{
|
||||
return root.CreateNode(path);
|
||||
}
|
||||
string nodeText = path.Substring(0, path.IndexOf(seperator));
|
||||
string subPath = path.Substring(path.IndexOf(seperator) + 1);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(nodeText))
|
||||
{
|
||||
return BuildNodeTreeBySeperator(root, subPath, seperator);
|
||||
}
|
||||
|
||||
TreeNode subNode = root.ContainsKey(nodeText) ? root[nodeText] : root.CreateNode(nodeText);
|
||||
return BuildNodeTreeBySeperator(subNode.Nodes, subPath, seperator);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,6 +103,7 @@
|
||||
<Compile Include="Extensions\SkinBOXExtensions.cs" />
|
||||
<Compile Include="Extensions\SkinExtensions.cs" />
|
||||
<Compile Include="Extensions\System.Numerics.cs" />
|
||||
<Compile Include="Extensions\TreeNodeCollectionExtensions.cs" />
|
||||
<Compile Include="Extensions\TreeNodeExtensions.cs" />
|
||||
<Compile Include="Extensions\TreeViewExtensions.cs" />
|
||||
<Compile Include="FileDialogFilter.cs" />
|
||||
|
||||
Reference in New Issue
Block a user