Skip to content

Instantly share code, notes, and snippets.

@kudaba
Created June 5, 2020 17:22
Show Gist options
  • Save kudaba/7e4878cde305eb892825415ac6fad60f to your computer and use it in GitHub Desktop.
Save kudaba/7e4878cde305eb892825415ac6fad60f to your computer and use it in GitHub Desktop.
Helper and example to implement serializer for imgui style.
//-------------------------------------------------------------------------------------------------
// Serialization helpers
//-------------------------------------------------------------------------------------------------
template <typename T>
static T& locStyleAdapter(T& aValue) { return aValue; }
static GC_Vector2f& locStyleAdapter(ImVec2& aValue) { return *(GC_Vector2f*)&aValue; }
static GC_Vector2f const& locStyleAdapter(ImVec2 const& aValue) { return *(GC_Vector2f const*)&aValue; }
static GC_Vector4f& locStyleAdapter(ImVec4& aValue) { return *(GC_Vector4f*)&aValue; }
static GC_Vector4f const& locStyleAdapter(ImVec4 const& aValue) { return *(GC_Vector4f const*)&aValue; }
//-------------------------------------------------------------------------------------------------
static GC_DynamicMap locSaveStyle(ImGuiStyle const& aStyle)
{
GC_DynamicMap map;
#define IMPLEMENT_STYLE(name) map.Set(#name, locStyleAdapter(aStyle.name));
#define IMPLEMENT_COLOR(name) map.Set(#name, locStyleAdapter(aStyle.Colors[name]));
#include "GT_StyleSerializeHelper.inl"
#undef IMPLEMENT_COLOR
#undef IMPLEMENT_STYLE
map.Set("ColorButtonPosition", (int)aStyle.ColorButtonPosition);
map.Set("WindowMenuButtonPosition", (int)aStyle.WindowMenuButtonPosition);
return map;
}
//-------------------------------------------------------------------------------------------------
static void locLoadStyle(GC_DynamicMap aMap, ImGuiStyle& aStyleOut)
{
#define IMPLEMENT_STYLE(name) aMap.Get(#name, locStyleAdapter(aStyleOut.name));
#define IMPLEMENT_COLOR(name) aMap.Get(#name, locStyleAdapter(aStyleOut.Colors[name]));
#include "GT_StyleSerializeHelper.inl"
#undef IMPLEMENT_COLOR
#undef IMPLEMENT_STYLE
int tmp = aStyleOut.ColorButtonPosition;
aMap.Get("ColorButtonPosition", tmp);
aStyleOut.ColorButtonPosition = tmp;
tmp = aStyleOut.WindowMenuButtonPosition;
aMap.Get("WindowMenuButtonPosition", tmp);
aStyleOut.WindowMenuButtonPosition = tmp;
}
//-------------------------------------------------------------------------------------------------
// Helper to quickly adapt code to imgui styles
//-------------------------------------------------------------------------------------------------
IMPLEMENT_STYLE(Alpha )
IMPLEMENT_STYLE(WindowPadding )
IMPLEMENT_STYLE(WindowRounding )
IMPLEMENT_STYLE(WindowBorderSize )
IMPLEMENT_STYLE(WindowMinSize )
IMPLEMENT_STYLE(WindowTitleAlign )
IMPLEMENT_STYLE(WindowMenuButtonPosition)
IMPLEMENT_STYLE(ChildRounding )
IMPLEMENT_STYLE(ChildBorderSize )
IMPLEMENT_STYLE(PopupRounding )
IMPLEMENT_STYLE(PopupBorderSize )
IMPLEMENT_STYLE(FramePadding )
IMPLEMENT_STYLE(FrameRounding )
IMPLEMENT_STYLE(FrameBorderSize )
IMPLEMENT_STYLE(ItemSpacing )
IMPLEMENT_STYLE(ItemInnerSpacing )
IMPLEMENT_STYLE(TouchExtraPadding )
IMPLEMENT_STYLE(IndentSpacing )
IMPLEMENT_STYLE(ColumnsMinSpacing )
IMPLEMENT_STYLE(ScrollbarSize )
IMPLEMENT_STYLE(ScrollbarRounding )
IMPLEMENT_STYLE(GrabMinSize )
IMPLEMENT_STYLE(GrabRounding )
IMPLEMENT_STYLE(TabRounding )
IMPLEMENT_STYLE(TabBorderSize )
IMPLEMENT_STYLE(TabMinWidthForUnselectedCloseButton)
IMPLEMENT_STYLE(ColorButtonPosition )
IMPLEMENT_STYLE(ButtonTextAlign )
IMPLEMENT_STYLE(SelectableTextAlign )
IMPLEMENT_STYLE(DisplayWindowPadding )
IMPLEMENT_STYLE(DisplaySafeAreaPadding )
IMPLEMENT_STYLE(MouseCursorScale )
IMPLEMENT_STYLE(AntiAliasedLines )
IMPLEMENT_STYLE(AntiAliasedFill )
IMPLEMENT_STYLE(CurveTessellationTol )
IMPLEMENT_STYLE(CircleSegmentMaxError )
IMPLEMENT_COLOR(ImGuiCol_Text)
IMPLEMENT_COLOR(ImGuiCol_TextDisabled)
IMPLEMENT_COLOR(ImGuiCol_WindowBg)
IMPLEMENT_COLOR(ImGuiCol_ChildBg)
IMPLEMENT_COLOR(ImGuiCol_PopupBg)
IMPLEMENT_COLOR(ImGuiCol_Border)
IMPLEMENT_COLOR(ImGuiCol_BorderShadow)
IMPLEMENT_COLOR(ImGuiCol_FrameBg)
IMPLEMENT_COLOR(ImGuiCol_FrameBgHovered)
IMPLEMENT_COLOR(ImGuiCol_FrameBgActive)
IMPLEMENT_COLOR(ImGuiCol_TitleBg)
IMPLEMENT_COLOR(ImGuiCol_TitleBgActive)
IMPLEMENT_COLOR(ImGuiCol_TitleBgCollapsed)
IMPLEMENT_COLOR(ImGuiCol_MenuBarBg)
IMPLEMENT_COLOR(ImGuiCol_ScrollbarBg)
IMPLEMENT_COLOR(ImGuiCol_ScrollbarGrab)
IMPLEMENT_COLOR(ImGuiCol_ScrollbarGrabHovered)
IMPLEMENT_COLOR(ImGuiCol_ScrollbarGrabActive)
IMPLEMENT_COLOR(ImGuiCol_CheckMark)
IMPLEMENT_COLOR(ImGuiCol_SliderGrab)
IMPLEMENT_COLOR(ImGuiCol_SliderGrabActive)
IMPLEMENT_COLOR(ImGuiCol_Button)
IMPLEMENT_COLOR(ImGuiCol_ButtonHovered)
IMPLEMENT_COLOR(ImGuiCol_ButtonActive)
IMPLEMENT_COLOR(ImGuiCol_Header)
IMPLEMENT_COLOR(ImGuiCol_HeaderHovered)
IMPLEMENT_COLOR(ImGuiCol_HeaderActive)
IMPLEMENT_COLOR(ImGuiCol_Separator)
IMPLEMENT_COLOR(ImGuiCol_SeparatorHovered)
IMPLEMENT_COLOR(ImGuiCol_SeparatorActive)
IMPLEMENT_COLOR(ImGuiCol_ResizeGrip)
IMPLEMENT_COLOR(ImGuiCol_ResizeGripHovered)
IMPLEMENT_COLOR(ImGuiCol_ResizeGripActive)
IMPLEMENT_COLOR(ImGuiCol_Tab)
IMPLEMENT_COLOR(ImGuiCol_TabHovered)
IMPLEMENT_COLOR(ImGuiCol_TabActive)
IMPLEMENT_COLOR(ImGuiCol_TabUnfocused)
IMPLEMENT_COLOR(ImGuiCol_TabUnfocusedActive)
IMPLEMENT_COLOR(ImGuiCol_DockingPreview)
IMPLEMENT_COLOR(ImGuiCol_DockingEmptyBg)
IMPLEMENT_COLOR(ImGuiCol_PlotLines)
IMPLEMENT_COLOR(ImGuiCol_PlotLinesHovered)
IMPLEMENT_COLOR(ImGuiCol_PlotHistogram)
IMPLEMENT_COLOR(ImGuiCol_PlotHistogramHovered)
IMPLEMENT_COLOR(ImGuiCol_TextSelectedBg)
IMPLEMENT_COLOR(ImGuiCol_DragDropTarget)
IMPLEMENT_COLOR(ImGuiCol_NavHighlight)
IMPLEMENT_COLOR(ImGuiCol_NavWindowingHighlight)
IMPLEMENT_COLOR(ImGuiCol_NavWindowingDimBg)
IMPLEMENT_COLOR(ImGuiCol_ModalWindowDimBg)
//-------------------------------------------------------------------------------------------------
// If this fails then it means the structure changed and previous values need to be changed
//-------------------------------------------------------------------------------------------------
static_assert(sizeof(ImGuiStyle) == 984, "ImGuiStyle changed, update serializer.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment