bevy_gooey 0.35.0

Game interfaces made easy for Bevy: a skin, ready-made game widgets (buttons, panels, menus, HUD meters, tooltips, modal dialogs) and keyboard and gamepad navigation, on Bevy's own headless widgets.
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
//! The reflect inspector ([`crate::inspector`]): rows built from a component's or a resource's
//! reflected fields, edits written back through `ReflectMut`, and rows that follow the data. Its
//! look is the widgets' own, so there is nothing for [`crate::apply_skin`] to add but a column's gap.

use std::any::TypeId;
use std::ops::RangeInclusive;

use bevy::color::Srgba;
use bevy::ecs::reflect::{AppTypeRegistry, ReflectComponent};
use bevy::prelude::*;
use bevy::reflect::attributes::CustomAttributes;
use bevy::reflect::enums::{DynamicEnum, DynamicVariant, VariantInfo};
use bevy::reflect::{GetPath, PartialReflect, Reflect, ReflectRef, TypeInfo, TypeRegistry};
use bevy::ui::Checked;
use bevy::ui_widgets::{ValueChange, observe};

use crate::choice::ChoiceValue;
use crate::color::ColorValue;
use crate::number::{NumberValue, VecChange, VecValue};
use crate::section::SectionId;
use crate::skin::Role;
use crate::text::{TextEditing, TextInputValue};
use crate::tooltip::Tooltip;
use crate::widgets::{
    choice, color_field, label, number_field, section, text_input, toggle, vec_field,
};

/// What an [`inspector`](crate::inspector) edits: one reflected component of an entity, or a
/// resource. The type must be registered for reflection (`app.register_type::<T>()`) with
/// `#[reflect(Component)]` or `#[reflect(Resource)]`.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_gooey::*;
/// #[derive(Component, Reflect, Default)]
/// #[reflect(Component)]
/// struct Engine { thrust: f32 }
///
/// #[derive(Resource, Reflect, Default)]
/// #[reflect(Resource)]
/// struct Weather { rain: bool }
///
/// # fn ui(mut commands: Commands, ship: Entity) {
/// commands.spawn(inspector(InspectTarget::component::<Engine>(ship)));
/// commands.spawn(inspector(InspectTarget::resource::<Weather>()));
/// # }
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum InspectTarget {
    /// A component of an entity.
    Component {
        /// The entity that has it.
        entity: Entity,
        /// The component's type.
        type_id: TypeId,
    },
    /// A resource.
    Resource {
        /// The resource's type.
        type_id: TypeId,
    },
}

impl InspectTarget {
    /// The component `T` of `entity`.
    pub fn component<T: Component + Reflect>(entity: Entity) -> Self {
        Self::Component {
            entity,
            type_id: TypeId::of::<T>(),
        }
    }

    /// The resource `T`.
    pub fn resource<T: Resource + Reflect>() -> Self {
        Self::Resource {
            type_id: TypeId::of::<T>(),
        }
    }

    fn type_id(self) -> TypeId {
        match self {
            Self::Component { type_id, .. } | Self::Resource { type_id } => type_id,
        }
    }
}

/// An [`inspector`](crate::inspector)'s root: what it edits. The rows are built from the target
/// the frame this is added and kept in step with it by [`sync_inspectors`]; to inspect something
/// else, despawn the inspector and spawn another.
#[derive(Component, Clone, Debug)]
pub struct Inspector {
    /// What it edits.
    pub target: InspectTarget,
    rows: Vec<Row>,
}

/// A step for a number field built from a reflected number, as a field attribute:
/// `#[reflect(@InspectStep(0.5))]`. Without it an integer steps by 1 and a float by 0.1 (a
/// hundredth of a range, when it has one).
#[derive(Reflect, Clone, Copy, Debug, PartialEq)]
pub struct InspectStep(pub f32);

/// A player's edit, sent on the [`inspector`](crate::inspector) after it was written to the
/// component or resource. `path` is Bevy's reflect path of the field from the target (`"speed"`,
/// `"nested.speed"`, `"0"`). A value the game wrote itself sends none.
#[derive(EntityEvent, Clone, Debug, PartialEq, Eq)]
pub struct InspectorChange {
    /// The inspector.
    #[event_target]
    pub entity: Entity,
    /// The field that changed.
    pub path: String,
}

/// On each row an inspector builds (not on a section): which inspector it belongs to and which
/// field it edits.
#[derive(Component, Clone, Debug, PartialEq, Eq)]
pub struct InspectRow {
    /// The inspector.
    pub inspector: Entity,
    /// The field's reflect path, as in [`InspectorChange::path`].
    pub path: String,
}

#[derive(Clone, Debug)]
struct Row {
    entity: Entity,
    path: String,
    kind: Kind,
}

#[derive(Clone, Debug, PartialEq)]
enum Kind {
    Bool,
    Number,
    Choice(Vec<String>),
    Vec(usize),
    Colour,
    Text,
    ReadOnly(String),
}

/// What the widgets are built from: a description of the target's fields, owned so it outlives the
/// borrow of the world it was read from.
enum Field {
    Bool {
        value: bool,
    },
    Number {
        value: f32,
        step: f32,
        range: RangeInclusive<f32>,
    },
    Choice {
        options: Vec<String>,
        selected: usize,
    },
    Vec {
        values: Vec<f32>,
    },
    Colour {
        value: Srgba,
    },
    Text {
        value: String,
    },
    Group {
        fields: Vec<Named>,
    },
    ReadOnly {
        text: String,
    },
}

struct Named {
    name: String,
    path: String,
    docs: Option<String>,
    field: Field,
}

/// A change a widget asks for.
enum Edit {
    Bool(bool),
    Number(f64),
    Variant(String),
    Vec(Vec<f32>),
    Colour(Srgba),
    Text(String),
}

/// A row's state to move to the field's value.
enum Update {
    Bool(bool),
    Number(f32),
    Choice(usize),
    Vec(Vec<f32>),
    Colour(Srgba),
    Text(String),
    ReadOnly(String),
}

/// A column of the fields of a reflected component or resource, each one the widget for its type:
/// a `bool` is a [`toggle`], a unit enum a [`choice`], a number a [`number_field`], a `Vec2`,
/// `Vec3` or `Vec4` a [`vec_field`], a `Srgba` or `Color` a [`color_field`], a `String` a
/// [`text_input`], a nested struct a [`section`] of its own fields; anything else is a line of
/// text that follows the value. A field's doc comment is its tooltip, `#[reflect(@RangeInclusive::<f32>::new(0.0, 10.0))]`
/// its range and `#[reflect(@InspectStep(0.5))]` its step. An edit is written to the field and
/// sent as one [`InspectorChange`](crate::InspectorChange); a value the game writes moves the row
/// with no event.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_gooey::*;
/// use std::ops::RangeInclusive;
///
/// #[derive(Component, Reflect, Default)]
/// #[reflect(Component)]
/// struct Engine {
///     /// Newtons at full burn.
///     #[reflect(@RangeInclusive::<f32>::new(0.0, 100.0))]
///     thrust: f32,
///     afterburner: bool,
/// }
///
/// # fn ui(mut commands: Commands, ship: Entity) {
/// commands.spawn((
///     inspector(InspectTarget::component::<Engine>(ship)),
///     observe(|change: On<InspectorChange>| info!("{} edited", change.path)),
/// ));
/// # }
/// ```
pub fn inspector(target: InspectTarget) -> impl Bundle {
    (
        Node {
            flex_direction: FlexDirection::Column,
            ..default()
        },
        Role::Inspector,
        Inspector {
            target,
            rows: Vec::new(),
        },
    )
}

/// The entity that holds the target: the component's own, or the resource's.
fn locate(world: &World, target: InspectTarget) -> Option<Entity> {
    match target {
        InspectTarget::Component { entity, .. } => Some(entity),
        InspectTarget::Resource { type_id } => {
            let id = world.components().get_id(type_id)?;
            world.resource_entities().get(id)
        }
    }
}

/// The reflected value of the target.
fn read<'w>(
    world: &'w World,
    registry: &TypeRegistry,
    target: InspectTarget,
) -> Option<&'w dyn Reflect> {
    let reflect = registry.get(target.type_id())?.data::<ReflectComponent>()?;
    let entity = world.get_entity(locate(world, target)?).ok()?;
    reflect.reflect(entity)
}

/// The value of the field at `path`; the path of the target itself is empty.
fn field_at<'v>(value: &'v dyn Reflect, path: &str) -> Option<&'v dyn PartialReflect> {
    if path.is_empty() {
        return Some(value.as_partial_reflect());
    }
    value.reflect_path(format!(".{path}").as_str()).ok()
}

/// "max_speed" as "Max speed".
fn humanise(name: &str) -> String {
    let mut words = name.replace('_', " ");
    if let Some(first) = words.get_mut(0..1) {
        first.make_ascii_uppercase();
    }
    words
}

/// A field's doc comment as one tooltip line.
fn docs_of(docs: Option<&str>) -> Option<String> {
    let text = docs?
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>()
        .join(" ");
    (!text.is_empty()).then_some(text)
}

fn number_of(value: &dyn PartialReflect) -> Option<f64> {
    macro_rules! numbers {
        ($($t:ty),*) => {
            $(if let Some(n) = value.try_downcast_ref::<$t>() {
                return Some(*n as f64);
            })*
        };
    }
    numbers!(f32, f64, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
    None
}

fn is_integer(value: &dyn PartialReflect) -> bool {
    macro_rules! integers {
        ($($t:ty),*) => { $(value.try_downcast_ref::<$t>().is_some())||* };
    }
    integers!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize)
}

/// Writes `new` to a number field of any numeric type (an integer rounds); whether it changed.
fn set_number(value: &mut dyn PartialReflect, new: f64) -> Option<bool> {
    macro_rules! floats {
        ($($t:ty),*) => {
            $(if let Some(n) = value.try_downcast_mut::<$t>() {
                let new = new as $t;
                let changed = *n != new;
                *n = new;
                return Some(changed);
            })*
        };
    }
    macro_rules! integers {
        ($($t:ty),*) => {
            $(if let Some(n) = value.try_downcast_mut::<$t>() {
                let new = new.round() as $t;
                let changed = *n != new;
                *n = new;
                return Some(changed);
            })*
        };
    }
    floats!(f32, f64);
    integers!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
    None
}

fn vector_of(value: &dyn PartialReflect) -> Option<Vec<f32>> {
    if let Some(v) = value.try_downcast_ref::<Vec2>() {
        return Some(v.to_array().to_vec());
    }
    if let Some(v) = value.try_downcast_ref::<Vec3>() {
        return Some(v.to_array().to_vec());
    }
    value
        .try_downcast_ref::<Vec4>()
        .map(|v| v.to_array().to_vec())
}

fn colour_of(value: &dyn PartialReflect) -> Option<Srgba> {
    if let Some(c) = value.try_downcast_ref::<Srgba>() {
        return Some(*c);
    }
    value.try_downcast_ref::<Color>().map(|c| Srgba::from(*c))
}

/// The variant names of an enum whose variants all carry nothing.
fn unit_variants(value: &dyn PartialReflect) -> Option<Vec<String>> {
    let ReflectRef::Enum(e) = value.reflect_ref() else {
        return None;
    };
    let TypeInfo::Enum(info) = e.get_represented_type_info()? else {
        return None;
    };
    info.iter()
        .all(|v| matches!(v, VariantInfo::Unit(_)))
        .then(|| info.iter().map(|v| v.name().to_string()).collect())
}

fn range_of(attributes: &CustomAttributes) -> RangeInclusive<f32> {
    if let Some(r) = attributes.get::<RangeInclusive<f32>>() {
        return *r.start()..=*r.end();
    }
    if let Some(r) = attributes.get::<RangeInclusive<f64>>() {
        return *r.start() as f32..=*r.end() as f32;
    }
    if let Some(r) = attributes.get::<RangeInclusive<i32>>() {
        return *r.start() as f32..=*r.end() as f32;
    }
    f32::NEG_INFINITY..=f32::INFINITY
}

/// Describes one field: its widget, from the value's reflected kind.
fn describe(
    value: &dyn PartialReflect,
    attributes: Option<&CustomAttributes>,
    path: &str,
) -> Field {
    if let Some(value) = value.try_downcast_ref::<bool>() {
        return Field::Bool { value: *value };
    }
    if let Some(number) = number_of(value) {
        let range = attributes.map_or(f32::NEG_INFINITY..=f32::INFINITY, range_of);
        let default_step = if is_integer(value) {
            1.0
        } else if range.start().is_finite() && range.end().is_finite() {
            ((range.end() - range.start()) / 100.0).max(f32::EPSILON)
        } else {
            0.1
        };
        let step = attributes
            .and_then(|a| a.get::<InspectStep>())
            .map_or(default_step, |s| s.0);
        return Field::Number {
            value: number as f32,
            step,
            range,
        };
    }
    if let Some(values) = vector_of(value) {
        return Field::Vec { values };
    }
    if let Some(value) = colour_of(value) {
        return Field::Colour { value };
    }
    if let Some(value) = value.try_downcast_ref::<String>() {
        return Field::Text {
            value: value.clone(),
        };
    }
    match value.reflect_ref() {
        ReflectRef::Struct(s) => {
            let info = s.get_represented_struct_info();
            let fields = (0..s.field_len())
                .filter_map(|i| {
                    let name = s.name_at(i)?;
                    let field = info.and_then(|info| info.field_at(i));
                    let path = join(path, name);
                    Some(Named {
                        name: humanise(name),
                        field: describe(
                            s.field_at(i)?,
                            field.map(|f| f.custom_attributes()),
                            &path,
                        ),
                        docs: docs_of(field.and_then(|f| f.docs())),
                        path,
                    })
                })
                .collect();
            Field::Group { fields }
        }
        ReflectRef::TupleStruct(t) => {
            let info = t.get_represented_tuple_struct_info();
            let fields = (0..t.field_len())
                .filter_map(|i| {
                    let field = info.and_then(|info| info.field_at(i));
                    let path = join(path, &i.to_string());
                    Some(Named {
                        name: i.to_string(),
                        field: describe(t.field(i)?, field.map(|f| f.custom_attributes()), &path),
                        docs: docs_of(field.and_then(|f| f.docs())),
                        path,
                    })
                })
                .collect();
            Field::Group { fields }
        }
        ReflectRef::Enum(e) => match unit_variants(value) {
            Some(options) => {
                let selected = e.variant_index();
                Field::Choice { options, selected }
            }
            None => Field::ReadOnly {
                text: format!("{value:?}"),
            },
        },
        _ => Field::ReadOnly {
            text: format!("{value:?}"),
        },
    }
}

fn join(path: &str, name: &str) -> String {
    if path.is_empty() {
        name.to_string()
    } else {
        format!("{path}.{name}")
    }
}

/// Builds the rows of every inspector that was just added. Runs in
/// [`crate::GooeySystems::Style`], before everything that draws them, so a row stands in the
/// frame its inspector does.
pub fn build_inspectors(
    added: Query<(Entity, &Inspector), Added<Inspector>>,
    mut commands: Commands,
) {
    for (root, _) in &added {
        commands.queue(move |world: &mut World| build(world, root));
    }
}

fn build(world: &mut World, root: Entity) {
    let Some(target) = world.get::<Inspector>(root).map(|i| i.target) else {
        return;
    };
    let registry = world.resource::<AppTypeRegistry>().clone();
    let described = {
        let registry = registry.read();
        read(world, &registry, target).map(|value| {
            let field = describe(value.as_partial_reflect(), None, "");
            (value.reflect_type_path().to_string(), field)
        })
    };
    let mut rows = Vec::new();
    match described {
        Some((type_path, Field::Group { fields })) => {
            for named in fields {
                spawn_named(world, root, root, &type_path, named, &mut rows);
            }
        }
        Some((type_path, field)) => {
            let named = Named {
                name: type_path.rsplit("::").next().unwrap_or("Value").to_string(),
                path: String::new(),
                docs: None,
                field,
            };
            spawn_named(world, root, root, &type_path, named, &mut rows);
        }
        None => {
            world.spawn((
                label("Nothing to inspect: the type is not registered for reflection, or it is not there."),
                ChildOf(root),
            ));
        }
    }
    if let Some(mut inspector) = world.get_mut::<Inspector>(root) {
        inspector.rows = rows;
    }
}

/// Spawns the widget of one field under `parent` and records the row.
fn spawn_named(
    world: &mut World,
    inspector: Entity,
    parent: Entity,
    type_path: &str,
    named: Named,
    rows: &mut Vec<Row>,
) {
    let Named {
        name,
        path,
        docs,
        field,
    } = named;
    let row = InspectRow {
        inspector,
        path: path.clone(),
    };
    let (entity, kind) = match field {
        Field::Bool { value } => (
            world
                .spawn((toggle(name, value), observe(on_bool), row))
                .id(),
            Kind::Bool,
        ),
        Field::Number { value, step, range } => (
            world
                .spawn((
                    number_field(name, value, step, range),
                    observe(on_number),
                    row,
                ))
                .id(),
            Kind::Number,
        ),
        Field::Choice { options, selected } => (
            world
                .spawn((
                    choice(name, options.clone(), selected),
                    observe(on_choice),
                    row,
                ))
                .id(),
            Kind::Choice(options),
        ),
        Field::Vec { values } => {
            let count = values.len();
            let unbounded = f32::NEG_INFINITY..=f32::INFINITY;
            let mut entity = match count {
                2 => world.spawn(vec_field(name, [values[0], values[1]], 0.1, unbounded)),
                3 => world.spawn(vec_field(
                    name,
                    [values[0], values[1], values[2]],
                    0.1,
                    unbounded,
                )),
                _ => world.spawn(vec_field(
                    name,
                    [values[0], values[1], values[2], values[3]],
                    0.1,
                    unbounded,
                )),
            };
            entity.insert((observe(on_vec), row));
            (entity.id(), Kind::Vec(count))
        }
        Field::Colour { value } => (
            world
                .spawn((color_field(name, value), observe(on_colour), row))
                .id(),
            Kind::Colour,
        ),
        Field::Text { value } => (
            world
                .spawn((text_input(name, value), observe(on_text), row))
                .id(),
            Kind::Text,
        ),
        Field::ReadOnly { text } => (
            world.spawn((label(format!("{name}: {text}")), row)).id(),
            Kind::ReadOnly(name),
        ),
        Field::Group { fields } => {
            let id = format!("inspect:{type_path}.{path}");
            let group = world
                .spawn((section(name, true, ()), SectionId::new(id)))
                .id();
            if let Some(docs) = docs {
                world.entity_mut(group).insert(Tooltip::new(docs));
            }
            world.entity_mut(group).insert(ChildOf(parent));
            let body = world
                .get::<Children>(group)
                .and_then(|kids| kids.get(1).copied());
            if let Some(body) = body {
                for named in fields {
                    spawn_named(world, inspector, body, type_path, named, rows);
                }
            }
            return;
        }
    };
    if let Some(docs) = docs {
        world.entity_mut(entity).insert(Tooltip::new(docs));
    }
    world.entity_mut(entity).insert(ChildOf(parent));
    rows.push(Row { entity, path, kind });
}

fn on_bool(change: On<ValueChange<bool>>, rows: Query<&InspectRow>, mut commands: Commands) {
    ask(
        &rows,
        change.source,
        Edit::Bool(change.value),
        &mut commands,
    );
}

fn on_number(change: On<ValueChange<f32>>, rows: Query<&InspectRow>, mut commands: Commands) {
    ask(
        &rows,
        change.source,
        Edit::Number(f64::from(change.value)),
        &mut commands,
    );
}

fn on_choice(
    change: On<ValueChange<usize>>,
    rows: Query<&InspectRow>,
    inspectors: Query<&Inspector>,
    mut commands: Commands,
) {
    let Ok(row) = rows.get(change.source) else {
        return;
    };
    let name = inspectors.get(row.inspector).ok().and_then(|i| {
        i.rows
            .iter()
            .find(|r| r.entity == change.source)
            .and_then(|r| match &r.kind {
                Kind::Choice(options) => options.get(change.value).cloned(),
                _ => None,
            })
    });
    if let Some(name) = name {
        ask(&rows, change.source, Edit::Variant(name), &mut commands);
    }
}

fn on_vec(change: On<VecChange>, rows: Query<&InspectRow>, mut commands: Commands) {
    ask(
        &rows,
        change.entity,
        Edit::Vec(change.values.clone()),
        &mut commands,
    );
}

fn on_colour(change: On<ValueChange<Srgba>>, rows: Query<&InspectRow>, mut commands: Commands) {
    ask(
        &rows,
        change.source,
        Edit::Colour(change.value),
        &mut commands,
    );
}

fn on_text(change: On<ValueChange<String>>, rows: Query<&InspectRow>, mut commands: Commands) {
    ask(
        &rows,
        change.source,
        Edit::Text(change.value.clone()),
        &mut commands,
    );
}

fn ask(rows: &Query<&InspectRow>, row: Entity, edit: Edit, commands: &mut Commands) {
    let Ok(row) = rows.get(row) else {
        return;
    };
    let (inspector, path) = (row.inspector, row.path.clone());
    commands.queue(move |world: &mut World| apply(world, inspector, path, edit));
}

/// Writes an edit to the field and tells the game, unless the field already had that value.
fn apply(world: &mut World, inspector: Entity, path: String, edit: Edit) {
    let Some(target) = world.get::<Inspector>(inspector).map(|i| i.target) else {
        return;
    };
    let registry = world.resource::<AppTypeRegistry>().clone();
    let Some(reflect) = registry
        .read()
        .get(target.type_id())
        .and_then(|r| r.data::<ReflectComponent>())
        .cloned()
    else {
        return;
    };
    let Some(holder) = locate(world, target) else {
        return;
    };
    let Ok(mut entity) = world.get_entity_mut(holder) else {
        return;
    };
    let Some(mut value) = reflect.reflect_mut(&mut entity) else {
        return;
    };
    let changed = {
        let root = value.bypass_change_detection();
        let field = if path.is_empty() {
            Some(root.as_partial_reflect_mut())
        } else {
            root.reflect_path_mut(format!(".{path}").as_str()).ok()
        };
        field.is_some_and(|field| write(field, edit))
    };
    if changed {
        value.set_changed();
        world.trigger(InspectorChange {
            entity: inspector,
            path,
        });
    }
}

/// Puts an edit into a field of the kind it was made for; whether the field changed.
fn write(field: &mut dyn PartialReflect, edit: Edit) -> bool {
    match edit {
        Edit::Bool(new) => field.try_downcast_mut::<bool>().is_some_and(|f| {
            let changed = *f != new;
            *f = new;
            changed
        }),
        Edit::Number(new) => set_number(field, new).unwrap_or(false),
        Edit::Text(new) => field.try_downcast_mut::<String>().is_some_and(|f| {
            let changed = *f != new;
            *f = new;
            changed
        }),
        Edit::Colour(new) => {
            if let Some(f) = field.try_downcast_mut::<Srgba>() {
                let changed = *f != new;
                *f = new;
                changed
            } else if let Some(f) = field.try_downcast_mut::<Color>() {
                let new = Color::Srgba(new);
                let changed = *f != new;
                *f = new;
                changed
            } else {
                false
            }
        }
        Edit::Vec(new) => {
            if let (Some(f), [x, y]) = (
                field.try_downcast_mut::<Vec2>(),
                new[..].try_into().unwrap_or([f32::NAN; 2]),
            ) {
                let new = Vec2::new(x, y);
                let changed = *f != new;
                *f = new;
                changed
            } else if let (Some(f), [x, y, z]) = (
                field.try_downcast_mut::<Vec3>(),
                new[..].try_into().unwrap_or([f32::NAN; 3]),
            ) {
                let new = Vec3::new(x, y, z);
                let changed = *f != new;
                *f = new;
                changed
            } else if let (Some(f), [x, y, z, w]) = (
                field.try_downcast_mut::<Vec4>(),
                new[..].try_into().unwrap_or([f32::NAN; 4]),
            ) {
                let new = Vec4::new(x, y, z, w);
                let changed = *f != new;
                *f = new;
                changed
            } else {
                false
            }
        }
        Edit::Variant(name) => {
            let same =
                matches!(field.reflect_ref(), ReflectRef::Enum(e) if e.variant_name() == name);
            if same {
                return false;
            }
            let variant = DynamicEnum::new(name, DynamicVariant::Unit);
            field.try_apply(&variant).is_ok()
        }
    }
}

/// Keeps every inspector's rows as its target says: each row's state moves to the field's value
/// when it differs, with no event, and read-only lines follow the value. A text field the player is
/// typing in is left alone. Runs in [`crate::GooeySystems::Style`] before the rows are drawn; with
/// nothing changed it writes nothing.
pub fn sync_inspectors(world: &mut World, inspectors: &mut QueryState<(Entity, &Inspector)>) {
    let Some(registry) = world.get_resource::<AppTypeRegistry>().cloned() else {
        return;
    };
    let mut updates: Vec<(Entity, Update)> = Vec::new();
    {
        let registry = registry.read();
        let world_ref: &World = world;
        for (_, inspector) in inspectors.iter(world_ref) {
            let Some(value) = read(world_ref, &registry, inspector.target) else {
                continue;
            };
            for row in &inspector.rows {
                if let Some(update) = differs(world_ref, row, value) {
                    updates.push((row.entity, update));
                }
            }
        }
    }
    for (row, update) in updates {
        let Ok(mut entity) = world.get_entity_mut(row) else {
            continue;
        };
        match update {
            Update::Bool(on) => {
                if on {
                    entity.insert(Checked);
                } else {
                    entity.remove::<Checked>();
                }
            }
            Update::Number(n) => {
                entity.insert(NumberValue(n));
            }
            Update::Choice(i) => {
                entity.insert(ChoiceValue(i));
            }
            Update::Vec(v) => {
                entity.insert(VecValue(v));
            }
            Update::Colour(c) => {
                entity.insert(ColorValue(c));
            }
            Update::Text(t) => {
                entity.insert(TextInputValue(t));
            }
            Update::ReadOnly(text) => {
                if let Some(mut t) = entity.get_mut::<Text>() {
                    t.0 = text;
                }
            }
        }
    }
}

/// What a row shows that the field no longer says, if anything.
fn differs(world: &World, row: &Row, target: &dyn Reflect) -> Option<Update> {
    let field = field_at(target, &row.path)?;
    let entity = world.get_entity(row.entity).ok()?;
    match &row.kind {
        Kind::Bool => {
            let on = *field.try_downcast_ref::<bool>()?;
            (entity.contains::<Checked>() != on).then_some(Update::Bool(on))
        }
        Kind::Number => {
            let n = number_of(field)? as f32;
            (entity.get::<NumberValue>()?.0 != n).then_some(Update::Number(n))
        }
        Kind::Choice(_) => {
            let ReflectRef::Enum(e) = field.reflect_ref() else {
                return None;
            };
            let index = e.variant_index();
            (entity.get::<ChoiceValue>()?.0 != index).then_some(Update::Choice(index))
        }
        Kind::Vec(_) => {
            let values = vector_of(field)?;
            (entity.get::<VecValue>()?.0 != values).then_some(Update::Vec(values))
        }
        Kind::Colour => {
            let c = colour_of(field)?;
            let now = entity.get::<ColorValue>()?.0;
            let far = [
                (c.red, now.red),
                (c.green, now.green),
                (c.blue, now.blue),
                (c.alpha, now.alpha),
            ]
            .iter()
            .any(|(a, b)| (a - b).abs() > 1e-4);
            far.then_some(Update::Colour(c))
        }
        Kind::Text => {
            if entity.contains::<TextEditing>() {
                return None;
            }
            let text = field.try_downcast_ref::<String>()?;
            (&entity.get::<TextInputValue>()?.0 != text).then(|| Update::Text(text.clone()))
        }
        Kind::ReadOnly(name) => {
            let text = format!("{name}: {field:?}");
            (entity.get::<Text>()?.0 != text).then_some(Update::ReadOnly(text))
        }
    }
}