mouse camera controls
This commit is contained in:
@@ -1,46 +1,59 @@
|
||||
use bevy::{input::ButtonInput, math::Vec3, prelude::*, render::camera::Camera};
|
||||
use bevy::{
|
||||
input::{mouse::MouseWheel, ButtonInput},
|
||||
prelude::*,
|
||||
render::camera::Camera,
|
||||
window::PrimaryWindow,
|
||||
};
|
||||
|
||||
use crate::TileOffset;
|
||||
|
||||
// Todo: Middle mouse button should drag the playfield around
|
||||
// Zoom with + and -
|
||||
#[allow(dead_code)]
|
||||
pub fn movement(
|
||||
time: Res<Time>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<(&mut Transform, &mut TileOffset, &mut OrthographicProjection), With<Camera>>,
|
||||
pub fn mouse_zoom(
|
||||
mut evr_scroll: EventReader<MouseWheel>,
|
||||
mut query: Query<&mut OrthographicProjection, With<Camera>>,
|
||||
) {
|
||||
for (mut transform, mut offset, mut ortho) in query.iter_mut() {
|
||||
let mut direction = Vec3::ZERO;
|
||||
use bevy::input::mouse::MouseScrollUnit;
|
||||
for ev in evr_scroll.read() {
|
||||
match ev.unit {
|
||||
MouseScrollUnit::Line => {
|
||||
let scroll = ev.y / 10.0;
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||
direction -= Vec3::new(1.0, 0.0, 0.0);
|
||||
for mut ortho in query.iter_mut() {
|
||||
ortho.scale -= scroll;
|
||||
ortho.scale = ortho.scale.clamp(0.5, 1.0);
|
||||
dbg!(ortho.scale);
|
||||
}
|
||||
}
|
||||
MouseScrollUnit::Pixel => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyD) {
|
||||
direction += Vec3::new(1.0, 0.0, 0.0);
|
||||
}
|
||||
pub fn mouse_movement(
|
||||
primary_window: Query<&Window, With<PrimaryWindow>>,
|
||||
mouse_buttons: Res<ButtonInput<MouseButton>>,
|
||||
mut query: Query<(&mut Transform, &mut TileOffset, &OrthographicProjection), With<Camera>>,
|
||||
mut last_pos: Local<Option<Vec2>>,
|
||||
) {
|
||||
if !mouse_buttons.pressed(MouseButton::Middle) {
|
||||
*last_pos = None;
|
||||
return;
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||
direction += Vec3::new(0.0, 1.0, 0.0);
|
||||
}
|
||||
let Ok(window) = primary_window.get_single() else {
|
||||
return;
|
||||
};
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyS) {
|
||||
direction -= Vec3::new(0.0, 1.0, 0.0);
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyZ) {
|
||||
ortho.scale += time.delta_seconds();
|
||||
}
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyX) {
|
||||
ortho.scale -= time.delta_seconds();
|
||||
}
|
||||
|
||||
ortho.scale = ortho.scale.clamp(0.5, 1.0);
|
||||
for (mut transform, mut offset, ortho) in query.iter_mut() {
|
||||
let current_pos = match window.cursor_position() {
|
||||
Some(c) => Vec2::new(c.x, -c.y),
|
||||
None => return,
|
||||
};
|
||||
let delta_device_pixels = current_pos - last_pos.unwrap_or(current_pos);
|
||||
*last_pos = Some(current_pos);
|
||||
|
||||
let z = transform.translation.z;
|
||||
offset.translation += time.delta_seconds() * direction * (16.0 * 8.0);
|
||||
offset.translation.x -= delta_device_pixels.x * ortho.scale;
|
||||
offset.translation.y -= delta_device_pixels.y * ortho.scale;
|
||||
|
||||
offset.x = (offset.translation.x / 16.0) as isize;
|
||||
offset.y = (offset.translation.y / 16.0) as isize;
|
||||
|
Reference in New Issue
Block a user