bevy_vello icon indicating copy to clipboard operation
bevy_vello copied to clipboard

System that calculates VelloTextSection's ContentSize for bevy_ui's layouting

Open RobertBrewitz opened this issue 9 months ago • 1 comments

I think we should add a system that can calculate the content size for bevy_ui for VelloTextSections; Or perhaps create a new "VelloUiTextSection" component that does the calculation, i.e. the bounding box of the text node is sent to the ContentSize component.

This would allow consumers to draw VelloScenes based on the bounding box size of VelloTextSection.

Example code.

pub fn calculate_content_size(
    mut text_section_q: Query<
        (&mut ContentSize, &mut VelloTextSection, &GlobalTransform),
        Changed<VelloTextSection>,
    >,
    camera: Single<(&Camera, &GlobalTransform), With<Camera2d>>,
    fonts: Res<Assets<VelloFont>>,
) {
    let (camera, view) = *camera;

    for (mut cs, text, gtransform) in text_section_q.iter_mut() {
        if let Some(font) = fonts.get(&text.style.font) {
            if let Some(rect) = text.bb_in_screen_space(font, gtransform, camera, view) {
                // TODO: scale from setting
                let size = rect.size();
                let measure = NodeMeasure::Fixed(bevy::ui::FixedMeasure {
                    size: Vec2::new(size.x.abs(), size.y.abs()),
                });
                cs.set(measure);
            }
        }
    }
}

Consuming:

fn render_in_text_bounding_box_system(
    mut query: Query<(&ComputedNode, &mut VelloScene), With<VelloTextSection>>,
) {
    for (node, mut scene) in query.iter_mut() {
        let scene = &mut scene.inner;
        scene.reset();
        let size = node.size();
        let shape = vello::kurbo::RoundedRect::new(
            0.,
            0.,
            size.x as f64,
            size.y as f64,
            4.,
        );
        // scene.fill(...)
    }
}

RobertBrewitz avatar Mar 22 '25 22:03 RobertBrewitz

Definitely a good idea. Use of sizeof seems lowlevel and sophmoric. This would feel better with the ECS.

nuzzles avatar Apr 02 '25 16:04 nuzzles

started work here https://github.com/linebender/bevy_vello/pull/159

RobertBrewitz avatar Jun 12 '25 14:06 RobertBrewitz

The functionality exists now.

RobertBrewitz avatar Jun 23 '25 20:06 RobertBrewitz