Skip to content

Commit

Permalink
Utility methods for Val (bevyengine#6134)
Browse files Browse the repository at this point in the history
# Objective

Adds a better interface for performing mathematical operations with UI unit `Val`. Fixes bevyengine#6080.

## Solution

- Added `try_add` and `try_sub` methods to Val.
- Removed the `Add` and `AddAssign` impls for `Val` that introduced unintuitive and bug-prone behaviour.
- As a consequence of the prior,  ~~changed the `Add` and `Sub` impls for the `Size` struct to take a `(Val, Val)` instead of `Vec2`~~ deleted the `Add` and `Sub` impls for the `Size` struct
- Added a `From<(Val, Val)>` impl for the `Size` struct
- Added `evaluate(size: f32)` method that converts from `Val::Percent` to `Val::Px`.
- Added `try_add_with_size` and `try_sub_with_size` methods to `Val`, which evaluate `Val::Percent` values into `Val::Px` values before adding.

---

## Migration Guide

Instead of using the + and - operators, perform calculations on `Val`s using the new `try_add` and `try_sub` methods. Multiplication and division remained unchanged. Also, when adding or subtracting from `Size`, ~~use a `Val` tuple instead of `Vec2`~~ perform the addition on `width` and `height` separately.


Co-authored-by: Dawid Piotrowski <41804418+Pietrek14@users.noreply.github.com>
  • Loading branch information
2 people authored and james7132 committed Oct 28, 2022
1 parent df2d7fd commit 5678d0e
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 95 deletions.
1 change: 1 addition & 0 deletions crates/bevy_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ taffy = "0.1.0"
serde = { version = "1", features = ["derive"] }
smallvec = { version = "1.6", features = ["union", "const_generics"] }
bytemuck = { version = "1.5", features = ["derive"] }
thiserror = "1.0.37"
63 changes: 12 additions & 51 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::Val;
use bevy_math::Vec2;
use bevy_reflect::Reflect;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use std::ops::{Div, DivAssign, Mul, MulAssign};

/// A type which is commonly used to define positions, margins, paddings and borders.
///
Expand Down Expand Up @@ -356,42 +355,15 @@ impl Size {
};
}

impl Add<Vec2> for Size {
type Output = Size;

fn add(self, rhs: Vec2) -> Self::Output {
Self {
width: self.width + rhs.x,
height: self.height + rhs.y,
}
}
}

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

impl Sub<Vec2> for Size {
type Output = Size;

fn sub(self, rhs: Vec2) -> Self::Output {
impl From<(Val, Val)> for Size {
fn from(vals: (Val, Val)) -> Self {
Self {
width: self.width - rhs.x,
height: self.height - rhs.y,
width: vals.0,
height: vals.1,
}
}
}

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

impl Mul<f32> for Size {
type Output = Size;

Expand Down Expand Up @@ -433,27 +405,16 @@ mod tests {
use super::*;

#[test]
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.))
);
fn test_size_from() {
let size: Size = (Val::Px(20.), Val::Px(30.)).into();

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(Val::Px(20.), Val::Px(20.)) - Vec2::new(10., 10.),
Size::new(Val::Px(10.), Val::Px(10.))
size,
Size {
width: Val::Px(20.),
height: Val::Px(30.),
}
);

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]
Expand Down
Loading

0 comments on commit 5678d0e

Please sign in to comment.