diff options
Diffstat (limited to 'src/geom/align.rs')
| -rw-r--r-- | src/geom/align.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/geom/align.rs b/src/geom/align.rs new file mode 100644 index 00000000..1030a133 --- /dev/null +++ b/src/geom/align.rs @@ -0,0 +1,48 @@ +use super::*; + +/// Where to align something along a directed axis. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] +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, +} + +impl Align { + /// Returns the position of this alignment in the given range. + pub fn apply(self, range: Range<Length>) -> Length { + match self { + Self::Start => range.start, + Self::Center => (range.start + range.end) / 2.0, + Self::End => range.end, + } + } + + /// The inverse alignment. + pub fn inv(self) -> Self { + match self { + Self::Start => Self::End, + Self::Center => Self::Center, + Self::End => Self::Start, + } + } +} + +impl Default for Align { + fn default() -> Self { + Self::Start + } +} + +impl Display for Align { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.pad(match self { + Self::Start => "start", + Self::Center => "center", + Self::End => "end", + }) + } +} |
