#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
}
impl Point {
pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
impl std::ops::Add for Point {
type Output = Point;
fn add(self, rhs: Point) -> Point {
Point::new(self.x + rhs.x, self.y + rhs.y)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Size {
pub width: f32,
pub height: f32,
}
impl Size {
pub const ZERO: Self = Self { width: 0.0, height: 0.0 };
pub fn new(width: f32, height: f32) -> Self {
Self { width, height }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Rect {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl Rect {
pub const ZERO: Self = Self { x: 0.0, y: 0.0, width: 0.0, height: 0.0 };
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self { x, y, width, height }
}
pub fn from_ltrb(left: f32, top: f32, right: f32, bottom: f32) -> Self {
Self { x: left, y: top, width: right - left, height: bottom - top }
}
pub fn right(&self) -> f32 {
self.x + self.width
}
pub fn bottom(&self) -> f32 {
self.y + self.height
}
pub fn size(&self) -> Size {
Size::new(self.width, self.height)
}
pub fn contains(&self, p: Point) -> bool {
p.x >= self.x && p.x <= self.right() && p.y >= self.y && p.y <= self.bottom()
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
pub const TRANSPARENT: Self = Self { r: 0.0, g: 0.0, b: 0.0, a: 0.0 };
pub const WHITE: Self = Self { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
pub const BLACK: Self = Self { r: 0.0, g: 0.0, b: 0.0, a: 1.0 };
pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
Self { r, g, b, a: 1.0 }
}
pub fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
Self::rgb(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0)
}
pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
Self::rgba(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a as f32 / 255.0)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Axis {
#[default]
Vertical,
Horizontal,
}
impl Axis {
pub fn main(&self, p: Point) -> f32 {
match self {
Axis::Vertical => p.y,
Axis::Horizontal => p.x,
}
}
pub fn cross(&self, p: Point) -> f32 {
match self {
Axis::Vertical => p.x,
Axis::Horizontal => p.y,
}
}
pub fn main_size(&self, s: Size) -> f32 {
match self {
Axis::Vertical => s.height,
Axis::Horizontal => s.width,
}
}
pub fn cross_size(&self, s: Size) -> f32 {
match self {
Axis::Vertical => s.width,
Axis::Horizontal => s.height,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transform2D {
pub rotation: f32,
pub scale_x: f32,
pub scale_y: f32,
pub translate: Point,
}
impl Default for Transform2D {
fn default() -> Self {
Self { rotation: 0.0, scale_x: 1.0, scale_y: 1.0, translate: Point::ZERO }
}
}
impl Transform2D {
pub const IDENTITY: Self = Self { rotation: 0.0, scale_x: 1.0, scale_y: 1.0, translate: Point::ZERO };
pub fn is_identity(&self) -> bool {
self.rotation == 0.0 && self.scale_x == 1.0 && self.scale_y == 1.0 && self.translate == Point::ZERO
}
pub fn translate(x: f32, y: f32) -> Self {
Self { translate: Point::new(x, y), ..Self::IDENTITY }
}
pub fn rotate(angle: f32) -> Self {
Self { rotation: angle, ..Self::IDENTITY }
}
pub fn scale(s: f32) -> Self {
Self { scale_x: s, scale_y: s, ..Self::IDENTITY }
}
pub fn stretch(sx: f32, sy: f32) -> Self {
Self { scale_x: sx, scale_y: sy, ..Self::IDENTITY }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RRectDef {
pub radius: f32,
}
impl RRectDef {
pub const fn new(radius: f32) -> Self {
Self { radius }
}
}