From 677b4ebe7e56248e10484156b3a17bd490a8c67c Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Thu, 9 Mar 2023 14:12:54 +0000 Subject: [PATCH] Add methods for calculating the size and postion of UI nodes (#7930) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: François --- crates/bevy_ui/src/ui_node.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index a457f699ddb3db..52290aa3a5f565 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -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; @@ -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 { + 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 { + let rect = self.logical_rect(transform); + Rect { + min: rect.min / scale_factor, + max: rect.max / scale_factor, + } + } } impl Node {