Compare commits

...

10 Commits

Author SHA1 Message Date
Nic Barker
8237122350 more small fixes 2025-01-09 12:34:36 +13:00
Nic Barker
077eee61a5 update odin bindings and revert some accidental change
s
2025-01-09 12:31:53 +13:00
Nic Barker
5446fb6d7e update generated wasm 2025-01-09 12:25:00 +13:00
Nic Barker
f186024e74 Expose getter for max element counts 2025-01-09 12:21:20 +13:00
Nic Barker
73d2186d95 merge main 2025-01-09 12:17:53 +13:00
Nic Barker
5cca7f5026 Update for c99 compat 2025-01-09 12:11:08 +13:00
Nic Barker
3f01ee4a4e Disable cairo example because of github actions issues
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 15s
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Failing after 12s
2025-01-09 10:08:51 +13:00
FintasticMan
a431254de4
[Core] Add check for supported C/C++ versions (#144) 2025-01-09 10:05:50 +13:00
David Styrbjörn
7cc719e61f
[Documentation] Updated example for Clay_SetPointerState (#169) 2025-01-09 09:56:24 +13:00
Nic Barker
c12cefeaf4
[Layout] Improve shrink size distribution (#173)
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 15s
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Failing after 11s
2025-01-08 19:39:20 +13:00
10 changed files with 140 additions and 136 deletions

View File

@ -8,7 +8,7 @@ add_subdirectory("examples/cpp-project-example")
# Don't try to compile C99 projects using MSVC
if(NOT MSVC)
add_subdirectory("examples/raylib-sidebar-scrolling-container")
add_subdirectory("examples/cairo-pdf-rendering")
# add_subdirectory("examples/cairo-pdf-rendering") Some issue with github actions populating cairo, disable for now
add_subdirectory("examples/clay-official-website")
add_subdirectory("examples/introducing-clay-video-demo")
add_subdirectory("examples/SDL2-video-demo")

View File

@ -100,7 +100,7 @@ Clay_RenderCommandArray CreateLayout() {
CLAY_RECTANGLE({ .color = COLOR_LIGHT })
) {
CLAY(CLAY_ID("ProfilePictureOuter"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_GROW() }, .padding = {16, 16}, .childGap = 16, .childAlignment = { .y = CLAY_ALIGN_Y_CENTER }), CLAY_RECTANGLE({ .color = COLOR_RED })) {
CLAY(CLAY_ID("ProfilePicture"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(60), .height = CLAY_SIZING_FIXED(60) }}), CLAY_IMAGE({ .imageData = &profilePicture, .height = 60, .width = 60 })) {}
CLAY(CLAY_ID("ProfilePicture"), CLAY_LAYOUT({ .sizing = { .width = CLAY_SIZING_FIXED(60), .height = CLAY_SIZING_FIXED(60) }}), CLAY_IMAGE({ .imageData = &profilePicture, .sourceDimensions = {60, 60} })) {}
CLAY_TEXT(CLAY_STRING("Clay - UI Library"), CLAY_TEXT_CONFIG({ .fontSize = 24, .textColor = {255, 255, 255, 255} }));
}
@ -1059,11 +1059,11 @@ CLAY(CLAY_IMAGE({ .image = { .format = IMAGE_FORMAT_RGBA, .internalData = &image
// Load an image somewhere in your code
Image profilePicture = LoadImage("profilePicture.png");
// Declare a reusable image config
Clay_ImageElementConfig imageConfig = (Clay_ImageElementConfig) { .imageData = &profilePicture, .height = 60, .width = 60 };
Clay_ImageElementConfig imageConfig = (Clay_ImageElementConfig) { .imageData = &profilePicture, .sourceDimensions = {60, 60} };
// Declare an image element using a reusable config
CLAY(CLAY_IMAGE(imageConfig)) {}
// Declare an image element using an inline config
CLAY(CLAY_IMAGE({ .imageData = &profilePicture, .height = 60, .width = 60 })) {}
CLAY(CLAY_IMAGE({ .imageData = &profilePicture, .sourceDimensions = {60, 60} })) {}
// Rendering example
Image *imageToRender = renderCommand->elementConfig.imageElementConfig->imageData;
```

Binary file not shown.

Binary file not shown.

Binary file not shown.

264
clay.h
View File

@ -21,6 +21,14 @@
#ifndef CLAY_HEADER
#define CLAY_HEADER
#if !( \
(defined(__cplusplus) && __cplusplus >= 202002L) || \
(defined(__STDC__) && __STDC__ == 1 && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
defined(_MSC_VER) \
)
#error "Clay requires C99, C++20, or MSVC"
#endif
#ifdef CLAY_WASM
#define CLAY_WASM_EXPORT(name) __attribute__((export_name(name)))
#else
@ -186,9 +194,6 @@ typedef struct Clay_Context Clay_Context;
CLAY__TYPEDEF(Clay_Arena, struct {
uintptr_t nextAllocation;
Clay_Context* context;
int32_t maxElementCount;
int32_t maxMeasureTextCacheWordCount;
size_t capacity;
char *memory;
});
@ -513,7 +518,9 @@ Clay_RenderCommand * Clay_RenderCommandArray_Get(Clay_RenderCommandArray* array,
void Clay_SetDebugModeEnabled(bool enabled);
bool Clay_IsDebugModeEnabled(void);
void Clay_SetCullingEnabled(bool enabled);
int32_t Clay_GetMaxElementCount(void);
void Clay_SetMaxElementCount(int32_t maxElementCount);
int32_t Clay_GetMaxMeasureTextCacheWordCount(void);
void Clay_SetMaxMeasureTextCacheWordCount(int32_t maxMeasureTextCacheWordCount);
// Internal API functions required by macros
@ -558,9 +565,7 @@ extern uint32_t Clay__debugViewWidth;
#define CLAY__MAXFLOAT 3.40282346638528859812e+38F
#endif
thread_local Clay_Context *Clay__currentContext;
thread_local int32_t Clay__nextInitMaxElementCount = 8192;
thread_local int32_t Clay__nextInitMaxMeasureTextCacheWordCount = 8192;
Clay_Context *Clay__currentContext;
void Clay__ErrorHandlerFunctionDefault(Clay_ErrorData errorText) {
(void) errorText;
}
@ -1358,21 +1363,23 @@ Clay__CharArray Clay__CharArray_Allocate_Arena(int32_t capacity, Clay_Arena *are
// __GENERATED__ template
struct Clay_Context {
bool warningsEnabled = true;
Clay_ErrorHandler errorHandler = { .errorHandlerFunction = Clay__ErrorHandlerFunctionDefault };
int32_t maxElementCount;
int32_t maxMeasureTextCacheWordCount;
bool warningsEnabled;
Clay_ErrorHandler errorHandler;
Clay_BooleanWarnings booleanWarnings;
Clay__WarningArray warnings = CLAY__DEFAULT_STRUCT;
Clay__WarningArray warnings;
Clay_PointerData pointerInfo = { .position = {-1, -1} };
Clay_Dimensions layoutDimensions = CLAY__DEFAULT_STRUCT;
Clay_ElementId dynamicElementIndexBaseHash = { .id = 128476991, .stringId = { .length = 8, .chars = "Auto ID" } };
uint32_t dynamicElementIndex = 0;
bool debugModeEnabled = false;
bool disableCulling = false;
bool externalScrollHandlingEnabled = false;
uint32_t debugSelectedElementId = 0;
uint32_t generation = 0;
uint64_t arenaResetOffset = 0;
Clay_PointerData pointerInfo;
Clay_Dimensions layoutDimensions;
Clay_ElementId dynamicElementIndexBaseHash;
uint32_t dynamicElementIndex;
bool debugModeEnabled;
bool disableCulling;
bool externalScrollHandlingEnabled;
uint32_t debugSelectedElementId;
uint32_t generation;
uint64_t arenaResetOffset;
Clay_Arena internalArena;
// Layout Elements / Render Commands
Clay_LayoutElementArray layoutElements;
@ -1414,7 +1421,14 @@ struct Clay_Context {
Clay__CharArray dynamicStringData;
Clay__DebugElementDataArray debugElementData;
};
CLAY__TYPEDEF(Clay_Context, struct Clay_Context);
struct Clay__AlignClay_Context {
char c;
Clay_Context x;
};
typedef struct {
Clay_Context wrapped;
} Clay__Clay_ContextWrapper;
Clay_Context* Clay__Context_Allocate_Arena(Clay_Arena *arena) {
uint32_t alignment = CLAY__ALIGNMENT(Clay_Context);
@ -1427,8 +1441,7 @@ Clay_Context* Clay__Context_Allocate_Arena(Clay_Arena *arena) {
return NULL;
}
arena->nextAllocation = arenaOffsetAligned + totalSizeBytes;
arena->context = (Clay_Context*)((uintptr_t)arena->memory + arenaOffsetAligned);
return arena->context;
return (Clay_Context*)((uintptr_t)arena->memory + arenaOffsetAligned);
}
Clay_String Clay__WriteStringToCharBuffer(Clay__CharArray *buffer, Clay_String string) {
@ -1447,12 +1460,13 @@ Clay_String Clay__WriteStringToCharBuffer(Clay__CharArray *buffer, Clay_String s
Clay_Vector2 (*Clay__QueryScrollOffset)(uint32_t elementId);
#endif
Clay_LayoutElement* Clay__GetOpenLayoutElement() {
Clay_LayoutElement* Clay__GetOpenLayoutElement(void) {
Clay_Context* context = Clay_GetCurrentContext();
return Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_Get(&context->openLayoutElementStack, context->openLayoutElementStack.length - 1));
}
uint32_t Clay__GetParentElementId(Clay_Context *context) {
uint32_t Clay__GetParentElementId(void) {
Clay_Context* context = Clay_GetCurrentContext();
return Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_Get(&context->openLayoutElementStack, context->openLayoutElementStack.length - 2))->id;
}
@ -1592,7 +1606,7 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text
}
#endif
uint32_t id = Clay__HashTextWithConfig(text, config);
uint32_t hashBucket = id % (context->internalArena.maxMeasureTextCacheWordCount / 32);
uint32_t hashBucket = id % (context->maxMeasureTextCacheWordCount / 32);
int32_t elementIndexPrevious = 0;
int32_t elementIndex = context->measureTextHashMap.internalArray[hashBucket];
while (elementIndex != 0) {
@ -1776,7 +1790,7 @@ void Clay__GenerateIdForAnonymousElement(Clay_LayoutElement *openLayoutElement)
Clay__StringArray_Add(&context->layoutElementIdStrings, elementId.stringId);
}
void Clay__ElementPostConfiguration() {
void Clay__ElementPostConfiguration(void) {
Clay_Context* context = Clay_GetCurrentContext();
if (context->booleanWarnings.maxElementsExceeded) {
return;
@ -1867,7 +1881,7 @@ void Clay__ElementPostConfiguration() {
context->elementConfigBuffer.length -= openLayoutElement->elementConfigs.length;
}
void Clay__CloseElement() {
void Clay__CloseElement(void) {
Clay_Context* context = Clay_GetCurrentContext();
if (context->booleanWarnings.maxElementsExceeded) {
return;
@ -1962,7 +1976,7 @@ void Clay__CloseElement() {
}
}
void Clay__OpenElement() {
void Clay__OpenElement(void) {
Clay_Context* context = Clay_GetCurrentContext();
if (context->layoutElements.length == context->layoutElements.capacity - 1 || context->booleanWarnings.maxElementsExceeded) {
context->booleanWarnings.maxElementsExceeded = true;
@ -2009,11 +2023,11 @@ void Clay__OpenTextElement(Clay_String text, Clay_TextElementConfig *textConfig)
Clay__int32_tArray_RemoveSwapback(&context->openLayoutElementStack, (int)context->openLayoutElementStack.length - 1);
}
void Clay__InitializeEphemeralMemory(Clay_Arena *arena) {
Clay_Context* context = arena->context;
int32_t maxElementCount = arena->maxElementCount;
void Clay__InitializeEphemeralMemory(Clay_Context* context) {
int32_t maxElementCount = context->maxElementCount;
// Ephemeral Memory - reset every frame
context->internalArena.nextAllocation = context->arenaResetOffset;
Clay_Arena *arena = &context->internalArena;
arena->nextAllocation = context->arenaResetOffset;
context->layoutElementChildrenBuffer = Clay__int32_tArray_Allocate_Arena(maxElementCount, arena);
context->layoutElements = Clay_LayoutElementArray_Allocate_Arena(maxElementCount, arena);
@ -2047,11 +2061,11 @@ void Clay__InitializeEphemeralMemory(Clay_Arena *arena) {
context->dynamicStringData = Clay__CharArray_Allocate_Arena(maxElementCount, arena);
}
void Clay__InitializePersistentMemory(Clay_Arena *arena) {
void Clay__InitializePersistentMemory(Clay_Context* context) {
// Persistent memory - initialized once and not reset
Clay_Context* context = arena->context;
int32_t maxElementCount = arena->maxElementCount;
int32_t maxMeasureTextCacheWordCount = arena->maxMeasureTextCacheWordCount;
int32_t maxElementCount = context->maxElementCount;
int32_t maxMeasureTextCacheWordCount = context->maxMeasureTextCacheWordCount;
Clay_Arena *arena = &context->internalArena;
context->scrollContainerDatas = Clay__ScrollContainerDataInternalArray_Allocate_Arena(10, arena);
context->layoutElementsHashMapInternal = Clay__LayoutElementHashMapItemArray_Allocate_Arena(maxElementCount, arena);
@ -2067,72 +2081,48 @@ void Clay__InitializePersistentMemory(Clay_Arena *arena) {
}
CLAY__TYPEDEF(Clay__SizeDistributionType, enum {
CLAY__SIZE_DISTRIBUTION_TYPE_SCROLL_CONTAINER,
CLAY__SIZE_DISTRIBUTION_TYPE_RESIZEABLE_CONTAINER,
CLAY__SIZE_DISTRIBUTION_TYPE_GROW_CONTAINER,
});
float Clay__DistributeSizeAmongChildren(bool xAxis, float sizeToDistribute, Clay__int32_tArray resizableContainerBuffer, Clay__SizeDistributionType distributionType) {
void Clay__CompressChildrenAlongAxis(bool xAxis, float totalSizeToDistribute, Clay__int32_tArray resizableContainerBuffer) {
Clay_Context* context = Clay_GetCurrentContext();
Clay__int32_tArray remainingElements = context->openClipElementStack;
remainingElements.length = 0;
Clay__int32_tArray largestContainers = context->openClipElementStack;
largestContainers.length = 0;
for (int32_t i = 0; i < resizableContainerBuffer.length; ++i) {
Clay__int32_tArray_Add(&remainingElements, Clay__int32_tArray_Get(&resizableContainerBuffer, i));
}
while (totalSizeToDistribute > 0) {
float largestSize = 0;
float targetSize = 0;
for (int32_t i = 0; i < resizableContainerBuffer.length; ++i) {
Clay_LayoutElement *childElement = Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_Get(&resizableContainerBuffer, i));
float childSize = xAxis ? childElement->dimensions.width : childElement->dimensions.height;
if (childSize == largestSize) {
Clay__int32_tArray_Add(&largestContainers, Clay__int32_tArray_Get(&resizableContainerBuffer, i));
} else if (childSize > largestSize) {
targetSize = largestSize;
largestSize = childSize;
largestContainers.length = 0;
Clay__int32_tArray_Add(&largestContainers, Clay__int32_tArray_Get(&resizableContainerBuffer, i));
}
else if (childSize > targetSize) {
targetSize = childSize;
}
}
while (sizeToDistribute != 0 && remainingElements.length > 0) {
float dividedSize = sizeToDistribute / (float)remainingElements.length;
for (int32_t childOffset = 0; childOffset < remainingElements.length; childOffset++) {
Clay_LayoutElement *childElement = Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_Get(&remainingElements, childOffset));
Clay_SizingAxis childSizing = xAxis ? childElement->layoutConfig->sizing.width : childElement->layoutConfig->sizing.height;
targetSize = CLAY__MAX(targetSize, (largestSize * largestContainers.length) - totalSizeToDistribute) / largestContainers.length;
for (int32_t childOffset = 0; childOffset < largestContainers.length; childOffset++) {
Clay_LayoutElement *childElement = Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_Get(&largestContainers, childOffset));
float *childSize = xAxis ? &childElement->dimensions.width : &childElement->dimensions.height;
float childMinSize = xAxis ? childElement->minDimensions.width : childElement->minDimensions.height;
bool canDistribute = true;
if ((sizeToDistribute < 0 && *childSize == childSizing.size.minMax.min) || (sizeToDistribute > 0 && *childSize == childSizing.size.minMax.max)) {
canDistribute = false;
}
// Currently, we don't support squishing aspect ratio images on their Y axis as it would break ratio
else if (!xAxis && Clay__ElementHasConfig(childElement, CLAY__ELEMENT_CONFIG_TYPE_IMAGE)) {
canDistribute = false;
}
else {
switch (distributionType) {
case CLAY__SIZE_DISTRIBUTION_TYPE_RESIZEABLE_CONTAINER: break;
case CLAY__SIZE_DISTRIBUTION_TYPE_GROW_CONTAINER: if (childSizing.type != CLAY__SIZING_TYPE_GROW) canDistribute = false; break;
case CLAY__SIZE_DISTRIBUTION_TYPE_SCROLL_CONTAINER: {
if (Clay__ElementHasConfig(childElement, CLAY__ELEMENT_CONFIG_TYPE_SCROLL_CONTAINER)) {
Clay_ScrollElementConfig *scrollConfig = Clay__FindElementConfigWithType(childElement, CLAY__ELEMENT_CONFIG_TYPE_SCROLL_CONTAINER).scrollElementConfig;
if ((xAxis && !scrollConfig->horizontal) || (!xAxis && !scrollConfig->vertical)) {
Clay__int32_tArray_RemoveSwapback(&remainingElements, childOffset);
childOffset--;
continue;
}
}
}
}
}
if (!canDistribute) {
Clay__int32_tArray_RemoveSwapback(&remainingElements, childOffset);
childOffset--;
continue;
}
float oldChildSize = *childSize;
*childSize = CLAY__MAX(CLAY__MAX(CLAY__MIN(childSizing.size.minMax.max, *childSize + dividedSize), childSizing.size.minMax.min), childMinSize);
float diff = *childSize - oldChildSize;
if (diff > -0.01 && diff < 0.01) {
Clay__int32_tArray_RemoveSwapback(&remainingElements, childOffset);
*childSize = CLAY__MAX(childMinSize, targetSize);
totalSizeToDistribute -= (oldChildSize - *childSize);
if (*childSize == childMinSize) {
Clay__int32_tArray_RemoveSwapback(&largestContainers, childOffset);
childOffset--;
continue;
}
sizeToDistribute -= diff;
}
if (largestContainers.length == 0) {
break;
}
}
return (sizeToDistribute > -0.01 && sizeToDistribute < 0.01) ? 0 : sizeToDistribute;
}
void Clay__SizeContainersAlongAxis(bool xAxis) {
@ -2226,7 +2216,7 @@ void Clay__SizeContainersAlongAxis(bool xAxis) {
if (sizingAlongAxis) {
float sizeToDistribute = parentSize - parentPadding * 2 - innerContentSize;
// If the content is too large, compress the children as much as possible
// The content is too large, compress the children as much as possible
if (sizeToDistribute < 0) {
// If the parent can scroll in the axis direction in this direction, don't compress children, just leave them alone
if (Clay__ElementHasConfig(parent, CLAY__ELEMENT_CONFIG_TYPE_SCROLL_CONTAINER)) {
@ -2236,12 +2226,7 @@ void Clay__SizeContainersAlongAxis(bool xAxis) {
}
}
// Scrolling containers preferentially compress before others
sizeToDistribute = Clay__DistributeSizeAmongChildren(xAxis, sizeToDistribute, resizableContainerBuffer, CLAY__SIZE_DISTRIBUTION_TYPE_SCROLL_CONTAINER);
// If there is still height to make up, remove it from all containers that haven't hit their minimum size
if (sizeToDistribute < 0) {
Clay__DistributeSizeAmongChildren(xAxis, sizeToDistribute, resizableContainerBuffer, CLAY__SIZE_DISTRIBUTION_TYPE_RESIZEABLE_CONTAINER);
}
Clay__CompressChildrenAlongAxis(xAxis, -sizeToDistribute, resizableContainerBuffer);
// The content is too small, allow SIZING_GROW containers to expand
} else if (sizeToDistribute > 0 && growContainerCount > 0) {
float targetSize = (sizeToDistribute + growContainerContentSize) / (float)growContainerCount;
@ -3528,10 +3513,10 @@ Clay__WarningArray Clay__WarningArray_Allocate_Arena(int32_t capacity, Clay_Aren
arena->nextAllocation = arenaOffsetAligned + totalSizeBytes;
}
else {
arena->context->errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
Clay__currentContext->errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
.errorType = CLAY_ERROR_TYPE_ARENA_CAPACITY_EXCEEDED,
.errorText = CLAY_STRING("Clay attempted to allocate memory in its arena, but ran out of capacity. Try increasing the capacity of the arena passed to Clay_Initialize()"),
.userData = arena->context->errorHandler.userData });
.userData = Clay__currentContext->errorHandler.userData });
}
return array;
}
@ -3556,10 +3541,10 @@ void* Clay__Array_Allocate_Arena(int32_t capacity, uint32_t itemSize, uint32_t a
return (void*)((uintptr_t)arena->memory + (uintptr_t)arenaOffsetAligned);
}
else {
arena->context->errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
Clay__currentContext->errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
.errorType = CLAY_ERROR_TYPE_ARENA_CAPACITY_EXCEEDED,
.errorText = CLAY_STRING("Clay attempted to allocate memory in its arena, but ran out of capacity. Try increasing the capacity of the arena passed to Clay_Initialize()"),
.userData = arena->context->errorHandler.userData });
.userData = Clay__currentContext->errorHandler.userData });
}
return CLAY__NULL;
}
@ -3593,18 +3578,25 @@ bool Clay__Array_AddCapacityCheck(int32_t length, int32_t capacity)
// PUBLIC API FROM HERE ---------------------------------------
CLAY_WASM_EXPORT("Clay_MinMemorySize")
uint32_t Clay_MinMemorySize() {
Clay_Context fakeContext = {};
Clay_Arena fakeArena = {
.maxElementCount = Clay__nextInitMaxElementCount,
.maxMeasureTextCacheWordCount = Clay__nextInitMaxMeasureTextCacheWordCount,
.capacity = SIZE_MAX,
.memory = (char*)&fakeContext,
uint32_t Clay_MinMemorySize(void) {
Clay_Context fakeContext = {
.maxElementCount = 8192,
.maxMeasureTextCacheWordCount = 16384,
.internalArena = {
.capacity = SIZE_MAX,
.memory = (char*)&fakeContext,
}
};
Clay__Context_Allocate_Arena(&fakeArena);
Clay__InitializePersistentMemory(&fakeArena);
Clay__InitializeEphemeralMemory(&fakeArena);
return fakeArena.nextAllocation;
Clay_Context* currentContext = Clay_GetCurrentContext();
if (currentContext) {
fakeContext.maxElementCount = currentContext->maxElementCount;
fakeContext.maxMeasureTextCacheWordCount = currentContext->maxElementCount;
}
// Reserve space in the arena for the context, important for calculating min memory size correctly
Clay__Context_Allocate_Arena(&fakeContext.internalArena);
Clay__InitializePersistentMemory(&fakeContext);
Clay__InitializeEphemeralMemory(&fakeContext);
return fakeContext.internalArena.nextAllocation;
}
CLAY_WASM_EXPORT("Clay_CreateArenaWithCapacityAndMemory")
@ -3701,17 +3693,16 @@ void Clay_SetPointerState(Clay_Vector2 position, bool isPointerDown) {
CLAY_WASM_EXPORT("Clay_Initialize")
Clay_Context* Clay_Initialize(Clay_Arena arena, Clay_Dimensions layoutDimensions, Clay_ErrorHandler errorHandler) {
arena.maxElementCount = Clay__nextInitMaxElementCount;
arena.maxMeasureTextCacheWordCount = Clay__nextInitMaxMeasureTextCacheWordCount;
Clay_Context* context = Clay__Context_Allocate_Arena(&arena);
if (context == NULL)
{
return NULL;
}
if (context == NULL) return NULL;
// DEFAULTS
context->maxElementCount = 8192;
context->maxMeasureTextCacheWordCount = context->maxElementCount * 2;
context->errorHandler = CLAY__INIT(Clay_ErrorHandler) { Clay__ErrorHandlerFunctionDefault };
Clay_SetCurrentContext(context);
context->internalArena = arena;
Clay__InitializePersistentMemory(&context->internalArena);
Clay__InitializeEphemeralMemory(&context->internalArena);
Clay__InitializePersistentMemory(context);
Clay__InitializeEphemeralMemory(context);
for (int32_t i = 0; i < context->layoutElementsHashMap.capacity; ++i) {
context->layoutElementsHashMap.internalArray[i] = -1;
}
@ -3721,13 +3712,13 @@ Clay_Context* Clay_Initialize(Clay_Arena arena, Clay_Dimensions layoutDimensions
context->measureTextHashMapInternal.length = 1; // Reserve the 0 value to mean "no next element"
context->layoutDimensions = layoutDimensions;
if (errorHandler.errorHandlerFunction) {
arena.context->errorHandler = errorHandler;
context->errorHandler = errorHandler;
}
return context;
}
CLAY_WASM_EXPORT("Clay_GetCurrentContext")
Clay_Context* Clay_GetCurrentContext() {
Clay_Context* Clay_GetCurrentContext(void) {
return Clay__currentContext;
}
@ -3851,9 +3842,9 @@ void Clay_UpdateScrollContainers(bool enableDragScrolling, Clay_Vector2 scrollDe
}
CLAY_WASM_EXPORT("Clay_BeginLayout")
void Clay_BeginLayout() {
void Clay_BeginLayout(void) {
Clay_Context* context = Clay_GetCurrentContext();
Clay__InitializeEphemeralMemory(&context->internalArena);
Clay__InitializeEphemeralMemory(context);
context->generation++;
context->dynamicElementIndex = 0;
// Set up the root container that covers the entire window
@ -3901,7 +3892,7 @@ Clay_ElementId Clay_GetElementIdWithIndex(Clay_String idString, uint32_t index)
return Clay__HashString(idString, index, 0);
}
bool Clay_Hovered() {
bool Clay_Hovered(void) {
Clay_Context* context = Clay_GetCurrentContext();
if (context->booleanWarnings.maxElementsExceeded) {
return false;
@ -3969,7 +3960,7 @@ void Clay_SetDebugModeEnabled(bool enabled) {
}
CLAY_WASM_EXPORT("Clay_IsDebugModeEnabled")
bool Clay_IsDebugModeEnabled() {
bool Clay_IsDebugModeEnabled(void) {
Clay_Context* context = Clay_GetCurrentContext();
return context->debugModeEnabled;
}
@ -3986,14 +3977,27 @@ void Clay_SetExternalScrollHandlingEnabled(bool enabled) {
context->externalScrollHandlingEnabled = enabled;
}
CLAY_WASM_EXPORT("Clay_GetMaxElementCount")
int32_t Clay_GetMaxElementCount(void) {
Clay_Context* context = Clay_GetCurrentContext();
return context->maxElementCount;
}
CLAY_WASM_EXPORT("Clay_SetMaxElementCount")
void Clay_SetMaxElementCount(int32_t maxElementCount) {
Clay__nextInitMaxElementCount = maxElementCount;
Clay_Context* context = Clay_GetCurrentContext();
context->maxElementCount = maxElementCount;
}
CLAY_WASM_EXPORT("Clay_GetMaxMeasureTextCacheWordCount")
int32_t Clay_GetMaxMeasureTextCacheWordCount(void) {
Clay_Context* context = Clay_GetCurrentContext();
return context->maxMeasureTextCacheWordCount;
}
CLAY_WASM_EXPORT("Clay_SetMaxMeasureTextCacheWordCount")
void Clay_SetMaxMeasureTextCacheWordCount(int32_t maxMeasureTextCacheWordCount) {
Clay__nextInitMaxMeasureTextCacheWordCount = maxMeasureTextCacheWordCount;
Clay__currentContext->maxMeasureTextCacheWordCount = maxMeasureTextCacheWordCount;
}
#endif // CLAY_IMPLEMENTATION

View File

@ -208,10 +208,10 @@ void HandleClayErrors(Clay_ErrorData errorData) {
printf("%s", errorData.errorText.chars);
if (errorData.errorType == CLAY_ERROR_TYPE_ELEMENTS_CAPACITY_EXCEEDED) {
reinitializeClay = true;
Clay_SetMaxElementCount(Clay__maxElementCount * 2);
Clay_SetMaxElementCount(Clay_GetMaxElementCount() * 2);
} else if (errorData.errorType == CLAY_ERROR_TYPE_TEXT_MEASUREMENT_CAPACITY_EXCEEDED) {
reinitializeClay = true;
Clay_SetMaxMeasureTextCacheWordCount(Clay__maxMeasureTextCacheWordCount * 2);
Clay_SetMaxMeasureTextCacheWordCount(Clay_GetMaxMeasureTextCacheWordCount() * 2);
}
}