Compare commits

...

5 Commits

Author SHA1 Message Date
Courtney Strachan
1a92eed685
Merge 6cd721e647 into 9e7595b873 2025-01-11 10:40:31 +01:00
Nic Barker
9e7595b873 Fixed a bug where minMemorySize could cause a memory overwrite
Some checks failed
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Waiting to run
CMake on multiple platforms / build (Release, clang, clang++, ubuntu-latest) (push) Failing after 12s
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Failing after 11s
2025-01-11 21:37:21 +13:00
Nic Barker
32d1a31dfe Fix uint64 usage for wasm 2025-01-11 21:35:45 +13:00
Nic Barker
b2b50724e2 Fix bug in html renderer debug tools 2025-01-11 20:45:20 +13:00
Courtney Strachan
6cd721e647 Added missing public and private API function bindings 2025-01-07 22:37:58 -05:00
4 changed files with 61 additions and 24 deletions

1
.gitignore vendored
View File

@ -1,6 +1,5 @@
cmake-build-debug/ cmake-build-debug/
cmake-build-release/ cmake-build-release/
build/
.DS_Store .DS_Store
.idea/ .idea/
node_modules/ node_modules/

View File

@ -269,6 +269,18 @@ TypedConfig :: struct {
id: ElementId, id: ElementId,
} }
PointerDataInteractionState :: enum EnumBackingType {
PRESSED_THIS_FRAME,
PRESSED,
RELEASED_THIS_FRAME,
RELEASED,
}
PointerData :: struct {
position: Vector2,
state: PointerDataInteractionState,
}
ErrorType :: enum { ErrorType :: enum {
TEXT_MEASUREMENT_FUNCTION_NOT_PROVIDED, TEXT_MEASUREMENT_FUNCTION_NOT_PROVIDED,
ARENA_CAPACITY_EXCEEDED, ARENA_CAPACITY_EXCEEDED,
@ -282,12 +294,12 @@ ErrorType :: enum {
ErrorData :: struct { ErrorData :: struct {
errorType: ErrorType, errorType: ErrorType,
errorText: String, errorText: String,
userData: rawptr userData: rawptr,
} }
ErrorHandler :: struct { ErrorHandler :: struct {
handler: proc "c" (errorData: ErrorData), handler: proc "c" (errorData: ErrorData),
userData: rawptr userData: rawptr,
} }
@(link_prefix = "Clay_", default_calling_convention = "c") @(link_prefix = "Clay_", default_calling_convention = "c")
@ -300,24 +312,31 @@ foreign Clay {
SetLayoutDimensions :: proc(dimensions: Dimensions) --- SetLayoutDimensions :: proc(dimensions: Dimensions) ---
BeginLayout :: proc() --- BeginLayout :: proc() ---
EndLayout :: proc() -> ClayArray(RenderCommand) --- EndLayout :: proc() -> ClayArray(RenderCommand) ---
PointerOver :: proc(id: ElementId) -> bool ---
GetElementId :: proc(id: String) -> ElementId --- GetElementId :: proc(id: String) -> ElementId ---
GetElementIdWithIndex :: proc(id: String, index: u32) -> ElementId ---
Hovered :: proc() -> bool ---
OnHover :: proc(onHoverFunction: proc "c" (elementId: ElementId, pointerInfo: PointerData, userData: rawptr), userData: rawptr) ---
PointerOver :: proc(id: ElementId) -> bool ---
GetScrollContainerData :: proc(id: ElementId) -> ScrollContainerData --- GetScrollContainerData :: proc(id: ElementId) -> ScrollContainerData ---
SetMeasureTextFunction :: proc(measureTextFunction: proc "c" (text: ^String, config: ^TextElementConfig) -> Dimensions) --- SetMeasureTextFunction :: proc(measureTextFunction: proc "c" (text: ^String, config: ^TextElementConfig) -> Dimensions) ---
SetQueryScrollOffsetFunction :: proc(queryScrollOffsetFunction: proc "c" (elementId: u32) -> Vector2) ---
RenderCommandArray_Get :: proc(array: ^ClayArray(RenderCommand), index: i32) -> ^RenderCommand --- RenderCommandArray_Get :: proc(array: ^ClayArray(RenderCommand), index: i32) -> ^RenderCommand ---
SetDebugModeEnabled :: proc(enabled: bool) --- SetDebugModeEnabled :: proc(enabled: bool) ---
IsDebugModeEnabled :: proc() -> bool ---
SetCullingEnabled :: proc(enabled: bool) ---
SetMaxElementCount :: proc(maxElementCount: i32) ---
SetMaxMeasureTextCacheWordCount :: proc(maxMeasureTextCacheWordCount: i32) ---
} }
@(link_prefix = "Clay_", default_calling_convention = "c", private) @(link_prefix = "Clay_", default_calling_convention = "c", private)
foreign Clay { foreign Clay {
_OpenElement :: proc() --- _OpenElement :: proc() ---
_CloseElement :: proc() --- _CloseElement :: proc() ---
_StoreLayoutConfig :: proc(config: LayoutConfig) -> ^LayoutConfig ---
_ElementPostConfiguration :: proc() --- _ElementPostConfiguration :: proc() ---
_OpenTextElement :: proc(text: String, textConfig: ^TextElementConfig) ---
_AttachId :: proc(id: ElementId) --- _AttachId :: proc(id: ElementId) ---
_AttachLayoutConfig :: proc(layoutConfig: ^LayoutConfig) --- _AttachLayoutConfig :: proc(layoutConfig: ^LayoutConfig) ---
_AttachElementConfig :: proc(config: rawptr, type: ElementConfigType) --- _AttachElementConfig :: proc(config: rawptr, type: ElementConfigType) ---
_StoreLayoutConfig :: proc(config: LayoutConfig) -> ^LayoutConfig ---
_StoreRectangleElementConfig :: proc(config: RectangleElementConfig) -> ^RectangleElementConfig --- _StoreRectangleElementConfig :: proc(config: RectangleElementConfig) -> ^RectangleElementConfig ---
_StoreTextElementConfig :: proc(config: TextElementConfig) -> ^TextElementConfig --- _StoreTextElementConfig :: proc(config: TextElementConfig) -> ^TextElementConfig ---
_StoreImageElementConfig :: proc(config: ImageElementConfig) -> ^ImageElementConfig --- _StoreImageElementConfig :: proc(config: ImageElementConfig) -> ^ImageElementConfig ---
@ -326,7 +345,8 @@ foreign Clay {
_StoreScrollElementConfig :: proc(config: ScrollElementConfig) -> ^ScrollElementConfig --- _StoreScrollElementConfig :: proc(config: ScrollElementConfig) -> ^ScrollElementConfig ---
_StoreBorderElementConfig :: proc(config: BorderElementConfig) -> ^BorderElementConfig --- _StoreBorderElementConfig :: proc(config: BorderElementConfig) -> ^BorderElementConfig ---
_HashString :: proc(toHash: String, index: u32, seed: u32) -> ElementId --- _HashString :: proc(toHash: String, index: u32, seed: u32) -> ElementId ---
_GetOpenLayoutElementId :: proc() -> u32 --- _OpenTextElement :: proc(text: String, textConfig: ^TextElementConfig) ---
_GetParentElementId :: proc() -> u32 ---
} }
@(require_results, deferred_none = _CloseElement) @(require_results, deferred_none = _CloseElement)
@ -347,7 +367,7 @@ UI :: proc(configs: ..TypedConfig) -> bool {
} }
Layout :: proc(config: LayoutConfig) -> TypedConfig { Layout :: proc(config: LayoutConfig) -> TypedConfig {
return {type = ElementConfigType.Layout, config = _StoreLayoutConfig(config) } return {type = ElementConfigType.Layout, config = _StoreLayoutConfig(config)}
} }
Rectangle :: proc(config: RectangleElementConfig) -> TypedConfig { Rectangle :: proc(config: RectangleElementConfig) -> TypedConfig {
@ -383,23 +403,35 @@ Border :: proc(config: BorderElementConfig) -> TypedConfig {
} }
BorderOutside :: proc(outsideBorders: BorderData) -> TypedConfig { BorderOutside :: proc(outsideBorders: BorderData) -> TypedConfig {
return { type = ElementConfigType.Border, config = _StoreBorderElementConfig((BorderElementConfig){left = outsideBorders, right = outsideBorders, top = outsideBorders, bottom = outsideBorders}) } return {
type = ElementConfigType.Border,
config = _StoreBorderElementConfig((BorderElementConfig){left = outsideBorders, right = outsideBorders, top = outsideBorders, bottom = outsideBorders}),
}
} }
BorderOutsideRadius :: proc(outsideBorders: BorderData, radius: f32) -> TypedConfig { BorderOutsideRadius :: proc(outsideBorders: BorderData, radius: f32) -> TypedConfig {
return { type = ElementConfigType.Border, config = _StoreBorderElementConfig( return {
(BorderElementConfig){left = outsideBorders, right = outsideBorders, top = outsideBorders, bottom = outsideBorders, cornerRadius = {radius, radius, radius, radius}}, type = ElementConfigType.Border,
) } config = _StoreBorderElementConfig(
(BorderElementConfig){left = outsideBorders, right = outsideBorders, top = outsideBorders, bottom = outsideBorders, cornerRadius = {radius, radius, radius, radius}},
),
}
} }
BorderAll :: proc(allBorders: BorderData) -> TypedConfig { BorderAll :: proc(allBorders: BorderData) -> TypedConfig {
return { type = ElementConfigType.Border, config = _StoreBorderElementConfig((BorderElementConfig){left = allBorders, right = allBorders, top = allBorders, bottom = allBorders, betweenChildren = allBorders}) } return {
type = ElementConfigType.Border,
config = _StoreBorderElementConfig((BorderElementConfig){left = allBorders, right = allBorders, top = allBorders, bottom = allBorders, betweenChildren = allBorders}),
}
} }
BorderAllRadius :: proc(allBorders: BorderData, radius: f32) -> TypedConfig { BorderAllRadius :: proc(allBorders: BorderData, radius: f32) -> TypedConfig {
return { type = ElementConfigType.Border, config = _StoreBorderElementConfig( return {
(BorderElementConfig){left = allBorders, right = allBorders, top = allBorders, bottom = allBorders, cornerRadius = {radius, radius, radius, radius}}, type = ElementConfigType.Border,
) } config = _StoreBorderElementConfig(
(BorderElementConfig){left = allBorders, right = allBorders, top = allBorders, bottom = allBorders, cornerRadius = {radius, radius, radius, radius}},
),
}
} }
CornerRadiusAll :: proc(radius: f32) -> CornerRadius { CornerRadiusAll :: proc(radius: f32) -> CornerRadius {
@ -427,5 +459,5 @@ MakeString :: proc(label: string) -> String {
} }
ID :: proc(label: string, index: u32 = 0) -> TypedConfig { ID :: proc(label: string, index: u32 = 0) -> TypedConfig {
return { type = ElementConfigType.Id, id = _HashString(MakeString(label), index, 0) } return {type = ElementConfigType.Id, id = _HashString(MakeString(label), index, 0)}
} }

18
clay.h
View File

@ -1390,7 +1390,7 @@ struct Clay_Context {
bool externalScrollHandlingEnabled; bool externalScrollHandlingEnabled;
uint32_t debugSelectedElementId; uint32_t debugSelectedElementId;
uint32_t generation; uint32_t generation;
uint64_t arenaResetOffset; uintptr_t arenaResetOffset;
Clay_Arena internalArena; Clay_Arena internalArena;
// Layout Elements / Render Commands // Layout Elements / Render Commands
Clay_LayoutElementArray layoutElements; Clay_LayoutElementArray layoutElements;
@ -2102,6 +2102,9 @@ void Clay__CompressChildrenAlongAxis(bool xAxis, float totalSizeToDistribute, Cl
float targetSize = 0; float targetSize = 0;
for (int32_t i = 0; i < resizableContainerBuffer.length; ++i) { for (int32_t i = 0; i < resizableContainerBuffer.length; ++i) {
Clay_LayoutElement *childElement = Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_Get(&resizableContainerBuffer, i)); Clay_LayoutElement *childElement = Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_Get(&resizableContainerBuffer, i));
if (!xAxis && Clay__ElementHasConfig(childElement, CLAY__ELEMENT_CONFIG_TYPE_IMAGE)) {
continue;
}
float childSize = xAxis ? childElement->dimensions.width : childElement->dimensions.height; float childSize = xAxis ? childElement->dimensions.width : childElement->dimensions.height;
if ((childSize - largestSize) < 0.1 && (childSize - largestSize) > -0.1) { if ((childSize - largestSize) < 0.1 && (childSize - largestSize) > -0.1) {
Clay__int32_tArray_Add(&largestContainers, Clay__int32_tArray_Get(&resizableContainerBuffer, i)); Clay__int32_tArray_Add(&largestContainers, Clay__int32_tArray_Get(&resizableContainerBuffer, i));
@ -2471,9 +2474,9 @@ void Clay__CalculateFinalLayout() {
while (sortMax > 0) { // todo dumb bubble sort while (sortMax > 0) { // todo dumb bubble sort
for (int32_t i = 0; i < sortMax; ++i) { for (int32_t i = 0; i < sortMax; ++i) {
Clay__LayoutElementTreeRoot current = *Clay__LayoutElementTreeRootArray_Get(&context->layoutElementTreeRoots, i); Clay__LayoutElementTreeRoot current = *Clay__LayoutElementTreeRootArray_Get(&context->layoutElementTreeRoots, i);
Clay__LayoutElementTreeRoot *next = Clay__LayoutElementTreeRootArray_Get(&context->layoutElementTreeRoots, i + 1); Clay__LayoutElementTreeRoot next = *Clay__LayoutElementTreeRootArray_Get(&context->layoutElementTreeRoots, i + 1);
if (next->zIndex < current.zIndex) { if (next.zIndex < current.zIndex) {
Clay__LayoutElementTreeRootArray_Set(&context->layoutElementTreeRoots, i, *next); Clay__LayoutElementTreeRootArray_Set(&context->layoutElementTreeRoots, i, next);
Clay__LayoutElementTreeRootArray_Set(&context->layoutElementTreeRoots, i + 1, current); Clay__LayoutElementTreeRootArray_Set(&context->layoutElementTreeRoots, i + 1, current);
} }
} }
@ -3222,16 +3225,19 @@ void Clay__RenderDebugView() {
Clay_TextElementConfig *infoTitleConfig = CLAY_TEXT_CONFIG({ .textColor = CLAY__DEBUGVIEW_COLOR_3, .fontSize = 16, .wrapMode = CLAY_TEXT_WRAP_NONE }); Clay_TextElementConfig *infoTitleConfig = CLAY_TEXT_CONFIG({ .textColor = CLAY__DEBUGVIEW_COLOR_3, .fontSize = 16, .wrapMode = CLAY_TEXT_WRAP_NONE });
Clay_ElementId scrollId = Clay__HashString(CLAY_STRING("Clay__DebugViewOuterScrollPane"), 0, 0); Clay_ElementId scrollId = Clay__HashString(CLAY_STRING("Clay__DebugViewOuterScrollPane"), 0, 0);
float scrollYOffset = 0; float scrollYOffset = 0;
bool pointerInDebugView = context->pointerInfo.position.y < context->layoutDimensions.height - 300;
for (int32_t i = 0; i < context->scrollContainerDatas.length; ++i) { for (int32_t i = 0; i < context->scrollContainerDatas.length; ++i) {
Clay__ScrollContainerDataInternal *scrollContainerData = Clay__ScrollContainerDataInternalArray_Get(&context->scrollContainerDatas, i); Clay__ScrollContainerDataInternal *scrollContainerData = Clay__ScrollContainerDataInternalArray_Get(&context->scrollContainerDatas, i);
if (scrollContainerData->elementId == scrollId.id) { if (scrollContainerData->elementId == scrollId.id) {
if (!context->externalScrollHandlingEnabled) { if (!context->externalScrollHandlingEnabled) {
scrollYOffset = scrollContainerData->scrollPosition.y; scrollYOffset = scrollContainerData->scrollPosition.y;
} else {
pointerInDebugView = context->pointerInfo.position.y + scrollContainerData->scrollPosition.y < context->layoutDimensions.height - 300;
} }
break; break;
} }
} }
int32_t highlightedRow = context->pointerInfo.position.y < context->layoutDimensions.height - 300 int32_t highlightedRow = pointerInDebugView
? (int32_t)((context->pointerInfo.position.y - scrollYOffset) / (float)CLAY__DEBUGVIEW_ROW_HEIGHT) - 1 ? (int32_t)((context->pointerInfo.position.y - scrollYOffset) / (float)CLAY__DEBUGVIEW_ROW_HEIGHT) - 1
: -1; : -1;
if (context->pointerInfo.position.x < context->layoutDimensions.width - (float)Clay__debugViewWidth) { if (context->pointerInfo.position.x < context->layoutDimensions.width - (float)Clay__debugViewWidth) {
@ -3609,7 +3615,7 @@ uint32_t Clay_MinMemorySize(void) {
.maxMeasureTextCacheWordCount = Clay__defaultMaxMeasureTextWordCacheCount, .maxMeasureTextCacheWordCount = Clay__defaultMaxMeasureTextWordCacheCount,
.internalArena = { .internalArena = {
.capacity = SIZE_MAX, .capacity = SIZE_MAX,
.memory = (char*)&fakeContext, .memory = NULL,
} }
}; };
Clay_Context* currentContext = Clay_GetCurrentContext(); Clay_Context* currentContext = Clay_GetCurrentContext();