mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/PCK-Studio.git
synced 2026-05-28 20:44:34 +00:00
Move Common functionality to Core project & rendering and Model support as well
This commit is contained in:
47
PckStudio.Core/Extensions/TreeNodeExtensions.cs
Normal file
47
PckStudio.Core/Extensions/TreeNodeExtensions.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace PckStudio.Core.Extensions
|
||||
{
|
||||
public static class TreeNodeExtensions
|
||||
{
|
||||
public static bool IsTagOfType<T>(this TreeNode node) where T : class
|
||||
{
|
||||
return node?.Tag is T;
|
||||
}
|
||||
|
||||
public static bool TryGetTagData<TOut>(this TreeNode node, out TOut tagData) where TOut : class
|
||||
{
|
||||
if (node?.Tag is TOut data)
|
||||
{
|
||||
tagData = data;
|
||||
return true;
|
||||
}
|
||||
tagData = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool Contains(this TreeNode thisNode, TreeNode childNode)
|
||||
{
|
||||
if (childNode.Parent == null)
|
||||
return false;
|
||||
if (thisNode.Equals(childNode.Parent))
|
||||
return true;
|
||||
// If the parent node is not equal to the first node,
|
||||
// call the TreeNode.Contains recursively using the parent of the node.
|
||||
return thisNode.Contains(childNode.Parent);
|
||||
}
|
||||
|
||||
public static List<TreeNode> GetChildNodes(this TreeNode thisNode)
|
||||
{
|
||||
List<TreeNode> nodes = new List<TreeNode>(thisNode.Nodes.Count);
|
||||
foreach (TreeNode node in thisNode.Nodes)
|
||||
{
|
||||
nodes.Add(node);
|
||||
nodes.AddRange(node.GetChildNodes());
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user