Fix Node::physical_rect and add a physical_size method (#8551)
# Objective * `Node::physical_rect` divides the logical size of the node by the scale factor, when it should multiply. * Add a `physical_size` method to `Node` that calculates the physical size of a node. --- ## Changelog * Added a method `physical_size` to `Node` that calculates the physical size of the `Node` based on the given scale factor. * Fixed the `Node::physical_rect` method, the logical size should be multiplied by the scale factor to get the physical size. * Removed the `scale_value` function from the `text` widget module and replaced its usage with `Node::physical_size`. * Derived `Copy` for `Node` (since it's only a wrapped `Vec2`). * Made `Node::size` const.
This commit is contained in:
parent
1644426761
commit
a35ed552fa
@ -15,7 +15,7 @@ use std::ops::{Div, DivAssign, Mul, MulAssign};
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
/// Describes the size of a UI node
|
/// Describes the size of a UI node
|
||||||
#[derive(Component, Debug, Clone, Reflect)]
|
#[derive(Component, Debug, Copy, Clone, Reflect)]
|
||||||
#[reflect(Component, Default)]
|
#[reflect(Component, Default)]
|
||||||
pub struct Node {
|
pub struct Node {
|
||||||
/// The size of the node as width and height in logical pixels
|
/// The size of the node as width and height in logical pixels
|
||||||
@ -26,10 +26,19 @@ pub struct Node {
|
|||||||
impl Node {
|
impl Node {
|
||||||
/// The calculated node size as width and height in logical pixels
|
/// The calculated node size as width and height in logical pixels
|
||||||
/// automatically calculated by [`super::layout::ui_layout_system`]
|
/// automatically calculated by [`super::layout::ui_layout_system`]
|
||||||
pub fn size(&self) -> Vec2 {
|
pub const fn size(&self) -> Vec2 {
|
||||||
self.calculated_size
|
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`].
|
/// Returns the logical pixel coordinates of the UI node, based on its [`GlobalTransform`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn logical_rect(&self, transform: &GlobalTransform) -> Rect {
|
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.
|
/// Returns the physical pixel coordinates of the UI node, based on its [`GlobalTransform`] and the scale factor.
|
||||||
#[inline]
|
#[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);
|
let rect = self.logical_rect(transform);
|
||||||
Rect {
|
Rect {
|
||||||
min: rect.min / scale_factor,
|
min: Vec2::new(
|
||||||
max: rect.max / scale_factor,
|
(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,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,10 +18,6 @@ use bevy_text::{
|
|||||||
use bevy_window::{PrimaryWindow, Window};
|
use bevy_window::{PrimaryWindow, Window};
|
||||||
use taffy::style::AvailableSpace;
|
use taffy::style::AvailableSpace;
|
||||||
|
|
||||||
fn scale_value(value: f32, factor: f64) -> f32 {
|
|
||||||
(value as f64 * factor) as f32
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Text system flags
|
/// Text system flags
|
||||||
///
|
///
|
||||||
/// Used internally by [`measure_text_system`] and [`text_system`] to schedule text for processing.
|
/// 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
|
// Skip the text node if it is waiting for a new measure func
|
||||||
if !text_flags.needs_new_measure_func {
|
if !text_flags.needs_new_measure_func {
|
||||||
let node_size = Vec2::new(
|
let physical_node_size = node.physical_size(scale_factor);
|
||||||
scale_value(node.size().x, scale_factor),
|
|
||||||
scale_value(node.size().y, scale_factor),
|
|
||||||
);
|
|
||||||
|
|
||||||
match text_pipeline.queue_text(
|
match text_pipeline.queue_text(
|
||||||
fonts,
|
fonts,
|
||||||
@ -189,7 +182,7 @@ fn queue_text(
|
|||||||
scale_factor,
|
scale_factor,
|
||||||
text.alignment,
|
text.alignment,
|
||||||
text.linebreak_behavior,
|
text.linebreak_behavior,
|
||||||
node_size,
|
physical_node_size,
|
||||||
font_atlas_set_storage,
|
font_atlas_set_storage,
|
||||||
texture_atlases,
|
texture_atlases,
|
||||||
textures,
|
textures,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user