29 lines
899 B
Rust
29 lines
899 B
Rust
|
|
use bevy::{prelude::*, window::PrimaryWindow};
|
||
|
|
|
||
|
|
pub struct PointerPlugin;
|
||
|
|
|
||
|
|
impl Plugin for PointerPlugin {
|
||
|
|
fn build(&self, app: &mut App) {
|
||
|
|
app.init_resource::<PointerTarget>()
|
||
|
|
.add_systems(Update, update_pointer_target);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// World position of the active touch or mouse cursor, if any.
|
||
|
|
#[derive(Resource, Default)]
|
||
|
|
pub struct PointerTarget(pub Option<Vec2>);
|
||
|
|
|
||
|
|
fn update_pointer_target(
|
||
|
|
window: Single<&Window, With<PrimaryWindow>>,
|
||
|
|
camera: Single<(&Camera, &GlobalTransform)>,
|
||
|
|
touches: Res<Touches>,
|
||
|
|
mut target: ResMut<PointerTarget>,
|
||
|
|
) {
|
||
|
|
let (camera, camera_transform) = *camera;
|
||
|
|
// Touch and cursor positions are both in logical window pixels
|
||
|
|
target.0 = touches
|
||
|
|
.first_pressed_position()
|
||
|
|
.or_else(|| window.cursor_position())
|
||
|
|
.and_then(|position| camera.viewport_to_world_2d(camera_transform, position).ok());
|
||
|
|
}
|