Skip to content

Commit 11a7157

Browse files
committed
Utility methods for Val (bevyengine#6134)
Adds a better interface for performing mathematical operations with UI unit `Val`. Fixes bevyengine#6080. - 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. --- 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>
1 parent 91d9116 commit 11a7157

File tree

3 files changed

+278
-96
lines changed

3 files changed

+278
-96
lines changed

crates/bevy_ui/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ taffy = "0.1.0"
3434
serde = { version = "1", features = ["derive"] }
3535
smallvec = { version = "1.6", features = ["union", "const_generics"] }
3636
bytemuck = { version = "1.5", features = ["derive"] }
37-
thiserror = "1.0.37"
37+
thiserror = "1.0.37"

crates/bevy_ui/src/geometry.rs

+12-51
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::Val;
2-
use bevy_math::Vec2;
32
use bevy_reflect::Reflect;
4-
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
3+
use std::ops::{Div, DivAssign, Mul, MulAssign};
54

65
/// A type which is commonly used to define positions, margins, paddings and borders.
76
///
@@ -356,42 +355,15 @@ impl Size {
356355
};
357356
}
358357

359-
impl Add<Vec2> for Size {
360-
type Output = Size;
361-
362-
fn add(self, rhs: Vec2) -> Self::Output {
363-
Self {
364-
width: self.width + rhs.x,
365-
height: self.height + rhs.y,
366-
}
367-
}
368-
}
369-
370-
impl AddAssign<Vec2> for Size {
371-
fn add_assign(&mut self, rhs: Vec2) {
372-
self.width += rhs.x;
373-
self.height += rhs.y;
374-
}
375-
}
376-
377-
impl Sub<Vec2> for Size {
378-
type Output = Size;
379-
380-
fn sub(self, rhs: Vec2) -> Self::Output {
358+
impl From<(Val, Val)> for Size {
359+
fn from(vals: (Val, Val)) -> Self {
381360
Self {
382-
width: self.width - rhs.x,
383-
height: self.height - rhs.y,
361+
width: vals.0,
362+
height: vals.1,
384363
}
385364
}
386365
}
387366

388-
impl SubAssign<Vec2> for Size {
389-
fn sub_assign(&mut self, rhs: Vec2) {
390-
self.width -= rhs.x;
391-
self.height -= rhs.y;
392-
}
393-
}
394-
395367
impl Mul<f32> for Size {
396368
type Output = Size;
397369

@@ -433,27 +405,16 @@ mod tests {
433405
use super::*;
434406

435407
#[test]
436-
fn test_size_add() {
437-
assert_eq!(
438-
Size::new(Val::Px(10.), Val::Px(10.)) + Vec2::new(10., 10.),
439-
Size::new(Val::Px(20.), Val::Px(20.))
440-
);
408+
fn test_size_from() {
409+
let size: Size = (Val::Px(20.), Val::Px(30.)).into();
441410

442-
let mut size = Size::new(Val::Px(10.), Val::Px(10.));
443-
size += Vec2::new(10., 10.);
444-
assert_eq!(size, Size::new(Val::Px(20.), Val::Px(20.)));
445-
}
446-
447-
#[test]
448-
fn test_size_sub() {
449411
assert_eq!(
450-
Size::new(Val::Px(20.), Val::Px(20.)) - Vec2::new(10., 10.),
451-
Size::new(Val::Px(10.), Val::Px(10.))
412+
size,
413+
Size {
414+
width: Val::Px(20.),
415+
height: Val::Px(30.),
416+
}
452417
);
453-
454-
let mut size = Size::new(Val::Px(20.), Val::Px(20.));
455-
size -= Vec2::new(10., 10.);
456-
assert_eq!(size, Size::new(Val::Px(10.), Val::Px(10.)));
457418
}
458419

459420
#[test]

0 commit comments

Comments
 (0)