bevy_mod_outline 0.13.0

A mesh outlining plugin for Bevy.
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
use bevy::prelude::*;

use crate::{InheritOutline, PropagateOutline, StopPropagateOutline};

#[derive(Clone, Component, Default)]
pub(crate) struct PropagateOutlineChild;

/// The components added to and removed from an entity that inherits an outline
/// via propagation.
type OutlineMarks = (InheritOutline, PropagateOutlineChild);

/// Adds the observers which propagate outlines to descendant entities.
pub(crate) fn add_propagate_observers(app: &mut App) {
    app.add_observer(on_add_propagate)
        .add_observer(on_remove_propagate)
        .add_observer(on_add_stop)
        .add_observer(on_remove_stop)
        .add_observer(on_insert_child_of)
        .add_observer(on_remove_child_of)
        .add_observer(on_add_child_marker)
        .add_observer(on_remove_child_marker);
}

fn mark_children(
    entity: Entity,
    children_q: &Query<&Children>,
    stop_q: Option<&Query<(), With<StopPropagateOutline>>>,
    source_q: &Query<(), With<PropagateOutline>>,
    commands: &mut Commands,
) {
    let Ok(children) = children_q.get(entity) else {
        return;
    };
    if stop_q.is_some_and(|q| q.contains(entity)) {
        return;
    }
    for child in children.iter() {
        if source_q.contains(child) {
            // A nested source manages its own subtree.
            continue;
        }
        commands.entity(child).try_insert(OutlineMarks::default());
    }
}

fn unmark_children(
    entity: Entity,
    children_q: &Query<&Children>,
    stop_q: Option<&Query<(), With<StopPropagateOutline>>>,
    source_q: &Query<(), With<PropagateOutline>>,
    commands: &mut Commands,
) {
    let Ok(children) = children_q.get(entity) else {
        return;
    };
    if stop_q.is_some_and(|q| q.contains(entity)) {
        return;
    }
    for child in children.iter() {
        if source_q.contains(child) {
            continue;
        }
        commands.entity(child).try_remove::<OutlineMarks>();
    }
}

fn propagates_to_children(
    entity: Entity,
    stop_q: &Query<(), With<StopPropagateOutline>>,
    source_q: &Query<(), With<PropagateOutline>>,
    marker_q: &Query<(), With<PropagateOutlineChild>>,
) -> bool {
    !stop_q.contains(entity) && (source_q.contains(entity) || marker_q.contains(entity))
}

fn on_add_child_marker(
    add: On<Add, PropagateOutlineChild>,
    children_q: Query<&Children>,
    stop_q: Query<(), With<StopPropagateOutline>>,
    source_q: Query<(), With<PropagateOutline>>,
    mut commands: Commands,
) {
    mark_children(
        add.entity,
        &children_q,
        Some(&stop_q),
        &source_q,
        &mut commands,
    );
}

fn on_remove_child_marker(
    remove: On<Remove, PropagateOutlineChild>,
    children_q: Query<&Children>,
    stop_q: Query<(), With<StopPropagateOutline>>,
    source_q: Query<(), With<PropagateOutline>>,
    mut commands: Commands,
) {
    unmark_children(
        remove.entity,
        &children_q,
        Some(&stop_q),
        &source_q,
        &mut commands,
    );
}

fn on_add_propagate(
    add: On<Add, PropagateOutline>,
    children_q: Query<&Children>,
    stop_q: Query<(), With<StopPropagateOutline>>,
    source_q: Query<(), With<PropagateOutline>>,
    mut commands: Commands,
) {
    let entity = add.entity;
    commands.entity(entity).try_remove::<OutlineMarks>();
    mark_children(entity, &children_q, Some(&stop_q), &source_q, &mut commands);
}

fn on_remove_propagate(
    remove: On<Remove, PropagateOutline>,
    children_q: Query<&Children>,
    parent_q: Query<&ChildOf>,
    stop_q: Query<(), With<StopPropagateOutline>>,
    source_q: Query<(), With<PropagateOutline>>,
    marker_q: Query<(), With<PropagateOutlineChild>>,
    mut commands: Commands,
) {
    let entity = remove.entity;
    if parent_q.get(entity).is_ok_and(|child_of| {
        propagates_to_children(child_of.parent(), &stop_q, &source_q, &marker_q)
    }) {
        // Rejoin the outer region as a marked child.
        commands.entity(entity).try_insert(OutlineMarks::default());
    } else {
        unmark_children(entity, &children_q, Some(&stop_q), &source_q, &mut commands);
    }
}

fn on_add_stop(
    add: On<Add, StopPropagateOutline>,
    children_q: Query<&Children>,
    source_q: Query<(), With<PropagateOutline>>,
    marker_q: Query<(), With<PropagateOutlineChild>>,
    mut commands: Commands,
) {
    let entity = add.entity;
    if source_q.contains(entity) || marker_q.contains(entity) {
        unmark_children(entity, &children_q, None, &source_q, &mut commands);
    }
}

fn on_remove_stop(
    remove: On<Remove, StopPropagateOutline>,
    children_q: Query<&Children>,
    source_q: Query<(), With<PropagateOutline>>,
    marker_q: Query<(), With<PropagateOutlineChild>>,
    mut commands: Commands,
) {
    let entity = remove.entity;
    if source_q.contains(entity) || marker_q.contains(entity) {
        mark_children(entity, &children_q, None, &source_q, &mut commands);
    }
}

fn on_insert_child_of(
    insert: On<Insert, ChildOf>,
    parent_q: Query<&ChildOf>,
    stop_q: Query<(), With<StopPropagateOutline>>,
    source_q: Query<(), With<PropagateOutline>>,
    marker_q: Query<(), With<PropagateOutlineChild>>,
    mut commands: Commands,
) {
    let entity = insert.entity;
    let Ok(child_of) = parent_q.get(entity) else {
        return;
    };
    if source_q.contains(entity) {
        return;
    }
    if propagates_to_children(child_of.parent(), &stop_q, &source_q, &marker_q) {
        commands.entity(entity).try_insert(OutlineMarks::default());
    } else if marker_q.contains(entity) {
        // Moved out of a propagating region.
        commands.entity(entity).try_remove::<OutlineMarks>();
    }
}

fn on_remove_child_of(
    remove: On<Remove, ChildOf>,
    marker_q: Query<(), With<PropagateOutlineChild>>,
    mut commands: Commands,
) {
    let entity = remove.entity;
    if marker_q.contains(entity) {
        commands.entity(entity).try_remove::<OutlineMarks>();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Builds an app with the propagation observers registered.
    fn setup() -> App {
        let mut app = App::new();
        add_propagate_observers(&mut app);
        app
    }

    /// Spawns an entity, optionally as a child of `parent`, with `extra` bundle.
    fn spawn(app: &mut App, parent: Option<Entity>, extra: impl Bundle) -> Entity {
        let mut e = app.world_mut().spawn(extra);
        if let Some(parent) = parent {
            e.insert(ChildOf(parent));
        }
        e.id()
    }

    fn is_marked(app: &App, entity: Entity) -> bool {
        let world = app.world();
        world.get::<InheritOutline>(entity).is_some()
            && world.get::<PropagateOutlineChild>(entity).is_some()
    }

    fn is_unmarked(app: &App, entity: Entity) -> bool {
        let world = app.world();
        world.get::<InheritOutline>(entity).is_none()
            && world.get::<PropagateOutlineChild>(entity).is_none()
    }

    fn flush(app: &mut App) {
        app.world_mut().flush();
    }

    /// Builds `root -> a -> b -> c` with no propagation components.
    fn linear_chain(app: &mut App) -> (Entity, Entity, Entity, Entity) {
        let root = spawn(app, None, ());
        let a = spawn(app, Some(root), ());
        let b = spawn(app, Some(a), ());
        let c = spawn(app, Some(b), ());
        flush(app);
        (root, a, b, c)
    }

    #[test]
    fn test_add_propagate_marks_descendants() {
        let mut app = setup();
        let (root, a, b, c) = linear_chain(&mut app);

        app.world_mut().entity_mut(root).insert(PropagateOutline);
        flush(&mut app);

        assert!(
            is_unmarked(&app, root),
            "root (the source) must not be marked"
        );
        assert!(is_marked(&app, a));
        assert!(is_marked(&app, b));
        assert!(is_marked(&app, c));
    }

    #[test]
    fn test_remove_propagate_unmarks_descendants() {
        let mut app = setup();
        let (root, a, b, c) = linear_chain(&mut app);
        app.world_mut().entity_mut(root).insert(PropagateOutline);
        flush(&mut app);

        app.world_mut()
            .entity_mut(root)
            .remove::<PropagateOutline>();
        flush(&mut app);

        assert!(is_unmarked(&app, a));
        assert!(is_unmarked(&app, b));
        assert!(is_unmarked(&app, c));
    }

    #[test]
    fn test_add_stop_prunes_subtree() {
        let mut app = setup();
        let (root, a, b, c) = linear_chain(&mut app);
        app.world_mut().entity_mut(root).insert(PropagateOutline);
        flush(&mut app);

        // Stop at `b`.
        app.world_mut().entity_mut(b).insert(StopPropagateOutline);
        flush(&mut app);

        assert!(is_marked(&app, a));
        assert!(is_marked(&app, b), "the stop entity keeps its own mark");
        assert!(is_unmarked(&app, c));
    }

    #[test]
    fn test_remove_stop_restores_subtree() {
        let mut app = setup();
        let (root, a, b, c) = linear_chain(&mut app);
        app.world_mut().entity_mut(root).insert(PropagateOutline);
        app.world_mut().entity_mut(b).insert(StopPropagateOutline);
        flush(&mut app);
        assert!(is_unmarked(&app, c));

        app.world_mut()
            .entity_mut(b)
            .remove::<StopPropagateOutline>();
        flush(&mut app);

        assert!(is_marked(&app, a));
        assert!(is_marked(&app, b));
        assert!(is_marked(&app, c));
    }

    #[test]
    fn test_stop_present_before_propagate() {
        let mut app = setup();
        let root = spawn(&mut app, None, ());
        let a = spawn(&mut app, Some(root), StopPropagateOutline);
        let b = spawn(&mut app, Some(a), ());
        flush(&mut app);

        app.world_mut().entity_mut(root).insert(PropagateOutline);
        flush(&mut app);

        assert!(is_marked(&app, a), "the boundary itself is still marked");
        assert!(is_unmarked(&app, b), "the boundary blocks its children");
    }

    #[test]
    fn test_attach_new_child_to_marked_parent() {
        let mut app = setup();
        let (root, a, _b, _c) = linear_chain(&mut app);
        app.world_mut().entity_mut(root).insert(PropagateOutline);
        flush(&mut app);

        // Attach a new subtree under the marked `a`.
        let z = spawn(&mut app, Some(a), ());
        let z_child = spawn(&mut app, Some(z), ());
        flush(&mut app);

        assert!(is_marked(&app, z));
        assert!(is_marked(&app, z_child));
    }

    #[test]
    fn test_reparent_into_propagating_region() {
        let mut app = setup();
        let (root, a, _b, _c) = linear_chain(&mut app);
        app.world_mut().entity_mut(root).insert(PropagateOutline);

        // A detached subtree.
        let x = spawn(&mut app, None, ());
        let y = spawn(&mut app, Some(x), ());
        flush(&mut app);
        assert!(is_unmarked(&app, x));

        app.world_mut().entity_mut(x).insert(ChildOf(a));
        flush(&mut app);

        assert!(is_marked(&app, x));
        assert!(is_marked(&app, y));
    }

    #[test]
    fn test_reparent_out_of_region() {
        let mut app = setup();
        let (root, a, _b, _c) = linear_chain(&mut app);
        app.world_mut().entity_mut(root).insert(PropagateOutline);
        let x = spawn(&mut app, Some(a), ());
        let y = spawn(&mut app, Some(x), ());
        flush(&mut app);
        assert!(is_marked(&app, x));
        assert!(is_marked(&app, y));

        // Move `x` out to a non-propagating root.
        let other = spawn(&mut app, None, ());
        app.world_mut().entity_mut(x).insert(ChildOf(other));
        flush(&mut app);

        assert!(is_unmarked(&app, x));
        assert!(is_unmarked(&app, y));
    }

    #[test]
    fn test_orphan_removes_marks() {
        let mut app = setup();
        let (root, a, b, c) = linear_chain(&mut app);
        app.world_mut().entity_mut(root).insert(PropagateOutline);
        flush(&mut app);
        assert!(is_marked(&app, b));

        app.world_mut().entity_mut(b).remove::<ChildOf>();
        flush(&mut app);

        assert!(is_unmarked(&app, b));
        assert!(is_unmarked(&app, c));
        assert!(
            is_marked(&app, a),
            "ancestor above the orphan is unaffected"
        );
    }

    #[test]
    fn test_nested_propagate_boundary() {
        let mut app = setup();
        // root -> a -> d(source) -> e
        let root = spawn(&mut app, None, ());
        let a = spawn(&mut app, Some(root), ());
        let d = spawn(&mut app, Some(a), PropagateOutline);
        let e = spawn(&mut app, Some(d), ());
        flush(&mut app);
        assert!(is_marked(&app, e));
        assert!(is_unmarked(&app, d));

        app.world_mut().entity_mut(root).insert(PropagateOutline);
        flush(&mut app);
        assert!(is_marked(&app, a));
        assert!(
            is_unmarked(&app, d),
            "nested source is excluded from outer region"
        );
        assert!(is_marked(&app, e));

        // Remove the outer source; the nested region stays intact.
        app.world_mut()
            .entity_mut(root)
            .remove::<PropagateOutline>();
        flush(&mut app);
        assert!(is_unmarked(&app, a));
        assert!(is_unmarked(&app, d));
        assert!(is_marked(&app, e), "nested region is preserved");
    }

    #[test]
    fn test_nested_removal_rejoins_outer_region() {
        let mut app = setup();
        // root(source) -> a -> d(source) -> e
        let root = spawn(&mut app, None, PropagateOutline);
        let a = spawn(&mut app, Some(root), ());
        let d = spawn(&mut app, Some(a), PropagateOutline);
        let e = spawn(&mut app, Some(d), ());
        flush(&mut app);
        assert!(is_marked(&app, a));
        assert!(is_unmarked(&app, d));
        assert!(is_marked(&app, e));

        // Demote `d`; it rejoins the outer region.
        app.world_mut().entity_mut(d).remove::<PropagateOutline>();
        flush(&mut app);
        assert!(
            is_marked(&app, d),
            "demoted source rejoins the outer region"
        );
        assert!(is_marked(&app, e));
    }

    #[test]
    fn test_stop_takes_precedence_over_source() {
        let mut app = setup();
        // A source that is also stopped.
        let root = spawn(&mut app, None, (PropagateOutline, StopPropagateOutline));
        let a = spawn(&mut app, Some(root), ());
        let b = spawn(&mut app, Some(a), ());
        flush(&mut app);
        assert!(is_unmarked(&app, a), "stop takes precedence over source");
        assert!(is_unmarked(&app, b));

        // Lift the stop.
        app.world_mut()
            .entity_mut(root)
            .remove::<StopPropagateOutline>();
        flush(&mut app);
        assert!(is_marked(&app, a));
        assert!(is_marked(&app, b));

        // Re-apply the stop.
        app.world_mut()
            .entity_mut(root)
            .insert(StopPropagateOutline);
        flush(&mut app);
        assert!(is_unmarked(&app, a));
        assert!(is_unmarked(&app, b));
    }

    #[test]
    fn test_batch_spawn_cascade() {
        let mut app = setup();
        // Spawn the whole chain before flushing: each marker insert is still
        // queued when the next child attaches, yet the ripple must mark every
        // descendant.
        let root = spawn(&mut app, None, PropagateOutline);
        let a = spawn(&mut app, Some(root), ());
        let b = spawn(&mut app, Some(a), ());
        let c = spawn(&mut app, Some(b), ());
        flush(&mut app);

        assert!(is_unmarked(&app, root));
        assert!(is_marked(&app, a));
        assert!(is_marked(&app, b));
        assert!(is_marked(&app, c));
    }

    #[test]
    fn test_deep_batch_spawn() {
        let mut app = setup();
        // Eight levels below the source, all spawned before a single flush.
        let root = spawn(&mut app, None, PropagateOutline);
        let mut parent = root;
        let mut chain = Vec::new();
        for _ in 0..8 {
            parent = spawn(&mut app, Some(parent), ());
            chain.push(parent);
        }
        flush(&mut app);

        assert!(is_unmarked(&app, root));
        for entity in chain {
            assert!(is_marked(&app, entity), "every level must be marked");
        }
    }

    #[test]
    fn test_promote_marked_child_to_source() {
        let mut app = setup();
        // root(source) -> a -> b, with `a` and `b` as marked children.
        let root = spawn(&mut app, None, PropagateOutline);
        let a = spawn(&mut app, Some(root), ());
        let b = spawn(&mut app, Some(a), ());
        flush(&mut app);
        assert!(is_marked(&app, a));
        assert!(is_marked(&app, b));

        // Promote `a` to a source: it sheds its own mark but keeps `b` marked,
        // now inheriting from `a` instead of `root`.
        app.world_mut().entity_mut(a).insert(PropagateOutline);
        flush(&mut app);

        assert!(
            is_unmarked(&app, a),
            "a is now a source, not a marked child"
        );
        assert!(is_marked(&app, b), "b keeps inheriting from the new source");
    }

    #[test]
    fn test_batch_spawn_cascade_deferred() {
        let mut app = setup();
        // Build the whole chain via deferred `Commands`: nothing applies until
        // the single flush.
        let (root, a, b, c);
        {
            let world = app.world_mut();
            let mut commands = world.commands();
            root = commands.spawn(PropagateOutline).id();
            a = commands.spawn(ChildOf(root)).id();
            b = commands.spawn(ChildOf(a)).id();
            c = commands.spawn(ChildOf(b)).id();
        }
        app.world_mut().flush();

        assert!(is_unmarked(&app, root));
        assert!(is_marked(&app, a));
        assert!(is_marked(&app, b));
        assert!(is_marked(&app, c));
    }

    #[test]
    fn test_reparent_marked_subtree_under_pending_parent_deferred() {
        let mut app = setup();
        let root = spawn(&mut app, None, PropagateOutline);
        let x = spawn(&mut app, Some(root), ());
        let y = spawn(&mut app, Some(x), ());
        flush(&mut app);
        assert!(is_marked(&app, x));
        assert!(is_marked(&app, y));

        // Via deferred `Commands`: spawn `p` under the source and reparent `x`
        // under `p`, all pending until flush.
        {
            let world = app.world_mut();
            let mut commands = world.commands();
            let p = commands.spawn(ChildOf(root)).id();
            commands.entity(x).insert(ChildOf(p));
        }
        app.world_mut().flush();

        assert!(is_marked(&app, x));
        assert!(is_marked(&app, y));
    }

    #[test]
    fn test_reparent_marked_subtree_under_pending_parent() {
        let mut app = setup();
        let root = spawn(&mut app, None, PropagateOutline);
        flush(&mut app);

        // A marked subtree `x -> y` under the source.
        let x = spawn(&mut app, Some(root), ());
        let y = spawn(&mut app, Some(x), ());
        flush(&mut app);
        assert!(is_marked(&app, x));
        assert!(is_marked(&app, y));

        // One unflushed batch: attach `p` under the source (its mark still
        // queued) then reparent `x` under `p`; the whole `p -> x -> y` chain
        // must end marked.
        let p = spawn(&mut app, Some(root), ());
        app.world_mut().entity_mut(x).insert(ChildOf(p));
        flush(&mut app);

        assert!(is_marked(&app, p));
        assert!(is_marked(&app, x));
        assert!(is_marked(&app, y));
    }
}