vn/code/vn_ui.h

219 lines
5.1 KiB
C
Raw Permalink Normal View History

2023-06-17 17:00:55 +00:00
/* date = April 29th 2023 8:22 pm */
#ifndef VN_UI_H
#define VN_UI_H
2023-06-27 14:14:28 +00:00
//- sixten: Keying
2023-06-17 17:00:55 +00:00
struct ui_key
{
2023-12-23 07:27:22 +00:00
u64 Value;
2023-06-17 17:00:55 +00:00
};
inline b32 AreEqual(ui_key A, ui_key B)
{
2023-12-23 07:27:22 +00:00
b32 Result = (A.Value == B.Value);
return(Result);
2023-06-17 17:00:55 +00:00
}
2023-06-27 14:14:28 +00:00
//- sixten: Semantic sizing
2023-09-13 04:42:11 +00:00
enum ui_size_kind
2023-06-17 17:00:55 +00:00
{
2023-12-23 07:27:22 +00:00
UI_SizeKind_Pixels,
UI_SizeKind_TextContent,
UI_SizeKind_PercentOfParent,
UI_SizeKind_ChildrenSum,
2023-06-17 17:00:55 +00:00
};
struct ui_size
{
2023-12-23 07:27:22 +00:00
ui_size_kind Kind;
r32 Value;
r32 Strictness;
2023-06-17 17:00:55 +00:00
};
2023-09-13 04:42:11 +00:00
static ui_size UI_MakeSize(ui_size_kind Kind, r32 Value, r32 Strictness);
#define UI_Pixels(Value, Strictness) UI_MakeSize(UI_SizeKind_Pixels, Value, Strictness)
#define UI_Em(Value, Strictness) UI_MakeSize(UI_SizeKind_Pixels, (Value)*UI_TopFontSize(), Strictness)
#define UI_TextContent(Value, Strictness) UI_MakeSize(UI_SizeKind_TextContent, Value, Strictness)
#define UI_Percent(Value, Strictness) UI_MakeSize(UI_SizeKind_PercentOfParent, Value, Strictness)
#define UI_ChildrenSum(Value, Strictness) UI_MakeSize(UI_SizeKind_ChildrenSum, Value, Strictness)
2023-06-17 17:00:55 +00:00
2023-06-27 14:14:28 +00:00
//- sixten: UI core
enum
{
2023-12-23 07:27:22 +00:00
UI_BoxFlag_Clickable = (1 << 0),
UI_BoxFlag_DrawText = (1 << 1),
UI_BoxFlag_DrawBorder = (1 << 2),
UI_BoxFlag_DrawBackground = (1 << 3),
UI_BoxFlag_DrawDropShadow = (1 << 4),
UI_BoxFlag_Clip = (1 << 5),
UI_BoxFlag_HotAnimation = (1 << 6),
UI_BoxFlag_ActiveAnimation = (1 << 7),
UI_BoxFlag_OverflowX = (1 << 8),
UI_BoxFlag_OverflowY = (1 << 9),
UI_BoxFlag_FloatingX = (1 << 10),
UI_BoxFlag_FloatingY = (1 << 11),
UI_BoxFlag_Scrollable = (1 << 12),
2023-06-27 14:14:28 +00:00
};
typedef u32 ui_box_flags;
2023-07-19 15:09:41 +00:00
#define UI_CUSTOM_DRAW_CALLBACK(name) void name(render_group *Group, glyph_atlas *Atlas, struct ui_box *Box, void *Data)
typedef UI_CUSTOM_DRAW_CALLBACK(ui_custom_draw_callback);
2023-06-17 17:00:55 +00:00
struct ui_box
{
2023-12-23 07:27:22 +00:00
// sixten: building
ui_box *First;
ui_box *Last;
ui_box *Next;
ui_box *Prev;
ui_box *Parent;
// sixten: hashing
ui_box *HashNext;
ui_box *HashPrev;
ui_key Key;
ui_key Seed;
u64 LastFrameTouched;
// sixten: per-frame data
ui_box_flags Flags;
string String;
ui_size SemanticSize[Axis2_Count];
2024-01-20 11:18:57 +00:00
v2_r32 FixedP;
v4_r32 TextColor;
v4_r32 BackgroundColor;
v4_r32 BorderColor;
2023-12-23 07:27:22 +00:00
r32 BorderThickness;
axis2 LayoutAxis;
2024-01-20 11:18:57 +00:00
v4_r32 CornerRadii;
2023-12-23 07:27:22 +00:00
font_id Font;
r32 FontSize;
2024-01-20 11:18:57 +00:00
v2_r32 Offset;
2023-12-23 07:27:22 +00:00
platform_cursor HoverCursor;
ui_custom_draw_callback *DrawCallback;
void *DrawCallbackData;
2024-01-20 11:18:57 +00:00
v2_r32 ComputedRelativeP;
v2_r32 ComputedDim;
2023-12-23 07:27:22 +00:00
// sixten: retained data
range2_r32 Rect;
r32 HotTransition;
r32 ActiveTransition;
2024-01-20 11:18:57 +00:00
v2_r32 ApproachingRelativeP;
2023-06-17 17:00:55 +00:00
};
struct ui_box_bucket
{
2023-12-23 07:27:22 +00:00
ui_box *First;
ui_box *Last;
2023-06-17 17:00:55 +00:00
};
struct ui_signal
{
2023-12-23 07:27:22 +00:00
ui_box *Box;
2024-01-20 11:18:57 +00:00
v2_r32 MouseP;
v2_r32 dMouseP;
v2_r32 DragDelta;
v2_r32 Scroll;
2023-12-23 07:27:22 +00:00
b8 Clicked;
b8 Pressed;
b8 PressedRight;
b8 Released;
b8 Hovering;
b8 Dragging;
2023-06-17 17:00:55 +00:00
};
2023-07-19 15:09:41 +00:00
#include "generated/vn_ui.meta.h"
2023-06-17 17:00:55 +00:00
struct ui
{
2023-12-23 07:27:22 +00:00
arena *Arena;
arena *FrameArena;
ui_box *FirstFreeBox;
ui_box *LastFreeBox;
ui_box_bucket BoxBuckets[256];
ui_box *RootNode;
ui_box *TooltipNode;
ui_box *ContainerNode;
u64 CurrentFrame;
ui_key Hot;
ui_key Active;
r64 Time;
r64 BlinkTime;
r64 Blink;
ui_key NextHot;
b32 NextHotSet;
u64 DragData[8];
2024-01-20 11:18:57 +00:00
v2_r32 DragStartP;
2023-12-23 07:27:22 +00:00
ui_style_stacks Stacks;
platform_event_list *EventList;
2024-01-20 11:18:57 +00:00
v2_r32 dMouseP;
v2_r32 MouseP;
2023-12-23 07:27:22 +00:00
glyph_atlas *GlyphAtlas;
2023-06-17 17:00:55 +00:00
};
//- sixten: State management
inline void UI_SetState(ui *UI);
inline ui *UI_GetState(void);
2023-09-13 04:42:11 +00:00
inline ui_key UI_HotKey(void);
inline ui_key UI_ActiveKey(void);
2023-07-19 15:09:41 +00:00
inline platform_event_list *UI_EventList(void);
inline arena *UI_FrameArena(void);
2024-01-20 11:18:57 +00:00
inline v2_r32 UI_MouseP(void);
2023-07-19 15:09:41 +00:00
inline glyph_atlas *UI_GlyphAtlas(void);
2023-06-17 17:00:55 +00:00
//- sixten: Drag helpers
2024-01-20 11:18:57 +00:00
inline void UI_SetDragStartP(v2_r32 P);
2023-06-17 17:00:55 +00:00
inline void UI_UpdateDragStartP(void);
2024-01-20 11:18:57 +00:00
inline v2_r32 UI_GetDragStartP(void);
inline void UI_StoreDragV2(v2_r32 DragData);
inline v2_r32 UI_GetDragV2(void);
2023-06-17 17:00:55 +00:00
inline void UI_StoreDragR32(r32 DragData);
inline r32 UI_GetDragR32(void);
inline void UI_StoreDragPayload(void *Data); // sixten(NOTE): Payload MUST be 64-bytes.
inline void UI_GetDragDataPayload(void *Data);
inline void UI_StoreDragPointer(void *Data);
inline void *UI_GetDragDataPointer(void);
//- sixten: Helpers
static r64 UI_Time(void);
2023-12-23 07:27:22 +00:00
static r64 UI_Blink(void);
static void UI_ResetBlink(void);
2023-06-17 17:00:55 +00:00
//- sixten: Key functions
2023-06-27 14:14:28 +00:00
static ui_key UI_EmptyKey(void);
static ui_key UI_SeedKey(ui_key Key, ui_key Seed);
2023-09-13 04:42:11 +00:00
static ui_key UI_KeyFromString(string String);
static string UI_BoxStringFromKey(ui_key Key);
static ui_box *UI_BoxFromKey(ui *UI, ui_key Key);
2023-06-17 17:00:55 +00:00
//- sixten: Box creation
2023-06-27 14:14:28 +00:00
static ui_box *UI_MakeBox(ui_box_flags Flags, string String);
static ui_box *UI_MakeBoxF(ui_box_flags Flags, char *Format, ...);
2023-08-22 03:19:51 +00:00
static void UI_EquipBoxText(ui_box *Box, string String);
static void UI_EquipBoxCustomDrawCallback(ui_box *Box, ui_custom_draw_callback *DrawCallback, void *Data);
2023-06-27 14:14:28 +00:00
//- sixten: User interaction
static ui_signal UI_SignalFromBox(ui_box *Box);
2023-06-17 17:00:55 +00:00
//- sixten: Building and rendering
2023-07-19 15:09:41 +00:00
static void UI_Init(ui *UI);
2024-01-20 11:18:57 +00:00
static void UI_BeginBuild(ui *UI, v2_r32 ScreenDim);
2023-06-27 14:14:28 +00:00
static void UI_EndBuild(void);
static void UI_RenderFrame(render_group *RenderGroup);
2024-01-20 11:18:57 +00:00
static void UI_NewFrame(ui *UI, platform_event_list *EventList, v2_r32 MouseP, r32 dtForFrame, glyph_atlas *GlyphAtlas);
2023-06-17 17:00:55 +00:00
#endif //VN_UI_H