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 event for clicks #9240

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d7c1113
Add API to support publishing click events for UI nodes
Jul 22, 2023
ae0e726
Refactor game_menu example to use Click events
Jul 22, 2023
b63703f
Add LastInteraction into prelude
Jul 22, 2023
615db07
Refactor for LastInteraction In ButtonBundle
Jul 23, 2023
8db5137
Fix PreUpdate systems call order
Jul 24, 2023
bdd29aa
Revert to old name for menu_action
Jul 24, 2023
396d95f
Extend state example to use Click events
Jul 24, 2023
7169033
Remove EventReader<Click> for simpler add_systems call
Jul 24, 2023
68d8f7a
Fix name error for function calls
Jul 24, 2023
7805a07
Combine Update add systems calls
Jul 24, 2023
b62bab1
For reals combine all add_systems calls run on Update
Jul 24, 2023
47b3226
Move add order for consistency
Jul 24, 2023
bb0a31d
Refactor display & visibility example for click events
Sep 2, 2023
020baca
Merge branch 'main' into add-event-for-clicks
Sep 2, 2023
9134ca3
Fix game menu example for changed event reader iterator
Sep 2, 2023
f07e153
Fix event reader iterator call on missed example
Sep 2, 2023
0729791
Minor grammar and syntax changes
Sep 2, 2023
898d202
Refactor size constraints example for click event
Sep 2, 2023
5174150
Remove ButtonActivatedEvent, replace with Click
Sep 2, 2023
bf4655c
Fix failing doc test
Sep 2, 2023
8dd6901
Change scope of event writer in an attempt to fix doc test
Sep 3, 2023
6842ccf
Simplify send of click event
Sep 3, 2023
15abb9c
Remove LastInteraction struct
Sep 3, 2023
94c39d9
Remove unused import
Sep 3, 2023
589d9d0
Simplify tuple unpacking for click events in examples
Sep 3, 2023
86961a3
Change tuple packing back
Sep 3, 2023
95d34b5
Move the click send to incorporate hover
Sep 3, 2023
ad556b2
Amend click event comment
Sep 4, 2023
5f12182
Change Click -> Clicked
Sep 6, 2023
198542a
Refactor iteration to old method
Sep 6, 2023
9f92e48
Fix click typo
Sep 6, 2023
4c9b271
Remove old comment
Sep 7, 2023
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
22 changes: 3 additions & 19 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_ecs::{
change_detection::DetectChangesMut,
entity::Entity,
event,
prelude::{Changed, Component, Event, With},
prelude::{Component, Event, With},
query::WorldQuery,
reflect::ReflectComponent,
system::{Local, Query, Res},
Expand Down Expand Up @@ -159,6 +159,7 @@ pub fn ui_focus_system(
ui_stack: Res<UiStack>,
mut node_query: Query<NodeQuery>,
primary_window: Query<Entity, With<PrimaryWindow>>,
mut click_events: event::EventWriter<Click>,
) {
let primary_window = primary_window.iter().next();

Expand All @@ -176,6 +177,7 @@ pub fn ui_focus_system(
if let Some(mut interaction) = node.interaction {
if *interaction == Interaction::Pressed {
*interaction = Interaction::None;
click_events.send(Click(node.entity));
AnbyKatz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -317,21 +319,3 @@ pub fn ui_focus_system(
}
}
}

/// The system that sends a click event to publish which entities where clicked
/// for all UI nodes that contain the [`LastInteraction`] component
///
/// Included by default with `UiPlugin`.
pub fn ui_click(
mut click_events: event::EventWriter<Click>,
mut buttons: Query<(Entity, &Interaction, &mut LastInteraction), Changed<Interaction>>,
) {
for (entity, &interaction, mut last_interaction) in &mut buttons {
// Publish a click event for every entity that has changed in one cycle from
// `Interaction::Hovered` to `Interaction::Pressed`
if interaction == Interaction::Hovered && last_interaction.0 == Interaction::Pressed {
click_events.send(Click(entity));
}
last_interaction.0 = interaction;
}
}
6 changes: 1 addition & 5 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ impl Plugin for UiPlugin {
.add_event::<Click>()
.add_systems(
PreUpdate,
(
ui_focus_system.in_set(UiSystem::Focus).after(InputSystem),
ui_click,
)
.chain(),
ui_focus_system.in_set(UiSystem::Focus).after(InputSystem),
);
// add these systems to front because these must run before transform update systems
#[cfg(feature = "bevy_text")]
Expand Down
7 changes: 2 additions & 5 deletions crates/bevy_ui/src/node_bundles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::widget::TextFlags;
use crate::{
widget::{Button, UiImageSize},
BackgroundColor, BorderColor, ContentSize, FocusPolicy, Interaction, LastInteraction, Node,
Style, UiImage, UiTextureAtlasImage, ZIndex,
BackgroundColor, BorderColor, ContentSize, FocusPolicy, Interaction, Node, Style, UiImage,
UiTextureAtlasImage, ZIndex,
};
use bevy_asset::Handle;
use bevy_ecs::bundle::Bundle;
Expand Down Expand Up @@ -307,8 +307,6 @@ pub struct ButtonBundle {
pub view_visibility: ViewVisibility,
/// Indicates the depth at which the node should appear in the UI
pub z_index: ZIndex,
/// Holds the previous Interation
pub last_interaction: LastInteraction,
}

impl Default for ButtonBundle {
Expand All @@ -328,7 +326,6 @@ impl Default for ButtonBundle {
inherited_visibility: Default::default(),
view_visibility: Default::default(),
z_index: Default::default(),
last_interaction: Default::default(),
}
}
}