use super::Vector;
#[derive(Clone, PartialEq, Default)]
pub struct Rect {
pub top: f32,
pub bottom: f32,
pub left: f32,
pub right: f32,
}
impl Rect {
pub fn new() -> RectBuilder {
RectBuilder::default()
}
}
#[derive(Default)]
pub struct RectBuilder {
top: f32,
bottom: f32,
left: f32,
right: f32,
}
impl RectBuilder {
pub fn top(mut self, top: f32) -> Self {
self.top = top;
self
}
pub fn bottom(mut self, bottom: f32) -> Self {
self.bottom = bottom;
self
}
pub fn left(mut self, left: f32) -> Self {
self.left = left;
self
}
pub fn right(mut self, right: f32) -> Self {
self.right = right;
self
}
pub fn with_pos(mut self, pos: Vector) -> Self {
self.top = pos.1;
self.bottom = pos.1;
self.left = pos.0;
self.right = pos.0;
self
}
pub fn with_pos_and_size(mut self, pos: Vector, size: Vector) -> Self {
self.top = pos.1 + size.1 * 0.5;
self.bottom = pos.1 - size.1 * 0.5;
self.left = pos.0 - size.0 * 0.5;
self.right = pos.0 + size.0 * 0.5;
self
}
pub fn with_pos_and_maybe_size(
mut self,
pos: Vector,
size_opt: Option<Vector>,
) -> Self {
self = if let Some(size) = size_opt {
self.with_pos_and_size(pos, size)
} else {
self.with_pos(pos)
};
self
}
pub fn build(self) -> Rect {
let RectBuilder {
top,
bottom,
left,
right,
} = self;
Rect {
top,
bottom,
left,
right,
}
}
}