Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Utility methods for Val #6134

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
71056a3
made resource_scope work for non-send resources
Pietrek14 Sep 27, 2022
d607e6b
Merge branch 'bevyengine:main' into main
Pietrek14 Sep 27, 2022
83f2417
fixed the non-send resource scope tests
Pietrek14 Sep 27, 2022
6da0e41
formatting
Pietrek14 Sep 27, 2022
3500039
simplified panic in a non-send resource scope test
Pietrek14 Sep 28, 2022
d823b1b
Merge branch 'bevyengine:main' into main
Pietrek14 Sep 28, 2022
f68eb33
changed the name of non-send struct used for testing
Pietrek14 Sep 28, 2022
56a5dd7
added arithmetic functions for Val
Pietrek14 Sep 29, 2022
ad9da5f
removed adding and subtracting of val with f32
Pietrek14 Sep 29, 2022
fef5ead
Merge branch 'bevyengine:main' into val-arithmetic
Pietrek14 Sep 30, 2022
c9dfd36
changed the names of val methods to be more in line with the standard…
Pietrek14 Oct 1, 2022
cb577bc
formatting
Pietrek14 Oct 1, 2022
1fb6049
deleted warnings from Val::try_add and Val::try_sub
Pietrek14 Oct 4, 2022
7b33282
deleted unnecessary use
Pietrek14 Oct 4, 2022
bd2240f
Val::evaluate returns f32
Pietrek14 Oct 4, 2022
3c57795
clippy
Pietrek14 Oct 4, 2022
e039468
added docs for Add and Subs impls for Size struct
Pietrek14 Oct 4, 2022
69fa054
formatting
Pietrek14 Oct 5, 2022
2fdb45c
replace add and sub impls for Size struct with from<(Val, Val)>
Pietrek14 Oct 5, 2022
da41750
ValArithmeticError derives Clone and Copy
Pietrek14 Oct 5, 2022
769362c
added error messages to ValArithmeticError
Pietrek14 Oct 5, 2022
495fef9
made the docs for Val::try_add_assign_with_size not reference self wh…
Pietrek14 Oct 5, 2022
54ef797
formatting
Pietrek14 Oct 5, 2022
a8e9ca9
doc comments for Val::try_add_assign and Val::try_sub_assign
Pietrek14 Oct 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 36 additions & 21 deletions crates/bevy_ui/src/geometry.rs
@@ -1,5 +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 @@ -356,39 +355,55 @@ impl Size {
};
}

impl Add<Vec2> for Size {
impl Add<(Val, Val)> for Size {
Pietrek14 marked this conversation as resolved.
Show resolved Hide resolved
type Output = Size;

fn add(self, rhs: Vec2) -> Self::Output {
/// Adds ([`Val`], [`Val`]) respectively to the width and the height of the [`Size`] struct.
///
/// # Panics
/// If the [`Val`]s can't be added correctly (if they are of different variants).
fn add(self, rhs: (Val, Val)) -> Self::Output {
Pietrek14 marked this conversation as resolved.
Show resolved Hide resolved
Self {
width: self.width + rhs.x,
height: self.height + rhs.y,
width: self.width.try_add(rhs.0).unwrap(),
height: self.height.try_add(rhs.1).unwrap(),
}
}
}

impl AddAssign<Vec2> for Size {
fn add_assign(&mut self, rhs: Vec2) {
self.width += rhs.x;
self.height += rhs.y;
impl AddAssign<(Val, Val)> for Size {
/// Add-assigns ([`Val`], [`Val`]) respectively to the width and the height of the [`Size`] struct.
///
/// # Panics
/// If the [`Val`]s can't be added correctly (if they are of different variants).
fn add_assign(&mut self, rhs: (Val, Val)) {
self.width.try_add_assign(rhs.0).unwrap();
self.height.try_add_assign(rhs.1).unwrap();
}
}

impl Sub<Vec2> for Size {
impl Sub<(Val, Val)> for Size {
type Output = Size;

fn sub(self, rhs: Vec2) -> Self::Output {
/// Subtracts ([`Val`], [`Val`]) respectively to the width and the height of the [`Size`] struct.
///
/// # Panics
/// If the [`Val`]s can't be subtracted correctly (if they are of different variants).
fn sub(self, rhs: (Val, Val)) -> Self::Output {
Self {
width: self.width - rhs.x,
height: self.height - rhs.y,
width: self.width.try_sub(rhs.0).unwrap(),
height: self.height.try_sub(rhs.1).unwrap(),
}
}
}

impl SubAssign<Vec2> for Size {
fn sub_assign(&mut self, rhs: Vec2) {
self.width -= rhs.x;
self.height -= rhs.y;
impl SubAssign<(Val, Val)> for Size {
/// Subtract-assigns ([`Val`], [`Val`]) respectively to the width and the height of the [`Size`] struct.
///
/// # Panics
/// If the [`Val`]s can't be subtracted correctly (if they are of different variants).
fn sub_assign(&mut self, rhs: (Val, Val)) {
self.width.try_sub_assign(rhs.0).unwrap();
self.height.try_sub_assign(rhs.1).unwrap();
}
}

Expand Down Expand Up @@ -435,24 +450,24 @@ mod tests {
#[test]
fn test_size_add() {
assert_eq!(
Size::new(Val::Px(10.), Val::Px(10.)) + Vec2::new(10., 10.),
Size::new(Val::Px(10.), Val::Px(10.)) + (Val::Px(10.), Val::Px(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.);
size += (Val::Px(10.), Val::Px(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(20.), Val::Px(20.)) - (Val::Px(10.), Val::Px(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.);
size -= (Val::Px(10.), Val::Px(10.));
assert_eq!(size, Size::new(Val::Px(10.), Val::Px(10.)));
}

Expand Down