Skip to content

Instantly share code, notes, and snippets.

@kudaba
Created October 3, 2019 05:48
Show Gist options
  • Save kudaba/ae0b43b88347441ba6b9a3911afb0db8 to your computer and use it in GitHub Desktop.
Save kudaba/ae0b43b88347441ba6b9a3911afb0db8 to your computer and use it in GitHub Desktop.
Helper to set and forget imgui style changes.
//-------------------------------------------------------------------------------------------------
// Helper for managing style changes. Can set and forget.
// Usage 1: ImGuiStyleScope scope(ImGuiCol_Text, ImGuiColors::Red);
// Usage 2:
// ImGuiStyleScope scope;
// scope.Color(ImGuiCol_Text, ImGuiColors::Yellow)
// .Style(ImGuiStyleVar_Alpha, 0.4f);
//-------------------------------------------------------------------------------------------------
class ImGuiStyleScope : GC_NonCopyable
{
public:
ImGuiStyleScope() : myColors(0), myVars(0) {}
ImGuiStyleScope(ImGuiCol anIndex, ImU32 aValue) : ImGuiStyleScope()
{
Color(anIndex, aValue);
}
ImGuiStyleScope(ImGuiCol anIndex, ImVec4 const& aValue) : ImGuiStyleScope()
{
Color(anIndex, aValue);
}
ImGuiStyleScope(ImGuiStyleVar anIndex, float aValue) : ImGuiStyleScope()
{
Style(anIndex, aValue);
}
ImGuiStyleScope(ImGuiStyleVar anIndex, ImVec2 const& aValue) : ImGuiStyleScope()
{
Style(anIndex, aValue);
}
~ImGuiStyleScope()
{
if (myColors) ImGui::PopStyleColor(myColors);
if (myVars) ImGui::PopStyleVar(myVars);
}
ImGuiStyleScope& Color(ImGuiCol anIndex, ImU32 aValue)
{
ImGui::PushStyleColor(anIndex, aValue);
++myColors;
return *this;
}
ImGuiStyleScope& Color(ImGuiCol anIndex, ImVec4 const& aValue)
{
ImGui::PushStyleColor(anIndex, aValue);
++myColors;
return *this;
}
ImGuiStyleScope& Style(ImGuiStyleVar anIndex, float aValue)
{
ImGui::PushStyleVar(anIndex, aValue);
++myVars;
return *this;
}
ImGuiStyleScope& Style(ImGuiStyleVar anIndex, ImVec2 const& aValue)
{
ImGui::PushStyleVar(anIndex, aValue);
++myVars;
return *this;
}
private:
uint myColors;
uint myVars;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment