Tree
See the TreeDemo class.
public void Draw()
{
Gui.Heading("Scroll");
{
Gui.NextHeight(Size.Fixed(150f));
// Render a scrollable tree.
if (Gui.BeginScroll())
{
//note:By holding down the Alt key while clicking Foldout, you can open / close all child elements.
Gui.Tree(scrollRoots, new(nodeDrawer, scrollSelection, nodeHandler), Ctx.GetWidgetRect());
Gui.EndScroll();
}
}
Gui.Heading("FixedScrollTree");
{
// Render a fixed-size scrollable tree.
Gui.FixedScrollTree(150, fixedScrollRoots, new(nodeDrawer, fixedScrollSelection, nodeHandler));
}
}
/// <summary>
/// Represents a node in the tree structure.
/// </summary>
class Node
{
public bool IsCheck { get; set; }
public string Name { get; set; }
public List<Node> Children { get; } = new();
public bool HasChild => 0 < Children.Count;
}
/// <summary>
/// Handles rendering of tree nodes.
/// </summary>
class NodeDrawer : ITreeNodeDrawer<Node>
{
readonly Gui ui;
public NodeDrawer(Gui ui)
{
this.ui = ui;
}
/// <summary>
/// Draws a tree node.
/// </summary>
/// <param name="node">The node to draw.</param>
/// <param name="rect">The rectangle area allocated for the node.</param>
public void Draw(Node node, Rect rect)
{
using var rects = ui.RectsBuilder.Fixed(rect.height).Fit(1).BuildHorizontal(rect);
var isCheck = node.IsCheck;
ui.NextRect(rects.R0).CheckBox(ref isCheck);
node.IsCheck = isCheck;
ui.NextRect(rects.R1).Text(node.Name);
}
}
/// <summary>
/// Handles tree node interactions, such as expanding and selecting nodes.
/// </summary>
class NodeHandler : NodeHandlerBase<Node>
{
public NodeHandler(InputContext input) : base(input)
{
}
public override IEnumerable<Node> GetChildren(Node node) => node.Children;
}