ponko 0.0.1

A fast 2D game engine for simulating millions of things: ECS on all cores, hot reload, an in-game inspector, AI-agent control, and a C++ API.
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
//! The runtime inspector: an egui overlay, toggled with the
//! backquote key (`` ` ``, left of 1), that shows the
//! running game. Sim controls (pause, step, speed), frame and system times,
//! every entity with its components, live editing of reflected components
//! and resources, and picking units in the world.
//!
//! Paused, it's an editor ([`crate::editor`]): select, drag, duplicate and
//! delete entities in the world, add and remove components, undo, place
//! prefabs, save and load scenes, and Play / Stop.
//!
//! Built with the `inspector` feature; turned on per game with
//! [`Game::inspector`](crate::Game::inspector) (on in debug builds). While
//! closed, it costs nothing: egui doesn't run and no events are kept.

use std::collections::HashSet;

use egui::{
    Button, CollapsingHeader, Color32, DragValue, Key, RichText, ScrollArea, Sense, Stroke, TextEdit, Ui, vec2,
};
use glam::Vec2;
use ponko_ecs::{ComponentId, Entity, World};
use serde_json::Value;

use crate::editor::Editor;
use crate::reflect::short_name;
use crate::{Ctx, Rect, Sprite, Transform2D, WorldReflect};

/// Environment variable that turns the inspector on (`1`) or off (`0`),
/// overriding [`Game::inspector`](crate::Game::inspector).
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub(crate) const ENV_INSPECTOR: &str = "CRABBY_INSPECTOR";

/// The key that opens and closes the inspector.
pub(crate) const TOGGLE_CODE: winit::keyboard::KeyCode = winit::keyboard::KeyCode::Backquote;

/// Whether the inspector is on, and whether it starts open:
/// `CRABBY_INSPECTOR` (`1`, `0` or `open`) if set, else the game's choice.
/// In web builds, the page's `?inspector` (`=1`, `=0`, `=open`) instead.
pub(crate) fn enabled(game_default: bool) -> (bool, bool) {
    #[cfg(target_arch = "wasm32")]
    let setting = crate::web::query_param("inspector").map(|v| if v.is_empty() { "1".into() } else { v });
    #[cfg(not(target_arch = "wasm32"))]
    let setting = std::env::var(ENV_INSPECTOR).ok();
    match setting.as_deref() {
        Some("1") => (true, false),
        Some("open") => (true, true),
        Some("0") => (false, false),
        _ => (game_default, false),
    }
}

/// Frames kept for the frame-time graph.
const HISTORY: usize = 240;
/// How close (window pixels) a click must be to an entity to pick it.
const PICK_RADIUS: f32 = 24.0;
/// Height of one row in the entity list.
const ROW_HEIGHT: f32 = 18.0;
/// Where Ctrl+D puts the copy, from the original (world units).
const DUPLICATE_OFFSET: Vec2 = Vec2::new(16.0, 16.0);
/// Folder of `assets/` the prefab palette lists.
const PREFAB_FOLDER: &str = "prefabs";
/// Scene file used when none is typed.
const DEFAULT_SCENE: &str = "levels/level.json";

/// What the overlay draws this frame: egui's triangles and texture changes.
pub(crate) struct Overlay {
    pub primitives: Vec<egui::ClippedPrimitive>,
    pub textures: egui::TexturesDelta,
    pub pixels_per_point: f32,
}

impl Overlay {
    /// Adds a frame that wasn't drawn yet: its texture changes still apply.
    pub fn merge_older(&mut self, mut older: Overlay) {
        let mut textures = std::mem::take(&mut older.textures);
        textures.append(std::mem::take(&mut self.textures));
        self.textures = textures;
    }
}

impl Drop for Overlay {
    /// A frame that's never drawn (the game quits first) is fine to drop:
    /// its textures die with the renderer.
    fn drop(&mut self) {
        self.textures.clear();
    }
}

/// The inspector's panels and what's selected in them. Independent of the
/// window, so it can be run (and tested) on any egui context.
#[derive(Default)]
pub(crate) struct Panels {
    pub selected: Option<Entity>,
    /// Only list entities with this component.
    filter: Option<ComponentId>,
    /// The next click in the world selects the entity under it.
    pub picking: bool,
    /// A click to pick at (window pixels), from the window events.
    pub pick_at: Option<Vec2>,
    /// The last edit that failed, shown until the next one.
    error: Option<String>,
    frame_ms: Vec<f32>,
    frame_at: usize,
    /// Non-empty archetypes that pass the filter, and the first row of each.
    rows: Vec<(usize, usize)>,
    reflected: HashSet<String>,
    /// Edits with undo, and Play / Stop.
    pub editor: Editor,
    /// Paused with the panels open: clicks and shortcut keys in the world
    /// go to the editor, not the game.
    pub editing: bool,
    /// The entity being dragged in the world.
    drag: Option<Drag>,
    /// A prefab the next click in the world places.
    placing: Option<String>,
    /// Prefab files in `assets/prefabs/`; `None` until listed.
    prefabs: Option<Vec<String>>,
    scene_path: String,
    prefab_path: String,
    /// What the last scene or prefab action did.
    status: Option<String>,
}

/// An entity being moved with the mouse.
struct Drag {
    e: Entity,
    /// Its transform before, for undo.
    before: Value,
    /// From the mouse to its position, so it doesn't jump to the cursor.
    grab: Vec2,
    moved: bool,
}

impl Panels {
    /// Draws the inspector into `ui` and applies what was changed in it to `ctx`.
    pub fn show(&mut self, ui: &mut Ui, ctx: &mut Ctx) {
        self.editing = ctx.is_paused();
        self.record_frame(ctx.dt() * 1000.0);
        self.reflected.clear();
        self.reflected
            .extend(ctx.world.reflected_components().into_iter().map(str::to_owned));
        if let Some(at) = self.pick_at.take() {
            self.picking = false;
            if let Some(e) = pick(ctx, at) {
                self.selected = Some(e);
            }
        }
        if self.selected.is_some_and(|e| !ctx.world.is_alive(e)) {
            self.selected = None;
        }

        egui::Panel::right("crabby-inspector")
            .default_size(360.0)
            .resizable(true)
            .show(ui, |ui| {
                ScrollArea::vertical().id_salt("inspector-scroll").show(ui, |ui| {
                    self.sim_controls(ui, ctx);
                    self.frame_graph(ui);
                    if let Some(error) = &self.error {
                        ui.colored_label(Color32::LIGHT_RED, error);
                    }
                    CollapsingHeader::new("Edit")
                        .default_open(true)
                        .show(ui, |ui| self.edit_tools(ui, ctx));
                    if let Some(e) = self.selected {
                        ui.separator();
                        self.entity(ui, ctx, e);
                        ui.separator();
                    }
                    CollapsingHeader::new("Entities")
                        .default_open(true)
                        .show(ui, |ui| self.entity_list(ui, ctx));
                    if !ctx.world.reflected_resources().is_empty() {
                        CollapsingHeader::new("Resources")
                            .default_open(true)
                            .show(ui, |ui| self.resources(ui, ctx));
                    }
                    CollapsingHeader::new("Systems")
                        .default_open(true)
                        .show(ui, |ui| systems(ui, ctx));
                });
            });
        if self.editing {
            self.world_input(ui, ctx);
            self.shortcuts(ui, ctx);
        } else if let Some(drag) = self.drag.take() {
            self.end_drag(drag);
        }
        self.highlight(ui, ctx);
    }

    /// Shows an edit's error, or forgets the last one.
    fn report<T>(&mut self, result: Result<T, String>) -> Option<T> {
        match result {
            Ok(v) => {
                self.error = None;
                Some(v)
            }
            Err(e) => {
                self.error = Some(e);
                None
            }
        }
    }

    /// Mouse in the world while paused: click selects (or places the armed
    /// prefab), drag moves, right or middle drag pans, the wheel zooms.
    fn world_input(&mut self, ui: &Ui, ctx: &mut Ctx) {
        let (pos, pressed, down, pan, scroll, shift) = ui.input(|i| {
            let p = &i.pointer;
            let pan = if p.secondary_down() || p.middle_down() {
                p.delta()
            } else {
                egui::Vec2::ZERO
            };
            let pos = p.latest_pos().map(|p| Vec2::new(p.x, p.y));
            (
                pos,
                p.primary_pressed(),
                p.primary_down(),
                pan,
                i.smooth_scroll_delta.y,
                i.modifiers.shift,
            )
        });
        let Some(pos) = pos else { return };
        if !ui.ctx().is_pointer_over_egui() {
            if pan != egui::Vec2::ZERO {
                ctx.camera.position -= Vec2::new(pan.x, pan.y) / ctx.camera.zoom;
            }
            if scroll != 0.0 {
                ctx.camera.zoom_at(pos, 1.1f32.powf(scroll / 50.0));
            }
            if pressed {
                self.click(ctx, pos, shift);
            }
        }
        if let Some(drag) = &mut self.drag {
            if down && ctx.world.is_alive(drag.e) {
                let to = ctx.camera.screen_to_world(pos) + drag.grab;
                if let Some(t) = ctx.world.get_mut::<Transform2D>(drag.e)
                    && t.position != to
                {
                    t.position = to;
                    drag.moved = true;
                }
            } else if let Some(drag) = self.drag.take() {
                self.end_drag(drag);
            }
        }
    }

    /// A click in the world at window point `at`.
    fn click(&mut self, ctx: &mut Ctx, at: Vec2, keep_placing: bool) {
        let world_at = ctx.camera.screen_to_world(at);
        if let Some(path) = self.placing.clone() {
            let placed = self.editor.spawn_prefab(ctx, &path, world_at);
            if let Some(e) = self.report(placed) {
                self.selected = Some(e);
            }
            if !keep_placing {
                self.placing = None;
            }
            return;
        }
        self.selected = pick(ctx, at);
        if let Some(e) = self.selected
            && let Some(t) = ctx.world.get::<Transform2D>(e)
        {
            let grab = t.position - world_at;
            if let Ok(before) = ctx.world.get_json(e, "Transform2D") {
                self.drag = Some(Drag {
                    e,
                    before,
                    grab,
                    moved: false,
                });
            }
        }
    }

    fn end_drag(&mut self, drag: Drag) {
        if drag.moved {
            self.editor.moved(drag.e, drag.before);
        }
    }

    /// Keys while paused: Delete, Ctrl+D (duplicate), Ctrl+Z (undo), Esc.
    fn shortcuts(&mut self, ui: &Ui, ctx: &mut Ctx) {
        if ui.ctx().egui_wants_keyboard_input() {
            return;
        }
        let (delete, duplicate, undo, escape) = ui.input(|i| {
            (
                i.key_pressed(Key::Delete) || i.key_pressed(Key::Backspace),
                i.modifiers.command && i.key_pressed(Key::D),
                i.modifiers.command && i.key_pressed(Key::Z),
                i.key_pressed(Key::Escape),
            )
        });
        if escape {
            self.placing = None;
        }
        if undo {
            self.undo(ctx);
        }
        let Some(e) = self.selected else { return };
        if delete {
            let deleted = self.editor.delete(ctx, e);
            self.report(deleted);
            self.selected = None;
        } else if duplicate {
            let copy = self.editor.duplicate(ctx, e, DUPLICATE_OFFSET);
            if let Some(copy) = self.report(copy) {
                self.selected = Some(copy);
            }
        }
    }

    fn undo(&mut self, ctx: &mut Ctx) {
        let undone = self.editor.undo(ctx);
        self.report(undone);
    }

    /// Undo, new entity, scenes, and the prefab palette.
    fn edit_tools(&mut self, ui: &mut Ui, ctx: &mut Ctx) {
        ui.horizontal(|ui| {
            if ui.add_enabled(self.editor.can_undo(), Button::new("Undo")).clicked() {
                self.undo(ctx);
            }
            if ui
                .button("New entity")
                .on_hover_text("An entity with only a transform, mid-screen")
                .clicked()
            {
                let at = ctx.camera.center();
                self.selected = Some(self.editor.spawn_empty(ctx, at));
            }
        });
        if !self.editing {
            ui.label(RichText::new("Pause to edit in the world").weak());
        } else {
            ui.label(
                RichText::new("Click selects, drag moves, right-drag pans, wheel zooms. Del, Ctrl+D, Ctrl+Z.").weak(),
            );
        }

        ui.horizontal(|ui| {
            ui.label("Scene");
            ui.add(
                TextEdit::singleline(&mut self.scene_path)
                    .hint_text(DEFAULT_SCENE)
                    .desired_width(150.0),
            );
            let path = if self.scene_path.is_empty() {
                DEFAULT_SCENE
            } else {
                &self.scene_path
            }
            .to_owned();
            if ui.button("Save").clicked() {
                let saved = ctx.save_scene(&path).map(|()| format!("saved {path}"));
                self.status = self.report(saved);
            }
            if ui
                .button("Load")
                .on_hover_text("Replaces the world; clears undo")
                .clicked()
            {
                let loaded = ctx.load_scene(&path).map(|()| format!("loaded {path}"));
                if let Some(done) = self.report(loaded) {
                    self.status = Some(done);
                    self.editor = Editor::default();
                    self.selected = None;
                }
            }
        });

        ui.horizontal(|ui| {
            ui.label("Prefab");
            let hint = format!("{PREFAB_FOLDER}/thing.json");
            ui.add(
                TextEdit::singleline(&mut self.prefab_path)
                    .hint_text(hint)
                    .desired_width(150.0),
            );
            let save = ui.add_enabled(
                self.selected.is_some() && !self.prefab_path.is_empty(),
                Button::new("Save"),
            );
            if let Some(e) = self.selected
                && save.on_hover_text("Saves the selected entity as this prefab").clicked()
            {
                let path = self.prefab_path.clone();
                let saved = ctx.save_prefab(e, &path).map(|()| format!("saved {path}"));
                self.status = self.report(saved);
                self.prefabs = None;
            }
        });
        let prefabs = self
            .prefabs
            .get_or_insert_with(|| crate::assets::list(PREFAB_FOLDER, "json"))
            .clone();
        ui.horizontal_wrapped(|ui| {
            for path in &prefabs {
                let name = path.rsplit('/').next().unwrap_or(path).trim_end_matches(".json");
                let armed = self.placing.as_deref() == Some(path.as_str());
                let tip = "Click, then click in the world to place it (hold Shift to place more)";
                if ui.selectable_label(armed, name).on_hover_text(tip).clicked() {
                    self.placing = if armed { None } else { Some(path.clone()) };
                }
            }
            if ui
                .small_button("⟳")
                .on_hover_text(format!("List assets/{PREFAB_FOLDER}/ again"))
                .clicked()
            {
                self.prefabs = None;
            }
        });
        if let Some(status) = &self.status {
            ui.label(RichText::new(status).weak());
        }
    }

    fn record_frame(&mut self, ms: f32) {
        if self.frame_ms.len() < HISTORY {
            self.frame_ms.push(ms);
        } else {
            self.frame_ms[self.frame_at] = ms;
        }
        self.frame_at = (self.frame_at + 1) % HISTORY;
    }

    fn sim_controls(&mut self, ui: &mut Ui, ctx: &mut Ctx) {
        ui.horizontal(|ui| {
            if ctx.is_paused() {
                let tip = if self.editor.playing() {
                    "Resume"
                } else {
                    "Run the game; Stop puts the world back as it is now"
                };
                if ui.button("Play").on_hover_text(tip).clicked() {
                    self.editor.play(ctx);
                }
            } else if ui.button("Pause").clicked() {
                ctx.pause();
            }
            if self.editor.playing() {
                let stop = ui
                    .button("Stop")
                    .on_hover_text("Pause and put the world back as it was at Play");
                if stop.clicked() {
                    let stopped = self.editor.stop(ctx);
                    self.report(stopped);
                    self.drag = None;
                }
                if ui
                    .button("Keep")
                    .on_hover_text("Keep the world as it is: Stop won't go back")
                    .clicked()
                {
                    self.editor.keep();
                }
            }
            if ui.button("Step").on_hover_text("Run one tick (pauses)").clicked() {
                ctx.pause();
                ctx.step();
            }
            let mut speed = ctx.sim_speed();
            let drag = DragValue::new(&mut speed).range(0.0..=16.0).speed(0.02).suffix("×");
            if ui.add(drag).on_hover_text("Simulation speed").changed() {
                ctx.set_sim_speed(speed);
            }
        });
        ui.label(format!(
            "tick {}   {:.0} fps   {} entities",
            ctx.sim_tick(),
            ctx.fps(),
            ctx.world.len()
        ));
    }

    /// The last frames' times as bars; the line marks 60 fps.
    fn frame_graph(&self, ui: &mut Ui) {
        let width = ui.available_width();
        let (rect, response) = ui.allocate_exact_size(vec2(width, 40.0), Sense::hover());
        let painter = ui.painter();
        painter.rect_filled(rect, 2.0, Color32::from_black_alpha(80));
        let target = 1000.0 / 60.0;
        // Scaled to the slowest frame, but at most 4× the target, so one
        // long frame (loading) doesn't flatten the rest.
        let top = self
            .frame_ms
            .iter()
            .copied()
            .fold(target * 2.0, f32::max)
            .min(target * 4.0);
        let bar = rect.width() / HISTORY as f32;
        let n = self.frame_ms.len();
        for i in 0..n {
            // Oldest on the left.
            let ms = if n < HISTORY {
                self.frame_ms[i]
            } else {
                self.frame_ms[(self.frame_at + i) % HISTORY]
            };
            let h = rect.height() * (ms / top).min(1.0);
            let x = rect.left() + i as f32 * bar;
            let color = if ms > target * 1.5 {
                Color32::from_rgb(230, 90, 70)
            } else {
                Color32::from_rgb(90, 180, 120)
            };
            painter.rect_filled(
                egui::Rect::from_min_max(egui::pos2(x, rect.bottom() - h), egui::pos2(x + bar, rect.bottom())),
                0.0,
                color,
            );
        }
        let y = rect.bottom() - rect.height() * target / top;
        painter.hline(rect.x_range(), y, Stroke::new(1.0, Color32::from_white_alpha(60)));
        if n > 0 {
            let last = self.frame_ms[(self.frame_at + HISTORY - 1) % HISTORY];
            response.on_hover_text(format!("frame time; last {last:.2} ms, line at 60 fps"));
        }
    }

    fn resources(&mut self, ui: &mut Ui, ctx: &mut Ctx) {
        let names: Vec<String> = ctx.world.reflected_resources().into_iter().map(str::to_owned).collect();
        for name in names {
            let Ok(mut value) = ctx.world.resource_json(&name) else {
                ui.label(RichText::new(format!("{name} (not inserted)")).weak());
                continue;
            };
            CollapsingHeader::new(&name)
                .id_salt(("resource", &name))
                .show(ui, |ui| {
                    if edit_value(ui, &mut value) {
                        self.error = ctx.world.set_resource_json(&name, value).err().map(|e| e.to_string());
                    }
                });
        }
    }

    fn entity_list(&mut self, ui: &mut Ui, ctx: &mut Ctx) {
        let world = &ctx.world;
        ui.horizontal(|ui| {
            ui.label("With");
            let label = self
                .filter
                .map_or("anything".to_owned(), |id| component_name(world, id));
            egui::ComboBox::from_id_salt("inspector-filter")
                .selected_text(label)
                .show_ui(ui, |ui| {
                    ui.selectable_value(&mut self.filter, None, "anything");
                    for (i, info) in world.components().iter().enumerate() {
                        let Some(id) = world.component_at(i) else { continue };
                        ui.selectable_value(&mut self.filter, Some(id), short_name(info.name()));
                    }
                });
            let pick = ui
                .selectable_label(self.picking, "Pick")
                .on_hover_text("Click a unit in the world to select it");
            if pick.clicked() {
                self.picking = !self.picking;
            }
        });
        let total = list_rows(world, self.filter, &mut self.rows);
        ui.label(RichText::new(format!("{total} matching")).weak());
        let mut clicked = None;
        ScrollArea::vertical()
            .id_salt("inspector-entities")
            .max_height(220.0)
            .auto_shrink([false, true])
            .show_rows(ui, ROW_HEIGHT, total, |ui, range| {
                for row in range {
                    let Some(e) = entity_at(world, &self.rows, row) else {
                        continue;
                    };
                    let text = format!("{e:?}  {}", components_label(world, e));
                    let selected = self.selected == Some(e);
                    if ui.selectable_label(selected, text).clicked() {
                        clicked = Some(e);
                    }
                }
            });
        if clicked.is_some() {
            self.selected = clicked;
        }
    }

    fn entity(&mut self, ui: &mut Ui, ctx: &mut Ctx, e: Entity) {
        let position = ctx.world.get::<Transform2D>(e).map(|t| t.position);
        ui.horizontal(|ui| {
            ui.heading(format!("{e:?}"));
            if let Some(at) = position
                && ui.button("Look at").clicked()
            {
                ctx.camera.look_at(at);
            }
            if ui.button("Duplicate").on_hover_text("Ctrl+D").clicked() {
                let copy = self.editor.duplicate(ctx, e, DUPLICATE_OFFSET);
                if let Some(copy) = self.report(copy) {
                    self.selected = Some(copy);
                }
            }
            if ui.button("Despawn").on_hover_text("Del").clicked() {
                let deleted = self.editor.delete(ctx, e);
                self.report(deleted);
                self.selected = None;
            }
        });
        if self.selected.is_none() {
            return;
        }
        let ids: Vec<ComponentId> = ctx.world.components_of(e).map(<[_]>::to_vec).unwrap_or_default();
        let has: HashSet<String> = ids.iter().map(|&id| component_name(&ctx.world, id)).collect();
        for id in ids {
            let name = component_name(&ctx.world, id);
            if !self.reflected.contains(&name) {
                ui.label(RichText::new(format!("{name} (not reflected)")).weak())
                    .on_hover_text("world.reflect::<T>() makes it readable and editable here");
                continue;
            }
            let Ok(mut value) = ctx.world.get_json(e, &name) else {
                continue;
            };
            let mut remove = false;
            CollapsingHeader::new(&name)
                .id_salt(("component", &name))
                .default_open(true)
                .show(ui, |ui| {
                    if edit_value(ui, &mut value) {
                        let set = self.editor.set(ctx, e, &name, value);
                        self.report(set);
                    }
                    remove = ui.small_button("Remove").clicked();
                });
            if remove {
                let removed = self.editor.remove(ctx, e, &name);
                self.report(removed);
            }
        }
        let mut addable: Vec<&String> = self.reflected.iter().filter(|n| !has.contains(*n)).collect();
        addable.sort();
        let mut add = None;
        egui::ComboBox::from_id_salt("inspector-add")
            .selected_text("Add component")
            .show_ui(ui, |ui| {
                for name in addable {
                    if ui.selectable_label(false, name).clicked() {
                        add = Some(name.clone());
                    }
                }
            });
        if let Some(name) = add {
            let added = self.editor.add(ctx, e, &name);
            self.report(added);
        }
    }

    /// A ring around the selected entity, in the world.
    fn highlight(&self, ui: &Ui, ctx: &Ctx) {
        let Some(e) = self.selected else { return };
        let Some(t) = ctx.world.get::<Transform2D>(e) else {
            return;
        };
        let at = ctx.camera.world_to_screen(t.position);
        let painter = ui.ctx().layer_painter(egui::LayerId::new(
            egui::Order::Background,
            egui::Id::unique("inspector-highlight"),
        ));
        let center = egui::pos2(at.x, at.y);
        painter.circle_stroke(center, 14.0, Stroke::new(2.0, Color32::from_rgb(255, 200, 40)));
        painter.circle_stroke(center, 16.0, Stroke::new(1.0, Color32::from_black_alpha(160)));
    }
}

/// Short name of component `id`, as reflection and the control endpoint name it.
fn component_name(world: &World, id: ComponentId) -> String {
    short_name(world.component_info(id).name())
}

/// The components of `e`, comma separated.
fn components_label(world: &World, e: Entity) -> String {
    let Some(ids) = world.components_of(e) else {
        return String::new();
    };
    let mut out = String::new();
    for (i, &id) in ids.iter().enumerate() {
        if i > 0 {
            out.push_str(", ");
        }
        out.push_str(&component_name(world, id));
    }
    out
}

/// Fills `rows` with each non-empty archetype that has `filter` and the row
/// its entities start at. Returns the number of rows.
fn list_rows(world: &World, filter: Option<ComponentId>, rows: &mut Vec<(usize, usize)>) -> usize {
    rows.clear();
    let mut total = 0;
    for (i, arch) in world.archetypes().iter().enumerate() {
        if arch.is_empty() || filter.is_some_and(|id| !arch.has(id)) {
            continue;
        }
        rows.push((i, total));
        total += arch.len();
    }
    total
}

/// The entity on `row` of a list made by [`list_rows`].
fn entity_at(world: &World, rows: &[(usize, usize)], row: usize) -> Option<Entity> {
    let k = rows.partition_point(|&(_, start)| start <= row).checked_sub(1)?;
    let (arch, start) = rows[k];
    world.archetypes()[arch].entities().get(row - start).copied()
}

/// The entity under window point `at`: the top sprite there (by `z`), else
/// the entity with a `Transform2D` closest to it, within [`PICK_RADIUS`]
/// pixels. Sprites are hit by their unrotated rectangle.
fn pick(ctx: &mut Ctx, at: Vec2) -> Option<Entity> {
    let camera = ctx.camera;
    let world_at = camera.screen_to_world(at);
    let mut top: Option<(f32, Entity)> = None;
    for (e, t, s) in ctx.world.query::<(Entity, &Transform2D, &Sprite)>().iter() {
        let size = s.region.map_or(s.texture.size(), |r| r.size()) * t.scale.abs();
        let min = t.position - size * s.pivot;
        let rect = Rect { min, max: min + size };
        if rect.contains(world_at) && top.is_none_or(|(z, _)| s.z >= z) {
            top = Some((s.z, e));
        }
    }
    if let Some((_, e)) = top {
        return Some(e);
    }
    let mut best: Option<(f32, Entity)> = None;
    for (e, t) in ctx.world.query::<(Entity, &Transform2D)>().iter() {
        let d = camera.world_to_screen(t.position).distance_squared(at);
        if d <= PICK_RADIUS * PICK_RADIUS && best.is_none_or(|(b, _)| d < b) {
            best = Some((d, e));
        }
    }
    best.map(|(_, e)| e)
}

/// Edits a JSON value in place with the widget that fits it. Returns whether it changed.
fn edit_value(ui: &mut Ui, value: &mut Value) -> bool {
    match value {
        Value::Null => {
            ui.label(RichText::new("none").weak());
            false
        }
        Value::Bool(b) => ui.checkbox(b, "").changed(),
        Value::Number(n) => edit_number(ui, n),
        Value::String(s) => {
            // Applied when editing ends, not per key: a half-typed enum
            // variant ("Blu") would be refused and overwritten.
            let id = ui.next_auto_id();
            let mut draft = ui.data_mut(|d| d.get_temp::<String>(id)).unwrap_or_else(|| s.clone());
            let response = ui.text_edit_singleline(&mut draft);
            if response.has_focus() {
                ui.data_mut(|d| d.insert_temp(id, draft));
                false
            } else {
                ui.data_mut(|d| d.remove::<String>(id));
                let changed = response.lost_focus() && draft != *s;
                if changed {
                    *s = draft;
                }
                changed
            }
        }
        Value::Array(items) if items.len() <= 4 && items.iter().all(Value::is_number) => {
            // Vectors and colors: one row.
            let mut changed = false;
            ui.horizontal(|ui| {
                for item in items.iter_mut() {
                    if let Value::Number(n) = item {
                        changed |= edit_number(ui, n);
                    }
                }
            });
            changed
        }
        Value::Array(items) => {
            let mut changed = false;
            CollapsingHeader::new(format!("[{}]", items.len()))
                .id_salt(ui.next_auto_id())
                .show(ui, |ui| {
                    for (i, item) in items.iter_mut().enumerate() {
                        ui.push_id(i, |ui| {
                            ui.horizontal(|ui| {
                                ui.label(i.to_string());
                                changed |= edit_value(ui, item);
                            });
                        });
                    }
                });
            changed
        }
        Value::Object(fields) => {
            let mut changed = false;
            egui::Grid::new(ui.next_auto_id())
                .num_columns(2)
                .striped(true)
                .show(ui, |ui| {
                    for (key, field) in fields.iter_mut() {
                        ui.label(key);
                        ui.push_id(key, |ui| changed |= edit_value(ui, field));
                        ui.end_row();
                    }
                });
            changed
        }
    }
}

/// Keeps the number's kind: unsigned stays unsigned, integers stay integers.
fn edit_number(ui: &mut Ui, n: &mut serde_json::Number) -> bool {
    if let Some(mut v) = n.as_u64() {
        let changed = ui.add(DragValue::new(&mut v)).changed();
        *n = v.into();
        changed
    } else if let Some(mut v) = n.as_i64() {
        let changed = ui.add(DragValue::new(&mut v)).changed();
        *n = v.into();
        changed
    } else {
        let mut v = n.as_f64().unwrap_or(0.0);
        let speed = (v.abs() * 0.01).clamp(0.001, 10.0);
        let changed = ui.add(DragValue::new(&mut v).speed(speed)).changed();
        if let Some(f) = serde_json::Number::from_f64(v) {
            *n = f;
        }
        changed
    }
}

/// Each system's time this frame, slowest first.
fn systems(ui: &mut Ui, ctx: &Ctx) {
    let timings = ctx.system_timings();
    if timings.is_empty() {
        ui.label(RichText::new("no systems").weak());
        return;
    }
    let top = timings.iter().map(|t| t.avg_ms).fold(0.001, f32::max);
    egui::Grid::new("inspector-systems").num_columns(3).show(ui, |ui| {
        for t in timings {
            let name = t.name.rsplit("::").next().unwrap_or(t.name);
            ui.label(if t.sim {
                format!("{name} (sim)")
            } else {
                name.to_owned()
            })
            .on_hover_text(t.name);
            ui.label(format!("{:.3} ms", t.avg_ms));
            let (rect, _) = ui.allocate_exact_size(vec2(60.0, 8.0), Sense::hover());
            let mut bar = rect;
            bar.set_width(rect.width() * t.avg_ms / top);
            ui.painter().rect_filled(bar, 1.0, Color32::from_rgb(242, 140, 40));
            ui.end_row();
        }
    });
}

/// The overlay as part of the windowed loop: egui's context and window
/// input, around [`Panels`].
pub(crate) struct Inspector {
    pub open: bool,
    egui: egui::Context,
    input: egui_winit::State,
    pub panels: Panels,
}

impl Inspector {
    pub fn new(window: &winit::window::Window, max_texture_side: usize) -> Self {
        let egui = egui::Context::default();
        let input = egui_winit::State::new(
            egui.clone(),
            egui::ViewportId::ROOT,
            window,
            Some(window.scale_factor() as f32),
            None,
            Some(max_texture_side),
        );
        Self {
            open: false,
            egui,
            input,
            panels: Panels::default(),
        }
    }

    /// Feeds a window event to egui. Returns `true` if the game shouldn't
    /// see it: egui uses it, or it's the click that picks an entity.
    pub fn on_event(&mut self, window: &winit::window::Window, event: &winit::event::WindowEvent) -> bool {
        use winit::event::{ElementState, MouseButton, WindowEvent};
        if !self.open {
            return false;
        }
        let consumed = self.input.on_window_event(window, event).consumed;
        if self.panels.picking
            && !self.egui.is_pointer_over_egui()
            && let WindowEvent::MouseInput {
                state: ElementState::Pressed,
                button: MouseButton::Left,
                ..
            } = event
        {
            let at = self.egui.input(|i| i.pointer.latest_pos());
            self.panels.pick_at = at.map(|p| Vec2::new(p.x, p.y));
            return true;
        }
        let input = matches!(
            event,
            WindowEvent::KeyboardInput { .. } | WindowEvent::MouseInput { .. } | WindowEvent::MouseWheel { .. }
        );
        if self.panels.editing && input {
            // Paused: the editor has the mouse in the world, and its keys.
            let mouse = !matches!(event, WindowEvent::KeyboardInput { .. });
            return consumed || mouse || self.is_editor_key(event);
        }
        consumed && input
    }

    /// Whether `event` is one of the editor's keys (see `Panels::shortcuts`).
    fn is_editor_key(&self, event: &winit::event::WindowEvent) -> bool {
        use winit::keyboard::{KeyCode, PhysicalKey};
        let winit::event::WindowEvent::KeyboardInput { event: key, .. } = event else {
            return false;
        };
        let PhysicalKey::Code(code) = key.physical_key else {
            return false;
        };
        let command = self.egui.input(|i| i.modifiers.command);
        match code {
            KeyCode::Delete | KeyCode::Backspace | KeyCode::Escape => true,
            KeyCode::KeyD | KeyCode::KeyZ => command,
            _ => false,
        }
    }

    /// Runs the panels for this frame and hands their drawing to the renderer.
    pub fn frame(&mut self, window: &winit::window::Window, ctx: &mut Ctx) {
        ctx.ui.overlay_has_pointer = false;
        if !self.open {
            return;
        }
        let raw = self.input.take_egui_input(window);
        let panels = &mut self.panels;
        let output = self.egui.run_ui(raw, |ui| panels.show(ui, ctx));
        ctx.ui.overlay_has_pointer = self.egui.is_pointer_over_egui();
        self.input.handle_platform_output(window, output.platform_output);
        let primitives = self.egui.tessellate(output.shapes, output.pixels_per_point);
        if let Some(renderer) = ctx.renderer.as_mut() {
            renderer.set_overlay(Overlay {
                primitives,
                textures: output.textures_delta,
                pixels_per_point: output.pixels_per_point,
            });
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};
    use serde_json::json;

    #[derive(Serialize, Deserialize)]
    struct Health {
        current: f32,
        max: u32,
    }
    struct Secret;

    fn run(panels: &mut Panels, ctx: &mut Ctx) -> egui::FullOutput {
        let egui = egui::Context::default();
        let input = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1280.0, 720.0))),
            ..Default::default()
        };
        let mut out = egui.run_ui(input, |ui| panels.show(ui, ctx));
        out.textures_delta.clear();
        out
    }

    fn game() -> (Ctx, Entity) {
        let mut ctx = Ctx::new(1.0 / 60.0, Vec2::new(1280.0, 720.0));
        ctx.world.reflect::<Health>();
        ctx.world.reflect::<Transform2D>();
        let e = ctx.world.spawn((
            Transform2D::at(Vec2::new(10.0, 0.0)),
            Health { current: 3.0, max: 10 },
            Secret,
        ));
        for i in 0..100 {
            ctx.world.spawn((Transform2D::at(Vec2::new(i as f32 * 100.0, 500.0)),));
        }
        (ctx, e)
    }

    /// Runs one frame of `panels` on a lasting egui context (pointer state
    /// carries over between frames) with these input events.
    fn frame(egui: &egui::Context, panels: &mut Panels, ctx: &mut Ctx, mut events: Vec<egui::Event>) {
        // egui keeps held modifiers from ModifiersChanged, not from key events.
        let command = events
            .iter()
            .any(|e| matches!(e, egui::Event::Key { modifiers, .. } if modifiers.command));
        let held = if command {
            egui::Modifiers::COMMAND
        } else {
            Default::default()
        };
        events.insert(0, egui::Event::ModifiersChanged(held));
        let input = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1280.0, 720.0))),
            events,
            ..Default::default()
        };
        let mut out = egui.run_ui(input, |ui| panels.show(ui, ctx));
        out.textures_delta.clear();
    }
    fn button(x: f32, y: f32, pressed: bool) -> egui::Event {
        egui::Event::PointerButton {
            pos: egui::pos2(x, y),
            button: egui::PointerButton::Primary,
            pressed,
            modifiers: Default::default(),
        }
    }
    fn key(key: Key, command: bool) -> egui::Event {
        egui::Event::Key {
            key,
            physical_key: None,
            pressed: true,
            repeat: false,
            modifiers: if command {
                egui::Modifiers::COMMAND
            } else {
                Default::default()
            },
        }
    }
    fn edit_scene() -> (egui::Context, Panels, Ctx, Entity) {
        let (mut ctx, _) = game();
        ctx.pause();
        let e = ctx.world.spawn((
            Transform2D::at(Vec2::new(300.0, 300.0)),
            Health { current: 1.0, max: 1 },
        ));
        let egui = egui::Context::default();
        let mut panels = Panels::default();
        frame(&egui, &mut panels, &mut ctx, vec![]); // lay the panels out once
        (egui, panels, ctx, e)
    }

    #[test]
    fn paused_a_click_selects_a_drag_moves_and_undo_puts_it_back() {
        let (egui, mut panels, mut ctx, e) = edit_scene();
        let at = |ctx: &Ctx| ctx.world.get::<Transform2D>(e).unwrap().position;
        frame(
            &egui,
            &mut panels,
            &mut ctx,
            vec![
                egui::Event::PointerMoved(egui::pos2(305.0, 302.0)),
                button(305.0, 302.0, true),
            ],
        );
        assert_eq!(panels.selected, Some(e));
        frame(
            &egui,
            &mut panels,
            &mut ctx,
            vec![egui::Event::PointerMoved(egui::pos2(355.0, 322.0))],
        );
        assert_eq!(
            at(&ctx),
            Vec2::new(350.0, 320.0),
            "moved by the drag, not snapped to the cursor"
        );
        frame(&egui, &mut panels, &mut ctx, vec![button(355.0, 322.0, false)]);
        assert!(panels.editor.can_undo());
        frame(&egui, &mut panels, &mut ctx, vec![key(Key::Z, true)]);
        assert_eq!(at(&ctx), Vec2::new(300.0, 300.0));
    }

    #[test]
    fn paused_keys_duplicate_and_delete_the_selection() {
        let (egui, mut panels, mut ctx, e) = edit_scene();
        panels.selected = Some(e);
        let before = ctx.world.len();
        frame(&egui, &mut panels, &mut ctx, vec![key(Key::D, true)]);
        let copy = panels.selected.unwrap();
        assert_ne!(copy, e);
        assert_eq!(
            ctx.world.get::<Transform2D>(copy).unwrap().position,
            Vec2::new(316.0, 316.0)
        );
        frame(&egui, &mut panels, &mut ctx, vec![key(Key::Delete, false)]);
        assert!(!ctx.world.is_alive(copy) && panels.selected.is_none());
        assert_eq!(ctx.world.len(), before);
    }

    #[test]
    fn running_the_world_clicks_go_to_the_game() {
        let (egui, mut panels, mut ctx, e) = edit_scene();
        ctx.resume();
        frame(
            &egui,
            &mut panels,
            &mut ctx,
            vec![
                egui::Event::PointerMoved(egui::pos2(300.0, 300.0)),
                button(300.0, 300.0, true),
            ],
        );
        assert_eq!(panels.selected, None);
        assert!(!panels.editing);
        let _ = e;
    }

    #[test]
    fn play_then_stop_from_the_panels_restores_the_world() {
        let (egui, mut panels, mut ctx, e) = edit_scene();
        panels.editor.play(&mut ctx);
        ctx.world.despawn(e);
        frame(&egui, &mut panels, &mut ctx, vec![]);
        panels.editor.stop(&mut ctx).unwrap();
        assert!(
            ctx.world.get::<Health>(e).is_some(),
            "back, with the same id (the world was reloaded)"
        );
    }

    #[test]
    fn panels_draw_a_selected_entity_with_reflected_and_hidden_components() {
        let (mut ctx, e) = game();
        let mut panels = Panels {
            selected: Some(e),
            ..Default::default()
        };
        let out = run(&mut panels, &mut ctx);
        assert!(!out.shapes.is_empty());
        assert_eq!(panels.selected, Some(e));
        assert!(panels.error.is_none());
    }

    #[test]
    fn a_despawned_selection_is_dropped() {
        let (mut ctx, e) = game();
        let mut panels = Panels {
            selected: Some(e),
            ..Default::default()
        };
        ctx.world.despawn(e);
        let _ = run(&mut panels, &mut ctx);
        assert_eq!(panels.selected, None);
    }

    #[test]
    fn rows_cover_every_matching_entity_once() {
        let (ctx, e) = game();
        let mut rows = Vec::new();
        let total = list_rows(&ctx.world, None, &mut rows);
        assert_eq!(total, 101);
        let listed: HashSet<Entity> = (0..total).filter_map(|r| entity_at(&ctx.world, &rows, r)).collect();
        assert_eq!(listed.len(), 101);
        assert!(listed.contains(&e));
        assert_eq!(entity_at(&ctx.world, &rows, total), None);

        let health = ctx.world.component_by_name(std::any::type_name::<Health>()).unwrap();
        assert_eq!(list_rows(&ctx.world, Some(health), &mut rows), 1);
        assert_eq!(entity_at(&ctx.world, &rows, 0), Some(e));
    }

    #[test]
    fn a_pick_selects_the_nearest_entity_under_the_click() {
        let (mut ctx, e) = game();
        let at = ctx.camera.world_to_screen(Vec2::new(12.0, 3.0));
        let mut panels = Panels {
            picking: true,
            pick_at: Some(at),
            ..Default::default()
        };
        let _ = run(&mut panels, &mut ctx);
        assert_eq!(panels.selected, Some(e));
        assert!(!panels.picking, "picking ends after one click");

        let far = ctx.camera.world_to_screen(Vec2::new(55.0, -300.0));
        assert_eq!(pick(&mut ctx, far), None, "nothing within reach");
    }

    #[test]
    fn number_edits_keep_their_kind() {
        let egui = egui::Context::default();
        let mut value = json!({ "current": 2.5, "max": 7, "offset": -3, "name": "crab", "on": true });
        egui.run_ui(egui::RawInput::default(), |ui| {
            assert!(!edit_value(ui, &mut value), "nothing changes without input");
        })
        .drop_without_applying_deltas();
        assert_eq!(
            value,
            json!({ "current": 2.5, "max": 7, "offset": -3, "name": "crab", "on": true })
        );
        assert!(value["max"].is_u64() && value["offset"].is_i64() && value["current"].is_f64());
    }

    #[test]
    fn frame_history_wraps_around() {
        let mut panels = Panels::default();
        for i in 0..HISTORY + 10 {
            panels.record_frame(i as f32);
        }
        assert_eq!(panels.frame_ms.len(), HISTORY);
        assert_eq!(panels.frame_at, 10);
        assert_eq!(panels.frame_ms[9], (HISTORY + 9) as f32);
    }
}