Skip to main content

Scroll

scroll

See the ScrollDemo.cs.

using Gridrand.Contracts;
using Gridrand.RimGui.Manual.Utility;
namespace Gridrand.RimGui.Manual
{
/// <summary>
/// Demonstrates various scrolling behaviors.
/// </summary>
class ScrollDemo : ManualBase, IManual
{
int outerTextCount = 20;
int textCountWithIterator = 20;

float height = 100f;

float outerHeight = 180f;
float innerHeight = 100f;
int innerTextCount = 10;

// Flag to determine if the scroll should remain fixed at the bottom.
bool shouldFixToBottom;

public ScrollDemo(ManualBaseResource p) : base(p)
{
}

public void Draw()
{
DrawSettings();
using var scope = Style.ScrollBar.FixToBottom.Begin(shouldFixToBottom);
DrawWithDynamic();
DrawUsingHandler();
DrawToBottomOfWindow();
}

void DrawSettings()
{
Gui.Heading("Settings");

var rects = LayoutBuilder.AllocateMainAndToolTipRect();

Gui.NextRect(rects.Main).LabelInput("FixToBottom", ref shouldFixToBottom);
Gui.NextRect(rects.ToolTip).ToolTip("Keep the scroll fixed at the bottom even when new widgets are added.");
}

void DrawWithDynamic()
{
Gui.HeadingTooltip(
"Dynamic",
"A scroll that allows dynamic addition of widgets.\n" +
"Suitable when the number of widgets is not large, as it processes all of them.");

// Controls for modifying scroll properties.
outerTextCount = Gui.LabelSliderNumeric("OuterTextCount", outerTextCount, 0, 100);
innerTextCount = Gui.LabelSliderNumeric("innerTextCount", innerTextCount, 0, 100);
outerHeight = Gui.LabelSliderNumeric("OuterHeight", outerHeight, 0, 300);
innerHeight = Gui.LabelSliderNumeric("InnerHeight", innerHeight, 0, 300);

// Set SpacingY to 0 within this scope.
using var s = Style.SpacingYs.Begin(0f);
using var p = Gui.BeginFramePadding1();
Gui.NextHeight(Size.Fixed(outerHeight));
// Outer scrollable area.
if (Gui.BeginScroll())
{
using (Gui.BeginFramePadding1())
{
// Inner scrollable area.
Gui.NextHeight(Size.Fixed(innerHeight));
if (Gui.BeginScroll())
{
for (int i = 0; i < innerTextCount; i++)
{
using (Ctx.PushId(i))
Gui.Text($"inner:{i}");
}
// Must be executed if BeginScroll() returns true.
Gui.EndScroll();
}
}

for (int i = 0; i < outerTextCount; i++)
{
Gui.Text($"outer:{i}");
}

Gui.EndScroll();
}
}

void DrawUsingHandler()
{
Gui.HeadingTooltip(
"Fixed",
"A scroll where the number and height of widgets must be determined before processing.\n" +
"It only processes the displayed widgets, making it fast even when there are many.");

// Controls for modifying fixed scroll properties.
Gui.LabelInputNumeric("Height", ref height);
textCountWithIterator = Gui.LabelSliderNumeric("TextCount", textCountWithIterator, 0, 100);

using var p = Gui.BeginFramePadding1();
// Create a scroll handler with fixed height and item count.
var handler = Gui.CreateScrollHandler(height, textCountWithIterator, Style.StandardHeight, 0f);
// Render only visible items based on the scroll handler.
while (handler.Next())
{
Gui.Text($"{handler.CurrentIndex}");
}
}

/// <summary>
/// Shows how to display the scroll view at the very bottom of the window.
/// </summary>
void DrawToBottomOfWindow()
{
Gui.Heading("Fill to Bottom");
using var p = Gui.BeginFramePadding1();

Ctx.AddSpaceIfNeeded();
// Create a scroll handler that fills the remaining height in the window.
var handler = Gui.CreateScrollHandler(Ctx.GetRemainRect().height, 100, Style.StandardHeight, 0f);
// Render only visible items based on the scroll handler.
while (handler.Next())
{
Gui.Text(handler.CurrentIndex.ToString());
}
}
}
}