From 820f3f3b07fa9d74a52f82469a2b5583cba79c91 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Fri, 5 May 2023 13:29:54 +0100 Subject: [PATCH 1/5] Flatten Style properties using Size --- crates/bevy_ui/src/layout/convert.rs | 63 ++++++++++++---------------- crates/bevy_ui/src/ui_node.rs | 60 ++++++++++++++++---------- 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/crates/bevy_ui/src/layout/convert.rs b/crates/bevy_ui/src/layout/convert.rs index 41b2fbcffb732..d3fe37679849d 100644 --- a/crates/bevy_ui/src/layout/convert.rs +++ b/crates/bevy_ui/src/layout/convert.rs @@ -3,8 +3,8 @@ use taffy::style_helpers; use crate::{ AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, GridAutoFlow, GridPlacement, GridTrack, GridTrackRepetition, JustifyContent, JustifyItems, JustifySelf, - MaxTrackSizingFunction, MinTrackSizingFunction, PositionType, RepeatedGridTrack, Size, Style, - UiRect, Val, + MaxTrackSizingFunction, MinTrackSizingFunction, PositionType, RepeatedGridTrack, Style, UiRect, + Val, }; use super::LayoutContext; @@ -63,15 +63,6 @@ impl UiRect { } } -impl Size { - fn map_to_taffy_size(self, map_fn: impl Fn(Val) -> T) -> taffy::geometry::Size { - taffy::geometry::Size { - width: map_fn(self.width), - height: map_fn(self.height), - } - } -} - pub fn from_style(context: &LayoutContext, style: &Style) -> taffy::style::Style { taffy::style::Style { display: style.display.into(), @@ -102,17 +93,23 @@ pub fn from_style(context: &LayoutContext, style: &Style) -> taffy::style::Style flex_grow: style.flex_grow, flex_shrink: style.flex_shrink, flex_basis: style.flex_basis.into_dimension(context), - size: style.size.map_to_taffy_size(|s| s.into_dimension(context)), - min_size: style - .min_size - .map_to_taffy_size(|s| s.into_dimension(context)), - max_size: style - .max_size - .map_to_taffy_size(|s| s.into_dimension(context)), + size: taffy::prelude::Size { + width: style.width.into_dimension(context), + height: style.height.into_dimension(context), + }, + min_size: taffy::prelude::Size { + width: style.min_width.into_dimension(context), + height: style.min_height.into_dimension(context), + }, + max_size: taffy::prelude::Size { + width: style.max_width.into_dimension(context), + height: style.max_height.into_dimension(context), + }, aspect_ratio: style.aspect_ratio, - gap: style - .gap - .map_to_taffy_size(|s| s.into_length_percentage(context)), + gap: taffy::prelude::Size { + width: style.column_gap.into_length_percentage(context), + height: style.row_gap.into_length_percentage(context), + }, grid_auto_flow: style.grid_auto_flow.into(), grid_template_rows: style .grid_template_rows @@ -439,24 +436,16 @@ mod tests { 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.), - }, + width: Val::Px(0.), + height: Val::Auto, + min_width: Val::Px(0.), + min_height: Val::Percent(0.), + max_width: Val::Auto, + max_height: Val::Px(0.), aspect_ratio: None, overflow: crate::Overflow::clip(), - gap: Size { - width: Val::Px(0.), - height: Val::Percent(0.), - }, + column_gap: Val::Px(0.), + row_gap: Val::Percent(0.), grid_auto_flow: GridAutoFlow::ColumnDense, grid_template_rows: vec![ GridTrack::px(10.0), diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index fd551d3d365e8..97562f04fe0fe 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -1,4 +1,4 @@ -use crate::{Size, UiRect}; +use crate::UiRect; use bevy_asset::Handle; use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_math::{Rect, Vec2}; @@ -336,32 +336,35 @@ pub struct Style { /// pub bottom: Val, - /// The ideal size of the node + /// The ideal width of the node. `width` is used when it is within the bounds defined by `min_width` and `max_width`. /// - /// `size.width` is used when it is within the bounds defined by `min_size.width` and `max_size.width`. - /// `size.height` is used when it is within the bounds defined by `min_size.height` and `max_size.height`. + /// + pub width: Val, + + /// The ideal height of the node. `height` is used when it is within the bounds defined by `min_height` and `max_height`. /// - ///
/// - pub size: Size, + pub height: Val, - /// The minimum size of the node + /// The minimum width of the node. `min_width` is used if it is greater than either `width` and/or `max_width`. /// - /// `min_size.width` is used if it is greater than either `size.width` or `max_size.width`, or both. - /// `min_size.height` is used if it is greater than either `size.height` or `max_size.height`, or both. + /// + pub min_width: Val, + + /// The minimum height of the node. `min_height` is used if it is greater than either `height` and/or `max_height`. /// - ///
/// - pub min_size: Size, + pub min_height: Val, - /// The maximum size of the node + /// The maximum width of the node. `max_width` is used if it is within the bounds defined by `min_width` and `width`. /// - /// `max_size.width` is used if it is within the bounds defined by `min_size.width` and `size.width`. - /// `max_size.height` is used if it is within the bounds defined by `min_size.height` and `size.height. + /// + pub max_width: Val, + + /// The maximum height of the node. `max_height` is used if it is within the bounds defined by `min_height` and `height`. /// - ///
/// - pub max_size: Size, + pub max_height: Val, /// The aspect ratio of the node (defined as `width / height`) /// @@ -506,12 +509,19 @@ pub struct Style { /// pub flex_basis: Val, - /// The size of the gutters between items in flexbox layout or rows/columns in a grid layout + /// The size of the gutters between items in a vertical flexbox layout or between rows in a grid layout + /// + /// Note: Values of `Val::Auto` are not valid and are treated as zero. + /// + /// + pub row_gap: Val, + + /// The size of the gutters between items in a horizontal flexbox layout or between column in a grid layout /// /// Note: Values of `Val::Auto` are not valid and are treated as zero. /// - /// - pub gap: Size, + /// + pub column_gap: Val, /// Controls whether automatically placed grid items are placed row-wise or column-wise. And whether the sparse or dense packing algorithm is used. /// Only affect Grid layouts @@ -576,12 +586,16 @@ impl Style { flex_grow: 0.0, flex_shrink: 1.0, flex_basis: Val::Auto, - size: Size::AUTO, - min_size: Size::AUTO, - max_size: Size::AUTO, + width: Val::Auto, + height: Val::Auto, + min_width: Val::Auto, + min_height: Val::Auto, + max_width: Val::Auto, + max_height: Val::Auto, aspect_ratio: None, overflow: Overflow::DEFAULT, - gap: Size::all(Val::Px(0.0)), + row_gap: Val::Px(0.0), + column_gap: Val::Px(0.0), grid_auto_flow: GridAutoFlow::DEFAULT, grid_template_rows: Vec::new(), grid_template_columns: Vec::new(), From cc1a2757ea999b06bc6db5beee88b50544b8da66 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Fri, 5 May 2023 13:31:18 +0100 Subject: [PATCH 2/5] Remove Size struct --- crates/bevy_ui/src/geometry.rs | 232 --------------------------------- crates/bevy_ui/src/lib.rs | 1 - 2 files changed, 233 deletions(-) diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 363c8acf698a5..796be56f01f48 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -1,6 +1,5 @@ use crate::Val; use bevy_reflect::{FromReflect, Reflect, ReflectFromReflect}; -use std::ops::{Div, DivAssign, Mul, MulAssign}; /// A type which is commonly used to define margins, paddings and borders. /// @@ -282,152 +281,6 @@ impl Default for UiRect { } } -/// A 2-dimensional area defined by a width and height. -/// -/// It is commonly used to define the size of a text or UI element. -#[derive(Copy, Clone, PartialEq, Debug, Reflect, FromReflect)] -#[reflect(FromReflect, PartialEq)] -pub struct Size { - /// The width of the 2-dimensional area. - pub width: Val, - /// The height of the 2-dimensional area. - pub height: Val, -} - -impl Size { - pub const DEFAULT: Self = Self::AUTO; - - /// Creates a new [`Size`] from a width and a height. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Size, Val}; - /// # - /// let size = Size::new(Val::Px(100.0), Val::Px(200.0)); - /// - /// assert_eq!(size.width, Val::Px(100.0)); - /// assert_eq!(size.height, Val::Px(200.0)); - /// ``` - pub const fn new(width: Val, height: Val) -> Self { - Size { width, height } - } - - /// Creates a new [`Size`] where both sides take the given value. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Size, Val}; - /// # - /// let size = Size::all(Val::Px(10.)); - /// - /// assert_eq!(size.width, Val::Px(10.0)); - /// assert_eq!(size.height, Val::Px(10.0)); - /// ``` - pub const fn all(value: Val) -> Self { - Self { - width: value, - height: value, - } - } - - /// Creates a new [`Size`] where `width` takes the given value, - /// and `height` is set to [`Val::Auto`]. - - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Size, Val}; - /// # - /// let size = Size::width(Val::Px(10.)); - /// - /// assert_eq!(size.width, Val::Px(10.0)); - /// assert_eq!(size.height, Val::Auto); - /// ``` - pub const fn width(width: Val) -> Self { - Self { - width, - height: Val::Auto, - } - } - - /// Creates a new [`Size`] where `height` takes the given value, - /// and `width` is set to [`Val::Auto`]. - /// - /// # Example - /// - /// ``` - /// # use bevy_ui::{Size, Val}; - /// # - /// let size = Size::height(Val::Px(10.)); - /// - /// assert_eq!(size.width, Val::Auto); - /// assert_eq!(size.height, Val::Px(10.)); - /// ``` - pub const fn height(height: Val) -> Self { - Self { - width: Val::Auto, - height, - } - } - - /// Creates a Size where both values are [`Val::Auto`]. - pub const AUTO: Self = Self::all(Val::Auto); -} - -impl Default for Size { - fn default() -> Self { - Self::DEFAULT - } -} - -impl From<(Val, Val)> for Size { - fn from(vals: (Val, Val)) -> Self { - Self { - width: vals.0, - height: vals.1, - } - } -} - -impl Mul for Size { - type Output = Size; - - fn mul(self, rhs: f32) -> Self::Output { - Self::Output { - width: self.width * rhs, - height: self.height * rhs, - } - } -} - -impl MulAssign for Size { - fn mul_assign(&mut self, rhs: f32) { - self.width *= rhs; - self.height *= rhs; - } -} - -impl Div for Size { - type Output = Size; - - fn div(self, rhs: f32) -> Self::Output { - Self::Output { - width: self.width / rhs, - height: self.height / rhs, - } - } -} - -impl DivAssign for Size { - fn div_assign(&mut self, rhs: f32) { - self.width /= rhs; - self.height /= rhs; - } -} - #[cfg(test)] mod tests { use super::*; @@ -446,91 +299,6 @@ mod tests { assert_eq!(UiRect::default(), UiRect::DEFAULT); } - #[test] - fn test_size_from() { - let size: Size = (Val::Px(20.), Val::Px(30.)).into(); - - assert_eq!( - size, - Size { - width: Val::Px(20.), - height: Val::Px(30.), - } - ); - } - - #[test] - fn test_size_mul() { - assert_eq!(Size::all(Val::Px(10.)) * 2., Size::all(Val::Px(20.))); - - let mut size = Size::all(Val::Px(10.)); - size *= 2.; - assert_eq!(size, Size::all(Val::Px(20.))); - } - - #[test] - fn test_size_div() { - assert_eq!( - Size::new(Val::Px(20.), Val::Px(20.)) / 2., - Size::new(Val::Px(10.), Val::Px(10.)) - ); - - let mut size = Size::new(Val::Px(20.), Val::Px(20.)); - size /= 2.; - assert_eq!(size, Size::new(Val::Px(10.), Val::Px(10.))); - } - - #[test] - fn test_size_all() { - let length = Val::Px(10.); - - assert_eq!( - Size::all(length), - Size { - width: length, - height: length - } - ); - } - - #[test] - fn test_size_width() { - let width = Val::Px(10.); - - assert_eq!( - Size::width(width), - Size { - width, - ..Default::default() - } - ); - } - - #[test] - fn test_size_height() { - let height = Val::Px(7.); - - assert_eq!( - Size::height(height), - Size { - height, - ..Default::default() - } - ); - } - - #[test] - fn size_default_equals_const_default() { - assert_eq!( - Size::default(), - Size { - width: Val::Auto, - height: Val::Auto - } - ); - assert_eq!(Size::default(), Size::DEFAULT); - } - #[test] fn test_uirect_axes() { let x = Val::Px(1.); diff --git a/crates/bevy_ui/src/lib.rs b/crates/bevy_ui/src/lib.rs index b7ec10fb290b5..3cc3c9b157485 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -109,7 +109,6 @@ impl Plugin for UiPlugin { .register_type::() .register_type::() .register_type::() - .register_type::() .register_type::() .register_type::