arkham/src/ui/geometry.rs

52 lines
921 B
Rust

use std::ops::{Add, Sub};
pub struct Rect {
pub pos: Pos,
pub width: u16,
pub height: u16,
}
impl Rect {
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
Rect {
pos: Pos::new(x, y),
width,
height,
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct Pos(u16, u16);
impl Pos {
pub fn new(x: u16, y: u16) -> Self {
Pos(x, y)
}
pub fn x(&self) -> u16 {
self.0
}
pub fn y(&self) -> u16 {
self.1
}
}
impl Add for Pos {
type Output = Pos;
fn add(self, rhs: Self) -> Self::Output {
Pos(self.x() + rhs.x(), self.y() + rhs.y())
}
}
impl Sub for Pos {
type Output = Pos;
fn sub(self, rhs: Self) -> Self::Output {
Pos(
(self.x() as i32 - rhs.x() as i32).max(0) as u16,
(self.y() as i32 - rhs.y() as i32).max(0) as u16,
)
}
}