Skip to content

Instantly share code, notes, and snippets.

@kuujoo
Last active July 16, 2022 17:52
Show Gist options
  • Save kuujoo/2b73e6f8c5e82cda1f0e2d36423c8626 to your computer and use it in GitHub Desktop.
Save kuujoo/2b73e6f8c5e82cda1f0e2d36423c8626 to your computer and use it in GitHub Desktop.
ImGui enum selector in C#
public static class ImGuiUtils
{
static bool EnumSelector<T>(string name, int idOffset, ref T selection)
{
bool change = false;
int idx = idOffset;
var enumType = typeof(T);
if (ImGui.BeginCombo(name, Enum.GetName(enumType, selection)))
{
var enumValues = Enum.GetValues(enumType);
foreach (T i in enumValues)
{
bool selected = i.Equals(selection);
ImGui.PushID(idx);
{
if (ImGui.Selectable(Enum.GetName(enumType, i), selected))
{
selection = i;
change = true;
}
if (selected)
{
ImGui.SetItemDefaultFocus();
}
}
ImGui.PopID();
idx++;
}
ImGui.EndCombo();
}
return change;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment