diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-11-17 17:09:19 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-11-17 17:09:19 +0100 |
| commit | 89f2e71852e96062ea9b756bf92fbf4e894871b1 (patch) | |
| tree | 836099ebd17adf30a24fc62464dfdd3d9c248480 /src/geom | |
| parent | 9a800daa82833c57eee04e92c701ca9a05a67d3b (diff) | |
Align node
Diffstat (limited to 'src/geom')
| -rw-r--r-- | src/geom/align.rs | 64 | ||||
| -rw-r--r-- | src/geom/spec.rs | 13 |
2 files changed, 33 insertions, 44 deletions
diff --git a/src/geom/align.rs b/src/geom/align.rs index 59960084..b0cf69db 100644 --- a/src/geom/align.rs +++ b/src/geom/align.rs @@ -3,18 +3,14 @@ use super::*; /// Where to align something along an axis. #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum Align { - /// Align at the start of the axis. - Start, - /// Align in the middle of the axis. - Center, - /// Align at the end of the axis. - End, /// Align at the left side of the axis. Left, - /// Align at the right side of the axis. - Right, /// Align at the top side of the axis. Top, + /// Align in the middle of the axis. + Center, + /// Align at the right side of the axis. + Right, /// Align at the bottom side of the axis. Bottom, } @@ -23,12 +19,10 @@ impl Align { /// The axis this alignment belongs to if it is specific. pub fn axis(self) -> Option<SpecAxis> { match self { - Self::Start => None, - Self::Center => None, - Self::End => None, Self::Left => Some(SpecAxis::Horizontal), - Self::Right => Some(SpecAxis::Horizontal), Self::Top => Some(SpecAxis::Vertical), + Self::Center => None, + Self::Right => Some(SpecAxis::Horizontal), Self::Bottom => Some(SpecAxis::Vertical), } } @@ -36,60 +30,42 @@ impl Align { /// The inverse alignment. pub fn inv(self) -> Self { match self { - Self::Start => Self::End, - Self::Center => Self::Center, - Self::End => Self::Start, Self::Left => Self::Right, - Self::Right => Self::Left, Self::Top => Self::Bottom, + Self::Center => Self::Center, + Self::Right => Self::Left, Self::Bottom => Self::Top, } } /// Returns the position of this alignment in the given range. - pub fn resolve(self, dir: Dir, range: Range<Length>) -> Length { - #[cfg(debug_assertions)] - if let Some(axis) = self.axis() { - debug_assert_eq!(axis, dir.axis()) - } - + pub fn resolve(self, range: Range<Length>) -> Length { match self { - Self::Start => { - if dir.is_positive() { - range.start - } else { - range.end - } - } - Self::Center => (range.start + range.end) / 2.0, - Self::End => { - if dir.is_positive() { - range.end - } else { - range.start - } - } Self::Left | Self::Top => range.start, Self::Right | Self::Bottom => range.end, + Self::Center => (range.start + range.end) / 2.0, } } } -impl Default for Align { - fn default() -> Self { - Self::Start +impl From<Side> for Align { + fn from(side: Side) -> Self { + match side { + Side::Left => Self::Left, + Side::Top => Self::Top, + Side::Right => Self::Right, + Side::Bottom => Self::Bottom, + } } } impl Debug for Align { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.pad(match self { - Self::Start => "start", - Self::Center => "center", - Self::End => "end", Self::Left => "left", - Self::Right => "right", Self::Top => "top", + Self::Center => "center", + Self::Right => "right", Self::Bottom => "bottom", }) } diff --git a/src/geom/spec.rs b/src/geom/spec.rs index 4d631399..3b49b54a 100644 --- a/src/geom/spec.rs +++ b/src/geom/spec.rs @@ -23,6 +23,11 @@ impl<T> Spec<T> { Self { x: v.clone(), y: v } } + /// Borrows the individual fields. + pub fn as_ref(&self) -> Spec<&T> { + Spec { x: &self.x, y: &self.y } + } + /// Maps the individual fields with `f`. pub fn map<F, U>(self, mut f: F) -> Spec<U> where @@ -40,6 +45,14 @@ impl<T> Spec<T> { } } + /// Whether a condition is true for at least one of fields. + pub fn any<F>(self, mut f: F) -> bool + where + F: FnMut(&T) -> bool, + { + f(&self.x) || f(&self.y) + } + /// Whether a condition is true for both fields. pub fn all<F>(self, mut f: F) -> bool where |
