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:

  1. Define a SystemParam (we will refer to it as MyContentSize)
  2. Implement ComputeContentParam for MyContentSize
  3. Implement ComputeContentSize for MyContentSize. ComputeContentSize::compute_content is ran for each leaf node Entity with the provided components.
    • The sizes infered by the layouting algorithm is passed as the set_size parameter.
    • 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.
  4. Register MyContentSize as a content sized element computation using app.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.