mirror of
https://github.com/nicbarker/clay.git
synced 2025-04-18 20:28:01 +00:00
SetMesureText and SetQueryScrollOffset takes userData
This commit is contained in:
parent
8e7e30dda6
commit
bfed9520fc
32
clay.h
32
clay.h
@ -1,4 +1,4 @@
|
|||||||
// VERSION: 0.11
|
// VERSION: 0.12
|
||||||
|
|
||||||
/*
|
/*
|
||||||
NOTE: In order to use this library you must define
|
NOTE: In order to use this library you must define
|
||||||
@ -527,8 +527,8 @@ bool Clay_Hovered(void);
|
|||||||
void Clay_OnHover(void (*onHoverFunction)(Clay_ElementId elementId, Clay_PointerData pointerData, intptr_t userData), intptr_t userData);
|
void Clay_OnHover(void (*onHoverFunction)(Clay_ElementId elementId, Clay_PointerData pointerData, intptr_t userData), intptr_t userData);
|
||||||
bool Clay_PointerOver(Clay_ElementId elementId);
|
bool Clay_PointerOver(Clay_ElementId elementId);
|
||||||
Clay_ScrollContainerData Clay_GetScrollContainerData(Clay_ElementId id);
|
Clay_ScrollContainerData Clay_GetScrollContainerData(Clay_ElementId id);
|
||||||
void Clay_SetMeasureTextFunction(Clay_Dimensions (*measureTextFunction)(Clay_String *text, Clay_TextElementConfig *config));
|
void Clay_SetMeasureTextFunction(Clay_Dimensions (*measureTextFunction)(Clay_String *text, Clay_TextElementConfig *config, uintptr_t userData), uintptr_t userData);
|
||||||
void Clay_SetQueryScrollOffsetFunction(Clay_Vector2 (*queryScrollOffsetFunction)(uint32_t elementId));
|
void Clay_SetQueryScrollOffsetFunction(Clay_Vector2 (*queryScrollOffsetFunction)(uint32_t elementId, uintptr_t userData), uintptr_t userData);
|
||||||
Clay_RenderCommand * Clay_RenderCommandArray_Get(Clay_RenderCommandArray* array, int32_t index);
|
Clay_RenderCommand * Clay_RenderCommandArray_Get(Clay_RenderCommandArray* array, int32_t index);
|
||||||
void Clay_SetDebugModeEnabled(bool enabled);
|
void Clay_SetDebugModeEnabled(bool enabled);
|
||||||
bool Clay_IsDebugModeEnabled(void);
|
bool Clay_IsDebugModeEnabled(void);
|
||||||
@ -1407,6 +1407,8 @@ struct Clay_Context {
|
|||||||
uint32_t debugSelectedElementId;
|
uint32_t debugSelectedElementId;
|
||||||
uint32_t generation;
|
uint32_t generation;
|
||||||
uintptr_t arenaResetOffset;
|
uintptr_t arenaResetOffset;
|
||||||
|
uintptr_t mesureTextUserData;
|
||||||
|
uintptr_t queryScrollOffsetUserData;
|
||||||
Clay_Arena internalArena;
|
Clay_Arena internalArena;
|
||||||
// Layout Elements / Render Commands
|
// Layout Elements / Render Commands
|
||||||
Clay_LayoutElementArray layoutElements;
|
Clay_LayoutElementArray layoutElements;
|
||||||
@ -1480,11 +1482,11 @@ Clay_String Clay__WriteStringToCharBuffer(Clay__CharArray *buffer, Clay_String s
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CLAY_WASM
|
#ifdef CLAY_WASM
|
||||||
__attribute__((import_module("clay"), import_name("measureTextFunction"))) Clay_Dimensions Clay__MeasureText(Clay_String *text, Clay_TextElementConfig *config);
|
__attribute__((import_module("clay"), import_name("measureTextFunction"))) Clay_Dimensions Clay__MeasureText(Clay_String *text, Clay_TextElementConfig *config, uintptr_t userData);
|
||||||
__attribute__((import_module("clay"), import_name("queryScrollOffsetFunction"))) Clay_Vector2 Clay__QueryScrollOffset(uint32_t elementId);
|
__attribute__((import_module("clay"), import_name("queryScrollOffsetFunction"))) Clay_Vector2 Clay__QueryScrollOffset(uint32_t elementId, uintptr_t userData);
|
||||||
#else
|
#else
|
||||||
Clay_Dimensions (*Clay__MeasureText)(Clay_String *text, Clay_TextElementConfig *config);
|
Clay_Dimensions (*Clay__MeasureText)(Clay_String *text, Clay_TextElementConfig *config, uintptr_t userData);
|
||||||
Clay_Vector2 (*Clay__QueryScrollOffset)(uint32_t elementId);
|
Clay_Vector2 (*Clay__QueryScrollOffset)(uint32_t elementId, uintptr_t userData);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Clay_LayoutElement* Clay__GetOpenLayoutElement(void) {
|
Clay_LayoutElement* Clay__GetOpenLayoutElement(void) {
|
||||||
@ -1699,7 +1701,7 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text
|
|||||||
float lineWidth = 0;
|
float lineWidth = 0;
|
||||||
float measuredWidth = 0;
|
float measuredWidth = 0;
|
||||||
float measuredHeight = 0;
|
float measuredHeight = 0;
|
||||||
float spaceWidth = Clay__MeasureText(&CLAY__SPACECHAR, config).width;
|
float spaceWidth = Clay__MeasureText(&CLAY__SPACECHAR, config, context->mesureTextUserData).width;
|
||||||
Clay__MeasuredWord tempWord = { .next = -1 };
|
Clay__MeasuredWord tempWord = { .next = -1 };
|
||||||
Clay__MeasuredWord *previousWord = &tempWord;
|
Clay__MeasuredWord *previousWord = &tempWord;
|
||||||
while (end < text->length) {
|
while (end < text->length) {
|
||||||
@ -1717,7 +1719,7 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text
|
|||||||
if (current == ' ' || current == '\n') {
|
if (current == ' ' || current == '\n') {
|
||||||
int32_t length = end - start;
|
int32_t length = end - start;
|
||||||
Clay_String word = { .length = length, .chars = &text->chars[start] };
|
Clay_String word = { .length = length, .chars = &text->chars[start] };
|
||||||
Clay_Dimensions dimensions = Clay__MeasureText(&word, config);
|
Clay_Dimensions dimensions = Clay__MeasureText(&word, config, context->mesureTextUserData);
|
||||||
measuredHeight = CLAY__MAX(measuredHeight, dimensions.height);
|
measuredHeight = CLAY__MAX(measuredHeight, dimensions.height);
|
||||||
if (current == ' ') {
|
if (current == ' ') {
|
||||||
dimensions.width += spaceWidth;
|
dimensions.width += spaceWidth;
|
||||||
@ -1740,7 +1742,7 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text
|
|||||||
}
|
}
|
||||||
if (end - start > 0) {
|
if (end - start > 0) {
|
||||||
Clay_String lastWord = { .length = end - start, .chars = &text->chars[start] };
|
Clay_String lastWord = { .length = end - start, .chars = &text->chars[start] };
|
||||||
Clay_Dimensions dimensions = Clay__MeasureText(&lastWord, config);
|
Clay_Dimensions dimensions = Clay__MeasureText(&lastWord, config, context->mesureTextUserData);
|
||||||
Clay__AddMeasuredWord(CLAY__INIT(Clay__MeasuredWord) { .startOffset = start, .length = end - start, .width = dimensions.width, .next = -1 }, previousWord);
|
Clay__AddMeasuredWord(CLAY__INIT(Clay__MeasuredWord) { .startOffset = start, .length = end - start, .width = dimensions.width, .next = -1 }, previousWord);
|
||||||
lineWidth += dimensions.width;
|
lineWidth += dimensions.width;
|
||||||
measuredHeight = CLAY__MAX(measuredHeight, dimensions.height);
|
measuredHeight = CLAY__MAX(measuredHeight, dimensions.height);
|
||||||
@ -1902,7 +1904,7 @@ void Clay__ElementPostConfiguration(void) {
|
|||||||
scrollOffset = Clay__ScrollContainerDataInternalArray_Add(&context->scrollContainerDatas, CLAY__INIT(Clay__ScrollContainerDataInternal){.layoutElement = openLayoutElement, .scrollOrigin = {-1,-1}, .elementId = openLayoutElement->id, .openThisFrame = true});
|
scrollOffset = Clay__ScrollContainerDataInternalArray_Add(&context->scrollContainerDatas, CLAY__INIT(Clay__ScrollContainerDataInternal){.layoutElement = openLayoutElement, .scrollOrigin = {-1,-1}, .elementId = openLayoutElement->id, .openThisFrame = true});
|
||||||
}
|
}
|
||||||
if (context->externalScrollHandlingEnabled) {
|
if (context->externalScrollHandlingEnabled) {
|
||||||
scrollOffset->scrollPosition = Clay__QueryScrollOffset(scrollOffset->elementId);
|
scrollOffset->scrollPosition = Clay__QueryScrollOffset(scrollOffset->elementId, context->queryScrollOffsetUserData);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -3678,11 +3680,15 @@ Clay_Arena Clay_CreateArenaWithCapacityAndMemory(uint32_t capacity, void *offset
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef CLAY_WASM
|
#ifndef CLAY_WASM
|
||||||
void Clay_SetMeasureTextFunction(Clay_Dimensions (*measureTextFunction)(Clay_String *text, Clay_TextElementConfig *config)) {
|
void Clay_SetMeasureTextFunction(Clay_Dimensions (*measureTextFunction)(Clay_String *text, Clay_TextElementConfig *config, uintptr_t userData), uintptr_t userData) {
|
||||||
|
Clay_Context* context = Clay_GetCurrentContext();
|
||||||
Clay__MeasureText = measureTextFunction;
|
Clay__MeasureText = measureTextFunction;
|
||||||
|
context->mesureTextUserData = userData;
|
||||||
}
|
}
|
||||||
void Clay_SetQueryScrollOffsetFunction(Clay_Vector2 (*queryScrollOffsetFunction)(uint32_t elementId)) {
|
void Clay_SetQueryScrollOffsetFunction(Clay_Vector2 (*queryScrollOffsetFunction)(uint32_t elementId, uintptr_t userData), uintptr_t userData) {
|
||||||
|
Clay_Context* context = Clay_GetCurrentContext();
|
||||||
Clay__QueryScrollOffset = queryScrollOffsetFunction;
|
Clay__QueryScrollOffset = queryScrollOffsetFunction;
|
||||||
|
context->queryScrollOffsetUserData = userData;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -299,7 +299,10 @@ int main(int argc, char *argv[]) {
|
|||||||
fprintf(stderr, "Error: could not load font: %s\n", TTF_GetError());
|
fprintf(stderr, "Error: could not load font: %s\n", TTF_GetError());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
SDL2_fonts[FONT_ID_BODY_16] = (SDL2_Font) {
|
|
||||||
|
SDL2_Font fonts[1] = {};
|
||||||
|
|
||||||
|
fonts[FONT_ID_BODY_16] = (SDL2_Font) {
|
||||||
.fontId = FONT_ID_BODY_16,
|
.fontId = FONT_ID_BODY_16,
|
||||||
.font = font,
|
.font = font,
|
||||||
};
|
};
|
||||||
@ -315,12 +318,13 @@ int main(int argc, char *argv[]) {
|
|||||||
uint64_t totalMemorySize = Clay_MinMemorySize();
|
uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||||
Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
|
Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
|
||||||
|
|
||||||
Clay_SetMeasureTextFunction(SDL2_MeasureText);
|
|
||||||
|
|
||||||
int windowWidth = 0;
|
int windowWidth = 0;
|
||||||
int windowHeight = 0;
|
int windowHeight = 0;
|
||||||
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
||||||
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)windowWidth, (float)windowHeight }, (Clay_ErrorHandler) { HandleClayErrors });
|
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)windowWidth, (float)windowHeight }, (Clay_ErrorHandler) { HandleClayErrors });
|
||||||
|
|
||||||
|
Clay_SetMeasureTextFunction(SDL2_MeasureText, (uintptr_t)&fonts);
|
||||||
|
|
||||||
Uint64 NOW = SDL_GetPerformanceCounter();
|
Uint64 NOW = SDL_GetPerformanceCounter();
|
||||||
Uint64 LAST = 0;
|
Uint64 LAST = 0;
|
||||||
double deltaTime = 0;
|
double deltaTime = 0;
|
||||||
@ -361,7 +365,7 @@ int main(int argc, char *argv[]) {
|
|||||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
Clay_SDL2_Render(renderer, renderCommands);
|
Clay_SDL2_Render(renderer, renderCommands, fonts);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,10 @@ typedef struct app_state {
|
|||||||
SDL_Renderer *renderer;
|
SDL_Renderer *renderer;
|
||||||
} AppState;
|
} AppState;
|
||||||
|
|
||||||
static inline Clay_Dimensions SDL_MeasureText(Clay_String *text, Clay_TextElementConfig *config)
|
static inline Clay_Dimensions SDL_MeasureText(Clay_String *text, Clay_TextElementConfig *config, uintptr_t userData)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
TTF_Font *font = gFonts[config->fontId];
|
TTF_Font *font = gFonts[config->fontId];
|
||||||
int width, height;
|
int width, height;
|
||||||
|
|
||||||
@ -114,7 +116,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
|||||||
|
|
||||||
int width, height;
|
int width, height;
|
||||||
SDL_GetWindowSize(state->window, &width, &height);
|
SDL_GetWindowSize(state->window, &width, &height);
|
||||||
Clay_SetMeasureTextFunction(SDL_MeasureText);
|
Clay_SetMeasureTextFunction(SDL_MeasureText, 0);
|
||||||
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float) width, (float) height }, (Clay_ErrorHandler) { HandleClayErrors });
|
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float) width, (float) height }, (Clay_ErrorHandler) { HandleClayErrors });
|
||||||
|
|
||||||
*appstate = state;
|
*appstate = state;
|
||||||
|
@ -84,7 +84,7 @@ int main(void) {
|
|||||||
.width = GetScreenWidth(),
|
.width = GetScreenWidth(),
|
||||||
.height = GetScreenHeight()
|
.height = GetScreenHeight()
|
||||||
}, (Clay_ErrorHandler) { HandleClayErrors }); // This final argument is new since the video was published
|
}, (Clay_ErrorHandler) { HandleClayErrors }); // This final argument is new since the video was published
|
||||||
Clay_SetMeasureTextFunction(Raylib_MeasureText);
|
Clay_SetMeasureTextFunction(Raylib_MeasureText, 0);
|
||||||
Raylib_fonts[FONT_ID_BODY_16] = (Raylib_Font) {
|
Raylib_fonts[FONT_ID_BODY_16] = (Raylib_Font) {
|
||||||
.font = LoadFontEx("resources/Roboto-Regular.ttf", 48, 0, 400),
|
.font = LoadFontEx("resources/Roboto-Regular.ttf", 48, 0, 400),
|
||||||
.fontId = FONT_ID_BODY_16
|
.fontId = FONT_ID_BODY_16
|
||||||
|
@ -247,7 +247,7 @@ int main(void) {
|
|||||||
.height = GetScreenHeight() / 2
|
.height = GetScreenHeight() / 2
|
||||||
}, (Clay_ErrorHandler) { HandleClayErrors }); // This final argument is new since the video was published
|
}, (Clay_ErrorHandler) { HandleClayErrors }); // This final argument is new since the video was published
|
||||||
|
|
||||||
Clay_SetMeasureTextFunction(Raylib_MeasureText);
|
Clay_SetMeasureTextFunction(Raylib_MeasureText, 0);
|
||||||
Raylib_fonts[FONT_ID_BODY_16] = (Raylib_Font) {
|
Raylib_fonts[FONT_ID_BODY_16] = (Raylib_Font) {
|
||||||
.font = LoadFontEx("resources/Roboto-Regular.ttf", 48, 0, 400),
|
.font = LoadFontEx("resources/Roboto-Regular.ttf", 48, 0, 400),
|
||||||
.fontId = FONT_ID_BODY_16
|
.fontId = FONT_ID_BODY_16
|
||||||
|
@ -218,7 +218,7 @@ void HandleClayErrors(Clay_ErrorData errorData) {
|
|||||||
int main(void) {
|
int main(void) {
|
||||||
uint64_t totalMemorySize = Clay_MinMemorySize();
|
uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||||
Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
|
Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
|
||||||
Clay_SetMeasureTextFunction(Raylib_MeasureText);
|
Clay_SetMeasureTextFunction(Raylib_MeasureText, 0);
|
||||||
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);
|
||||||
profilePicture = LoadTextureFromImage(LoadImage("resources/profile-picture.png"));
|
profilePicture = LoadTextureFromImage(LoadImage("resources/profile-picture.png"));
|
||||||
|
@ -12,11 +12,12 @@ typedef struct
|
|||||||
TTF_Font *font;
|
TTF_Font *font;
|
||||||
} SDL2_Font;
|
} SDL2_Font;
|
||||||
|
|
||||||
static SDL2_Font SDL2_fonts[1];
|
|
||||||
|
|
||||||
static Clay_Dimensions SDL2_MeasureText(Clay_String *text, Clay_TextElementConfig *config)
|
static Clay_Dimensions SDL2_MeasureText(Clay_String *text, Clay_TextElementConfig *config, uintptr_t userData)
|
||||||
{
|
{
|
||||||
TTF_Font *font = SDL2_fonts[config->fontId].font;
|
SDL2_Font *fonts = (SDL2_Font*)userData;
|
||||||
|
|
||||||
|
TTF_Font *font = fonts[config->fontId].font;
|
||||||
char *chars = (char *)calloc(text->length + 1, 1);
|
char *chars = (char *)calloc(text->length + 1, 1);
|
||||||
memcpy(chars, text->chars, text->length);
|
memcpy(chars, text->chars, text->length);
|
||||||
int width = 0;
|
int width = 0;
|
||||||
@ -34,7 +35,7 @@ static Clay_Dimensions SDL2_MeasureText(Clay_String *text, Clay_TextElementConfi
|
|||||||
|
|
||||||
SDL_Rect currentClippingRectangle;
|
SDL_Rect currentClippingRectangle;
|
||||||
|
|
||||||
static void Clay_SDL2_Render(SDL_Renderer *renderer, Clay_RenderCommandArray renderCommands)
|
static void Clay_SDL2_Render(SDL_Renderer *renderer, Clay_RenderCommandArray renderCommands, SDL2_Font *fonts)
|
||||||
{
|
{
|
||||||
for (uint32_t i = 0; i < renderCommands.length; i++)
|
for (uint32_t i = 0; i < renderCommands.length; i++)
|
||||||
{
|
{
|
||||||
@ -60,7 +61,7 @@ static void Clay_SDL2_Render(SDL_Renderer *renderer, Clay_RenderCommandArray ren
|
|||||||
Clay_String text = renderCommand->text;
|
Clay_String text = renderCommand->text;
|
||||||
char *cloned = (char *)calloc(text.length + 1, 1);
|
char *cloned = (char *)calloc(text.length + 1, 1);
|
||||||
memcpy(cloned, text.chars, text.length);
|
memcpy(cloned, text.chars, text.length);
|
||||||
TTF_Font* font = SDL2_fonts[config->fontId].font;
|
TTF_Font* font = fonts[config->fontId].font;
|
||||||
SDL_Surface *surface = TTF_RenderUTF8_Blended(font, cloned, (SDL_Color) {
|
SDL_Surface *surface = TTF_RenderUTF8_Blended(font, cloned, (SDL_Color) {
|
||||||
.r = (Uint8)config->textColor.r,
|
.r = (Uint8)config->textColor.r,
|
||||||
.g = (Uint8)config->textColor.g,
|
.g = (Uint8)config->textColor.g,
|
||||||
|
@ -89,7 +89,7 @@ Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int scre
|
|||||||
|
|
||||||
uint32_t measureCalls = 0;
|
uint32_t measureCalls = 0;
|
||||||
|
|
||||||
static inline Clay_Dimensions Raylib_MeasureText(Clay_String *text, Clay_TextElementConfig *config) {
|
static inline Clay_Dimensions Raylib_MeasureText(Clay_String *text, Clay_TextElementConfig *config, uintptr_t userData) {
|
||||||
measureCalls++;
|
measureCalls++;
|
||||||
// Measure string size for Font
|
// Measure string size for Font
|
||||||
Clay_Dimensions textSize = { 0 };
|
Clay_Dimensions textSize = { 0 };
|
||||||
|
Loading…
Reference in New Issue
Block a user