Editor UI tree nodes for a file browser. FolderNode represents a filesystem directory, builds child FolderNode entries from subdirectories, invokes a callback when selected, and draws a folder icon and name. FileNode represents a file, stores mesh entry count, and draws the file name (with mesh count when >0) and tooltip.
using System;
using System.IO;
namespace ModelPro.Editor;
/// <summary>A tree node representing a folder on disk.</summary>
public class FolderNode : TreeNode<DirectoryInfo>
{
public string FullPath { get; }
Action<string> _onSelected;
public override string Name => System.IO.Path.GetFileName( FullPath );
public FolderNode( string fullPath, Action<string> onSelected ) : base( new DirectoryInfo( fullPath ) )
{
FullPath = fullPath;
_onSelected = onSelected;
Height = Theme.RowHeight;
}
protected override void BuildChildren()
{
Clear();
foreach ( var dir in Directory.GetDirectories( FullPath )
.OrderBy( x => x, StringComparer.OrdinalIgnoreCase ) )
{
AddItem( new FolderNode( dir, _onSelected ) );
}
}
public override void OnSelectionChanged( bool state )
{
if ( state )
_onSelected?.Invoke( FullPath );
}
public override void OnPaint( VirtualWidget item )
{
PaintSelection( item );
var rect = item.Rect;
Paint.SetPen( Theme.Yellow );
Paint.DrawIcon( rect, "folder", 18, TextFlag.LeftCenter );
rect.Left += 24;
Paint.SetPen( Theme.Text );
Paint.SetDefaultFont();
Paint.DrawText( rect, Name, TextFlag.LeftCenter );
}
public override string GetTooltip()
{
return FullPath;
}
}
/// <summary>A tree node representing a file.</summary>
public class FileNode : TreeNode<string>
{
public string FullPath { get; }
public int MeshEntryCount { get; }
public override string Name => System.IO.Path.GetFileName( FullPath );
public FileNode( string fullPath, int meshEntryCount ) : base( fullPath )
{
FullPath = fullPath;
MeshEntryCount = meshEntryCount;
Height = Theme.RowHeight;
}
public override void OnPaint( VirtualWidget item )
{
PaintSelection( item );
var rect = item.Rect;
Paint.SetPen( Theme.Text );
Paint.SetDefaultFont();
Paint.DrawText( rect, MeshEntryCount > 0 ? $"{Name} ({MeshEntryCount} meshes)" : Name, TextFlag.LeftCenter );
}
public override string GetTooltip()
{
return FullPath;
}
}