codecraft 0.1.1

A minimalist 3D game engine built on parts of Bevy (ECS, color) with wgpu and winit: OpenPBR materials, clustered lighting, an immediate-mode UI, audio and gamepad haptics
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Dev mode: what somebody building the game sees, and a player does not.
//!
//! F12 turns it on and off, and a small `DEV` sits in the corner while it is
//! on so nobody wonders why the camera is behaving strangely. F11 shows and
//! hides the outliner while it is on, F10 steps through the faces the UI is
//! lettered in, and F9 puts the frame profiler up. What it *shows* is the scene's business — a grid, a free
//! camera, gizmos on the lights — since only the scene knows how big its world
//! is; this is the switch they all read.
//!
//! ```no_run
//! # use codecraft::AppState;
//! # fn demo(app: &mut AppState) {
//! if app.dev_mode() {
//!     // show the working
//! }
//! # }
//! ```

mod menu;

use menu::{BADGE_ENTITIES, badge, menu};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use crate::ecs::{
    Application, Commands, Component, Entity, Plugin, Res, ResMut, Resource, With, World,
};
use crate::input::{KeyCode, Keys};
use crate::ui::{ButtonIcons, Interaction, MouseInput, PointerCapture, ScreenSize, Submenu};

/// The key that turns it on and off.
pub const TOGGLE: KeyCode = KeyCode::F12;

/// The key that shows and hides the outliner, while dev mode is on.
///
/// Beside F12 on the keyboard as it is beside it here: one turns the working
/// on, the next says what the scene is made of.
pub const OUTLINER: KeyCode = KeyCode::F11;

/// The key that steps through the five faces the UI can be drawn in.
///
/// Here rather than in a settings screen because choosing between them is a
/// matter of looking at them: they are metric-compatible, so cycling changes
/// the lettering under a UI that does not otherwise move, and the only way to
/// judge that is side by side.
pub const FONT: KeyCode = KeyCode::F10;

/// The key that shows and hides the frame profiler, while dev mode is on.
pub const PROFILER: KeyCode = KeyCode::F9;

/// Whether dev mode is on.
#[derive(Resource, Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct DevMode(pub bool);

impl DevMode {
    pub fn is_on(self) -> bool {
        self.0
    }
}

/// The badge in the corner, and anything it opened.
///
/// Not a scene entity on purpose: dev mode survives a scene change, so the
/// thing that says it is on has to as well.
#[derive(Component, Clone, Copy, Debug)]
pub struct DevBadge;

/// Whether the dev menu is open, shared with the badge's click handler --
/// which runs inside the UI schedule and so cannot reach the world itself.
#[derive(Resource, Clone, Default)]
pub struct DevMenu {
    open: Arc<AtomicBool>,
    /// Set by VIEW > OUTLINER, and spent by [`dev_menu_system`] -- a button's
    /// callback runs inside the UI schedule and cannot reach the world.
    outliner_requested: Arc<AtomicBool>,
    /// The panel and everything in it, to take down when it closes.
    entities: Vec<Entity>,
    /// The badge's own button, so its caret can be turned over.
    badge_button: Option<Entity>,
}

impl DevMenu {
    pub fn is_open(&self) -> bool {
        self.open.load(Ordering::Relaxed)
    }
}
/// Flips dev mode on F12, and puts the badge up or takes it down.
pub fn dev_mode_system(keys: Res<Keys>, dev: Res<DevMode>, mut commands: Commands) {
    if !keys.just_pressed(TOGGLE) {
        return;
    }
    let on = !dev.is_on();
    // The badge and the menu are built straight into the world rather than
    // through `Commands`: a panel lays itself out against the screen and
    // hands back the entities it made, and both of those need the world now
    // rather than at the end of the frame.
    commands.queue(move |world: &mut World| set(world, on));
}

/// Turns dev mode on or off, and puts the badge up or takes it down.
///
/// What F12 does, for an app that wants to start in dev mode rather than be
/// switched into it -- see [`crate::AppState::set_dev_mode`]. Setting it to
/// what it already is rebuilds the badge and closes the menu, which is
/// harmless.
pub fn set(world: &mut World, on: bool) {
    let was = std::mem::replace(&mut world.resource_mut::<DevMode>().0, on);
    if was != on {
        log::info!("dev mode {}", if on { "on" } else { "off" });
    }

    let mut menu = world.remove_resource::<DevMenu>().unwrap_or_default();
    for entity in menu.entities.drain(..) {
        if let Ok(entity) = world.get_entity_mut(entity) {
            entity.despawn();
        }
    }
    // A badge from before the menu knew about it -- there should be none,
    // but a stray one would sit in the corner for the rest of the run.
    let strays: Vec<Entity> = world
        .query_filtered::<Entity, With<DevBadge>>()
        .iter(world)
        .collect();
    for entity in strays {
        world.entity_mut(entity).despawn();
    }

    if on {
        let (width, height) = {
            let screen = world.resource::<ScreenSize>();
            (screen.width, screen.height)
        };
        let open = menu.open.clone();
        let layout = badge(world, open, width, height);
        menu.badge_button = layout.buttons.first().copied();
        menu.entities = layout.entities();
        for &entity in &menu.entities {
            world.entity_mut(entity).insert(DevBadge);
        }
    } else {
        menu.open.store(false, Ordering::Relaxed);
        menu.badge_button = None;
        // The outliner is dev-only, so it goes when dev mode does.
        world.resource_mut::<crate::ui::Outliner>().open = false;
    }
    world.insert_resource(menu);
}

/// Shows and hides the outliner on [`OUTLINER`], while dev mode is on.
///
/// The same thing VIEW > OUTLINER does, and worth a key of its own because it
/// is the panel you flick on to find what something is called and off again
/// to see the thing itself. Three clicks through a menu, or one key.
///
/// Dead outside dev mode: the outliner is dev-only, and F11 belongs to
/// whatever the game wants it for the rest of the time.
pub fn outliner_key_system(
    keys: Res<Keys>,
    dev: Res<DevMode>,
    mut outliner: ResMut<crate::ui::Outliner>,
) {
    if !dev.is_on() || !keys.just_pressed(OUTLINER) {
        return;
    }
    outliner.toggle();
    log::info!(
        "outliner {}",
        match outliner.is_open() {
            true => "up",
            false => "down",
        },
    );
}

/// Shows and hides the frame profiler on [`PROFILER`], while dev mode is on.
///
/// Off until asked for: timing costs a little, and a chart nobody wanted is a
/// panel over the game.
pub fn profiler_key_system(
    keys: Res<Keys>,
    dev: Res<DevMode>,
    mut profiler: ResMut<crate::ui::Profiler>,
) {
    if dev.is_on() && keys.just_pressed(PROFILER) {
        profiler.on = !profiler.on;
        log::info!("profiler {}", match profiler.on {
            true => "on",
            false => "off",
        });
    }
}

/// Steps through the five Monaspace faces on [`FONT`], while dev mode is on.
pub fn font_key_system(
    keys: Res<Keys>,
    dev: Res<DevMode>,
    mut font: ResMut<crate::ui::Font>,
) {
    if dev.is_on() && keys.just_pressed(FONT) {
        font.cycle();
    }
}

/// Puts the dev menu up and takes it down as the badge is clicked.
pub fn dev_menu_system(dev: Res<DevMode>, mut commands: Commands) {
    if !dev.is_on() {
        return;
    }
    commands.queue(|world: &mut World| {
        let mut menu = world.remove_resource::<DevMenu>().unwrap_or_default();

        // VIEW > OUTLINER, taken and spent: the panel is the outliner's own
        // business, and the menu only asks.
        if menu.outliner_requested.swap(false, Ordering::Relaxed) {
            world.resource_mut::<crate::ui::Outliner>().toggle();
        }

        // Two things close the menu, and neither of them is the badge.
        //
        // Choosing an entry: a menu you picked from has done its job, and one
        // that stays up over the thing it just changed is in the way. A row
        // that only opens a submenu is not a choice, so those are left alone.
        //
        // Clicking anywhere else: the click was meant for the game, and
        // reaching back up to the corner to put the menu away is a chore
        // nobody should have to do.
        if menu.entities.len() > BADGE_ENTITIES {
            let chosen = menu.entities[BADGE_ENTITIES..].iter().any(|&entity| {
                world
                    .get::<Interaction>(entity)
                    .is_some_and(|interaction| interaction.clicked)
                    && world.get::<Submenu>(entity).is_none()
            });
            let elsewhere = world.resource::<MouseInput>().just_pressed
                && !world.resource::<PointerCapture>().over_panel;
            if chosen || elsewhere {
                menu.open.store(false, Ordering::Relaxed);
            }
        }

        let wanted = menu.open.load(Ordering::Relaxed);

        // The caret says which way the menu will go next, not which way it
        // went: up to open it, and back down to put it away.
        if let Some(button) = menu.badge_button {
            if let Some(mut icons) = world.get_mut::<ButtonIcons>(button) {
                icons.trailing = Some(match wanted {
                    true => crate::ui::icons::path::CARET_DOWN,
                    false => crate::ui::icons::path::CARET_UP,
                });
            }
        }
        // The badge's own entities are the first `badge_len`; the menu's are
        // whatever came after, so this is the test for "is the menu up".
        let up = menu.entities.len() > BADGE_ENTITIES;
        if wanted != up {
            if wanted {
                let (width, height) = {
                    let screen = world.resource::<ScreenSize>();
                    (screen.width, screen.height)
                };
                let requested = menu.outliner_requested.clone();
                let layout = self::menu(world, requested, width, height);
                menu.entities.extend(layout.entities());
            } else {
                for entity in menu.entities.split_off(BADGE_ENTITIES) {
                    if let Ok(entity) = world.get_entity_mut(entity) {
                        entity.despawn();
                    }
                }
            }
        }
        world.insert_resource(menu);
    });
}

pub struct DevPlugin;

impl Plugin for DevPlugin {
    fn build(&self, app: &mut Application) {
        app.init_resource::<DevMode>();
        app.init_resource::<DevMenu>();
        // The badge is a widget, so it needs to know how big the screen is.
        // `UiPlugin` inserts this too; whichever runs first wins and they
        // agree about the default.
        app.init_resource::<ScreenSize>();
        // Toggled by F11 as well as by the menu, and read here either way.
        // `UiPlugin` owns it; this only fills in for an app built without one.
        app.init_resource::<crate::ui::Outliner>();
        app.init_resource::<crate::ui::Font>();
        app.add_update_systems(dev_mode_system);
        app.add_update_systems(dev_menu_system);
        app.add_update_systems(outliner_key_system);
        app.add_update_systems(font_key_system);
        app.init_resource::<crate::ui::Profiler>();
        app.add_update_systems(profiler_key_system);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ecs::Application;
    use crate::ui::{CursorPosition, Submenu, UiPlugin, Visibility};

    /// An app with the dev plugin, the UI it builds on, and the input both
    /// read.
    fn app() -> Application {
        let mut app = Application::new();
        app.add_plugin(UiPlugin);
        app.insert_resource(Keys::default());
        app.add_plugin(DevPlugin);
        app.world.insert_resource(ScreenSize {
            width: 1280.0,
            height: 800.0,
        });
        app
    }

    /// A press and a release, with the frame between them run — a key held
    /// down is not a second press.
    fn press(app: &mut Application, key: KeyCode) {
        app.world.resource_mut::<Keys>().press(key, false);
        app.update();
        let mut keys = app.world.resource_mut::<Keys>();
        keys.end_frame();
        keys.release(key);
    }

    fn press_f12(app: &mut Application) {
        press(app, TOGGLE);
    }

    fn outliner_open(app: &Application) -> bool {
        app.world.resource::<crate::ui::Outliner>().is_open()
    }

    fn badges(app: &mut Application) -> usize {
        app.world
            .query_filtered::<Entity, With<DevBadge>>()
            .iter(&app.world)
            .count()
    }

    /// Clicks whatever is under the middle of `entity`.
    fn click(app: &mut Application, entity: Entity) {
        let rect = {
            let world = &app.world;
            let pos = world.get::<crate::ui::Position>(entity).unwrap();
            let size = world.get::<crate::ui::Size>(entity).unwrap();
            pos.rect(size)
        };
        app.world.insert_resource(CursorPosition {
            x: rect.center_x(),
            y: rect.center_y(),
        });
        app.world.resource_mut::<crate::ui::MouseInput>().left_down = true;
        app.world
            .resource_mut::<crate::ui::MouseInput>()
            .just_pressed = true;
        app.update();
        app.world.resource_mut::<crate::ui::MouseInput>().left_down = false;
        app.update();
    }


    /// A press somewhere the panels are not, which is what a click on the
    /// game looks like from here.
    fn click_at(app: &mut Application, x: f32, y: f32) {
        app.world.insert_resource(CursorPosition { x, y });
        let mut mouse = app.world.resource_mut::<crate::ui::MouseInput>();
        mouse.left_down = true;
        mouse.just_pressed = true;
        app.update();
        app.world.insert_resource(crate::ui::MouseInput::default());
        app.update();
    }

    /// A leaf entry in the open menu -- one that does something rather than
    /// opening another menu.
    fn leaf(app: &mut Application) -> Entity {
        let menu = app.world.resource::<DevMenu>().clone();
        menu.entities[BADGE_ENTITIES..]
            .iter()
            .copied()
            .find(|&entity| {
                app.world.get::<crate::ui::Interaction>(entity).is_some()
                    && app.world.get::<crate::ui::Submenu>(entity).is_none()
            })
            .expect("the menu should have an entry that is not a submenu")
    }

    /// A menu you have picked from has done its job.
    #[test]
    fn choosing_an_entry_closes_the_menu() {
        let mut app = app();
        press_f12(&mut app);
        let badge = badge_button(&mut app);
        click(&mut app, badge);
        assert!(app.world.resource::<DevMenu>().is_open());

        let entry = leaf(&mut app);
        click(&mut app, entry);
        app.update();
        assert!(
            !app.world.resource::<DevMenu>().is_open(),
            "picking from a menu puts it away",
        );
    }

    /// A row that only opens another menu is not a choice.
    #[test]
    fn hovering_a_submenu_row_leaves_the_menu_up() {
        let mut app = app();
        press_f12(&mut app);
        let badge = badge_button(&mut app);
        click(&mut app, badge);

        let parent = {
            let mut query = app.world.query::<(Entity, &crate::ui::Submenu)>();
            query
                .iter(&app.world)
                .next()
                .map(|(entity, _)| entity)
                .expect("the menu has submenus")
        };
        click(&mut app, parent);
        assert!(
            app.world.resource::<DevMenu>().is_open(),
            "a row that only opens another menu has not been chosen",
        );
    }

    /// The click was meant for the game, and reaching back to the corner to
    /// put the menu away is a chore.
    #[test]
    fn clicking_away_from_the_menu_closes_it() {
        let mut app = app();
        press_f12(&mut app);
        let badge = badge_button(&mut app);
        click(&mut app, badge);
        assert!(app.world.resource::<DevMenu>().is_open());

        click_at(&mut app, 20.0, 20.0);
        assert!(
            !app.world.resource::<DevMenu>().is_open(),
            "a click on the game is not a click on the menu",
        );
    }

    /// The caret says which way the menu goes next, not which way it went.
    #[test]
    fn the_badge_caret_turns_over_with_the_menu() {
        let mut app = app();
        press_f12(&mut app);
        let badge = badge_button(&mut app);
        app.update();

        let caret = |app: &Application| {
            app.world
                .get::<crate::ui::ButtonIcons>(badge)
                .and_then(|icons| icons.trailing)
        };
        assert_eq!(caret(&app), Some(crate::ui::icons::path::CARET_UP));

        click(&mut app, badge);
        app.update();
        assert_eq!(
            caret(&app),
            Some(crate::ui::icons::path::CARET_DOWN),
            "it is up, so the arrow offers to put it back down",
        );
    }
    /// The button the badge is made of.
    fn badge_button(app: &mut Application) -> Entity {
        app.world
            .query_filtered::<Entity, (With<DevBadge>, With<crate::ui::Interaction>)>()
            .iter(&app.world)
            .next()
            .expect("the badge should have a button in it")
    }

    #[test]
    fn f12_turns_it_on_and_off_again() {
        let mut app = app();
        assert!(
            !app.world.resource::<DevMode>().is_on(),
            "off to start with"
        );

        press_f12(&mut app);
        assert!(app.world.resource::<DevMode>().is_on());

        press_f12(&mut app);
        assert!(!app.world.resource::<DevMode>().is_on());
    }

    #[test]
    fn the_badge_is_up_only_while_it_is_on() {
        let mut app = app();
        assert_eq!(badges(&mut app), 0);

        press_f12(&mut app);
        assert!(badges(&mut app) > 0, "the badge says dev mode is on");

        press_f12(&mut app);
        assert_eq!(badges(&mut app), 0, "and goes away with it");
    }

    #[test]
    fn f11_shows_the_outliner_and_hides_it_again() {
        let mut app = app();
        press_f12(&mut app);
        assert!(!outliner_open(&app), "down to start with");

        press(&mut app, OUTLINER);
        assert!(outliner_open(&app), "one press puts it up");

        press(&mut app, OUTLINER);
        assert!(!outliner_open(&app), "and the next takes it down");
    }

    #[test]
    fn f10_steps_through_the_faces() {
        use crate::ui::{Family, Font};

        let mut app = app();
        press_f12(&mut app);
        assert_eq!(app.world.resource::<Font>().family(), Family::Neon);

        press(&mut app, FONT);
        assert_eq!(app.world.resource::<Font>().family(), Family::Neon.next());

        // All the way round and back, since the point is to compare them.
        for _ in 1..Family::ALL.len() {
            press(&mut app, FONT);
        }
        assert_eq!(app.world.resource::<Font>().family(), Family::Neon);
    }

    #[test]
    fn f10_does_nothing_outside_dev_mode() {
        use crate::ui::{Family, Font};

        let mut app = app();
        press(&mut app, FONT);
        assert_eq!(app.world.resource::<Font>().family(), Family::Neon);
    }

    /// F11 is the game's the rest of the time.
    #[test]
    fn f11_does_nothing_outside_dev_mode() {
        let mut app = app();
        assert!(!app.world.resource::<DevMode>().is_on());

        press(&mut app, OUTLINER);
        assert!(!outliner_open(&app), "no outliner without the working");
    }

    #[test]
    fn leaving_dev_mode_takes_the_outliner_with_it() {
        let mut app = app();
        press_f12(&mut app);
        press(&mut app, OUTLINER);
        assert!(outliner_open(&app));

        press_f12(&mut app);
        assert!(
            !outliner_open(&app),
            "the outliner cannot outlive the mode it belongs to",
        );
    }

    #[test]
    fn nothing_happens_on_the_frames_between() {
        let mut app = app();
        press_f12(&mut app);

        // Held, not pressed again: it should not flicker back off.
        app.update();
        app.update();
        assert!(app.world.resource::<DevMode>().is_on());
    }

    #[test]
    fn the_badge_opens_the_menu_and_closes_it_again() {
        let mut app = app();
        press_f12(&mut app);
        let badge = badge_button(&mut app);

        assert!(!app.world.resource::<DevMenu>().is_open());
        click(&mut app, badge);
        assert!(app.world.resource::<DevMenu>().is_open(), "one click opens");

        click(&mut app, badge);
        assert!(
            !app.world.resource::<DevMenu>().is_open(),
            "and the next one closes",
        );
    }

    #[test]
    fn leaving_dev_mode_takes_the_menu_with_it() {
        let mut app = app();
        press_f12(&mut app);
        let badge = badge_button(&mut app);
        click(&mut app, badge);
        assert!(app.world.resource::<DevMenu>().is_open());

        press_f12(&mut app);
        assert!(
            !app.world.resource::<DevMenu>().is_open(),
            "the menu cannot outlive the mode it belongs to",
        );
        assert_eq!(badges(&mut app), 0);
    }

    /// The one submenu behaviour worth pinning: it is not up until the
    /// pointer is on the thing that opens it, and it opens away from the edge
    /// the menu is against.
    #[test]
    fn a_submenu_waits_to_be_hovered_and_opens_to_the_left() {
        let mut app = app();
        press_f12(&mut app);
        let badge = badge_button(&mut app);
        click(&mut app, badge);

        let (parent, submenu_panel) = {
            let world = &mut app.world;
            let mut query = world.query::<(Entity, &Submenu)>();
            let (parent, submenu) = query.iter(world).next().expect("the menu has submenus");
            (parent, submenu.panel)
        };

        let shown = |app: &Application| {
            app.world
                .get::<Visibility>(submenu_panel)
                .copied()
                .unwrap_or_default()
                .0
        };
        assert!(!shown(&app), "closed until the pointer arrives");

        let parent_rect = {
            let world = &app.world;
            let pos = world.get::<crate::ui::Position>(parent).unwrap();
            let size = world.get::<crate::ui::Size>(parent).unwrap();
            pos.rect(size)
        };
        app.world.insert_resource(CursorPosition {
            x: parent_rect.center_x(),
            y: parent_rect.center_y(),
        });
        app.update();

        assert!(shown(&app), "hovering the parent opens it");
        let child = app.world.get::<crate::ui::Position>(submenu_panel).unwrap();
        assert!(
            child.x < parent_rect.x,
            "a menu in the bottom-right corner has no room to its right, so \
             its submenus go left: child at {} against a parent at {}",
            child.x,
            parent_rect.x,
        );

        // And it stays up while the pointer is inside it, so the pointer can
        // travel from one to the other.
        let (x, y) = {
            let world = &app.world;
            let pos = world.get::<crate::ui::Position>(submenu_panel).unwrap();
            let size = world.get::<crate::ui::Size>(submenu_panel).unwrap();
            let rect = pos.rect(size);
            (rect.center_x(), rect.center_y())
        };
        app.world.insert_resource(CursorPosition { x, y });
        app.update();
        assert!(shown(&app), "and stays open once the pointer is in it");

        // Away from both, and it closes.
        app.world
            .insert_resource(CursorPosition { x: 10.0, y: 10.0 });
        app.update();
        assert!(!shown(&app), "and closes once the pointer leaves both");
    }
}