aboutsummaryrefslogtreecommitdiff
path: root/src/brush.rs
blob: 8d3ee1c932e389284bab4eda0488abb476a376a6 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::fmt;

use crate::bitmap::MapPoint;

#[derive(Debug, Copy, Clone)]
pub enum Brush {
    Line(LineBrush),
    Circle(CircleBrush),
    RectSelect(RectSelectBrush),
    Fill,
    Custom { size: u8 },
}

#[derive(Debug, Copy, Clone)]
pub struct LineBrush {
    pub size: u8,
    pub start: Option<MapPoint>,
    pub extend: bool,
}

#[derive(Debug, Copy, Clone)]
pub struct CircleBrush {
    pub size: u8,
}

#[derive(Debug, Copy, Clone)]
pub struct RectSelectBrush {
    pub start: Option<MapPoint>,
    pub end: Option<MapPoint>,
    pub active_end: bool,
}

impl Brush {
    pub fn grow(&mut self) {
        match self {
            Brush::Line(LineBrush { ref mut size, .. }) => *size += 1,
            Brush::Circle(CircleBrush { ref mut size, .. }) => *size += 1,
            Brush::Custom { ref mut size, .. } => *size += 1,
            _ => (),
        }
    }

    pub fn shrink(&mut self) {
        match self {
            Brush::Line(LineBrush { ref mut size, .. }) => *size = size.saturating_sub(1),
            Brush::Circle(CircleBrush { ref mut size, .. }) => *size = size.saturating_sub(1),
            Brush::Custom { ref mut size, .. } => *size = size.saturating_sub(1),
            _ => (),
        }
    }

    pub fn new(size: u8) -> Self {
        Brush::Circle(CircleBrush { size })
    }

    pub fn line(size: u8, extend: bool) -> Self {
        Brush::Line(LineBrush {
            size,
            start: None,
            extend,
        })
    }

    pub fn rect() -> Self {
        Brush::RectSelect(RectSelectBrush {
            start: None,
            end: None,
            active_end: true,
        })
    }

    pub fn is_line(&self) -> bool {
        matches!(self, Self::Line(_))
    }

    pub fn is_rect(&self) -> bool {
        matches!(self, Self::RectSelect(_))
    }

    pub fn size(&self) -> Option<u8> {
        match self {
            Brush::Line(LineBrush { size, .. }) => Some(*size),
            Brush::Circle(CircleBrush { size }) => Some(*size),
            _ => None,
        }
    }
}

impl fmt::Display for Brush {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Brush::Line(LineBrush { extend, .. }) => {
                write!(f, "LINE{}", if *extend { "+" } else { "" })
            }
            Brush::Circle(..) => write!(f, "CIRCLE"),
            Brush::RectSelect { .. } => write!(f, "SELECT"),
            Brush::Fill => write!(f, "FILL"),
            Brush::Custom { .. } => write!(f, "CUSTOM"),
        }
    }
}