bevy/crates/bevy_ui/src/geometry.rs
KDecay 7a7f097485 Move Size to bevy_ui (#4285)
# Objective

- Related #4276.
- Part of the splitting process of #3503.

## Solution

- Move `Size` to `bevy_ui`.

## Reasons

- `Size` is only needed in `bevy_ui` (because it needs to use `Val` instead of `f32`), but it's also used as a worse `Vec2`  replacement in other areas.
- `Vec2` is more powerful than `Size` so it should be used whenever possible.
- Discussion in #3503.

## Changelog

### Changed

- The `Size` type got moved from `bevy_math` to `bevy_ui`.

## Migration Guide

- The `Size` type got moved from `bevy::math` to `bevy::ui`. To migrate you just have to import `bevy::ui::Size` instead of `bevy::math::Math` or use the `bevy::prelude` instead.

Co-authored-by: KDecay <KDecayMusic@protonmail.com>
2022-04-25 13:54:46 +00:00

150 lines
3.1 KiB
Rust

use bevy_math::Vec2;
use bevy_reflect::Reflect;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
/// A two dimensional "size" as defined by a width and height
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
#[reflect(PartialEq)]
pub struct Size<T: Reflect + PartialEq = f32> {
pub width: T,
pub height: T,
}
impl<T: Reflect + PartialEq> Size<T> {
pub fn new(width: T, height: T) -> 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>;
fn add(self, rhs: Vec2) -> Self::Output {
Self {
width: self.width + rhs.x,
height: self.height + rhs.y,
}
}
}
impl<T: Reflect + PartialEq> AddAssign<Vec2> for Size<T>
where
T: AddAssign<f32>,
{
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>;
fn sub(self, rhs: Vec2) -> Self::Output {
Self {
width: self.width - rhs.x,
height: self.height - rhs.y,
}
}
}
impl<T: Reflect + PartialEq> SubAssign<Vec2> for Size<T>
where
T: SubAssign<f32>,
{
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>;
fn mul(self, rhs: f32) -> Self::Output {
Self::Output {
width: self.width * rhs,
height: self.height * rhs,
}
}
}
impl<T: Reflect + PartialEq> MulAssign<f32> for Size<T>
where
T: MulAssign<f32>,
{
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>;
fn div(self, rhs: f32) -> Self::Output {
Self::Output {
width: self.width / rhs,
height: self.height / rhs,
}
}
}
impl<T: Reflect + PartialEq> DivAssign<f32> for Size<T>
where
T: DivAssign<f32>,
{
fn div_assign(&mut self, rhs: f32) {
self.width /= rhs;
self.height /= rhs;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_ops() {
type SizeF = Size<f32>;
assert_eq!(
SizeF::new(10., 10.) + Vec2::new(10., 10.),
SizeF::new(20., 20.)
);
assert_eq!(
SizeF::new(20., 20.) - Vec2::new(10., 10.),
SizeF::new(10., 10.)
);
assert_eq!(SizeF::new(10., 10.) * 2., SizeF::new(20., 20.));
assert_eq!(SizeF::new(20., 20.) / 2., SizeF::new(10., 10.));
let mut size = SizeF::new(10., 10.);
size += Vec2::new(10., 10.);
assert_eq!(size, SizeF::new(20., 20.));
}
}