diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index fd551d3d365e8..60454ace5bae7 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -15,7 +15,7 @@ use std::ops::{Div, DivAssign, Mul, MulAssign}; use thiserror::Error; /// Describes the size of a UI node -#[derive(Component, Debug, Clone, Reflect)] +#[derive(Component, Debug, Copy, Clone, Reflect)] #[reflect(Component, Default)] pub struct Node { /// The size of the node as width and height in logical pixels @@ -26,10 +26,19 @@ pub struct Node { impl Node { /// The calculated node size as width and height in logical pixels /// automatically calculated by [`super::layout::ui_layout_system`] - pub fn size(&self) -> Vec2 { + pub const fn size(&self) -> Vec2 { self.calculated_size } + /// Returns the size of the node in physical pixels based on the given scale factor. + #[inline] + pub fn physical_size(&self, scale_factor: f64) -> Vec2 { + Vec2::new( + (self.calculated_size.x as f64 * scale_factor) as f32, + (self.calculated_size.y as f64 * scale_factor) as f32, + ) + } + /// Returns the logical pixel coordinates of the UI node, based on its [`GlobalTransform`]. #[inline] pub fn logical_rect(&self, transform: &GlobalTransform) -> Rect { @@ -38,11 +47,17 @@ impl Node { /// Returns the physical pixel coordinates of the UI node, based on its [`GlobalTransform`] and the scale factor. #[inline] - pub fn physical_rect(&self, transform: &GlobalTransform, scale_factor: f32) -> Rect { + pub fn physical_rect(&self, transform: &GlobalTransform, scale_factor: f64) -> Rect { let rect = self.logical_rect(transform); Rect { - min: rect.min / scale_factor, - max: rect.max / scale_factor, + min: Vec2::new( + (rect.min.x as f64 * scale_factor) as f32, + (rect.min.y as f64 * scale_factor) as f32, + ), + max: Vec2::new( + (rect.max.x as f64 * scale_factor) as f32, + (rect.max.y as f64 * scale_factor) as f32, + ), } } } diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index f2a240faa6e28..c56ae98bc5997 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -18,10 +18,6 @@ use bevy_text::{ use bevy_window::{PrimaryWindow, Window}; use taffy::style::AvailableSpace; -fn scale_value(value: f32, factor: f64) -> f32 { - (value as f64 * factor) as f32 -} - /// Text system flags /// /// Used internally by [`measure_text_system`] and [`text_system`] to schedule text for processing. @@ -178,10 +174,7 @@ fn queue_text( ) { // Skip the text node if it is waiting for a new measure func if !text_flags.needs_new_measure_func { - let node_size = Vec2::new( - scale_value(node.size().x, scale_factor), - scale_value(node.size().y, scale_factor), - ); + let physical_node_size = node.physical_size(scale_factor); match text_pipeline.queue_text( fonts, @@ -189,7 +182,7 @@ fn queue_text( scale_factor, text.alignment, text.linebreak_behavior, - node_size, + physical_node_size, font_atlas_set_storage, texture_atlases, textures,