From 5875ea7db03e3f64d60b305de7f58ffb245f6b32 Mon Sep 17 00:00:00 2001 From: Peter Hebden Date: Tue, 27 Sep 2022 18:11:39 +0000 Subject: [PATCH] Add additional constructors for `UiRect` to specify values for specific fields (#5988) # Objective Often one wants to create a `UiRect` with a value only specifying a single field. These ways are already available, but not the most ergonomic: ```rust UiRect::new(Val::Undefined, Val::Undefined, Val::Percent(25.0), Val::Undefined) ``` ```rust UiRect { top: Val::Percent(25.0), ..default() } ``` ## Solution Introduce 6 new constructors: - `horizontal` - `vertical` - `left` - `right` - `top` - `bottom` So the above code can be written instead as: ```rust UiRect::top(Val::Percent(25.0)) ``` This solution is similar to the style fields `margin-left`, `padding-top`, etc. that you would see in CSS, from which bevy's UI has other inspiration. Therefore, it should still feel intuitive to users coming from CSS. --- ## Changelog ### Added - Additional constructors for `UiRect` to specify values for specific fields --- crates/bevy_ui/src/geometry.rs | 128 +++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index bf0220eb3197d..13c8de3bbc7ce 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -184,6 +184,134 @@ impl UiRect { bottom: value, } } + + /// Creates a new [`UiRect`] where `left` and `right` take the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{UiRect, Val}; + /// # + /// let ui_rect = UiRect::horizontal(Val::Px(10.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::Undefined); + /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// ``` + pub fn horizontal(value: Val) -> Self { + UiRect { + left: value, + right: value, + ..Default::default() + } + } + + /// Creates a new [`UiRect`] where `top` and `bottom` take the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{UiRect, Val}; + /// # + /// let ui_rect = UiRect::vertical(Val::Px(10.0)); + /// + /// assert_eq!(ui_rect.left, Val::Undefined); + /// assert_eq!(ui_rect.right, Val::Undefined); + /// assert_eq!(ui_rect.top, Val::Px(10.0)); + /// assert_eq!(ui_rect.bottom, Val::Px(10.0)); + /// ``` + pub fn vertical(value: Val) -> Self { + UiRect { + top: value, + bottom: value, + ..Default::default() + } + } + + /// Creates a new [`UiRect`] where `left` takes the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{UiRect, Val}; + /// # + /// let ui_rect = UiRect::left(Val::Px(10.0)); + /// + /// assert_eq!(ui_rect.left, Val::Px(10.0)); + /// assert_eq!(ui_rect.right, Val::Undefined); + /// assert_eq!(ui_rect.top, Val::Undefined); + /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// ``` + pub fn left(value: Val) -> Self { + UiRect { + left: value, + ..Default::default() + } + } + + /// Creates a new [`UiRect`] where `right` takes the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{UiRect, Val}; + /// # + /// let ui_rect = UiRect::right(Val::Px(10.0)); + /// + /// assert_eq!(ui_rect.left, Val::Undefined); + /// assert_eq!(ui_rect.right, Val::Px(10.0)); + /// assert_eq!(ui_rect.top, Val::Undefined); + /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// ``` + pub fn right(value: Val) -> Self { + UiRect { + right: value, + ..Default::default() + } + } + + /// Creates a new [`UiRect`] where `top` takes the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{UiRect, Val}; + /// # + /// let ui_rect = UiRect::top(Val::Px(10.0)); + /// + /// assert_eq!(ui_rect.left, Val::Undefined); + /// assert_eq!(ui_rect.right, Val::Undefined); + /// assert_eq!(ui_rect.top, Val::Px(10.0)); + /// assert_eq!(ui_rect.bottom, Val::Undefined); + /// ``` + pub fn top(value: Val) -> Self { + UiRect { + top: value, + ..Default::default() + } + } + + /// Creates a new [`UiRect`] where `bottom` takes the given value. + /// + /// # Example + /// + /// ``` + /// # use bevy_ui::{UiRect, Val}; + /// # + /// let ui_rect = UiRect::bottom(Val::Px(10.0)); + /// + /// assert_eq!(ui_rect.left, Val::Undefined); + /// assert_eq!(ui_rect.right, Val::Undefined); + /// assert_eq!(ui_rect.top, Val::Undefined); + /// assert_eq!(ui_rect.bottom, Val::Px(10.0)); + /// ``` + pub fn bottom(value: Val) -> Self { + UiRect { + bottom: value, + ..Default::default() + } + } } /// A 2-dimensional area defined by a width and height.