Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Node::physical_rect and add a physical_size method #8551

Merged
merged 7 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ impl Node {
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: f32) -> Vec2 {
self.calculated_size * scale_factor
}

/// Returns the logical pixel coordinates of the UI node, based on its [`GlobalTransform`].
#[inline]
pub fn logical_rect(&self, transform: &GlobalTransform) -> Rect {
Expand All @@ -41,8 +47,8 @@ impl Node {
pub fn physical_rect(&self, transform: &GlobalTransform, scale_factor: f32) -> Rect {
let rect = self.logical_rect(transform);
Rect {
min: rect.min / scale_factor,
max: rect.max / scale_factor,
min: rect.min * scale_factor,
max: rect.max * scale_factor,
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,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
}

#[derive(Clone)]
pub struct TextMeasure {
pub info: TextMeasureInfo,
Expand Down Expand Up @@ -176,18 +172,15 @@ pub fn text_system(
let mut text_query = text_queries.p2();
for entity in queued_text.drain(..) {
if let Ok((node, text, mut text_layout_info)) = text_query.get_mut(entity) {
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 as f32);

match text_pipeline.queue_text(
&fonts,
&text.sections,
scale_factor,
text.alignment,
text.linebreak_behavior,
node_size,
physical_node_size,
&mut font_atlas_set_storage,
&mut texture_atlases,
&mut textures,
Expand Down