nicbarker / clay

High performance UI layout library in C.
https://nicbarker.com/clay
zlib License
12.66k stars 444 forks source link

[feature request] Function to get bounding box of element? #265

Closed sammonius closed 1 month ago

sammonius commented 1 month ago

It would be a neat feature to have. I found a way to do it but it involves using Clay_GetHashMapItem, which is an internal function, so it wouldn't really work unless it was inside the library.

Clay_BoundingBox Clay_GetElementBoundingBox(Clay_ElementId elmId) { 
    Clay_LayoutElementHashMapItem* item = Clay_GetHashMapItem(elmId.id);
    return item ? item->boundingBox : (Clay_BoundingBox){0};
}

// (or it could just take the id member of an ElementId, which would look like (CLAY_ID("myElm").id))
// Clay_BoundingBox Clay_GetElementBoundingBox(int id) { 
//     Clay_LayoutElementHashMapItem* item = Clay_GetHashMapItem(id);
//     return item ? item->boundingBox : (Clay_BoundingBox){0};
// }

(I don't know anything about code styling or PR requests, so I'm just leaving it in an issue in case it's wrong)

The use case I have for this are slots in a node graph, that I need to connect with a line through a custom render command. The slots are layed out with Clay, but the line connecting them needs their positions on the screen in order to be drawn correctly, so I would need the bounding boxes of both slots to draw the line. It's intended to look something like this image I found:

Image

nicbarker commented 1 month ago

My apologies, we actually already have the Clay_GetElementData function for this exact purpose 😅 It had been left out of the README, I've added it here: https://github.com/nicbarker/clay/commit/256fc325498c542f577f416c97d49e56d1550be2

sammonius commented 1 month ago

Oh, ok, thank you!

PiggybankStudios commented 1 month ago

For what it's worth, I ran into this today and thought it was not in the API, so I made a change to my version of clay.h to add this as a temporary hack. Was planning on making a pull request for it soon, but I guess there's no need.

I wonder if maybe the name could be better? Or maybe it just needs an prominent example written for it?

I was a bit surprised when I realized that the example UI you build in the introduction video doesn't actually do anything besides check if the mouse is over an element and check if the left mouse button is pressed. It seems like almost any widget besides a button is going to need to know location data in order to process it's input appropriately. In a sense this is the primary output of Clay, since it is a layout engine.

Not sure if there is really a problem that needs a solution, but just casting my 2¢ into the mix since I ran into this today.