use std::ops::{Add, Sub};
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
}
impl Point {
pub const ZERO: Point = Point { x: 0.0, y: 0.0 };
pub fn new(x: f32, y: f32) -> Self {
Point { x, y }
}
}
impl Add for Point {
type Output = Point;
fn add(self, rhs: Point) -> Point {
Point::new(self.x + rhs.x, self.y + rhs.y)
}
}
impl Sub for Point {
type Output = Point;
fn sub(self, rhs: Point) -> Point {
Point::new(self.x - rhs.x, self.y - rhs.y)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Size {
pub width: f32,
pub height: f32,
}
impl Size {
pub fn new(width: f32, height: f32) -> Self {
Size { width, height }
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Rect {
pub origin: Point,
pub size: Size,
}
impl Rect {
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Rect {
origin: Point::new(x, y),
size: Size::new(width, height),
}
}
pub fn right(&self) -> f32 {
self.origin.x + self.size.width
}
pub fn bottom(&self) -> f32 {
self.origin.y + self.size.height
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Edges<T> {
pub top: T,
pub right: T,
pub bottom: T,
pub left: T,
}
impl<T: Copy> Edges<T> {
pub fn uniform(value: T) -> Self {
Edges {
top: value,
right: value,
bottom: value,
left: value,
}
}
}
impl Edges<f32> {
pub fn horizontal(&self) -> f32 {
self.left + self.right
}
pub fn vertical(&self) -> f32 {
self.top + self.bottom
}
pub fn is_zero(&self) -> bool {
self.top == 0.0 && self.right == 0.0 && self.bottom == 0.0 && self.left == 0.0
}
}
impl Add for Edges<f32> {
type Output = Edges<f32>;
fn add(self, rhs: Edges<f32>) -> Edges<f32> {
Edges {
top: self.top + rhs.top,
right: self.right + rhs.right,
bottom: self.bottom + rhs.bottom,
left: self.left + rhs.left,
}
}
}