257 lines
6.4 KiB
C
257 lines
6.4 KiB
C
////////////////////////////////////////////////////////////
|
|
//~ Key types
|
|
|
|
#define UI_NilKey ((UI_Key) { 0 })
|
|
#define UI_RootKey ((UI_Key) { 0xa3deb3749ef35a7aUll })
|
|
|
|
Struct(UI_Key)
|
|
{
|
|
u64 hash;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Size types
|
|
|
|
Enum(UI_SizeKind)
|
|
{
|
|
UI_SizeKind_Fit,
|
|
UI_SizeKind_Fill,
|
|
UI_SizeKind_Pixel,
|
|
UI_SizeKind_TextContents,
|
|
};
|
|
|
|
Struct(UI_Size)
|
|
{
|
|
UI_SizeKind kind;
|
|
f32 v;
|
|
f32 strictness;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Style types
|
|
|
|
#define UI_StyleKindsXMacro(x) \
|
|
x(Tag, u64) \
|
|
x(Parent, struct UI_Box *) \
|
|
x(LayoutAxis, Axis) \
|
|
x(Width, UI_Size) \
|
|
x(Height, UI_Size) \
|
|
x(Size, UI_Size) \
|
|
x(BackgroundColor, u32) \
|
|
x(BorderColor, u32) \
|
|
x(Tint, u32) \
|
|
x(Border, f32) \
|
|
x(Font, ResourceKey) \
|
|
x(FontSize, u32) \
|
|
x(Text, String) \
|
|
x(TextPadding, f32) \
|
|
x(Rounding, f32) \
|
|
/* ------------------------------------------- */
|
|
|
|
Enum(UI_StyleKind)
|
|
{
|
|
#define X(name, type) UI_StyleKind_##name,
|
|
UI_StyleKind_None,
|
|
UI_StyleKindsXMacro(X)
|
|
UI_StyleKind_Count,
|
|
#undef X
|
|
};
|
|
|
|
Struct(UI_Style)
|
|
{
|
|
UI_StyleKind kind;
|
|
b32 pop_when_used;
|
|
b32 forced;
|
|
/* Union of all style fields */
|
|
union
|
|
{
|
|
#define X(name, type) type name;
|
|
UI_StyleKindsXMacro(X)
|
|
#undef X
|
|
};
|
|
};
|
|
|
|
Struct(UI_StyleNode)
|
|
{
|
|
UI_StyleNode *next;
|
|
u64 checkpoint;
|
|
UI_Style style;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Box types
|
|
|
|
/* NOTE: UI flags are located here since they are shared between application & shader code */
|
|
|
|
Enum(UI_BoxFlag)
|
|
{
|
|
UI_BoxFlag_None = 0,
|
|
UI_BoxFlag_DrawText = (1 << 0),
|
|
};
|
|
|
|
Struct(UI_Box)
|
|
{
|
|
//- Hash list
|
|
UI_Box *next_in_bin;
|
|
UI_Box *prev_in_bin;
|
|
|
|
//- Tree
|
|
UI_Box *parent;
|
|
UI_Box *first;
|
|
UI_Box *last;
|
|
UI_Box *next;
|
|
UI_Box *prev;
|
|
u64 count;
|
|
|
|
//- Persistent data
|
|
UI_Key key;
|
|
|
|
//- Per-build data
|
|
UI_BoxFlag flags;
|
|
|
|
String display_text;
|
|
|
|
GPU_Resource *background_texture;
|
|
Vec2 background_texture_uv0;
|
|
Vec2 background_texture_uv1;
|
|
|
|
Axis layout_axis;
|
|
|
|
UI_Size pref_size[Axis_CountXY];
|
|
u32 background_color;
|
|
u32 border_color;
|
|
u32 text_color;
|
|
u32 tint;
|
|
f32 border;
|
|
f32 text_padding;
|
|
f32 rounding;
|
|
|
|
ResourceKey font_resource;
|
|
f32 font_size;
|
|
|
|
//- Layout data
|
|
F_Run glyph_run;
|
|
F_Font *font;
|
|
f32 solved_dims[Axis_CountXY];
|
|
f32 layout_cursor;
|
|
|
|
//- Post-solve data
|
|
Vec2 p0;
|
|
Vec2 p1;
|
|
};
|
|
Struct(UI_BoxBin)
|
|
{
|
|
UI_Box *first;
|
|
UI_Box *last;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Checkpoint types
|
|
|
|
Struct(UI_Checkpoint)
|
|
{
|
|
UI_Checkpoint *next;
|
|
u64 v;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ State types
|
|
|
|
#define UI_NumBoxLookupBins 16384
|
|
|
|
Struct(UI_SharedState)
|
|
{
|
|
//- Build state
|
|
Arena *build_arena;
|
|
Arena *back_build_arena;
|
|
|
|
UI_BoxBin *box_bins;
|
|
UI_BoxBin *back_box_bins;
|
|
|
|
UI_Box *root_box;
|
|
UI_Box *back_root_box;
|
|
|
|
u64 boxes_count;
|
|
u64 back_boxes_count;
|
|
|
|
UI_Checkpoint *top_checkpoint;
|
|
UI_Checkpoint *first_free_checkpoint;
|
|
|
|
UI_StyleNode *style_tops[UI_StyleKind_Count];
|
|
UI_StyleNode *first_free_style_node;
|
|
|
|
//- Render state
|
|
i64 gpu_submit_fence_target;
|
|
GPU_TransientBuffer draw_rects_tbuff;
|
|
Arena *draw_rects_arena;
|
|
|
|
} extern UI_shared_state;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Startup
|
|
|
|
void UI_Startup(void);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Font helpers
|
|
|
|
ResourceKey UI_GetDefaultFontResource(void);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Key helpers
|
|
|
|
u64 UI_HashFromTop(void);
|
|
u64 UI_HashFromString(u64 seed, String str);
|
|
UI_Key UI_KeyFromString(u64 seed, String str);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Checkpoint helpers
|
|
|
|
void UI_PushCheckpoint(void);
|
|
void UI_PopCheckpoint(void);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Style helpers
|
|
|
|
UI_StyleNode *UI_PushStyleNode(UI_Style desc);
|
|
UI_Style UI_PopStyleNode(UI_StyleKind kind);
|
|
UI_StyleNode *UI_PeekTopStyleNode(UI_StyleKind kind);
|
|
UI_Style UI_StyleFromTopNode(UI_StyleKind kind, b32 use);
|
|
|
|
#define UI_SetNext(name, v) UI_PushStyleNode((UI_Style) { .kind = UI_StyleKind_##name, .name = v, .pop_when_used = 1 })
|
|
#define UI_Push(name, v) UI_PushStyleNode((UI_Style) { .kind = UI_StyleKind_##name, .name = v })
|
|
#define UI_ForceNext(name, v) UI_PushStyleNode((UI_Style) { .kind = UI_StyleKind_##name, .name = v, .pop_when_used = 1, .forced = 1 })
|
|
#define UI_ForcePush(name, v) UI_PushStyleNode((UI_Style) { .kind = UI_StyleKind_##name, .name = v, .forced = 1 })
|
|
#define UI_Pop(name, v) UI_PopStyleNode(UI_StyleKind_##name).name
|
|
#define UI_UseTop(name) UI_StyleFromTopNode(UI_StyleKind_##name, 1).name
|
|
#define UI_PeekTop(name) UI_StyleFromTopNode(UI_StyleKind_##name, 0).name
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Size helpers
|
|
|
|
#define UI_SIZE(_kind, _v, _s) (UI_Size) { .kind = (_kind), .v = (_v), .strictness = (_s) }
|
|
|
|
#define UI_PIX(_v, _s) UI_SIZE(UI_SizeKind_Pixel, (_v), (_s))
|
|
#define UI_FIT(_s) UI_SIZE(UI_SizeKind_Fit, 0, (_s))
|
|
#define UI_FILL(_v, _s) UI_SIZE(UI_SizeKind_Fill, (_v), (_s))
|
|
#define UI_TXT(_s) UI_SIZE(UI_SizeKind_TextContents, 0, (_s))
|
|
#define UI_EM(_v, _s) UI_SIZE(UI_SizeKind_Pixel, (f32)UI_PeekTop(FontSize) * (_v), (_s))
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Box
|
|
|
|
UI_Box *UI_BuildBox(UI_BoxFlag flags, UI_Key key);
|
|
|
|
void UI_SetBackgroundTexture(UI_Box *box, GPU_Resource *texture, Vec2 uv0, Vec2 uv1);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Begin build
|
|
|
|
void UI_BeginBuild(void);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ End build
|
|
|
|
GPU_ResourceDesc UI_GetRenderTargetDesc(Vec2I32 size);
|
|
i64 UI_EndBuild(GPU_Resource *render_target);
|