updated clay.h to latest commit

This commit is contained in:
johan0A 2025-02-06 23:50:42 +01:00
parent 1c4d5a8b42
commit 0504787d85
8 changed files with 785 additions and 697 deletions

View file

@ -8,8 +8,9 @@ pub extern var Clay__debugViewWidth: u32;
/// 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
pub extern fn Clay_GetElementData(id: ElementId) ElementData;
pub extern fn Clay_MinMemorySize() u32;
pub extern fn Clay_CreateArenaWithCapacityAndMemory(capacity: u32, offset: [*c]u8) Arena;
pub extern fn Clay_CreateArenaWithCapacityAndMemory(capacity: u32, offset: ?*anyopaque) 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;
@ -21,11 +22,11 @@ pub const cdefs = struct {
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, usize) callconv(.c) void, userData: usize) void;
pub extern fn Clay_OnHover(onHoverFunction: *const fn (ElementId, PointerData, ?*anyopaque) callconv(.c) void, userData: ?*anyopaque) 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_SetMeasureTextFunction(measureTextFunction: *const fn (StringSlice, *TextElementConfig, ?*anyopaque) callconv(.c) Dimensions, userData: ?*anyopaque) void;
pub extern fn Clay_SetQueryScrollOffsetFunction(queryScrollOffsetFunction: *const fn (u32, ?*anyopaque) callconv(.c) Vector2, userData: ?*anyopaque) 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;
@ -36,20 +37,12 @@ pub const cdefs = struct {
pub extern fn Clay_SetMaxMeasureTextCacheWordCount(maxMeasureTextCacheWordCount: i32) void;
pub extern fn Clay_ResetMeasureTextCache() void;
pub extern fn Clay__ConfigureOpenElement(config: ElementDeclaration) void;
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__AttachId(id: ElementId) ElementId;
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;
@ -59,7 +52,13 @@ pub const EnumBackingType = u8;
pub const String = extern struct {
length: i32,
chars: [*:0]const u8,
chars: [*]const u8,
};
pub const StringSlice = extern struct {
length: i32 = 0,
chars: [*]const u8,
base_chars: [*]const u8,
};
pub const Context = opaque {};
@ -80,6 +79,7 @@ pub const Vector2 = extern struct {
y: f32,
};
/// order: r, g, b, a
pub const Color = [4]f32;
pub const BoundingBox = extern struct {
@ -102,25 +102,25 @@ const SizingConstraint = extern union {
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,
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 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 } };
return .{ .type = .grow, .size = .{ .minmax = size_minmax } };
}
pub fn fitMinMax(size_minmax: SizingMinMax) SizingAxis {
return .{ .type = .FIT, .size = .{ .minmax = size_minmax } };
return .{ .type = .fit, .size = .{ .minmax = size_minmax } };
}
pub fn fixed(size: f32) SizingAxis {
return .{ .type = .FIXED, .size = .{ .minmax = .{ .max = size, .min = size } } };
return .{ .type = .fixed, .size = .{ .minmax = .{ .max = size, .min = size } } };
}
pub fn percent(size_percent: f32) SizingAxis {
return .{ .type = .PERCENT, .size = .{ .percent = size_percent } };
return .{ .type = .percent, .size = .{ .percent = size_percent } };
}
};
@ -134,20 +134,33 @@ pub const Sizing = extern struct {
};
pub const Padding = extern struct {
x: u16 = 0,
y: u16 = 0,
left: u16 = 0,
right: u16 = 0,
top: u16 = 0,
bottom: u16 = 0,
pub fn xy(vertical_padding: u16, horizontal_padding: u16) Padding {
return .{
.top = vertical_padding,
.bottom = vertical_padding,
.left = horizontal_padding,
.right = horizontal_padding,
};
}
pub fn all(size: u16) Padding {
return Padding{
.x = size,
.y = size,
.left = size,
.right = size,
.top = size,
.bottom = size,
};
}
};
pub const TextElementConfigWrapMode = enum(c_uint) {
pub const TextElementConfigWrapMode = enum(EnumBackingType) {
words = 0,
newlines = 1,
new_lines = 1,
none = 2,
};
@ -158,18 +171,19 @@ pub const TextElementConfig = extern struct {
letter_spacing: u16 = 0,
line_height: u16 = 0,
wrap_mode: TextElementConfigWrapMode = .words,
hash_string_contents: bool = false,
};
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 FloatingAttachPointType = enum(EnumBackingType) {
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 {
@ -177,41 +191,29 @@ pub const FloatingAttachPoints = extern struct {
parent: FloatingAttachPointType,
};
pub const PointerCaptureMode = enum(c_uint) {
CAPTURE = 0,
PASSTHROUGH = 1,
pub const FloatingAttachToElement = enum(EnumBackingType) {
to_none = 0,
to_parent = 1,
to_element_with_id = 2,
to_root = 3,
};
pub const PointerCaptureMode = enum(EnumBackingType) {
capture = 0,
passthrough = 1,
};
pub const FloatingElementConfig = extern struct {
offset: Vector2,
expand: Dimensions,
zIndex: u16,
parentId: u32,
attachment: FloatingAttachPoints,
pointerCaptureMode: PointerCaptureMode,
offset: Vector2 = .{ .x = 0, .y = 0 },
expand: Dimensions = .{ .w = 0, .h = 0 },
parentId: u32 = 0,
zIndex: i16 = 0,
attach_points: FloatingAttachPoints = .{ .element = .left_top, .parent = .left_top },
pointer_capture_mode: PointerCaptureMode = .capture,
attach_to: FloatingAttachToElement = .to_none,
};
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) {
pub const RenderCommandType = enum(EnumBackingType) {
none = 0,
rectangle = 1,
border = 2,
@ -222,13 +224,7 @@ pub const RenderCommandType = enum(u8) {
custom = 7,
};
pub const RenderCommandArray = extern struct {
capacity: i32,
length: i32,
internalArray: [*]RenderCommand,
};
pub const PointerDataInteractionState = enum(c_uint) {
pub const PointerDataInteractionState = enum(EnumBackingType) {
pressed_this_frame = 0,
pressed = 1,
released_this_frame = 2,
@ -240,7 +236,7 @@ pub const PointerData = extern struct {
state: PointerDataInteractionState,
};
pub const ErrorType = enum(c_uint) {
pub const ErrorType = enum(EnumBackingType) {
text_measurement_function_not_provided = 0,
arena_capacity_exceeded = 1,
elements_capacity_exceeded = 2,
@ -251,14 +247,14 @@ pub const ErrorType = enum(c_uint) {
};
pub const ErrorData = extern struct {
errorType: ErrorType,
errorText: String,
userData: usize,
error_type: ErrorType,
error_text: String,
user_data: ?*anyopaque,
};
pub const ErrorHandler = extern struct {
error_handler_function: ?*const fn (ErrorData) callconv(.c) void = null,
user_data: usize = 0,
user_data: ?*anyopaque = null,
};
pub const CornerRadius = extern struct {
@ -277,26 +273,173 @@ pub const CornerRadius = extern struct {
}
};
pub const BorderData = extern struct {
width: u32 = 0,
color: Color = .{ 0, 0, 0, 0 },
};
pub const ElementId = extern struct {
id: u32,
offset: u32,
base_id: u32,
string_id: String,
pub fn ID(string: []const u8) ElementId {
return cdefs.Clay__HashString(makeClayString(string), 0, 0);
}
pub fn IDI(string: []const u8, index: u32) ElementId {
return cdefs.Clay__HashString(makeClayString(string), index, 0);
}
pub fn localID(string: []const u8) ElementId {
return cdefs.Clay__HashString(makeClayString(string), 0, cdefs.Clay__GetParentElementId());
}
pub fn localIDI(string: []const u8, index: u32) ElementId {
return cdefs.Clay__HashString(makeClayString(string), index, cdefs.Clay__GetParentElementId());
}
};
pub const RenderCommand = extern struct {
bounding_box: BoundingBox,
config: ElementConfigUnion,
text: String,
render_data: RenderData,
user_data: *anyopaque,
id: u32,
z_index: i16,
command_type: RenderCommandType,
};
pub const SizingType = enum(EnumBackingType) {
fit = 0,
grow = 1,
percent = 2,
fixed = 3,
};
pub const LayoutDirection = enum(EnumBackingType) {
left_to_right = 0,
top_to_bottom = 1,
};
pub const LayoutAlignmentX = enum(EnumBackingType) {
left = 0,
right = 1,
center = 2,
};
pub const LayoutAlignmentY = enum(EnumBackingType) {
top = 0,
bottom = 1,
center = 2,
};
pub const ChildAlignment = extern struct {
x: LayoutAlignmentX = .left,
y: LayoutAlignmentY = .top,
pub const center = ChildAlignment{ .x = .center, .y = .center };
};
pub const LayoutConfig = extern struct {
/// sizing of the element
sizing: Sizing = .{},
/// padding arround children
padding: Padding = .{},
/// gap between the children
child_gap: u16 = 0,
/// alignment of the children
child_alignment: ChildAlignment = .{},
/// direction of the children's layout
direction: LayoutDirection = .left_to_right,
};
pub fn ClayArray(comptime T: type) type {
return extern struct {
capacity: u32,
length: u32,
internal_array: [*]T,
};
}
pub const BorderWidth = extern struct {
left: u16 = 0,
right: u16 = 0,
top: u16 = 0,
bottom: u16 = 0,
between_children: u16 = 0,
pub fn outside(width: u16) BorderWidth {
return .{
.left = width,
.right = width,
.top = width,
.bottom = width,
.between_children = 0,
};
}
pub fn all(width: u16) BorderWidth {
return .{
.left = width,
.right = width,
.top = width,
.bottom = width,
.between_children = width,
};
}
};
pub const BorderElementConfig = extern struct {
color: Color = .{ 0, 0, 0, 255 },
width: BorderWidth = .{},
};
pub const TextRenderData = extern struct {
string_contents: StringSlice,
text_color: Color,
font_id: u16,
font_size: u16,
letter_spacing: u16,
line_height: u16,
};
pub const RectangleRenderData = extern struct {
background_color: Color,
corner_radius: CornerRadius,
};
pub const ImageRenderData = extern struct {
background_color: Color,
corner_radius: CornerRadius,
source_dimensions: Dimensions,
image_data: ?*anyopaque,
};
pub const CustomRenderData = extern struct {
background_color: Color,
corner_radius: CornerRadius,
custom_data: ?*anyopaque,
};
pub const ImageElementConfig = extern struct {
image_data: ?*const anyopaque,
source_dimensions: Dimensions,
};
pub const BorderRenderData = extern struct {
color: Color,
corner_radius: CornerRadius,
width: BorderWidth,
};
pub const RenderData = extern union {
rectangle: RectangleRenderData,
text: TextRenderData,
image: ImageRenderData,
custom: CustomRenderData,
border: BorderRenderData,
};
pub const CustomElementConfig = extern struct {
custom_data: ?*anyopaque = null,
};
pub const ScrollContainerData = extern struct {
// Note: This is a pointer to the real internal scroll position, mutating it may cause a change in final layout.
// Intended for use with external functionality that modifies scroll position, such as scroll bars or auto scrolling.
@ -308,108 +451,9 @@ pub const ScrollContainerData = extern struct {
found: bool,
};
pub const SizingType = enum(EnumBackingType) {
FIT = 0,
GROW = 1,
PERCENT = 2,
FIXED = 3,
};
pub const SizingConstraints = extern union {
size_minmax: SizingMinMax,
size_percent: f32,
};
pub const LayoutDirection = enum(EnumBackingType) {
LEFT_TO_RIGHT = 0,
TOP_TO_BOTTOM = 1,
};
pub const LayoutAlignmentX = enum(EnumBackingType) {
LEFT = 0,
RIGHT = 1,
CENTER = 2,
};
pub const LayoutAlignmentY = enum(EnumBackingType) {
TOP = 0,
BOTTOM = 1,
CENTER = 2,
};
pub const ChildAlignment = extern struct {
x: LayoutAlignmentX = .LEFT,
y: LayoutAlignmentY = .TOP,
pub const CENTER = ChildAlignment{ .x = .CENTER, .y = .CENTER };
};
pub const LayoutConfig = extern struct {
/// sizing of the element
sizing: Sizing = .{},
/// padding arround children
padding: Padding = .{},
/// gap between the children
child_gap: u16 = 0,
/// alignement of the children
child_alignment: ChildAlignment = .{},
/// direction of the children's layout
direction: LayoutDirection = .LEFT_TO_RIGHT,
};
pub fn ClayArray(comptime T: type) type {
return extern struct {
capacity: u32,
length: u32,
internal_array: [*]T,
};
}
pub const RectangleElementConfig = extern struct {
color: Color = .{ 255, 255, 255, 255 },
corner_radius: CornerRadius = .{},
};
pub const BorderElementConfig = extern struct {
left: BorderData = .{},
right: BorderData = .{},
top: BorderData = .{},
bottom: BorderData = .{},
between_children: BorderData = .{},
corner_radius: CornerRadius = .{},
pub fn outside(color: Color, width: u32, radius: f32) BorderElementConfig {
const data = BorderData{ .color = color, .width = width };
return BorderElementConfig{
.left = data,
.right = data,
.top = data,
.bottom = data,
.between_children = .{},
.corner_radius = .all(radius),
};
}
pub fn all(color: Color, width: u32, radius: f32) BorderElementConfig {
const data = BorderData{ .color = color, .width = width };
return BorderElementConfig{
.left = data,
.right = data,
.top = data,
.bottom = data,
.between_children = data,
.corner_radius = .all(radius),
};
}
};
pub const ImageElementConfig = extern struct {
image_data: *const anyopaque,
source_dimensions: Dimensions,
};
pub const CustomElementConfig = extern struct {
custom_data: *anyopaque,
pub const ElementData = extern struct {
bounding_box: BoundingBox,
found: bool,
};
pub const ScrollElementConfig = extern struct {
@ -417,65 +461,41 @@ pub const ScrollElementConfig = extern struct {
vertical: bool = false,
};
pub const SharedElementConfig = extern struct {
backgroundColor: Color,
cornerRadius: CornerRadius,
userData: ?*anyopaque,
};
pub const ElementConfigType = enum(EnumBackingType) {
rectangle_config = 1,
border_config = 2,
floating_config = 4,
scroll_config = 8,
image_config = 16,
text_config = 32,
custom_config = 64,
none = 0,
border = 1,
floating = 2,
scroll = 3,
image = 4,
text = 5,
custom = 6,
shared = 7,
};
pub const Attach = struct {
pub fn rectangle(config: RectangleElementConfig) Attach {
cdefs.Clay__AttachElementConfig(ElementConfigUnion{ .rectangle_config = cdefs.Clay__StoreRectangleElementConfig(config) }, .rectangle_config);
return .{};
}
pub fn border(config: BorderElementConfig) Attach {
cdefs.Clay__AttachElementConfig(ElementConfigUnion{ .border_config = cdefs.Clay__StoreBorderElementConfig(config) }, .border_config);
return .{};
}
pub fn floating(config: FloatingElementConfig) Attach {
cdefs.Clay__AttachElementConfig(ElementConfigUnion{ .floating_config = cdefs.Clay__StoreFloatingElementConfig(config) }, .floating_config);
return .{};
}
pub fn scroll(config: ScrollElementConfig) Attach {
cdefs.Clay__AttachElementConfig(ElementConfigUnion{ .scroll_config = cdefs.Clay__StoreScrollElementConfig(config) }, .scroll_config);
return .{};
}
pub fn image(config: ImageElementConfig) Attach {
cdefs.Clay__AttachElementConfig(ElementConfigUnion{ .image_config = cdefs.Clay__StoreImageElementConfig(config) }, .image_config);
return .{};
}
pub fn text(config: TextElementConfig) Attach {
cdefs.Clay__AttachElementConfig(ElementConfigUnion{ .text_config = cdefs.Clay__StoreTextElementConfig(config) }, .text_config);
return .{};
}
pub fn custom(config: CustomElementConfig) Attach {
cdefs.Clay__AttachElementConfig(ElementConfigUnion{ .custom_config = cdefs.Clay__StoreCustomElementConfig(config) }, .custom_config);
return .{};
}
pub fn ID(string: []const u8) Attach {
cdefs.Clay__AttachId(cdefs.Clay__HashString(makeClayString(string), 0, 0));
return .{};
}
pub fn IDI(string: []const u8, index: u32) Attach {
cdefs.Clay__AttachId(cdefs.Clay__HashString(makeClayString(string), index, 0));
return .{};
}
pub fn layout(config: LayoutConfig) Attach {
cdefs.Clay__AttachLayoutConfig(cdefs.Clay__StoreLayoutConfig(config));
return .{};
}
pub const ElementDeclaration = extern struct {
id: ElementId = .{ .base_id = 0, .id = 0, .offset = 0, .string_id = .{ .chars = undefined, .length = 0 } },
layout: LayoutConfig = .{},
background_color: Color = .{ 0, 0, 0, 0 },
corner_radius: CornerRadius = .{},
image: ImageElementConfig = .{ .image_data = null, .source_dimensions = .{ .h = 0, .w = 0 } },
floating: FloatingElementConfig = .{},
custom: CustomElementConfig = .{},
scroll: ScrollElementConfig = .{},
border: BorderElementConfig = .{},
user_data: ?*anyopaque = null,
};
pub inline fn UI() fn (configs: []const Attach) callconv(.@"inline") fn (void) void {
pub inline fn UI() fn (config: ElementDeclaration) callconv(.@"inline") fn (void) void {
cdefs.Clay__OpenElement();
return struct {
inline fn f(configs: []const Attach) fn (void) void {
_ = configs;
cdefs.Clay__ElementPostConfiguration();
inline fn f(config: ElementDeclaration) fn (void) void {
cdefs.Clay__ConfigureOpenElement(config);
return struct {
fn f(_: void) void {
cdefs.Clay__CloseElement();
@ -485,6 +505,7 @@ pub inline fn UI() fn (configs: []const Attach) callconv(.@"inline") fn (void) v
}.f;
}
pub const getElementData = cdefs.Clay_GetElementData;
pub const minMemorySize = cdefs.Clay_MinMemorySize;
pub const setPointerState = cdefs.Clay_SetPointerState;
pub const initialize = cdefs.Clay_Initialize;
@ -494,10 +515,10 @@ pub const updateScrollContainers = cdefs.Clay_UpdateScrollContainers;
pub const setLayoutDimensions = cdefs.Clay_SetLayoutDimensions;
pub const beginLayout = cdefs.Clay_BeginLayout;
pub const endLayout = cdefs.Clay_EndLayout;
pub const getElementIdWithIndex = cdefs.Clay_GetElementIdWithIndex;
pub const hovered = cdefs.Clay_Hovered;
pub const pointerOver = cdefs.Clay_PointerOver;
pub const getScrollContainerData = cdefs.Clay_GetScrollContainerData;
pub const setQueryScrollOffsetFunction = cdefs.Clay_SetQueryScrollOffsetFunction;
pub const renderCommandArrayGet = cdefs.Clay_RenderCommandArray_Get;
pub const setDebugModeEnabled = cdefs.Clay_SetDebugModeEnabled;
pub const isDebugModeEnabled = cdefs.Clay_IsDebugModeEnabled;
@ -508,43 +529,86 @@ pub const getMaxMeasureTextCacheWordCount = cdefs.Clay_GetMaxMeasureTextCacheWor
pub const setMaxMeasureTextCacheWordCount = cdefs.Clay_SetMaxMeasureTextCacheWordCount;
pub const resetMeasureTextCache = cdefs.Clay_ResetMeasureTextCache;
/// `context` must be of same size as a pointer
/// `context` must be of same size as a pointer or be of type `void`
pub fn onHover(
context: anytype,
user_data: anytype,
comptime onHoverFunction: fn (
context: @TypeOf(context),
element_id: ElementId,
pointer_data: PointerData,
context: @TypeOf(user_data),
) void,
) void {
if (@sizeOf(@TypeOf(context)) != @sizeOf(usize)) @compileError("`context` must be of same size as a pointer");
const ContextType = @TypeOf(context);
cdefs.Clay_OnHover(struct {
pub fn f(element_id: ElementId, pointer_data: PointerData, data: usize) callconv(.C) void {
onHoverFunction(
if (@typeInfo(ContextType) == .pointer)
@ptrFromInt(@as(usize, @bitCast(data)))
else
@bitCast(data),
element_id,
pointer_data,
);
}
}.f, context);
if (!(@TypeOf(user_data) == void) and @sizeOf(@TypeOf(user_data)) != @sizeOf(usize))
@compileError("`context` must be of same size as a pointer or be of type `void`");
cdefs.Clay_OnHover(
struct {
pub fn f(element_id: ElementId, pointer_data: PointerData, userData: ?*anyopaque) callconv(.C) void {
onHoverFunction(
element_id,
pointer_data,
AnyopaquePtrToAnytype(@TypeOf(user_data), userData),
);
}
}.f,
anytypeToAnyopaquePtr(user_data),
);
}
/// `context` must be of same size as a pointer or be of type `void`
pub fn setMeasureTextFunction(
user_data: anytype,
comptime measureTextFunction: fn (
[]const u8,
*TextElementConfig,
context: @TypeOf(user_data),
) Dimensions,
) void {
if (!(@TypeOf(user_data) == void) and @sizeOf(@TypeOf(user_data)) != @sizeOf(usize))
@compileError("`context` must be of same size as a pointer or be of type `void`");
cdefs.Clay_SetMeasureTextFunction(
struct {
pub fn f(string: StringSlice, config: *TextElementConfig, userData: ?*anyopaque) callconv(.c) Dimensions {
return measureTextFunction(
string.chars[0..@intCast(string.length)],
config,
AnyopaquePtrToAnytype(@TypeOf(user_data), userData),
);
}
}.f,
anytypeToAnyopaquePtr(user_data),
);
}
/// `context` must be of same size as a pointer or be of type `void`
pub fn setQueryScrollOffsetFunction(
user_data: anytype,
comptime queryScrollOffsetFunction: fn (
u32,
@TypeOf(user_data),
) Vector2,
) void {
if (!(@TypeOf(user_data) == void) and @sizeOf(@TypeOf(user_data)) != @sizeOf(usize))
@compileError("`context` must be of same size as a pointer or be of type `void`");
cdefs.Clay_SetQueryScrollOffsetFunction(
struct {
pub fn f(scroll: u32, userData: ?*anyopaque) callconv(.c) Dimensions {
return queryScrollOffsetFunction(
scroll,
AnyopaquePtrToAnytype(@TypeOf(user_data), userData),
);
}
}.f,
anytypeToAnyopaquePtr(user_data),
);
}
pub fn createArenaWithCapacityAndMemory(buffer: []u8) Arena {
return cdefs.Clay_CreateArenaWithCapacityAndMemory(@intCast(buffer.len), buffer.ptr);
}
pub fn setMeasureTextFunction(comptime measureTextFunction: fn ([]const u8, *TextElementConfig) Dimensions) void {
cdefs.Clay_SetMeasureTextFunction(struct {
pub fn f(string: *String, config: *TextElementConfig) callconv(.C) Dimensions {
return measureTextFunction(@ptrCast(string.chars[0..@intCast(string.length)]), config);
}
}.f);
}
pub fn makeClayString(string: []const u8) String {
return .{
.chars = @ptrCast(@constCast(string)),
@ -556,14 +620,26 @@ pub fn text(string: []const u8, config: TextElementConfig) void {
cdefs.Clay__OpenTextElement(makeClayString(string), cdefs.Clay__StoreTextElementConfig(config));
}
pub fn ID(string: []const u8) ElementId {
return cdefs.Clay__HashString(makeClayString(string), 0, 0);
}
pub fn IDI(string: []const u8, index: u32) ElementId {
return cdefs.Clay__HashString(makeClayString(string), index, 0);
}
pub fn getElementId(string: []const u8) ElementId {
return cdefs.Clay_GetElementId(makeClayString(string));
}
fn anytypeToAnyopaquePtr(user_data: anytype) ?*anyopaque {
if (@TypeOf(user_data) == void) {
return null;
} else if (@typeInfo(@TypeOf(user_data)) == .pointer) {
return @ptrCast(@alignCast(@constCast(user_data)));
} else {
return @ptrFromInt(@as(usize, @bitCast(user_data)));
}
}
fn AnyopaquePtrToAnytype(T: type, userData: ?*anyopaque) T {
if (T == void) {
return {};
} else if (@typeInfo(T) == .pointer) {
return @ptrCast(@alignCast(@constCast(userData)));
} else {
return @bitCast(@as(usize, @intFromPtr(userData)));
}
}