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

[Merged by Bors] - Fixed bevy_ui touch input #4099

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
20 changes: 20 additions & 0 deletions crates/bevy_input/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ impl Touches {
self.pressed.get(&id)
}

/// Checks if any touch input was just pressed.
pub fn any_just_pressed(&self) -> bool {
!self.just_pressed.is_empty()
}

/// Returns `true` if the input corresponding to the `id` has just been pressed.
pub fn just_pressed(&self, id: u64) -> bool {
self.just_pressed.contains_key(&id)
Expand All @@ -239,6 +244,11 @@ impl Touches {
self.just_released.get(&id)
}

/// Checks if any touch input was just released.
pub fn any_just_released(&self) -> bool {
!self.just_released.is_empty()
}

/// Returns `true` if the input corresponding to the `id` has just been released.
pub fn just_released(&self, id: u64) -> bool {
self.just_released.contains_key(&id)
Expand All @@ -249,6 +259,11 @@ impl Touches {
self.just_released.values()
}

/// Checks if any touch input was just cancelled.
pub fn any_just_cancelled(&self) -> bool {
!self.just_cancelled.is_empty()
}

/// Returns `true` if the input corresponding to the `id` has just been cancelled.
pub fn just_cancelled(&self, id: u64) -> bool {
self.just_cancelled.contains_key(&id)
Expand All @@ -259,6 +274,11 @@ impl Touches {
self.just_cancelled.values()
}

/// Retrieves the position of the first currently pressed touch, if any
pub fn first_pressed_position(&self) -> Option<Vec2> {
self.pressed.values().next().map(|t| t.position)
}

/// Processes a [`TouchInput`] event by updating the `pressed`, `just_pressed`,
/// `just_released`, and `just_cancelled` collections.
fn process_touch_event(&mut self, event: &TouchInput) {
Expand Down
13 changes: 7 additions & 6 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ pub fn ui_focus_system(
Option<&CalculatedClip>,
)>,
) {
let cursor_position = windows
.get_primary()
.and_then(|window| window.cursor_position());

// reset entities that were both clicked and released in the last frame
for entity in state.entities_to_reset.drain(..) {
if let Ok(mut interaction) = node_query.get_component_mut::<Interaction>(entity) {
Expand All @@ -83,7 +79,7 @@ pub fn ui_focus_system(
}

let mouse_released =
mouse_button_input.just_released(MouseButton::Left) || touches_input.just_released(0);
mouse_button_input.just_released(MouseButton::Left) || touches_input.any_just_released();
if mouse_released {
for (_entity, _node, _global_transform, interaction, _focus_policy, _clip) in
node_query.iter_mut()
Expand All @@ -97,7 +93,12 @@ pub fn ui_focus_system(
}

let mouse_clicked =
mouse_button_input.just_pressed(MouseButton::Left) || touches_input.just_pressed(0);
mouse_button_input.just_pressed(MouseButton::Left) || touches_input.any_just_pressed();

let cursor_position = windows
.get_primary()
.and_then(|window| window.cursor_position())
.or_else(|| touches_input.first_pressed_position());
Comment on lines +98 to +101
Copy link
Contributor

@Weibye Weibye Jun 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fix whatever "touch-position doesn't update"-bug that #4352 is trying to fix? I'm a bit unclear what that bug actually is, and this has something related to touch position so I'd figured I'd ask.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, #4352 seems to attempt to fix internal touch position update, so I guess not?
What I'm doing is considering basically any touch position as a cursor position

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alrighty. Any improvements (if needed) can come in new PRs addressing that specifically. Approving this :)


let mut moused_over_z_sorted_nodes = node_query
.iter_mut()
Expand Down
63 changes: 62 additions & 1 deletion examples/ios/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ fn main() {
.add_startup_system(setup_scene)
.add_startup_system(setup_music)
.add_system(touch_camera)
.add_system(button_handler)
.run();
}

fn touch_camera(
windows: ResMut<Windows>,
mut touches: EventReader<TouchInput>,
mut camera: Query<&mut Transform, With<Camera>>,
mut camera: Query<&mut Transform, With<Camera3d>>,
mut last_position: Local<Option<Vec2>>,
) {
for touch in touches.iter() {
Expand Down Expand Up @@ -47,6 +48,7 @@ fn setup_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
// plane
commands.spawn_bundle(PbrBundle {
Expand Down Expand Up @@ -86,6 +88,65 @@ fn setup_scene(
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});

// Test ui
commands.spawn_bundle(Camera2dBundle::default());
commands
.spawn_bundle(ButtonBundle {
style: Style {
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(50.0),
right: Val::Px(50.0),
top: Val::Auto,
bottom: Val::Px(50.0),
},
..default()
},
..default()
})
.with_children(|b| {
b.spawn_bundle(TextBundle {
text: Text {
sections: vec![TextSection {
value: "Test Button".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::BLACK,
},
}],
alignment: TextAlignment {
vertical: VerticalAlign::Center,
horizontal: HorizontalAlign::Center,
},
},
..default()
});
});
}

fn button_handler(
mut interaction_query: Query<
(&Interaction, &mut UiColor),
(Changed<Interaction>, With<Button>),
>,
) {
for (interaction, mut color) in interaction_query.iter_mut() {
match *interaction {
Interaction::Clicked => {
*color = Color::BLUE.into();
}
Interaction::Hovered => {
*color = Color::GRAY.into();
}
Interaction::None => {
*color = Color::WHITE.into();
}
}
}
}

fn setup_music(asset_server: Res<AssetServer>, audio: Res<Audio>) {
Expand Down