|
| 1 | +//! Demonstrates the use of [`Callback`]s, which run once when triggered. |
| 2 | +//! |
| 3 | +//! These can be useful to help structure your logic in a push-based fashion, |
| 4 | +//! reducing the overhead of running extremely rarely run systems |
| 5 | +//! and improving schedule flexibility. Their advantage over [`SystemId`]s is |
| 6 | +//! that they are [`Asset`]s. |
| 7 | +//! |
| 8 | +//! See the [`RunCallbackWorld::run_callback`](bevy::prelude::RunCallbackWorld::run_callback) |
| 9 | +//! docs for more details. |
| 10 | +
|
| 11 | +use bevy::prelude::*; |
| 12 | + |
| 13 | +fn main() { |
| 14 | + App::new() |
| 15 | + .add_plugins(DefaultPlugins) |
| 16 | + // `init_asset` must be called for new types of callbacks. |
| 17 | + // By default, only `Callback` (no input or output) is inited. |
| 18 | + .init_asset::<Callback<PressedDown>>() |
| 19 | + .add_systems(Startup, setup) |
| 20 | + .add_systems(Update, evaluate_callbacks) |
| 21 | + .run(); |
| 22 | +} |
| 23 | + |
| 24 | +// Need to store the `Handle` to the callback |
| 25 | +#[derive(Component)] |
| 26 | +struct OnPressedDown(Handle<Callback<PressedDown>>); |
| 27 | + |
| 28 | +// Input and output of `Callback`s must implement `TypePath` |
| 29 | +#[derive(TypePath)] |
| 30 | +struct PressedDown(Entity); |
| 31 | + |
| 32 | +// Need mutible access to `Assets` to add a callback |
| 33 | +fn setup(mut commands: Commands, mut callbacks: ResMut<Assets<Callback<PressedDown>>>) { |
| 34 | + // Camera |
| 35 | + commands.spawn(Camera2dBundle::default()); |
| 36 | + |
| 37 | + // root node |
| 38 | + commands |
| 39 | + .spawn(NodeBundle { |
| 40 | + style: Style { |
| 41 | + width: Val::Percent(100.0), |
| 42 | + height: Val::Percent(100.0), |
| 43 | + justify_content: JustifyContent::Center, |
| 44 | + align_items: AlignItems::Center, |
| 45 | + ..default() |
| 46 | + }, |
| 47 | + ..default() |
| 48 | + }) |
| 49 | + .with_children(|parent| { |
| 50 | + // button |
| 51 | + parent |
| 52 | + .spawn(( |
| 53 | + ButtonBundle { |
| 54 | + style: Style { |
| 55 | + width: Val::Px(250.0), |
| 56 | + height: Val::Px(65.0), |
| 57 | + margin: UiRect::all(Val::Px(20.0)), |
| 58 | + justify_content: JustifyContent::Center, |
| 59 | + align_items: AlignItems::Center, |
| 60 | + ..default() |
| 61 | + }, |
| 62 | + background_color: Color::TEAL.into(), |
| 63 | + ..default() |
| 64 | + }, |
| 65 | + // Add callback |
| 66 | + OnPressedDown(callbacks.add(Callback::from_system(toggle))), |
| 67 | + )) |
| 68 | + .with_children(|parent| { |
| 69 | + // text |
| 70 | + parent.spawn(TextBundle::from_section( |
| 71 | + "false", |
| 72 | + TextStyle { |
| 73 | + font_size: 40.0, |
| 74 | + color: Color::BLACK, |
| 75 | + ..default() |
| 76 | + }, |
| 77 | + )); |
| 78 | + }); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +// `Callback`s can be created from any system. |
| 83 | +fn toggle( |
| 84 | + In(pressed_down): In<PressedDown>, |
| 85 | + mut text: Query<&mut Text>, |
| 86 | + children: Query<&Children>, |
| 87 | + mut value: Local<bool>, |
| 88 | +) { |
| 89 | + *value = !*value; |
| 90 | + let children = children.get(pressed_down.0).unwrap(); |
| 91 | + let mut text = text.iter_many_mut(children); |
| 92 | + let mut text = text.fetch_next().unwrap(); |
| 93 | + text.sections[0].value = value.to_string(); |
| 94 | +} |
| 95 | + |
| 96 | +/// Runs the systems associated with each `OnPressedDown` component if button is pressed. |
| 97 | +/// |
| 98 | +/// This could be done in an exclusive system rather than using `Commands` if preferred. |
| 99 | +fn evaluate_callbacks( |
| 100 | + on_pressed_down: Query<(Entity, &OnPressedDown, &Interaction), Changed<Interaction>>, |
| 101 | + mut commands: Commands, |
| 102 | +) { |
| 103 | + for (entity, on_button_pressed, interaction) in &on_pressed_down { |
| 104 | + if *interaction == Interaction::Pressed { |
| 105 | + commands.run_callback_with_input(on_button_pressed.0.clone(), PressedDown(entity)); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments