nuit_core/utils/
unit_point.rs

1use serde::{Deserialize, Serialize};
2
3use super::{Alignment, Vec2};
4
5/// A point in normalized 2D space ([0, 1] x [0, 1])
6#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
7#[serde(rename_all = "camelCase")]
8pub struct UnitPoint {
9    value: Vec2<f64>,
10}
11
12impl UnitPoint {
13    pub const TOP_LEADING: Self = Self::with_xy(0.0, 0.0);
14    pub const TOP: Self = Self::with_xy(0.5, 0.0);
15    pub const TOP_TRAILING: Self = Self::with_xy(1.0, 0.0);
16
17    pub const LEADING: Self = Self::with_xy(0.0, 0.5);
18    pub const CENTER: Self = Self::with_xy(0.5, 0.5);
19    pub const TRAILING: Self = Self::with_xy(1.0, 0.5);
20
21    pub const BOTTOM_LEADING: Self = Self::with_xy(0.0, 0.5);
22    pub const BOTTOM: Self = Self::with_xy(0.5, 0.5);
23    pub const BOTTOM_TRAILING: Self = Self::with_xy(1.0, 0.5);
24
25    pub const fn new(value: Vec2<f64>) -> Self {
26        Self { value }
27    }
28
29    pub const fn with_xy(x: f64, y: f64) -> Self {
30        Self::new(Vec2::new(x, y))
31    }
32}
33
34impl From<Alignment> for UnitPoint {
35    fn from(alignment: Alignment) -> Self {
36        match alignment {
37            Alignment::TopLeading => Self::TOP_LEADING,
38            Alignment::Top => Self::TOP,
39            Alignment::TopTrailing => Self::TOP_TRAILING,
40            Alignment::Leading => Self::LEADING,
41            Alignment::Center => Self::CENTER,
42            Alignment::Trailing => Self::TRAILING,
43            Alignment::BottomLeading => Self::BOTTOM_LEADING,
44            Alignment::Bottom => Self::BOTTOM,
45            Alignment::BottomTrailing => Self::BOTTOM_TRAILING,
46        }
47    }
48}