1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::text::TextSize;

#[derive(Copy, Clone, Debug)]
pub enum TextPos {
    Px(isize, isize),
    /// See [TextSize::get_max_characters] for maximum x and y
    ColRow(usize, usize),
}

impl TextPos {
    pub fn to_px(&self, size: TextSize) -> (isize, isize) {
        match self {
            TextPos::Px(x, y) => (*x, *y),
            TextPos::ColRow(col, row) => (
                (col * (size.get_size().0 + size.get_spacing())) as isize,
                (row * (size.get_size().1 + size.get_spacing())) as isize,
            ),
        }
    }
}

macro_rules! impl_to_px {
    ($num_type: ty) => {
        impl From<($num_type, $num_type)> for TextPos {
            fn from((x, y): ($num_type, $num_type)) -> Self {
                TextPos::Px(x as isize, y as isize)
            }
        }
    };
}

macro_rules! impl_to_colrow {
    ($num_type: ty) => {
        impl From<($num_type, $num_type)> for TextPos {
            fn from((x, y): ($num_type, $num_type)) -> Self {
                TextPos::ColRow(x as usize, y as usize)
            }
        }
    };
}

impl_to_px!(isize);
impl_to_px!(i8);
impl_to_px!(i16);
impl_to_px!(i32);
impl_to_px!(i64);
impl_to_px!(i128);

impl_to_colrow!(usize);
impl_to_colrow!(u8);
impl_to_colrow!(u16);
impl_to_colrow!(u32);
impl_to_colrow!(u64);
impl_to_colrow!(u128);

impl TextPos {
    pub fn usize_px(x: usize, y: usize) -> TextPos {
        TextPos::Px(x as isize, y as isize)
    }

    pub fn f32_px(x: f32, y: f32) -> TextPos {
        TextPos::Px(x as isize, y as isize)
    }
}