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
//! Focusable components and bundles to ease navigable UI declaration.
use bevy::prelude::{Bundle, ButtonBundle, Component, NodeBundle};

use crate::{
    menu::{MenuBuilder, MenuSetting, NavMarker},
    resolve::Focusable,
};

/// A button like the default bevy [`ButtonBundle`], but with an added
/// [`Focusable`] component so that it can be used with this crate.
#[derive(Default, Clone, Bundle)]
pub struct FocusableButtonBundle {
    /// The bevy components.
    #[bundle]
    pub button_bundle: ButtonBundle,
    /// The [`Focusable`] type.
    pub focus: Focusable,
}
impl From<ButtonBundle> for FocusableButtonBundle {
    fn from(button_bundle: ButtonBundle) -> Self {
        FocusableButtonBundle {
            button_bundle,
            ..Default::default()
        }
    }
}

/// A [`NodeBundle`] delimiting a menu,
/// which [`Focusable`] will be marked with `marker`.
///
/// - See [`MenuSetting`] for details on how menus work.
/// - See [`NavMarker`] for how marking works.
#[derive(Bundle)]
pub struct MarkingMenuBundle<T: Component> {
    /// How navigation within that menu works.
    pub setting: MenuSetting,
    /// Specify from where this menu is reachable.
    pub builder: MenuBuilder,
    /// What component of type `T` to add to all [`Focusable`]s within
    /// this menu.
    pub marker: NavMarker<T>,
    /// The bevy components.
    #[bundle]
    pub node: NodeBundle,
}
/// A [`NodeBundle`] delimiting a menu.
///
/// - See [`MenuSetting`] for details on how menus work.
/// - Use [`MenuBundle::marking`] if you need [`Focusable`]s in your menu to
///   share a specific component.
#[derive(Clone, Bundle)]
pub struct MenuBundle {
    /// How navigation within that menu works.
    pub setting: MenuSetting,
    /// Specify from where this menu is reachable.
    pub builder: MenuBuilder,
    /// The bevy components.
    #[bundle]
    pub node: NodeBundle,
}
impl MenuBundle {
    /// Add `marker` to all [`Focusable`]s in this menu whenever it is created.
    ///
    /// See [`NavMarker`] for how marking works.
    pub fn marking<T: Component>(self, marker: T) -> MarkingMenuBundle<T> {
        let Self {
            setting,
            builder,
            node,
        } = self;
        MarkingMenuBundle {
            setting,
            builder,
            marker: NavMarker(marker),
            node,
        }
    }
}