cuicui_layout Content sized nodes
Leaf nodes (nodes that do not contain other nodes) may be “content-sized”.
For example, in cuicui_layout_bevy_ui, a leaf node containing an image may
keep the same aspect ratio as the image.
How to define content-sized elements?
First off, if you are already using cuicui_layout_bevy_ui or
cuicui_layout_bevy_sprite, you don’t need to do anything, those plugins
already take care of elements that should depend on the size of their content.
If you need to implement content-sized elements for your own UI stuff, you will need to:
- Define a
SystemParam(we will refer to it asMyContentSize) - Implement
ComputeContentParamforMyContentSize- In
ComputeContentParam::Components, tell which components are used to tell content size. UseAnyOfif several. - In
ComputeContentParam::condition, tell when the size update system should run
- In
- Implement
ComputeContentSizeforMyContentSize.ComputeContentSize::compute_contentis ran for each leaf nodeEntitywith the provided components.- The sizes infered by the layouting algorithm is passed as the
set_sizeparameter. - The return value is the sizes as they should be, based on the passed
components - Note that the non-content-sized axis will always keep the pre-set size, regardless of the return value.
- The sizes infered by the layouting algorithm is passed as the
- Register
MyContentSizeas a content sized element computation usingapp.add_content_sized::<MyContentSize>().
And that’s it!
The two distinct traits are required due to a limitation in the rust type system. Trying to merge the two traits came close to unleashing Cthulhu into the world. Do not ask me to merge them, do not open an issue for merging them, this way lies madness.
Example
The best examples are the content_sized.rs modules in cuicui_layout_bevy_ui
and cuicui_layout_bevy_sprite.
Please take a look at them to get an idea of the kind of code you need to write.