2019-02-28 23:54:12 +00:00
|
|
|
use crate::vec::Vec2;
|
2019-03-01 00:04:14 +00:00
|
|
|
use std::ops::{Add, Div, Mul, Sub};
|
2018-03-16 23:09:47 +00:00
|
|
|
|
|
|
|
/// 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 {
|
2018-04-10 18:53:25 +00:00
|
|
|
left,
|
|
|
|
right,
|
|
|
|
top,
|
|
|
|
bottom,
|
2018-03-16 23:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
2018-04-10 18:53:25 +00:00
|
|
|
fn from(
|
|
|
|
(left, right, top, bottom): (usize, usize, usize, usize),
|
|
|
|
) -> Margins {
|
2018-03-16 23:09:47 +00:00
|
|
|
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 {
|
2018-06-11 06:29:10 +00:00
|
|
|
(left as usize, right as usize, top as usize, bottom as usize).into()
|
2018-03-16 23:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<((i32, i32), (i32, i32))> for Margins {
|
2018-04-10 18:53:25 +00:00
|
|
|
fn from(
|
|
|
|
((left, right), (top, bottom)): ((i32, i32), (i32, i32)),
|
|
|
|
) -> Margins {
|
2018-03-16 23:09:47 +00:00
|
|
|
(left, right, top, bottom).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<((usize, usize), (usize, usize))> for Margins {
|
|
|
|
fn from(
|
2018-04-10 18:53:25 +00:00
|
|
|
((left, right), (top, bottom)): ((usize, usize), (usize, usize)),
|
2018-03-16 23:09:47 +00:00
|
|
|
) -> 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|