ori_core/layout/
padding.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use super::{Size, Vector};

/// A padding of a rectangle.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Padding {
    /// The top padding.
    pub top: f32,
    /// The right padding.
    pub right: f32,
    /// The bottom padding.
    pub bottom: f32,
    /// The left padding.
    pub left: f32,
}

impl Padding {
    /// Create a new [`Padding`].
    pub const fn new(top: f32, right: f32, bottom: f32, left: f32) -> Self {
        Self {
            top,
            right,
            bottom,
            left,
        }
    }

    /// Create a new [`Padding`] with the same value for all sides.
    pub const fn all(value: f32) -> Self {
        Self::new(value, value, value, value)
    }

    /// Get the size of the padding.
    pub fn size(&self) -> Size {
        Size::new(self.left + self.right, self.top + self.bottom)
    }

    /// Get the offset of the padding.
    pub fn offset(&self) -> Vector {
        Vector::new(self.left, self.top)
    }
}

impl From<(f32, f32, f32, f32)> for Padding {
    fn from(value: (f32, f32, f32, f32)) -> Self {
        Self::new(value.0, value.1, value.2, value.3)
    }
}

impl From<[f32; 4]> for Padding {
    fn from(value: [f32; 4]) -> Self {
        Self::new(value[0], value[1], value[2], value[3])
    }
}

impl From<(f32, f32)> for Padding {
    fn from((horizontal, vertical): (f32, f32)) -> Self {
        Self::new(vertical, horizontal, vertical, horizontal)
    }
}

impl From<[f32; 2]> for Padding {
    fn from([horizontal, vertical]: [f32; 2]) -> Self {
        Self::new(vertical, horizontal, vertical, horizontal)
    }
}

impl From<f32> for Padding {
    fn from(value: f32) -> Self {
        Self::all(value)
    }
}