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
137
use std::ops::{Add, Div, Mul, Sub};
use vec::Vec2;

/// 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.
    pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
        Margins {
            left,
            right,
            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 From<(usize, usize, usize, usize)> for Margins {
    fn from(
        (left, right, top, bottom): (usize, usize, usize, usize),
    ) -> Margins {
        Margins::new(left, right, top, bottom)
    }
}

impl From<(i32, i32, i32, i32)> for Margins {
    fn from((left, right, top, bottom): (i32, i32, i32, i32)) -> Margins {
        (left as usize, right as usize, top as usize, bottom as usize).into()
    }
}

impl From<((i32, i32), (i32, i32))> for Margins {
    fn from(
        ((left, right), (top, bottom)): ((i32, i32), (i32, i32)),
    ) -> Margins {
        (left, right, top, bottom).into()
    }
}
impl From<((usize, usize), (usize, usize))> for Margins {
    fn from(
        ((left, right), (top, bottom)): ((usize, usize), (usize, usize)),
    ) -> Margins {
        (left, right, top, bottom).into()
    }
}

impl<T: Into<Margins>> Add<T> for Margins {
    type Output = Margins;

    fn add(self, other: T) -> Margins {
        let ov = other.into();

        Margins {
            left: self.left + ov.left,
            right: self.right + ov.right,
            top: self.top + ov.top,
            bottom: self.bottom + ov.bottom,
        }
    }
}

impl<T: Into<Margins>> Sub<T> for Margins {
    type Output = Margins;

    fn sub(self, other: T) -> Margins {
        let ov = other.into();

        Margins {
            left: self.left - ov.left,
            right: self.right - ov.right,
            top: self.top - ov.top,
            bottom: self.bottom - ov.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,
        }
    }
}