Skip to content

Commit

Permalink
Add additional constructors for UiRect to specify values for specif…
Browse files Browse the repository at this point in the history
…ic fields (bevyengine#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
  • Loading branch information
Piturnah authored and james7132 committed Oct 28, 2022
1 parent 8de0e86 commit 3e5eecb
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3e5eecb

Please sign in to comment.