mirror of
https://github.com/nicbarker/clay.git
synced 2025-04-23 14:48:06 +00:00
Compare commits
4 Commits
38f36bb22d
...
0d6a28a81c
Author | SHA1 | Date | |
---|---|---|---|
|
0d6a28a81c | ||
|
ad49977f1b | ||
|
61490e4557 | ||
|
d7e072fe25 |
@ -367,6 +367,8 @@ Context :: struct {} // opaque structure, only use as a pointer
|
|||||||
|
|
||||||
@(link_prefix = "Clay_", default_calling_convention = "c")
|
@(link_prefix = "Clay_", default_calling_convention = "c")
|
||||||
foreign Clay {
|
foreign Clay {
|
||||||
|
_OpenElement :: proc() ---
|
||||||
|
_CloseElement :: proc() ---
|
||||||
MinMemorySize :: proc() -> u32 ---
|
MinMemorySize :: proc() -> u32 ---
|
||||||
CreateArenaWithCapacityAndMemory :: proc(capacity: c.size_t, offset: [^]u8) -> Arena ---
|
CreateArenaWithCapacityAndMemory :: proc(capacity: c.size_t, offset: [^]u8) -> Arena ---
|
||||||
SetPointerState :: proc(position: Vector2, pointerDown: bool) ---
|
SetPointerState :: proc(position: Vector2, pointerDown: bool) ---
|
||||||
@ -399,9 +401,7 @@ foreign Clay {
|
|||||||
|
|
||||||
@(link_prefix = "Clay_", default_calling_convention = "c", private)
|
@(link_prefix = "Clay_", default_calling_convention = "c", private)
|
||||||
foreign Clay {
|
foreign Clay {
|
||||||
_OpenElement :: proc() ---
|
|
||||||
_ConfigureOpenElement :: proc(config: ElementDeclaration) ---
|
_ConfigureOpenElement :: proc(config: ElementDeclaration) ---
|
||||||
_CloseElement :: proc() ---
|
|
||||||
_HashString :: proc(key: String, offset: u32, seed: u32) -> ElementId ---
|
_HashString :: proc(key: String, offset: u32, seed: u32) -> ElementId ---
|
||||||
_OpenTextElement :: proc(text: String, textConfig: ^TextElementConfig) ---
|
_OpenTextElement :: proc(text: String, textConfig: ^TextElementConfig) ---
|
||||||
_StoreTextElementConfig :: proc(config: TextElementConfig) -> ^TextElementConfig ---
|
_StoreTextElementConfig :: proc(config: TextElementConfig) -> ^TextElementConfig ---
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
30
clay.h
30
clay.h
@ -1156,6 +1156,7 @@ CLAY__ARRAY_DEFINE(Clay__MeasuredWord, Clay__MeasuredWordArray)
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
Clay_Dimensions unwrappedDimensions;
|
Clay_Dimensions unwrappedDimensions;
|
||||||
int32_t measuredWordsStartIndex;
|
int32_t measuredWordsStartIndex;
|
||||||
|
float minWidth;
|
||||||
bool containsNewlines;
|
bool containsNewlines;
|
||||||
// Hash map data
|
// Hash map data
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
@ -1504,6 +1505,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_Dimensions dimensions = Clay__MeasureText(CLAY__INIT(Clay_StringSlice) { .length = length, .chars = &text->chars[start], .baseChars = text->chars }, config, context->measureTextUserData);
|
Clay_Dimensions dimensions = Clay__MeasureText(CLAY__INIT(Clay_StringSlice) { .length = length, .chars = &text->chars[start], .baseChars = text->chars }, config, context->measureTextUserData);
|
||||||
|
measured->minWidth = CLAY__MAX(dimensions.width, measured->minWidth);
|
||||||
measuredHeight = CLAY__MAX(measuredHeight, dimensions.height);
|
measuredHeight = CLAY__MAX(measuredHeight, dimensions.height);
|
||||||
if (current == ' ') {
|
if (current == ' ') {
|
||||||
dimensions.width += spaceWidth;
|
dimensions.width += spaceWidth;
|
||||||
@ -1529,6 +1531,7 @@ Clay__MeasureTextCacheItem *Clay__MeasureTextCached(Clay_String *text, Clay_Text
|
|||||||
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);
|
||||||
|
measured->minWidth = CLAY__MAX(dimensions.width, measured->minWidth);
|
||||||
}
|
}
|
||||||
measuredWidth = CLAY__MAX(lineWidth, measuredWidth);
|
measuredWidth = CLAY__MAX(lineWidth, measuredWidth);
|
||||||
|
|
||||||
@ -1663,21 +1666,25 @@ void Clay__CloseElement(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float leftRightPadding = (float)(layoutConfig->padding.left + layoutConfig->padding.right);
|
||||||
|
float topBottomPadding = (float)(layoutConfig->padding.top + layoutConfig->padding.bottom);
|
||||||
|
|
||||||
// Attach children to the current open element
|
// Attach children to the current open element
|
||||||
openLayoutElement->childrenOrTextContent.children.elements = &context->layoutElementChildren.internalArray[context->layoutElementChildren.length];
|
openLayoutElement->childrenOrTextContent.children.elements = &context->layoutElementChildren.internalArray[context->layoutElementChildren.length];
|
||||||
if (layoutConfig->layoutDirection == CLAY_LEFT_TO_RIGHT) {
|
if (layoutConfig->layoutDirection == CLAY_LEFT_TO_RIGHT) {
|
||||||
openLayoutElement->dimensions.width = (float)(layoutConfig->padding.left + layoutConfig->padding.right);
|
openLayoutElement->dimensions.width = leftRightPadding;
|
||||||
|
openLayoutElement->minDimensions.width = leftRightPadding;
|
||||||
for (int32_t i = 0; i < openLayoutElement->childrenOrTextContent.children.length; i++) {
|
for (int32_t i = 0; i < openLayoutElement->childrenOrTextContent.children.length; i++) {
|
||||||
int32_t childIndex = Clay__int32_tArray_GetValue(&context->layoutElementChildrenBuffer, (int)context->layoutElementChildrenBuffer.length - openLayoutElement->childrenOrTextContent.children.length + i);
|
int32_t childIndex = Clay__int32_tArray_GetValue(&context->layoutElementChildrenBuffer, (int)context->layoutElementChildrenBuffer.length - openLayoutElement->childrenOrTextContent.children.length + i);
|
||||||
Clay_LayoutElement *child = Clay_LayoutElementArray_Get(&context->layoutElements, childIndex);
|
Clay_LayoutElement *child = Clay_LayoutElementArray_Get(&context->layoutElements, childIndex);
|
||||||
openLayoutElement->dimensions.width += child->dimensions.width;
|
openLayoutElement->dimensions.width += child->dimensions.width;
|
||||||
openLayoutElement->dimensions.height = CLAY__MAX(openLayoutElement->dimensions.height, child->dimensions.height + layoutConfig->padding.top + layoutConfig->padding.bottom);
|
openLayoutElement->dimensions.height = CLAY__MAX(openLayoutElement->dimensions.height, child->dimensions.height + topBottomPadding);
|
||||||
// Minimum size of child elements doesn't matter to scroll containers as they can shrink and hide their contents
|
// Minimum size of child elements doesn't matter to scroll containers as they can shrink and hide their contents
|
||||||
if (!elementHasScrollHorizontal) {
|
if (!elementHasScrollHorizontal) {
|
||||||
openLayoutElement->minDimensions.width += child->minDimensions.width;
|
openLayoutElement->minDimensions.width += child->minDimensions.width;
|
||||||
}
|
}
|
||||||
if (!elementHasScrollVertical) {
|
if (!elementHasScrollVertical) {
|
||||||
openLayoutElement->minDimensions.height = CLAY__MAX(openLayoutElement->minDimensions.height, child->minDimensions.height + layoutConfig->padding.top + layoutConfig->padding.bottom);
|
openLayoutElement->minDimensions.height = CLAY__MAX(openLayoutElement->minDimensions.height, child->minDimensions.height + topBottomPadding);
|
||||||
}
|
}
|
||||||
Clay__int32_tArray_Add(&context->layoutElementChildren, childIndex);
|
Clay__int32_tArray_Add(&context->layoutElementChildren, childIndex);
|
||||||
}
|
}
|
||||||
@ -1686,18 +1693,19 @@ void Clay__CloseElement(void) {
|
|||||||
openLayoutElement->minDimensions.width += childGap;
|
openLayoutElement->minDimensions.width += childGap;
|
||||||
}
|
}
|
||||||
else if (layoutConfig->layoutDirection == CLAY_TOP_TO_BOTTOM) {
|
else if (layoutConfig->layoutDirection == CLAY_TOP_TO_BOTTOM) {
|
||||||
openLayoutElement->dimensions.height = (float)(layoutConfig->padding.top + layoutConfig->padding.bottom);
|
openLayoutElement->dimensions.height = topBottomPadding;
|
||||||
|
openLayoutElement->minDimensions.height = topBottomPadding;
|
||||||
for (int32_t i = 0; i < openLayoutElement->childrenOrTextContent.children.length; i++) {
|
for (int32_t i = 0; i < openLayoutElement->childrenOrTextContent.children.length; i++) {
|
||||||
int32_t childIndex = Clay__int32_tArray_GetValue(&context->layoutElementChildrenBuffer, (int)context->layoutElementChildrenBuffer.length - openLayoutElement->childrenOrTextContent.children.length + i);
|
int32_t childIndex = Clay__int32_tArray_GetValue(&context->layoutElementChildrenBuffer, (int)context->layoutElementChildrenBuffer.length - openLayoutElement->childrenOrTextContent.children.length + i);
|
||||||
Clay_LayoutElement *child = Clay_LayoutElementArray_Get(&context->layoutElements, childIndex);
|
Clay_LayoutElement *child = Clay_LayoutElementArray_Get(&context->layoutElements, childIndex);
|
||||||
openLayoutElement->dimensions.height += child->dimensions.height;
|
openLayoutElement->dimensions.height += child->dimensions.height;
|
||||||
openLayoutElement->dimensions.width = CLAY__MAX(openLayoutElement->dimensions.width, child->dimensions.width + layoutConfig->padding.left + layoutConfig->padding.right);
|
openLayoutElement->dimensions.width = CLAY__MAX(openLayoutElement->dimensions.width, child->dimensions.width + leftRightPadding);
|
||||||
// Minimum size of child elements doesn't matter to scroll containers as they can shrink and hide their contents
|
// Minimum size of child elements doesn't matter to scroll containers as they can shrink and hide their contents
|
||||||
if (!elementHasScrollVertical) {
|
if (!elementHasScrollVertical) {
|
||||||
openLayoutElement->minDimensions.height += child->minDimensions.height;
|
openLayoutElement->minDimensions.height += child->minDimensions.height;
|
||||||
}
|
}
|
||||||
if (!elementHasScrollHorizontal) {
|
if (!elementHasScrollHorizontal) {
|
||||||
openLayoutElement->minDimensions.width = CLAY__MAX(openLayoutElement->minDimensions.width, child->minDimensions.width + layoutConfig->padding.left + layoutConfig->padding.right);
|
openLayoutElement->minDimensions.width = CLAY__MAX(openLayoutElement->minDimensions.width, child->minDimensions.width + leftRightPadding);
|
||||||
}
|
}
|
||||||
Clay__int32_tArray_Add(&context->layoutElementChildren, childIndex);
|
Clay__int32_tArray_Add(&context->layoutElementChildren, childIndex);
|
||||||
}
|
}
|
||||||
@ -1849,7 +1857,7 @@ void Clay__OpenTextElement(Clay_String text, Clay_TextElementConfig *textConfig)
|
|||||||
Clay__StringArray_Add(&context->layoutElementIdStrings, elementId.stringId);
|
Clay__StringArray_Add(&context->layoutElementIdStrings, elementId.stringId);
|
||||||
Clay_Dimensions textDimensions = { .width = textMeasured->unwrappedDimensions.width, .height = textConfig->lineHeight > 0 ? (float)textConfig->lineHeight : textMeasured->unwrappedDimensions.height };
|
Clay_Dimensions textDimensions = { .width = textMeasured->unwrappedDimensions.width, .height = textConfig->lineHeight > 0 ? (float)textConfig->lineHeight : textMeasured->unwrappedDimensions.height };
|
||||||
textElement->dimensions = textDimensions;
|
textElement->dimensions = textDimensions;
|
||||||
textElement->minDimensions = CLAY__INIT(Clay_Dimensions) { .width = textMeasured->unwrappedDimensions.height, .height = textDimensions.height }; // TODO not sure this is the best way to decide min width for text
|
textElement->minDimensions = CLAY__INIT(Clay_Dimensions) { .width = textMeasured->minWidth, .height = textDimensions.height };
|
||||||
textElement->childrenOrTextContent.textElementData = Clay__TextElementDataArray_Add(&context->textElementData, CLAY__INIT(Clay__TextElementData) { .text = text, .preferredDimensions = textMeasured->unwrappedDimensions, .elementIndex = context->layoutElements.length - 1 });
|
textElement->childrenOrTextContent.textElementData = Clay__TextElementDataArray_Add(&context->textElementData, CLAY__INIT(Clay__TextElementData) { .text = text, .preferredDimensions = textMeasured->unwrappedDimensions, .elementIndex = context->layoutElements.length - 1 });
|
||||||
textElement->elementConfigs = CLAY__INIT(Clay__ElementConfigArraySlice) {
|
textElement->elementConfigs = CLAY__INIT(Clay__ElementConfigArraySlice) {
|
||||||
.length = 1,
|
.length = 1,
|
||||||
@ -2237,25 +2245,25 @@ void Clay__SizeContainersAlongAxis(bool xAxis) {
|
|||||||
for (int32_t childOffset = 0; childOffset < resizableContainerBuffer.length; childOffset++) {
|
for (int32_t childOffset = 0; childOffset < resizableContainerBuffer.length; childOffset++) {
|
||||||
Clay_LayoutElement *childElement = Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_GetValue(&resizableContainerBuffer, childOffset));
|
Clay_LayoutElement *childElement = Clay_LayoutElementArray_Get(&context->layoutElements, Clay__int32_tArray_GetValue(&resizableContainerBuffer, childOffset));
|
||||||
Clay_SizingAxis childSizing = xAxis ? childElement->layoutConfig->sizing.width : childElement->layoutConfig->sizing.height;
|
Clay_SizingAxis childSizing = xAxis ? childElement->layoutConfig->sizing.width : childElement->layoutConfig->sizing.height;
|
||||||
|
float minSize = xAxis ? childElement->minDimensions.width : childElement->minDimensions.height;
|
||||||
float *childSize = xAxis ? &childElement->dimensions.width : &childElement->dimensions.height;
|
float *childSize = xAxis ? &childElement->dimensions.width : &childElement->dimensions.height;
|
||||||
|
|
||||||
if (!xAxis && Clay__ElementHasConfig(childElement, CLAY__ELEMENT_CONFIG_TYPE_IMAGE)) {
|
if (!xAxis && Clay__ElementHasConfig(childElement, CLAY__ELEMENT_CONFIG_TYPE_IMAGE)) {
|
||||||
continue; // Currently we don't support resizing aspect ratio images on the Y axis because it would break the ratio
|
continue; // Currently we don't support resizing aspect ratio images on the Y axis because it would break the ratio
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're laying out the children of a scroll panel, grow containers expand to the height of the inner content, not the outer container
|
|
||||||
float maxSize = parentSize - parentPadding;
|
float maxSize = parentSize - parentPadding;
|
||||||
|
// If we're laying out the children of a scroll panel, grow containers expand to the size of the inner content, not the outer container
|
||||||
if (Clay__ElementHasConfig(parent, CLAY__ELEMENT_CONFIG_TYPE_SCROLL)) {
|
if (Clay__ElementHasConfig(parent, CLAY__ELEMENT_CONFIG_TYPE_SCROLL)) {
|
||||||
Clay_ScrollElementConfig *scrollElementConfig = Clay__FindElementConfigWithType(parent, CLAY__ELEMENT_CONFIG_TYPE_SCROLL).scrollElementConfig;
|
Clay_ScrollElementConfig *scrollElementConfig = Clay__FindElementConfigWithType(parent, CLAY__ELEMENT_CONFIG_TYPE_SCROLL).scrollElementConfig;
|
||||||
if (((xAxis && scrollElementConfig->horizontal) || (!xAxis && scrollElementConfig->vertical))) {
|
if (((xAxis && scrollElementConfig->horizontal) || (!xAxis && scrollElementConfig->vertical))) {
|
||||||
maxSize = CLAY__MAX(maxSize, innerContentSize);
|
maxSize = CLAY__MAX(maxSize, innerContentSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (childSizing.type == CLAY__SIZING_TYPE_FIT) {
|
if (childSizing.type == CLAY__SIZING_TYPE_GROW) {
|
||||||
*childSize = CLAY__MAX(childSizing.size.minMax.min, CLAY__MIN(*childSize, maxSize));
|
|
||||||
} else if (childSizing.type == CLAY__SIZING_TYPE_GROW) {
|
|
||||||
*childSize = CLAY__MIN(maxSize, childSizing.size.minMax.max);
|
*childSize = CLAY__MIN(maxSize, childSizing.size.minMax.max);
|
||||||
}
|
}
|
||||||
|
*childSize = CLAY__MAX(minSize, CLAY__MIN(*childSize, maxSize));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,16 +99,22 @@ static inline Clay_Dimensions Raylib_MeasureText(Clay_StringSlice text, Clay_Tex
|
|||||||
|
|
||||||
float scaleFactor = config->fontSize/(float)fontToUse.baseSize;
|
float scaleFactor = config->fontSize/(float)fontToUse.baseSize;
|
||||||
|
|
||||||
for (int i = 0; i < text.length; ++i)
|
int byte_index = 0;
|
||||||
{
|
while (byte_index < text.length) {
|
||||||
if (text.chars[i] == '\n') {
|
if (text.chars[byte_index] == '\n') {
|
||||||
maxTextWidth = fmax(maxTextWidth, lineTextWidth);
|
maxTextWidth = fmax(maxTextWidth, lineTextWidth);
|
||||||
lineTextWidth = 0;
|
lineTextWidth = 0;
|
||||||
|
byte_index++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int index = text.chars[i] - 32;
|
|
||||||
if (fontToUse.glyphs[index].advanceX != 0) lineTextWidth += fontToUse.glyphs[index].advanceX;
|
int codepoint_bytes = 0;
|
||||||
else lineTextWidth += (fontToUse.recs[index].width + fontToUse.glyphs[index].offsetX);
|
int codepoint = GetCodepoint(&text.chars[byte_index], &codepoint_bytes);
|
||||||
|
int glyph_index = GetGlyphIndex(fontToUse, codepoint);
|
||||||
|
byte_index += codepoint_bytes;
|
||||||
|
|
||||||
|
if (fontToUse.glyphs[glyph_index].advanceX != 0) lineTextWidth += fontToUse.glyphs[glyph_index].advanceX;
|
||||||
|
else lineTextWidth += (fontToUse.recs[glyph_index].width + fontToUse.glyphs[glyph_index].offsetX);
|
||||||
}
|
}
|
||||||
|
|
||||||
maxTextWidth = fmax(maxTextWidth, lineTextWidth);
|
maxTextWidth = fmax(maxTextWidth, lineTextWidth);
|
||||||
|
Loading…
Reference in New Issue
Block a user