second pass

This commit is contained in:
johan0A 2025-01-14 00:07:44 +01:00
parent f6a86434e2
commit 3cf1e2c74e
13 changed files with 407 additions and 884 deletions

View file

@ -4,48 +4,70 @@ const builtin = @import("builtin");
/// for direct calls to the clay c library
pub const cdefs = struct {
// TODO: should use @extern instead but zls does not yet support it well and that is more important
extern "c" fn Clay_MinMemorySize() u32;
extern "c" fn Clay_CreateArenaWithCapacityAndMemory(capacity: u32, offset: [*c]u8) Arena;
extern "c" fn Clay_SetPointerState(position: Vector2, pointer_down: bool) void;
extern "c" fn Clay_Initialize(arena: Arena, layout_dimensions: Dimensions) void;
extern "c" fn Clay_UpdateScrollContainers(is_pointer_active: bool, scroll_delta: Vector2, delta_time: f32) void;
extern "c" fn Clay_SetLayoutDimensions(dimensions: Dimensions) void;
extern "c" fn Clay_BeginLayout() void;
extern "c" fn Clay_EndLayout() ClayArray(RenderCommand);
extern "c" fn Clay_PointerOver(id: ElementId) bool;
extern "c" fn Clay_GetElementId(id: String) ElementId;
extern "c" fn Clay_GetScrollContainerData(id: ElementId) ScrollContainerData;
extern "c" fn Clay_SetMeasureTextFunction(measureTextFunction: *const fn (*String, *TextElementConfig) callconv(.C) Dimensions) void;
extern "c" fn Clay_RenderCommandArray_Get(array: *ClayArray(RenderCommand), index: i32) *RenderCommand;
extern "c" fn Clay_SetDebugModeEnabled(enabled: bool) void;
pub extern fn Clay_MinMemorySize() u32;
pub extern fn Clay_CreateArenaWithCapacityAndMemory(capacity: u32, offset: [*c]u8) Arena;
pub extern fn Clay_SetPointerState(position: Vector2, pointerDown: bool) void;
pub extern fn Clay_Initialize(arena: Arena, layoutDimensions: Dimensions, errorHandler: ErrorHandler) *Context;
pub extern fn Clay_GetCurrentContext() *Context;
pub extern fn Clay_SetCurrentContext(context: *Context) void;
pub extern fn Clay_UpdateScrollContainers(enableDragScrolling: bool, scrollDelta: Vector2, deltaTime: f32) void;
pub extern fn Clay_SetLayoutDimensions(dimensions: Dimensions) void;
pub extern fn Clay_BeginLayout() void;
pub extern fn Clay_EndLayout() ClayArray(RenderCommand);
pub extern fn Clay_GetElementId(idString: String) ElementId;
pub extern fn Clay_GetElementIdWithIndex(idString: String, index: u32) ElementId;
pub extern fn Clay_Hovered() bool;
pub extern fn Clay_OnHover(onHoverFunction: ?*const fn (ElementId, PointerData, isize) callconv(.c) void, userData: isize) void;
pub extern fn Clay_PointerOver(elementId: ElementId) bool;
pub extern fn Clay_GetScrollContainerData(id: ElementId) ScrollContainerData;
pub extern fn Clay_SetMeasureTextFunction(measureTextFunction: *const fn (*String, *TextElementConfig) callconv(.c) Dimensions) void;
pub extern fn Clay_SetQueryScrollOffsetFunction(queryScrollOffsetFunction: ?*const fn (u32) callconv(.c) Vector2) void;
pub extern fn Clay_RenderCommandArray_Get(array: *ClayArray(RenderCommand), index: i32) *RenderCommand;
pub extern fn Clay_SetDebugModeEnabled(enabled: bool) void;
pub extern fn Clay_IsDebugModeEnabled() bool;
pub extern fn Clay_SetCullingEnabled(enabled: bool) void;
pub extern fn Clay_GetMaxElementCount() i32;
pub extern fn Clay_SetMaxElementCount(maxElementCount: i32) void;
pub extern fn Clay_GetMaxMeasureTextCacheWordCount() i32;
pub extern fn Clay_SetMaxMeasureTextCacheWordCount(maxMeasureTextCacheWordCount: i32) void;
pub extern fn Clay_ResetMeasureTextCache() void;
extern "c" fn Clay__OpenElement() void;
extern "c" fn Clay__CloseElement() void;
extern "c" fn Clay__ElementPostConfiguration() void;
extern "c" fn Clay__OpenTextElement(text: String, textConfig: *TextElementConfig) void;
extern "c" fn Clay__AttachId(id: ElementId) void;
extern "c" fn Clay__AttachLayoutConfig(layoutConfig: *LayoutConfig) void;
extern "c" fn Clay__AttachElementConfig(config: *anyopaque, type: ElementConfigType) void;
extern "c" fn Clay__StoreLayoutConfig(config: LayoutConfig) *LayoutConfig;
extern "c" fn Clay__StoreRectangleElementConfig(config: RectangleElementConfig) *RectangleElementConfig;
extern "c" fn Clay__StoreTextElementConfig(config: TextElementConfig) *TextElementConfig;
extern "c" fn Clay__StoreImageElementConfig(config: ImageElementConfig) *ImageElementConfig;
extern "c" fn Clay__StoreFloatingElementConfig(config: FloatingElementConfig) *FloatingElementConfig;
extern "c" fn Clay__StoreCustomElementConfig(config: CustomElementConfig) *CustomElementConfig;
extern "c" fn Clay__StoreScrollElementConfig(config: ScrollElementConfig) *ScrollElementConfig;
extern "c" fn Clay__StoreBorderElementConfig(config: BorderElementConfig) *BorderElementConfig;
extern "c" fn Clay__HashString(toHash: String, index: u32, seed: u32) ElementId;
extern "c" fn Clay__GetOpenLayoutElementId() u32;
pub extern fn Clay__OpenElement() void;
pub extern fn Clay__CloseElement() void;
pub extern fn Clay__StoreLayoutConfig(config: LayoutConfig) *LayoutConfig;
pub extern fn Clay__ElementPostConfiguration() void;
pub extern fn Clay__AttachId(id: ElementId) void;
pub extern fn Clay__AttachLayoutConfig(config: *LayoutConfig) void;
pub extern fn Clay__AttachElementConfig(config: ElementConfigUnion, @"type": ElementConfigType) void;
pub extern fn Clay__StoreRectangleElementConfig(config: RectangleElementConfig) *RectangleElementConfig;
pub extern fn Clay__StoreTextElementConfig(config: TextElementConfig) *TextElementConfig;
pub extern fn Clay__StoreImageElementConfig(config: ImageElementConfig) *ImageElementConfig;
pub extern fn Clay__StoreFloatingElementConfig(config: FloatingElementConfig) *FloatingElementConfig;
pub extern fn Clay__StoreCustomElementConfig(config: CustomElementConfig) *CustomElementConfig;
pub extern fn Clay__StoreScrollElementConfig(config: ScrollElementConfig) *ScrollElementConfig;
pub extern fn Clay__StoreBorderElementConfig(config: BorderElementConfig) *BorderElementConfig;
pub extern fn Clay__HashString(key: String, offset: u32, seed: u32) ElementId;
pub extern fn Clay__OpenTextElement(text: String, textConfig: *TextElementConfig) void;
pub extern fn Clay__GetParentElementId() u32;
pub extern var CLAY_LAYOUT_DEFAULT: LayoutConfig;
pub extern var Clay__debugViewHighlightColor: Color;
pub extern var Clay__debugViewWidth: u32;
};
pub const EnumBackingType = u8;
pub const String = extern struct {
length: c_int,
chars: [*c]c_char,
length: i32,
chars: [*:0]const u8,
};
pub const Vector2 = extern struct {
x: f32,
y: f32,
pub const Context = opaque {};
pub const Arena = extern struct {
nextAllocation: usize,
capacity: usize,
memory: [*]u8,
};
pub const Dimensions = extern struct {
@ -53,13 +75,13 @@ pub const Dimensions = extern struct {
h: f32,
};
pub const Arena = extern struct {
label: String,
next_allocation: u64,
capacity: u64,
memory: [*c]c_char,
pub const Vector2 = extern struct {
x: f32,
y: f32,
};
pub const Color = [4]f32;
pub const BoundingBox = extern struct {
x: f32,
y: f32,
@ -67,7 +89,177 @@ pub const BoundingBox = extern struct {
height: f32,
};
pub const Color = [4]f32;
pub const SizingMinMax = extern struct {
min: f32 = 0,
max: f32 = 0,
};
const SizingConstraint = extern union {
minmax: SizingMinMax,
percent: f32,
};
pub const SizingAxis = extern struct {
// Note: `min` is used for CLAY_SIZING_PERCENT, slightly different to clay.h due to lack of C anonymous unions
size: SizingConstraint = .{ .minmax = .{} },
type: SizingType = .FIT,
pub const grow = SizingAxis{ .type = .GROW, .size = .{ .minmax = .{ .min = 0, .max = 0 } } };
pub const fit = SizingAxis{ .type = .FIT, .size = .{ .minmax = .{ .min = 0, .max = 0 } } };
pub fn growMinMax(size_minmax: SizingMinMax) SizingAxis {
return .{ .type = .GROW, .size = .{ .minmax = size_minmax } };
}
pub fn fitMinMax(size_minmax: SizingMinMax) SizingAxis {
return .{ .type = .FIT, .size = .{ .minmax = size_minmax } };
}
pub fn fixed(size: f32) SizingAxis {
return .{ .type = .FIXED, .size = .{ .minmax = .{ .max = size, .min = size } } };
}
pub fn percent(size_percent: f32) SizingAxis {
return .{ .type = .PERCENT, .size = .{ .percent = size_percent } };
}
};
pub const Sizing = extern struct {
/// width
w: SizingAxis = .{},
/// height
h: SizingAxis = .{},
pub const grow = Sizing{ .h = .grow, .w = .grow };
};
pub const Padding = extern struct {
x: u16 = 0,
y: u16 = 0,
pub fn all(size: u16) Padding {
return Padding{
.x = size,
.y = size,
};
}
};
pub const TextElementConfigWrapMode = enum(c_uint) {
words = 0,
newlines = 1,
none = 2,
};
pub const TextElementConfig = extern struct {
color: Color = .{ 0, 0, 0, 255 },
font_id: u16 = 0,
font_size: u16 = 20,
letter_spacing: u16 = 0,
line_height: u16 = 0,
wrap_mode: TextElementConfigWrapMode = .words,
};
pub const FloatingAttachPointType = enum(u8) {
LEFT_TOP = 0,
LEFT_CENTER = 1,
LEFT_BOTTOM = 2,
CENTER_TOP = 3,
CENTER_CENTER = 4,
CENTER_BOTTOM = 5,
RIGHT_TOP = 6,
RIGHT_CENTER = 7,
RIGHT_BOTTOM = 8,
};
pub const FloatingAttachPoints = extern struct {
element: FloatingAttachPointType,
parent: FloatingAttachPointType,
};
pub const PointerCaptureMode = enum(c_uint) {
CAPTURE = 0,
PASSTHROUGH = 1,
};
pub const FloatingElementConfig = extern struct {
offset: Vector2,
expand: Dimensions,
zIndex: u16,
parentId: u32,
attachment: FloatingAttachPoints,
pointerCaptureMode: PointerCaptureMode,
};
pub const Border = extern struct {
width: u32,
color: Color,
};
pub const ElementConfigUnion = extern union {
rectangle_config: *RectangleElementConfig,
text_config: *TextElementConfig,
image_config: *ImageElementConfig,
floating_config: *FloatingElementConfig,
custom_config: *CustomElementConfig,
scroll_config: *ScrollElementConfig,
border_config: *BorderElementConfig,
};
pub const ElementConfig = extern struct {
type: ElementConfigType,
config: ElementConfigUnion,
};
pub const RenderCommandType = enum(u8) {
none = 0,
rectangle = 1,
border = 2,
text = 3,
image = 4,
scissor_start = 5,
scissor_end = 6,
custom = 7,
};
pub const RenderCommandArray = extern struct {
capacity: i32,
length: i32,
internalArray: [*]RenderCommand,
};
pub const PointerDataInteractionState = enum(c_uint) {
pressed_this_frame = 0,
pressed = 1,
released_this_frame = 2,
released = 3,
};
pub const PointerData = extern struct {
position: Vector2,
state: PointerDataInteractionState,
};
pub const ErrorType = enum(c_uint) {
text_measurement_function_not_provided = 0,
arena_capacity_exceeded = 1,
elements_capacity_exceeded = 2,
text_measurement_capacity_exceeded = 3,
duplicate_id = 4,
floating_container_parent_not_found = 5,
internal_error = 6,
};
pub const ErrorData = extern struct {
errorType: ErrorType,
errorText: String,
userData: usize,
};
pub const ErrorHandler = extern struct {
error_handler_function: ?*const fn (ErrorData) callconv(.c) void = null,
user_data: usize = 0,
};
pub const CornerRadius = extern struct {
top_left: f32 = 0,
@ -97,50 +289,6 @@ pub const ElementId = extern struct {
string_id: String,
};
pub const EnumBackingType = u8;
pub const RenderCommandType = enum(EnumBackingType) {
None,
Rectangle,
Border,
Text,
Image,
ScissorStart,
ScissorEnd,
Custom,
};
pub const TextWrapMode = enum(EnumBackingType) {
Words,
Newlines,
None,
};
pub const FloatingAttachPointType = enum(EnumBackingType) {
LEFT_TOP,
LEFT_CENTER,
LEFT_BOTTOM,
CENTER_TOP,
CENTER_CENTER,
CENTER_BOTTOM,
RIGHT_TOP,
RIGHT_CENTER,
RIGHT_BOTTOM,
};
pub const FloatingAttachPoints = extern struct {
element: FloatingAttachPointType,
parent: FloatingAttachPointType,
};
pub const ElementConfigUnion = extern union {
rectangle_element_config: *RectangleElementConfig,
text_element_config: *TextElementConfig,
image_element_config: *ImageElementConfig,
custom_element_config: *CustomElementConfig,
border_element_config: *BorderElementConfig,
};
pub const RenderCommand = extern struct {
bounding_box: BoundingBox,
config: ElementConfigUnion,
@ -161,83 +309,32 @@ pub const ScrollContainerData = extern struct {
};
pub const SizingType = enum(EnumBackingType) {
FIT,
GROW,
PERCENT,
FIXED,
};
pub const SizingConstraintsMinMax = extern struct {
min: f32 = 0,
max: f32 = 0,
FIT = 0,
GROW = 1,
PERCENT = 2,
FIXED = 3,
};
pub const SizingConstraints = extern union {
size_minmax: SizingConstraintsMinMax,
size_minmax: SizingMinMax,
size_percent: f32,
};
pub const SizingAxis = extern struct {
// Note: `min` is used for CLAY_SIZING_PERCENT, slightly different to clay.h due to lack of C anonymous unions
constraints: SizingConstraints = .{ .size_minmax = .{} },
type: SizingType = .FIT,
pub const grow = SizingAxis{ .type = .GROW, .constraints = .{ .size_minmax = .{ .min = 0, .max = 0 } } };
pub const fit = SizingAxis{ .type = .FIT, .constraints = .{ .size_minmax = .{ .min = 0, .max = 0 } } };
pub fn growMinMax(size_minmax: SizingConstraintsMinMax) SizingAxis {
return .{ .type = .GROW, .constraints = .{ .size_minmax = size_minmax } };
}
pub fn fitMinMax(size_minmax: SizingConstraintsMinMax) SizingAxis {
return .{ .type = .FIT, .constraints = .{ .size_minmax = size_minmax } };
}
pub fn fixed(size: f32) SizingAxis {
return .{ .type = .FIXED, .constraints = .{ .size_minmax = .{ .max = size, .min = size } } };
}
pub fn percent(size_percent: f32) SizingAxis {
return .{ .type = .PERCENT, .constraints = .{ .size_percent = size_percent } };
}
};
pub const Sizing = extern struct {
/// width
w: SizingAxis = .{},
/// height
h: SizingAxis = .{},
pub const grow = Sizing{ .h = .grow, .w = .grow };
};
pub const Padding = extern struct {
x: u16 = 0,
y: u16 = 0,
pub fn all(size: u16) Padding {
return Padding{
.x = size,
.y = size,
};
}
};
pub const LayoutDirection = enum(EnumBackingType) {
LEFT_TO_RIGHT = 0,
TOP_TO_BOTTOM = 1,
};
pub const LayoutAlignmentX = enum(EnumBackingType) {
LEFT,
RIGHT,
CENTER,
LEFT = 0,
RIGHT = 1,
CENTER = 2,
};
pub const LayoutAlignmentY = enum(EnumBackingType) {
TOP,
BOTTOM,
CENTER,
TOP = 0,
BOTTOM = 1,
CENTER = 2,
};
pub const ChildAlignment = extern struct {
@ -306,28 +403,11 @@ pub const BorderElementConfig = extern struct {
}
};
pub const TextElementConfig = extern struct {
color: Color = .{ 0, 0, 0, 255 },
font_id: u16 = 0,
font_size: u16 = 20,
letter_spacing: u16 = 0,
line_height: u16 = 0,
wrap_mode: TextWrapMode = .Words,
};
pub const ImageElementConfig = extern struct {
image_data: *const anyopaque,
source_dimensions: Dimensions,
};
pub const FloatingElementConfig = extern struct {
offset: Vector2,
expand: Dimensions,
z_index: u16,
parent_id: u32,
attachment: FloatingAttachPoints,
};
pub const CustomElementConfig = extern struct {
custom_data: *anyopaque,
};
@ -338,52 +418,49 @@ pub const ScrollElementConfig = extern struct {
};
pub const ElementConfigType = enum(EnumBackingType) {
Rectangle = 1,
Border = 2,
Floating = 4,
Scroll = 8,
Image = 16,
Text = 32,
Custom = 64,
rectangle_config = 1,
border_config = 2,
floating_config = 4,
scroll_config = 8,
image_config = 16,
text_config = 32,
custom_config = 64,
// zig specific enum types
id,
Layout,
layout_config,
};
pub const Config = union(ElementConfigType) {
Rectangle: *RectangleElementConfig,
Border: *BorderElementConfig,
Floating: *FloatingElementConfig,
Scroll: *ScrollElementConfig,
Image: *ImageElementConfig,
Text: *TextElementConfig,
Custom: *CustomElementConfig,
rectangle_config: *RectangleElementConfig,
border_config: *BorderElementConfig,
floating_config: *FloatingElementConfig,
scroll_config: *ScrollElementConfig,
image_config: *ImageElementConfig,
text_config: *TextElementConfig,
custom_config: *CustomElementConfig,
id: ElementId,
Layout: *LayoutConfig,
layout_config: *LayoutConfig,
pub fn layout(config: LayoutConfig) Config {
return Config{ .Layout = cdefs.Clay__StoreLayoutConfig(config) };
}
pub fn rectangle(config: RectangleElementConfig) Config {
return Config{ .Rectangle = cdefs.Clay__StoreRectangleElementConfig(config) };
}
pub fn text(config: TextElementConfig) Config {
return Config{ .Text = cdefs.Clay__StoreTextElementConfig(config) };
}
pub fn image(config: ImageElementConfig) Config {
return Config{ .Image = cdefs.Clay__StoreImageElementConfig(config) };
}
pub fn floating(config: FloatingElementConfig) Config {
return Config{ .Floating = cdefs.Clay__StoreFloatingElementConfig(config) };
}
pub fn custom(config: CustomElementConfig) Config {
return Config{ .Custom = cdefs.Clay__StoreCustomElementConfig(config) };
}
pub fn scroll(config: ScrollElementConfig) Config {
return Config{ .Scroll = cdefs.Clay__StoreScrollElementConfig(config) };
return Config{ .rectangle_config = cdefs.Clay__StoreRectangleElementConfig(config) };
}
pub fn border(config: BorderElementConfig) Config {
return Config{ .Border = cdefs.Clay__StoreBorderElementConfig(config) };
return Config{ .border_config = cdefs.Clay__StoreBorderElementConfig(config) };
}
pub fn floating(config: FloatingElementConfig) Config {
return Config{ .floating_config = cdefs.Clay__StoreFloatingElementConfig(config) };
}
pub fn scroll(config: ScrollElementConfig) Config {
return Config{ .scroll_config = cdefs.Clay__StoreScrollElementConfig(config) };
}
pub fn image(config: ImageElementConfig) Config {
return Config{ .image_config = cdefs.Clay__StoreImageElementConfig(config) };
}
pub fn text(config: TextElementConfig) Config {
return Config{ .text_config = cdefs.Clay__StoreTextElementConfig(config) };
}
pub fn custom(config: CustomElementConfig) Config {
return Config{ .custom_config = cdefs.Clay__StoreCustomElementConfig(config) };
}
pub fn ID(string: []const u8) Config {
return Config{ .id = hashString(makeClayString(string), 0, 0) };
@ -391,10 +468,12 @@ pub const Config = union(ElementConfigType) {
pub fn IDI(string: []const u8, index: u32) Config {
return Config{ .id = hashString(makeClayString(string), index, 0) };
}
pub fn layout(config: LayoutConfig) Config {
return Config{ .layout_config = cdefs.Clay__StoreLayoutConfig(config) };
}
};
pub const minMemorySize = cdefs.Clay_MinMemorySize;
pub const createArenaWithCapacityAndMemory = cdefs.Clay_CreateArenaWithCapacityAndMemory;
pub const initialize = cdefs.Clay_Initialize;
pub const setLayoutDimensions = cdefs.Clay_SetLayoutDimensions;
pub const beginLayout = cdefs.Clay_BeginLayout;
@ -404,64 +483,27 @@ pub const renderCommandArrayGet = cdefs.Clay_RenderCommandArray_Get;
pub const setDebugModeEnabled = cdefs.Clay_SetDebugModeEnabled;
pub const hashString = cdefs.Clay__HashString;
pub fn createArenaWithCapacityAndMemory(buffer: []u8) Arena {
return cdefs.Clay_CreateArenaWithCapacityAndMemory(@intCast(buffer.len), buffer.ptr);
}
pub inline fn UI(configs: []const Config) fn (void) void {
std.debug.print("-> OPEN ELEMENT\n", .{});
cdefs.Clay__OpenElement();
for (configs) |config| {
switch (config) {
.Layout => |layoutConf| {
std.debug.print("LAYOUT CONF: {}\n", .{layoutConf});
cdefs.Clay__AttachLayoutConfig(layoutConf);
},
.layout_config => |layoutConf| cdefs.Clay__AttachLayoutConfig(layoutConf),
.id => |id| cdefs.Clay__AttachId(id),
inline else => |elem_config| {
std.debug.print("elem_config CONF: {}\n", .{elem_config});
cdefs.Clay__AttachElementConfig(@ptrCast(elem_config), config);
},
inline else => |elem_config, tag| cdefs.Clay__AttachElementConfig(@unionInit(ElementConfigUnion, @tagName(tag), elem_config), config),
}
}
std.debug.print("-> POST ELEMENT\n", .{});
cdefs.Clay__ElementPostConfiguration();
return struct {
fn f(_: void) void {
std.time.sleep(1000);
std.debug.print("-> CLOSE ELEMENT\n", .{});
cdefs.Clay__CloseElement();
}
}.f;
}
pub fn OPEN(configs: []const Config) bool {
std.debug.print("-> OPEN ELEMENT\n", .{});
cdefs.Clay__OpenElement();
for (configs) |config| {
switch (config) {
.Layout => |layoutConf| {
std.debug.print("LAYOUT CONF: {}\n", .{layoutConf});
cdefs.Clay__AttachLayoutConfig(layoutConf);
},
.id => |id| cdefs.Clay__AttachId(id),
inline else => |elem_config| {
std.debug.print("elem_config CONF: {}\n", .{elem_config});
cdefs.Clay__AttachElementConfig(@ptrCast(elem_config), config);
},
}
}
std.debug.print("-> POST ELEMENT\n", .{});
cdefs.Clay__ElementPostConfiguration();
return true;
}
pub fn CLOSE() void {
std.debug.print("-> CLOSE ELEMENT\n", .{});
cdefs.Clay__CloseElement();
}
pub fn singleElem(configs: []const Config) void {
_ = OPEN(configs);
CLOSE();
}
pub fn endLayout() ClayArray(RenderCommand) {
return cdefs.Clay_EndLayout();
}
@ -490,8 +532,7 @@ pub fn makeClayString(string: []const u8) String {
}
pub fn text(string: []const u8, config: Config) void {
// std.debug.print("TEXT 2 conf: {}\n", .{config.Text});
cdefs.Clay__OpenTextElement(makeClayString(string), config.Text);
cdefs.Clay__OpenTextElement(makeClayString(string), config.text_config);
}
pub fn ID(string: []const u8) ElementId {