Skip to content

UI content transform#9341

Open
ickshonpe wants to merge 5 commits intobevyengine:mainfrom
ickshonpe:ui-content-transform
Open

UI content transform#9341
ickshonpe wants to merge 5 commits intobevyengine:mainfrom
ickshonpe:ui-content-transform

Conversation

@ickshonpe
Copy link
Contributor

@ickshonpe ickshonpe commented Aug 2, 2023

Objective

Allow limited rotation and flipping of a UI node's content without modifying its Transform component or breaking the UI layout and clipping.

Fixes #8998

Solution

Generalization of #6688.

UI nodes have a UiContentTransform component, with identity, flip_x, flip_y, rotate_left and rotate_right methods which you can compose to get whatever orientation you want.

text-content-transform-example content-transform-ui-example pbr_content_transform

Notes:

  • The API is a bit mathy and might be quite counterintuitive for some users, particularly when working with text.

  • I really hate the naming scheme using cardinal directions for the variants of UiContentTransform. There is a long discussion about this at UI Image Rotation #6688. All the other ideas were terrible too.

  • Probably still got a few bugs but it all seems to work mostly. Needs some refactoring and more doc comments.

  • In the original design widgets didn't need to be aware of the content transform and layout handled everything. But I realised it wasn't going to work without changes to Taffy (probably coming in Taffy 0.4).

  • You can still use the regular Transform component to rotate and scale UI elements as before.

  • This is a relatively large PR but it doesn't make too many changes, most of the code is examples and helper methods.


Changelog

  • Removed the flip_x and flip_y from the UiImage and UiTextureAtlasImage components.
  • Added the UiContentTransform enum component.
  • Added ui_image_symmetries and text_transformed examples.
  • Implemented From<UiContentOrientation> for Mat4
  • Added a content_transform: UiContentTransform field to all UI node bundles.

Migration Guide

The flip_x and flip_y fields have been removed from the UiImage and UiTextureAtlasImage components. The flip_x() and flip_y() methods of the UiContentTransform component now control image flipping.

…ipping of a UI node's content such as images and text.

`bevy_ui::ui_node`
* Added `UiContentOrientation` enum component.
* Implemented `From<UiContentOrientation>` for `Mat4`

`bevy_ui::layout`
* If a node has a `ContentSize` and its `UiContentOrientation` is set to rotate the node sideways, the width and height of the MeasureFunc's inputs and outputs are flipped.

`bevy_ui::widget::text::text_system`
* The height and width of the text bounds is switched for text nodes orientated sideways.

`bevy_ui::node_bundle`
* Added field `content_orientation: UiContentOrientation` to all UI node bundles.

examples:
* Changed PBR example to use `UiContentOrientation` instead of manipulation of the transform.
* Added the example `ui_image_symmetries`.
…ent, instead of `UiSurface::update_measure`.

Fixed bugs in extraction transform calculations causing incorrect positioning and sizing.

Added `text_transformed` example.

Clean up formatting and applied CI suggested fixes.
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-UI Graphical user interfaces, styles, layouts, and widgets labels Aug 3, 2023
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this to the linked PR.

@alice-i-cecile
Copy link
Member

I wish we could call this Orientation or something, but it's a) currently UI specific and b) also includes flipping. Hmm.

@alice-i-cecile alice-i-cecile added the M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Sep 15, 2023
@mogambro
Copy link
Contributor

What about UiOrientation for now then? And if we get to a point where it can be more generalized, we rename it.

UiOrientation may not sound better than UiContentTransform but I personally prefer Orientation to Transform because Orientation has more meaning and is less abstract (After all, in the end about everything we do in CS is transforming some input to some output ;))

Maybe UiContentOrientation or ContentOrientation?

@nicoburns
Copy link
Contributor

This seems like a good direction.

@ickshonpe A few questions:

  • Does this work with container nodes (to transform the entire sub-tree)?
  • How does this interact with layout (CSS transforms for example happen after layout - so layout is entirely unaffected)?
  • What is the difference between this and a regular Transform?

@ickshonpe
Copy link
Contributor Author

ickshonpe commented Dec 28, 2023

This seems like a good direction.

@ickshonpe A few questions:

  • Does this work with container nodes (to transform the entire sub-tree)?

No that changes are only applied to single nodes. I'm not keen on allowing transformations of sub-trees. I'd prefer to simulate it by spawning a second layout tree with a transformation that is applied globally to all nodes in the tree.

  • How does this interact with layout (CSS transforms for example happen after layout - so layout is entirely unaffected)?

The layout is unaffected, only the content within the node is transformed respecting the node's boundaries.

  • What is the difference between this and a regular Transform?

The regular Transform doesn't respect the layout. This adjusts the size of the content after the transformation so it still fits the node.

The regular Transform allows arbitrary scaling and rotation of content, but it's not very useful usually as it breaks the layout and clipping results in a distorted mess.

@alice-i-cecile alice-i-cecile added S-Adopt-Me The original PR author has no intent to complete this work. Pick me up! X-Contentious There are nontrivial implications that should be thought through labels May 28, 2024
@upsuper
Copy link

upsuper commented Oct 23, 2024

I saw that this PR is said to fix #8998 which is about vertical text support like CSS's writing-mode and text-orientation, but the screenshots included in the description don't really seem to demonstrate such support. Need of vertical text mainly originates from CJK scripts, in which the orientation of individual characters are usually unchanged, but they are laid in different direction as shown below.

Different direction of text

However, all the examples shown in the PR description is about rotating the whole text piece, which can't really be called vertical text support. If the only thing this PR does is to support such rotation, I would suggest either it should not mark that issue as fixed, or that issue should be updated to a narrower scope. But maybe I'm missing something?

@ickshonpe
Copy link
Contributor Author

However, all the examples shown in the PR description is about rotating the whole text piece, which can't really be called vertical text support. If the only thing this PR does is to support such rotation, I would suggest either it should not mark that issue as fixed, or that issue should be updated to a narrower scope. But maybe I'm missing something?

Yeah I should I have been clearer maybe, the vertical text support I'm talking about here is for tasks like labelling the vertical axis of a graph. This PR is mainly about changing the orientation of content and negotiating with Taffy to find the ideal size to fit the reorientated content into the UI node layout. It's meant to be content agnostic as far as possible.

The layout of glyphs into rows for languages that can be written vertically like Japanese is something separate and would be handled by bevy_text. @tjamaan has been working on improving bevy's bidi support I think, might have an opinon.

@upsuper
Copy link

upsuper commented Oct 25, 2024

In that case, would it be better removing the "Fixes #8998" from this PR description? Or if the intention of #8998 is actually just this, then removing the mention of writing mode and text orientation from that issue to avoid causing confusion?

@BenjaminBrienen
Copy link
Contributor

triage: still up for adoption with some nasty merge conflicts

@alice-i-cecile alice-i-cecile added X-Needs-SME This type of work requires an SME to approve it. and removed X-Contentious There are nontrivial implications that should be thought through labels May 19, 2025
@ickshonpe ickshonpe mentioned this pull request May 23, 2025
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-UI Graphical user interfaces, styles, layouts, and widgets C-Feature A new feature, making something new possible M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Adopt-Me The original PR author has no intent to complete this work. Pick me up! X-Needs-SME This type of work requires an SME to approve it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rotation-aware text layout

6 participants