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
//! The split: what keeps a [`crate::split`]'s two panes in proportion, and what moves its divider
//! by pointer, key and pad. Its look is [`crate::apply_skin`]'s, like every widget's.

use bevy::input::ButtonState;
use bevy::input::gamepad::{Gamepad, GamepadButton};
use bevy::input::keyboard::{KeyCode, KeyboardInput};
use bevy::input_focus::{FocusedInput, InputFocus, InputFocusVisible};
use bevy::picking::events::{
    PointerCancel, PointerClick, PointerDrag, PointerDragEnd, PointerDragStart, PointerPress,
    PointerRelease,
};
use bevy::picking::pointer::PointerButton;
use bevy::prelude::*;
use bevy::ui::{InteractionDisabled, Pressed};
use bevy::ui_widgets::ValueChange;

use crate::model::{
    PAD_REPEAT_DELAY, PAD_REPEAT_RATE, SPLIT_KEY_STEP_PX, repeat_steps, split_clamp, split_drag,
    split_mins,
};
use crate::nav::show_focus;
use crate::skin::Role;

/// How a [`crate::split`] lays its two panes out.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Reflect)]
#[reflect(Default, Debug, PartialEq, Hash, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SplitAxis {
    /// Side by side: the first pane on the left, the second on the right, a vertical divider
    /// between them that Left and Right move.
    #[default]
    Horizontal,
    /// One above the other: the first pane on top, a horizontal divider that Up and Down move.
    Vertical,
}

/// A [`crate::split`]'s root: which way it lays its panes out. Part of what `split()` spawns; it
/// requires [`SplitFraction`] and [`SplitMin`], so a game sets either in the same tuple as the
/// split and its own value wins.
#[derive(Component, Clone, Copy, Debug, PartialEq, Eq, Reflect)]
#[reflect(Component, Debug, PartialEq, Clone)]
#[require(SplitFraction, SplitMin)]
pub struct Split {
    axis: SplitAxis,
}

impl Split {
    pub(crate) fn new(axis: SplitAxis) -> Self {
        Self { axis }
    }

    /// The way the panes are laid out.
    pub fn axis(&self) -> SplitAxis {
        self.axis
    }
}

/// How much of a [`crate::split`] the first pane has, from 0 to 1: its share of the split's length
/// on its axis less the divider's. It is the widget's state: read it to know the layout, set it to
/// move the divider from code (the panes follow in the same frame, and no [`ValueChange`] is sent
/// for a change the game made itself) or save it, as plain data, with the player's other settings.
///
/// It is a preference: the player's drag and the keys keep it inside the panes' [`SplitMin`], and a
/// window too small for them lays the panes out as small as they must be and leaves this alone.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_gooey::*;
/// fn remember(splits: Query<&SplitFraction, Changed<SplitFraction>>) {
///     for fraction in &splits {
///         info!("the sidebar takes {:.0}%", fraction.0 * 100.0);
///     }
/// }
/// ```
#[derive(Component, Clone, Copy, Debug, PartialEq, Reflect)]
#[reflect(Component, Default, Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SplitFraction(pub f32);

impl Default for SplitFraction {
    fn default() -> Self {
        Self(0.5)
    }
}

/// The smallest each pane of a [`crate::split`] may be, in logical pixels: the divider never takes
/// either below it, and a window too small for both shrinks the second pane first. Zero (the
/// default) lets a pane collapse to nothing.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_gooey::*;
/// # fn ui(mut commands: Commands) {
/// commands.spawn((
///     split(SplitAxis::Horizontal, panel(), panel()),
///     SplitFraction(0.3),
///     SplitMin::new(180.0, 320.0),
/// ));
/// # }
/// ```
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Reflect)]
#[reflect(Component, Default, Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SplitMin {
    /// The first pane's least size along the axis.
    pub first: f32,
    /// The second pane's least size along the axis.
    pub second: f32,
}

impl SplitMin {
    /// Minimums of `first` and `second` logical pixels.
    pub fn new(first: f32, second: f32) -> Self {
        Self { first, second }
    }
}

/// A [`crate::split`]'s divider, the part a pointer grabs and focus rests on. Part of what
/// `split()` spawns. Give it `InteractionDisabled` to fix the layout: it still takes focus, and
/// moves nothing.
#[derive(Component, Clone, Copy, Debug, PartialEq, Eq, Reflect)]
#[reflect(Component, Debug, PartialEq, Clone)]
pub struct SplitDivider {
    pub(crate) axis: SplitAxis,
}

impl SplitDivider {
    pub(crate) fn new(axis: SplitAxis) -> Self {
        Self { axis }
    }

    /// The axis of the split this divider is in.
    pub fn axis(&self) -> SplitAxis {
        self.axis
    }
}

/// Where a divider was when a drag began; its presence says a drag is going.
#[derive(Component, Clone, Copy, Debug)]
pub(crate) struct SplitGrab(f32);

/// Where a split was before its first pane was collapsed, so the next collapse gives it back.
#[derive(Component, Clone, Copy, Debug)]
pub(crate) struct SplitSaved(f32);

/// A request to collapse a split's first pane to its minimum, or restore it, from its divider:
/// Enter, the pad's South button and a double click.
#[derive(EntityEvent, Clone, Copy, Debug)]
pub(crate) struct ToggleSplit {
    #[event_target]
    pub(crate) entity: Entity,
}

/// What a divider's handlers need to know about its split.
struct Reading {
    root: Entity,
    axis: SplitAxis,
    fraction: f32,
    min: SplitMin,
    /// The room the panes share, in logical pixels: the split's length on its axis, less the
    /// divider's.
    extent: f32,
    disabled: bool,
}

impl Reading {
    /// The fraction `delta` logical pixels from here, held to the minimums.
    fn moved(&self, delta: f32) -> f32 {
        split_drag(
            self.fraction,
            delta,
            self.extent,
            self.min.first,
            self.min.second,
        )
    }

    /// The fraction with the first pane as small, or as large, as it may be.
    fn end(&self, large: bool) -> f32 {
        let to = if large { 1.0 } else { 0.0 };
        split_clamp(to, self.extent, self.min.first, self.min.second)
    }
}

type Dividers = (
    &'static ChildOf,
    &'static SplitDivider,
    &'static ComputedNode,
    Has<InteractionDisabled>,
);

/// What a click on a divider is checked against: disabled, pressed and mid-drag.
type Pointed = (Has<InteractionDisabled>, Has<Pressed>, Has<SplitGrab>);
type Roots = (
    &'static SplitFraction,
    &'static SplitMin,
    &'static ComputedNode,
);

/// A system parameter that reads a divider's split (its fraction, minimums and room); it is in the
/// signature of [`split_by_pad`], so it is public. Nothing outside the crate has a use for it.
#[derive(bevy::ecs::system::SystemParam)]
pub struct SplitGeometry<'w, 's> {
    dividers: Query<'w, 's, Dividers>,
    roots: Query<'w, 's, Roots>,
}

impl SplitGeometry<'_, '_> {
    /// The axis of the split `divider` is in, if it is a divider that takes input: not a disabled one.
    fn enabled(&self, divider: Entity) -> Option<SplitAxis> {
        let (_, divider, _, disabled) = self.dividers.get(divider).ok()?;
        (!disabled).then_some(divider.axis)
    }

    fn read(&self, divider: Entity) -> Option<Reading> {
        let Ok((parent, divider_axis, node, disabled)) = self.dividers.get(divider) else {
            debug!(
                "split divider {divider} is not read: it lacks a `ChildOf`, `SplitDivider` or \
                 `ComputedNode` (a node that has not been laid out has no `ComputedNode`)"
            );
            return None;
        };
        let Ok((fraction, min, root_node)) = self.roots.get(parent.parent()) else {
            debug!(
                "split divider {divider} is not read: its parent {} lacks a `SplitFraction`, \
                 `SplitMin` or `ComputedNode`",
                parent.parent()
            );
            return None;
        };
        let along = |size: Vec2, axis: SplitAxis| match axis {
            SplitAxis::Horizontal => size.x,
            SplitAxis::Vertical => size.y,
        };
        let axis = divider_axis.axis;
        let whole = along(root_node.size(), axis) * root_node.inverse_scale_factor();
        let bar = along(node.size(), axis) * node.inverse_scale_factor();
        Some(Reading {
            root: parent.parent(),
            axis,
            fraction: fraction.0,
            min: *min,
            extent: (whole - bar).max(0.0),
            disabled,
        })
    }
}

/// Reports a move of a split's divider: one [`ValueChange<f32>`] on the split, unless it is no
/// change (a step into an end says nothing).
fn report(commands: &mut Commands, reading: &Reading, to: f32, is_final: bool) {
    if to != reading.fraction {
        commands.trigger(ValueChange {
            source: reading.root,
            value: to,
            is_final,
        });
    }
}

/// Keeps the state: a [`ValueChange<f32>`] on a split becomes its [`SplitFraction`]. The split's
/// own observer.
pub(crate) fn split_self_update(
    change: On<ValueChange<f32>>,
    mut splits: Query<&mut SplitFraction, With<Split>>,
) {
    if let Ok(mut fraction) = splits.get_mut(change.source) {
        fraction.set_if_neq(SplitFraction(change.value));
    }
}

/// A press on the divider is its own: it stops here, and marks it pressed.
pub(crate) fn divider_on_press(
    mut press: On<PointerPress>,
    dividers: Query<(Has<InteractionDisabled>, Has<Pressed>), With<SplitDivider>>,
    mut commands: Commands,
) {
    press.propagate(false);
    if press.button == PointerButton::Primary
        && dividers
            .get(press.entity)
            .is_ok_and(|(disabled, pressed)| !disabled && !pressed)
    {
        commands.entity(press.entity).insert(Pressed);
    }
}

/// Releasing the pointer on the divider lets it go.
pub(crate) fn divider_on_release(mut release: On<PointerRelease>, mut commands: Commands) {
    release.propagate(false);
    commands.entity(release.entity).remove::<Pressed>();
}

/// A drag begins: the divider remembers where it started.
pub(crate) fn divider_on_drag_start(
    mut start: On<PointerDragStart>,
    geometry: SplitGeometry,
    mut commands: Commands,
) {
    start.propagate(false);
    if start.button != PointerButton::Primary {
        debug!(
            "split divider {} is not dragged: the drag is by {:?}, not the primary button",
            start.entity, start.button
        );
        return;
    }
    if let Some(reading) = geometry.read(start.entity) {
        if reading.disabled {
            debug!(
                "split divider {} is not dragged: it is `InteractionDisabled`",
                start.entity
            );
            return;
        }
        commands
            .entity(start.entity)
            .insert((Pressed, SplitGrab(reading.fraction)));
    }
}

/// The divider follows the pointer: the fraction is where the drag began plus the distance along the
/// axis over the room, so `d` pixels of drag change it by `d` over the room, held to the minimums.
pub(crate) fn divider_on_drag(
    mut drag: On<PointerDrag>,
    ui_scale: Res<UiScale>,
    grabs: Query<&SplitGrab>,
    geometry: SplitGeometry,
    mut commands: Commands,
) {
    drag.propagate(false);
    let Ok(grab) = grabs.get(drag.entity) else {
        debug!(
            "split divider {} ignores a drag: no drag began on it (no `SplitGrab`)",
            drag.entity
        );
        return;
    };
    let Some(reading) = geometry.read(drag.entity) else {
        return;
    };
    let along = match reading.axis {
        SplitAxis::Horizontal => drag.distance.x,
        SplitAxis::Vertical => drag.distance.y,
    } / ui_scale.0;
    let to = split_drag(
        grab.0,
        along,
        reading.extent,
        reading.min.first,
        reading.min.second,
    );
    debug!(
        "split divider {} dragged {along} layout px (pointer distance {}) of room {}: fraction {} to {to}",
        drag.entity, drag.distance, reading.extent, reading.fraction
    );
    report(&mut commands, &reading, to, false);
}

/// The drag ends: the last value is final.
pub(crate) fn divider_on_drag_end(
    mut end: On<PointerDragEnd>,
    grabs: Query<(), With<SplitGrab>>,
    geometry: SplitGeometry,
    mut commands: Commands,
) {
    end.propagate(false);
    commands.entity(end.entity).remove::<(Pressed, SplitGrab)>();
    if grabs.contains(end.entity)
        && let Some(reading) = geometry.read(end.entity)
    {
        commands.trigger(ValueChange {
            source: reading.root,
            value: reading.fraction,
            is_final: true,
        });
    }
}

/// A cancelled pointer lets the divider go where it is.
pub(crate) fn divider_on_cancel(mut cancel: On<PointerCancel>, mut commands: Commands) {
    cancel.propagate(false);
    commands
        .entity(cancel.entity)
        .remove::<(Pressed, SplitGrab)>();
}

/// A double click collapses the first pane to its minimum, or restores it: the pointer's way to
/// size a split without a drag (WCAG 2.5.7). Only a click that ends a press counts, never one that
/// ends a drag: the pointer lets go over the divider it just moved, and Bevy calls that a click.
pub(crate) fn divider_on_click(
    mut click: On<PointerClick>,
    dividers: Query<Pointed, With<SplitDivider>>,
    mut commands: Commands,
) {
    click.propagate(false);
    if click.button == PointerButton::Primary
        && click.count == 2
        && dividers
            .get(click.entity)
            .is_ok_and(|(disabled, pressed, dragged)| !disabled && pressed && !dragged)
    {
        commands.trigger(ToggleSplit {
            entity: click.entity,
        });
    }
}

/// The arrows along the axis step a focused divider, held ones repeating; Home and End take the
/// first pane to its smallest and largest; Enter collapses it or restores it. The other arrows
/// are not handled here: they move focus on.
pub(crate) fn divider_on_key_input(
    mut input: On<FocusedInput<KeyboardInput>>,
    geometry: SplitGeometry,
    mut commands: Commands,
) {
    if input.input.state != ButtonState::Pressed {
        return;
    }
    let Some(reading) = geometry.read(input.focused_entity) else {
        return;
    };
    let step = |sign: f32| reading.moved(sign * SPLIT_KEY_STEP_PX);
    let key = input.input.key_code;
    let to = match (reading.axis, key) {
        (SplitAxis::Horizontal, KeyCode::ArrowLeft) | (SplitAxis::Vertical, KeyCode::ArrowUp) => {
            step(-1.0)
        }
        (SplitAxis::Horizontal, KeyCode::ArrowRight)
        | (SplitAxis::Vertical, KeyCode::ArrowDown) => step(1.0),
        (_, KeyCode::Home) => reading.end(false),
        (_, KeyCode::End) => reading.end(true),
        (_, KeyCode::Enter) => {
            input.propagate(false);
            if !reading.disabled && !input.input.repeat {
                commands.trigger(ToggleSplit {
                    entity: input.focused_entity,
                });
            }
            return;
        }
        _ => return,
    };
    input.propagate(false);
    if !reading.disabled {
        report(&mut commands, &reading, to, true);
    }
}

/// Collapses a split's first pane to its minimum, remembering where it was, or gives that back.
pub(crate) fn toggle_split(
    event: On<ToggleSplit>,
    geometry: SplitGeometry,
    saved: Query<&SplitSaved>,
    mut commands: Commands,
) {
    let Some(reading) = geometry.read(event.entity) else {
        return;
    };
    if reading.disabled || reading.extent <= 0.0 {
        return;
    }
    let smallest = reading.end(false);
    let at_smallest = (reading.fraction - smallest) * reading.extent < 0.5;
    let to = if at_smallest {
        let back = saved.get(reading.root).map_or(0.5, |s| s.0);
        commands.entity(reading.root).remove::<SplitSaved>();
        split_clamp(back, reading.extent, reading.min.first, reading.min.second)
    } else {
        commands
            .entity(reading.root)
            .insert(SplitSaved(reading.fraction));
        smallest
    };
    report(&mut commands, &reading, to, true);
}

/// The D-pad steps a focused divider along its axis, held to repeat like a slider's, and the pad's
/// South button collapses or restores its first pane. Shows focus. Runs in
/// [`crate::GooeySystems::Navigate`]. The keys are the divider's own observer's.
pub fn split_by_pad(
    time: Res<Time<Real>>,
    focus: Res<InputFocus>,
    mut shown: ResMut<InputFocusVisible>,
    gamepads: Query<&Gamepad>,
    geometry: SplitGeometry,
    mut held: Local<(i8, f32)>,
    mut commands: Commands,
) {
    let down = |button| gamepads.iter().any(|g| g.pressed(button));
    let Some((divider, axis)) = focus
        .get()
        .and_then(|f| geometry.enabled(f).map(|a| (f, a)))
    else {
        *held = (0, 0.0);
        return;
    };
    if gamepads
        .iter()
        .any(|g| g.just_pressed(GamepadButton::South))
    {
        show_focus(&mut shown);
        commands.trigger(ToggleSplit { entity: divider });
    }
    let (plus, minus, across) = match axis {
        SplitAxis::Horizontal => (
            GamepadButton::DPadRight,
            GamepadButton::DPadLeft,
            [GamepadButton::DPadUp, GamepadButton::DPadDown],
        ),
        SplitAxis::Vertical => (
            GamepadButton::DPadDown,
            GamepadButton::DPadUp,
            [GamepadButton::DPadLeft, GamepadButton::DPadRight],
        ),
    };
    let direction = down(plus) as i8 - down(minus) as i8;
    if direction == 0 || across.into_iter().any(down) {
        *held = (0, 0.0);
        return;
    }
    let steps = if held.0 == direction {
        let before = held.1;
        held.1 += time.delta_secs();
        repeat_steps(before, held.1, PAD_REPEAT_DELAY, PAD_REPEAT_RATE)
    } else {
        *held = (direction, 0.0);
        1
    };
    if steps == 0 {
        return;
    }
    show_focus(&mut shown);
    if let Some(reading) = geometry.read(divider) {
        let to = reading.moved(f32::from(direction) * steps as f32 * SPLIT_KEY_STEP_PX);
        report(&mut commands, &reading, to, true);
    }
}

/// Lays each split's panes out from its [`SplitFraction`] and [`SplitMin`]: the first pane grows by
/// the fraction (held to the minimums) and the second by what is left, from a basis of nothing, so
/// they share the room the divider leaves in exactly that ratio, and each pane's own minimum is
/// the one [`split_mins`] leaves for the room it has now. A window that shrinks is laid out by
/// flexbox the frame it does and the minimums follow the next. Runs in
/// [`crate::GooeySystems::Style`], before [`crate::apply_skin`]; a split that did not change costs
/// a comparison and writes nothing.
pub fn update_splits(
    splits: Query<(&Split, &SplitFraction, &SplitMin, &ComputedNode, &Children)>,
    dividers: Query<&ComputedNode, With<SplitDivider>>,
    mut parts: Query<(&Role, &mut Node)>,
) {
    for (split, fraction, min, node, children) in &splits {
        let along = |size: Vec2| match split.axis {
            SplitAxis::Horizontal => size.x,
            SplitAxis::Vertical => size.y,
        };
        let bar = children
            .iter()
            .find_map(|c| dividers.get(c).ok())
            .map_or(0.0, |d| along(d.size()) * d.inverse_scale_factor());
        let extent = (along(node.size()) * node.inverse_scale_factor() - bar).max(0.0);
        let first = split_clamp(fraction.0, extent, min.first, min.second);
        let (min_first, min_second) = split_mins(extent, min.first, min.second);
        let panes: Vec<Entity> = children
            .iter()
            .filter(|&c| parts.get(c).is_ok_and(|(role, _)| *role == Role::SplitPane))
            .collect();
        for (pane, (grow, least)) in panes
            .into_iter()
            .zip([(first, min_first), (1.0 - first, min_second)])
        {
            let Ok((_, mut node)) = parts.get_mut(pane) else {
                continue;
            };
            let least = Val::Px(least);
            let (min_width, min_height) = match split.axis {
                SplitAxis::Horizontal => (least, Val::Px(0.0)),
                SplitAxis::Vertical => (Val::Px(0.0), least),
            };
            if node.flex_grow != grow
                || node.min_width != min_width
                || node.min_height != min_height
            {
                node.flex_grow = grow;
                node.min_width = min_width;
                node.min_height = min_height;
            }
        }
    }
}