Skip to main content

Basic Usage

begin-usage

See the BasicUsage.cs.

using Gridrand.Contracts;
using Gridrand.RimGui.Manual;
namespace Gridrand.RimGui
{
/// <summary>
/// Basic usage.
///
/// The following classes are especially important within the library:
/// <see cref="Gui"/>
/// <see cref="Context"/>
/// <see cref="Style"/>
///
/// You can access Context and Style through the Gui class:
/// <see cref="Gui.Context"/>
/// <see cref="Gui.Style"/>
///
/// The <see cref="Context"/> class is a central class that manages and provides various context information
/// necessary for GUI rendering and interaction.
///
/// The <see cref="Style"/> is a class used to configure the overall settings of the GUI and individual widget settings.
///
/// In <see cref="BasicUsage"/>, we use <see cref="ManualBase.Gui"/>,
/// <see cref="ManualBase.Ctx"/>, and <see cref="ManualBase.Style"/> as defined in <see cref="ManualBase"/>.
/// </summary>
class BasicUsage : ManualBase, IManual
{

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

public void Draw()
{
Gui.Text("See the comments in the code.");

// Simply executing this will render the widget (TextButton).
if (Gui.Button("TextButton"))
{
// Action when pressed
}

/// The state of the most recently rendered widget can be retrieved
/// using the <see cref="Context"/> methods.
var isLastPressed = Ctx.IsLastPressed();
var isLastPressedThisFrame = Ctx.IsLastPressedThisFrame();
var isLastHovered = Ctx.IsLastHovered();
var lastWidgetRect = Ctx.GetLastWidgetRect();

/// Previously executed widgets are drawn behind later ones.
/// For example, if the drawing positions of "A" and "B" overlap,
/// "A" will be drawn behind "B".
///
/// If you want to draw a previously executed widget in front,
/// refer to `<see cref="DeferredBuildingDemo"/>`.
Gui.Text("A");
Gui.Text("B");

/// To change properties such as widget colors,
/// modify the properties of `<see cref="Style"/>`.
using (Style.Text.Colors.Begin(new Color32(180, 0, 0, 255)))
{
// Settings can also be stacked like this.
using (Style.Text.Colors.Begin(new Color32(0, 180, 0, 255)))
{
Gui.Text("Green");
}
Gui.Text("Red");
}

for (int i = 0; i < 3; i++)
{
/// Each widget is assigned an ID,
/// which is generated based on the stacked IDs and arguments.
/// If IDs are the same, unintended behavior may occur,
/// so they must be unique.
///
/// Since the implementation ensures that IDs do not overlap,
/// users generally do not need to worry about it.
/// However, for widgets inside a `for` loop, like the `TextButton()` below,
/// IDs should be stacked using `<see cref="Context.PushId{T}(T)"/>`.
using var idScope = Ctx.PushId(i);
if (Gui.Button("TextButton"))
{
Logger.Log($"Pressed {i}");
}
}
}
}
}

Explanation

This guide demonstrates the simplest way to render interactive UI elements with RimGui. It covers text rendering, buttons, querying widget states, draw order, styling, and generating unique IDs for widgets in loops.


1. Rendering Text and Buttons

  • Text: Use Gui.Text("Your text here") to render a simple text label.
  • Button: Use Gui.Button("Label") to render a button. The method returns a boolean indicating whether the button was pressed.
// Render a descriptive label
Gui.Text("See the comments in the code.");

// Render a button and handle clicks
if (Gui.Button("TextButton"))
{
// Action when button is pressed
}

2. Querying Widget State

After rendering a widget, you can retrieve its interaction state via the Context (Ctx) API:

MethodDescription
Ctx.IsLastPressed()Returns true if the last widget was pressed (ever).
Ctx.IsLastPressedThisFrame()Returns true if the last widget was pressed in the current frame.
Ctx.IsLastHovered()Returns true if the last widget is currently hovered.
Ctx.GetLastWidgetRect()Returns the rectangle bounds of the last widget.
var pressedEver = Ctx.IsLastPressed();
var pressedNow = Ctx.IsLastPressedThisFrame();
var hovered = Ctx.IsLastHovered();
var rect = Ctx.GetLastWidgetRect();

3. Draw Order

Widgets are drawn in the order they are executed. Widgets rendered later will appear on top of earlier ones if they overlap.

Gui.Text("A");
Gui.Text("B");

In this example, "B" is drawn on top of "A" if their positions overlap. To defer drawing until after higher-level elements have rendered, see the deferred-building example.


4. Styling

You can temporarily override style properties (such as text color) by using the style stacks provided by Style:

// Change text color to red
using (Style.Text.Colors.Begin(new Color32(180, 0, 0, 255)))
{
// Nested style overrides
using (Style.Text.Colors.Begin(new Color32(0, 180, 0, 255)))
{
Gui.Text("Green");
}

Gui.Text("Red");
}
  • Styles can be nested and will revert to previous values when disposed.

5. Unique IDs for Repeated Widgets

Widgets need unique IDs to avoid conflicts, especially inside loops. RimGui automatically generates IDs based on argument values, but when rendering similar widgets in loops, use Ctx.PushId<T>(value) to scope IDs:

for (int i = 0; i < 3; i++)
{
using var idScope = Ctx.PushId(i);
if (Gui.Button("TextButton"))
{
Logger.Log($"Pressed {i}");
}
}
  • Ctx.PushId(i) ensures each button in the loop has a distinct ID, preventing unintended interactions.