From bf085ee1d260191face3fdb10c11fe284e98a3e5 Mon Sep 17 00:00:00 2001 From: KDecay Date: Mon, 1 Aug 2022 16:27:16 +0000 Subject: [PATCH] Remove `Size` and `UiRect` generics (#5404) # Objective - Migrate changes from #3503. ## Solution - Change `Size` and `UiRect` to `Size` and `UiRect` using `Val`. - Implement `Sub`, `SubAssign`, `Mul`, `MulAssign`, `Div` and `DivAssign` for `Val`. - Update tests for `Size`. --- ## Changelog ### Changed - The generic `T` of `Size` and `UiRect` got removed and instead they both now always use `Val`. ## Migration Guide - The generic `T` of `Size` and `UiRect` got removed and instead they both now always use `Val`. If you used a `Size` consider replacing it with a `Vec2` which is way more powerful. Co-authored-by: KDecay --- crates/bevy_ui/src/flex/convert.rs | 19 +++- crates/bevy_ui/src/geometry.rs | 160 +++++++++++++---------------- crates/bevy_ui/src/lib.rs | 5 +- crates/bevy_ui/src/ui_node.rs | 82 +++++++++++++-- crates/bevy_ui/src/widget/image.rs | 7 +- crates/bevy_ui/src/widget/text.rs | 4 +- 6 files changed, 166 insertions(+), 111 deletions(-) diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index 57c17cdad9..6fe1a27393 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -5,7 +5,7 @@ use crate::{ pub fn from_rect( scale_factor: f64, - rect: UiRect, + rect: UiRect, ) -> taffy::geometry::Rect { taffy::geometry::Rect { start: from_val(scale_factor, rect.left), @@ -16,16 +16,16 @@ pub fn from_rect( } } -pub fn from_f32_size(scale_factor: f64, size: Size) -> taffy::geometry::Size { +pub fn from_f32_size(scale_factor: f64, size: Size) -> taffy::geometry::Size { taffy::geometry::Size { - width: (scale_factor * size.width as f64) as f32, - height: (scale_factor * size.height as f64) as f32, + width: val_to_f32(scale_factor, size.width), + height: val_to_f32(scale_factor, size.height), } } pub fn from_val_size( scale_factor: f64, - size: Size, + size: Size, ) -> taffy::geometry::Size { taffy::geometry::Size { width: from_val(scale_factor, size.width), @@ -60,6 +60,15 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style { } } +/// Converts a [`Val`] to a [`f32`] while respecting the scale factor. +pub fn val_to_f32(scale_factor: f64, val: Val) -> f32 { + match val { + Val::Undefined | Val::Auto => 0.0, + Val::Px(value) => (scale_factor * value as f64) as f32, + Val::Percent(value) => value / 100.0, + } +} + pub fn from_val(scale_factor: f64, val: Val) -> taffy::style::Dimension { match val { Val::Auto => taffy::style::Dimension::Auto, diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index e60dd3089f..620e96225d 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -1,3 +1,4 @@ +use crate::Val; use bevy_math::Vec2; use bevy_reflect::Reflect; use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; @@ -119,20 +120,20 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; /// bottom: Val::Px(40.0), /// }; /// ``` -#[derive(Copy, Clone, PartialEq, Debug, Reflect)] +#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)] #[reflect(PartialEq)] -pub struct UiRect { +pub struct UiRect { /// The value corresponding to the left side of the UI rect. - pub left: T, + pub left: Val, /// The value corresponding to the right side of the UI rect. - pub right: T, + pub right: Val, /// The value corresponding to the top side of the UI rect. - pub top: T, + pub top: Val, /// The value corresponding to the bottom side of the UI rect. - pub bottom: T, + pub bottom: Val, } -impl UiRect { +impl UiRect { /// Creates a new [`UiRect`] from the values specified. /// /// # Example @@ -152,7 +153,7 @@ impl UiRect { /// assert_eq!(ui_rect.top, Val::Px(30.0)); /// assert_eq!(ui_rect.bottom, Val::Px(40.0)); /// ``` - pub fn new(left: T, right: T, top: T, bottom: T) -> Self { + pub fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self { UiRect { left, right, @@ -175,43 +176,29 @@ impl UiRect { /// assert_eq!(ui_rect.top, Val::Px(10.0)); /// assert_eq!(ui_rect.bottom, Val::Px(10.0)); /// ``` - pub fn all(value: T) -> Self - where - T: Clone, - { + pub fn all(value: Val) -> Self { UiRect { - left: value.clone(), - right: value.clone(), - top: value.clone(), + left: value, + right: value, + top: value, bottom: value, } } } -impl Default for UiRect { - fn default() -> Self { - Self { - left: Default::default(), - right: Default::default(), - top: Default::default(), - bottom: Default::default(), - } - } -} - /// 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)] +#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)] #[reflect(PartialEq)] -pub struct Size { +pub struct Size { /// The width of the 2-dimensional area. - pub width: T, + pub width: Val, /// The height of the 2-dimensional area. - pub height: T, + pub height: Val, } -impl Size { +impl Size { /// Creates a new [`Size`] from a width and a height. /// /// # Example @@ -224,25 +211,13 @@ impl Size { /// assert_eq!(size.width, Val::Px(100.0)); /// assert_eq!(size.height, Val::Px(200.0)); /// ``` - pub fn new(width: T, height: T) -> Self { + pub fn new(width: Val, height: Val) -> Self { Size { width, height } } } -impl Default for Size { - fn default() -> Self { - Self { - width: Default::default(), - height: Default::default(), - } - } -} - -impl Add for Size -where - T: Add, -{ - type Output = Size; +impl Add for Size { + type Output = Size; fn add(self, rhs: Vec2) -> Self::Output { Self { @@ -252,21 +227,15 @@ where } } -impl AddAssign for Size -where - T: AddAssign, -{ +impl AddAssign for Size { fn add_assign(&mut self, rhs: Vec2) { self.width += rhs.x; self.height += rhs.y; } } -impl Sub for Size -where - T: Sub, -{ - type Output = Size; +impl Sub for Size { + type Output = Size; fn sub(self, rhs: Vec2) -> Self::Output { Self { @@ -276,21 +245,15 @@ where } } -impl SubAssign for Size -where - T: SubAssign, -{ +impl SubAssign for Size { fn sub_assign(&mut self, rhs: Vec2) { self.width -= rhs.x; self.height -= rhs.y; } } -impl Mul for Size -where - T: Mul, -{ - type Output = Size; +impl Mul for Size { + type Output = Size; fn mul(self, rhs: f32) -> Self::Output { Self::Output { @@ -300,21 +263,15 @@ where } } -impl MulAssign for Size -where - T: MulAssign, -{ +impl MulAssign for Size { fn mul_assign(&mut self, rhs: f32) { self.width *= rhs; self.height *= rhs; } } -impl Div for Size -where - T: Div, -{ - type Output = Size; +impl Div for Size { + type Output = Size; fn div(self, rhs: f32) -> Self::Output { Self::Output { @@ -324,10 +281,7 @@ where } } -impl DivAssign for Size -where - T: DivAssign, -{ +impl DivAssign for Size { fn div_assign(&mut self, rhs: f32) { self.width /= rhs; self.height /= rhs; @@ -339,22 +293,50 @@ mod tests { use super::*; #[test] - fn size_ops() { + fn test_size_add() { assert_eq!( - Size::new(10., 10.) + Vec2::new(10., 10.), - Size::new(20., 20.) + Size::new(Val::Px(10.), Val::Px(10.)) + Vec2::new(10., 10.), + Size::new(Val::Px(20.), Val::Px(20.)) ); - assert_eq!( - Size::new(20., 20.) - Vec2::new(10., 10.), - Size::new(10., 10.) - ); - assert_eq!(Size::new(10., 10.) * 2., Size::new(20., 20.)); - assert_eq!(Size::new(20., 20.) / 2., Size::new(10., 10.)); - - let mut size = Size::new(10., 10.); + let mut size = Size::new(Val::Px(10.), Val::Px(10.)); size += Vec2::new(10., 10.); + assert_eq!(size, Size::new(Val::Px(20.), Val::Px(20.))); + } - assert_eq!(size, Size::new(20., 20.)); + #[test] + fn test_size_sub() { + assert_eq!( + Size::new(Val::Px(20.), Val::Px(20.)) - Vec2::new(10., 10.), + Size::new(Val::Px(10.), Val::Px(10.)) + ); + + let mut size = Size::new(Val::Px(20.), Val::Px(20.)); + size -= Vec2::new(10., 10.); + assert_eq!(size, Size::new(Val::Px(10.), Val::Px(10.))); + } + + #[test] + fn test_size_mul() { + assert_eq!( + Size::new(Val::Px(10.), Val::Px(10.)) * 2., + Size::new(Val::Px(20.), Val::Px(20.)) + ); + + let mut size = Size::new(Val::Px(10.), Val::Px(10.)); + size *= 2.; + assert_eq!(size, Size::new(Val::Px(20.), 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.))); } } diff --git a/crates/bevy_ui/src/lib.rs b/crates/bevy_ui/src/lib.rs index 54a0e75189..6ec98338b3 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -67,9 +67,8 @@ impl Plugin for UiPlugin { .register_type::>() .register_type::() .register_type::() - .register_type::>() - .register_type::>() - .register_type::>() + .register_type::() + .register_type::() .register_type::