Skip to content

Commit

Permalink
Update Odin bindings to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nicbarker committed Jan 31, 2025
1 parent 577946a commit bff5586
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 321 deletions.
6 changes: 3 additions & 3 deletions bindings/odin/build-clay-lib.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
cp ../../clay.h clay.c;
# Intel Mac
clang -c -DCLAY_IMPLEMENTATION -o clay.o -static -target x86_64-apple-darwin clay.c -fPIC && ar r clay-odin/macos/clay.a clay.o;
clang -c -DCLAY_IMPLEMENTATION -o clay.o -ffreestanding -static -target x86_64-apple-darwin clay.c -fPIC && ar r clay-odin/macos/clay.a clay.o;
# ARM Mac
clang -c -DCLAY_IMPLEMENTATION -g -o clay.o -static clay.c -fPIC && ar r clay-odin/macos-arm64/clay.a clay.o;
# x64 Windows
clang -c -DCLAY_IMPLEMENTATION -o clay-odin/windows/clay.lib -target x86_64-pc-windows-msvc -fuse-ld=llvm-lib -static clay.c;
clang -c -DCLAY_IMPLEMENTATION -o clay-odin/windows/clay.lib -ffreestanding -target x86_64-pc-windows-msvc -fuse-ld=llvm-lib -static clay.c;
# Linux
clang -c -DCLAY_IMPLEMENTATION -o clay.o -static -target x86_64-unknown-linux-gnu clay.c -fPIC && ar r clay-odin/linux/clay.a clay.o;
clang -c -DCLAY_IMPLEMENTATION -o clay.o -ffreestanding -static -target x86_64-unknown-linux-gnu clay.c -fPIC && ar r clay-odin/linux/clay.a clay.o;
# WASM
clang -c -DCLAY_IMPLEMENTATION -o clay-odin/wasm/clay.o -target wasm32 -nostdlib -static clay.c;
rm clay.o;
Expand Down
125 changes: 43 additions & 82 deletions bindings/odin/clay-odin/clay.odin
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ RenderCommandType :: enum EnumBackingType {

RectangleElementConfig :: struct {
color: Color,
cornerRadius: CornerRadius,
}

TextWrapMode :: enum EnumBackingType {
Expand Down Expand Up @@ -135,14 +134,17 @@ BorderElementConfig :: struct {
top: BorderData,
bottom: BorderData,
betweenChildren: BorderData,
cornerRadius: CornerRadius,
}

ScrollElementConfig :: struct {
horizontal: bool,
vertical: bool,
}

SharedElementConfig :: struct {
cornerRadius: CornerRadius
}

FloatingAttachPointType :: enum EnumBackingType {
LEFT_TOP,
LEFT_CENTER,
Expand Down Expand Up @@ -182,13 +184,18 @@ ElementConfigUnion :: struct #raw_union {
borderElementConfig: ^BorderElementConfig,
}

TextOrSharedConfig :: struct #raw_union {
text: StringSlice,
sharedConfig: ^SharedElementConfig
}

RenderCommand :: struct {
boundingBox: BoundingBox,
config: ElementConfigUnion,
text: StringSlice,
zIndex: i32,
id: u32,
commandType: RenderCommandType,
boundingBox: BoundingBox,
config: ElementConfigUnion,
textOrSharedConfig: TextOrSharedConfig,
zIndex: i32,
id: u32,
commandType: RenderCommandType,
}

ScrollContainerData :: struct {
Expand Down Expand Up @@ -273,10 +280,16 @@ ClayArray :: struct($type: typeid) {
internalArray: [^]type,
}

TypedConfig :: struct {
type: ElementConfigType,
config: rawptr,
id: ElementId,
ElementDeclaration :: struct {
id: ElementId,
layout: LayoutConfig,
rectangle: RectangleElementConfig,
image: ImageElementConfig,
floating: FloatingElementConfig,
custom: CustomElementConfig,
scroll: ScrollElementConfig,
border: BorderElementConfig,
shared: SharedElementConfig,
}

ErrorType :: enum {
Expand Down Expand Up @@ -321,51 +334,27 @@ foreign Clay {
@(link_prefix = "Clay_", default_calling_convention = "c", private)
foreign Clay {
_OpenElement :: proc() ---
_ConfigureOpenElement :: proc(config: ElementDeclaration) ---
_CloseElement :: proc() ---
_ElementPostConfiguration :: proc() ---
_OpenTextElement :: proc(text: String, textConfig: ^TextElementConfig) ---
_AttachId :: proc(id: ElementId) ---
_AttachLayoutConfig :: proc(layoutConfig: ^LayoutConfig) ---
_AttachElementConfig :: proc(config: rawptr, type: ElementConfigType) ---
_StoreLayoutConfig :: proc(config: LayoutConfig) -> ^LayoutConfig ---
_StoreRectangleElementConfig :: proc(config: RectangleElementConfig) -> ^RectangleElementConfig ---
_StoreTextElementConfig :: proc(config: TextElementConfig) -> ^TextElementConfig ---
_StoreImageElementConfig :: proc(config: ImageElementConfig) -> ^ImageElementConfig ---
_StoreFloatingElementConfig :: proc(config: FloatingElementConfig) -> ^FloatingElementConfig ---
_StoreCustomElementConfig :: proc(config: CustomElementConfig) -> ^CustomElementConfig ---
_StoreScrollElementConfig :: proc(config: ScrollElementConfig) -> ^ScrollElementConfig ---
_StoreBorderElementConfig :: proc(config: BorderElementConfig) -> ^BorderElementConfig ---
_HashString :: proc(toHash: String, index: u32, seed: u32) -> ElementId ---
_GetOpenLayoutElementId :: proc() -> u32 ---
}

@(require_results, deferred_none = _CloseElement)
UI :: proc(configs: ..TypedConfig) -> bool {
_OpenElement()
for config in configs {
#partial switch (config.type) {
case ElementConfigType.Id:
_AttachId(config.id)
case ElementConfigType.Layout:
_AttachLayoutConfig(cast(^LayoutConfig)config.config)
case:
_AttachElementConfig(config.config, config.type)
}
}
_ElementPostConfiguration()
return true
ClayOpenElement :: struct {
configure: proc (config: ElementDeclaration) -> bool
}

Layout :: proc(config: LayoutConfig) -> TypedConfig {
return {type = ElementConfigType.Layout, config = _StoreLayoutConfig(config) }
ConfigureOpenElement :: proc(config: ElementDeclaration) -> bool {
_ConfigureOpenElement(config)
return true;
}

PaddingAll :: proc (padding: u16) -> Padding {
return { padding, padding, padding, padding }
}

Rectangle :: proc(config: RectangleElementConfig) -> TypedConfig {
return {type = ElementConfigType.Rectangle, config = _StoreRectangleElementConfig(config)}
@(deferred_none = _CloseElement)
UI :: proc() -> ClayOpenElement {
_OpenElement()
return { configure = ConfigureOpenElement }
}

Text :: proc(text: string, config: ^TextElementConfig) {
Expand All @@ -376,44 +365,16 @@ TextConfig :: proc(config: TextElementConfig) -> ^TextElementConfig {
return _StoreTextElementConfig(config)
}

Image :: proc(config: ImageElementConfig) -> TypedConfig {
return {type = ElementConfigType.Image, config = _StoreImageElementConfig(config)}
}

Floating :: proc(config: FloatingElementConfig) -> TypedConfig {
return {type = ElementConfigType.Floating, config = _StoreFloatingElementConfig(config)}
}

Custom :: proc(config: CustomElementConfig) -> TypedConfig {
return {type = ElementConfigType.Custom, config = _StoreCustomElementConfig(config)}
}

Scroll :: proc(config: ScrollElementConfig) -> TypedConfig {
return {type = ElementConfigType.Scroll, config = _StoreScrollElementConfig(config)}
}

Border :: proc(config: BorderElementConfig) -> TypedConfig {
return {type = ElementConfigType.Border, config = _StoreBorderElementConfig(config)}
}

BorderOutside :: proc(outsideBorders: BorderData) -> TypedConfig {
return { type = ElementConfigType.Border, config = _StoreBorderElementConfig((BorderElementConfig){left = outsideBorders, right = outsideBorders, top = outsideBorders, bottom = outsideBorders}) }
}

BorderOutsideRadius :: proc(outsideBorders: BorderData, radius: f32) -> TypedConfig {
return { type = ElementConfigType.Border, config = _StoreBorderElementConfig(
(BorderElementConfig){left = outsideBorders, right = outsideBorders, top = outsideBorders, bottom = outsideBorders, cornerRadius = {radius, radius, radius, radius}},
) }
PaddingAll :: proc(allPadding: u16) -> Padding {
return { left = allPadding, right = allPadding, top = allPadding, bottom = allPadding }
}

BorderAll :: proc(allBorders: BorderData) -> TypedConfig {
return { type = ElementConfigType.Border, config = _StoreBorderElementConfig((BorderElementConfig){left = allBorders, right = allBorders, top = allBorders, bottom = allBorders, betweenChildren = allBorders}) }
BorderOutside :: proc(outsideBorders: BorderData) -> BorderElementConfig {
return { left = outsideBorders, right = outsideBorders, top = outsideBorders, bottom = outsideBorders }
}

BorderAllRadius :: proc(allBorders: BorderData, radius: f32) -> TypedConfig {
return { type = ElementConfigType.Border, config = _StoreBorderElementConfig(
(BorderElementConfig){left = allBorders, right = allBorders, top = allBorders, bottom = allBorders, cornerRadius = {radius, radius, radius, radius}},
) }
BorderAll :: proc(allBorders: BorderData) -> BorderElementConfig {
return { left = allBorders, right = allBorders, top = allBorders, bottom = allBorders, betweenChildren = allBorders }
}

CornerRadiusAll :: proc(radius: f32) -> CornerRadius {
Expand All @@ -440,6 +401,6 @@ MakeString :: proc(label: string) -> String {
return String{chars = raw_data(label), length = cast(c.int)len(label)}
}

ID :: proc(label: string, index: u32 = 0) -> TypedConfig {
return { type = ElementConfigType.Id, id = _HashString(MakeString(label), index, 0) }
ID :: proc(label: string, index: u32 = 0) -> ElementId {
return _HashString(MakeString(label), index, 0)
}
Binary file modified bindings/odin/clay-odin/linux/clay.a
Binary file not shown.
Binary file modified bindings/odin/clay-odin/macos-arm64/clay.a
Binary file not shown.
Binary file modified bindings/odin/clay-odin/macos/clay.a
Binary file not shown.
Binary file modified bindings/odin/clay-odin/wasm/clay.o
Binary file not shown.
Binary file modified bindings/odin/clay-odin/windows/clay.lib
Binary file not shown.
Loading

0 comments on commit bff5586

Please sign in to comment.