Skip to content

Commit 5a71831

Browse files
committed
The size field of CalculatedSize should not be a Size (#7641)
# Objective The `size` field of `CalculatedSize` shouldn't be a `Size` as it only ever stores (unscaled) pixel values. By default its fields are `Val::Auto` but these are converted to `0`s before being sent to Taffy. ## Solution Change the `size` field of `CalculatedSize` to a Vec2. ## Changelog * Changed the `size` field of `CalculatedSize` to a Vec2. * Removed the `Val` <-> `f32` conversion code for `CalculatedSize`. ## Migration Guide * The size field of `CalculatedSize` has been changed to a `Vec2`.
1 parent 40bbbbb commit 5a71831

File tree

5 files changed

+18
-30
lines changed

5 files changed

+18
-30
lines changed

crates/bevy_ui/src/flex/convert.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ pub fn from_rect(
1515
}
1616
}
1717

18-
pub fn from_f32_size(scale_factor: f64, size: Size) -> taffy::geometry::Size<f32> {
19-
taffy::geometry::Size {
20-
width: val_to_f32(scale_factor, size.width),
21-
height: val_to_f32(scale_factor, size.height),
22-
}
23-
}
24-
2518
pub fn from_val_size(
2619
scale_factor: f64,
2720
size: Size,
@@ -57,15 +50,6 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style {
5750
}
5851
}
5952

60-
/// Converts a [`Val`] to a [`f32`] while respecting the scale factor.
61-
pub fn val_to_f32(scale_factor: f64, val: Val) -> f32 {
62-
match val {
63-
Val::Undefined | Val::Auto => 0.0,
64-
Val::Px(value) => (scale_factor * value as f64) as f32,
65-
Val::Percent(value) => value / 100.0,
66-
}
67-
}
68-
6953
pub fn from_val(scale_factor: f64, val: Val) -> taffy::style::Dimension {
7054
match val {
7155
Val::Auto => taffy::style::Dimension::Auto,

crates/bevy_ui/src/flex/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ impl FlexSurface {
8686
let taffy_style = convert::from_style(scale_factor, style);
8787
let measure = taffy::node::MeasureFunc::Boxed(Box::new(
8888
move |constraints: Size<Option<f32>>, _available: Size<AvailableSpace>| {
89-
let mut size = convert::from_f32_size(scale_factor, calculated_size.size);
89+
let mut size = Size {
90+
width: (scale_factor * calculated_size.size.x as f64) as f32,
91+
height: (scale_factor * calculated_size.size.y as f64) as f32,
92+
};
9093
match (constraints.width, constraints.height) {
9194
(None, None) => {}
9295
(Some(width), None) => {

crates/bevy_ui/src/ui_node.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,15 @@ impl Default for FlexWrap {
563563
#[derive(Component, Copy, Clone, Debug, Reflect)]
564564
#[reflect(Component)]
565565
pub struct CalculatedSize {
566-
/// The size of the node
567-
pub size: Size,
566+
/// The size of the node in logical pixels
567+
pub size: Vec2,
568568
/// Whether to attempt to preserve the aspect ratio when determining the layout for this item
569569
pub preserve_aspect_ratio: bool,
570570
}
571571

572572
impl CalculatedSize {
573573
const DEFAULT: Self = Self {
574-
size: Size::DEFAULT,
574+
size: Vec2::ZERO,
575575
preserve_aspect_ratio: false,
576576
};
577577
}

crates/bevy_ui/src/widget/image.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::{CalculatedSize, Size, UiImage, Val};
1+
use crate::{CalculatedSize, UiImage};
22
use bevy_asset::Assets;
33
use bevy_ecs::{
44
query::Without,
55
system::{Query, Res},
66
};
7+
use bevy_math::Vec2;
78
use bevy_render::texture::Image;
89
use bevy_text::Text;
910

@@ -14,10 +15,10 @@ pub fn update_image_calculated_size_system(
1415
) {
1516
for (mut calculated_size, image) in &mut query {
1617
if let Some(texture) = textures.get(&image.texture) {
17-
let size = Size {
18-
width: Val::Px(texture.texture_descriptor.size.width as f32),
19-
height: Val::Px(texture.texture_descriptor.size.height as f32),
20-
};
18+
let size = Vec2::new(
19+
texture.texture_descriptor.size.width as f32,
20+
texture.texture_descriptor.size.height as f32,
21+
);
2122
// Update only if size has changed to avoid needless layout calculations
2223
if size != calculated_size.size {
2324
calculated_size.size = size;

crates/bevy_ui/src/widget/text.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{CalculatedSize, Size, Style, UiScale, Val};
1+
use crate::{CalculatedSize, Style, UiScale, Val};
22
use bevy_asset::Assets;
33
use bevy_ecs::{
44
entity::Entity,
@@ -135,10 +135,10 @@ pub fn text_system(
135135
panic!("Fatal error when processing text: {e}.");
136136
}
137137
Ok(info) => {
138-
calculated_size.size = Size {
139-
width: Val::Px(scale_value(info.size.x, inv_scale_factor)),
140-
height: Val::Px(scale_value(info.size.y, inv_scale_factor)),
141-
};
138+
calculated_size.size = Vec2::new(
139+
scale_value(info.size.x, inv_scale_factor),
140+
scale_value(info.size.y, inv_scale_factor),
141+
);
142142
match text_layout_info {
143143
Some(mut t) => *t = info,
144144
None => {

0 commit comments

Comments
 (0)