Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an enum with only the numeric variants of Val #7569

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/flex/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style {
justify_content: value.justify_content.into(),
position: from_rect(scale_factor, value.position),
margin: from_rect(scale_factor, value.margin),
padding: from_rect(scale_factor, value.padding),
border: from_rect(scale_factor, value.border),
padding: from_rect(scale_factor, value.padding.into()),
border: from_rect(scale_factor, value.border.into()),
flex_grow: value.flex_grow,
flex_shrink: value.flex_shrink,
flex_basis: from_val(scale_factor, value.flex_basis),
Expand Down
99 changes: 65 additions & 34 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Val;
use crate::{Breadth, Val};
use bevy_reflect::Reflect;
use std::ops::{Div, DivAssign, Mul, MulAssign};

Expand Down Expand Up @@ -95,13 +95,13 @@ use std::ops::{Div, DivAssign, Mul, MulAssign};
/// A padding is used to create space around UI elements, inside of any defined borders.
///
/// ```
/// # use bevy_ui::{UiRect, Val};
/// # use bevy_ui::{UiRect, Breadth};
/// #
/// let padding = UiRect {
/// left: Val::Px(10.0),
/// right: Val::Px(20.0),
/// top: Val::Px(30.0),
/// bottom: Val::Px(40.0),
/// left: Breadth::Px(10.0),
/// right: Breadth::Px(20.0),
/// top: Breadth::Px(30.0),
/// bottom: Breadth::Px(40.0),
/// };
/// ```
///
Expand All @@ -110,37 +110,33 @@ use std::ops::{Div, DivAssign, Mul, MulAssign};
/// A border is used to define the width of the border of a UI element.
///
/// ```
/// # use bevy_ui::{UiRect, Val};
/// # use bevy_ui::{UiRect, Breadth};
/// #
/// let border = UiRect {
/// left: Val::Px(10.0),
/// right: Val::Px(20.0),
/// top: Val::Px(30.0),
/// bottom: Val::Px(40.0),
/// left: Breadth::Px(10.0),
/// right: Breadth::Px(20.0),
/// top: Breadth::Px(30.0),
/// bottom: Breadth::Px(40.0),
/// };
/// ```
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
#[reflect(PartialEq)]
pub struct UiRect {
pub struct UiRect<T: Default + Copy + Clone + PartialEq = Val> {
/// The value corresponding to the left side of the UI rect.
pub left: Val,
pub left: T,
/// The value corresponding to the right side of the UI rect.
pub right: Val,
pub right: T,
/// The value corresponding to the top side of the UI rect.
pub top: Val,
pub top: T,
/// The value corresponding to the bottom side of the UI rect.
pub bottom: Val,
pub bottom: T,
}

impl UiRect {
pub const DEFAULT: Self = Self {
left: Val::DEFAULT,
right: Val::DEFAULT,
top: Val::DEFAULT,
bottom: Val::DEFAULT,
};

/// Creates a new [`UiRect`] from the values specified.
impl<T> UiRect<T>
where
UiRect<T>: Default,
T: Default + Copy + Clone + PartialEq,
{
///
/// # Example
///
Expand All @@ -159,7 +155,7 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Px(30.0));
/// assert_eq!(ui_rect.bottom, Val::Px(40.0));
/// ```
pub const fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self {
pub const fn new(left: T, right: T, top: T, bottom: T) -> Self {
UiRect {
left,
right,
Expand All @@ -182,7 +178,7 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Px(10.0));
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
/// ```
pub const fn all(value: Val) -> Self {
pub const fn all(value: T) -> Self {
UiRect {
left: value,
right: value,
Expand All @@ -205,7 +201,7 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Undefined);
/// assert_eq!(ui_rect.bottom, Val::Undefined);
/// ```
pub fn horizontal(value: Val) -> Self {
pub fn horizontal(value: T) -> Self {
UiRect {
left: value,
right: value,
Expand All @@ -227,7 +223,7 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Px(10.0));
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
/// ```
pub fn vertical(value: Val) -> Self {
pub fn vertical(value: T) -> Self {
UiRect {
top: value,
bottom: value,
Expand All @@ -249,7 +245,7 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Undefined);
/// assert_eq!(ui_rect.bottom, Val::Undefined);
/// ```
pub fn left(value: Val) -> Self {
pub fn left(value: T) -> Self {
UiRect {
left: value,
..Default::default()
Expand All @@ -270,7 +266,7 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Undefined);
/// assert_eq!(ui_rect.bottom, Val::Undefined);
/// ```
pub fn right(value: Val) -> Self {
pub fn right(value: T) -> Self {
UiRect {
right: value,
..Default::default()
Expand All @@ -291,7 +287,7 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Px(10.0));
/// assert_eq!(ui_rect.bottom, Val::Undefined);
/// ```
pub fn top(value: Val) -> Self {
pub fn top(value: T) -> Self {
UiRect {
top: value,
..Default::default()
Expand All @@ -312,20 +308,55 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Undefined);
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
/// ```
pub fn bottom(value: Val) -> Self {
pub fn bottom(value: T) -> Self {
UiRect {
bottom: value,
..Default::default()
}
}
}

impl Default for UiRect {
impl UiRect<Val> {
pub const DEFAULT: Self = Self {
left: Val::DEFAULT,
right: Val::DEFAULT,
top: Val::DEFAULT,
bottom: Val::DEFAULT,
};
}

impl Default for UiRect<Val> {
fn default() -> Self {
Self::DEFAULT
}
}

impl UiRect<Breadth> {
pub const DEFAULT: Self = Self {
left: Breadth::DEFAULT,
right: Breadth::DEFAULT,
top: Breadth::DEFAULT,
bottom: Breadth::DEFAULT,
};
}

impl Default for UiRect<Breadth> {
fn default() -> Self {
Self::DEFAULT
}
}

impl From<UiRect<Breadth>> for UiRect<Val> {
fn from(rect: UiRect<Breadth>) -> Self {
Self {
left: rect.left.into(),
right: rect.right.into(),
top: rect.top.into(),
bottom: rect.bottom.into(),
}
}
}

/// A 2-dimensional area defined by a width and height.
///
/// It is commonly used to define the size of a text or UI element.
Expand Down
Loading