-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Added multiple callback logic to event_listener #22
Open
av1roytman
wants to merge
8
commits into
aevyrie:main
Choose a base branch
from
av1roytman:MultipleCallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c0b76fa
Added multiple callback logic to event_listener.rs
av1roytman 22777ad
Updated changes to event_listener.rs based on PR feedback
XavierJCallait e711947
start of fixing ListenerId
av1roytman b8e35dd
Fixed rest of the functions and also changed take method to remove al…
av1roytman f304efb
Fixed Take method
av1roytman c69495e
Removed unused import
av1roytman 93d7bad
Updated and added comments
XavierJCallait 957ded7
Played with API
XavierJCallait File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use bevy::{input::{keyboard::KeyboardInput, mouse::{MouseButton, MouseButtonInput}}, prelude::*}; | ||
use bevy_eventlistener::prelude::*; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_plugins(EventListenerPlugin::<MyEvent<1>>::default()) | ||
.add_systems(Startup, setup) | ||
.add_systems(Update, keyboard_events) | ||
.insert_resource(TargetEntity(Entity::PLACEHOLDER)) | ||
.run(); | ||
} | ||
|
||
#[derive(Clone, Event, EntityEvent)] | ||
#[can_bubble] | ||
struct MyEvent<const N: usize> { | ||
target: Entity, | ||
} | ||
|
||
#[derive(Resource)] | ||
struct TargetEntity(Entity); | ||
|
||
fn setup(mut commands: Commands, mut target_entity: ResMut<TargetEntity>, my_query: Query<On>) { | ||
|
||
// create empty listener | ||
let listener = commands.spawn((Name::new("Foo"), On::<MyEvent<1>>::default())); | ||
|
||
// Option 2 is to create a listener with callbacks ahead of time, and insert that | ||
// Create an empty listener | ||
let mut bar_listener = On::<MyEvent<1>>::default(); | ||
// Add callbacks to it via `Commands`, and return the id of the callback you just added | ||
let callback_1 = commands.add_callback(bar_listener, || info!("hello from a callback 1")); | ||
// Repeat as many times as you'd like | ||
let callback_2 = commands.add_callback(bar_listener, || info!("hello from a callback 2")); | ||
// Add the listener, which now has two callbacks, to a new entity: | ||
commands.spawn((Name::new("Bar"), bar_listener)); | ||
|
||
// Option 3 is to add to an existing listener which you got from a query or similar | ||
// Create an empty listener | ||
let mut baz_listener = my_query.single_mut(); | ||
// Same API as above: we have mutable access to the listener, so we can add callbacks to it | ||
let callback_3 = commands.add_callback(baz_listener, || info!("hello from a callback 3")); | ||
|
||
// Note that commands.add_callback() could be provided in a few ways: | ||
// same as above: Commands::add_callback(&mut self, listener: &mut On<E>) -> ListenerId | ||
// on the listener: On::<E>::add_callback(&mut self, &mut World) -> ListenerId | ||
|
||
// Because `add_callback` returns the id, you can then use it to remove or replace callbacks | ||
let callback_id = commands.add_callback(listener, || info!("hello world")); | ||
commands.remove_callback(listener, callback_id); // also despawns the callback entity | ||
commands.replace_callback(listener, callback_id, || info!("a new callback")); | ||
|
||
} | ||
|
||
fn some_simple_system(time: Res<Time>) { | ||
info!("Hello from a bevy system! {:?} has elapsed", time.elapsed()); | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
fn keyboard_events( | ||
target: Res<TargetEntity>, | ||
mut inputs: EventReader<KeyboardInput>, | ||
mut ev1: EventWriter<MyEvent<1>>, | ||
) { | ||
let target = target.0; | ||
for input in inputs | ||
.read() | ||
.filter(|input| !input.state.is_pressed()) | ||
.map(|input| input.key_code) | ||
{ev1.send(MyEvent { | ||
target | ||
});} | ||
} | ||
|
||
/* | ||
fn setup(mut commands: Commands, mut listener: ResMut<Click>, ) { | ||
// In the simplest case, you can add a listener with no callbacks in a bundle | ||
commands.spawn((Name::new("Foo"), On::<Click>::default())); | ||
|
||
// Option 2 is to create a listener with callbacks ahead of time, and insert that | ||
// Create an empty listener | ||
let mut bar_listener = On::<Click>::default(); | ||
// Add callbacks to it via `Commands`, and return the id of the callback you just added | ||
let callback_1 = commands.add_callback(bar_listener, || info!("hello from a callback 1")); | ||
// Repeat as many times as you'd like | ||
let callback_2 = commands.add_callback(bar_listener, || info!("hello from a callback 2")); | ||
// Add the listener, which now has two callbacks, to a new entity: | ||
commands.spawn((Name::new("Bar"), bar_listener)); | ||
|
||
// Option 3 is to add to an existing listener which you got from a query or similar | ||
// Create an empty listener | ||
let mut baz_listener = my_query.single_mut(); | ||
// Same API as above: we have mutable access to the listener, so we can add callbacks to it | ||
let callback_3 = commands.add_callback(baz_listener, || info!("hello from a callback 3")); | ||
|
||
// Note that commands.add_callback() could be provided in a few ways: | ||
// same as above: Commands::add_callback(&mut self, listener: &mut On<E>) -> ListenerId | ||
// on the listener: On::<E>::add_callback(&mut self, &mut World) -> ListenerId | ||
|
||
// Because `add_callback` returns the id, you can then use it to remove or replace callbacks | ||
let callback_id = commands.add_callback(listener, || info!("hello world")); | ||
commands.remove_callback(listener, callback_id); // also despawns the callback entity | ||
commands.replace_callback(listener, callback_id, || info!("a new callback")); | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ListenerHandle
no longer exists :)