Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Can't set userData on text elements #271

Open
mikejsavage opened this issue Feb 17, 2025 · 2 comments
Open

[Core] Can't set userData on text elements #271

mikejsavage opened this issue Feb 17, 2025 · 2 comments

Comments

@mikejsavage
Copy link
Contributor

mikejsavage commented Feb 17, 2025

edit: looking at the macro API, calling ConfigureOpenElement on a text element is just not valid code so there are No Bugs Here, but it would be nice to have a replacement for CLAY_EXTEND_CONFIG_TEXT.


Hi, it seems like clay doesn't forward Clay_ElementDeclaration::userData through to TEXT render commands:

#include <stdio.h>
#define CLAY_IMPLEMENTATION
#include "clay.h"

int main( int argc, char ** argv ) {
    static char mem[ 10000000 ];
    Clay_Arena arena = Clay_CreateArenaWithCapacityAndMemory( sizeof( mem ), mem );
    Clay_Initialize( arena, { }, { } );

    auto measure_text = []( Clay_StringSlice text, Clay_TextElementConfig * config, void * user_data ) -> Clay_Dimensions {
        return { 5, 5 };
    };
    Clay_SetMeasureTextFunction( measure_text, NULL );

    Clay_SetLayoutDimensions( Clay_Dimensions { 100, 100 } );
    Clay_BeginLayout();

    Clay__OpenElement();
    Clay__ConfigureOpenElement( Clay_ElementDeclaration {
        .id = 1,
        .userData = ( void * ) 1,
    } );
    Clay__OpenTextElement( CLAY_STRING( "Hello" ), Clay__StoreTextElementConfig( Clay_TextElementConfig { } ) );
    Clay__ConfigureOpenElement( Clay_ElementDeclaration {
        .id = 2,
        .userData = ( void * ) 2,
    } );
    Clay__CloseElement();

    Clay_RenderCommandArray layout = Clay_EndLayout();
    for( int32_t i = 0; i < layout.length; i++ ) {
        const Clay_RenderCommand & command = layout.internalArray[ i ];
        const Clay_BoundingBox & bounds = command.boundingBox;
        printf( "%d %d,%d %dx%d %p\n", command.commandType, int( bounds.x ), int( bounds.y ), int( bounds.width ), int( bounds.height ), command.userData );
    }

    return 0;
}

I would expect this to produce a TEXT render command with the userData pointer set to 1 or 2, but instead clay emits a NONE render command (which I suppose may also be a bug, although it goes away if you don't configure the text element) with userData = 2 and a TEXT command with userData = NULL.

@mikejsavage mikejsavage changed the title [Core] userData doesn't get forwarded to text elements [Core] Can't set userData on text elements Feb 17, 2025
@nicbarker
Copy link
Owner

Thanks for this one, looking into it now!

@nicbarker
Copy link
Owner

Ah, I see the issue here. This wasn't obvious to me because I use the macros - but "ConfigureOpenElement" only applies to non text elements opened with Clay_OpenElement. OpenTextElement is a very poor name because it implies the text element remains open for configuration etc, when in reality it's an atomic element - it should be something like CreateTextElement or DeclareTextElement.

I suspect we will need to add a seperate userData pointer to the text element config, so you can pass it as part of Clay__OpenTextElement.

I will look into the render command NONE being emitted, this should never happen so is a bug in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants