-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add event for clicks #10141
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 event for clicks #10141
Conversation
Can you say more about why this is a better pattern? What problems is this solving, and why should we add another way to do this? I think that our "UI interaction" model is very half-baked, but I'm not yet sold that this is the right fix. |
|
/// | ||
/// Note click captures the full click/press-release action. | ||
#[derive(Event)] | ||
pub struct Clicked(pub Entity); |
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.
Not sure "clicked" conveys the right meaning, as the name doesn't imply it's on release. The Web API's click event is only emitted on release just like you implemented it here, though.
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.
I think clicks in any UI always works this way. But won't hurt to mention in the docs, of course!
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.
Yeah, "click" is the right terminology here. We have "pressed" as a one-phase event, "just pressed" as a two-phase event, and "clicked" as a three-phase event.
Co-authored-by: Pascal Hertleif <[email protected]>
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.
This PR improves the existing implementation a bit, but in the long run this code needs to be completely replaced with a solution more similar to bevy_mod_picking.
I would like to clarify that this PR doesn't improve or change any behavior. It just adds an event for proper "clicks". |
I just want to link another PR with a similar goal and different strategy. #8157 which adds to the This is technically non-breaking but it is beginning a design for a very core aspect of So I think I am not in favor, but I don't feel strongly about that. |
I saw it. But I think that "released" not a state, it's a state transition from
Sure, but it requires only 6 new lines of code logic inside Bevy, this is why I decided to suggest it.
Looks like logic will be event-driven, so this should play nicely with the upcoming rework. |
Hi, I got surprised by the pressed behavior on buttons not being click. I realized thanks to @Shatur's comment that click is a transition and instead of adding /// Excerpt from this gist[1].
///
/// * * *
///
/// A trigger event that signifies a click on a button.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_asky::view::click::{self, Click};
/// fn setup(mut commands: Commands) {
/// commands.spawn(ButtonBundle::default())
/// .observe(|trigger: Trigger<Click>|
/// eprintln!("Clicked on {}", trigger.entity()));
/// }
/// ```
/// [1]: https://gist.github.com/shanecelis/06b2d1a598e1e06d0a00671596e9f74f
#[derive(Event, Debug)]
pub struct Click;
/// This system looks at [Button] [Interaction] changes. If that state changes
/// from [Interaction::Pressed] to [Interaction::Hovered] then it will trigger a
/// [Click] event targeting the Entity with the [Interaction] component.
///
/// TODO: The `Local<EntityHashMap>` should be reset or drop elements that are stale
/// so it doesn't grow unbounded.
fn button_click(
mut interaction_query: Query<(Entity, &Interaction), (Changed<Interaction>, With<Button>)>,
mut last_state: Local<EntityHashMap<Interaction>>,
mut commands: Commands,
) {
for (id, interaction) in &mut interaction_query {
let last = last_state.get(&id);
if *interaction == Interaction::Hovered && matches!(last, Some(Interaction::Pressed)) {
commands.trigger_targets(Click, id);
}
last_state.insert(id, *interaction);
}
} |
Closing in favor of integrating this functionality into the now-upstreamed |
Objective
Solution
Click
events when anInteraction
goes fromInteraction::Hovered
toInteraction::Pressed
.Click
event sends intoUiPlugin
by default.game_menu.rs
example to useClick
rather then manually checking the Interaction.Changelog
Added
Click
events when anInteraction
goes fromInteraction::Hovered
toInteraction::Pressed
.