From 684b32a326f49d290bd2e5694a47cad168fb75f1 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Wed, 8 Feb 2023 14:14:57 +0000 Subject: [PATCH 01/26] changes: Added a type `Breadth`, that is similar to `Val` but with only evaluatable variants. Gave UiRect a type parameter, so it can take `Breadth` or `Val` values. Added tests for `Breadth` and `UiRect`. Changed style properties to use `Breadth` instead of `Val` for the padding and border properties. Changed `bevy_ui::flex::convert::from_rect` to take an `UiRect>` instead of a `UiRect`. Made minimal necessary changes to the examples. --- crates/bevy_ui/src/flex/convert.rs | 12 +- crates/bevy_ui/src/geometry.rs | 88 +++--- crates/bevy_ui/src/ui_node.rs | 323 ++++++++++++++++++++++- examples/ui/text_layout.rs | 8 +- examples/ui/ui.rs | 4 +- examples/window/scale_factor_override.rs | 2 +- 6 files changed, 384 insertions(+), 53 deletions(-) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index c60feb97cd591..ad5f1c87aa690 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -3,15 +3,15 @@ use crate::{ PositionType, Size, Style, UiRect, Val, }; -pub fn from_rect( +pub fn from_rect + Default + PartialEq + Copy + Clone + PartialEq>( scale_factor: f64, - rect: UiRect, + rect: UiRect, ) -> taffy::geometry::Rect { taffy::geometry::Rect { - left: from_val(scale_factor, rect.left), - right: from_val(scale_factor, rect.right), - top: from_val(scale_factor, rect.top), - bottom: from_val(scale_factor, rect.bottom), + left: from_val(scale_factor, rect.left.into()), + right: from_val(scale_factor, rect.right.into()), + top: from_val(scale_factor, rect.top.into()), + bottom: from_val(scale_factor, rect.bottom.into()), } } diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 8f80c346e5682..0c75a521931de 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -1,4 +1,4 @@ -use crate::Val; +use crate::{Breadth, Val}; use bevy_reflect::Reflect; use std::ops::{Div, DivAssign, Mul, MulAssign}; @@ -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), /// }; /// ``` /// @@ -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 { /// 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 UiRect +where + UiRect: Default, + T: Default + Copy + Clone + PartialEq, +{ /// /// # Example /// @@ -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, @@ -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, @@ -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, @@ -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, @@ -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() @@ -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() @@ -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() @@ -312,7 +308,7 @@ 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() @@ -320,7 +316,31 @@ impl UiRect { } } -impl Default for UiRect { +impl UiRect { + pub const DEFAULT: Self = Self { + left: Val::DEFAULT, + right: Val::DEFAULT, + top: Val::DEFAULT, + bottom: Val::DEFAULT, + }; +} + +impl Default for UiRect { + fn default() -> Self { + Self::DEFAULT + } +} + +impl UiRect { + pub const DEFAULT: Self = Self { + left: Breadth::DEFAULT, + right: Breadth::DEFAULT, + top: Breadth::DEFAULT, + bottom: Breadth::DEFAULT, + }; +} + +impl Default for UiRect { fn default() -> Self { Self::DEFAULT } diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 31f232c4d1d85..83d44affbbde4 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -207,6 +207,169 @@ impl Val { } } +/// An enum that describes the thickness of something in flexbox layout options +#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] +#[reflect(PartialEq, Serialize, Deserialize)] +pub enum Breadth { + /// A value in pixels + Px(f32), + /// A value in percent + Percent(f32), +} + +impl Breadth { + pub const DEFAULT: Self = Self::Px(0.); +} + +impl Default for Breadth { + fn default() -> Self { + Self::DEFAULT + } +} + +impl From for Val { + fn from(value: Breadth) -> Self { + match value { + Breadth::Px(inner) => Val::Px(inner), + Breadth::Percent(inner) => Val::Percent(inner), + } + } +} + +impl TryFrom for Breadth { + type Error = BreadthConversionError; + fn try_from(value: Val) -> Result { + match value { + Val::Px(inner) => Ok(Breadth::Px(inner)), + Val::Percent(inner) => Ok(Breadth::Percent(inner)), + _ => Err(Self::Error::NonEvaluateable), + } + } +} + +impl Mul for Breadth { + type Output = Breadth; + + fn mul(self, rhs: f32) -> Self::Output { + match self { + Breadth::Px(value) => Breadth::Px(value * rhs), + Breadth::Percent(value) => Breadth::Percent(value * rhs), + } + } +} + +impl MulAssign for Breadth { + fn mul_assign(&mut self, rhs: f32) { + match self { + Breadth::Px(value) | Breadth::Percent(value) => *value *= rhs, + } + } +} + +impl Div for Breadth { + type Output = Breadth; + + fn div(self, rhs: f32) -> Self::Output { + match self { + Breadth::Px(value) => Breadth::Px(value / rhs), + Breadth::Percent(value) => Breadth::Percent(value / rhs), + } + } +} + +impl DivAssign for Breadth { + fn div_assign(&mut self, rhs: f32) { + match self { + Breadth::Px(value) | Breadth::Percent(value) => *value /= rhs, + } + } +} + +#[derive(Debug, Eq, PartialEq, Clone, Copy, Error)] +pub enum BreadthArithmeticError { + #[error("the variants of the Breadths don't match")] + NonIdenticalVariants, +} + +#[derive(Debug, Eq, PartialEq, Clone, Copy, Error)] +pub enum BreadthConversionError { + #[error("Cannot convert from non-evaluatable variants (non-numeric)")] + NonEvaluateable, +} + +impl Breadth { + /// Tries to add the values of two [`Breadth`]s. + /// Returns [`BreadthArithmeticError::NonIdenticalVariants`] if two [`Breadth`]s are of different variants. + pub fn try_add(&self, rhs: Breadth) -> Result { + match (self, rhs) { + (Breadth::Px(value), Breadth::Px(rhs_value)) => Ok(Breadth::Px(value + rhs_value)), + (Breadth::Percent(value), Breadth::Percent(rhs_value)) => { + Ok(Breadth::Percent(value + rhs_value)) + } + _ => Err(BreadthArithmeticError::NonIdenticalVariants), + } + } + + /// Adds `rhs` to `self` and assigns the result to `self` (see [`Breadth::try_add`]) + pub fn try_add_assign(&mut self, rhs: Breadth) -> Result<(), BreadthArithmeticError> { + *self = self.try_add(rhs)?; + Ok(()) + } + + /// Tries to subtract the values of two [`Breadth`]s. + /// Returns [`BreadthArithmeticError::NonIdenticalVariants`] if two [`Breadth`]s are of different variants. + pub fn try_sub(&self, rhs: Breadth) -> Result { + match (self, rhs) { + (Breadth::Px(value), Breadth::Px(rhs_value)) => Ok(Breadth::Px(value - rhs_value)), + (Breadth::Percent(value), Breadth::Percent(rhs_value)) => { + Ok(Breadth::Percent(value - rhs_value)) + } + _ => Err(BreadthArithmeticError::NonIdenticalVariants), + } + } + + /// Subtracts `rhs` from `self` and assigns the result to `self` (see [`Breadth::try_sub`]) + pub fn try_sub_assign(&mut self, rhs: Breadth) -> Result<(), BreadthArithmeticError> { + *self = self.try_sub(rhs)?; + Ok(()) + } + + /// A convenience function for simple evaluation of [`Breadth::Percent`] variant into a concrete [`Breadth::Px`] value. + /// Otherwise it returns an [`f32`] containing the evaluated value in pixels. + /// + /// **Note:** If a [`Breadth::Px`] is evaluated, it's inner value returned unchanged. + pub fn evaluate(&self, size: f32) -> f32 { + match self { + Breadth::Percent(value) => size * value / 100.0, + Breadth::Px(value) => *value, + } + } + + /// Similar to [`Breadth::try_add`], but performs [`Breadth::evaluate`] on both values before adding. + /// Returns an [`f32`] value in pixels. + pub fn add_with_size(&self, rhs: Breadth, size: f32) -> f32 { + self.evaluate(size) + rhs.evaluate(size) + } + + /// Similar to [`Breadth::try_add_assign`], but performs [`Breadth::evaluate`] on both values before adding. + /// The value gets converted to [`Breadth::Px`]. + pub fn add_assign_with_size(&mut self, rhs: Breadth, size: f32) { + *self = Breadth::Px(self.evaluate(size) + rhs.evaluate(size)); + } + + /// Similar to [`Breadth::try_sub`], but performs [`Breadth::evaluate`] on both values before subtracting. + /// Returns an [`f32`] value in pixels. + pub fn sub_with_size(&self, rhs: Breadth, size: f32) -> f32 { + self.evaluate(size) - rhs.evaluate(size) + } + + /// Similar to [`Breadth::try_sub_assign`], but performs [`Breadth::evaluate`] on both values before adding. + /// The value gets converted to [`Breadth::Px`]. + pub fn sub_assign_with_size(&mut self, rhs: Breadth, size: f32) { + *self = Breadth::Px(self.add_with_size(rhs, size)); + } +} + /// Describes the style of a UI node /// /// It uses the [Flexbox](https://cssreference.io/flexbox/) system. @@ -239,9 +402,9 @@ pub struct Style { /// The margin of the node pub margin: UiRect, /// The padding of the node - pub padding: UiRect, + pub padding: UiRect, /// The border of the node - pub border: UiRect, + pub border: UiRect, /// Defines how much a flexbox item should grow if there's space available pub flex_grow: f32, /// How to shrink if there's not enough space available @@ -275,10 +438,10 @@ 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, + position: UiRect::all(Val::Auto), + margin: UiRect::all(Val::Px(0.)), + padding: UiRect::all(Breadth::Px(0.)), + border: UiRect::all(Breadth::Px(0.)), flex_grow: 0.0, flex_shrink: 1.0, flex_basis: Val::Auto, @@ -831,4 +994,152 @@ mod tests { "the given variant of Val is not evaluateable (non-numeric)" ); } + + use crate::BreadthArithmeticError; + + use super::Breadth; + + #[test] + fn breadth_try_add() { + let px_sum = Breadth::Px(20.).try_add(Breadth::Px(22.)).unwrap(); + let percent_sum = Breadth::Percent(50.) + .try_add(Breadth::Percent(50.)) + .unwrap(); + + assert_eq!(px_sum, Breadth::Px(42.)); + assert_eq!(percent_sum, Breadth::Percent(100.)); + } + + #[test] + fn breadth_try_add_to_self() { + let mut breadth = Breadth::Px(5.); + + breadth.try_add_assign(Breadth::Px(3.)).unwrap(); + + assert_eq!(breadth, Breadth::Px(8.)); + } + + #[test] + fn breadth_try_sub() { + let px_sum = Breadth::Px(72.).try_sub(Breadth::Px(30.)).unwrap(); + let percent_sum = Breadth::Percent(100.) + .try_sub(Breadth::Percent(50.)) + .unwrap(); + + assert_eq!(px_sum, Breadth::Px(42.)); + assert_eq!(percent_sum, Breadth::Percent(50.)); + } + + #[test] + fn different_variant_breadth_try_add() { + let different_variant_sum_1 = Breadth::Px(50.).try_add(Breadth::Percent(50.)); + let different_variant_sum_2 = Breadth::Percent(50.).try_add(Breadth::Px(50.)); + + assert_eq!( + different_variant_sum_1, + Err(BreadthArithmeticError::NonIdenticalVariants) + ); + assert_eq!( + different_variant_sum_2, + Err(BreadthArithmeticError::NonIdenticalVariants) + ); + } + + #[test] + fn different_variant_breadth_try_sub() { + let different_variant_diff_1 = Breadth::Px(50.).try_sub(Breadth::Percent(50.)); + let different_variant_diff_2 = Breadth::Percent(50.).try_sub(Breadth::Px(50.)); + + assert_eq!( + different_variant_diff_1, + Err(BreadthArithmeticError::NonIdenticalVariants) + ); + assert_eq!( + different_variant_diff_2, + Err(BreadthArithmeticError::NonIdenticalVariants) + ); + } + + #[test] + fn breadth_evaluate_percent() { + let size = 250.; + let result = Breadth::Percent(80.).evaluate(size); + + assert_eq!(result, size * 0.8); + } + + #[test] + fn breadth_evaluate_px() { + let size = 250.; + let result = Breadth::Px(10.).evaluate(size); + + assert_eq!(result, 10.); + } + + #[test] + fn breadth_add_with_size() { + let size = 250.; + + let px_sum = Breadth::Px(21.).add_with_size(Breadth::Px(21.), size); + let percent_sum = Breadth::Percent(20.).add_with_size(Breadth::Percent(30.), size); + let mixed_sum = Breadth::Px(20.).add_with_size(Breadth::Percent(30.), size); + + assert_eq!(px_sum, 42.); + assert_eq!(percent_sum, 0.5 * size); + assert_eq!(mixed_sum, 20. + 0.3 * size); + } + + #[test] + fn breadth_sub_with_size() { + let size = 250.; + + let px_sum = Breadth::Px(60.).sub_with_size(Breadth::Px(18.), size); + let percent_sum = Breadth::Percent(80.).sub_with_size(Breadth::Percent(30.), size); + let mixed_sum = Breadth::Percent(50.).sub_with_size(Breadth::Px(30.), size); + + assert_eq!(px_sum, 42.); + assert_eq!(percent_sum, 0.5 * size); + assert_eq!(mixed_sum, 0.5 * size - 30.); + } + + #[test] + fn breadth_arithmetic_error_messages() { + assert_eq!( + format!("{}", BreadthArithmeticError::NonIdenticalVariants), + "the variants of the Breadths don't match" + ); + } + + #[test] + fn from_breadth_to_val() { + let inner_value = 11.; + + assert_eq!(Val::from(Breadth::Px(inner_value)), Val::Px(inner_value)); + assert_eq!( + Val::from(Breadth::Percent(inner_value)), + Val::Percent(inner_value) + ); + } + + #[test] + fn try_from_val_to_breadth() { + let inner_value = 22.; + + assert_eq!( + Breadth::try_from(Val::Auto), + Err(crate::BreadthConversionError::NonEvaluateable) + ); + assert_eq!( + Breadth::try_from(Val::Px(inner_value)), + Ok(Breadth::Px(inner_value)) + ); + assert_eq!( + Breadth::try_from(Val::Percent(inner_value)), + Ok(Breadth::Percent(inner_value)) + ); + assert_eq!( + Breadth::try_from(Val::Undefined), + Err(crate::BreadthConversionError::NonEvaluateable) + ); + } } diff --git a/examples/ui/text_layout.rs b/examples/ui/text_layout.rs index 2514e2ddd2f78..8b55386ba2a7a 100644 --- a/examples/ui/text_layout.rs +++ b/examples/ui/text_layout.rs @@ -161,10 +161,10 @@ fn spawn_nested_text_bundle( style: Style { margin, padding: UiRect { - top: Val::Px(1.), - left: Val::Px(5.), - right: Val::Px(5.), - bottom: Val::Px(1.), + top: Breadth::Px(1.), + left: Breadth::Px(5.), + right: Breadth::Px(5.), + bottom: Breadth::Px(1.), }, ..Default::default() }, diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 08d5a710f9dfc..298b3237c22db 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -36,7 +36,7 @@ fn setup(mut commands: Commands, asset_server: Res) { .spawn(NodeBundle { style: Style { size: Size::new(Val::Px(200.0), Val::Percent(100.0)), - border: UiRect::all(Val::Px(2.0)), + border: UiRect::all(Breadth::Px(2.0)), ..default() }, background_color: Color::rgb(0.65, 0.65, 0.65).into(), @@ -170,7 +170,7 @@ fn setup(mut commands: Commands, asset_server: Res) { bottom: Val::Px(10.0), ..default() }, - border: UiRect::all(Val::Px(20.0)), + border: UiRect::all(Breadth::Px(20.0)), ..default() }, background_color: Color::rgb(0.4, 0.4, 1.0).into(), diff --git a/examples/window/scale_factor_override.rs b/examples/window/scale_factor_override.rs index 5af87dd416cb8..383c454221dcf 100644 --- a/examples/window/scale_factor_override.rs +++ b/examples/window/scale_factor_override.rs @@ -38,7 +38,7 @@ fn setup(mut commands: Commands, asset_server: Res) { .spawn(NodeBundle { style: Style { size: Size::new(Val::Px(200.0), Val::Percent(100.0)), - border: UiRect::all(Val::Px(2.0)), + border: UiRect::all(Breadth::Px(2.0)), ..default() }, background_color: Color::rgb(0.65, 0.65, 0.65).into(), From 4b2c93ba95c72b8da9ed977609fc04a55544875d Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Wed, 8 Feb 2023 14:42:06 +0000 Subject: [PATCH 02/26] improved the `Breadth` doc comment description --- crates/bevy_ui/src/ui_node.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 83d44affbbde4..413c8140aacbf 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -207,7 +207,12 @@ impl Val { } } -/// An enum that describes the thickness of something in flexbox layout options +/// An enum that describes possible types of evaluatable (numeric) value in flexbox layout options +/// +/// `Breadth` should be identical to `Val` except that it has no non-evaluatable variants +/// and some functions are changed to reflect that they always have a defined ouput. +/// So, for example, [`Val::try_add_with_size`] can return an error, but `Breadth`'s equivalent +/// returns an `f32` and is renamed to [`Breadth::add_with_size`]. #[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] #[reflect(PartialEq, Serialize, Deserialize)] pub enum Breadth { From 9597f1512752a1e5d7da110e54bad6f35ea5b78b Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Wed, 8 Feb 2023 15:07:47 +0000 Subject: [PATCH 03/26] impl From for and use it to convert the padding and border values in `bevy_ui::flex::convert::from_style` --- crates/bevy_ui/src/flex/convert.rs | 16 ++++++++-------- crates/bevy_ui/src/geometry.rs | 11 +++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index ad5f1c87aa690..6e394822f4c41 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -3,15 +3,15 @@ use crate::{ PositionType, Size, Style, UiRect, Val, }; -pub fn from_rect + Default + PartialEq + Copy + Clone + PartialEq>( +pub fn from_rect( scale_factor: f64, - rect: UiRect, + rect: UiRect, ) -> taffy::geometry::Rect { taffy::geometry::Rect { - left: from_val(scale_factor, rect.left.into()), - right: from_val(scale_factor, rect.right.into()), - top: from_val(scale_factor, rect.top.into()), - bottom: from_val(scale_factor, rect.bottom.into()), + left: from_val(scale_factor, rect.left), + right: from_val(scale_factor, rect.right), + top: from_val(scale_factor, rect.top), + bottom: from_val(scale_factor, rect.bottom), } } @@ -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), diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 0c75a521931de..f24d238d8fb2e 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -346,6 +346,17 @@ impl Default for UiRect { } } +impl From> for UiRect { + fn from(rect: UiRect) -> 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. From 7d02b25c206d78a3c21e9e1ee181d4a80029b3b2 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Wed, 8 Feb 2023 16:05:38 +0000 Subject: [PATCH 04/26] cargo fmt --all --- crates/bevy_ui/src/ui_node.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 413c8140aacbf..5bc4dc2e5ed50 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -208,10 +208,10 @@ impl Val { } /// An enum that describes possible types of evaluatable (numeric) value in flexbox layout options -/// +/// /// `Breadth` should be identical to `Val` except that it has no non-evaluatable variants -/// and some functions are changed to reflect that they always have a defined ouput. -/// So, for example, [`Val::try_add_with_size`] can return an error, but `Breadth`'s equivalent +/// and some functions are changed to reflect that they always have a defined ouput. +/// So, for example, [`Val::try_add_with_size`] can return an error, but `Breadth`'s equivalent /// returns an `f32` and is renamed to [`Breadth::add_with_size`]. #[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] #[reflect(PartialEq, Serialize, Deserialize)] From 7ef9ca4b537d3baa6d1b461b70562edbe989196f Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 9 Feb 2023 08:50:09 +0000 Subject: [PATCH 05/26] added plugin type registrations for `Breadth` and `UiRect` --- crates/bevy_ui/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_ui/src/lib.rs b/crates/bevy_ui/src/lib.rs index 8ea3b454108d8..a2027c4807ed0 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -95,10 +95,12 @@ impl Plugin for UiPlugin { .register_type::() .register_type::() .register_type::() + .register_type::>() .register_type::