summaryrefslogtreecommitdiffstats
path: root/core/src/styles.rs
blob: e0bfc802f5ffd730576d3d772d125f9936e31ebf (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use super::colors::Color;
use super::types::Float;

#[derive(Copy, Clone, PartialEq, Debug)]
#[non_exhaustive]
pub enum Pattern {
    Solid(Color),
    None,
}

impl Pattern {
    pub fn is_none(&self) -> bool {
        matches!(self, Pattern::None)
    }
}

impl Default for Pattern {
    fn default() -> Self {
        Pattern::Solid(Color::default())
    }
}

#[derive(Clone, PartialEq, Debug)]
pub struct FillStyle {
    pub(crate) pattern: Pattern,
}

impl FillStyle {
    pub fn solid(color: Color) -> Self {
        FillStyle {
            pattern: Pattern::Solid(color),
        }
    }

    pub fn pattern(&self) -> Pattern {
        self.pattern
    }

    pub fn set_pattern(&mut self, pattern: Pattern) {
        self.pattern = pattern;
    }
}

impl Default for FillStyle {
    fn default() -> Self {
        FillStyle {
            pattern: Pattern::None,
        }
    }
}

// TODO: probably move Float into an enum with Defined(f64) and Variable(handle)
#[derive(Clone, Debug)]
pub struct StrokeStyle {
    pub(crate) pattern: Pattern,
    pub(crate) dash: Option<DashStyle>,
    pub(crate) line_width: Option<Float>,
}

impl StrokeStyle {
    pub fn solid(color: Color) -> Self {
        StrokeStyle {
            pattern: Pattern::Solid(color),
            dash: None,
            line_width: None,
        }
    }

    pub fn builder() -> StrokeStyleBuilder {
        StrokeStyleBuilder::default()
    }

    pub fn pattern(&self) -> Pattern {
        self.pattern
    }

    pub fn set_pattern(&mut self, pattern: Pattern) {
        self.pattern = pattern;
    }

    pub fn dash(&self) -> Option<&DashStyle> {
        self.dash.as_ref()
    }

    pub fn set_dash(&mut self, dash: Option<DashStyle>) {
        self.dash = dash;
    }

    pub fn line_width(&self) -> Option<Float> {
        self.line_width
    }

    pub fn set_line_width(&mut self, line_width: Option<Float>) {
        self.line_width = line_width;
    }
}

impl Default for StrokeStyle {
    fn default() -> Self {
        Self::builder().build()
    }
}

#[derive(Clone, Default)]
pub struct StrokeStyleBuilder {
    pattern: Option<Pattern>,
    dash: Option<DashStyle>,
    line_width: Option<Float>,
}

impl StrokeStyleBuilder {
    pub fn pattern(&mut self, pattern: Pattern) -> &mut Self {
        self.pattern = Some(pattern);
        self
    }

    pub fn dash(&mut self, dash: DashStyle) -> &mut Self {
        self.dash = Some(dash);
        self
    }

    pub fn line_width(&mut self, width: Float) -> &mut Self {
        self.line_width = Some(width);
        self
    }

    pub fn build(&mut self) -> StrokeStyle {
        StrokeStyle {
            pattern: self.pattern.unwrap_or_default(),
            dash: self.dash.clone(),
            line_width: self.line_width,
        }
    }

}

#[derive(Clone, Debug, Default)]
pub struct DashStyle {
    pub(crate) dashes: Vec<Float>,
    pub(crate) offset: Float,
}

impl DashStyle {
    pub fn new(dashes: Vec<Float>, offset: Float) -> Self {
        Self { dashes, offset }
    }

    pub fn dashes(&self) -> &[Float] {
        &self.dashes
    }

    pub fn dashes_mut(&mut self) -> &mut Vec<Float> {
        &mut self.dashes
    }

    pub fn offset(&self) -> Float {
        self.offset
    }

    // TODO: does this makes sense?
    pub fn offset_mut(&mut self) -> &mut Float {
        &mut self.offset
    }
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct DefinedStrokeStyle {
    pub(crate) pattern: Pattern,
    pub(crate) dash: Option<DefinedDashStyle>,
    pub(crate) line_width: f64,
}

#[derive(Clone, PartialEq, Debug, Default)]
pub struct DefinedDashStyle {
    pub(crate) dashes: Vec<f64>,
    pub(crate) offset: f64,
}

impl DefinedDashStyle {
    pub fn dashes(&self) -> &[f64] {
        &self.dashes
    }

    pub fn offset(&self) -> f64 {
        self.offset
    }
}