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:
ickshonpe 2023-05-11 19:38:01 +01:00 committed by GitHub
parent 1644426761
commit a35ed552fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 14 deletions

View File

@ -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,
),
}
}
}

View File

@ -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,