Skip to content

Commit

Permalink
Fix Node::physical_rect and add a physical_size method (#8551)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
ickshonpe authored May 11, 2023
1 parent 1644426 commit a35ed55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
25 changes: 20 additions & 5 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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,
),
}
}
}
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 @@ -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.
Expand Down Expand Up @@ -178,18 +174,15 @@ 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,
&text.sections,
scale_factor,
text.alignment,
text.linebreak_behavior,
node_size,
physical_node_size,
font_atlas_set_storage,
texture_atlases,
textures,
Expand Down

0 comments on commit a35ed55

Please sign in to comment.