tuid/src/size.rs

182 lines
3.0 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

use std::{
fmt,
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign},
};
use crate::Vec2;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Size {
pub width: usize,
pub height: usize,
}
impl Size {
pub const ZERO: Self = Self {
width: 0,
height: 0,
};
pub const MAX: Self = Self {
width: usize::MAX,
height: usize::MAX,
};
pub fn new(width: usize, height: usize) -> Self {
Self { width, height }
}
pub fn clamp(&self, min: Size, max: Size) -> Size {
Self {
width: self.width.clamp(min.width, max.width),
height: self.height.clamp(min.height, max.height),
}
}
pub const fn to_vec2(self) -> Vec2 {
Vec2::new(self.width, self.height)
}
pub fn aspect_ratio(&self) -> usize {
self.height / self.width
}
}
impl fmt::Debug for Size {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}W×{:?}H", self.width, self.height)
}
}
impl fmt::Display for Size {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "(")?;
fmt::Display::fmt(&self.width, formatter)?;
write!(formatter, "×")?;
fmt::Display::fmt(&self.height, formatter)?;
write!(formatter, ")")
}
}
impl MulAssign<usize> for Size {
#[inline]
fn mul_assign(&mut self, other: usize) {
*self = Size {
width: self.width * other,
height: self.height * other,
};
}
}
impl Mul<Size> for usize {
type Output = Size;
#[inline]
fn mul(self, other: Size) -> Size {
other * self
}
}
impl Mul<usize> for Size {
type Output = Size;
#[inline]
fn mul(self, other: usize) -> Size {
Size {
width: self.width * other,
height: self.height * other,
}
}
}
impl DivAssign<usize> for Size {
#[inline]
fn div_assign(&mut self, other: usize) {
*self = Size {
width: self.width / other,
height: self.height / other,
};
}
}
impl Div<usize> for Size {
type Output = Size;
#[inline]
fn div(self, other: usize) -> Size {
Size {
width: self.width / other,
height: self.height / other,
}
}
}
impl Add<Size> for Size {
type Output = Size;
#[inline]
fn add(self, other: Size) -> Size {
Size {
width: self.width + other.width,
height: self.height + other.height,
}
}
}
impl AddAssign<Size> for Size {
#[inline]
fn add_assign(&mut self, other: Size) {
*self = *self + other;
}
}
impl Sub<Size> for Size {
type Output = Size;
#[inline]
fn sub(self, other: Size) -> Size {
Size {
width: self.width - other.width,
height: self.height - other.height,
}
}
}
impl SubAssign<Size> for Size {
#[inline]
fn sub_assign(&mut self, other: Size) {
*self = *self - other;
}
}
impl From<(usize, usize)> for Size {
#[inline]
fn from(v: (usize, usize)) -> Size {
Size {
width: v.0,
height: v.1,
}
}
}
impl From<Size> for (usize, usize) {
#[inline]
fn from(v: Size) -> (usize, usize) {
(v.width, v.height)
}
}
impl From<Vec2> for Size {
#[inline]
fn from(v: Vec2) -> Size {
v.to_size()
}
}
impl From<(u16, u16)> for Size {
fn from(s: (u16, u16)) -> Self {
Self {
width: s.0 as usize,
height: s.1 as usize,
}
}
}