Compare commits

...

4 Commits

Author SHA1 Message Date
Piggybank Studios
80995ff488
Merge fbf8251996 into 22e8cc318c 2025-03-07 15:42:17 -08:00
Nic Barker
22e8cc318c [Bindings/Odin] Update odin bindings for text config userdata pointer
Some checks are pending
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) Waiting to run
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Waiting to run
2025-03-08 11:08:04 +13:00
Michael Savage
8e6640f7a2
[Core] Add a userData pointer to Clay_TextElementConfig (#274) 2025-03-08 11:01:26 +13:00
Taylor Robbins (Piggybank Studios)
fbf8251996 Adds CLAY_TEXT_ALIGN_SHRINK option to Clay_TextAlignment which tells the TEXT command producing code it is allowed to pass a boundingBox that is horizontally smaller than the result of the MeasureText call for that string. The Renderer is then able to choose how it wants to fit a piece of text in a smaller space (often this involves removing some number of characters and replacing them with "..." characters) 2025-03-05 13:04:57 -08:00
11 changed files with 20 additions and 3 deletions

View File

@ -102,6 +102,7 @@ TextAlignment :: enum EnumBackingType {
} }
TextElementConfig :: struct { TextElementConfig :: struct {
userData: rawptr,
textColor: Color, textColor: Color,
fontId: u16, fontId: u16,
fontSize: u16, fontSize: u16,

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -489,7 +489,7 @@ errorHandler :: proc "c" (errorData: clay.ErrorData) {
} }
main :: proc() { main :: proc() {
minMemorySize: u32 = clay.MinMemorySize() minMemorySize: c.size_t = cast(c.size_t)clay.MinMemorySize()
memory := make([^]u8, minMemorySize) memory := make([^]u8, minMemorySize)
arena: clay.Arena = clay.CreateArenaWithCapacityAndMemory(minMemorySize, memory) arena: clay.Arena = clay.CreateArenaWithCapacityAndMemory(minMemorySize, memory)
clay.Initialize(arena, {cast(f32)raylib.GetScreenWidth(), cast(f32)raylib.GetScreenHeight()}, { handler = errorHandler }) clay.Initialize(arena, {cast(f32)raylib.GetScreenWidth(), cast(f32)raylib.GetScreenHeight()}, { handler = errorHandler })

18
clay.h
View File

@ -353,10 +353,14 @@ typedef CLAY_PACKED_ENUM {
CLAY_TEXT_ALIGN_CENTER, CLAY_TEXT_ALIGN_CENTER,
// Horizontally aligns wrapped lines of text to the right hand side of their bounding box. // Horizontally aligns wrapped lines of text to the right hand side of their bounding box.
CLAY_TEXT_ALIGN_RIGHT, CLAY_TEXT_ALIGN_RIGHT,
// The boundingBox passed to the TEXT render command may be smaller than the measured text size. The renderer must then decide how to shorten the text to make it fit
CLAY_TEXT_ALIGN_SHRINK,
} Clay_TextAlignment; } Clay_TextAlignment;
// Controls various functionality related to text elements. // Controls various functionality related to text elements.
typedef struct { typedef struct {
// A pointer that will be transparently passed through to the resulting render command.
void *userData;
// The RGBA color of the font to render, conventionally specified as 0-255. // The RGBA color of the font to render, conventionally specified as 0-255.
Clay_Color textColor; Clay_Color textColor;
// An integer transparently passed to Clay_MeasureText to identify the font to use. // An integer transparently passed to Clay_MeasureText to identify the font to use.
@ -2676,14 +2680,24 @@ void Clay__CalculateFinalLayout(void) {
continue; continue;
} }
float offset = (currentElementBoundingBox.width - wrappedLine->dimensions.width); float offset = (currentElementBoundingBox.width - wrappedLine->dimensions.width);
if (textElementConfig->textAlignment == CLAY_TEXT_ALIGN_LEFT) { if (textElementConfig->textAlignment == CLAY_TEXT_ALIGN_LEFT || textElementConfig->textAlignment == CLAY_TEXT_ALIGN_SHRINK) {
offset = 0; offset = 0;
} }
if (textElementConfig->textAlignment == CLAY_TEXT_ALIGN_CENTER) { if (textElementConfig->textAlignment == CLAY_TEXT_ALIGN_CENTER) {
offset /= 2; offset /= 2;
} }
Clay_BoundingBox textBoundingBox = {
currentElementBoundingBox.x + offset,
currentElementBoundingBox.y + yPosition,
wrappedLine->dimensions.width,
wrappedLine->dimensions.height
};
if (textElementConfig->textAlignment == CLAY_TEXT_ALIGN_SHRINK && boundingBox.width > currentElementBoundingBox.width) {
boundingBox.width = currentElementBoundingBox.width;
}
Clay__AddRenderCommand(CLAY__INIT(Clay_RenderCommand) { Clay__AddRenderCommand(CLAY__INIT(Clay_RenderCommand) {
.boundingBox = { currentElementBoundingBox.x + offset, currentElementBoundingBox.y + yPosition, wrappedLine->dimensions.width, wrappedLine->dimensions.height }, .boundingBox = { currentElementBoundingBox.x + offset, currentElementBoundingBox.y + yPosition, wrappedLine->dimensions.width, wrappedLine->dimensions.height },
.boundingBox = textBoundingBox,
.renderData = { .text = { .renderData = { .text = {
.stringContents = CLAY__INIT(Clay_StringSlice) { .length = wrappedLine->line.length, .chars = wrappedLine->line.chars, .baseChars = currentElement->childrenOrTextContent.textElementData->text.chars }, .stringContents = CLAY__INIT(Clay_StringSlice) { .length = wrappedLine->line.length, .chars = wrappedLine->line.chars, .baseChars = currentElement->childrenOrTextContent.textElementData->text.chars },
.textColor = textElementConfig->textColor, .textColor = textElementConfig->textColor,
@ -2692,7 +2706,7 @@ void Clay__CalculateFinalLayout(void) {
.letterSpacing = textElementConfig->letterSpacing, .letterSpacing = textElementConfig->letterSpacing,
.lineHeight = textElementConfig->lineHeight, .lineHeight = textElementConfig->lineHeight,
}}, }},
.userData = sharedConfig->userData, .userData = textElementConfig->userData,
.id = Clay__HashNumber(lineIndex, currentElement->id).id, .id = Clay__HashNumber(lineIndex, currentElement->id).id,
.zIndex = root->zIndex, .zIndex = root->zIndex,
.commandType = CLAY_RENDER_COMMAND_TYPE_TEXT, .commandType = CLAY_RENDER_COMMAND_TYPE_TEXT,

View File

@ -119,6 +119,7 @@
{name: 'bottomRight', type: 'float'}, {name: 'bottomRight', type: 'float'},
]}; ]};
let textConfigDefinition = { name: 'text', type: 'struct', members: [ let textConfigDefinition = { name: 'text', type: 'struct', members: [
{ name: 'userData', type: 'uint32_t' },
{ name: 'textColor', ...colorDefinition }, { name: 'textColor', ...colorDefinition },
{ name: 'fontId', type: 'uint16_t' }, { name: 'fontId', type: 'uint16_t' },
{ name: 'fontSize', type: 'uint16_t' }, { name: 'fontSize', type: 'uint16_t' },

View File

@ -119,6 +119,7 @@
{name: 'bottomRight', type: 'float'}, {name: 'bottomRight', type: 'float'},
]}; ]};
let textConfigDefinition = { name: 'text', type: 'struct', members: [ let textConfigDefinition = { name: 'text', type: 'struct', members: [
{ name: 'userData', type: 'uint32_t' },
{ name: 'textColor', ...colorDefinition }, { name: 'textColor', ...colorDefinition },
{ name: 'fontId', type: 'uint16_t' }, { name: 'fontId', type: 'uint16_t' },
{ name: 'fontSize', type: 'uint16_t' }, { name: 'fontSize', type: 'uint16_t' },