nightshade 0.13.3

A cross-platform data-oriented game engine.
Documentation
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
use nalgebra_glm::Vec4;

use crate::ecs::text::components::{TextAlignment, TextProperties, VerticalAlignment};
use crate::ecs::text::resources::FontAtlasData;
use crate::ecs::ui::components::{CanvasCommand, UiWidgetState};
use crate::ecs::ui::render_sync::select_best_font;
use crate::ecs::ui::types::Rect;
use crate::ecs::ui::types::UiTextInstance;
use crate::ecs::world::World;
use crate::render::wgpu::passes::geometry::{UiLayer, UiRect};
use crate::render::wgpu::text_mesh::generate_text_mesh;

pub(super) fn emit_canvas_commands(world: &mut World, dpi_scale: f32) {
    let canvas_entities: Vec<freecs::Entity> = world
        .ui
        .query_entities(crate::ecs::world::UI_WIDGET_STATE)
        .collect();

    struct CanvasEmit {
        entity: freecs::Entity,
        commands: Vec<CanvasCommand>,
        origin: nalgebra_glm::Vec2,
        clip_rect: Option<Rect>,
        layer: UiLayer,
        z_index: i32,
        hit_test_enabled: bool,
    }

    let mut canvases = Vec::new();

    for entity in canvas_entities {
        let is_canvas = matches!(
            world.ui.get_ui_widget_state(entity),
            Some(UiWidgetState::Canvas(_))
        );
        if !is_canvas {
            continue;
        }

        if !world.ui_node_effectively_visible(entity) {
            continue;
        }

        let (origin, clip_rect, layer, z_index) =
            if let Some(node) = world.ui.get_ui_layout_node(entity) {
                let node_layer = node.computed_layer.unwrap_or(UiLayer::Background);
                let zi = (node.computed_depth * 100.0) as i32;
                (
                    node.computed_rect.min,
                    node.computed_clip_rect,
                    node_layer,
                    zi,
                )
            } else {
                continue;
            };

        let (commands, hit_test_enabled) =
            if let Some(UiWidgetState::Canvas(data)) = world.ui.get_ui_widget_state(entity) {
                (data.commands.clone(), data.hit_test_enabled)
            } else {
                continue;
            };

        canvases.push(CanvasEmit {
            entity,
            commands,
            origin,
            clip_rect,
            layer,
            z_index,
            hit_test_enabled,
        });
    }

    let bitmap_fonts: Vec<FontAtlasData> = world
        .resources
        .text_cache
        .font_manager
        .bitmap_fonts
        .iter()
        .map(|arc| arc.as_ref().clone())
        .collect();

    for canvas in canvases {
        let mut bounds: Vec<(u32, Rect)> = Vec::new();

        for command in &canvas.commands {
            let command_id = match command {
                CanvasCommand::Rect { id, .. }
                | CanvasCommand::Circle { id, .. }
                | CanvasCommand::Line { id, .. }
                | CanvasCommand::Text { id, .. }
                | CanvasCommand::Polyline { id, .. }
                | CanvasCommand::RectStroke { id, .. }
                | CanvasCommand::CircleStroke { id, .. }
                | CanvasCommand::Arc { id, .. }
                | CanvasCommand::QuadraticBezier { id, .. }
                | CanvasCommand::CubicBezier { id, .. } => *id,
            };

            let command_bounds: Option<Rect> = match command {
                CanvasCommand::Rect {
                    position,
                    size,
                    color,
                    corner_radius,
                    ..
                } => {
                    world.resources.retained_ui.frame_rects.push(UiRect {
                        position: canvas.origin + position,
                        size: *size,
                        color: *color,
                        corner_radius: *corner_radius * dpi_scale,
                        border_width: 0.0,
                        border_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                        rotation: 0.0,
                        clip_rect: canvas.clip_rect,
                        layer: canvas.layer,
                        z_index: canvas.z_index + 1,
                    });
                    Some(Rect::from_min_max(*position, *position + *size))
                }
                CanvasCommand::Circle {
                    center,
                    radius,
                    color,
                    ..
                } => {
                    let top_left =
                        canvas.origin + center - nalgebra_glm::Vec2::new(*radius, *radius);
                    let diameter = *radius * 2.0;
                    world.resources.retained_ui.frame_rects.push(UiRect {
                        position: top_left,
                        size: nalgebra_glm::Vec2::new(diameter, diameter),
                        color: *color,
                        corner_radius: *radius * dpi_scale,
                        border_width: 0.0,
                        border_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                        rotation: 0.0,
                        clip_rect: canvas.clip_rect,
                        layer: canvas.layer,
                        z_index: canvas.z_index + 1,
                    });
                    let offset = nalgebra_glm::Vec2::new(*radius, *radius);
                    Some(Rect::from_min_max(center - offset, center + offset))
                }
                CanvasCommand::Line {
                    from,
                    to,
                    thickness,
                    color,
                    ..
                } => {
                    let world_from = canvas.origin + from;
                    let world_to = canvas.origin + to;
                    let delta = world_to - world_from;
                    let length = delta.magnitude();
                    if length < 0.001 {
                        continue;
                    }
                    let midpoint = (world_from + world_to) * 0.5;
                    let angle = delta.y.atan2(delta.x);
                    let rect_pos =
                        midpoint - nalgebra_glm::Vec2::new(length * 0.5, *thickness * 0.5);
                    world.resources.retained_ui.frame_rects.push(UiRect {
                        position: rect_pos,
                        size: nalgebra_glm::Vec2::new(length, *thickness),
                        color: *color,
                        corner_radius: 0.0,
                        border_width: 0.0,
                        border_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                        rotation: angle,
                        clip_rect: canvas.clip_rect,
                        layer: canvas.layer,
                        z_index: canvas.z_index + 1,
                    });
                    let min_x = from.x.min(to.x) - *thickness * 0.5;
                    let min_y = from.y.min(to.y) - *thickness * 0.5;
                    let max_x = from.x.max(to.x) + *thickness * 0.5;
                    let max_y = from.y.max(to.y) + *thickness * 0.5;
                    Some(Rect::from_min_max(
                        nalgebra_glm::Vec2::new(min_x, min_y),
                        nalgebra_glm::Vec2::new(max_x, max_y),
                    ))
                }
                CanvasCommand::Text {
                    text,
                    position,
                    font_size,
                    color,
                    ..
                } => {
                    let physical_size = *font_size * dpi_scale;
                    if let Some((canvas_font_idx, font_data)) =
                        select_best_font(&bitmap_fonts, physical_size)
                    {
                        let properties = TextProperties {
                            font_size: physical_size,
                            color: *color,
                            alignment: TextAlignment::Left,
                            vertical_alignment: VerticalAlignment::Top,
                            ..TextProperties::default()
                        };
                        let mesh = generate_text_mesh(text, font_data, &properties);
                        world
                            .resources
                            .retained_ui
                            .frame_text_meshes
                            .push(UiTextInstance {
                                mesh,
                                position: canvas.origin + position,
                                color: *color,
                                outline_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                                outline_width: 0.0,
                                font_size: physical_size,
                                font_index: canvas_font_idx,
                                clip_rect: canvas.clip_rect,
                                layer: canvas.layer,
                                z_index: canvas.z_index + 2,
                                character_colors: None,
                                character_background_colors: None,
                            });
                    }
                    None
                }
                CanvasCommand::Polyline {
                    points,
                    thickness,
                    color,
                    closed,
                    ..
                } => {
                    let segment_count = if *closed {
                        points.len()
                    } else {
                        points.len().saturating_sub(1)
                    };
                    for segment_index in 0..segment_count {
                        let from = canvas.origin + points[segment_index];
                        let to = canvas.origin + points[(segment_index + 1) % points.len()];
                        let delta = to - from;
                        let length = delta.magnitude();
                        if length < 0.001 {
                            continue;
                        }
                        let midpoint = (from + to) * 0.5;
                        let angle = delta.y.atan2(delta.x);
                        let rect_pos =
                            midpoint - nalgebra_glm::Vec2::new(length * 0.5, *thickness * 0.5);
                        world.resources.retained_ui.frame_rects.push(UiRect {
                            position: rect_pos,
                            size: nalgebra_glm::Vec2::new(length, *thickness),
                            color: *color,
                            corner_radius: 0.0,
                            border_width: 0.0,
                            border_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                            rotation: angle,
                            clip_rect: canvas.clip_rect,
                            layer: canvas.layer,
                            z_index: canvas.z_index + 1,
                        });
                    }
                    if !points.is_empty() {
                        let mut min = points[0];
                        let mut max = points[0];
                        for point in points {
                            min.x = min.x.min(point.x);
                            min.y = min.y.min(point.y);
                            max.x = max.x.max(point.x);
                            max.y = max.y.max(point.y);
                        }
                        Some(Rect::from_min_max(min, max))
                    } else {
                        None
                    }
                }
                CanvasCommand::RectStroke {
                    position,
                    size,
                    color,
                    thickness,
                    corner_radius: _,
                    ..
                } => {
                    let origin = canvas.origin + position;
                    let corners = [
                        origin,
                        origin + nalgebra_glm::Vec2::new(size.x, 0.0),
                        origin + *size,
                        origin + nalgebra_glm::Vec2::new(0.0, size.y),
                    ];
                    for edge_index in 0..4 {
                        let from = corners[edge_index];
                        let to = corners[(edge_index + 1) % 4];
                        let delta = to - from;
                        let length = delta.magnitude();
                        if length < 0.001 {
                            continue;
                        }
                        let midpoint = (from + to) * 0.5;
                        let angle = delta.y.atan2(delta.x);
                        let rect_pos =
                            midpoint - nalgebra_glm::Vec2::new(length * 0.5, *thickness * 0.5);
                        world.resources.retained_ui.frame_rects.push(UiRect {
                            position: rect_pos,
                            size: nalgebra_glm::Vec2::new(length, *thickness),
                            color: *color,
                            corner_radius: 0.0,
                            border_width: 0.0,
                            border_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                            rotation: angle,
                            clip_rect: canvas.clip_rect,
                            layer: canvas.layer,
                            z_index: canvas.z_index + 1,
                        });
                    }
                    Some(Rect::from_min_max(*position, *position + *size))
                }
                CanvasCommand::CircleStroke {
                    center,
                    radius,
                    color,
                    thickness,
                    ..
                } => {
                    let segment_count = ((*radius * 2.0) as usize).max(16);
                    let world_center = canvas.origin + center;
                    for segment_index in 0..segment_count {
                        let angle_a =
                            (segment_index as f32 / segment_count as f32) * std::f32::consts::TAU;
                        let angle_b = ((segment_index + 1) as f32 / segment_count as f32)
                            * std::f32::consts::TAU;
                        let from = world_center
                            + nalgebra_glm::Vec2::new(
                                angle_a.cos() * *radius,
                                angle_a.sin() * *radius,
                            );
                        let to = world_center
                            + nalgebra_glm::Vec2::new(
                                angle_b.cos() * *radius,
                                angle_b.sin() * *radius,
                            );
                        let delta = to - from;
                        let length = delta.magnitude();
                        if length < 0.001 {
                            continue;
                        }
                        let midpoint = (from + to) * 0.5;
                        let angle = delta.y.atan2(delta.x);
                        let rect_pos =
                            midpoint - nalgebra_glm::Vec2::new(length * 0.5, *thickness * 0.5);
                        world.resources.retained_ui.frame_rects.push(UiRect {
                            position: rect_pos,
                            size: nalgebra_glm::Vec2::new(length, *thickness),
                            color: *color,
                            corner_radius: 0.0,
                            border_width: 0.0,
                            border_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                            rotation: angle,
                            clip_rect: canvas.clip_rect,
                            layer: canvas.layer,
                            z_index: canvas.z_index + 1,
                        });
                    }
                    let offset = nalgebra_glm::Vec2::new(*radius, *radius);
                    Some(Rect::from_min_max(center - offset, center + offset))
                }
                CanvasCommand::Arc {
                    center,
                    radius,
                    start_angle,
                    end_angle,
                    thickness,
                    color,
                    ..
                } => {
                    let arc_span = end_angle - start_angle;
                    let segment_count = ((arc_span.abs() / std::f32::consts::TAU * (*radius * 2.0))
                        as usize)
                        .max(8);
                    let world_center = canvas.origin + center;
                    for segment_index in 0..segment_count {
                        let angle_a =
                            *start_angle + arc_span * (segment_index as f32 / segment_count as f32);
                        let angle_b = *start_angle
                            + arc_span * ((segment_index + 1) as f32 / segment_count as f32);
                        let from = world_center
                            + nalgebra_glm::Vec2::new(
                                angle_a.cos() * *radius,
                                angle_a.sin() * *radius,
                            );
                        let to = world_center
                            + nalgebra_glm::Vec2::new(
                                angle_b.cos() * *radius,
                                angle_b.sin() * *radius,
                            );
                        let delta = to - from;
                        let length = delta.magnitude();
                        if length < 0.001 {
                            continue;
                        }
                        let midpoint = (from + to) * 0.5;
                        let angle = delta.y.atan2(delta.x);
                        let rect_pos =
                            midpoint - nalgebra_glm::Vec2::new(length * 0.5, *thickness * 0.5);
                        world.resources.retained_ui.frame_rects.push(UiRect {
                            position: rect_pos,
                            size: nalgebra_glm::Vec2::new(length, *thickness),
                            color: *color,
                            corner_radius: 0.0,
                            border_width: 0.0,
                            border_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                            rotation: angle,
                            clip_rect: canvas.clip_rect,
                            layer: canvas.layer,
                            z_index: canvas.z_index + 1,
                        });
                    }
                    let offset = nalgebra_glm::Vec2::new(*radius, *radius);
                    Some(Rect::from_min_max(center - offset, center + offset))
                }
                CanvasCommand::QuadraticBezier {
                    start,
                    control,
                    end,
                    thickness,
                    color,
                    ..
                } => {
                    let arc_length_estimate = {
                        let chord = (end - start).magnitude();
                        let control_net =
                            (control - start).magnitude() + (end - control).magnitude();
                        (chord + control_net) * 0.5
                    };
                    let segment_count = (arc_length_estimate / 4.0).ceil().max(4.0) as usize;
                    for segment_index in 0..segment_count {
                        let t0 = segment_index as f32 / segment_count as f32;
                        let t1 = (segment_index + 1) as f32 / segment_count as f32;
                        let one_minus_t0 = 1.0 - t0;
                        let one_minus_t1 = 1.0 - t1;
                        let from = canvas.origin
                            + start * (one_minus_t0 * one_minus_t0)
                            + control * (2.0 * one_minus_t0 * t0)
                            + end * (t0 * t0);
                        let to = canvas.origin
                            + start * (one_minus_t1 * one_minus_t1)
                            + control * (2.0 * one_minus_t1 * t1)
                            + end * (t1 * t1);
                        let delta = to - from;
                        let length = delta.magnitude();
                        if length < 0.001 {
                            continue;
                        }
                        let midpoint = (from + to) * 0.5;
                        let angle = delta.y.atan2(delta.x);
                        let rect_pos =
                            midpoint - nalgebra_glm::Vec2::new(length * 0.5, *thickness * 0.5);
                        world.resources.retained_ui.frame_rects.push(UiRect {
                            position: rect_pos,
                            size: nalgebra_glm::Vec2::new(length, *thickness),
                            color: *color,
                            corner_radius: 0.0,
                            border_width: 0.0,
                            border_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                            rotation: angle,
                            clip_rect: canvas.clip_rect,
                            layer: canvas.layer,
                            z_index: canvas.z_index + 1,
                        });
                    }
                    let min_x = start.x.min(control.x).min(end.x);
                    let min_y = start.y.min(control.y).min(end.y);
                    let max_x = start.x.max(control.x).max(end.x);
                    let max_y = start.y.max(control.y).max(end.y);
                    Some(Rect::from_min_max(
                        nalgebra_glm::Vec2::new(min_x, min_y),
                        nalgebra_glm::Vec2::new(max_x, max_y),
                    ))
                }
                CanvasCommand::CubicBezier {
                    start,
                    control1,
                    control2,
                    end,
                    thickness,
                    color,
                    ..
                } => {
                    let arc_length_estimate = {
                        let chord = (end - start).magnitude();
                        let control_net = (control1 - start).magnitude()
                            + (control2 - control1).magnitude()
                            + (end - control2).magnitude();
                        (chord + control_net) * 0.5
                    };
                    let segment_count = (arc_length_estimate / 4.0).ceil().max(4.0) as usize;
                    for segment_index in 0..segment_count {
                        let t0 = segment_index as f32 / segment_count as f32;
                        let t1 = (segment_index + 1) as f32 / segment_count as f32;
                        let one_minus_t0 = 1.0 - t0;
                        let one_minus_t1 = 1.0 - t1;
                        let from = canvas.origin
                            + start * (one_minus_t0 * one_minus_t0 * one_minus_t0)
                            + control1 * (3.0 * one_minus_t0 * one_minus_t0 * t0)
                            + control2 * (3.0 * one_minus_t0 * t0 * t0)
                            + end * (t0 * t0 * t0);
                        let to = canvas.origin
                            + start * (one_minus_t1 * one_minus_t1 * one_minus_t1)
                            + control1 * (3.0 * one_minus_t1 * one_minus_t1 * t1)
                            + control2 * (3.0 * one_minus_t1 * t1 * t1)
                            + end * (t1 * t1 * t1);
                        let delta = to - from;
                        let length = delta.magnitude();
                        if length < 0.001 {
                            continue;
                        }
                        let midpoint = (from + to) * 0.5;
                        let angle = delta.y.atan2(delta.x);
                        let rect_pos =
                            midpoint - nalgebra_glm::Vec2::new(length * 0.5, *thickness * 0.5);
                        world.resources.retained_ui.frame_rects.push(UiRect {
                            position: rect_pos,
                            size: nalgebra_glm::Vec2::new(length, *thickness),
                            color: *color,
                            corner_radius: 0.0,
                            border_width: 0.0,
                            border_color: Vec4::new(0.0, 0.0, 0.0, 0.0),
                            rotation: angle,
                            clip_rect: canvas.clip_rect,
                            layer: canvas.layer,
                            z_index: canvas.z_index + 1,
                        });
                    }
                    let min_x = start.x.min(control1.x).min(control2.x).min(end.x);
                    let min_y = start.y.min(control1.y).min(control2.y).min(end.y);
                    let max_x = start.x.max(control1.x).max(control2.x).max(end.x);
                    let max_y = start.y.max(control1.y).max(control2.y).max(end.y);
                    Some(Rect::from_min_max(
                        nalgebra_glm::Vec2::new(min_x, min_y),
                        nalgebra_glm::Vec2::new(max_x, max_y),
                    ))
                }
            };

            if canvas.hit_test_enabled
                && let Some(cid) = command_id
                && let Some(rect) = command_bounds
            {
                bounds.push((cid, rect));
            }
        }

        if canvas.hit_test_enabled
            && let Some(UiWidgetState::Canvas(data)) =
                world.ui.get_ui_widget_state_mut(canvas.entity)
        {
            data.command_bounds = bounds;
        }
    }
}