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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::color::conv::IntoLinSrgba;
use crate::draw::drawing::DrawingContext;
use crate::draw::primitive::Primitive;
use crate::draw::properties::spatial::{self, orientation, position};
use crate::draw::properties::{
    ColorScalar, Draw, Drawn, IndicesChain, IndicesFromRange, IntoDrawn, LinSrgba, SetColor,
    SetOrientation, SetPosition, SetStroke, VerticesChain, VerticesFromRanges,
};
use crate::draw::{self, theme, Drawing};
use crate::geom::{self, Point2};
use crate::math::BaseFloat;
use lyon::path::iterator::FlattenedIterator;
use lyon::path::PathEvent;
use lyon::tessellation::{StrokeOptions, StrokeTessellator};
use std::ops;

/// A trait implemented for all polygon draw primitives.
pub trait SetPolygon<S>: Sized {
    /// Access to the polygon builder parameters.
    fn polygon_options_mut(&mut self) -> &mut PolygonOptions<S>;

    /// Specify no fill color and in turn no fill tessellation for the polygon.
    fn no_fill(mut self) -> Self {
        self.polygon_options_mut().no_fill = true;
        self
    }

    /// Specify a color to use for stroke tessellation.
    ///
    /// Stroke tessellation will only be performed if this method or one of the `SetStroke` methods
    /// are called.
    fn stroke_color<C>(mut self, color: C) -> Self
    where
        C: IntoLinSrgba<ColorScalar>,
    {
        self.polygon_options_mut().stroke_color = Some(color.into_lin_srgba());
        self
    }

    /// Specify the whole set of polygon options.
    fn polygon_options(mut self, opts: PolygonOptions<S>) -> Self {
        *self.polygon_options_mut() = opts;
        self
    }
}

/// State related to drawing a **Polygon**.
#[derive(Clone, Debug)]
pub struct PolygonInit<S = geom::scalar::Default> {
    opts: PolygonOptions<S>,
}

/// The set of options shared by all polygon types.
#[derive(Clone, Debug)]
pub struct PolygonOptions<S = geom::scalar::Default> {
    position: position::Properties<S>,
    orientation: orientation::Properties<S>,
    no_fill: bool,
    stroke_color: Option<LinSrgba>,
    color: Option<LinSrgba>,
    stroke: Option<StrokeOptions>,
}

/// A polygon with vertices already submitted.
#[derive(Clone, Debug)]
pub struct Polygon<S = geom::scalar::Default> {
    position: position::Properties<S>,
    orientation: orientation::Properties<S>,
    color: Option<LinSrgba>,
    stroke_color: Option<LinSrgba>,
    vertex_data_ranges: (
        draw::IntermediaryVertexDataRanges,
        draw::IntermediaryVertexDataRanges,
    ),
    index_ranges: (ops::Range<usize>, ops::Range<usize>),
    min_index: usize,
}

/// Initialised drawing state for a polygon.
pub type DrawingPolygonInit<'a, S = geom::scalar::Default> = Drawing<'a, PolygonInit<S>, S>;

/// Initialised drawing state for a polygon.
pub type DrawingPolygon<'a, S = geom::scalar::Default> = Drawing<'a, Polygon<S>, S>;

pub(crate) type PolygonVertices = VerticesChain<VerticesFromRanges, VerticesFromRanges>;
pub(crate) type PolygonIndices = IndicesChain<IndicesFromRange, IndicesFromRange>;

pub type DrawnPolygon<S> = Drawn<S, PolygonVertices, PolygonIndices>;

impl<S> PolygonInit<S> {
    /// Stroke the outline with the given color.
    pub fn stroke<C>(self, color: C) -> Self
    where
        C: IntoLinSrgba<ColorScalar>,
    {
        self.stroke_color(color)
    }

    /// Submit the path events to be tessellated.
    pub(crate) fn events<I>(self, ctxt: DrawingContext<S>, events: I) -> Polygon<S>
    where
        S: BaseFloat,
        I: IntoIterator<Item = PathEvent>,
    {
        let DrawingContext {
            mesh,
            fill_tessellator,
            path_event_buffer,
            ..
        } = ctxt;

        path_event_buffer.clear();
        path_event_buffer.extend(events);

        // Fill tessellation.
        let (fill_vdr, fill_ir, min_index) = if !self.opts.no_fill {
            let mut builder = mesh.builder();
            let opts = Default::default();
            let events = path_event_buffer.iter().cloned();
            let res = fill_tessellator.tessellate_path(events, &opts, &mut builder);
            if let Err(err) = res {
                eprintln!("fill tessellation failed: {:?}", err);
            }
            (
                builder.vertex_data_ranges(),
                builder.index_range(),
                builder.min_index(),
            )
        } else {
            let builder = mesh.builder();
            (
                builder.vertex_data_ranges(),
                builder.index_range(),
                builder.min_index(),
            )
        };

        // Stroke tessellation.
        let (stroke_vdr, stroke_ir) = match (self.opts.stroke, self.opts.stroke_color) {
            (options, color) if options.is_some() || color.is_some() => {
                let opts = options.unwrap_or_else(Default::default);
                let mut builder = mesh.builder();
                let mut stroke_tessellator = StrokeTessellator::default();
                let events = path_event_buffer.drain(..);
                let res = stroke_tessellator.tessellate_path(events, &opts, &mut builder);
                if let Err(err) = res {
                    eprintln!("stroke tessellation failed: {:?}", err);
                }
                let stroke_vdr = builder.vertex_data_ranges();
                let stroke_ir = builder.index_range();
                (stroke_vdr, stroke_ir)
            }
            _ => (Default::default(), 0..0),
        };

        path_event_buffer.clear();

        Polygon {
            position: self.opts.position,
            orientation: self.opts.orientation,
            color: self.opts.color,
            stroke_color: self.opts.stroke_color,
            vertex_data_ranges: (fill_vdr, stroke_vdr),
            index_ranges: (fill_ir, stroke_ir),
            min_index,
        }
    }

    /// Consumes an iterator of points and converts them to an iterator yielding path events.
    pub fn points<I>(self, ctxt: DrawingContext<S>, points: I) -> Polygon<S>
    where
        S: BaseFloat,
        I: IntoIterator,
        I::Item: Into<Point2<S>>,
    {
        let points = points.into_iter().map(|p| {
            let p: Point2<f32> = p.into().cast().expect("failed to cast point");
            p.into()
        });
        let close = true;
        let events = lyon::path::iterator::FromPolyline::new(close, points).path_events();
        self.events(ctxt, events)
    }
}

impl<'a, S, T> Drawing<'a, T, S>
where
    S: BaseFloat,
    T: SetPolygon<S> + Into<Primitive<S>>,
    Primitive<S>: Into<Option<T>>,
{
    /// Specify no fill color and in turn no fill tessellation for the polygon.
    pub fn no_fill(self) -> Self {
        self.map_ty(|ty| ty.no_fill())
    }

    /// Specify a color to use for stroke tessellation.
    ///
    /// Stroke tessellation will only be performed if this method or one of the `SetStroke` methods
    /// are called.
    pub fn stroke_color<C>(self, color: C) -> Self
    where
        C: IntoLinSrgba<ColorScalar>,
    {
        self.map_ty(|ty| ty.stroke_color(color))
    }

    /// Specify the whole set of polygon options.
    pub fn polygon_options(self, opts: PolygonOptions<S>) -> Self {
        self.map_ty(|ty| ty.polygon_options(opts))
    }
}

impl<'a, S> DrawingPolygonInit<'a, S>
where
    S: BaseFloat,
{
    /// Stroke the outline with the given color.
    pub fn stroke<C>(self, color: C) -> Self
    where
        C: IntoLinSrgba<ColorScalar>,
    {
        self.map_ty(|ty| ty.stroke(color))
    }

    /// Describe the polygon with a sequence of path events.
    pub fn events<I>(self, events: I) -> DrawingPolygon<'a, S>
    where
        S: BaseFloat,
        I: IntoIterator<Item = PathEvent>,
    {
        self.map_ty_with_context(|ty, ctxt| ty.events(ctxt, events))
    }

    /// Describe the polygon with a sequence of points.
    pub fn points<I>(self, points: I) -> DrawingPolygon<'a, S>
    where
        S: BaseFloat,
        I: IntoIterator,
        I::Item: Into<Point2<S>>,
    {
        self.map_ty_with_context(|ty, ctxt| ty.points(ctxt, points))
    }
}

impl<S> Polygon<S> {
    /// The implementation of `into_drawn` that allows for the retrieval of different theming
    /// defaults.
    pub(crate) fn into_drawn_themed(self, draw: Draw<S>, p: &theme::Primitive) -> DrawnPolygon<S>
    where
        S: BaseFloat,
    {
        let Polygon {
            position,
            orientation,
            color,
            stroke_color,
            vertex_data_ranges: (fill_vdr, stroke_vdr),
            index_ranges: (fill_ir, stroke_ir),
            min_index,
        } = self;

        let fill_color = match fill_ir.len() == 0 {
            true => None,
            false => color.or_else(|| Some(draw.theme().fill_lin_srgba(p))),
        };
        let stroke_color = match stroke_ir.len() == 0 {
            true => None,
            false => stroke_color.or_else(|| Some(draw.theme().stroke_lin_srgba(p))),
        };

        let fill_vertices = VerticesFromRanges::new(fill_vdr, fill_color);
        let fill_indices = IndicesFromRange::new(fill_ir, min_index);
        let stroke_vertices = VerticesFromRanges::new(stroke_vdr, stroke_color);
        let stroke_indices = IndicesFromRange::new(stroke_ir, min_index);
        let vertices = (fill_vertices, stroke_vertices).into();
        let indices = (fill_indices, stroke_indices).into();
        let dimensions = spatial::dimension::Properties::default();
        let spatial = spatial::Properties {
            dimensions,
            orientation,
            position,
        };

        (spatial, vertices, indices)
    }
}

impl<S> IntoDrawn<S> for Polygon<S>
where
    S: BaseFloat,
{
    type Vertices = PolygonVertices;
    type Indices = PolygonIndices;
    fn into_drawn(self, draw: Draw<S>) -> DrawnPolygon<S> {
        self.into_drawn_themed(draw, &theme::Primitive::Polygon)
    }
}

impl<S> Default for PolygonInit<S> {
    fn default() -> Self {
        let opts = Default::default();
        PolygonInit { opts }
    }
}

impl<S> Default for PolygonOptions<S> {
    fn default() -> Self {
        let position = Default::default();
        let orientation = Default::default();
        let no_fill = false;
        let color = None;
        let stroke_color = None;
        let stroke = None;
        PolygonOptions {
            position,
            orientation,
            no_fill,
            color,
            stroke_color,
            stroke,
        }
    }
}

impl<S> SetPolygon<S> for PolygonOptions<S> {
    fn polygon_options_mut(&mut self) -> &mut PolygonOptions<S> {
        self
    }
}

impl<S> SetOrientation<S> for PolygonInit<S> {
    fn properties(&mut self) -> &mut orientation::Properties<S> {
        SetOrientation::properties(&mut self.opts.orientation)
    }
}

impl<S> SetPosition<S> for PolygonInit<S> {
    fn properties(&mut self) -> &mut position::Properties<S> {
        SetPosition::properties(&mut self.opts.position)
    }
}

impl<S> SetColor<ColorScalar> for PolygonInit<S> {
    fn rgba_mut(&mut self) -> &mut Option<LinSrgba> {
        SetColor::rgba_mut(&mut self.opts.color)
    }
}

impl<S> SetPolygon<S> for PolygonInit<S> {
    fn polygon_options_mut(&mut self) -> &mut PolygonOptions<S> {
        SetPolygon::polygon_options_mut(&mut self.opts)
    }
}

impl<S> SetStroke for PolygonInit<S> {
    fn stroke_options_mut(&mut self) -> &mut StrokeOptions {
        SetStroke::stroke_options_mut(&mut self.opts.stroke)
    }
}

impl<S> SetOrientation<S> for Polygon<S> {
    fn properties(&mut self) -> &mut orientation::Properties<S> {
        SetOrientation::properties(&mut self.orientation)
    }
}

impl<S> SetPosition<S> for Polygon<S> {
    fn properties(&mut self) -> &mut position::Properties<S> {
        SetPosition::properties(&mut self.position)
    }
}

impl<S> SetColor<ColorScalar> for Polygon<S> {
    fn rgba_mut(&mut self) -> &mut Option<LinSrgba> {
        SetColor::rgba_mut(&mut self.color)
    }
}

impl<S> From<PolygonInit<S>> for Primitive<S> {
    fn from(prim: PolygonInit<S>) -> Self {
        Primitive::PolygonInit(prim)
    }
}

impl<S> From<Polygon<S>> for Primitive<S> {
    fn from(prim: Polygon<S>) -> Self {
        Primitive::Polygon(prim)
    }
}

impl<S> Into<Option<PolygonInit<S>>> for Primitive<S> {
    fn into(self) -> Option<PolygonInit<S>> {
        match self {
            Primitive::PolygonInit(prim) => Some(prim),
            _ => None,
        }
    }
}

impl<S> Into<Option<Polygon<S>>> for Primitive<S> {
    fn into(self) -> Option<Polygon<S>> {
        match self {
            Primitive::Polygon(prim) => Some(prim),
            _ => None,
        }
    }
}