mirror of
https://github.com/nicbarker/clay.git
synced 2025-04-22 06:08:03 +00:00
Compare commits
6 Commits
f49ec15da8
...
45e60ee36c
Author | SHA1 | Date | |
---|---|---|---|
|
45e60ee36c | ||
|
258993ad17 | ||
|
2603add03c | ||
|
f335ad09e2 | ||
|
5a04f6fbdc | ||
|
b62667dd10 |
43
README.md
43
README.md
@ -480,12 +480,30 @@ Takes a pointer to a function that can be used to measure the `width, height` di
|
|||||||
|
|
||||||
**Note 2: It is essential that this function is as fast as possible.** For text heavy use-cases this function is called many times, and despite the fact that clay caches text measurements internally, it can easily become the dominant overall layout cost if the provided function is slow. **This is on the hot path!**
|
**Note 2: It is essential that this function is as fast as possible.** For text heavy use-cases this function is called many times, and despite the fact that clay caches text measurements internally, it can easily become the dominant overall layout cost if the provided function is slow. **This is on the hot path!**
|
||||||
|
|
||||||
|
### Clay_SetMaxElementCount
|
||||||
|
|
||||||
|
`void Clay_SetMaxElementCount(uint32_t maxElementCount)`
|
||||||
|
|
||||||
|
Updates the internal maximum element count, allowing clay to allocate larger UI hierarchies.
|
||||||
|
|
||||||
|
**Note: You will need to reinitialize clay, after calling [Clay_MinMemorySize()](#clay_minmemorysize) to calculate updated memory requirements.**
|
||||||
|
|
||||||
|
### Clay_SetMaxMeasureTextCacheWordCount
|
||||||
|
|
||||||
|
`void Clay_SetMaxMeasureTextCacheWordCount(uint32_t maxMeasureTextCacheWordCount)`
|
||||||
|
|
||||||
|
Updates the internal text measurement cache size, allowing clay to allocate more text. The value represents how many seperate words can be stored in the text measurement cache.
|
||||||
|
|
||||||
|
**Note: You will need to reinitialize clay, after calling [Clay_MinMemorySize()](#clay_minmemorysize) to calculate updated memory requirements.**
|
||||||
|
|
||||||
### Clay_Initialize
|
### Clay_Initialize
|
||||||
|
|
||||||
`void Clay_Initialize(Clay_Arena arena, Clay_Dimensions layoutDimensions, Clay_ErrorHandler errorHandler)`
|
`void Clay_Initialize(Clay_Arena arena, Clay_Dimensions layoutDimensions, Clay_ErrorHandler errorHandler)`
|
||||||
|
|
||||||
Initializes the internal memory mapping, sets the internal dimensions for layout, and binds an error handler for clay to use when something goes wrong.
|
Initializes the internal memory mapping, sets the internal dimensions for layout, and binds an error handler for clay to use when something goes wrong.
|
||||||
|
|
||||||
|
Reference: [Clay_Arena](#clay_createarenawithcapacityandmemory), [Clay_ErrorHandler](#clay_errorhandler)
|
||||||
|
|
||||||
### Clay_SetLayoutDimensions
|
### Clay_SetLayoutDimensions
|
||||||
|
|
||||||
`void Clay_SetLayoutDimensions(Clay_Dimensions dimensions)`
|
`void Clay_SetLayoutDimensions(Clay_Dimensions dimensions)`
|
||||||
@ -1542,6 +1560,29 @@ Element is subject to [culling](#visibility-culling). Otherwise, a single `Clay_
|
|||||||
|
|
||||||
## Data Structures & Definitions
|
## Data Structures & Definitions
|
||||||
|
|
||||||
|
### Clay_String
|
||||||
|
|
||||||
|
```C
|
||||||
|
typedef struct {
|
||||||
|
int length;
|
||||||
|
const char *chars;
|
||||||
|
} Clay_String;
|
||||||
|
```
|
||||||
|
|
||||||
|
`Clay_String` is a string container that clay uses internally to represent all strings.
|
||||||
|
|
||||||
|
**Fields**
|
||||||
|
|
||||||
|
**`.length`** - `int`
|
||||||
|
|
||||||
|
The number of characters in the string, _not including an optional null terminator._
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**`.chars`** - `const char *`
|
||||||
|
|
||||||
|
A pointer to the contents of the string. This data is not guaranteed to be null terminated, so if you are passing it to code that expects standard null terminated C strings, you will need to copy the data and append a null terminator.
|
||||||
|
|
||||||
### Clay_ElementId
|
### Clay_ElementId
|
||||||
|
|
||||||
```C
|
```C
|
||||||
@ -1823,7 +1864,7 @@ An enum representing the type of error Clay encountered. It's up to the user to
|
|||||||
- `CLAY_ERROR_TYPE_TEXT_MEASUREMENT_FUNCTION_NOT_PROVIDED` - The user is attempting to use `CLAY_TEXT` and either forgot to call [Clay_SetMeasureTextFunction](#clay_setmeasuretextfunction) or accidentally passed a null function pointer.
|
- `CLAY_ERROR_TYPE_TEXT_MEASUREMENT_FUNCTION_NOT_PROVIDED` - The user is attempting to use `CLAY_TEXT` and either forgot to call [Clay_SetMeasureTextFunction](#clay_setmeasuretextfunction) or accidentally passed a null function pointer.
|
||||||
- `CLAY_ERROR_TYPE_ARENA_CAPACITY_EXCEEDED` - Clay was initialized with an Arena that was too small for the configured [Clay_SetMaxElementCount](#clay_setmaxelementcount). Try using [Clay_MinMemorySize()](#clay_minmemorysize) to get the exact number of bytes required by the current configuration.
|
- `CLAY_ERROR_TYPE_ARENA_CAPACITY_EXCEEDED` - Clay was initialized with an Arena that was too small for the configured [Clay_SetMaxElementCount](#clay_setmaxelementcount). Try using [Clay_MinMemorySize()](#clay_minmemorysize) to get the exact number of bytes required by the current configuration.
|
||||||
- `CLAY_ERROR_TYPE_ELEMENTS_CAPACITY_EXCEEDED` - The declared UI hierarchy has too many elements for the configured max element count. Use [Clay_SetMaxElementCount](#clay_setmaxelementcount) to increase the max, then call [Clay_MinMemorySize()](#clay_minmemorysize) again and reinitialize clay's memory with the required size.
|
- `CLAY_ERROR_TYPE_ELEMENTS_CAPACITY_EXCEEDED` - The declared UI hierarchy has too many elements for the configured max element count. Use [Clay_SetMaxElementCount](#clay_setmaxelementcount) to increase the max, then call [Clay_MinMemorySize()](#clay_minmemorysize) again and reinitialize clay's memory with the required size.
|
||||||
- `CLAY_ERROR_TYPE_ELEMENTS_CAPACITY_EXCEEDED` - The declared UI hierarchy has too much text for the configured text measure cache size. Use [Clay_SetMeasureTextCacheSize](#clay_setmeasuretextcachesize) to increase the max, then call [Clay_MinMemorySize()](#clay_minmemorysize) again and reinitialize clay's memory with the required size.
|
- `CLAY_ERROR_TYPE_ELEMENTS_CAPACITY_EXCEEDED` - The declared UI hierarchy has too much text for the configured text measure cache size. Use [Clay_SetMaxMeasureTextCacheWordCount](#clay_setmeasuretextcachesize) to increase the max, then call [Clay_MinMemorySize()](#clay_minmemorysize) again and reinitialize clay's memory with the required size.
|
||||||
- `CLAY_ERROR_TYPE_DUPLICATE_ID` - Two elements in Clays UI Hierarchy have been declared with exactly the same ID. Set a breakpoint in your error handler function for a stack trace back to exactly where this occured.
|
- `CLAY_ERROR_TYPE_DUPLICATE_ID` - Two elements in Clays UI Hierarchy have been declared with exactly the same ID. Set a breakpoint in your error handler function for a stack trace back to exactly where this occured.
|
||||||
- `CLAY_ERROR_TYPE_FLOATING_CONTAINER_PARENT_NOT_FOUND` - A `CLAY_FLOATING` element was declared with the `.parentId` property, but no element with that ID was found. Set a breakpoint in your error handler function for a stack trace back to exactly where this occured.
|
- `CLAY_ERROR_TYPE_FLOATING_CONTAINER_PARENT_NOT_FOUND` - A `CLAY_FLOATING` element was declared with the `.parentId` property, but no element with that ID was found. Set a breakpoint in your error handler function for a stack trace back to exactly where this occured.
|
||||||
- `CLAY_ERROR_TYPE_INTERNAL_ERROR` - Clay has encountered an internal logic or memory error. Please report this as a bug with a stack trace to help us fix these!
|
- `CLAY_ERROR_TYPE_INTERNAL_ERROR` - Clay has encountered an internal logic or memory error. Please report this as a bug with a stack trace to help us fix these!
|
||||||
|
43
clay.h
43
clay.h
@ -144,9 +144,8 @@ typedef struct
|
|||||||
} Clay__StringArray;
|
} Clay__StringArray;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Clay_String label;
|
uintptr_t nextAllocation;
|
||||||
uint64_t nextAllocation;
|
size_t capacity;
|
||||||
uint64_t capacity;
|
|
||||||
char *memory;
|
char *memory;
|
||||||
} Clay_Arena;
|
} Clay_Arena;
|
||||||
|
|
||||||
@ -479,7 +478,7 @@ Clay_RenderCommand * Clay_RenderCommandArray_Get(Clay_RenderCommandArray* array,
|
|||||||
void Clay_SetDebugModeEnabled(bool enabled);
|
void Clay_SetDebugModeEnabled(bool enabled);
|
||||||
void Clay_SetCullingEnabled(bool enabled);
|
void Clay_SetCullingEnabled(bool enabled);
|
||||||
void Clay_SetMaxElementCount(uint32_t maxElementCount);
|
void Clay_SetMaxElementCount(uint32_t maxElementCount);
|
||||||
void Clay_SetMeasureTextCacheSize(uint32_t measureTextCacheSize);
|
void Clay_SetMaxMeasureTextCacheWordCount(uint32_t maxMeasureTextCacheWordCount);
|
||||||
|
|
||||||
// Internal API functions required by macros
|
// Internal API functions required by macros
|
||||||
void Clay__OpenElement();
|
void Clay__OpenElement();
|
||||||
@ -525,10 +524,11 @@ extern uint32_t Clay__debugViewWidth;
|
|||||||
|
|
||||||
bool Clay__warningsEnabled = true;
|
bool Clay__warningsEnabled = true;
|
||||||
uint32_t Clay__maxElementCount = 8192;
|
uint32_t Clay__maxElementCount = 8192;
|
||||||
uint32_t Clay__measureTextCacheSize = 16384;
|
uint32_t Clay__maxMeasureTextCacheWordCount = 16384;
|
||||||
Clay_ErrorHandler Clay__errorHandler = CLAY__INIT(Clay_ErrorHandler) { .errorHandlerFunction = Clay__Noop };
|
void Clay__ErrorHandlerFunctionDefault(Clay_ErrorData errorText) {}
|
||||||
|
Clay_ErrorHandler Clay__errorHandler = CLAY__INIT(Clay_ErrorHandler) { .errorHandlerFunction = Clay__ErrorHandlerFunctionDefault };
|
||||||
|
|
||||||
void Clay__Noop() {};
|
void Clay__Noop() {}
|
||||||
|
|
||||||
Clay_String CLAY__SPACECHAR = CLAY__INIT(Clay_String) { .length = 1, .chars = " " };
|
Clay_String CLAY__SPACECHAR = CLAY__INIT(Clay_String) { .length = 1, .chars = " " };
|
||||||
Clay_String CLAY__STRING_DEFAULT = CLAY__INIT(Clay_String) { .length = 0, .chars = NULL };
|
Clay_String CLAY__STRING_DEFAULT = CLAY__INIT(Clay_String) { .length = 0, .chars = NULL };
|
||||||
@ -561,7 +561,7 @@ typedef struct
|
|||||||
Clay__WarningArray Clay__WarningArray_Allocate_Arena(uint32_t capacity, Clay_Arena *arena) {
|
Clay__WarningArray Clay__WarningArray_Allocate_Arena(uint32_t capacity, Clay_Arena *arena) {
|
||||||
size_t totalSizeBytes = capacity * sizeof(Clay_String);
|
size_t totalSizeBytes = capacity * sizeof(Clay_String);
|
||||||
Clay__WarningArray array = CLAY__INIT(Clay__WarningArray){.capacity = capacity, .length = 0};
|
Clay__WarningArray array = CLAY__INIT(Clay__WarningArray){.capacity = capacity, .length = 0};
|
||||||
uintptr_t nextAllocAddress = (uintptr_t)arena->nextAllocation + (uintptr_t)arena->memory;
|
uintptr_t nextAllocAddress = arena->nextAllocation + (uintptr_t)arena->memory;
|
||||||
uintptr_t arenaOffsetAligned = nextAllocAddress + (CLAY__ALIGNMENT(Clay_String) - (nextAllocAddress % CLAY__ALIGNMENT(Clay_String)));
|
uintptr_t arenaOffsetAligned = nextAllocAddress + (CLAY__ALIGNMENT(Clay_String) - (nextAllocAddress % CLAY__ALIGNMENT(Clay_String)));
|
||||||
arenaOffsetAligned -= (uintptr_t)arena->memory;
|
arenaOffsetAligned -= (uintptr_t)arena->memory;
|
||||||
if (arenaOffsetAligned + totalSizeBytes <= arena->capacity) {
|
if (arenaOffsetAligned + totalSizeBytes <= arena->capacity) {
|
||||||
@ -591,7 +591,7 @@ Clay__Warning *Clay__WarningArray_Add(Clay__WarningArray *array, Clay__Warning i
|
|||||||
void* Clay__Array_Allocate_Arena(uint32_t capacity, uint32_t itemSize, uint32_t alignment, Clay_Arena *arena)
|
void* Clay__Array_Allocate_Arena(uint32_t capacity, uint32_t itemSize, uint32_t alignment, Clay_Arena *arena)
|
||||||
{
|
{
|
||||||
size_t totalSizeBytes = capacity * itemSize;
|
size_t totalSizeBytes = capacity * itemSize;
|
||||||
uintptr_t nextAllocAddress = (uintptr_t)arena->nextAllocation + (uintptr_t)arena->memory;
|
uintptr_t nextAllocAddress = arena->nextAllocation + (uintptr_t)arena->memory;
|
||||||
uintptr_t arenaOffsetAligned = nextAllocAddress + (alignment - (nextAllocAddress % alignment));
|
uintptr_t arenaOffsetAligned = nextAllocAddress + (alignment - (nextAllocAddress % alignment));
|
||||||
arenaOffsetAligned -= (uintptr_t)arena->memory;
|
arenaOffsetAligned -= (uintptr_t)arena->memory;
|
||||||
if (arenaOffsetAligned + totalSizeBytes <= arena->capacity) {
|
if (arenaOffsetAligned + totalSizeBytes <= arena->capacity) {
|
||||||
@ -1569,6 +1569,7 @@ Clay__MeasuredWord *Clay__AddMeasuredWord(Clay__MeasuredWord word, Clay__Measure
|
|||||||
}
|
}
|
||||||
|
|
||||||
Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_TextElementConfig *config) {
|
Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_TextElementConfig *config) {
|
||||||
|
#ifndef CLAY_WASM
|
||||||
if (!Clay__MeasureText) {
|
if (!Clay__MeasureText) {
|
||||||
Clay__errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
|
Clay__errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
|
||||||
.errorType = CLAY_ERROR_TYPE_TEXT_MEASUREMENT_FUNCTION_NOT_PROVIDED,
|
.errorType = CLAY_ERROR_TYPE_TEXT_MEASUREMENT_FUNCTION_NOT_PROVIDED,
|
||||||
@ -1576,8 +1577,9 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text
|
|||||||
.userData = Clay__errorHandler.userData });
|
.userData = Clay__errorHandler.userData });
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
uint32_t id = Clay__HashTextWithConfig(text, config);
|
uint32_t id = Clay__HashTextWithConfig(text, config);
|
||||||
uint32_t hashBucket = id % (Clay__measureTextCacheSize / 8);
|
uint32_t hashBucket = id % (Clay__maxMeasureTextCacheWordCount / 32);
|
||||||
int32_t elementIndexPrevious = 0;
|
int32_t elementIndexPrevious = 0;
|
||||||
int32_t elementIndex = Clay__measureTextHashMap.internalArray[hashBucket];
|
int32_t elementIndex = Clay__measureTextHashMap.internalArray[hashBucket];
|
||||||
while (elementIndex != 0) {
|
while (elementIndex != 0) {
|
||||||
@ -1647,7 +1649,7 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text
|
|||||||
if (!Clay__booleanWarnings.maxTextMeasureCacheExceeded) {
|
if (!Clay__booleanWarnings.maxTextMeasureCacheExceeded) {
|
||||||
Clay__errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
|
Clay__errorHandler.errorHandlerFunction(CLAY__INIT(Clay_ErrorData) {
|
||||||
.errorType = CLAY_ERROR_TYPE_TEXT_MEASUREMENT_CAPACITY_EXCEEDED,
|
.errorType = CLAY_ERROR_TYPE_TEXT_MEASUREMENT_CAPACITY_EXCEEDED,
|
||||||
.errorText = CLAY_STRING("Clay has run out of space in it's internal text measurement cache. Try using Clay_SetMeasureTextCacheSize() (default 16384, with 1 unit storing 1 measured word)."),
|
.errorText = CLAY_STRING("Clay has run out of space in it's internal text measurement cache. Try using Clay_SetMaxMeasureTextCacheWordCount() (default 16384, with 1 unit storing 1 measured word)."),
|
||||||
.userData = Clay__errorHandler.userData });
|
.userData = Clay__errorHandler.userData });
|
||||||
Clay__booleanWarnings.maxTextMeasureCacheExceeded = true;
|
Clay__booleanWarnings.maxTextMeasureCacheExceeded = true;
|
||||||
}
|
}
|
||||||
@ -2028,9 +2030,9 @@ void Clay__InitializePersistentMemory(Clay_Arena *arena) {
|
|||||||
Clay__layoutElementsHashMap = Clay__int32_tArray_Allocate_Arena(Clay__maxElementCount, arena);
|
Clay__layoutElementsHashMap = Clay__int32_tArray_Allocate_Arena(Clay__maxElementCount, arena);
|
||||||
Clay__measureTextHashMapInternal = Clay__MeasureTextCacheItemArray_Allocate_Arena(Clay__maxElementCount, arena);
|
Clay__measureTextHashMapInternal = Clay__MeasureTextCacheItemArray_Allocate_Arena(Clay__maxElementCount, arena);
|
||||||
Clay__measureTextHashMapInternalFreeList = Clay__int32_tArray_Allocate_Arena(Clay__maxElementCount, arena);
|
Clay__measureTextHashMapInternalFreeList = Clay__int32_tArray_Allocate_Arena(Clay__maxElementCount, arena);
|
||||||
Clay__measuredWordsFreeList = Clay__int32_tArray_Allocate_Arena(Clay__measureTextCacheSize, arena);
|
Clay__measuredWordsFreeList = Clay__int32_tArray_Allocate_Arena(Clay__maxMeasureTextCacheWordCount, arena);
|
||||||
Clay__measureTextHashMap = Clay__int32_tArray_Allocate_Arena(Clay__maxElementCount, arena);
|
Clay__measureTextHashMap = Clay__int32_tArray_Allocate_Arena(Clay__maxElementCount, arena);
|
||||||
Clay__measuredWords = Clay__MeasuredWordArray_Allocate_Arena(Clay__measureTextCacheSize, arena);
|
Clay__measuredWords = Clay__MeasuredWordArray_Allocate_Arena(Clay__maxMeasureTextCacheWordCount, arena);
|
||||||
Clay__pointerOverIds = Clay__ElementIdArray_Allocate_Arena(Clay__maxElementCount, arena);
|
Clay__pointerOverIds = Clay__ElementIdArray_Allocate_Arena(Clay__maxElementCount, arena);
|
||||||
Clay__debugElementData = Clay__DebugElementDataArray_Allocate_Arena(Clay__maxElementCount, arena);
|
Clay__debugElementData = Clay__DebugElementDataArray_Allocate_Arena(Clay__maxElementCount, arena);
|
||||||
Clay__arenaResetOffset = arena->nextAllocation;
|
Clay__arenaResetOffset = arena->nextAllocation;
|
||||||
@ -3178,7 +3180,9 @@ void Clay__RenderDebugView() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int32_t highlightedRow = (int32_t)((Clay__pointerInfo.position.y - scrollYOffset) / (float)CLAY__DEBUGVIEW_ROW_HEIGHT) - 1;
|
int32_t highlightedRow = Clay__pointerInfo.position.y < Clay__layoutDimensions.height - 300
|
||||||
|
? (int32_t)((Clay__pointerInfo.position.y - scrollYOffset) / (float)CLAY__DEBUGVIEW_ROW_HEIGHT) - 1
|
||||||
|
: -1;
|
||||||
if (Clay__pointerInfo.position.x < Clay__layoutDimensions.width - (float)Clay__debugViewWidth) {
|
if (Clay__pointerInfo.position.x < Clay__layoutDimensions.width - (float)Clay__debugViewWidth) {
|
||||||
highlightedRow = -1;
|
highlightedRow = -1;
|
||||||
}
|
}
|
||||||
@ -3474,7 +3478,7 @@ void Clay__RenderDebugView() {
|
|||||||
|
|
||||||
CLAY_WASM_EXPORT("Clay_MinMemorySize")
|
CLAY_WASM_EXPORT("Clay_MinMemorySize")
|
||||||
uint32_t Clay_MinMemorySize() {
|
uint32_t Clay_MinMemorySize() {
|
||||||
Clay_Arena fakeArena = CLAY__INIT(Clay_Arena) { .capacity = INT64_MAX };
|
Clay_Arena fakeArena = CLAY__INIT(Clay_Arena) { .capacity = SIZE_MAX };
|
||||||
Clay__InitializePersistentMemory(&fakeArena);
|
Clay__InitializePersistentMemory(&fakeArena);
|
||||||
Clay__InitializeEphemeralMemory(&fakeArena);
|
Clay__InitializeEphemeralMemory(&fakeArena);
|
||||||
return fakeArena.nextAllocation;
|
return fakeArena.nextAllocation;
|
||||||
@ -3493,9 +3497,6 @@ Clay_Arena Clay_CreateArenaWithCapacityAndMemory(uint32_t capacity, void *offset
|
|||||||
void Clay_SetMeasureTextFunction(Clay_Dimensions (*measureTextFunction)(Clay_String *text, Clay_TextElementConfig *config)) {
|
void Clay_SetMeasureTextFunction(Clay_Dimensions (*measureTextFunction)(Clay_String *text, Clay_TextElementConfig *config)) {
|
||||||
Clay__MeasureText = measureTextFunction;
|
Clay__MeasureText = measureTextFunction;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef CLAY_WASM
|
|
||||||
void Clay_SetQueryScrollOffsetFunction(Clay_Vector2 (*queryScrollOffsetFunction)(uint32_t elementId)) {
|
void Clay_SetQueryScrollOffsetFunction(Clay_Vector2 (*queryScrollOffsetFunction)(uint32_t elementId)) {
|
||||||
Clay__QueryScrollOffset = queryScrollOffsetFunction;
|
Clay__QueryScrollOffset = queryScrollOffsetFunction;
|
||||||
}
|
}
|
||||||
@ -3830,9 +3831,9 @@ void Clay_SetMaxElementCount(uint32_t maxElementCount) {
|
|||||||
Clay__maxElementCount = maxElementCount;
|
Clay__maxElementCount = maxElementCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
CLAY_WASM_EXPORT("Clay_SetMeasureTextCacheSize")
|
CLAY_WASM_EXPORT("Clay_SetMaxMeasureTextCacheWordCount")
|
||||||
void Clay_SetMeasureTextCacheSize(uint32_t measureTextCacheSize) {
|
void Clay_SetMaxMeasureTextCacheWordCount(uint32_t maxMeasureTextCacheWordCount) {
|
||||||
Clay__measureTextCacheSize = measureTextCacheSize;
|
Clay__maxMeasureTextCacheWordCount = maxMeasureTextCacheWordCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //CLAY_IMPLEMENTATION
|
#endif //CLAY_IMPLEMENTATION
|
||||||
|
@ -109,6 +109,10 @@ void Layout() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HandleClayErrors(Clay_ErrorData errorData) {
|
||||||
|
printf("%s", errorData.errorText.chars);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
// First we set up our cairo surface.
|
// First we set up our cairo surface.
|
||||||
// In this example we will use the PDF backend,
|
// In this example we will use the PDF backend,
|
||||||
@ -131,11 +135,11 @@ int main(void) {
|
|||||||
Clay_Cairo_Initialize(cr);
|
Clay_Cairo_Initialize(cr);
|
||||||
|
|
||||||
uint64_t totalMemorySize = Clay_MinMemorySize();
|
uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||||
Clay_Arena clayMemory = (Clay_Arena) { .label = CLAY_STRING("Clay Memory Arena"), .memory = malloc(totalMemorySize), .capacity = totalMemorySize };
|
Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
|
||||||
Clay_SetMeasureTextFunction(Clay_Cairo_MeasureText);
|
Clay_SetMeasureTextFunction(Clay_Cairo_MeasureText);
|
||||||
|
|
||||||
// We initialize Clay with the same size
|
// We initialize Clay with the same size
|
||||||
Clay_Initialize(clayMemory, (Clay_Dimensions) { width, height });
|
Clay_Initialize(clayMemory, (Clay_Dimensions) { width, height }, (Clay_ErrorHandler) { HandleClayErrors });
|
||||||
|
|
||||||
Clay_BeginLayout();
|
Clay_BeginLayout();
|
||||||
|
|
||||||
|
@ -311,23 +311,26 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
const importObject = {
|
const importObject = {
|
||||||
clay: { measureTextFunction: (addressOfDimensions, textToMeasure, addressOfConfig) => {
|
clay: {
|
||||||
let stringLength = memoryDataView.getUint32(textToMeasure, true);
|
|
||||||
let pointerToString = memoryDataView.getUint32(textToMeasure + 4, true);
|
measureTextFunction: (addressOfDimensions, textToMeasure, addressOfConfig) => {
|
||||||
let textConfig = readStructAtAddress(addressOfConfig, textConfigDefinition);
|
let stringLength = memoryDataView.getUint32(textToMeasure, true);
|
||||||
let textDecoder = new TextDecoder("utf-8");
|
let pointerToString = memoryDataView.getUint32(textToMeasure + 4, true);
|
||||||
let text = textDecoder.decode(memoryDataView.buffer.slice(pointerToString, pointerToString + stringLength));
|
let textConfig = readStructAtAddress(addressOfConfig, textConfigDefinition);
|
||||||
let sourceDimensions = getTextDimensions(text, `${Math.round(textConfig.fontSize.value * GLOBAL_FONT_SCALING_FACTOR)}px ${fontsById[textConfig.fontId.value]}`);
|
let textDecoder = new TextDecoder("utf-8");
|
||||||
memoryDataView.setFloat32(addressOfDimensions, sourceDimensions.width, true);
|
let text = textDecoder.decode(memoryDataView.buffer.slice(pointerToString, pointerToString + stringLength));
|
||||||
memoryDataView.setFloat32(addressOfDimensions + 4, sourceDimensions.height, true);
|
let sourceDimensions = getTextDimensions(text, `${Math.round(textConfig.fontSize.value * GLOBAL_FONT_SCALING_FACTOR)}px ${fontsById[textConfig.fontId.value]}`);
|
||||||
},
|
memoryDataView.setFloat32(addressOfDimensions, sourceDimensions.width, true);
|
||||||
queryScrollOffsetFunction: (addressOfOffset, elementId) => {
|
memoryDataView.setFloat32(addressOfDimensions + 4, sourceDimensions.height, true);
|
||||||
let container = document.getElementById(elementId.toString());
|
},
|
||||||
if (container) {
|
queryScrollOffsetFunction: (addressOfOffset, elementId) => {
|
||||||
memoryDataView.setFloat32(addressOfOffset, -container.scrollLeft, true);
|
let container = document.getElementById(elementId.toString());
|
||||||
memoryDataView.setFloat32(addressOfOffset + 4, -container.scrollTop, true);
|
if (container) {
|
||||||
|
memoryDataView.setFloat32(addressOfOffset, -container.scrollLeft, true);
|
||||||
|
memoryDataView.setFloat32(addressOfOffset + 4, -container.scrollTop, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}},
|
},
|
||||||
};
|
};
|
||||||
const { instance } = await WebAssembly.instantiateStreaming(
|
const { instance } = await WebAssembly.instantiateStreaming(
|
||||||
fetch("/clay/index.wasm"), importObject
|
fetch("/clay/index.wasm"), importObject
|
||||||
|
@ -4,10 +4,14 @@
|
|||||||
|
|
||||||
Clay_LayoutConfig layoutElement = Clay_LayoutConfig { .padding = {5} };
|
Clay_LayoutConfig layoutElement = Clay_LayoutConfig { .padding = {5} };
|
||||||
|
|
||||||
|
void HandleClayErrors(Clay_ErrorData errorData) {
|
||||||
|
printf("%s", errorData.errorText.chars);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
uint64_t totalMemorySize = Clay_MinMemorySize();
|
uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||||
Clay_Arena clayMemory = Clay_Arena { .label = CLAY_STRING("Clay Memory Arena"), .capacity = totalMemorySize, .memory = (char *)malloc(totalMemorySize) };
|
Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, (char *)malloc(totalMemorySize));
|
||||||
Clay_Initialize(clayMemory, Clay_Dimensions {1024,768});
|
Clay_Initialize(clayMemory, Clay_Dimensions {1024,768}, Clay_ErrorHandler { HandleClayErrors });
|
||||||
Clay_BeginLayout();
|
Clay_BeginLayout();
|
||||||
CLAY(CLAY_RECTANGLE({ .color = {255,255,255,0} }), CLAY_LAYOUT(layoutElement)) {}
|
CLAY(CLAY_RECTANGLE({ .color = {255,255,255,0} }), CLAY_LAYOUT(layoutElement)) {}
|
||||||
Clay_EndLayout();
|
Clay_EndLayout();
|
||||||
|
@ -211,13 +211,13 @@ void HandleClayErrors(Clay_ErrorData errorData) {
|
|||||||
Clay_SetMaxElementCount(Clay__maxElementCount * 2);
|
Clay_SetMaxElementCount(Clay__maxElementCount * 2);
|
||||||
} else if (errorData.errorType == CLAY_ERROR_TYPE_TEXT_MEASUREMENT_CAPACITY_EXCEEDED) {
|
} else if (errorData.errorType == CLAY_ERROR_TYPE_TEXT_MEASUREMENT_CAPACITY_EXCEEDED) {
|
||||||
reinitializeClay = true;
|
reinitializeClay = true;
|
||||||
Clay_SetMeasureTextCacheSize(Clay__measureTextCacheSize * 2);
|
Clay_SetMaxMeasureTextCacheWordCount(Clay__maxMeasureTextCacheWordCount * 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
uint64_t totalMemorySize = Clay_MinMemorySize();
|
uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||||
Clay_Arena clayMemory = (Clay_Arena) { .label = CLAY_STRING("Clay Memory Arena"), .memory = malloc(totalMemorySize), .capacity = totalMemorySize };
|
Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
|
||||||
Clay_SetMeasureTextFunction(Raylib_MeasureText);
|
Clay_SetMeasureTextFunction(Raylib_MeasureText);
|
||||||
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)GetScreenWidth(), (float)GetScreenHeight() }, (Clay_ErrorHandler) { HandleClayErrors });
|
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)GetScreenWidth(), (float)GetScreenHeight() }, (Clay_ErrorHandler) { HandleClayErrors });
|
||||||
Clay_Raylib_Initialize(1024, 768, "Clay - Raylib Renderer Example", FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI | FLAG_MSAA_4X_HINT);
|
Clay_Raylib_Initialize(1024, 768, "Clay - Raylib Renderer Example", FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI | FLAG_MSAA_4X_HINT);
|
||||||
@ -242,7 +242,7 @@ int main(void) {
|
|||||||
if (reinitializeClay) {
|
if (reinitializeClay) {
|
||||||
Clay_SetMaxElementCount(8192);
|
Clay_SetMaxElementCount(8192);
|
||||||
totalMemorySize = Clay_MinMemorySize();
|
totalMemorySize = Clay_MinMemorySize();
|
||||||
clayMemory = (Clay_Arena) { .label = CLAY_STRING("Clay Memory Arena"), .memory = malloc(totalMemorySize), .capacity = totalMemorySize };
|
clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
|
||||||
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)GetScreenWidth(), (float)GetScreenHeight() }, (Clay_ErrorHandler) { HandleClayErrors });
|
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)GetScreenWidth(), (float)GetScreenHeight() }, (Clay_ErrorHandler) { HandleClayErrors });
|
||||||
reinitializeClay = false;
|
reinitializeClay = false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user