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
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
//! Drag and drop with payloads: a source carries a typed value ([`DragPayload`]), a target takes it
//! ([`DropTarget`]), and a drop is one event ([`Dropped`], [`DropRefused`], [`DroppedOnWorld`]).
//! Nothing here moves a widget: the game does, in the observer of the drop.

use bevy::ecs::lifecycle::HookContext;
use bevy::ecs::resource::IsResource;
use bevy::ecs::world::DeferredWorld;
use bevy::input_focus::{InputFocus, InputFocusVisible};
use bevy::picking::events::{PointerCancel, PointerDrag, PointerDragEnd, PointerDragStart};
use bevy::picking::hover::HoverMap;
use bevy::picking::pointer::PointerId;
use bevy::prelude::*;
use bevy::ui::{ComputedNode, InteractionDisabled, UiGlobalTransform, UiScale};

use crate::model::{Toward, target_step, target_toward};
use crate::nav::show_focus;
use crate::shortcut::{Modifiers, Shortcut};
use crate::skin::Role;

/// How far, in logical pixels, a press must move before it is a drag: a click with a shaking hand
/// is still a click.
pub const DRAG_LIFT_PX: f32 = 6.0;

/// The `GlobalZIndex` of the ghost and the landing mark: over a dialog (100) and a toast (150),
/// under a binding's catcher.
const DRAG_Z: i32 = 900;

/// How far the ghost stands from the pointer, so the pointer sees what it is over.
const GHOST_OFFSET_PX: f32 = 14.0;

/// What the carried thing is, for the ghost beside the pointer. Without one the ghost shows the first
/// text the source holds (a row's name, a button's label).
#[derive(Component, Clone, Debug, Default, PartialEq, Eq, Reflect)]
#[reflect(Component, Default, Debug, PartialEq, Clone)]
pub struct DragLabel(pub String);

/// The keys that carry a payload without a pointer, so a drag is never the only way (WCAG 2.2,
/// 2.5.7): a keyboard or gamepad player focuses a source ([`DragPayload`]), presses
/// [`DragSettings::pick_up`] (a pad's [`DragSettings::pick_up_button`]), steers the mark with the
/// arrows or the D-pad (PageUp and PageDown, LB and RB step through the targets in reading
/// order), and drops with the same key again (a pad's South too). Escape or East puts it down. A
/// game that wants other keys writes this resource; a keymap the player edits is this value.
///
/// The pick-up key is not Enter, Space or South on purpose: Bevy's `Button` activates on those, and
/// a source that is a [`button`](crate::button) would be both pressed and lifted.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_gooey::*;
/// let settings = DragSettings {
///     pick_up: Shortcut::new(Modifiers::NONE, KeyCode::F2),
///     ..default()
/// };
/// assert_eq!(settings.pick_up_button, GamepadButton::West);
/// ```
#[derive(Resource, Clone, Debug, PartialEq, Reflect)]
#[reflect(Resource, Default, Debug, PartialEq, Clone)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(default)
)]
pub struct DragSettings {
    /// The chord that lifts the focused source and, while one is carried, puts it down on the
    /// marked target. Default: Alt+M.
    pub pick_up: Shortcut,
    /// The pad button that does the same, on any gamepad. Default: West (X on an Xbox pad).
    pub pick_up_button: GamepadButton,
}

impl Default for DragSettings {
    fn default() -> Self {
        Self {
            pick_up: Shortcut::new(Modifiers::ALT, KeyCode::KeyM),
            pick_up_button: GamepadButton::West,
        }
    }
}

/// Makes a node a drag source: pressing it and moving more than [`DRAG_LIFT_PX`] picks `T` up. The
/// node needs [`Pickable`] (every gooey widget has it; a sprite or mesh needs it added).
///
/// While it is carried the source has [`Dragging`]. A drag ends in exactly one of: [`Dropped`] on
/// a target that takes `T`, [`DropRefused`] over a target that does not, [`DroppedOnWorld`] over
/// nothing the interface draws, or nothing (released over a panel that is not a target, or
/// Escape). A source that is also a [`bevy_ui_widgets::Button`](bevy::ui_widgets::Button) is
/// activated by a click as before, and by a drag that ends on itself.
///
/// Removing the `DragPayload` makes the node a source no more: pressing it lifts nothing (an
/// inventory slot that was emptied). A node carries one payload type at a time.
///
/// Dragging is a shortcut and never the only way in (WCAG 2.2, 2.5.7): offer the same move as a
/// command, a [`crate::context_menu`] item for instance.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_gooey::*;
/// #[derive(Clone)]
/// struct Piece(u32);
///
/// # fn setup(mut commands: Commands) {
/// commands.spawn((button("Wall"), DragPayload(Piece(7))));
/// commands
///     .spawn((panel(), DropTarget::<Piece>::new()))
///     .observe(|dropped: On<Dropped<Piece>>| {
///         info!("piece {} dropped at {}", dropped.payload.0, dropped.pointer);
///     });
/// # }
/// ```
#[derive(Component, Clone, Debug, PartialEq, Eq)]
#[component(on_add = payload_added::<T>, on_remove = payload_removed)]
#[require(DragSource)]
pub struct DragPayload<T: Clone + Send + Sync + 'static>(pub T);

/// Makes a node a place a `T` may be dropped. A node may carry several, one for each type it takes.
/// The type is the match: a [`DragPayload<A>`] over a node with only a `DropTarget<B>` is refused,
/// visibly, and so is one the target's own test ([`DropTarget::accepting`]) says no to.
#[derive(Component, Clone, Debug)]
#[component(on_add = target_added::<T>)]
#[require(DropZone)]
pub struct DropTarget<T: Clone + Send + Sync + 'static> {
    accepts: Option<fn(&T) -> bool>,
    /// A test that reads the two nodes (this crate's own widgets keep their state on them): the
    /// target, the source and the payload.
    judge: Option<fn(EntityRef, EntityRef, &T) -> bool>,
}

impl<T: Clone + Send + Sync + 'static> DropTarget<T> {
    /// A target that takes every `T`.
    pub const fn new() -> Self {
        Self {
            accepts: None,
            judge: None,
        }
    }

    /// A target that takes a `T` when `accepts` says so: a slot that holds one kind of item, a
    /// folder that is not the dragged one.
    pub const fn accepting(accepts: fn(&T) -> bool) -> Self {
        Self {
            accepts: Some(accepts),
            judge: None,
        }
    }

    /// A target whose test reads the target's and the source's own components.
    pub(crate) const fn judged(judge: fn(EntityRef, EntityRef, &T) -> bool) -> Self {
        Self {
            accepts: None,
            judge: Some(judge),
        }
    }
}

impl<T: Clone + Send + Sync + 'static> Default for DropTarget<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// A `T` was dropped on a [`DropTarget<T>`] that takes it. An event on the target, so
/// `commands.spawn(..).observe(|on: On<Dropped<T>>| ..)` hears the drops on that node.
#[derive(EntityEvent, Clone, Debug, PartialEq)]
pub struct Dropped<T: Clone + Send + Sync + 'static> {
    /// The target.
    #[event_target]
    pub target: Entity,
    /// The source it was carried from.
    pub source: Entity,
    /// What was carried.
    pub payload: T,
    /// Where the pointer was let go, in logical window pixels (what `Camera::viewport_to_world`
    /// takes).
    pub pointer: Vec2,
}

/// A `T` was let go over a [`DropZone`](DropTarget) that does not take it: another type, or
/// [`DropTarget::accepting`] said no. An event on the target. Nothing moved.
#[derive(EntityEvent, Clone, Debug, PartialEq)]
pub struct DropRefused<T: Clone + Send + Sync + 'static> {
    /// The target that refused.
    #[event_target]
    pub target: Entity,
    /// The source.
    pub source: Entity,
    /// What was carried.
    pub payload: T,
    /// Where the pointer was let go, in logical window pixels.
    pub pointer: Vec2,
}

/// A `T` was let go over nothing the interface draws (the game world under a HUD): a host raycasts
/// from `pointer` to see what is there. Sent as a global event.
#[derive(Event, Clone, Debug, PartialEq)]
pub struct DroppedOnWorld<T: Clone + Send + Sync + 'static> {
    /// The source.
    pub source: Entity,
    /// What was carried.
    pub payload: T,
    /// Where the pointer was let go, in logical window pixels.
    pub pointer: Vec2,
}

/// On a source exactly while it is carried (the pointer has moved past [`DRAG_LIFT_PX`] and not let
/// go): dim it, or hide it, or ignore it. The ghost and the mark are the crate's.
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
#[reflect(Component, Default, Debug, Clone)]
pub struct Dragging;

/// On every [`DropTarget`] node that takes what is being carried, exactly while a drag is lifted:
/// the places the item may land, known the moment it is picked up (WAI-ARIA: "authors SHOULD show
/// a visual indication of potential drop targets"). It is the test the drop and the keyboard
/// steering use, so a candidate is a place the carry can really land: not the source itself, not
/// a target whose [`DropTarget::accepting`] test or a slot's rule says no, not a disabled or
/// hidden one. The crate draws nothing for it (see [`DropRefusing`]); style your own targets with
/// `Has<DropCandidate>`. Put on and taken off by the crate; never add it yourself.
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
#[reflect(Component, Default, Debug, Clone)]
pub struct DropCandidate;

/// On every enabled, visible [`DropTarget`] node that refuses what is being carried, exactly while
/// a drag is lifted: the opposite of [`DropCandidate`], for a source that is not the target itself.
/// The crate dims an inventory slot that has it, so in an equipment panel the one slot that fits
/// stays bright, and when everything takes the item nothing changes on screen. Put on and taken
/// off by the crate; never add it yourself.
#[derive(Component, Clone, Copy, Debug, Default, Reflect)]
#[reflect(Component, Default, Debug, Clone)]
pub struct DropRefusing;

/// The type-erased half of a source: how to send its typed events. Put on by [`DragPayload`].
#[derive(Component, Clone, Copy, Default)]
pub(crate) struct DragSource {
    world_drop: Option<fn(&mut Commands, EntityRef, Vec2)>,
    refused: Option<fn(&mut Commands, Entity, EntityRef, Vec2)>,
}

/// One type a [`DropZone`] takes: whether a source's payload is that type and passes the test, and
/// how to deliver it.
#[derive(Clone, Copy)]
struct Handler {
    accepts: fn(EntityRef, EntityRef) -> Option<bool>,
    deliver: fn(&mut Commands, EntityRef, EntityRef, Vec2) -> bool,
}

/// The type-erased half of a target: every type it takes. Put on by [`DropTarget`].
#[derive(Component, Default, Clone)]
pub(crate) struct DropZone {
    handlers: Vec<Handler>,
}

impl DropZone {
    /// Whether the zone takes `source`'s payload: some handler's type matches and passes its test.
    fn takes(&self, zone: EntityRef, source: EntityRef) -> bool {
        self.handlers
            .iter()
            .any(|h| (h.accepts)(zone, source) == Some(true))
    }
}

fn payload_added<T: Clone + Send + Sync + 'static>(
    mut world: DeferredWorld,
    HookContext { entity, .. }: HookContext,
) {
    if let Some(mut source) = world.get_mut::<DragSource>(entity) {
        source.world_drop = Some(world_drop::<T>);
        source.refused = Some(refused::<T>);
    }
}

/// A node that loses its payload is no longer a source: pressing it lifts nothing (an emptied
/// inventory slot).
fn payload_removed(mut world: DeferredWorld, HookContext { entity, .. }: HookContext) {
    world.commands().entity(entity).try_remove::<DragSource>();
}

fn target_added<T: Clone + Send + Sync + 'static>(
    mut world: DeferredWorld,
    HookContext { entity, .. }: HookContext,
) {
    if let Some(mut zone) = world.get_mut::<DropZone>(entity) {
        zone.handlers.push(Handler {
            accepts: accepts::<T>,
            deliver: deliver::<T>,
        });
    }
}

fn accepts<T: Clone + Send + Sync + 'static>(zone: EntityRef, source: EntityRef) -> Option<bool> {
    let payload = source.get::<DragPayload<T>>()?;
    let target = zone.get::<DropTarget<T>>()?;
    Some(
        target.accepts.is_none_or(|accepts| accepts(&payload.0))
            && target
                .judge
                .is_none_or(|judge| judge(zone, source, &payload.0)),
    )
}

fn deliver<T: Clone + Send + Sync + 'static>(
    commands: &mut Commands,
    zone: EntityRef,
    source: EntityRef,
    pointer: Vec2,
) -> bool {
    if accepts::<T>(zone, source) != Some(true) {
        return false;
    }
    let Some(payload) = source.get::<DragPayload<T>>() else {
        return false;
    };
    commands.trigger(Dropped {
        target: zone.id(),
        source: source.id(),
        payload: payload.0.clone(),
        pointer,
    });
    true
}

fn refused<T: Clone + Send + Sync + 'static>(
    commands: &mut Commands,
    target: Entity,
    source: EntityRef,
    pointer: Vec2,
) {
    if let Some(payload) = source.get::<DragPayload<T>>() {
        commands.trigger(DropRefused {
            target,
            source: source.id(),
            payload: payload.0.clone(),
            pointer,
        });
    }
}

fn world_drop<T: Clone + Send + Sync + 'static>(
    commands: &mut Commands,
    source: EntityRef,
    pointer: Vec2,
) {
    if let Some(payload) = source.get::<DragPayload<T>>() {
        commands.trigger(DroppedOnWorld {
            source: source.id(),
            payload: payload.0.clone(),
            pointer,
        });
    }
}

/// The one drag in progress, if any: a resource, because one pointer carries one thing.
/// [`GooeyNavPlugin`](crate::GooeyNavPlugin) adds it; read it to know what the pointer holds.
#[derive(Resource, Clone, Debug, Default)]
pub struct DragState {
    drag: Option<Drag>,
}

/// A press on a source, from the press to the let go.
#[derive(Clone, Debug)]
struct Drag {
    source: Entity,
    pointer_id: PointerId,
    /// The pointer, in logical window pixels.
    pointer: Vec2,
    /// Whether the pointer has moved far enough for this to be a drag.
    lifted: bool,
    /// The ghost's words.
    label: String,
    /// The target under the pointer, and whether it refuses the payload.
    over: Option<Entity>,
    refused: bool,
    /// Escape or East was pressed this frame: [`track_drag`] puts it down once the frame's Back
    /// has been left alone.
    cancelled: bool,
    /// Lifted by [`carry_by_input`], not by the pointer: the target under the mark is the one
    /// the arrows chose, not the one under the pointer.
    keyboard: bool,
}

impl DragState {
    /// The source being carried, once it is lifted.
    pub fn source(&self) -> Option<Entity> {
        self.drag.as_ref().filter(|d| d.lifted).map(|d| d.source)
    }

    /// The [`DropTarget`] node under the pointer while a drag is lifted.
    pub fn over(&self) -> Option<Entity> {
        self.drag.as_ref().filter(|d| d.lifted).and_then(|d| d.over)
    }

    /// Whether the node under the pointer refuses what is carried.
    pub fn refused(&self) -> bool {
        self.drag.as_ref().is_some_and(|d| d.lifted && d.refused)
    }

    /// Whether a drag is lifted.
    pub fn is_dragging(&self) -> bool {
        self.drag.as_ref().is_some_and(|d| d.lifted)
    }

    /// Whether the carried thing was lifted from the keyboard or a pad (see [`DragSettings`]):
    /// the crate's own input systems stand still then, so the arrow that steers the mark never
    /// also moves focus.
    pub fn carried_by_key(&self) -> bool {
        self.drag.as_ref().is_some_and(|d| d.lifted && d.keyboard)
    }
}

/// A run condition: whether something is being dragged.
pub fn is_dragging(state: Option<Res<DragState>>) -> bool {
    state.is_some_and(|s| s.is_dragging())
}

/// A press on a source that moves picks nothing up yet: it is remembered, and lifted at
/// [`DRAG_LIFT_PX`]. A global observer.
pub(crate) fn drag_on_start(
    mut start: On<PointerDragStart>,
    sources: Query<(), With<DragSource>>,
    mut state: ResMut<DragState>,
) {
    if !sources.contains(start.entity) || start.button != PointerButton::Primary {
        return;
    }
    start.propagate(false);
    if state.drag.is_none() {
        state.drag = Some(Drag {
            source: start.entity,
            pointer_id: start.pointer.id,
            pointer: start.pointer.position,
            lifted: false,
            label: String::new(),
            over: None,
            refused: false,
            cancelled: false,
            keyboard: false,
        });
    }
}

/// The pointer follows; far enough, the source is lifted. A global observer.
pub(crate) fn drag_on_drag(
    mut drag: On<PointerDrag>,
    mut state: ResMut<DragState>,
    labels: Query<&DragLabel>,
    texts: Query<&Text>,
    kids: Query<&Children>,
    mut commands: Commands,
) {
    let Some(held) = state.drag.as_mut().filter(|d| d.source == drag.entity) else {
        return;
    };
    drag.propagate(false);
    held.pointer = drag.pointer.position;
    if !held.lifted && drag.distance.length() >= DRAG_LIFT_PX {
        held.lifted = true;
        held.label = labels
            .get(held.source)
            .map(|label| label.0.clone())
            .ok()
            .or_else(|| first_text(held.source, &texts, &kids))
            .unwrap_or_default();
        commands.entity(held.source).insert(Dragging);
    }
}

/// The words of the first `Text` at or under `entity`, depth first.
fn first_text(entity: Entity, texts: &Query<&Text>, kids: &Query<&Children>) -> Option<String> {
    if let Ok(text) = texts.get(entity) {
        return Some(text.0.clone());
    }
    kids.get(entity)
        .ok()?
        .iter()
        .find_map(|kid| first_text(kid, texts, kids))
}

/// The entity the pointer is over, front first.
fn front_hit(hover: &HoverMap, pointer: PointerId) -> Option<Entity> {
    hover
        .get(&pointer)?
        .iter()
        .min_by(|a, b| {
            a.1.depth
                .partial_cmp(&b.1.depth)
                .unwrap_or(std::cmp::Ordering::Equal)
        })
        .map(|(entity, _)| *entity)
}

/// The nearest [`DropZone`] at or above `entity`.
fn zone_of(entity: Entity, zones: &Query<&DropZone>, parents: &Query<&ChildOf>) -> Option<Entity> {
    std::iter::once(entity)
        .chain(parents.iter_ancestors(entity))
        .find(|&e| zones.contains(e))
}

/// Which target is under the pointer while a drag is lifted, and whether it refuses what is
/// carried, so the mark and the ghost say so before the pointer is let go. Runs in
/// [`crate::GooeySystems::Navigate`]; costs nothing while nothing is dragged.
pub(crate) fn track_drag(
    mut state: ResMut<DragState>,
    hover: Option<Res<HoverMap>>,
    zones: Query<&DropZone>,
    parents: Query<&ChildOf>,
    entities: Query<EntityRef, Without<IsResource>>,
    mut commands: Commands,
) {
    if state.drag.as_ref().is_some_and(|d| d.cancelled)
        && let Some(held) = state.drag.take()
    {
        commands.entity(held.source).remove::<Dragging>();
        return;
    }
    let Some(held) = state.drag.as_mut() else {
        return;
    };
    if held.keyboard {
        return;
    }
    if !held.lifted {
        return;
    }
    let over = hover
        .as_ref()
        .and_then(|hover| front_hit(hover, held.pointer_id))
        .and_then(|front| zone_of(front, &zones, &parents));
    let refused = over.is_some_and(|zone| {
        let (Ok(zone_ref), Ok(source), Ok(handlers)) = (
            entities.get(zone),
            entities.get(held.source),
            zones.get(zone),
        ) else {
            return true;
        };
        !handlers.takes(zone_ref, source)
    });
    if held.over != over {
        held.over = over;
    }
    if held.refused != refused {
        held.refused = refused;
    }
}

/// Let go: the drop. One of [`Dropped`], [`DropRefused`], [`DroppedOnWorld`], or nothing (over an
/// interface node that takes no drops). A global observer.
#[allow(clippy::too_many_arguments)]
pub(crate) fn drag_on_end(
    mut end: On<PointerDragEnd>,
    mut state: ResMut<DragState>,
    hover: Option<Res<HoverMap>>,
    zones: Query<&DropZone>,
    parents: Query<&ChildOf>,
    nodes: Query<(), With<Node>>,
    entities: Query<EntityRef, Without<IsResource>>,
    mut commands: Commands,
) {
    if state.drag.as_ref().is_none_or(|d| d.source != end.entity) {
        return;
    }
    end.propagate(false);
    let Some(held) = state.drag.take() else {
        return;
    };
    commands.entity(held.source).remove::<Dragging>();
    if !held.lifted {
        return;
    }
    let pointer = end.pointer.position;
    let Ok(source) = entities.get(held.source) else {
        return;
    };
    let Some(drag_source) = source.get::<DragSource>().copied() else {
        return;
    };
    let front = hover
        .as_ref()
        .and_then(|hover| front_hit(hover, held.pointer_id));
    match front {
        Some(front) => {
            if let Some(zone) = zone_of(front, &zones, &parents)
                && let (Ok(zone_ref), Ok(handlers)) = (entities.get(zone), zones.get(zone))
            {
                release_on(
                    &mut commands,
                    zone,
                    zone_ref,
                    handlers,
                    source,
                    drag_source,
                    pointer,
                );
            } else if !nodes.contains(front)
                && let Some(world_drop) = drag_source.world_drop
            {
                // Something the interface does not draw (a sprite, a mesh): the game world.
                world_drop(&mut commands, source, pointer);
            }
        }
        None => {
            if let Some(world_drop) = drag_source.world_drop {
                world_drop(&mut commands, source, pointer);
            }
        }
    }
}

/// Lets go of `source` over `zone`: one [`Dropped`] if a type the zone takes matches, otherwise
/// one [`DropRefused`].
fn release_on(
    commands: &mut Commands,
    zone: Entity,
    zone_ref: EntityRef,
    handlers: &DropZone,
    source: EntityRef,
    drag_source: DragSource,
    pointer: Vec2,
) {
    let delivered = handlers
        .handlers
        .iter()
        .any(|h| (h.deliver)(commands, zone_ref, source, pointer));
    if !delivered && let Some(refused) = drag_source.refused {
        refused(commands, zone, source, pointer);
    }
}

/// A cancelled pointer (the touch was taken over) puts the thing down. A global observer.
pub(crate) fn drag_on_pointer_cancel(
    cancel: On<PointerCancel>,
    mut state: ResMut<DragState>,
    mut commands: Commands,
) {
    if let Some(held) = state.drag.as_ref()
        && held.source == cancel.entity
    {
        commands.entity(held.source).remove::<Dragging>();
        state.drag = None;
    }
}

/// Escape or a pad's East button puts a carried thing down where it was: nothing is sent, and the
/// press is no Back ([`crate::cancel_by_input`] does not run while a drag is lifted; the drag
/// itself is put down by `track_drag` after it, in the same frame). Runs in
/// [`crate::GooeySystems::Navigate`], before `cancel_by_input`.
pub fn cancel_drags(
    keys: Res<ButtonInput<KeyCode>>,
    gamepads: Query<&Gamepad>,
    mut state: ResMut<DragState>,
) {
    if !state.is_dragging() {
        return;
    }
    if (keys.just_pressed(KeyCode::Escape)
        || gamepads.iter().any(|g| g.just_pressed(GamepadButton::East)))
        && let Some(held) = state.drag.as_mut()
    {
        held.cancelled = true;
    }
}

/// A node's rect in the interface's own logical pixels, the space `Node` positions are in.
fn rect_of(node: &ComputedNode, at: &UiGlobalTransform) -> Rect {
    let inverse = node.inverse_scale_factor();
    Rect::from_center_size(at.translation * inverse, node.size() * inverse)
}

fn rect_array(rect: Rect) -> [f32; 4] {
    [rect.min.x, rect.min.y, rect.max.x, rect.max.y]
}

/// Carries a payload without a pointer (see [`DragSettings`]): the pick-up key on a focused
/// source lifts it, the arrows or the D-pad move the mark to the nearest target that takes the
/// payload in that direction, PageUp, PageDown, LB and RB step through the targets in reading
/// order, and the pick-up key again (a pad's South too) drops it on the marked one. With nothing
/// marked a drop does nothing; Escape or East ([`cancel_drags`]) puts the payload down.
///
/// Runs in [`crate::GooeySystems::Navigate`] before [`cancel_drags`]; while a payload is carried
/// by it [`crate::not_capturing`] is false, so the key that steers never also moves focus.
#[allow(clippy::too_many_arguments)]
pub(crate) fn carry_by_input(
    keys: Res<ButtonInput<KeyCode>>,
    gamepads: Query<&Gamepad>,
    settings: Res<DragSettings>,
    focus: Res<InputFocus>,
    mut shown: ResMut<InputFocusVisible>,
    mut state: ResMut<DragState>,
    ui_scale: Option<Res<UiScale>>,
    parents: Query<&ChildOf>,
    words: (Query<&DragLabel>, Query<&Text>, Query<&Children>),
    zones: Query<
        (
            Entity,
            &DropZone,
            &ComputedNode,
            &UiGlobalTransform,
            &InheritedVisibility,
        ),
        Without<InteractionDisabled>,
    >,
    frames: Query<(&ComputedNode, &UiGlobalTransform)>,
    entities: Query<EntityRef, Without<IsResource>>,
    mut commands: Commands,
) {
    let pad = |button: GamepadButton| gamepads.iter().any(|g| g.just_pressed(button));
    let key = |code: KeyCode| keys.just_pressed(code);
    let pick = settings.pick_up.just_pressed(&keys) || pad(settings.pick_up_button);
    let scale = ui_scale.as_ref().map_or(1.0, |s| s.0).max(f32::EPSILON);

    let Some(held) = state.drag.as_ref() else {
        // Nothing carried: the pick-up key on a focused source lifts it.
        if !pick {
            return;
        }
        let (labels, texts, kids) = &words;
        let Some(source) = focus.get().and_then(|focused| {
            std::iter::once(focused)
                .chain(parents.iter_ancestors(focused))
                .find(|&e| {
                    entities.get(e).is_ok_and(|e| {
                        e.contains::<DragSource>() && !e.contains::<InteractionDisabled>()
                    })
                })
        }) else {
            return;
        };
        let label = labels
            .get(source)
            .map(|label| label.0.clone())
            .ok()
            .or_else(|| first_text(source, texts, kids))
            .unwrap_or_default();
        let pointer = frames
            .get(source)
            .map_or(Vec2::ZERO, |(node, at)| rect_of(node, at).center() * scale);
        state.drag = Some(Drag {
            source,
            pointer_id: PointerId::Mouse,
            pointer,
            lifted: true,
            label,
            over: None,
            refused: false,
            cancelled: false,
            keyboard: true,
        });
        commands.entity(source).insert(Dragging);
        show_focus(&mut shown);
        return;
    };
    if !held.keyboard || held.cancelled {
        return;
    }
    let source = held.source;
    let (Ok(source_ref), Ok((source_node, source_at))) = (entities.get(source), frames.get(source))
    else {
        // The source was despawned while it was carried.
        state.drag = None;
        return;
    };

    if pick || pad(GamepadButton::South) {
        let over = held.over;
        state.drag = None;
        commands.entity(source).remove::<Dragging>();
        let pointer = over
            .and_then(|over| frames.get(over).ok())
            .map_or(Vec2::ZERO, |(node, at)| rect_of(node, at).center() * scale);
        if let Some(zone) = over
            && let (Ok(zone_ref), Ok((_, handlers, ..))) = (entities.get(zone), zones.get(zone))
            && let Some(drag_source) = source_ref.get::<DragSource>().copied()
        {
            release_on(
                &mut commands,
                zone,
                zone_ref,
                handlers,
                source_ref,
                drag_source,
                pointer,
            );
        }
        return;
    }

    let toward = [
        (KeyCode::ArrowUp, GamepadButton::DPadUp, Toward::Up),
        (KeyCode::ArrowDown, GamepadButton::DPadDown, Toward::Down),
        (KeyCode::ArrowLeft, GamepadButton::DPadLeft, Toward::Left),
        (KeyCode::ArrowRight, GamepadButton::DPadRight, Toward::Right),
    ]
    .into_iter()
    .find(|&(k, b, _)| key(k) || pad(b))
    .map(|(_, _, toward)| toward);
    let step = if key(KeyCode::PageDown) || pad(GamepadButton::RightTrigger) {
        Some(true)
    } else if key(KeyCode::PageUp) || pad(GamepadButton::LeftTrigger) {
        Some(false)
    } else {
        None
    };
    if toward.is_none() && step.is_none() {
        return;
    }

    // Only the targets that take the payload and are on screen are offered.
    let mut candidates: Vec<(Entity, Rect)> = zones
        .iter()
        .filter(|&(entity, handlers, node, _, visible)| {
            entity != source
                && visible.get()
                && node.size().cmpgt(Vec2::ZERO).all()
                && entities
                    .get(entity)
                    .is_ok_and(|zone_ref| handlers.takes(zone_ref, source_ref))
        })
        .map(|(entity, _, node, at, _)| (entity, rect_of(node, at)))
        .collect();
    candidates.sort_by_key(|&(entity, _)| entity);
    let rects: Vec<[f32; 4]> = candidates.iter().map(|&(_, r)| rect_array(r)).collect();
    let current = state
        .drag
        .as_ref()
        .and_then(|d| d.over)
        .and_then(|over| candidates.iter().position(|&(e, _)| e == over));
    let chosen = match (toward, step) {
        (Some(toward), _) => {
            let from =
                current.map_or_else(|| rect_array(rect_of(source_node, source_at)), |i| rects[i]);
            target_toward(from, &rects, toward)
        }
        (None, Some(forward)) => target_step(&rects, current, forward),
        (None, None) => None,
    };
    if let Some(index) = chosen
        && let Some(held) = state.drag.as_mut()
    {
        let (entity, rect) = candidates[index];
        held.over = Some(entity);
        held.refused = false;
        held.pointer = rect.center() * scale;
    }
}

/// The ghost: the carried thing's words in a card beside the pointer.
#[derive(Component)]
pub(crate) struct GhostPart;

/// The mark: the frame on the target under the pointer.
#[derive(Component)]
pub(crate) struct MarkPart;

/// The ghost's parts and the mark's, as `update_drag_visuals` writes them.
type GhostParts = (
    Entity,
    &'static mut Node,
    &'static mut Role,
    &'static mut Text,
);
type MarkParts = (Entity, &'static mut Node, &'static mut Role);
type IsGhost = (With<GhostPart>, Without<MarkPart>);
type IsMark = (With<MarkPart>, Without<GhostPart>);
type Laid = (Without<GhostPart>, Without<MarkPart>);

/// Draws a drag: spawns the ghost and the mark when a source is lifted, keeps them at the pointer
/// and on the target under it, in the accept or the refuse look, and despawns them when it ends.
/// Runs in [`crate::GooeySystems::Style`], before [`crate::apply_skin`]; costs nothing while
/// nothing is dragged.
pub(crate) fn update_drag_visuals(
    state: Option<Res<DragState>>,
    ui_scale: Option<Res<UiScale>>,
    mut ghosts: Query<GhostParts, IsGhost>,
    mut marks: Query<MarkParts, IsMark>,
    frames: Query<(&ComputedNode, &UiGlobalTransform), Laid>,
    mut commands: Commands,
) {
    let held = state
        .as_ref()
        .and_then(|state| state.drag.as_ref())
        .filter(|d| d.lifted);
    let Some(held) = held else {
        for (entity, ..) in &ghosts {
            commands.entity(entity).despawn();
        }
        for (entity, ..) in &marks {
            commands.entity(entity).despawn();
        }
        return;
    };
    let scale = ui_scale.as_ref().map_or(1.0, |s| s.0).max(f32::EPSILON);
    let at = held.pointer / scale + Vec2::splat(GHOST_OFFSET_PX);
    let (ghost_role, mark_role) = if held.refused {
        (Role::DragGhostRefused, Role::DropMarkRefused)
    } else {
        (Role::DragGhost, Role::DropMark)
    };
    match ghosts.iter_mut().next() {
        Some((_, mut node, mut role, mut text)) => {
            let mut next = node.clone();
            next.left = Val::Px(at.x);
            next.top = Val::Px(at.y);
            node.set_if_neq(next);
            role.set_if_neq(ghost_role);
            text.set_if_neq(Text::new(held.label.clone()));
        }
        None => {
            commands.spawn((
                Node {
                    position_type: PositionType::Absolute,
                    left: Val::Px(at.x),
                    top: Val::Px(at.y),
                    ..default()
                },
                Text::new(held.label.clone()),
                ghost_role,
                Pickable::IGNORE,
                GlobalZIndex(DRAG_Z),
                GhostPart,
            ));
        }
    }
    let rect = held.over.and_then(|over| {
        let (node, at) = frames.get(over).ok()?;
        Some(rect_of(node, at))
    });
    match marks.iter_mut().next() {
        Some((_, mut node, mut role)) => {
            let mut next = node.clone();
            match rect {
                Some(rect) => {
                    next.display = Display::Flex;
                    next.left = Val::Px(rect.min.x);
                    next.top = Val::Px(rect.min.y);
                    next.width = Val::Px(rect.width());
                    next.height = Val::Px(rect.height());
                }
                None => next.display = Display::None,
            }
            node.set_if_neq(next);
            role.set_if_neq(mark_role);
        }
        None => {
            commands.spawn((
                Node {
                    position_type: PositionType::Absolute,
                    display: Display::None,
                    ..default()
                },
                mark_role,
                Pickable::IGNORE,
                GlobalZIndex(DRAG_Z - 1),
                MarkPart,
            ));
        }
    }
}

/// A target that carries either drop mark.
type IsMarked = Or<(With<DropCandidate>, With<DropRefusing>)>;

/// Marks every target the lifted drag may land on with [`DropCandidate`] and every one that
/// refuses it with [`DropRefusing`], pointer, keyboard or pad alike, and takes the marks off when
/// it ends. Writes only a difference; while nothing is dragged it looks at the marked nodes, of
/// which there are none. Runs in [`crate::GooeySystems::Style`] after [`update_drag_visuals`].
pub(crate) fn update_drop_candidates(
    state: Option<Res<DragState>>,
    zones: Query<(Entity, &DropZone)>,
    marked: Query<Entity, IsMarked>,
    entities: Query<EntityRef, Without<IsResource>>,
    mut commands: Commands,
) {
    let source = state
        .as_ref()
        .and_then(|state| state.source())
        .and_then(|source| entities.get(source).ok());
    let Some(source) = source else {
        for entity in &marked {
            commands
                .entity(entity)
                .remove::<(DropCandidate, DropRefusing)>();
        }
        return;
    };
    for (entity, handlers) in &zones {
        let Ok(zone) = entities.get(entity) else {
            continue;
        };
        let live = entity != source.id()
            && !zone.contains::<InteractionDisabled>()
            && zone
                .get::<InheritedVisibility>()
                .is_none_or(|visible| visible.get());
        let takes = live && handlers.takes(zone, source);
        let refuses = live && !takes;
        if takes != zone.contains::<DropCandidate>() {
            if takes {
                commands.entity(entity).insert(DropCandidate);
            } else {
                commands.entity(entity).remove::<DropCandidate>();
            }
        }
        if refuses != zone.contains::<DropRefusing>() {
            if refuses {
                commands.entity(entity).insert(DropRefusing);
            } else {
                commands.entity(entity).remove::<DropRefusing>();
            }
        }
    }
}