From 0cbabefbad1fced86eae5c33d1c4fdd7f7dda89d Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Thu, 13 Apr 2023 21:52:21 +0100 Subject: [PATCH] `UiRect` axes constructor (#7656) # Objective We don't have a constructor function for `UiRect` that sets uniform horizontal and vertical values, even though it is a common pattern. ## Solution Add a constructor function to `UiRect` called `axes`, that sets both `left` and `right` to the same given horizontal value, and sets both `top` and `bottom` to same given vertical value. ## Changelog * Added a constructor function `axes` to `UiRect`. --- crates/bevy_ui/src/geometry.rs | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index ac70c93834d32..d58509ab53534 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -164,6 +164,29 @@ impl UiRect { } } + /// Creates a new [`UiRect`] where both `left` and `right` take the value of `horizontal`, and both `top` and `bottom` take the value of `vertical`. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{UiRect, Val}; + /// # + /// let ui_rect = UiRect::axes(Val::Px(10.0), Val::Percent(15.0)); + /// + /// assert_eq!(ui_rect.left, Val::Px(10.0)); + /// assert_eq!(ui_rect.right, Val::Px(10.0)); + /// assert_eq!(ui_rect.top, Val::Percent(15.0)); + /// assert_eq!(ui_rect.bottom, Val::Percent(15.0)); + /// ``` + pub fn axes(horizontal: Val, vertical: Val) -> Self { + UiRect { + left: horizontal, + right: horizontal, + top: vertical, + bottom: vertical, + } + } + /// Creates a new [`UiRect`] where `left` takes the given value, and /// the other fields are set to `Val::Px(0.)`. /// @@ -507,4 +530,18 @@ mod tests { ); assert_eq!(Size::default(), Size::DEFAULT); } + + #[test] + fn test_uirect_axes() { + let x = Val::Px(1.); + let y = Val::Vw(4.); + let r = UiRect::axes(x, y); + let h = UiRect::horizontal(x); + let v = UiRect::vertical(y); + + assert_eq!(r.top, v.top); + assert_eq!(r.bottom, v.bottom); + assert_eq!(r.left, h.left); + assert_eq!(r.right, h.right); + } }