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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::Vec2;
use std::ops::{Add, Div, Mul, Sub};

/// Four values representing each direction.
#[derive(Clone, Copy)]
pub struct Margins {
    /// Left margin
    pub left: usize,
    /// Right margin
    pub right: usize,
    /// Top margin
    pub top: usize,
    /// Bottom margin
    pub bottom: usize,
}

impl Margins {
    /// Creates a new Margins.
    #[deprecated(
        note = "`Margins::new()` is ambiguous. Use `Margins::lrtb()` instead."
    )]
    pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
        Self::lrtb(left, right, top, bottom)
    }

    /// Creates a new `Margins` object from the Left, Right, Top, Bottom fields.
    pub fn lrtb(left: usize, right: usize, top: usize, bottom: usize) -> Self {
        Margins {
            left,
            right,
            top,
            bottom,
        }
    }

    /// Creates a new `Margins` object from the Left, Top, Right, Bottom fields.
    pub fn ltrb(left_top: Vec2, right_bottom: Vec2) -> Self {
        Self::lrtb(left_top.x, right_bottom.x, left_top.y, right_bottom.y)
    }

    /// Creates a new `Margins` object from the Top, Right, Bottom, Left fields.
    pub fn trbl(top: usize, right: usize, bottom: usize, left: usize) -> Self {
        Self::lrtb(left, right, top, bottom)
    }

    /// Creates a new `Margins` object from the Left and Right fields.
    ///
    /// Top and Bottom will be 0.
    pub fn lr(left: usize, right: usize) -> Self {
        Self::lrtb(left, right, 0, 0)
    }

    /// Creates a new `Margins` object from the Top and Bottom fields.
    ///
    /// Left and Right will be 0.
    pub fn tb(top: usize, bottom: usize) -> Self {
        Self::lrtb(0, 0, top, bottom)
    }

    /// Returns left + right.
    pub fn horizontal(&self) -> usize {
        self.left + self.right
    }

    /// Returns top + bottom.
    pub fn vertical(&self) -> usize {
        self.top + self.bottom
    }

    /// Returns (left+right, top+bottom).
    pub fn combined(&self) -> Vec2 {
        Vec2::new(self.horizontal(), self.vertical())
    }

    /// Returns (left, top).
    pub fn top_left(&self) -> Vec2 {
        Vec2::new(self.left, self.top)
    }

    /// Returns (right, bottom).
    pub fn bot_right(&self) -> Vec2 {
        Vec2::new(self.right, self.bottom)
    }
}

impl Add<Margins> for Margins {
    type Output = Margins;

    fn add(self, other: Margins) -> Margins {
        Margins {
            left: self.left + other.left,
            right: self.right + other.right,
            top: self.top + other.top,
            bottom: self.bottom + other.bottom,
        }
    }
}

impl Sub<Margins> for Margins {
    type Output = Margins;

    fn sub(self, other: Margins) -> Margins {
        Margins {
            left: self.left - other.left,
            right: self.right - other.right,
            top: self.top - other.top,
            bottom: self.bottom - other.bottom,
        }
    }
}

impl Div<usize> for Margins {
    type Output = Margins;

    fn div(self, other: usize) -> Margins {
        Margins {
            left: self.left / other,
            right: self.right / other,
            top: self.top / other,
            bottom: self.bottom / other,
        }
    }
}

impl Mul<usize> for Margins {
    type Output = Margins;

    fn mul(self, other: usize) -> Margins {
        Margins {
            left: self.left * other,
            right: self.right * other,
            top: self.top * other,
            bottom: self.bottom * other,
        }
    }
}