Changed the UI proc to be like the one in the Odin bindings

This commit is contained in:
Stowy 2025-01-01 16:19:52 +01:00
parent 409bf1c3bf
commit e9522005db
5 changed files with 61 additions and 52 deletions

View File

@ -80,10 +80,7 @@ main :: () {
Clay.ID("OuterContainer"), Clay.ID("OuterContainer"),
Clay.Layout(.{ Clay.Layout(.{
layoutDirection = .TOP_TO_BOTTOM, layoutDirection = .TOP_TO_BOTTOM,
sizing = Clay.Sizing.{ sizing = layout_expand,
Clay.SizingGrow(),
Clay.SizingGrow(),
},
padding = .{16, 16}, padding = .{16, 16},
childGap = 16, childGap = 16,
}), }),

View File

@ -5,6 +5,8 @@ SOURCE_PATH :: "source";
// These have custom declaration in module.jai // These have custom declaration in module.jai
DECLARATIONS_TO_OMIT :: string.[ DECLARATIONS_TO_OMIT :: string.[
"Clay_Vector2", "Clay_Vector2",
"Clay__ElementConfigType",
"Clay__AlignClay__ElementConfigType",
]; ];
#if AT_COMPILE_TIME { #if AT_COMPILE_TIME {

View File

@ -1,38 +1,80 @@
Vector2 :: Math.Vector2; Vector2 :: Math.Vector2;
ElementConfigType :: enum s32 {
NONE :: 0;
RECTANGLE :: 1;
BORDER_CONTAINER :: 2;
FLOATING_CONTAINER :: 4;
SCROLL_CONTAINER :: 8;
IMAGE :: 16;
TEXT :: 32;
CUSTOM :: 64;
CLAY__ELEMENT_CONFIG_TYPE_NONE :: NONE;
CLAY__ELEMENT_CONFIG_TYPE_RECTANGLE :: RECTANGLE;
CLAY__ELEMENT_CONFIG_TYPE_BORDER_CONTAINER :: BORDER_CONTAINER;
CLAY__ELEMENT_CONFIG_TYPE_FLOATING_CONTAINER :: FLOATING_CONTAINER;
CLAY__ELEMENT_CONFIG_TYPE_SCROLL_CONTAINER :: SCROLL_CONTAINER;
CLAY__ELEMENT_CONFIG_TYPE_IMAGE :: IMAGE;
CLAY__ELEMENT_CONFIG_TYPE_TEXT :: TEXT;
CLAY__ELEMENT_CONFIG_TYPE_CUSTOM :: CUSTOM;
// Jai bindings specific types, please don't assume any value in those
// a it might change if the enums above overlap with it
// TODO Check if these values need to be powers of two
ID :: 256;
LAYOUT :: 257;
}
// This is passed to UI so that we can omit layout
TypedConfig :: struct {
type: ElementConfigType;
config: *void;
id: ElementId;
}
make_string :: (str: string) -> String { make_string :: (str: string) -> String {
return .{cast(u64, str.count), str.data}; return .{cast(u64, str.count), str.data};
} }
UI :: (id: ElementId, layout: LayoutConfig, configs: ..ElementConfig, $call := #caller_code) -> bool #must #expand { // The way of handling this is inspired by the odin bindings
UI :: (configs: ..TypedConfig, $call := #caller_code) -> bool #must #expand {
_OpenElement(); _OpenElement();
_AttachId(id); for config : configs {
_AttachLayoutConfig(_StoreLayoutConfig(layout)); if config.type == {
for configs _AttachElementConfig(it.config, it.type); case .ID;
_AttachId(config.id);
case .LAYOUT;
_AttachLayoutConfig(cast(*LayoutConfig, config.config));
case;
// config.config is a *void, it stores the address of the pointer that is stored in the union
// as ElementConfigUnion is a union of structs. We can't cast pointers directly to structs,
// we first cast the address of the *void and then dereference it.
// Maybe there's a cast modifier to avoid this, but I don't know it (no_check and trunc didn't work).
_AttachElementConfig(cast(*ElementConfigUnion, *config.config).*, config.type);
}
}
_ElementPostConfiguration(); _ElementPostConfiguration();
// !IMPORTANT
// TODO Fix the need to have to add the namespace here // TODO Fix the need to have to add the namespace here
#insert,scope(call) #code defer Clay._CloseElement();; #insert,scope(call) #code defer Clay._CloseElement();;
return true; return true;
// `defer _CloseElement();
} }
ID :: (label: string, index: u32 = 0) -> ElementId { ID :: (label: string, index: u32 = 0) -> TypedConfig {
return _HashString(make_string(label), index, 0); return .{type = .ID, id = _HashString(make_string(label), index, 0)};
} }
Layout :: (config: LayoutConfig) -> LayoutConfig { Layout :: (config: LayoutConfig) -> TypedConfig {
// We can just return the config because the layout is attached and stored in the UI function return .{type = .LAYOUT, config = _StoreLayoutConfig(config)};
return config;
} }
Rectangle :: (config: RectangleElementConfig) -> ElementConfig { Rectangle :: (config: RectangleElementConfig) -> TypedConfig {
return .{ return .{
type = .RECTANGLE, type = .RECTANGLE,
config.rectangleElementConfig = _StoreRectangleElementConfig(config) config = _StoreRectangleElementConfig(config)
}; };
} }

View File

@ -1,7 +1,7 @@
// //
// This file was auto-generated using the following command: // This file was auto-generated using the following command:
// //
// jai generate.jai - -compile -debug // jai generate.jai - -compile
// //
@ -119,29 +119,6 @@ _AlignClay_CornerRadius :: struct {
x: CornerRadius; x: CornerRadius;
} }
_ElementConfigType :: enum s32 {
NONE :: 0;
RECTANGLE :: 1;
BORDER_CONTAINER :: 2;
FLOATING_CONTAINER :: 4;
SCROLL_CONTAINER :: 8;
IMAGE :: 16;
TEXT :: 32;
CUSTOM :: 64;
CLAY__ELEMENT_CONFIG_TYPE_NONE :: NONE;
CLAY__ELEMENT_CONFIG_TYPE_RECTANGLE :: RECTANGLE;
CLAY__ELEMENT_CONFIG_TYPE_BORDER_CONTAINER :: BORDER_CONTAINER;
CLAY__ELEMENT_CONFIG_TYPE_FLOATING_CONTAINER :: FLOATING_CONTAINER;
CLAY__ELEMENT_CONFIG_TYPE_SCROLL_CONTAINER :: SCROLL_CONTAINER;
CLAY__ELEMENT_CONFIG_TYPE_IMAGE :: IMAGE;
CLAY__ELEMENT_CONFIG_TYPE_TEXT :: TEXT;
CLAY__ELEMENT_CONFIG_TYPE_CUSTOM :: CUSTOM;
}
_AlignClay__ElementConfigType :: struct {
c: u8;
x: _ElementConfigType;
}
LayoutDirection :: enum s32 { LayoutDirection :: enum s32 {
LEFT_TO_RIGHT :: 0; LEFT_TO_RIGHT :: 0;
TOP_TO_BOTTOM :: 1; TOP_TO_BOTTOM :: 1;
@ -429,7 +406,7 @@ _AlignClay_ElementConfigUnion :: struct {
} }
ElementConfig :: struct { ElementConfig :: struct {
type: _ElementConfigType; type: ElementConfigType;
config: ElementConfigUnion; config: ElementConfigUnion;
} }
_AlignClay_ElementConfig :: struct { _AlignClay_ElementConfig :: struct {
@ -594,7 +571,7 @@ _StoreLayoutConfig :: (config: LayoutConfig) -> *LayoutConfig #foreign clay "Cla
_ElementPostConfiguration :: () -> void #foreign clay "Clay__ElementPostConfiguration"; _ElementPostConfiguration :: () -> void #foreign clay "Clay__ElementPostConfiguration";
_AttachId :: (id: ElementId) -> void #foreign clay "Clay__AttachId"; _AttachId :: (id: ElementId) -> void #foreign clay "Clay__AttachId";
_AttachLayoutConfig :: (config: *LayoutConfig) -> void #foreign clay "Clay__AttachLayoutConfig"; _AttachLayoutConfig :: (config: *LayoutConfig) -> void #foreign clay "Clay__AttachLayoutConfig";
_AttachElementConfig :: (config: ElementConfigUnion, type: _ElementConfigType) -> void #foreign clay "Clay__AttachElementConfig"; _AttachElementConfig :: (config: ElementConfigUnion, type: ElementConfigType) -> void #foreign clay "Clay__AttachElementConfig";
_StoreRectangleElementConfig :: (config: RectangleElementConfig) -> *RectangleElementConfig #foreign clay "Clay__StoreRectangleElementConfig"; _StoreRectangleElementConfig :: (config: RectangleElementConfig) -> *RectangleElementConfig #foreign clay "Clay__StoreRectangleElementConfig";
_StoreTextElementConfig :: (config: TextElementConfig) -> *TextElementConfig #foreign clay "Clay__StoreTextElementConfig"; _StoreTextElementConfig :: (config: TextElementConfig) -> *TextElementConfig #foreign clay "Clay__StoreTextElementConfig";
_StoreImageElementConfig :: (config: ImageElementConfig) -> *ImageElementConfig #foreign clay "Clay__StoreImageElementConfig"; _StoreImageElementConfig :: (config: ImageElementConfig) -> *ImageElementConfig #foreign clay "Clay__StoreImageElementConfig";
@ -825,15 +802,6 @@ clay :: #library,no_dll "clay-jai/windows/clay";
assert(size_of(_AlignClay_CornerRadius) == 20, "_AlignClay_CornerRadius has size % instead of 20", size_of(_AlignClay_CornerRadius)); assert(size_of(_AlignClay_CornerRadius) == 20, "_AlignClay_CornerRadius has size % instead of 20", size_of(_AlignClay_CornerRadius));
} }
{
instance: _AlignClay__ElementConfigType;
assert(((cast(*void)(*instance.c)) - cast(*void)(*instance)) == 0, "_AlignClay__ElementConfigType.c has unexpected offset % instead of 0", ((cast(*void)(*instance.c)) - cast(*void)(*instance)));
assert(size_of(type_of(_AlignClay__ElementConfigType.c)) == 1, "_AlignClay__ElementConfigType.c has unexpected size % instead of 1", size_of(type_of(_AlignClay__ElementConfigType.c)));
assert(((cast(*void)(*instance.x)) - cast(*void)(*instance)) == 4, "_AlignClay__ElementConfigType.x has unexpected offset % instead of 4", ((cast(*void)(*instance.x)) - cast(*void)(*instance)));
assert(size_of(type_of(_AlignClay__ElementConfigType.x)) == 4, "_AlignClay__ElementConfigType.x has unexpected size % instead of 4", size_of(type_of(_AlignClay__ElementConfigType.x)));
assert(size_of(_AlignClay__ElementConfigType) == 8, "_AlignClay__ElementConfigType has size % instead of 8", size_of(_AlignClay__ElementConfigType));
}
{ {
instance: _AlignClay_LayoutDirection; instance: _AlignClay_LayoutDirection;
assert(((cast(*void)(*instance.c)) - cast(*void)(*instance)) == 0, "_AlignClay_LayoutDirection.c has unexpected offset % instead of 0", ((cast(*void)(*instance.c)) - cast(*void)(*instance))); assert(((cast(*void)(*instance.c)) - cast(*void)(*instance)) == 0, "_AlignClay_LayoutDirection.c has unexpected offset % instead of 0", ((cast(*void)(*instance.c)) - cast(*void)(*instance)));