Splitter
See the SplitterDemo class.
// Horizontal sizes and minimum sizes for the split containers
float horizontalSize1 = 100f;
float horizontalSize2 = 100f;
float horizontalMinSize1 = 30f;
float horizontalMinSize2 = 10f;
// Vertical sizes and minimum sizes for the split containers
float verticalSize1 = 100f;
float verticalSize2 = 100f;
float verticalMinSize1 = 20f;
float verticalMinSize2 = 20f;
// Widths for horizontal split containers (for demonstration)
readonly List<float> widths = new() { 50f, 50f, 50f };
readonly List<float> tempWidths = new();
/// <summary>
/// Demonstrates the use of splitters for resizing widgets horizontally.
/// Provides a minimum size input for each side and allows resizing of two widgets horizontally.
/// </summary>
void DrawByHorizontal()
{
Gui.Heading("Axis.Horizontal");
// Inputs for the minimum size of the horizontal split areas
Gui.LabelInputNumeric("MinSize1", ref horizontalMinSize1);
Gui.LabelInputNumeric("MinSize2", ref horizontalMinSize2);
using (Style.SpacingXs.Begin(0f))
{
// Allocate space for the two widgets and the splitter between them
using var rects2 = RectsBuilder
.FixedNonScale(horizontalSize1)
// Space for the splitter
.Fixed(1f)
.FixedUnscaled(horizontalSize2)
.BuildAllocatedHorizontal(horizontalSize1 + 1f * ScaleFactor + horizontalSize2, 50f);
// Create the splitter that resizes the two widgets horizontally
var result2 = Gui.Splitter(Axis.Horizontal, rects2.R1, horizontalSize1, horizontalSize2, horizontalMinSize1, horizontalMinSize2);
horizontalSize1 = result2.size1;
horizontalSize2 = result2.size2;
Gui.Frame(rects2.Whole);
}
}
/// <summary>
/// Demonstrates the use of splitters for resizing widgets vertically.
/// Provides a minimum size input for each side and allows resizing of two widgets vertically.
/// </summary>
void DrawByVertical()
{
Gui.Heading("Axis.Vertical");
// Inputs for the minimum size of the vertical split areas
Gui.LabelInputNumeric("MinSize1", ref verticalMinSize1);
Gui.LabelInputNumeric("MinSize2", ref verticalMinSize2);
// note:Since drawing with SpacingY set to 0 does not add space, explicitly add space.
Ctx.AddSpaceIfNeeded();
using (Style.SpacingYs.Begin(0f))
{
// Allocate space for the two widgets and the vertical splitter between them
using var rects = RectsBuilder
.FixedNonScale(verticalSize1)
// Space for the splitter
.Fixed(1f)
.FixedUnscaled(verticalSize2)
.BuildAllocatedVertical(height: verticalSize1 + 1f * ScaleFactor + verticalSize2);
// Create the splitter that resizes the two widgets vertically
var result = Gui.Splitter(Axis.Vertical, rects.R1, verticalSize1, verticalSize2, verticalMinSize1, verticalMinSize2);
verticalSize1 = result.size1;
verticalSize2 = result.size2;
Gui.Frame(rects.Whole);
}
}
/// <summary>
/// Demonstrates the use of splitters in a list of horizontally aligned elements.
/// Allows dynamic resizing of individual items in a horizontal list using splitters.
/// </summary>
void DrawLists()
{
Gui.Heading("Axis.Horizontal 2");
using (Ctx.Horizontal())
{
// Temporary list to store modified widths during this frame
tempWidths.ReplaceAll(widths);
// note:The position of the text and splitter is determined using the width from the previous frame.
// Using the width modified by operations in the current frame would mean altering a finalized value, which is not allowed.
for (int i = 0; i < widths.Count; i++)
{
using var s = Ctx.PushId(i);
// Draw the text for the item with a fixed width
Gui.NextWidthUnscaled(widths[i]).Text($"{i}");
if (i != widths.Count - 1)
{
// Space between the items
Gui.NextWidth(4f).Spacing();
var splitterRect = Ctx.GetLastWidgetRect();
var splitterResult = Gui.Splitter(Axis.Horizontal, splitterRect, tempWidths[i], tempWidths[i + 1], 10f, 10f);
tempWidths[i] = splitterResult.size1;
tempWidths[i + 1] = splitterResult.size2;
}
}
// Apply the modified widths to the original list
widths.ReplaceAll(tempWidths);
}
}