2020-01-06 23:51:38 +00:00
|
|
|
use crate::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.
|
2020-01-10 18:51:48 +00:00
|
|
|
#[deprecated(
|
|
|
|
note = "`Margins::new()` is ambiguous. Use `Margins::lrtb()` instead."
|
|
|
|
)]
|
2018-03-16 23:09:47 +00:00
|
|
|
pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
|
2020-01-10 18:51:48 +00:00
|
|
|
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 {
|
2018-03-16 23:09:47 +00:00
|
|
|
Margins {
|
2018-04-10 18:53:25 +00:00
|
|
|
left,
|
|
|
|
right,
|
|
|
|
top,
|
|
|
|
bottom,
|
2018-03-16 23:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:51:48 +00:00
|
|
|
/// 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)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:51:48 +00:00
|
|
|
impl Add<Margins> for Margins {
|
2018-03-16 23:09:47 +00:00
|
|
|
type Output = Margins;
|
|
|
|
|
2020-01-10 18:51:48 +00:00
|
|
|
fn add(self, other: Margins) -> Margins {
|
2018-03-16 23:09:47 +00:00
|
|
|
Margins {
|
2020-01-10 18:51:48 +00:00
|
|
|
left: self.left + other.left,
|
|
|
|
right: self.right + other.right,
|
|
|
|
top: self.top + other.top,
|
|
|
|
bottom: self.bottom + other.bottom,
|
2018-03-16 23:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:51:48 +00:00
|
|
|
impl Sub<Margins> for Margins {
|
2018-03-16 23:09:47 +00:00
|
|
|
type Output = Margins;
|
|
|
|
|
2020-01-10 18:51:48 +00:00
|
|
|
fn sub(self, other: Margins) -> Margins {
|
2018-03-16 23:09:47 +00:00
|
|
|
Margins {
|
2020-01-10 18:51:48 +00:00
|
|
|
left: self.left - other.left,
|
|
|
|
right: self.right - other.right,
|
|
|
|
top: self.top - other.top,
|
|
|
|
bottom: self.bottom - other.bottom,
|
2018-03-16 23:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|