1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use crate::elements::Vec2D;

/// An enum to set the alignment of a Text element's content
#[derive(Debug, Clone, Copy)]
pub enum TextAlign {
    /// Align to the beginning of the text
    Left,
    /// Align to the center of the text
    Centered,
    /// Align to the end of the text
    Right,
}

impl TextAlign {
    /// Align the given position as dictated by the `TextAlign` enum variation
    pub fn apply_to(&self, pos: Vec2D, text_length: isize) -> Vec2D {
        match self {
            TextAlign::Left => pos,
            TextAlign::Centered => pos - Vec2D::new(text_length / 2, 0),
            TextAlign::Right => pos - Vec2D::new(text_length, 0),
        }
    }
}