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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use solstice::{mesh::MappedIndexedMesh, texture::Texture, Context};

mod canvas;
mod shader;
mod shapes;
mod text;
mod transforms;
mod vertex;

use vertex::{Point, Vertex2D};

pub use canvas::Canvas;
pub use glyph_brush::{ab_glyph::FontVec, FontId};
pub use shader::Shader2D;
pub use shapes::*;
pub use transforms::*;

#[derive(Copy, Clone, Eq, PartialEq)]
pub enum DrawMode {
    Fill,
    Stroke,
}

#[derive(Debug)]
pub enum Graphics2DError {
    ShaderError(shader::Shader2DError),
    GraphicsError(solstice::GraphicsError),
}

impl std::fmt::Display for Graphics2DError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for Graphics2DError {}

impl From<solstice::GraphicsError> for Graphics2DError {
    fn from(err: solstice::GraphicsError) -> Self {
        Graphics2DError::GraphicsError(err)
    }
}

impl From<shader::Shader2DError> for Graphics2DError {
    fn from(err: shader::Shader2DError) -> Self {
        Graphics2DError::ShaderError(err)
    }
}

#[must_use]
pub struct Graphics2DLock<'a, 's> {
    ctx: &'a mut Context,
    inner: &'a mut Graphics2D,
    color: [f32; 4],
    index_offset: usize,
    vertex_offset: usize,
    pub transforms: Transforms,

    active_shader: Option<&'s mut Shader2D>,
    active_canvas: Option<&'s Canvas>,
}

impl<'a, 's> Graphics2DLock<'a, 's> {
    fn flush(&mut self) {
        let mesh = self.inner.mesh.unmap(self.ctx);

        let geometry = solstice::Geometry {
            mesh,
            draw_range: 0..self.index_offset,
            draw_mode: solstice::DrawMode::Triangles,
            instance_count: 1,
        };

        let shader = match self.active_shader.as_mut() {
            None => &mut self.inner.default_shader,
            Some(shader) => *shader,
        };
        let invert_y = self.active_canvas.is_some();
        shader.set_width_height(self.inner.width, self.inner.height, invert_y);
        shader.activate(self.ctx);
        solstice::Renderer::draw(
            self.ctx,
            shader,
            &geometry,
            solstice::PipelineSettings {
                depth_state: None,
                framebuffer: self.active_canvas.map(|c| &c.inner),
                ..solstice::PipelineSettings::default()
            },
        );

        self.index_offset = 0;
        self.vertex_offset = 0;
    }

    pub fn clear<C: Into<[f32; 4]>>(&mut self, color: C) {
        let [red, green, blue, alpha] = color.into();
        let color = solstice::Color {
            red,
            blue,
            green,
            alpha,
        };
        solstice::Renderer::clear(
            self.ctx,
            solstice::ClearSettings {
                color: Some(color.into()),
                depth: None,
                stencil: None,
                target: self.active_canvas.map(|c| &c.inner),
            },
        )
    }

    pub fn set_shader(&mut self, shader: &'s mut Shader2D) {
        self.flush();
        self.active_shader.replace(shader);
    }

    pub fn remove_active_shader(&mut self) {
        self.flush();
        self.active_shader.take();
    }

    pub fn set_canvas(&mut self, canvas: &'s Canvas) {
        self.flush();
        self.active_canvas.replace(canvas);
    }

    pub fn unset_canvas(&mut self) {
        self.flush();
        self.active_canvas.take();
    }

    fn bind_default_texture(&mut self) {
        let texture = &self.inner.default_texture;
        if !self.inner.default_shader.is_bound(texture) {
            self.flush();
            let texture = &self.inner.default_texture;
            self.inner.default_shader.bind_texture(texture);
        }
    }

    fn bind_texture<T: Texture + Copy>(&mut self, texture: T) {
        if !self.inner.default_shader.is_bound(texture) {
            self.flush();
            self.inner.default_shader.bind_texture(texture);
        }
    }

    pub fn set_color<C: Into<[f32; 4]>>(&mut self, color: C) {
        self.color = color.into();
    }

    pub fn line(&mut self, x1: f32, y1: f32, x2: f32, y2: f32) {
        self.bind_default_texture();
        let (x1, y1) = self.transforms.current().transform_point(x1, y1);
        let (x2, y2) = self.transforms.current().transform_point(x2, y2);
        self.stroke_polygon([Point::new(x1, y1), Point::new(x2, y2)].iter())
    }
    pub fn lines<P: Into<Point>, I: IntoIterator<Item = P>>(&mut self, points: I) {
        let transform = *self.transforms.current();
        let points = points.into_iter().map(|p| {
            let point: Point = p.into();
            Into::<lyon_tessellation::math::Point>::into(
                transform.transform_point(point.x, point.y),
            )
        });
        self.stroke_polygon(points);
    }
    pub fn image<T: Texture + Copy>(&mut self, rectangle: Rectangle, texture: T) {
        self.bind_texture(texture);
        self.inner_draw(DrawMode::Fill, rectangle);
    }

    pub fn draw<G: Geometry>(&mut self, draw_mode: DrawMode, geometry: G) {
        self.bind_default_texture();
        self.inner_draw(draw_mode, geometry);
    }

    fn inner_draw<G: Geometry>(&mut self, draw_mode: DrawMode, geometry: G) {
        let transform = {
            let transform = *self.transforms.current();
            let color = self.color;
            move |v: Vertex2D| {
                let (x, y) = transform.transform_point(v.position[0], v.position[1]);
                Vertex2D {
                    position: [x, y],
                    color,
                    ..v
                }
            }
        };
        match draw_mode {
            DrawMode::Fill => {
                let vertices = geometry.vertices().map(transform).collect::<Box<_>>();
                let indices = geometry
                    .indices()
                    .map(|i| self.vertex_offset as u32 + i)
                    .collect::<Box<_>>();
                self.fill_polygon(&vertices, &indices)
            }
            DrawMode::Stroke => self.stroke_polygon(
                geometry
                    .vertices()
                    .map(transform)
                    .map(Into::<lyon_tessellation::math::Point>::into),
            ),
        }
    }

    fn fill_polygon(&mut self, vertices: &[Vertex2D], indices: &[u32]) {
        self.buffer_geometry(vertices, indices)
    }

    fn stroke_polygon<P, I>(&mut self, vertices: I)
    where
        P: Into<lyon_tessellation::math::Point>,
        I: IntoIterator<Item = P>,
    {
        use lyon_tessellation::*;
        let mut builder = path::Builder::new();
        builder.polygon(&vertices.into_iter().map(Into::into).collect::<Box<[_]>>());
        let path = builder.build();

        struct WithColor([f32; 4]);

        impl StrokeVertexConstructor<Vertex2D> for WithColor {
            fn new_vertex(
                &mut self,
                point: lyon_tessellation::math::Point,
                attributes: StrokeAttributes<'_, '_>,
            ) -> Vertex2D {
                Vertex2D {
                    position: [point.x, point.y],
                    color: self.0,
                    uv: attributes.normal().into(),
                }
            }
        }

        let mut buffers: VertexBuffers<Vertex2D, u32> = VertexBuffers::new();
        {
            let mut tessellator = StrokeTessellator::new();
            tessellator
                .tessellate(
                    &path,
                    &StrokeOptions::default().with_line_width(5.),
                    &mut BuffersBuilder::new(&mut buffers, WithColor(self.color)),
                )
                .unwrap();
        }

        let indices = buffers
            .indices
            .iter()
            .map(|i| self.vertex_offset as u32 + *i)
            .collect::<Vec<_>>();

        self.buffer_geometry(&buffers.vertices, &indices);
    }

    fn buffer_geometry(&mut self, vertices: &[Vertex2D], indices: &[u32]) {
        if self.vertex_offset + vertices.len() > self.inner.mesh.vertex_capacity()
            || self.index_offset + indices.len() > self.inner.mesh.index_capacity()
        {
            // because we're flushing, all the index offsets are going to be wrong
            // we could avoid this if we knew before that we were going overflow the mesh
            // this is mostly straightforward except for the stroke tesselation so
            // TODO: when lyon is removed, figure out sizes and do the overflow check earlier
            let indices = indices
                .iter()
                .map(|i| *i - self.vertex_offset as u32)
                .collect::<Vec<_>>();
            self.flush();
            self.inner.mesh.set_vertices(vertices, self.vertex_offset);
            self.inner.mesh.set_indices(&indices, self.index_offset);
        } else {
            self.inner.mesh.set_vertices(vertices, self.vertex_offset);
            self.inner.mesh.set_indices(indices, self.index_offset);
        }
        self.vertex_offset += vertices.len();
        self.index_offset += indices.len();
    }

    pub fn print<S: AsRef<str>>(
        &mut self,
        font: glyph_brush::FontId,
        text: S,
        x: f32,
        y: f32,
        scale: f32,
    ) {
        self.flush();
        let bounds = Rectangle {
            x,
            y,
            width: self.inner.width - x,
            height: self.inner.height - y,
        };
        let text = glyph_brush::Text::new(text.as_ref())
            .with_color(self.color)
            .with_font_id(font)
            .with_scale(scale);
        self.inner.text_workspace.set_text(text, bounds, self.ctx);

        let invert_y = self.active_canvas.is_some();
        let shader = &mut self.inner.text_shader;
        shader.set_width_height(self.inner.width, self.inner.height, invert_y);
        shader.bind_texture(self.inner.text_workspace.texture());
        shader.activate(self.ctx);
        let geometry = self.inner.text_workspace.geometry(self.ctx);
        solstice::Renderer::draw(
            self.ctx,
            shader,
            &geometry,
            solstice::PipelineSettings {
                depth_state: None,
                ..solstice::PipelineSettings::default()
            },
        );
    }
}

impl Drop for Graphics2DLock<'_, '_> {
    fn drop(&mut self) {
        self.flush();
    }
}

pub struct Graphics2D {
    mesh: MappedIndexedMesh<Vertex2D, u32>,
    default_shader: Shader2D,
    default_texture: solstice::image::Image,
    text_workspace: text::Text,
    text_shader: Shader2D,
    width: f32,
    height: f32,
}

impl Graphics2D {
    pub fn new(ctx: &mut Context, width: f32, height: f32) -> Result<Self, Graphics2DError> {
        let mesh = MappedIndexedMesh::new(ctx, 10000, 10000)?;
        let default_shader = Shader2D::new(ctx, width, height)?;
        let default_texture = super::create_default_texture(ctx);
        let text_workspace = text::Text::new(ctx)?;
        let text_shader =
            super::Shader2D::with((text::DEFAULT_VERT, text::DEFAULT_FRAG), ctx, 0., 0.)?;
        Ok(Self {
            mesh,
            default_shader,
            default_texture,
            text_workspace,
            text_shader,
            width,
            height,
        })
    }

    pub fn start<'a>(&'a mut self, ctx: &'a mut Context) -> Graphics2DLock<'a, '_> {
        Graphics2DLock {
            ctx,
            inner: self,
            color: [1., 1., 1., 1.],
            index_offset: 0,
            vertex_offset: 0,
            transforms: Default::default(),
            active_shader: None,
            active_canvas: None,
        }
    }

    pub fn add_font(&mut self, font_data: glyph_brush::ab_glyph::FontVec) -> glyph_brush::FontId {
        self.text_workspace.add_font(font_data)
    }

    pub fn set_width_height(&mut self, width: f32, height: f32) {
        self.width = width;
        self.height = height;
    }

    pub fn dimensions(&self) -> (f32, f32) {
        (self.width, self.height)
    }
}

pub trait Geometry {
    type Vertices: Iterator<Item = Vertex2D>;
    type Indices: Iterator<Item = u32>;

    fn vertices(&self) -> Self::Vertices;
    fn indices(&self) -> Self::Indices;
}

pub trait SimpleConvexGeometry {
    type Vertices: Iterator<Item = Vertex2D>;
    fn vertices(&self) -> Self::Vertices;
    fn vertex_count(&self) -> usize;
}

impl<T: SimpleConvexGeometry> Geometry for T {
    type Vertices = T::Vertices;
    type Indices = std::iter::FlatMap<
        std::ops::Range<u32>,
        arrayvec::ArrayVec<[u32; 3]>,
        fn(u32) -> arrayvec::ArrayVec<[u32; 3]>,
    >;

    fn vertices(&self) -> Self::Vertices {
        T::vertices(self)
    }

    fn indices(&self) -> Self::Indices {
        (1..(self.vertex_count() as u32 - 1))
            .flat_map(|i| arrayvec::ArrayVec::<[u32; 3]>::from([0, i, i + 1]))
    }
}

macro_rules! impl_array_simple_convex_geom {
    ($ty:ty, $count:expr) => {
        impl SimpleConvexGeometry for [$ty; $count] {
            type Vertices = std::iter::Map<std::vec::IntoIter<$ty>, fn($ty) -> Vertex2D>;

            fn vertices(&self) -> Self::Vertices {
                self.to_vec().into_iter().map(Into::into)
            }

            fn vertex_count(&self) -> usize {
                self.len()
            }
        }

        impl SimpleConvexGeometry for &[$ty; $count] {
            type Vertices = std::iter::Map<std::vec::IntoIter<$ty>, fn($ty) -> Vertex2D>;

            fn vertices(&self) -> Self::Vertices {
                self.to_vec().into_iter().map(Into::into)
            }

            fn vertex_count(&self) -> usize {
                self.len()
            }
        }
    };
}

macro_rules! impl_32_array_simple_convex_geom {
    ($ty:ty) => {
        impl_array_simple_convex_geom!($ty, 1);
        impl_array_simple_convex_geom!($ty, 2);
        impl_array_simple_convex_geom!($ty, 3);
        impl_array_simple_convex_geom!($ty, 4);
        impl_array_simple_convex_geom!($ty, 5);
        impl_array_simple_convex_geom!($ty, 6);
        impl_array_simple_convex_geom!($ty, 7);
        impl_array_simple_convex_geom!($ty, 8);
        impl_array_simple_convex_geom!($ty, 9);
        impl_array_simple_convex_geom!($ty, 10);
        impl_array_simple_convex_geom!($ty, 11);
        impl_array_simple_convex_geom!($ty, 12);
        impl_array_simple_convex_geom!($ty, 13);
        impl_array_simple_convex_geom!($ty, 14);
        impl_array_simple_convex_geom!($ty, 15);
        impl_array_simple_convex_geom!($ty, 16);
        impl_array_simple_convex_geom!($ty, 17);
        impl_array_simple_convex_geom!($ty, 18);
        impl_array_simple_convex_geom!($ty, 19);
        impl_array_simple_convex_geom!($ty, 20);
        impl_array_simple_convex_geom!($ty, 21);
        impl_array_simple_convex_geom!($ty, 22);
        impl_array_simple_convex_geom!($ty, 23);
        impl_array_simple_convex_geom!($ty, 24);
        impl_array_simple_convex_geom!($ty, 25);
        impl_array_simple_convex_geom!($ty, 26);
        impl_array_simple_convex_geom!($ty, 27);
        impl_array_simple_convex_geom!($ty, 28);
        impl_array_simple_convex_geom!($ty, 29);
        impl_array_simple_convex_geom!($ty, 30);
        impl_array_simple_convex_geom!($ty, 31);
        impl_array_simple_convex_geom!($ty, 32);
    };
}

impl_32_array_simple_convex_geom!((f32, f32));
impl_32_array_simple_convex_geom!((f64, f64));
impl_32_array_simple_convex_geom!(Point);

impl<'a> SimpleConvexGeometry for &'a [Vertex2D] {
    type Vertices = std::iter::Copied<std::slice::Iter<'a, Vertex2D>>;

    fn vertices(&self) -> Self::Vertices {
        self.into_iter().copied()
    }

    fn vertex_count(&self) -> usize {
        self.len()
    }
}

impl<'a> SimpleConvexGeometry for &'a [(f32, f32)] {
    type Vertices =
        std::iter::Map<std::slice::Iter<'a, (f32, f32)>, fn(&'a (f32, f32)) -> Vertex2D>;

    fn vertices(&self) -> Self::Vertices {
        self.iter().map(|p| (*p).into())
    }

    fn vertex_count(&self) -> usize {
        self.len()
    }
}

impl<'a> SimpleConvexGeometry for &'a [(f64, f64)] {
    type Vertices =
        std::iter::Map<std::slice::Iter<'a, (f64, f64)>, fn(&'a (f64, f64)) -> Vertex2D>;

    fn vertices(&self) -> Self::Vertices {
        self.iter().map(|p| (*p).into())
    }

    fn vertex_count(&self) -> usize {
        self.len()
    }
}