Skip to content

Commit

Permalink
Added Val::ZERO Constant (#9566)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #9533

## Solution

* Added `Val::ZERO` as a constant which is defined as `Val::Px(0.)`.
* Added manual `PartialEq` implementation for `Val` which allows any
zero value to equal any other zero value. E.g., `Val::Px(0.) ==
Val::Percent(0.)` etc. This is technically a breaking change, as
`Val::Px(0.) == Val::Percent(0.)` now equals `true` instead of `false`
(as an example)
* Replaced instances of `Val::Px(0.)`, `Val::Percent(0.)`, etc. with
`Val::ZERO`
* Fixed `bevy_ui::layout::convert::tests::test_convert_from` test to
account for Taffy not equating `Points(0.)` and `Percent(0.)`. These
tests now use `assert_eq!(...)` instead of `assert!(matches!(...))`
which gives easier to diagnose error messages.
  • Loading branch information
bushrat011899 authored Aug 26, 2023
1 parent 349dd8b commit 90b3ac7
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 140 deletions.
67 changes: 37 additions & 30 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@ pub struct UiRect {

impl UiRect {
pub const DEFAULT: Self = Self {
left: Val::Px(0.),
right: Val::Px(0.),
top: Val::Px(0.),
bottom: Val::Px(0.),
left: Val::ZERO,
right: Val::ZERO,
top: Val::ZERO,
bottom: Val::ZERO,
};

pub const ZERO: Self = Self {
left: Val::ZERO,
right: Val::ZERO,
top: Val::ZERO,
bottom: Val::ZERO,
};

/// Creates a new [`UiRect`] from the values specified.
Expand Down Expand Up @@ -166,7 +173,7 @@ impl UiRect {
}

/// Creates a new [`UiRect`] where `left` and `right` take the given value,
/// and `top` and `bottom` set to zero `Val::Px(0.)`.
/// and `top` and `bottom` set to zero `Val::ZERO`.
///
/// # Example
///
Expand All @@ -177,8 +184,8 @@ impl UiRect {
///
/// assert_eq!(ui_rect.left, Val::Px(10.0));
/// assert_eq!(ui_rect.right, Val::Px(10.0));
/// assert_eq!(ui_rect.top, Val::Px(0.));
/// assert_eq!(ui_rect.bottom, Val::Px(0.));
/// assert_eq!(ui_rect.top, Val::ZERO);
/// assert_eq!(ui_rect.bottom, Val::ZERO);
/// ```
pub fn horizontal(value: Val) -> Self {
UiRect {
Expand All @@ -189,7 +196,7 @@ impl UiRect {
}

/// Creates a new [`UiRect`] where `top` and `bottom` take the given value,
/// and `left` and `right` are set to `Val::Px(0.)`.
/// and `left` and `right` are set to `Val::ZERO`.
///
/// # Example
///
Expand All @@ -198,8 +205,8 @@ impl UiRect {
/// #
/// let ui_rect = UiRect::vertical(Val::Px(10.0));
///
/// assert_eq!(ui_rect.left, Val::Px(0.));
/// assert_eq!(ui_rect.right, Val::Px(0.));
/// assert_eq!(ui_rect.left, Val::ZERO);
/// assert_eq!(ui_rect.right, Val::ZERO);
/// assert_eq!(ui_rect.top, Val::Px(10.0));
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
/// ```
Expand Down Expand Up @@ -235,7 +242,7 @@ impl UiRect {
}

/// Creates a new [`UiRect`] where `left` takes the given value, and
/// the other fields are set to `Val::Px(0.)`.
/// the other fields are set to `Val::ZERO`.
///
/// # Example
///
Expand All @@ -245,9 +252,9 @@ impl UiRect {
/// let ui_rect = UiRect::left(Val::Px(10.0));
///
/// assert_eq!(ui_rect.left, Val::Px(10.0));
/// assert_eq!(ui_rect.right, Val::Px(0.));
/// assert_eq!(ui_rect.top, Val::Px(0.));
/// assert_eq!(ui_rect.bottom, Val::Px(0.));
/// assert_eq!(ui_rect.right, Val::ZERO);
/// assert_eq!(ui_rect.top, Val::ZERO);
/// assert_eq!(ui_rect.bottom, Val::ZERO);
/// ```
pub fn left(value: Val) -> Self {
UiRect {
Expand All @@ -257,7 +264,7 @@ impl UiRect {
}

/// Creates a new [`UiRect`] where `right` takes the given value,
/// and the other fields are set to `Val::Px(0.)`.
/// and the other fields are set to `Val::ZERO`.
///
/// # Example
///
Expand All @@ -266,10 +273,10 @@ impl UiRect {
/// #
/// let ui_rect = UiRect::right(Val::Px(10.0));
///
/// assert_eq!(ui_rect.left, Val::Px(0.));
/// assert_eq!(ui_rect.left, Val::ZERO);
/// assert_eq!(ui_rect.right, Val::Px(10.0));
/// assert_eq!(ui_rect.top, Val::Px(0.));
/// assert_eq!(ui_rect.bottom, Val::Px(0.));
/// assert_eq!(ui_rect.top, Val::ZERO);
/// assert_eq!(ui_rect.bottom, Val::ZERO);
/// ```
pub fn right(value: Val) -> Self {
UiRect {
Expand All @@ -279,7 +286,7 @@ impl UiRect {
}

/// Creates a new [`UiRect`] where `top` takes the given value,
/// and the other fields are set to `Val::Px(0.)`.
/// and the other fields are set to `Val::ZERO`.
///
/// # Example
///
Expand All @@ -288,10 +295,10 @@ impl UiRect {
/// #
/// let ui_rect = UiRect::top(Val::Px(10.0));
///
/// assert_eq!(ui_rect.left, Val::Px(0.));
/// assert_eq!(ui_rect.right, Val::Px(0.));
/// assert_eq!(ui_rect.left, Val::ZERO);
/// assert_eq!(ui_rect.right, Val::ZERO);
/// assert_eq!(ui_rect.top, Val::Px(10.0));
/// assert_eq!(ui_rect.bottom, Val::Px(0.));
/// assert_eq!(ui_rect.bottom, Val::ZERO);
/// ```
pub fn top(value: Val) -> Self {
UiRect {
Expand All @@ -301,7 +308,7 @@ impl UiRect {
}

/// Creates a new [`UiRect`] where `bottom` takes the given value,
/// and the other fields are set to `Val::Px(0.)`.
/// and the other fields are set to `Val::ZERO`.
///
/// # Example
///
Expand All @@ -310,9 +317,9 @@ impl UiRect {
/// #
/// let ui_rect = UiRect::bottom(Val::Px(10.0));
///
/// assert_eq!(ui_rect.left, Val::Px(0.));
/// assert_eq!(ui_rect.right, Val::Px(0.));
/// assert_eq!(ui_rect.top, Val::Px(0.));
/// assert_eq!(ui_rect.left, Val::ZERO);
/// assert_eq!(ui_rect.right, Val::ZERO);
/// assert_eq!(ui_rect.top, Val::ZERO);
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
/// ```
pub fn bottom(value: Val) -> Self {
Expand All @@ -338,10 +345,10 @@ mod tests {
assert_eq!(
UiRect::default(),
UiRect {
left: Val::Px(0.),
right: Val::Px(0.),
top: Val::Px(0.),
bottom: Val::Px(0.)
left: Val::ZERO,
right: Val::ZERO,
top: Val::ZERO,
bottom: Val::ZERO
}
);
assert_eq!(UiRect::default(), UiRect::DEFAULT);
Expand Down
Loading

0 comments on commit 90b3ac7

Please sign in to comment.