Last active
July 16, 2022 17:52
-
-
Save kuujoo/2b73e6f8c5e82cda1f0e2d36423c8626 to your computer and use it in GitHub Desktop.
ImGui enum selector in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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