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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use crate::resources::*;
use oxygengine_core::prelude::*;
use oxygengine_ha_renderer::prelude::*;

#[cfg(not(feature = "scalar64"))]
use std::f32::consts::TAU;
#[cfg(feature = "scalar64")]
use std::f64::consts::TAU;

pub enum Renderable {
    PushTransform(Transform2d),
    PopTransform,
    PushBlending(MaterialBlending),
    PopBlending,
    PushScissor(Rect),
    PopScissor,
    Mesh(MeshRenderable),
    Sprite(SpriteRenderable),
    Text(TextRenderable),
    Shape(ShapeRenderable),
}

impl From<MeshRenderable> for Renderable {
    fn from(other: MeshRenderable) -> Self {
        Self::Mesh(other)
    }
}

impl From<SpriteRenderable> for Renderable {
    fn from(other: SpriteRenderable) -> Self {
        Self::Sprite(other)
    }
}

impl From<TextRenderable> for Renderable {
    fn from(other: TextRenderable) -> Self {
        Self::Text(other)
    }
}

impl From<ShapeRenderable> for Renderable {
    fn from(other: ShapeRenderable) -> Self {
        Self::Shape(other)
    }
}

#[derive(Debug, Default, Clone)]
pub struct MeshRenderable {
    pub transform: Transform2d,
    pub mesh: HaMeshInstance,
    pub material: HaMaterialInstance,
}

impl MeshRenderable {
    pub fn transform(mut self, value: Transform2d) -> Self {
        self.transform = value;
        self
    }

    pub fn mesh(mut self, value: HaMeshInstance) -> Self {
        self.mesh = value;
        self
    }

    pub fn material(mut self, value: HaMaterialInstance) -> Self {
        self.material = value;
        self
    }
}

#[derive(Debug, Clone)]
pub struct SpriteRenderable {
    pub transform: Transform2d,
    pub image: ImageReference,
    pub tint: Rgba,
    pub tiling: Vec2,
    pub region: Option<Rect>,
}

impl Default for SpriteRenderable {
    fn default() -> Self {
        Self {
            transform: Default::default(),
            image: Default::default(),
            tint: Rgba::white(),
            tiling: Vec2::one(),
            region: None,
        }
    }
}

impl SpriteRenderable {
    pub fn new(name: impl ToString) -> Self {
        Self::default().image(ImageReference::Asset(name.to_string()))
    }

    pub fn transform(mut self, value: Transform2d) -> Self {
        self.transform = value;
        self
    }

    pub fn position(mut self, value: impl Into<Vec2>) -> Self {
        self.transform.position = value.into();
        self
    }

    pub fn rotation(mut self, value: Scalar) -> Self {
        self.transform.rotation = value;
        self
    }

    pub fn size(mut self, value: impl Into<Vec2>) -> Self {
        self.transform.scale = value.into();
        self
    }

    pub fn image(mut self, value: ImageReference) -> Self {
        self.image = value;
        self
    }

    pub fn tint(mut self, value: impl Into<Rgba>) -> Self {
        self.tint = value.into();
        self
    }

    pub fn tiling(mut self, value: impl Into<Vec2>) -> Self {
        self.tiling = value.into();
        self
    }

    pub fn region(mut self, value: impl Into<Rect>) -> Self {
        self.region = Some(value.into());
        self
    }

    pub fn region_from_animation_frame(
        self,
        mut frame: usize,
        mut cols: usize,
        mut rows: usize,
    ) -> Self {
        cols = cols.max(1);
        rows = rows.max(1);
        frame = frame % cols * rows;
        let col = frame % cols;
        let row = frame / rows;
        self.region_from_tile_cell(col, row, cols, rows)
    }

    pub fn region_from_tile_cell(
        self,
        mut col: usize,
        mut row: usize,
        mut cols: usize,
        mut rows: usize,
    ) -> Self {
        cols = cols.max(1);
        rows = rows.max(1);
        col = col.min(cols - 1);
        row = row.min(rows - 1);
        let width = cols as Scalar;
        let height = rows as Scalar;
        self.region(rect(
            col as Scalar / width,
            row as Scalar / height,
            1.0 / width,
            1.0 / height,
        ))
    }
}

#[derive(Debug, Clone)]
pub struct TextRenderable {
    pub transform: Transform2d,
    pub content: HaTextContent,
    pub font: String,
    pub size: Scalar,
    pub color: Rgba,
    pub alignment: Vec2,
    pub bounds_width: Option<Scalar>,
    pub bounds_height: Option<Scalar>,
    pub wrapping: HaTextWrapping,
}

impl Default for TextRenderable {
    fn default() -> Self {
        Self {
            transform: Default::default(),
            content: Default::default(),
            font: Default::default(),
            size: 32.0,
            color: Rgba::white(),
            alignment: 0.0.into(),
            bounds_width: None,
            bounds_height: None,
            wrapping: Default::default(),
        }
    }
}

impl TextRenderable {
    pub fn new(font: impl ToString, content: impl Into<HaTextContent>) -> Self {
        Self::default().font(font).content(content)
    }

    pub fn transform(mut self, value: Transform2d) -> Self {
        self.transform = value;
        self
    }

    pub fn position(mut self, value: impl Into<Vec2>) -> Self {
        self.transform.position = value.into();
        self
    }

    pub fn rotation(mut self, value: Scalar) -> Self {
        self.transform.rotation = value;
        self
    }

    pub fn scale(mut self, value: impl Into<Vec2>) -> Self {
        self.transform.scale = value.into();
        self
    }

    pub fn content(mut self, value: impl Into<HaTextContent>) -> Self {
        self.content = value.into();
        self
    }

    pub fn font(mut self, value: impl ToString) -> Self {
        self.font = value.to_string();
        self
    }

    pub fn size(mut self, value: Scalar) -> Self {
        self.size = value;
        self
    }

    pub fn color(mut self, value: Rgba) -> Self {
        self.color = value;
        self
    }

    pub fn alignment(mut self, value: impl Into<Vec2>) -> Self {
        self.alignment = value.into();
        self
    }

    pub fn bounds_width(mut self, value: Option<Scalar>) -> Self {
        self.bounds_width = value;
        self
    }

    pub fn bounds_height(mut self, value: Option<Scalar>) -> Self {
        self.bounds_height = value;
        self
    }

    pub fn wrapping(mut self, value: HaTextWrapping) -> Self {
        self.wrapping = value;
        self
    }

    pub fn to_text_instance(&self) -> HaTextInstance {
        let mut result = HaTextInstance::default();
        result.set_content(self.content.to_owned());
        result.set_font(self.font.to_owned());
        result.set_size(self.size);
        result.set_color(self.color);
        result.set_alignment(self.alignment);
        result.set_pivot(self.alignment);
        result.set_bounds_width(self.bounds_width);
        result.set_bounds_height(self.bounds_height);
        result.set_wrapping(self.wrapping.to_owned());
        result
    }
}

#[derive(Debug, Clone)]
pub enum ShapeRenderable {
    Points {
        tint: Rgba,
        points: Vec<Vec2>,
        size: Scalar,
    },
    Lines {
        tint: Rgba,
        points: Vec<Vec2>,
        size: Scalar,
    },
    Polygon {
        tint: Rgba,
        points: Vec<Vec2>,
    },
    Triangles {
        tint: Rgba,
        points: Vec<[Vec2; 3]>,
    },
    Circle {
        tint: Rgba,
        center: Vec2,
        radius: Scalar,
        segments: usize,
    },
}

impl ShapeRenderable {
    pub fn factory(&self) -> Result<StaticVertexFactory, MeshError> {
        let (vertices, triangles) = match self {
            ShapeRenderable::Points { tint, points, size } => {
                let color = Vec4::from(*tint);
                let half_size = size * 0.5;
                let vertices = points
                    .iter()
                    .copied()
                    .flat_map(|point| {
                        let point = Vec3::from(point);
                        [
                            SurfaceVertexPC {
                                position: point + vec3(-half_size, -half_size, 0.0),
                                color,
                            },
                            SurfaceVertexPC {
                                position: point + vec3(half_size, -half_size, 0.0),
                                color,
                            },
                            SurfaceVertexPC {
                                position: point + vec3(half_size, half_size, 0.0),
                                color,
                            },
                            SurfaceVertexPC {
                                position: point + vec3(-half_size, half_size, 0.0),
                                color,
                            },
                        ]
                    })
                    .collect::<Vec<_>>();
                let triangles = (0..points.len())
                    .flat_map(|index| {
                        let offset = index as u32 * 4;
                        [
                            (offset, offset + 1, offset + 2),
                            (offset + 2, offset + 3, offset),
                        ]
                    })
                    .collect::<Vec<_>>();
                (vertices, triangles)
            }
            ShapeRenderable::Lines { tint, points, size } => {
                let color = Vec4::from(*tint);
                let half_size = size * 0.5;
                let vertices = points
                    .windows(2)
                    .flat_map(|points| {
                        let from = points[0];
                        let to = points[1];
                        let direction = to - from;
                        let forward = direction.normalized();
                        let right = vec2(-forward.y, forward.x);
                        [
                            SurfaceVertexPC {
                                position: (from - right * half_size).into(),
                                color,
                            },
                            SurfaceVertexPC {
                                position: (to - right * half_size).into(),
                                color,
                            },
                            SurfaceVertexPC {
                                position: (to + right * half_size).into(),
                                color,
                            },
                            SurfaceVertexPC {
                                position: (from + right * half_size).into(),
                                color,
                            },
                        ]
                    })
                    .chain(points.windows(3).map(|points| SurfaceVertexPC {
                        position: points[1].into(),
                        color,
                    }))
                    .collect::<Vec<_>>();
                let triangles = (0..(points.len() - 1))
                    .flat_map(|index| {
                        let offset = index as u32 * 4;
                        [
                            (offset, offset + 1, offset + 2),
                            (offset + 2, offset + 3, offset),
                        ]
                    })
                    .chain((0..(points.len() - 2)).flat_map(|index| {
                        let index = index as u32;
                        let knots_offset = (points.len() - 1) as u32 * 4;
                        let bar_offset = index * 4;
                        [
                            (knots_offset + index, bar_offset + 1, bar_offset + 4),
                            (knots_offset + index, bar_offset + 2, bar_offset + 7),
                        ]
                    }))
                    .collect::<Vec<_>>();
                (vertices, triangles)
            }
            ShapeRenderable::Polygon { tint, points } => {
                let color = Vec4::from(*tint);
                let vertices = points
                    .iter()
                    .copied()
                    .map(|point| SurfaceVertexPC {
                        position: Vec3::from(point),
                        color,
                    })
                    .collect::<Vec<_>>();
                let triangles = (0..points.len() - 2)
                    .map(|index| (0u32, index as u32 + 1, index as u32 + 2))
                    .collect::<Vec<_>>();
                (vertices, triangles)
            }
            ShapeRenderable::Triangles { tint, points } => {
                let color = Vec4::from(*tint);
                let vertices = points
                    .iter()
                    .flat_map(|[a, b, c]| {
                        [
                            SurfaceVertexPC {
                                position: Vec3::from(*a),
                                color,
                            },
                            SurfaceVertexPC {
                                position: Vec3::from(*b),
                                color,
                            },
                            SurfaceVertexPC {
                                position: Vec3::from(*c),
                                color,
                            },
                        ]
                    })
                    .collect::<Vec<_>>();
                let triangles = (0..points.len())
                    .map(|index| {
                        let index = index as u32 * 3;
                        (index, index + 1, index + 2)
                    })
                    .collect::<Vec<_>>();
                (vertices, triangles)
            }
            ShapeRenderable::Circle {
                tint,
                center,
                radius,
                segments,
            } => {
                let color = Vec4::from(*tint);
                let center = *center;
                let radius = *radius;
                let segments = *segments;
                let vertices = std::iter::once(SurfaceVertexPC {
                    position: center.into(),
                    color,
                })
                .chain((0..segments).map(|index| {
                    let angle = index as Scalar * TAU / segments as Scalar;
                    let (y, x) = angle.sin_cos();
                    SurfaceVertexPC {
                        position: (center + vec2(x, y) * radius).into(),
                        color,
                    }
                }))
                .collect::<Vec<_>>();
                let triangles = (0..segments)
                    .map(|index| {
                        (
                            0u32,
                            (index % segments) as u32 + 1,
                            ((index + 1) % segments) as u32 + 1,
                        )
                    })
                    .collect::<Vec<_>>();
                (vertices, triangles)
            }
        };
        let mut result = StaticVertexFactory::new(
            SurfaceVertexPC::vertex_layout()?,
            vertices.len(),
            triangles.len(),
            MeshDrawMode::Triangles,
        );
        result.vertices(&vertices, None)?;
        result.triangles(&triangles, None)?;
        Ok(result)
    }
}

pub struct Renderables {
    pub(crate) buffer_stack: Vec<Vec<Renderable>>,
    pub buffer_resize_count: usize,
    pub sprite_mesh_reference: MeshReference,
    pub sprite_material_reference: MaterialReference,
    pub sprite_filtering: ImageFiltering,
    pub text_material_reference: MaterialReference,
    pub text_pool_resize_count: usize,
    pub shape_material_reference: MaterialReference,
    pub shape_pool_resize_count: usize,
}

impl Default for Renderables {
    fn default() -> Self {
        Self {
            buffer_stack: vec![Default::default()],
            buffer_resize_count: 1024,
            sprite_mesh_reference: MeshReference::Asset("@mesh/surface/quad/pt".to_owned()),
            sprite_material_reference: MaterialReference::Asset(
                "@material/graph/prototype/sprite".to_owned(),
            ),
            sprite_filtering: ImageFiltering::Linear,
            text_material_reference: MaterialReference::Asset(
                "@material/graph/surface/flat/text".to_owned(),
            ),
            text_pool_resize_count: 64,
            shape_material_reference: MaterialReference::Asset(
                "@material/graph/surface/flat/color".to_owned(),
            ),
            shape_pool_resize_count: 64,
        }
    }
}

impl Renderables {
    pub fn depth(&self) -> usize {
        self.buffer_stack.len()
    }

    pub fn begin(&mut self) {
        self.buffer_stack.push(Default::default());
    }

    pub fn end(&mut self) {
        if let Some(buffer) = self.consume() {
            self.extend(buffer);
        }
    }

    pub fn collapse(&mut self) {
        while let Some(buffer) = self.consume() {
            self.extend(buffer);
        }
    }

    pub fn consume(&mut self) -> Option<Vec<Renderable>> {
        if self.buffer_stack.len() > 1 {
            self.buffer_stack.pop()
        } else {
            None
        }
    }

    pub fn extend(&mut self, buffer: impl IntoIterator<Item = Renderable>) {
        if let Some(last) = self.buffer_stack.last_mut() {
            last.extend(buffer);
        }
    }

    pub fn scope<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut Self),
    {
        self.begin();
        f(self);
        self.end();
    }

    pub fn draw(&mut self, renderable: impl Into<Renderable>) {
        if self.buffer_stack.is_empty() {
            self.buffer_stack.push(Default::default());
        }
        let last = self.buffer_stack.last_mut().unwrap();
        if last.len() == last.capacity() && self.buffer_resize_count > 0 {
            last.reserve(self.buffer_resize_count);
        }
        last.push(renderable.into());
    }
}