vn/code/gen_ui.cpp

129 lines
5.1 KiB
C++

struct ui_style_stack
{
char *Name;
char *Type;
char *MemberName;
};
ui_style_stack UIStyleStacks[] =
{
{ "Parent", "ui_box *", "Parent" },
{ "Width", "ui_size", "SemanticSize[Axis2_X]" },
{ "Height", "ui_size", "SemanticSize[Axis2_Y]" },
{ "FixedX", "r32", "FixedP.E[Axis2_X]" },
{ "FixedY", "r32", "FixedP.E[Axis2_Y]" },
{ "TextColor", "v4", "TextColor" },
{ "BackgroundColor", "v4", "BackgroundColor" },
{ "BorderColor", "v4", "BorderColor" },
{ "BorderThickness", "r32", "BorderThickness" },
{ "LayoutAxis", "axis2", "LayoutAxis" },
{ "CornerRadius", "r32", "CornerRadius" },
{ "Font", "font_id", "Font" },
{ "FontSize", "r32", "FontSize" },
{ "OffsetX", "r32", "Offset.x" },
{ "OffsetY", "r32", "Offset.y" },
};
static void GenUI(void)
{
FILE *Out = fopen("../code/generated/vn_generated_ui.h", "wb");
if(Out)
{
fprintf(Out, "struct ui_style_stacks\n");
fprintf(Out, "{\n");
for(s32 Index = 0;
Index < ArrayCount(UIStyleStacks);
++Index)
{
ui_style_stack Stack = UIStyleStacks[Index];
fprintf(Out, "\t%s %sStack[64];\n", Stack.Type, Stack.Name);
fprintf(Out, "\ts32 %sStackUsed;\n", Stack.Name);
fprintf(Out, "\tb32 AutoPop%s;\n", Stack.Name);
}
fprintf(Out, "};\n");
fclose(Out);
}
Out = fopen("../code/generated/vn_generated_ui.cpp", "wb");
if(Out)
{
for(s32 Index = 0;
Index < ArrayCount(UIStyleStacks);
++Index)
{
ui_style_stack Stack = UIStyleStacks[Index];
fprintf(Out, "inline void UI_Push%s(%s Element)\n", Stack.Name, Stack.Type);
fprintf(Out, "{\n");
fprintf(Out, "\tui *UI = UI_GetState();\n");
fprintf(Out, "\tAssert(UI->Stacks.%sStackUsed + 1 < ArrayCount(UI->Stacks.%sStack));\n", Stack.Name, Stack.Name);
fprintf(Out, "\tUI->Stacks.%sStack[UI->Stacks.%sStackUsed++] = Element;\n", Stack.Name, Stack.Name);
fprintf(Out, "}\n");
fprintf(Out, "\n");
fprintf(Out, "inline void UI_Pop%s(void)\n", Stack.Name);
fprintf(Out, "{\n");
fprintf(Out, "\tui *UI = UI_GetState();\n");
fprintf(Out, "\tAssert(UI->Stacks.%sStackUsed > 0);\n", Stack.Name);
fprintf(Out, "\t--UI->Stacks.%sStackUsed;\n", Stack.Name);
fprintf(Out, "}\n");
fprintf(Out, "\n");
fprintf(Out, "inline void UI_SetNext%s(%s Element)\n", Stack.Name, Stack.Type);
fprintf(Out, "{\n");
fprintf(Out, "\tui *UI = UI_GetState();\n");
fprintf(Out, "\tUI_Push%s(Element);\n", Stack.Name);
fprintf(Out, "\tUI->Stacks.AutoPop%s = true;\n", Stack.Name);
fprintf(Out, "}\n");
fprintf(Out, "\n");
fprintf(Out, "inline %s UI_First%s(void)\n", Stack.Type, Stack.Name);
fprintf(Out, "{\n");
fprintf(Out, "\tui *UI = UI_GetState();\n");
fprintf(Out, "\treturn(UI->Stacks.%sStack[0]);\n", Stack.Name);
fprintf(Out, "}\n");
fprintf(Out, "\n");
fprintf(Out, "inline %s UI_Top%s(void)\n", Stack.Type, Stack.Name);
fprintf(Out, "{\n");
fprintf(Out, "\tui *UI = UI_GetState();\n");
fprintf(Out, "\treturn(UI->Stacks.%sStack[UI->Stacks.%sStackUsed - 1]);\n", Stack.Name, Stack.Name);
fprintf(Out, "}\n");
fprintf(Out, "\n");
fprintf(Out, "#define UI_%s(Element) DeferLoop(UI_Push%s(Element), UI_Pop%s())\n",
Stack.Name, Stack.Name, Stack.Name);
fprintf(Out, "\n");
}
fprintf(Out, "\n");
fprintf(Out, "inline void UI_ApplyStyles(ui_box *Box)\n");
fprintf(Out, "{\n");
fprintf(Out, "\tui *UI = UI_GetState();\n");
for(s32 Index = 0;
Index < ArrayCount(UIStyleStacks);
++Index)
{
ui_style_stack Stack = UIStyleStacks[Index];
fprintf(Out, "\tAssert(UI->Stacks.%sStackUsed > 0);\n", Stack.Name);
fprintf(Out, "\tBox->%s = UI->Stacks.%sStack[UI->Stacks.%sStackUsed - 1];\n",
Stack.MemberName, Stack.Name, Stack.Name);
fprintf(Out, "\tif(UI->Stacks.AutoPop%s)\n", Stack.Name);
fprintf(Out, "\t{\n");
fprintf(Out, "\t\tUI_Pop%s();\n", Stack.Name);
fprintf(Out, "\t\tUI->Stacks.AutoPop%s = false;\n", Stack.Name);
fprintf(Out, "\t}\n\n");
}
fprintf(Out, "}\n");
fclose(Out);
}
}