Skip to content

Commit

Permalink
feat: basic touch input
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Jul 13, 2024
1 parent aa67edd commit 7013490
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ release = [ # Only in release (build with --release --no-default-features --feat
"bevy_embedded_assets",
"common",
"tts",
"touch",
]
common = ["input", "menu", "ui", "persist"]
common = ["input", "menu", "persist", "ui"]

3d_camera = []
input = ["leafwing-input-manager"]
Expand All @@ -35,6 +36,7 @@ navigation = ["bevy-alt-ui-navigation-lite"]
persist = ["bevy-persistent"]
pixel_perfect = []
resizable = []
touch = ["input"]
trace = ["release", "bevy/trace_tracy"]
ui = ["sickle_ui"]

Expand Down
2 changes: 1 addition & 1 deletion examples/jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn update_player(
};

// Move
let axis = input.axis_pair(&Action::Move);
let axis = input.clamped_axis_pair(&Action::Move);
let dir = axis.unwrap_or_default().x();
if dir.abs() > 0. {
player.velocity.x = dir * MOVE_VEL;
Expand Down
55 changes: 55 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ impl Plugin for InputPlugin {
Update,
handle_input.run_if(in_state(crate::GameState::Play)),
);

#[cfg(feature = "touch")]
app.add_systems(
PreUpdate,
touch_system
.chain()
.after(bevy::input::InputSystem)
.before(leafwing_input_manager::plugin::InputManagerSystem::Update),
);
}
}

Expand Down Expand Up @@ -52,11 +61,16 @@ fn init(mut cmd: Commands) {
input_map
.insert(Action::Jump, KeyCode::Space)
.insert(Action::Jump, GamepadButtonType::South)
.insert(Action::Jump, MouseButton::Left)
.insert(Action::Move, KeyboardVirtualDPad::WASD)
.insert(Action::Move, GamepadStick::LEFT)
.insert(Action::Pause, KeyCode::Escape)
.insert(Action::Pause, GamepadButtonType::Start);

// If touch input is enabled, make the touch movement map to the move action
#[cfg(feature = "touch")]
input_map.insert(Action::Move, MouseMove::default());

cmd.spawn(InputManagerBundle::with_map(input_map));
}

Expand All @@ -74,3 +88,44 @@ fn handle_input(
next_state.set(crate::GameState::Menu)
}
}

/// Touch inputs are converted to equivalent mouse values to make them
/// compatible with leafwing
#[cfg(feature = "touch")]
fn touch_system(
window: Query<Entity, With<bevy::window::PrimaryWindow>>,
touches: Res<Touches>,
mut mouse_button_writer: EventWriter<bevy::input::mouse::MouseButtonInput>,
mut cursor_moved_writer: EventWriter<CursorMoved>,
) {
use bevy::input::{mouse::MouseButtonInput, ButtonState};

let Ok(window) = window.get_single() else { return };

for _ in touches.iter_just_pressed() {
mouse_button_writer.send(MouseButtonInput {
button: MouseButton::Left,
state: ButtonState::Pressed,
window,
});
}

for _ in touches.iter_just_released() {
mouse_button_writer.send(MouseButtonInput {
button: MouseButton::Left,
state: ButtonState::Released,
window,
});
}

// Doesn't support multitouch
let Some(touch) = touches.iter().next() else {
return;
};

cursor_moved_writer.send(CursorMoved {
position: touch.position(),
delta: Some(touch.delta()),
window,
});
}

0 comments on commit 7013490

Please sign in to comment.