PckNodeSorter - Updated node sorting

This commit is contained in:
miku-666
2023-08-06 10:41:22 +02:00
parent 3a68ba50b9
commit 66e13bf9a9

View File

@@ -1,42 +1,56 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using OMI.Formats.Pck;
namespace PckStudio
{
public class PckNodeSorter : System.Collections.IComparer, IComparer<TreeNode>
public class PckNodeSorter : IComparer, IComparer<TreeNode>
{
private bool CheckForSkinAndCapeFiles(TreeNode node)
{
if (node.Tag is PckFile.FileData file)
if (IsPckFile(node, out PckFile.FileData file))
return file.Filetype == PckFile.FileData.FileType.SkinFile || file.Filetype == PckFile.FileData.FileType.CapeFile;
return false;
}
private bool IsPckFile(TreeNode node) => IsPckFile(node, out _);
private bool IsPckFile(TreeNode node, out PckFile.FileData file)
{
if (node.Tag is PckFile.FileData _file)
{
return file.Filetype == PckFile.FileData.FileType.SkinFile ||
file.Filetype == PckFile.FileData.FileType.CapeFile;
file = _file;
return true;
}
file = null;
return false;
}
public int Compare(TreeNode first, TreeNode second)
{
// ignore these files in order to preserve skin(and cape) files
if (CheckForSkinAndCapeFiles(first))
{
return 0;
}
if (CheckForSkinAndCapeFiles(second))
{
return 0;
}
if (IsPckFile(first) && !IsPckFile(second))
return -1;
if (!IsPckFile(first) && IsPckFile(second))
return 1;
int result = first.Text.CompareTo(second.Text);
if (result != 0) return result;
return first.ImageIndex.CompareTo(second.ImageIndex);
if (CheckForSkinAndCapeFiles(first))
return -1;
if (CheckForSkinAndCapeFiles(second))
return 1;
return first.Text.CompareTo(second.Text);
// weird fail save
//return first.ImageIndex.CompareTo(second.ImageIndex);
}
int System.Collections.IComparer.Compare(object x, object y)
int IComparer.Compare(object x, object y)
{
return x is TreeNode NodeX && y is TreeNode NodeY ? Compare(NodeX, NodeY) : 0;
if (x is not TreeNode NodeX)
return -1;
if (y is not TreeNode NodeY)
return 1;
return Compare(NodeX, NodeY);
}
}
}