Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OnPress callbacks #229

Merged
merged 10 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions src/screens/credits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,11 @@ use crate::{
pub(super) fn plugin(app: &mut App) {
app.add_systems(OnEnter(Screen::Credits), show_credits_screen);
app.add_systems(OnExit(Screen::Credits), disable_soundtrack);

app.add_systems(
Update,
handle_credits_action.run_if(in_state(Screen::Credits)),
);
app.register_type::<CreditsAction>();
}

#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Reflect)]
#[reflect(Component)]
enum CreditsAction {
Back,
}

fn show_credits_screen(mut commands: Commands) {
let enter_title = commands.register_one_shot_system(enter_title);

commands
.ui_root()
.insert(StateScoped(Screen::Credits))
Expand All @@ -39,7 +29,7 @@ fn show_credits_screen(mut commands: Commands) {
children.label("Ducky sprite - CC0 by Caz Creates Games");
children.label("Music - CC BY 3.0 by Kevin MacLeod");

children.button("Back").insert(CreditsAction::Back);
children.button("Back").insert(OnPress(enter_title));
});

commands.trigger(PlaySoundtrack::Key(SoundtrackKey::Credits));
Expand All @@ -49,15 +39,6 @@ fn disable_soundtrack(mut commands: Commands) {
commands.trigger(PlaySoundtrack::Disable);
}

fn handle_credits_action(
mut next_screen: ResMut<NextState<Screen>>,
mut button_query: Query<(&Interaction, &CreditsAction), Changed<Interaction>>,
) {
for (interaction, action) in &mut button_query {
if matches!(interaction, Interaction::Pressed) {
match action {
CreditsAction::Back => next_screen.set(Screen::Title),
}
}
}
fn enter_title(mut next_screen: ResMut<NextState<Screen>>) {
next_screen.set(Screen::Title);
}
51 changes: 18 additions & 33 deletions src/screens/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,35 @@ use crate::ui::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.add_systems(OnEnter(Screen::Title), show_title_screen);

app.register_type::<TitleAction>();
app.add_systems(Update, handle_title_action.run_if(in_state(Screen::Title)));
}

#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Reflect)]
#[reflect(Component)]
enum TitleAction {
Play,
Credits,
/// Exit doesn't work well with embedded applications.
fn show_title_screen(mut commands: Commands) {
let enter_playing = commands.register_one_shot_system(enter_playing);
let enter_credits = commands.register_one_shot_system(enter_credits);
#[cfg(not(target_family = "wasm"))]
Exit,
}
let exit_app = commands.register_one_shot_system(exit_app);

fn show_title_screen(mut commands: Commands) {
commands
.ui_root()
.insert(StateScoped(Screen::Title))
.with_children(|children| {
children.button("Play").insert(TitleAction::Play);
children.button("Credits").insert(TitleAction::Credits);
children.button("Play").insert(OnPress(enter_playing));
children.button("Credits").insert(OnPress(enter_credits));

#[cfg(not(target_family = "wasm"))]
children.button("Exit").insert(TitleAction::Exit);
children.button("Exit").insert(OnPress(exit_app));
});
}

fn handle_title_action(
mut next_screen: ResMut<NextState<Screen>>,
mut button_query: Query<(&Interaction, &TitleAction), Changed<Interaction>>,
#[cfg(not(target_family = "wasm"))] mut app_exit: EventWriter<AppExit>,
) {
for (interaction, action) in &mut button_query {
if matches!(interaction, Interaction::Pressed) {
match action {
TitleAction::Play => next_screen.set(Screen::Playing),
TitleAction::Credits => next_screen.set(Screen::Credits),

#[cfg(not(target_family = "wasm"))]
TitleAction::Exit => {
app_exit.send(AppExit::Success);
}
}
}
}
fn enter_playing(mut next_screen: ResMut<NextState<Screen>>) {
next_screen.set(Screen::Playing);
}

fn enter_credits(mut next_screen: ResMut<NextState<Screen>>) {
next_screen.set(Screen::Credits);
}

#[cfg(not(target_family = "wasm"))]
fn exit_app(mut app_exit: EventWriter<AppExit>) {
app_exit.send(AppExit::Success);
}
34 changes: 30 additions & 4 deletions src/ui/interaction.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use bevy::prelude::*;
use bevy::{ecs::system::SystemId, prelude::*};

use crate::game::{assets::SfxKey, audio::sfx::PlaySfx};

pub(super) fn plugin(app: &mut App) {
app.register_type::<InteractionPalette>();
app.add_systems(Update, (apply_interaction_palette, trigger_interaction_sfx));
app.register_type::<OnPress>();
app.add_systems(
Update,
(
apply_on_press,
apply_interaction_palette,
trigger_interaction_sfx,
),
);
}

/// Palette for widget interactions. Add this to an entity that supports [`Interaction`]s, such as a button,
Expand All @@ -17,6 +25,24 @@ pub struct InteractionPalette {
pub pressed: Color,
}

/// Component that calls a [one-shot system](https://bevyengine.org/news/bevy-0-12/#one-shot-systems)
/// when the [`Interaction`] component on the same entity changes to [`Interaction::Pressed`].
/// Use this in conjuction with [`Commands::register_one_shot_system`] to create a callback for e.g. a button press.
#[derive(Component, Debug, Reflect, Deref, DerefMut)]
#[reflect(Component, from_reflect = false)]
pub struct OnPress(#[reflect(ignore)] pub SystemId);

benfrankel marked this conversation as resolved.
Show resolved Hide resolved
fn apply_on_press(
interactions: Query<(&Interaction, &OnPress), Changed<Interaction>>,
mut commands: Commands,
) {
for (interaction, &OnPress(system_id)) in &interactions {
if matches!(interaction, Interaction::Pressed) {
commands.run_system(system_id);
}
}
}

fn apply_interaction_palette(
mut palette_query: Query<
(&Interaction, &InteractionPalette, &mut BackgroundColor),
Expand All @@ -34,10 +60,10 @@ fn apply_interaction_palette(
}

fn trigger_interaction_sfx(
mut interactions: Query<&Interaction, Changed<Interaction>>,
interactions: Query<&Interaction, Changed<Interaction>>,
mut commands: Commands,
) {
for interaction in &mut interactions {
for interaction in &interactions {
match interaction {
Interaction::Hovered => commands.trigger(PlaySfx::Key(SfxKey::ButtonHover)),
Interaction::Pressed => commands.trigger(PlaySfx::Key(SfxKey::ButtonPress)),
Expand Down
2 changes: 1 addition & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod widgets;

pub mod prelude {
pub use super::{
interaction::InteractionPalette,
interaction::{InteractionPalette, OnPress},
palette as ui_palette,
widgets::{Containers as _, Widgets as _},
};
Expand Down