Skip to content

Commit

Permalink
Remove Size and UiRect generics (#5404)
Browse files Browse the repository at this point in the history
# Objective

- Migrate changes from #3503.

## Solution

- Change `Size<T>` and `UiRect<T>` 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<f32>` consider replacing it with a `Vec2` which is way more powerful.


Co-authored-by: KDecay <[email protected]>
  • Loading branch information
KDecay and KDecay committed Aug 1, 2022
1 parent 6752c9c commit bf085ee
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 109 deletions.
19 changes: 14 additions & 5 deletions crates/bevy_ui/src/flex/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{

pub fn from_rect(
scale_factor: f64,
rect: UiRect<Val>,
rect: UiRect,
) -> taffy::geometry::Rect<taffy::style::Dimension> {
taffy::geometry::Rect {
start: from_val(scale_factor, rect.left),
Expand All @@ -16,16 +16,16 @@ pub fn from_rect(
}
}

pub fn from_f32_size(scale_factor: f64, size: Size<f32>) -> taffy::geometry::Size<f32> {
pub fn from_f32_size(scale_factor: f64, size: Size) -> taffy::geometry::Size<f32> {
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<Val>,
size: Size,
) -> taffy::geometry::Size<taffy::style::Dimension> {
taffy::geometry::Size {
width: from_val(scale_factor, size.width),
Expand Down Expand Up @@ -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,
Expand Down
156 changes: 69 additions & 87 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<T: Reflect + PartialEq> {
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<T: Reflect + PartialEq> UiRect<T> {
impl UiRect {
/// Creates a new [`UiRect`] from the values specified.
///
/// # Example
Expand All @@ -152,7 +153,7 @@ impl<T: Reflect + PartialEq> UiRect<T> {
/// 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,
Expand All @@ -175,43 +176,29 @@ impl<T: Reflect + PartialEq> UiRect<T> {
/// 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<T: Default + Reflect + PartialEq> Default for UiRect<T> {
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<T: Reflect + PartialEq = f32> {
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<T: Reflect + PartialEq> Size<T> {
impl Size {
/// Creates a new [`Size`] from a width and a height.
///
/// # Example
Expand All @@ -224,25 +211,13 @@ impl<T: Reflect + PartialEq> Size<T> {
/// 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<T: Default + Reflect + PartialEq> Default for Size<T> {
fn default() -> Self {
Self {
width: Default::default(),
height: Default::default(),
}
}
}

impl<T: Reflect + PartialEq> Add<Vec2> for Size<T>
where
T: Add<f32, Output = T>,
{
type Output = Size<T>;
impl Add<Vec2> for Size {
type Output = Size;

fn add(self, rhs: Vec2) -> Self::Output {
Self {
Expand All @@ -252,21 +227,15 @@ where
}
}

impl<T: Reflect + PartialEq> AddAssign<Vec2> for Size<T>
where
T: AddAssign<f32>,
{
impl AddAssign<Vec2> for Size {
fn add_assign(&mut self, rhs: Vec2) {
self.width += rhs.x;
self.height += rhs.y;
}
}

impl<T: Reflect + PartialEq> Sub<Vec2> for Size<T>
where
T: Sub<f32, Output = T>,
{
type Output = Size<T>;
impl Sub<Vec2> for Size {
type Output = Size;

fn sub(self, rhs: Vec2) -> Self::Output {
Self {
Expand All @@ -276,21 +245,15 @@ where
}
}

impl<T: Reflect + PartialEq> SubAssign<Vec2> for Size<T>
where
T: SubAssign<f32>,
{
impl SubAssign<Vec2> for Size {
fn sub_assign(&mut self, rhs: Vec2) {
self.width -= rhs.x;
self.height -= rhs.y;
}
}

impl<T: Reflect + PartialEq> Mul<f32> for Size<T>
where
T: Mul<f32, Output = T>,
{
type Output = Size<T>;
impl Mul<f32> for Size {
type Output = Size;

fn mul(self, rhs: f32) -> Self::Output {
Self::Output {
Expand All @@ -300,21 +263,15 @@ where
}
}

impl<T: Reflect + PartialEq> MulAssign<f32> for Size<T>
where
T: MulAssign<f32>,
{
impl MulAssign<f32> for Size {
fn mul_assign(&mut self, rhs: f32) {
self.width *= rhs;
self.height *= rhs;
}
}

impl<T: Reflect + PartialEq> Div<f32> for Size<T>
where
T: Div<f32, Output = T>,
{
type Output = Size<T>;
impl Div<f32> for Size {
type Output = Size;

fn div(self, rhs: f32) -> Self::Output {
Self::Output {
Expand All @@ -324,10 +281,7 @@ where
}
}

impl<T: Reflect + PartialEq> DivAssign<f32> for Size<T>
where
T: DivAssign<f32>,
{
impl DivAssign<f32> for Size {
fn div_assign(&mut self, rhs: f32) {
self.width /= rhs;
self.height /= rhs;
Expand All @@ -339,22 +293,50 @@ mod tests {
use super::*;

#[test]
fn size_ops() {
fn test_size_add() {
assert_eq!(
Size::new(Val::Px(10.), Val::Px(10.)) + Vec2::new(10., 10.),
Size::new(Val::Px(20.), Val::Px(20.))
);

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.)));
}

#[test]
fn test_size_sub() {
assert_eq!(
Size::new(10., 10.) + Vec2::new(10., 10.),
Size::new(20., 20.)
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(20., 20.) - Vec2::new(10., 10.),
Size::new(10., 10.)
Size::new(Val::Px(10.), Val::Px(10.)) * 2.,
Size::new(Val::Px(20.), Val::Px(20.))
);
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 *= 2.;
assert_eq!(size, Size::new(Val::Px(20.), Val::Px(20.)));
}

size += Vec2::new(10., 10.);
#[test]
fn test_size_div() {
assert_eq!(
Size::new(Val::Px(20.), Val::Px(20.)) / 2.,
Size::new(Val::Px(10.), Val::Px(10.))
);

assert_eq!(size, Size::new(20., 20.));
let mut size = Size::new(Val::Px(20.), Val::Px(20.));
size /= 2.;
assert_eq!(size, Size::new(Val::Px(10.), Val::Px(10.)));
}
}
5 changes: 2 additions & 3 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ impl Plugin for UiPlugin {
.register_type::<Option<f32>>()
.register_type::<Overflow>()
.register_type::<PositionType>()
.register_type::<Size<f32>>()
.register_type::<Size<Val>>()
.register_type::<UiRect<Val>>()
.register_type::<Size>()
.register_type::<UiRect>()
.register_type::<Style>()
.register_type::<UiColor>()
.register_type::<UiImage>()
Expand Down
Loading

0 comments on commit bf085ee

Please sign in to comment.