Skip to main content

RadioButton

radio-button

See the RadioButtonDemo.cs.

using Gridrand.Contracts;
using System;
using System.Collections.Generic;
namespace Gridrand.RimGui.Manual
{
/// <summary>
/// <see cref="Gui.RadioButton(bool, int)"/> demo.
/// </summary>
class RadioButtonDemo : ManualBase, IManual
{
bool isOnRadioButton1 = true;
bool isOnRadioButton2 = true;
Direction direction;

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

public void Draw()
{
Gui.RadioButton(ref isOnRadioButton1);

Gui.NextWidthHeight(50f, 50f).RadioButton(ref isOnRadioButton2);

direction = DrawRadioButtons(direction);
}

TEnum DrawRadioButtons<TEnum>(TEnum e) where TEnum : Enum
{
// Begin a horizontal layout scope
using var axisScope = Ctx.BeginHorizontal();

var selectedEnum = e;

// Iterate through each value in the enum
foreach (TEnum v in Enum.GetValues(typeof(TEnum)))
{
// Push a unique ID for the radio button context (based on hash code)
using var s2 = Ctx.PushId(v.GetHashCode());

// If the current enum value matches the selected one
if (EqualityComparer<TEnum>.Default.Equals(e, v))
{
// Draw a selected radio button; if clicked again, deselect it
if (!Gui.RadioButton(true))
selectedEnum = default;
}
else
{
// Draw an unselected radio button; if clicked, select this value
if (Gui.RadioButton(false))
selectedEnum = v;
}

// Display the enum value as a label next to the radio button
var name = v.ToString();
Gui.NextTextWidth(name).Text(name);

Gui.NextWidth(5f).Spacing();
}

return selectedEnum;
}

}
}