ori_core/layout/
alignment.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use super::{Size, Vector};

/// Alignment of content inside a container.
#[repr(C)]
#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Alignment {
    /// The horizontal alignment.
    pub x: f32,
    /// The vertical alignment.
    pub y: f32,
}

impl Alignment {
    /// Align the content at the center of the container.
    pub const CENTER: Self = Self::new(0.5, 0.5);
    /// Align the content at the top left of the container.
    pub const TOP_LEFT: Self = Self::new(0.0, 0.0);
    /// Align the content at the top of the container.
    pub const TOP: Self = Self::new(0.5, 0.0);
    /// Align the content at the top right of the container.
    pub const TOP_RIGHT: Self = Self::new(1.0, 0.0);
    /// Align the content at the left of the container.
    pub const LEFT: Self = Self::new(0.0, 0.5);
    /// Align the content at the right of the container.
    pub const RIGHT: Self = Self::new(1.0, 0.5);
    /// Align the content at the bottom left of the container.
    pub const BOTTOM_LEFT: Self = Self::new(0.0, 1.0);
    /// Align the content at the bottom of the container.
    pub const BOTTOM: Self = Self::new(0.5, 1.0);
    /// Align the content at the bottom right of the container.
    pub const BOTTOM_RIGHT: Self = Self::new(1.0, 1.0);

    /// Create a new alignment.
    pub const fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }

    /// Align the content inside the container.
    pub fn align(self, content: Size, container: Size) -> Vector {
        Vector::new(
            self.x * (container.width - content.width),
            self.y * (container.height - content.height),
        )
    }
}

impl From<(f32, f32)> for Alignment {
    fn from((x, y): (f32, f32)) -> Self {
        Self::new(x, y)
    }
}

impl From<[f32; 2]> for Alignment {
    fn from([x, y]: [f32; 2]) -> Self {
        Self::new(x, y)
    }
}