93 lines
1.9 KiB
Rust
93 lines
1.9 KiB
Rust
use crate::{
|
|
layout::UiNum,
|
|
util::{F32Util, impl_op},
|
|
};
|
|
use std::ops::*;
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
|
|
pub struct Vec2 {
|
|
pub x: f32,
|
|
pub y: f32,
|
|
}
|
|
|
|
pub const fn vec2(x: f32, y: f32) -> Vec2 {
|
|
Vec2::new(x, y)
|
|
}
|
|
|
|
impl Vec2 {
|
|
pub const ZERO: Self = Self::new(0.0, 0.0);
|
|
pub const ONE: Self = Self::new(1.0, 1.0);
|
|
|
|
pub const fn new(x: f32, y: f32) -> Self {
|
|
Self { x, y }
|
|
}
|
|
|
|
pub const fn lerp(self, from: impl const Into<Self>, to: impl const Into<Self>) -> Self {
|
|
let from = from.into();
|
|
let to = to.into();
|
|
Self {
|
|
x: self.x.lerp(from.x, to.x),
|
|
y: self.y.lerp(from.y, to.y),
|
|
}
|
|
}
|
|
|
|
pub const fn round(self) -> Self {
|
|
Self {
|
|
x: self.x.round(),
|
|
y: self.y.round(),
|
|
}
|
|
}
|
|
|
|
pub const fn floor(self) -> Self {
|
|
Self {
|
|
x: self.x.floor(),
|
|
y: self.y.floor(),
|
|
}
|
|
}
|
|
|
|
pub const fn ceil(self) -> Self {
|
|
Self {
|
|
x: self.x.ceil(),
|
|
y: self.y.ceil(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl const From<f32> for Vec2 {
|
|
fn from(v: f32) -> Self {
|
|
Self { x: v, y: v }
|
|
}
|
|
}
|
|
|
|
// this version looks kinda cool... is it more readable? more annoying to copy and change though
|
|
impl_op!(impl Add for Vec2: add x y);
|
|
impl_op!(Vec2 Sub sub; x y);
|
|
impl_op!(Vec2 Mul mul; x y);
|
|
impl_op!(Vec2 Div div; x y);
|
|
|
|
impl Neg for Vec2 {
|
|
type Output = Self;
|
|
|
|
fn neg(mut self) -> Self::Output {
|
|
self.x = -self.x;
|
|
self.y = -self.y;
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<T: UiNum, U: UiNum> From<(T, U)> for Vec2 {
|
|
fn from((x, y): (T, U)) -> Self {
|
|
Self {
|
|
x: x.to_f32(),
|
|
y: y.to_f32(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Debug for Vec2 {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "({}, {})", self.x, self.y)
|
|
}
|
|
}
|