use super::*;
impl Length {
#[must_use]
pub fn resolve(&self, parent_extent: f32, viewport: Size) -> Option<f32> {
match *self {
Self::Auto | Self::Fraction(_) => None,
Self::Px(px) => Some(px),
Self::Percent(pct) => Some(parent_extent * (pct * 0.01)),
Self::ViewportWidth(vw) => Some(viewport.get_width() * (vw * 0.01)),
Self::ViewportHeight(vh) => Some(viewport.get_height() * (vh * 0.01)),
}
}
}
impl Edges {
#[must_use]
pub const fn all(value: f32) -> Self {
Self {
top: value,
right: value,
bottom: value,
left: value,
}
}
#[must_use]
pub const fn symmetric(vertical: f32, horizontal: f32) -> Self {
Self {
top: vertical,
right: horizontal,
bottom: vertical,
left: horizontal,
}
}
#[must_use]
pub fn horizontal(&self) -> f32 {
self.get_left() + self.get_right()
}
#[must_use]
pub fn vertical(&self) -> f32 {
self.get_top() + self.get_bottom()
}
}
impl CornerRadius {
#[must_use]
pub const fn all(radius: f32) -> Self {
Self {
top_left: radius,
top_right: radius,
bottom_right: radius,
bottom_left: radius,
}
}
#[must_use]
pub fn max_radius(&self) -> f32 {
self.get_top_left()
.max(self.get_top_right())
.max(self.get_bottom_right())
.max(self.get_bottom_left())
}
}
impl Rect {
#[must_use]
pub fn right(&self) -> f32 {
self.get_x() + self.get_width()
}
#[must_use]
pub fn bottom(&self) -> f32 {
self.get_y() + self.get_height()
}
#[must_use]
pub fn contains(&self, point: Point) -> bool {
point.get_x() >= self.get_x()
&& point.get_x() < self.right()
&& point.get_y() >= self.get_y()
&& point.get_y() < self.bottom()
}
#[must_use]
pub fn intersect(&self, other: Self) -> Option<Self> {
let x0: f32 = self.get_x().max(other.get_x());
let y0: f32 = self.get_y().max(other.get_y());
let x1: f32 = self.right().min(other.right());
let y1: f32 = self.bottom().min(other.bottom());
if x1 > x0 && y1 > y0 {
Some(Self::new(x0, y0, x1 - x0, y1 - y0))
} else {
None
}
}
#[must_use]
pub fn union(&self, other: Self) -> Self {
if self.get_width() <= 0.0 || self.get_height() <= 0.0 {
return other;
}
if other.get_width() <= 0.0 || other.get_height() <= 0.0 {
return *self;
}
let x0: f32 = self.get_x().min(other.get_x());
let y0: f32 = self.get_y().min(other.get_y());
let x1: f32 = self.right().max(other.right());
let y1: f32 = self.bottom().max(other.bottom());
Self::new(x0, y0, x1 - x0, y1 - y0)
}
#[must_use]
pub fn inset(&self, edges: Edges) -> Self {
let w: f32 = (self.get_width() - edges.horizontal()).max(0.0);
let h: f32 = (self.get_height() - edges.vertical()).max(0.0);
Self::new(
self.get_x() + edges.get_left(),
self.get_y() + edges.get_top(),
w,
h,
)
}
}