Remove Val::Undefined (#7485)

This commit is contained in:
ickshonpe 2023-03-13 15:17:00 +00:00 committed by GitHub
parent d8b7fed4fe
commit 87dda354dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 361 additions and 380 deletions

View file

@ -11,13 +11,12 @@ impl Val {
Val::Auto => Val::Auto,
Val::Percent(value) => Val::Percent(value),
Val::Px(value) => Val::Px((scale_factor * value as f64) as f32),
Val::Undefined => Val::Undefined,
}
}
fn to_inset(self) -> LengthPercentageAuto {
match self {
Val::Auto | Val::Undefined => taffy::style::LengthPercentageAuto::Auto,
Val::Auto => taffy::style::LengthPercentageAuto::Auto,
Val::Percent(value) => taffy::style::LengthPercentageAuto::Percent(value / 100.0),
Val::Px(value) => taffy::style::LengthPercentageAuto::Points(value),
}
@ -67,7 +66,7 @@ impl<T: From<Val>> From<Size> for taffy::prelude::Size<T> {
impl From<Val> for taffy::style::Dimension {
fn from(value: Val) -> Self {
match value {
Val::Auto | Val::Undefined => taffy::style::Dimension::Auto,
Val::Auto => taffy::style::Dimension::Auto,
Val::Percent(value) => taffy::style::Dimension::Percent(value / 100.0),
Val::Px(value) => taffy::style::Dimension::Points(value),
}
@ -77,7 +76,7 @@ impl From<Val> for taffy::style::Dimension {
impl From<Val> for taffy::style::LengthPercentage {
fn from(value: Val) -> Self {
match value {
Val::Auto | Val::Undefined => taffy::style::LengthPercentage::Points(0.0),
Val::Auto => taffy::style::LengthPercentage::Points(0.0),
Val::Percent(value) => taffy::style::LengthPercentage::Percent(value / 100.0),
Val::Px(value) => taffy::style::LengthPercentage::Points(value),
}
@ -90,7 +89,6 @@ impl From<Val> for taffy::style::LengthPercentageAuto {
Val::Auto => taffy::style::LengthPercentageAuto::Auto,
Val::Percent(value) => taffy::style::LengthPercentageAuto::Percent(value / 100.0),
Val::Px(value) => taffy::style::LengthPercentageAuto::Points(value),
Val::Undefined => taffy::style::LengthPercentageAuto::Points(0.),
}
}
}
@ -106,10 +104,10 @@ pub fn from_style(scale_factor: f64, style: &Style) -> taffy::style::Style {
align_content: Some(style.align_content.into()),
justify_content: Some(style.justify_content.into()),
inset: taffy::prelude::Rect {
left: style.position.left.scaled(scale_factor).to_inset(),
right: style.position.right.scaled(scale_factor).to_inset(),
top: style.position.top.scaled(scale_factor).to_inset(),
bottom: style.position.bottom.scaled(scale_factor).to_inset(),
left: style.left.scaled(scale_factor).to_inset(),
right: style.right.scaled(scale_factor).to_inset(),
top: style.top.scaled(scale_factor).to_inset(),
bottom: style.bottom.scaled(scale_factor).to_inset(),
},
margin: style.margin.scaled(scale_factor).into(),
padding: style.padding.scaled(scale_factor).into(),
@ -224,3 +222,190 @@ impl From<FlexWrap> for taffy::style::FlexWrap {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_convert_from() {
let bevy_style = crate::Style {
display: Display::Flex,
position_type: PositionType::Absolute,
left: Val::Px(0.),
right: Val::Percent(0.),
top: Val::Auto,
bottom: Val::Auto,
direction: crate::Direction::Inherit,
flex_direction: FlexDirection::ColumnReverse,
flex_wrap: FlexWrap::WrapReverse,
align_items: AlignItems::Baseline,
align_self: AlignSelf::Start,
align_content: AlignContent::SpaceAround,
justify_content: JustifyContent::SpaceEvenly,
margin: UiRect {
left: Val::Percent(0.),
right: Val::Px(0.),
top: Val::Auto,
bottom: Val::Auto,
},
padding: UiRect {
left: Val::Percent(0.),
right: Val::Px(0.),
top: Val::Percent(0.),
bottom: Val::Percent(0.),
},
border: UiRect {
left: Val::Px(0.),
right: Val::Px(0.),
top: Val::Auto,
bottom: Val::Px(0.),
},
flex_grow: 1.,
flex_shrink: 0.,
flex_basis: Val::Px(0.),
size: Size {
width: Val::Px(0.),
height: Val::Auto,
},
min_size: Size {
width: Val::Px(0.),
height: Val::Percent(0.),
},
max_size: Size {
width: Val::Auto,
height: Val::Px(0.),
},
aspect_ratio: None,
overflow: crate::Overflow::Hidden,
gap: Size {
width: Val::Px(0.),
height: Val::Percent(0.),
},
};
let taffy_style = from_style(1.0, &bevy_style);
assert_eq!(taffy_style.display, taffy::style::Display::Flex);
assert_eq!(taffy_style.position, taffy::style::Position::Absolute);
assert!(matches!(
taffy_style.inset.left,
taffy::style::LengthPercentageAuto::Points(_)
));
assert!(matches!(
taffy_style.inset.right,
taffy::style::LengthPercentageAuto::Percent(_)
));
assert!(matches!(
taffy_style.inset.top,
taffy::style::LengthPercentageAuto::Auto
));
assert!(matches!(
taffy_style.inset.bottom,
taffy::style::LengthPercentageAuto::Auto
));
assert_eq!(
taffy_style.flex_direction,
taffy::style::FlexDirection::ColumnReverse
);
assert_eq!(taffy_style.flex_wrap, taffy::style::FlexWrap::WrapReverse);
assert_eq!(
taffy_style.align_items,
Some(taffy::style::AlignItems::Baseline)
);
assert_eq!(taffy_style.align_self, Some(taffy::style::AlignSelf::Start));
assert_eq!(
taffy_style.align_content,
Some(taffy::style::AlignContent::SpaceAround)
);
assert_eq!(
taffy_style.justify_content,
Some(taffy::style::JustifyContent::SpaceEvenly)
);
assert!(matches!(
taffy_style.margin.left,
taffy::style::LengthPercentageAuto::Percent(_)
));
assert!(matches!(
taffy_style.margin.right,
taffy::style::LengthPercentageAuto::Points(_)
));
assert!(matches!(
taffy_style.margin.top,
taffy::style::LengthPercentageAuto::Auto
));
assert!(matches!(
taffy_style.margin.bottom,
taffy::style::LengthPercentageAuto::Auto
));
assert!(matches!(
taffy_style.padding.left,
taffy::style::LengthPercentage::Percent(_)
));
assert!(matches!(
taffy_style.padding.right,
taffy::style::LengthPercentage::Points(_)
));
assert!(matches!(
taffy_style.padding.top,
taffy::style::LengthPercentage::Percent(_)
));
assert!(matches!(
taffy_style.padding.bottom,
taffy::style::LengthPercentage::Percent(_)
));
assert!(matches!(
taffy_style.border.left,
taffy::style::LengthPercentage::Points(_)
));
assert!(matches!(
taffy_style.border.right,
taffy::style::LengthPercentage::Points(_)
));
assert!(matches!(
taffy_style.border.top,
taffy::style::LengthPercentage::Points(_)
));
assert!(matches!(
taffy_style.border.bottom,
taffy::style::LengthPercentage::Points(_)
));
assert_eq!(taffy_style.flex_grow, 1.);
assert_eq!(taffy_style.flex_shrink, 0.);
assert!(matches!(
taffy_style.flex_basis,
taffy::style::Dimension::Points(_)
));
assert!(matches!(
taffy_style.size.width,
taffy::style::Dimension::Points(_)
));
assert!(matches!(
taffy_style.size.height,
taffy::style::Dimension::Auto
));
assert!(matches!(
taffy_style.min_size.width,
taffy::style::Dimension::Points(_)
));
assert!(matches!(
taffy_style.min_size.height,
taffy::style::Dimension::Percent(_)
));
assert!(matches!(
taffy_style.max_size.width,
taffy::style::Dimension::Auto
));
assert!(matches!(
taffy_style.max_size.height,
taffy::style::Dimension::Points(_)
));
assert_eq!(taffy_style.aspect_ratio, None);
assert_eq!(
taffy_style.gap.width,
taffy::style::LengthPercentage::Points(0.)
);
assert_eq!(
taffy_style.gap.height,
taffy::style::LengthPercentage::Percent(0.)
);
}
}

View file

@ -2,83 +2,10 @@ use crate::Val;
use bevy_reflect::Reflect;
use std::ops::{Div, DivAssign, Mul, MulAssign};
/// A type which is commonly used to define positions, margins, paddings and borders.
/// A type which is commonly used to define margins, paddings and borders.
///
/// # Examples
///
/// ## Position
///
/// A position is used to determine where to place a UI element.
///
/// ```
/// # use bevy_ui::{UiRect, Val};
/// # use bevy_utils::default;
/// #
/// let position = UiRect {
/// left: Val::Px(100.0),
/// top: Val::Px(50.0),
/// ..default()
/// };
/// ```
///
/// If you define opposite sides of the position, the size of the UI element will automatically be calculated
/// if not explicitly specified. This means that if you have a [`Size`] that uses [`Val::Undefined`](crate::Val::Undefined)
/// as a width and height, the size would be determined by the window size and the values specified in the position.
///
/// ```
/// # use bevy_ui::{UiRect, Val};
/// #
/// let position = UiRect {
/// left: Val::Px(100.0),
/// right: Val::Px(200.0),
/// top: Val::Px(300.0),
/// bottom: Val::Px(400.0),
/// };
/// ```
///
/// To determine the width of the UI element you have to take the width of the window and subtract it by the
/// left and right values of the position. To determine the height of the UI element you have to take the height
/// of the window and subtract it by the top and bottom values of the position. If we had a window with a width
/// and height of 1000px, the UI element declared above would have a width of 700px and a height of 300px.
///
/// ```
/// // Size of the window
/// let window_width = 1000.0;
/// let window_height = 1000.0;
///
/// // Values of the position
/// let left = 100.0;
/// let right = 200.0;
/// let top = 300.0;
/// let bottom = 400.0;
///
/// // Calculation to get the size of the UI element
/// let ui_element_width = window_width - left - right;
/// let ui_element_height = window_height - top - bottom;
///
/// assert_eq!(ui_element_width, 700.0);
/// assert_eq!(ui_element_height, 300.0);
/// ```
///
/// If you define a [`Size`] and also all four sides of the position, the top and left values of the position
/// are used to determine where to place the UI element. The size will not be calculated using the bottom and
/// right values of the position because the size of the UI element is already explicitly specified.
///
/// ```
/// # use bevy_ui::{UiRect, Size, Val, Style};
/// # use bevy_utils::default;
/// #
/// let style = Style {
/// position: UiRect { // Defining all four sides
/// left: Val::Px(100.0),
/// right: Val::Px(200.0),
/// top: Val::Px(300.0),
/// bottom: Val::Px(400.0),
/// },
/// size: Size::new(Val::Percent(100.0), Val::Percent(50.0)), // but also explicitly specifying a size
/// ..default()
/// };
/// ```
///
/// ## Margin
///
@ -134,10 +61,10 @@ pub struct UiRect {
impl UiRect {
pub const DEFAULT: Self = Self {
left: Val::DEFAULT,
right: Val::DEFAULT,
top: Val::DEFAULT,
bottom: Val::DEFAULT,
left: Val::Px(0.),
right: Val::Px(0.),
top: Val::Px(0.),
bottom: Val::Px(0.),
};
/// Creates a new [`UiRect`] from the values specified.
@ -191,7 +118,8 @@ impl UiRect {
}
}
/// Creates a new [`UiRect`] where `left` and `right` take the given value.
/// Creates a new [`UiRect`] where `left` and `right` take the given value,
/// and `top` and `bottom` set to zero `Val::Px(0.)`.
///
/// # Example
///
@ -202,8 +130,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::Undefined);
/// assert_eq!(ui_rect.bottom, Val::Undefined);
/// assert_eq!(ui_rect.top, Val::Px(0.));
/// assert_eq!(ui_rect.bottom, Val::Px(0.));
/// ```
pub fn horizontal(value: Val) -> Self {
UiRect {
@ -213,7 +141,8 @@ impl UiRect {
}
}
/// Creates a new [`UiRect`] where `top` and `bottom` take the given value.
/// Creates a new [`UiRect`] where `top` and `bottom` take the given value,
/// and `left` and `right` are set to `Val::Px(0.)`.
///
/// # Example
///
@ -222,8 +151,8 @@ impl UiRect {
/// #
/// 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.left, Val::Px(0.));
/// assert_eq!(ui_rect.right, Val::Px(0.));
/// assert_eq!(ui_rect.top, Val::Px(10.0));
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
/// ```
@ -235,7 +164,8 @@ impl UiRect {
}
}
/// Creates a new [`UiRect`] where `left` takes the given value.
/// Creates a new [`UiRect`] where `left` takes the given value, and
/// the other fields are set to `Val::Px(0.)`.
///
/// # Example
///
@ -245,9 +175,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::Undefined);
/// assert_eq!(ui_rect.top, Val::Undefined);
/// assert_eq!(ui_rect.bottom, Val::Undefined);
/// assert_eq!(ui_rect.right, Val::Px(0.));
/// assert_eq!(ui_rect.top, Val::Px(0.));
/// assert_eq!(ui_rect.bottom, Val::Px(0.));
/// ```
pub fn left(value: Val) -> Self {
UiRect {
@ -256,7 +186,8 @@ impl UiRect {
}
}
/// Creates a new [`UiRect`] where `right` takes the given value.
/// Creates a new [`UiRect`] where `right` takes the given value,
/// and the other fields are set to `Val::Px(0.)`.
///
/// # Example
///
@ -265,10 +196,10 @@ impl UiRect {
/// #
/// let ui_rect = UiRect::right(Val::Px(10.0));
///
/// assert_eq!(ui_rect.left, Val::Undefined);
/// assert_eq!(ui_rect.left, Val::Px(0.));
/// assert_eq!(ui_rect.right, Val::Px(10.0));
/// assert_eq!(ui_rect.top, Val::Undefined);
/// assert_eq!(ui_rect.bottom, Val::Undefined);
/// assert_eq!(ui_rect.top, Val::Px(0.));
/// assert_eq!(ui_rect.bottom, Val::Px(0.));
/// ```
pub fn right(value: Val) -> Self {
UiRect {
@ -277,7 +208,8 @@ impl UiRect {
}
}
/// Creates a new [`UiRect`] where `top` takes the given value.
/// Creates a new [`UiRect`] where `top` takes the given value,
/// and the other fields are set to `Val::Px(0.)`.
///
/// # Example
///
@ -286,10 +218,10 @@ impl UiRect {
/// #
/// 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.left, Val::Px(0.));
/// assert_eq!(ui_rect.right, Val::Px(0.));
/// assert_eq!(ui_rect.top, Val::Px(10.0));
/// assert_eq!(ui_rect.bottom, Val::Undefined);
/// assert_eq!(ui_rect.bottom, Val::Px(0.));
/// ```
pub fn top(value: Val) -> Self {
UiRect {
@ -298,7 +230,8 @@ impl UiRect {
}
}
/// Creates a new [`UiRect`] where `bottom` takes the given value.
/// Creates a new [`UiRect`] where `bottom` takes the given value,
/// and the other fields are set to `Val::Px(0.)`.
///
/// # Example
///
@ -307,9 +240,9 @@ impl UiRect {
/// #
/// 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.left, Val::Px(0.));
/// assert_eq!(ui_rect.right, Val::Px(0.));
/// assert_eq!(ui_rect.top, Val::Px(0.));
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
/// ```
pub fn bottom(value: Val) -> Self {
@ -376,7 +309,9 @@ impl Size {
}
}
/// Creates a new [`Size`] where `width` takes the given value and its `height` is `Val::Auto`.
/// Creates a new [`Size`] where `width` takes the given value,
/// and `height` is set to [`Val::Auto`].
///
/// # Example
///
@ -395,7 +330,8 @@ impl Size {
}
}
/// Creates a new [`Size`] where `height` takes the given value and its `width` is `Val::Auto`.
/// Creates a new [`Size`] where `height` takes the given value,
/// and `width` is set to [`Val::Auto`].
///
/// # Example
///
@ -416,9 +352,6 @@ impl Size {
/// Creates a Size where both values are [`Val::Auto`].
pub const AUTO: Self = Self::all(Val::Auto);
/// Creates a Size where both values are [`Val::Undefined`].
pub const UNDEFINED: Self = Self::all(Val::Undefined);
}
impl Default for Size {
@ -481,10 +414,10 @@ mod tests {
assert_eq!(
UiRect::default(),
UiRect {
left: Val::Undefined,
right: Val::Undefined,
top: Val::Undefined,
bottom: Val::Undefined
left: Val::Px(0.),
right: Val::Px(0.),
top: Val::Px(0.),
bottom: Val::Px(0.)
}
);
assert_eq!(UiRect::default(), UiRect::DEFAULT);

View file

@ -61,8 +61,6 @@ impl Default for Node {
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
#[reflect(PartialEq, Serialize, Deserialize)]
pub enum Val {
/// No value defined
Undefined,
/// Automatically determine this value
Auto,
/// Set this value in pixels
@ -72,7 +70,7 @@ pub enum Val {
}
impl Val {
pub const DEFAULT: Self = Self::Undefined;
pub const DEFAULT: Self = Self::Auto;
}
impl Default for Val {
@ -86,7 +84,6 @@ impl Mul<f32> for Val {
fn mul(self, rhs: f32) -> Self::Output {
match self {
Val::Undefined => Val::Undefined,
Val::Auto => Val::Auto,
Val::Px(value) => Val::Px(value * rhs),
Val::Percent(value) => Val::Percent(value * rhs),
@ -97,7 +94,7 @@ impl Mul<f32> for Val {
impl MulAssign<f32> for Val {
fn mul_assign(&mut self, rhs: f32) {
match self {
Val::Undefined | Val::Auto => {}
Val::Auto => {}
Val::Px(value) | Val::Percent(value) => *value *= rhs,
}
}
@ -108,7 +105,6 @@ impl Div<f32> for Val {
fn div(self, rhs: f32) -> Self::Output {
match self {
Val::Undefined => Val::Undefined,
Val::Auto => Val::Auto,
Val::Px(value) => Val::Px(value / rhs),
Val::Percent(value) => Val::Percent(value / rhs),
@ -119,7 +115,7 @@ impl Div<f32> for Val {
impl DivAssign<f32> for Val {
fn div_assign(&mut self, rhs: f32) {
match self {
Val::Undefined | Val::Auto => {}
Val::Auto => {}
Val::Px(value) | Val::Percent(value) => *value /= rhs,
}
}
@ -139,7 +135,7 @@ impl Val {
/// When adding non-numeric [`Val`]s, it returns the value unchanged.
pub fn try_add(&self, rhs: Val) -> Result<Val, ValArithmeticError> {
match (self, rhs) {
(Val::Undefined, Val::Undefined) | (Val::Auto, Val::Auto) => Ok(*self),
(Val::Auto, Val::Auto) => Ok(*self),
(Val::Px(value), Val::Px(rhs_value)) => Ok(Val::Px(value + rhs_value)),
(Val::Percent(value), Val::Percent(rhs_value)) => Ok(Val::Percent(value + rhs_value)),
_ => Err(ValArithmeticError::NonIdenticalVariants),
@ -157,7 +153,7 @@ impl Val {
/// When adding non-numeric [`Val`]s, it returns the value unchanged.
pub fn try_sub(&self, rhs: Val) -> Result<Val, ValArithmeticError> {
match (self, rhs) {
(Val::Undefined, Val::Undefined) | (Val::Auto, Val::Auto) => Ok(*self),
(Val::Auto, Val::Auto) => Ok(*self),
(Val::Px(value), Val::Px(rhs_value)) => Ok(Val::Px(value - rhs_value)),
(Val::Percent(value), Val::Percent(rhs_value)) => Ok(Val::Percent(value - rhs_value)),
_ => Err(ValArithmeticError::NonIdenticalVariants),
@ -236,6 +232,10 @@ pub struct Style {
pub display: Display,
/// Whether to arrange this node relative to other nodes, or positioned absolutely
pub position_type: PositionType,
pub left: Val,
pub right: Val,
pub top: Val,
pub bottom: Val,
/// Which direction the content of this node should go
pub direction: Direction,
/// Whether to use column or row layout
@ -252,8 +252,6 @@ pub struct Style {
pub align_content: AlignContent,
/// How items align according to the main axis
pub justify_content: JustifyContent,
/// The position of the node as described by its Rect
pub position: UiRect,
/// The amount of space around a node outside its border.
///
/// If a percentage value is used, the percentage is calculated based on the width of the parent node.
@ -329,7 +327,7 @@ pub struct Style {
pub overflow: Overflow,
/// The size of the gutters between the rows and columns of the flexbox layout
///
/// Values of `Size::UNDEFINED` and `Size::AUTO` are treated as zero.
/// A value of `Size::AUTO` is treated as zero.
pub gap: Size,
}
@ -337,6 +335,10 @@ impl Style {
pub const DEFAULT: Self = Self {
display: Display::DEFAULT,
position_type: PositionType::DEFAULT,
left: Val::Auto,
right: Val::Auto,
top: Val::Auto,
bottom: Val::Auto,
direction: Direction::DEFAULT,
flex_direction: FlexDirection::DEFAULT,
flex_wrap: FlexWrap::DEFAULT,
@ -344,7 +346,6 @@ impl Style {
align_self: AlignSelf::DEFAULT,
align_content: AlignContent::DEFAULT,
justify_content: JustifyContent::DEFAULT,
position: UiRect::DEFAULT,
margin: UiRect::DEFAULT,
padding: UiRect::DEFAULT,
border: UiRect::DEFAULT,
@ -356,7 +357,7 @@ impl Style {
max_size: Size::AUTO,
aspect_ratio: None,
overflow: Overflow::DEFAULT,
gap: Size::UNDEFINED,
gap: Size::AUTO,
};
}
@ -769,12 +770,10 @@ mod tests {
#[test]
fn val_try_add() {
let undefined_sum = Val::Undefined.try_add(Val::Undefined).unwrap();
let auto_sum = Val::Auto.try_add(Val::Auto).unwrap();
let px_sum = Val::Px(20.).try_add(Val::Px(22.)).unwrap();
let percent_sum = Val::Percent(50.).try_add(Val::Percent(50.)).unwrap();
assert_eq!(undefined_sum, Val::Undefined);
assert_eq!(auto_sum, Val::Auto);
assert_eq!(px_sum, Val::Px(42.));
assert_eq!(percent_sum, Val::Percent(100.));
@ -791,12 +790,10 @@ mod tests {
#[test]
fn val_try_sub() {
let undefined_sum = Val::Undefined.try_sub(Val::Undefined).unwrap();
let auto_sum = Val::Auto.try_sub(Val::Auto).unwrap();
let px_sum = Val::Px(72.).try_sub(Val::Px(30.)).unwrap();
let percent_sum = Val::Percent(100.).try_sub(Val::Percent(50.)).unwrap();
assert_eq!(undefined_sum, Val::Undefined);
assert_eq!(auto_sum, Val::Auto);
assert_eq!(px_sum, Val::Px(42.));
assert_eq!(percent_sum, Val::Percent(50.));
@ -804,9 +801,8 @@ mod tests {
#[test]
fn different_variant_val_try_add() {
let different_variant_sum_1 = Val::Undefined.try_add(Val::Auto);
let different_variant_sum_2 = Val::Px(50.).try_add(Val::Percent(50.));
let different_variant_sum_3 = Val::Percent(50.).try_add(Val::Undefined);
let different_variant_sum_1 = Val::Px(50.).try_add(Val::Percent(50.));
let different_variant_sum_2 = Val::Percent(50.).try_add(Val::Auto);
assert_eq!(
different_variant_sum_1,
@ -816,17 +812,12 @@ mod tests {
different_variant_sum_2,
Err(ValArithmeticError::NonIdenticalVariants)
);
assert_eq!(
different_variant_sum_3,
Err(ValArithmeticError::NonIdenticalVariants)
);
}
#[test]
fn different_variant_val_try_sub() {
let different_variant_diff_1 = Val::Undefined.try_sub(Val::Auto);
let different_variant_diff_2 = Val::Px(50.).try_sub(Val::Percent(50.));
let different_variant_diff_3 = Val::Percent(50.).try_sub(Val::Undefined);
let different_variant_diff_1 = Val::Px(50.).try_sub(Val::Percent(50.));
let different_variant_diff_2 = Val::Percent(50.).try_sub(Val::Auto);
assert_eq!(
different_variant_diff_1,
@ -836,10 +827,6 @@ mod tests {
different_variant_diff_2,
Err(ValArithmeticError::NonIdenticalVariants)
);
assert_eq!(
different_variant_diff_3,
Err(ValArithmeticError::NonIdenticalVariants)
);
}
#[test]
@ -861,10 +848,8 @@ mod tests {
#[test]
fn val_invalid_evaluation() {
let size = 250.;
let evaluate_undefined = Val::Undefined.evaluate(size);
let evaluate_auto = Val::Auto.evaluate(size);
assert_eq!(evaluate_undefined, Err(ValArithmeticError::NonEvaluateable));
assert_eq!(evaluate_auto, Err(ValArithmeticError::NonEvaluateable));
}
@ -906,10 +891,8 @@ mod tests {
fn val_try_add_non_numeric_with_size() {
let size = 250.;
let undefined_sum = Val::Undefined.try_add_with_size(Val::Undefined, size);
let percent_sum = Val::Auto.try_add_with_size(Val::Auto, size);
assert_eq!(undefined_sum, Err(ValArithmeticError::NonEvaluateable));
assert_eq!(percent_sum, Err(ValArithmeticError::NonEvaluateable));
}
@ -924,4 +907,9 @@ mod tests {
"the given variant of Val is not evaluateable (non-numeric)"
);
}
#[test]
fn default_val_equals_const_default_val() {
assert_eq!(Val::default(), Val::DEFAULT);
}
}

View file

@ -25,9 +25,7 @@ pub fn text_constraint(min_size: Val, size: Val, max_size: Val, scale_factor: f6
match (min_size, size, max_size) {
(_, _, Val::Px(max)) => scale_value(max, scale_factor),
(Val::Px(min), _, _) => scale_value(min, scale_factor),
(Val::Undefined, Val::Px(size), Val::Undefined) | (Val::Auto, Val::Px(size), Val::Auto) => {
scale_value(size, scale_factor)
}
(Val::Auto, Val::Px(size), Val::Auto) => scale_value(size, scale_factor),
_ => f32::MAX,
}
}

View file

@ -78,11 +78,8 @@ fn setup(
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(10.0),
left: Val::Px(10.0),
..default()
},
bottom: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),
);

View file

@ -101,11 +101,8 @@ fn setup_instructions(mut commands: Commands, asset_server: Res<AssetServer>) {
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(10.0),
left: Val::Px(10.0),
..default()
},
bottom: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),));
}

View file

@ -203,23 +203,16 @@ fn setup(
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
},
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),
);
commands.spawn((
TextBundle::from_section("", text_style).with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(10.0),
right: Val::Px(10.0),
..default()
},
top: Val::Px(10.0),
right: Val::Px(10.0),
..default()
}),
ExampleDisplay,
@ -365,8 +358,8 @@ fn example_control_system(
.world_to_viewport(camera_global_transform, world_position)
.unwrap();
style.position.bottom = Val::Px(viewport_position.y);
style.position.left = Val::Px(viewport_position.x);
style.bottom = Val::Px(viewport_position.y);
style.left = Val::Px(viewport_position.x);
}
let mut display = display.single_mut();

View file

@ -106,11 +106,8 @@ fn setup_scene(
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(10.0),
left: Val::Px(10.0),
..default()
},
bottom: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),
);

View file

@ -145,11 +145,8 @@ fn setup_instructions(mut commands: Commands, asset_server: Res<AssetServer>) {
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
},
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),));
}

View file

@ -84,11 +84,8 @@ fn setup(
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(20.0),
left: Val::Px(100.0),
..default()
},
top: Val::Px(20.0),
left: Val::Px(100.0),
..default()
}),
);
@ -104,11 +101,8 @@ fn setup(
),
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(130.0),
right: Val::Px(0.0),
..default()
},
top: Val::Px(130.0),
right: Val::Px(0.0),
..default()
},
transform: Transform {
@ -129,11 +123,8 @@ fn setup(
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(20.0),
right: Val::Px(20.0),
..default()
},
bottom: Val::Px(20.0),
right: Val::Px(20.0),
..default()
}),
EnvironmentMapLabel,

View file

@ -76,11 +76,8 @@ fn setup(
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
},
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),
);

View file

@ -173,11 +173,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
},
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
}),
);

View file

@ -240,11 +240,8 @@ fn setup(
])
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: SCOREBOARD_TEXT_PADDING,
left: SCOREBOARD_TEXT_PADDING,
..default()
},
top: SCOREBOARD_TEXT_PADDING,
left: SCOREBOARD_TEXT_PADDING,
..default()
}),
);

View file

@ -401,12 +401,8 @@ mod menu {
// This takes the icons out of the flexbox flow, to be positioned exactly
position_type: PositionType::Absolute,
// The icon will be close to the left border of the button
position: UiRect {
left: Val::Px(10.0),
right: Val::Auto,
top: Val::Auto,
bottom: Val::Auto,
},
left: Val::Px(10.0),
right: Val::Auto,
..default()
};
let button_text_style = TextStyle {

View file

@ -78,11 +78,8 @@ fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
])
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
},
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),
);

View file

@ -100,12 +100,10 @@ fn setup_scene(
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(50.0),
right: Val::Px(50.0),
top: Val::Auto,
bottom: Val::Px(50.0),
},
left: Val::Px(50.0),
right: Val::Px(50.0),
top: Val::Auto,
bottom: Val::Px(50.0),
..default()
},
..default()

View file

@ -140,11 +140,8 @@ fn setup(
])
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
},
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),
);

View file

@ -121,11 +121,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
])
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
},
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
}),
StatsText,

View file

@ -89,12 +89,8 @@ fn spawn_button(
ButtonBundle {
style: Style {
size: Size::new(Val::Percent(width), Val::Percent(width)),
position: UiRect {
bottom: Val::Percent(100.0 / total * i as f32),
left: Val::Percent(100.0 / total * j as f32),
..default()
},
bottom: Val::Percent(100.0 / total * i as f32),
left: Val::Percent(100.0 / total * j as f32),
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
..default()

View file

@ -49,11 +49,8 @@ fn atlas_render_system(
image: texture_atlas.texture.clone().into(),
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(0.0),
left: Val::Px(512.0 * x_offset),
..default()
},
top: Val::Px(0.0),
left: Val::Px(512.0 * x_offset),
..default()
},
..default()
@ -85,10 +82,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut state: ResM
background_color: Color::NONE.into(),
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(0.0),
..default()
},
bottom: Val::Px(0.0),
..default()
},
..default()

View file

@ -43,11 +43,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Set the style of the TextBundle itself.
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(5.0),
right: Val::Px(15.0),
..default()
},
bottom: Val::Px(5.0),
right: Val::Px(15.0),
..default()
}),
ColorText,

View file

@ -37,11 +37,8 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(5.0),
left: Val::Px(15.0),
..default()
},
top: Val::Px(5.0),
left: Val::Px(15.0),
..default()
}),
);
@ -56,15 +53,9 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
.with_text_alignment(TextAlignment::Center)
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(5.0),
right: Val::Px(15.0),
..default()
},
max_size: Size {
width: Val::Px(400.),
height: Val::Undefined,
},
top: Val::Px(5.0),
right: Val::Px(15.0),
max_size: Size::width(Val::Px(400.)),
..default()
})
);
@ -115,11 +106,8 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
])
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(5.0),
right: Val::Px(15.0),
..default()
},
bottom: Val::Px(5.0),
right: Val::Px(15.0),
..default()
}),
TextChanges,
@ -136,11 +124,8 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
.with_style(Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(5.0),
left: Val::Px(15.0),
..default()
},
bottom: Val::Px(5.0),
left: Val::Px(15.0),
size: Size {
width: Val::Px(200.0),
..default()

View file

@ -27,7 +27,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands
.spawn(NodeBundle {
style: Style {
size: Size::width(Val::Percent(100.0)),
size: Size::all(Val::Percent(100.)),
justify_content: JustifyContent::SpaceBetween,
..default()
},
@ -38,8 +38,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
parent
.spawn(NodeBundle {
style: Style {
size: Size::width(Val::Px(200.0)),
border: UiRect::all(Val::Px(2.0)),
size: Size::width(Val::Px(200.)),
border: UiRect::all(Val::Px(2.)),
..default()
},
background_color: Color::rgb(0.65, 0.65, 0.65).into(),
@ -50,7 +50,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
parent
.spawn(NodeBundle {
style: Style {
size: Size::width(Val::Percent(100.0)),
size: Size::width(Val::Percent(100.)),
..default()
},
background_color: Color::rgb(0.15, 0.15, 0.15).into(),
@ -68,7 +68,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
)
.with_style(Style {
margin: UiRect::all(Val::Px(5.0)),
margin: UiRect::all(Val::Px(5.)),
..default()
}),
// Because this is a distinct label widget and
@ -85,7 +85,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
size: Size::width(Val::Px(200.0)),
size: Size::width(Val::Px(200.)),
..default()
},
background_color: Color::rgb(0.15, 0.15, 0.15).into(),
@ -101,11 +101,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 25.,
color: Color::WHITE,
},
)
.with_style(Style {
size: Size::height(Val::Px(25.)),
..default()
}),
),
Label,
));
// List with hidden overflow
@ -114,7 +110,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
flex_direction: FlexDirection::Column,
align_self: AlignSelf::Stretch,
size: Size::height(Val::Percent(50.0)),
size: Size::height(Val::Percent(50.)),
overflow: Overflow::Hidden,
..default()
},
@ -129,7 +125,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
flex_direction: FlexDirection::Column,
flex_grow: 1.0,
max_size: Size::UNDEFINED,
align_items: AlignItems::Center,
..default()
},
@ -153,7 +148,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
)
.with_style(Style {
flex_shrink: 0.,
size: Size::new(Val::Undefined, Val::Px(20.)),
size: Size::height(Val::Px(20.)),
..default()
}),
Label,
@ -166,26 +161,23 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
parent
.spawn(NodeBundle {
style: Style {
size: Size::new(Val::Px(200.0), Val::Px(200.0)),
size: Size::all(Val::Px(200.)),
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(210.0),
bottom: Val::Px(10.0),
..default()
},
border: UiRect::all(Val::Px(20.0)),
left: Val::Px(210.),
bottom: Val::Px(10.),
border: UiRect::all(Val::Px(20.)),
..default()
},
background_color: Color::rgb(0.4, 0.4, 1.0).into(),
background_color: Color::rgb(0.4, 0.4, 1.).into(),
..default()
})
.with_children(|parent| {
parent.spawn(NodeBundle {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
size: Size::all(Val::Percent(100.)),
..default()
},
background_color: Color::rgb(0.8, 0.8, 1.0).into(),
background_color: Color::rgb(0.8, 0.8, 1.).into(),
..default()
});
});
@ -193,7 +185,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
parent
.spawn(NodeBundle {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
size: Size::all(Val::Percent(100.)),
position_type: PositionType::Absolute,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
@ -205,10 +197,10 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
parent
.spawn(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
size: Size::all(Val::Px(100.)),
..default()
},
background_color: Color::rgb(1.0, 0.0, 0.0).into(),
background_color: Color::rgb(1.0, 0.0, 0.).into(),
..default()
})
.with_children(|parent| {
@ -217,11 +209,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Take the size of the parent node.
size: Size::all(Val::Percent(100.)),
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(20.0),
bottom: Val::Px(20.0),
..default()
},
left: Val::Px(20.),
bottom: Val::Px(20.),
..default()
},
background_color: Color::rgb(1.0, 0.3, 0.3).into(),
@ -231,11 +220,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
size: Size::all(Val::Percent(100.)),
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(40.0),
bottom: Val::Px(40.0),
..default()
},
left: Val::Px(40.),
bottom: Val::Px(40.),
..default()
},
background_color: Color::rgb(1.0, 0.5, 0.5).into(),
@ -245,11 +231,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
size: Size::all(Val::Percent(100.)),
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(60.0),
bottom: Val::Px(60.0),
..default()
},
left: Val::Px(60.),
bottom: Val::Px(60.),
..default()
},
background_color: Color::rgb(1.0, 0.7, 0.7).into(),
@ -260,11 +243,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
style: Style {
size: Size::all(Val::Percent(100.)),
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(80.0),
bottom: Val::Px(80.0),
..default()
},
left: Val::Px(80.),
bottom: Val::Px(80.),
..default()
},
background_color: Color::rgba(1.0, 0.9, 0.9, 0.4).into(),
@ -286,6 +266,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
})
.with_children(|parent| {
// bevy logo (image)
parent
.spawn(ImageBundle {
style: Style {
@ -327,7 +308,7 @@ fn mouse_scroll(
};
scrolling_list.position += dy;
scrolling_list.position = scrolling_list.position.clamp(-max_scroll, 0.);
style.position.top = Val::Px(scrolling_list.position);
style.top = Val::Px(scrolling_list.position);
}
}
}

View file

@ -38,11 +38,8 @@ fn setup(mut commands: Commands, asset_server: ResMut<AssetServer>) {
style: Style {
size: Size::new(Val::Percent(50.0), Val::Percent(50.0)),
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Percent(25.),
top: Val::Percent(25.),
..default()
},
left: Val::Percent(25.),
top: Val::Percent(25.),
justify_content: JustifyContent::SpaceAround,
align_items: AlignItems::Center,
..default()

View file

@ -40,11 +40,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Set the style of the TextBundle itself.
.with_style(Style {
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(5.),
right: Val::Px(10.),
..default()
},
bottom: Val::Px(5.),
right: Val::Px(10.),
..default()
}),
));

View file

@ -45,11 +45,8 @@ fn setup(mut commands: Commands) {
background_color: Color::RED.into(),
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(10.0),
bottom: Val::Px(40.0),
..default()
},
left: Val::Px(10.0),
bottom: Val::Px(40.0),
size: Size::new(Val::Px(100.0), Val::Px(50.0)),
..default()
},
@ -63,11 +60,8 @@ fn setup(mut commands: Commands) {
background_color: Color::BLUE.into(),
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(45.0),
bottom: Val::Px(30.0),
..default()
},
left: Val::Px(45.0),
bottom: Val::Px(30.0),
size: Size::new(Val::Px(100.0), Val::Px(50.0)),
..default()
},
@ -81,11 +75,8 @@ fn setup(mut commands: Commands) {
background_color: Color::GREEN.into(),
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(70.0),
bottom: Val::Px(20.0),
..default()
},
left: Val::Px(70.0),
bottom: Val::Px(20.0),
size: Size::new(Val::Px(100.0), Val::Px(75.0)),
..default()
},
@ -100,11 +91,8 @@ fn setup(mut commands: Commands) {
background_color: Color::PURPLE.into(),
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(15.0),
bottom: Val::Px(10.0),
..default()
},
left: Val::Px(15.0),
bottom: Val::Px(10.0),
size: Size::new(Val::Px(100.0), Val::Px(60.0)),
..default()
},
@ -119,11 +107,8 @@ fn setup(mut commands: Commands) {
background_color: Color::YELLOW.into(),
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(-15.0),
bottom: Val::Px(-15.0),
..default()
},
left: Val::Px(-15.0),
bottom: Val::Px(-15.0),
size: Size::new(Val::Px(100.0), Val::Px(125.0)),
..default()
},

View file

@ -201,11 +201,8 @@ pub(crate) mod test_setup {
.with_style(Style {
align_self: AlignSelf::FlexStart,
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
},
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
}),
ModeText,