Skip to content

Commit 0ef1514

Browse files
committed
fixes content size scaling
1 parent f817af9 commit 0ef1514

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This release supports Bevy version 0.16 and has an [MSRV][] of 1.87.
2222
- Adds `NoVelloScale` component to disable scaling for specific entities.
2323
- Adds `VelloScreenSpace` component.
2424
- Adds `VelloScreenSpace` to `bevy_vello::prelude`.
25-
- Adds `calculate_text_content_size` system that calculates the content size of `VelloTextSection` if it has a `ContentSize` component.
25+
- Adds functionality that automatically calculates the content size of `VelloTextSection` if it has a `ContentSize` component which is also `VelloScreenScale` aware.
2626

2727
### Changed
2828

src/integrations/text/plugin.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ use bevy::{
44
};
55

66
use super::{
7-
VelloFont, font_loader::VelloFontLoader, render, vello_text::calculate_text_content_size,
7+
VelloFont,
8+
font_loader::VelloFontLoader,
9+
render,
10+
vello_text::{
11+
calculate_text_section_content_size, calculate_text_section_content_size_on_change,
12+
},
813
};
9-
use crate::render::extract::VelloExtractStep;
14+
use crate::render::{VelloScreenScale, extract::VelloExtractStep};
1015

1116
pub struct VelloTextIntegrationPlugin;
1217

@@ -15,7 +20,11 @@ impl Plugin for VelloTextIntegrationPlugin {
1520
app.init_asset::<VelloFont>()
1621
.init_asset_loader::<VelloFontLoader>()
1722
.add_plugins(RenderAssetPlugin::<VelloFont>::default())
18-
.add_systems(Update, calculate_text_content_size);
23+
.add_systems(Update, calculate_text_section_content_size_on_change)
24+
.add_systems(
25+
Update,
26+
calculate_text_section_content_size.run_if(resource_changed::<VelloScreenScale>),
27+
);
1928

2029
#[cfg(feature = "default_font")]
2130
{

src/integrations/text/vello_text.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
use std::ops::Mul;
2+
13
use bevy::{
24
prelude::*,
35
render::view::{self, VisibilityClass},
46
ui::{ContentSize, NodeMeasure},
57
};
68
use vello::peniko::{self, Brush};
79

8-
use crate::{VelloFont, render::VelloView};
10+
use crate::{
11+
VelloFont,
12+
render::{VelloScreenScale, VelloView},
13+
};
914

1015
#[derive(Component, Default, Clone)]
1116
#[require(VelloTextAnchor, Transform, Visibility, VisibilityClass)]
@@ -224,13 +229,43 @@ impl VelloTextSection {
224229
}
225230
}
226231

227-
pub fn calculate_text_content_size(
232+
pub fn calculate_text_section_content_size_on_change(
228233
mut button_q: Query<
229234
(&mut ContentSize, &mut VelloTextSection, &GlobalTransform),
230235
Changed<VelloTextSection>,
231236
>,
232237
camera: Single<(&Camera, &GlobalTransform), With<VelloView>>,
233238
fonts: Res<Assets<VelloFont>>,
239+
screen_scale: Res<VelloScreenScale>,
240+
) {
241+
let (camera, camera_transform) = *camera;
242+
243+
for (mut cs, text, gtransform) in button_q.iter_mut() {
244+
if let Some(rect) = text.bb_in_screen_space(
245+
fonts.get(&text.style.font).unwrap(),
246+
gtransform,
247+
camera,
248+
camera_transform,
249+
) {
250+
let size = rect.size();
251+
let width = text.width.unwrap_or(size.x.abs().mul(screen_scale.0));
252+
let height = text.height.unwrap_or(size.y.abs().mul(screen_scale.0));
253+
let measure = NodeMeasure::Fixed(bevy::ui::FixedMeasure {
254+
size: Vec2::new(width, height),
255+
});
256+
cs.set(measure);
257+
}
258+
}
259+
}
260+
261+
pub fn calculate_text_section_content_size(
262+
mut button_q: Query<
263+
(&mut ContentSize, &mut VelloTextSection, &GlobalTransform),
264+
With<VelloTextSection>,
265+
>,
266+
camera: Single<(&Camera, &GlobalTransform), With<VelloView>>,
267+
fonts: Res<Assets<VelloFont>>,
268+
screen_scale: Res<VelloScreenScale>,
234269
) {
235270
let (camera, camera_transform) = *camera;
236271

@@ -242,8 +277,8 @@ pub fn calculate_text_content_size(
242277
camera_transform,
243278
) {
244279
let size = rect.size();
245-
let width = text.width.unwrap_or(size.x.abs());
246-
let height = text.height.unwrap_or(size.y.abs());
280+
let width = text.width.unwrap_or(size.x.abs().mul(screen_scale.0));
281+
let height = text.height.unwrap_or(size.y.abs().mul(screen_scale.0));
247282
let measure = NodeMeasure::Fixed(bevy::ui::FixedMeasure {
248283
size: Vec2::new(width, height),
249284
});

0 commit comments

Comments
 (0)