moved makeClayString to String namespace

This commit is contained in:
johan0A 2025-03-08 16:59:35 +01:00
parent aed6a03a63
commit b536cdc894

View file

@ -57,6 +57,14 @@ pub const String = extern struct {
length: i32, length: i32,
/// Pointer to the character data /// Pointer to the character data
chars: [*]const u8, chars: [*]const u8,
/// Converts a Zig string slice to a Clay_String
pub fn fromSlice(string: []const u8) String {
return .{
.chars = @ptrCast(@constCast(string)),
.length = @intCast(string.len),
};
}
}; };
/// Clay StringSlice is used to represent non-owning string slices /// Clay StringSlice is used to represent non-owning string slices
@ -411,37 +419,37 @@ pub const ElementId = extern struct {
/// Creates a global element ID from a string literal /// Creates a global element ID from a string literal
pub fn ID(string: []const u8) ElementId { pub fn ID(string: []const u8) ElementId {
return cdefs.Clay__HashString(makeClayString(string), 0, 0); return cdefs.Clay__HashString(.fromSlice(string), 0, 0);
} }
/// Creates a global element ID with an index component for use in loops /// Creates a global element ID with an index component for use in loops
/// Equivalent to ID("prefix0"), ID("prefix1"), etc. without string allocations /// Equivalent to ID("prefix0"), ID("prefix1"), etc. without string allocations
pub fn IDI(string: []const u8, index: u32) ElementId { pub fn IDI(string: []const u8, index: u32) ElementId {
return cdefs.Clay__HashString(makeClayString(string), index, 0); return cdefs.Clay__HashString(.fromSlice(string), index, 0);
} }
/// Creates a local element ID from a string literal /// Creates a local element ID from a string literal
/// Local IDs are scoped to the current parent element /// Local IDs are scoped to the current parent element
pub fn localID(string: []const u8) ElementId { pub fn localID(string: []const u8) ElementId {
return cdefs.Clay__HashString(makeClayString(string), 0, cdefs.Clay__GetParentElementId()); return cdefs.Clay__HashString(.fromSlice(string), 0, cdefs.Clay__GetParentElementId());
} }
/// Creates a local element ID from a string literal with index /// Creates a local element ID from a string literal with index
/// Local IDs are scoped to the current parent element /// Local IDs are scoped to the current parent element
pub fn localIDI(string: []const u8, index: u32) ElementId { pub fn localIDI(string: []const u8, index: u32) ElementId {
return cdefs.Clay__HashString(makeClayString(string), index, cdefs.Clay__GetParentElementId()); return cdefs.Clay__HashString(.fromSlice(string), index, cdefs.Clay__GetParentElementId());
} }
/// Creates a global element ID from a source location (@src()) /// Creates a global element ID from a source location (@src())
/// Useful for auto-generating unique IDs based on code location /// Useful for auto-generating unique IDs based on code location
pub fn fromSrc(comptime src: std.builtin.SourceLocation) ElementId { pub fn fromSrc(comptime src: std.builtin.SourceLocation) ElementId {
cdefs.Clay__HashString(makeClayString(src.module ++ "#" ++ src.file ++ ":" ++ std.fmt.comptimePrint({}, src.column)), 0, 0); cdefs.Clay__HashString(.fromSlice(src.module ++ "#" ++ src.file ++ ":" ++ std.fmt.comptimePrint({}, src.column)), 0, 0);
} }
/// Creates a global element ID from a source location (@src()) with an index /// Creates a global element ID from a source location (@src()) with an index
/// Useful for auto-generating unique IDs based on code location in loops /// Useful for auto-generating unique IDs based on code location in loops
pub fn fromSrcI(comptime src: std.builtin.SourceLocation, index: u32) ElementId { pub fn fromSrcI(comptime src: std.builtin.SourceLocation, index: u32) ElementId {
cdefs.Clay__HashString(makeClayString(src.module ++ "#" ++ src.file ++ ":" ++ std.fmt.comptimePrint({}, src.column)), index, 0); cdefs.Clay__HashString(.fromSlice(src.module ++ "#" ++ src.file ++ ":" ++ std.fmt.comptimePrint({}, src.column)), index, 0);
} }
}; };
@ -1032,15 +1040,6 @@ pub fn createArenaWithCapacityAndMemory(buffer: []u8) Arena {
return cdefs.Clay_CreateArenaWithCapacityAndMemory(@intCast(buffer.len), buffer.ptr); return cdefs.Clay_CreateArenaWithCapacityAndMemory(@intCast(buffer.len), buffer.ptr);
} }
// TODO move to String namespace
/// Converts a Zig string slice to a Clay_String
pub fn makeClayString(string: []const u8) String {
return .{
.chars = @ptrCast(@constCast(string)),
.length = @intCast(string.len),
};
}
/// Creates a text element with the given string and configuration /// Creates a text element with the given string and configuration
/// ///
/// Example: /// Example:
@ -1048,7 +1047,7 @@ pub fn makeClayString(string: []const u8) String {
/// text("Hello World", .{ .font_size = 24, .color = .{255, 0, 0, 255} }); /// text("Hello World", .{ .font_size = 24, .color = .{255, 0, 0, 255} });
/// ``` /// ```
pub fn text(string: []const u8, config: TextElementConfig) void { pub fn text(string: []const u8, config: TextElementConfig) void {
cdefs.Clay__OpenTextElement(makeClayString(string), cdefs.Clay__StoreTextElementConfig(config)); cdefs.Clay__OpenTextElement(.fromSlice(string), cdefs.Clay__StoreTextElementConfig(config));
} }
/// Gets an element's ID from a string /// Gets an element's ID from a string
@ -1060,7 +1059,7 @@ pub fn text(string: []const u8, config: TextElementConfig) void {
/// const isHovered = pointerOver(buttonId); /// const isHovered = pointerOver(buttonId);
/// ``` /// ```
pub fn getElementId(string: []const u8) ElementId { pub fn getElementId(string: []const u8) ElementId {
return cdefs.Clay_GetElementId(makeClayString(string)); return cdefs.Clay_GetElementId(.fromSlice(string));
} }
fn anytypeToAnyopaquePtr(user_data: anytype) ?*anyopaque { fn anytypeToAnyopaquePtr(user_data: anytype) ?*anyopaque {