Skip to content

Add a pressed interaction and change clicked to fire on release #420

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

Closed
Closed
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
23 changes: 15 additions & 8 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use bevy_window::CursorMoved;
pub enum Interaction {
Clicked,
Hovered,
Pressed,
None,
}

Expand All @@ -37,6 +38,7 @@ pub struct State {
cursor_moved_event_reader: EventReader<CursorMoved>,
cursor_position: Vec2,
hovered_entity: Option<Entity>,
just_clicked: bool,
}

pub fn ui_focus_system(
Expand All @@ -55,17 +57,19 @@ pub fn ui_focus_system(
state.cursor_position = cursor_moved.position;
}

if mouse_button_input.just_released(MouseButton::Left) {
if state.just_clicked {
for (_entity, _node, _transform, interaction, _focus_policy) in &mut node_query.iter() {
if let Some(mut interaction) = interaction {
if *interaction == Interaction::Clicked {
*interaction = Interaction::None;
}
}
}
state.just_clicked = false;
}

let mouse_clicked = mouse_button_input.just_pressed(MouseButton::Left);
let mouse_pressed = mouse_button_input.just_pressed(MouseButton::Left);
let mouse_released = mouse_button_input.just_released(MouseButton::Left);
let mut hovered_entity = None;

{
Expand All @@ -85,9 +89,7 @@ pub fn ui_focus_system(
Some((entity, focus_policy, interaction, FloatOrd(position.z())))
} else {
if let Some(mut interaction) = interaction {
if *interaction == Interaction::Hovered {
*interaction = Interaction::None;
}
*interaction = Interaction::None;
}
None
}
Expand All @@ -97,10 +99,15 @@ pub fn ui_focus_system(
moused_over_z_sorted_nodes.sort_by_key(|(_, _, _, z)| -*z);
for (entity, focus_policy, interaction, _) in moused_over_z_sorted_nodes {
if let Some(mut interaction) = interaction {
if mouse_clicked {
if mouse_pressed {
// only consider nodes with ClickState "clickable"
if *interaction != Interaction::Clicked {
if *interaction != Interaction::Pressed {
*interaction = Interaction::Pressed;
}
} else if mouse_released {
if *interaction == Interaction::Pressed {
*interaction = Interaction::Clicked;
state.just_clicked = true;
}
} else if *interaction == Interaction::None {
*interaction = Interaction::Hovered;
Expand All @@ -113,7 +120,7 @@ pub fn ui_focus_system(
FocusPolicy::Block => {
break;
}
FocusPolicy::Pass => { /* allow the next node to be hovered/clicked */ }
FocusPolicy::Pass => { /* allow the next node to be hovered/pressed/clicked */ }
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions examples/ui/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct ButtonMaterials {
normal: Handle<ColorMaterial>,
hovered: Handle<ColorMaterial>,
pressed: Handle<ColorMaterial>,
clicked: Handle<ColorMaterial>,
}

impl FromResources for ButtonMaterials {
Expand All @@ -23,6 +24,7 @@ impl FromResources for ButtonMaterials {
normal: materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
hovered: materials.add(Color::rgb(0.05, 0.05, 0.05).into()),
pressed: materials.add(Color::rgb(0.1, 0.5, 0.1).into()),
clicked: materials.add(Color::rgb(1.0, 0.0, 0.0).into()),
}
}
}
Expand All @@ -41,13 +43,17 @@ fn button_system(
let mut text = text_query.get_mut::<Text>(children[0]).unwrap();
match *interaction {
Interaction::Clicked => {
text.value = "Press".to_string();
*material = button_materials.pressed;
text.value = "Click".to_string();
*material = button_materials.clicked;
}
Interaction::Hovered => {
text.value = "Hover".to_string();
*material = button_materials.hovered;
}
Interaction::Pressed => {
text.value = "Press".to_string();
*material = button_materials.pressed;
}
Interaction::None => {
text.value = "Button".to_string();
*material = button_materials.normal;
Expand Down