bevy_vello
bevy_vello copied to clipboard
System that calculates VelloTextSection's ContentSize for bevy_ui's layouting
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(...)
}
}
Definitely a good idea. Use of sizeof seems lowlevel and sophmoric. This would feel better with the ECS.
started work here https://github.com/linebender/bevy_vello/pull/159
The functionality exists now.