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

Add methods for calculating the size and postion of UI nodes #7930

Merged
merged 14 commits into from
Mar 9, 2023
17 changes: 17 additions & 0 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_render::{
color::Color,
texture::{Image, DEFAULT_IMAGE_HANDLE},
};
use bevy_transform::prelude::GlobalTransform;
use serde::{Deserialize, Serialize};
use std::ops::{Div, DivAssign, Mul, MulAssign};
use thiserror::Error;
Expand All @@ -26,6 +27,22 @@ impl Node {
pub fn size(&self) -> Vec2 {
self.calculated_size
}

/// Returns the logical pixel coordinates of the UI node, based on its `GlobalTransform`.
#[inline]
pub fn logical_rect(&self, transform: &GlobalTransform) -> Rect {
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
Rect::from_center_size(transform.translation().truncate(), self.size())
}

/// 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 {
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved
let rect = self.logical_rect(transform);
Rect {
min: rect.min / scale_factor,
max: rect.max / scale_factor,
}
}
}

impl Node {
Expand Down