Skip to content

Commit

Permalink
UiRect axes constructor (#7656)
Browse files Browse the repository at this point in the history
# 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`.
  • Loading branch information
ickshonpe authored Apr 13, 2023
1 parent b9f3272 commit 0cbabef
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.)`.
///
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 0cbabef

Please sign in to comment.