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

0.11 Migration: What to use instead of CalculatedSize #701

Open
nicopap opened this issue Jul 10, 2023 · 2 comments
Open

0.11 Migration: What to use instead of CalculatedSize #701

nicopap opened this issue Jul 10, 2023 · 2 comments

Comments

@nicopap
Copy link
Contributor

nicopap commented Jul 10, 2023

In bevy 0.10, CalculatedSized used to have a size field representing the node's "calculated" size, ie: size before layout constraints (is my understanding)

This section of the migration guide describe that "CalculatedSize has been renamed to ContentSize".

However, there is no mention of what to use instead of size! ContentSize has a measure_func field I've no idea how to get back the value that previously was there!

It seems ContentSize was changed in bevyengine/bevy#7779, the migration guide mentions UiImageSize, but I also need to know the content size for text nodes.

@kaosat-dev
Copy link
Contributor

Very good question, I am running into the same issue as well.

nicopap added a commit to nicopap/bevy-website that referenced this issue Jul 11, 2023
nicopap added a commit to nicopap/bevy-website that referenced this issue Jul 11, 2023
nicopap added a commit to nicopap/bevy-website that referenced this issue Jul 11, 2023
@nicopap
Copy link
Contributor Author

nicopap commented Jul 11, 2023

It is impossible to access equivalent data in 0.11 for text. You need to manually compute it. Here is how you would do it.

/// Due to a regression in bevy 0.11, it is now impossible to access
/// text pre-layouting
fn create_text_measure(
    fonts: Res<Assets<Font>>,
    texts: Query<&Text>,
    ui_scale: Res<UiScale>,
    windows: Query<&Window, With<PrimaryWindow>>,
) {
    let window_factor = windows.get_single().map(|w| w.resolution.scale_factor());
    let window_factor = window_factor.unwrap_or(1.);

    for text in &texts {
        let Ok(measure) = TextPipeline::default().create_text_measure(
            &fonts,
            &text.sections,
            ui_scale.scale * window_factor,
            text.alignment,
            text.linebreak_behavior,
        ) else {
            continue;
        };
        measure.compute_size(/* Vec2::INFINITY if unbound */)
    }
}

You might be worried about the TextPipeline::default().create_text_measure but worry not! the TextPipeline state isn't used in any way in create_text_measure

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

Successfully merging a pull request may close this issue.

3 participants