mirror of
https://github.com/maplibre/maplibre-rs.git
synced 2025-12-08 19:05:57 +00:00
Refactor the mouse input handling
This commit is contained in:
parent
748076dc46
commit
bc5b12159d
@ -1,19 +1,11 @@
|
||||
use std::f32::consts::FRAC_PI_2;
|
||||
use cgmath::{EuclideanSpace, Point3, Vector2, Vector3, Zero};
|
||||
|
||||
use crate::render::camera;
|
||||
use cgmath::{EuclideanSpace, InnerSpace, Matrix4, SquareMatrix, Vector2, Vector3, Vector4};
|
||||
use log::info;
|
||||
|
||||
const SAFE_FRAC_PI_2: f32 = FRAC_PI_2 - 0.0001;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CameraController {
|
||||
translate_x: f64,
|
||||
translate_y: f64,
|
||||
direct_translate_x: f64,
|
||||
direct_translate_y: f64,
|
||||
|
||||
zoom: f64,
|
||||
camera_position: Option<Vector3<f64>>,
|
||||
camera_translate: Vector3<f64>,
|
||||
|
||||
speed: f64,
|
||||
sensitivity: f64,
|
||||
@ -22,11 +14,8 @@ pub struct CameraController {
|
||||
impl CameraController {
|
||||
pub fn new(speed: f64, sensitivity: f64) -> Self {
|
||||
Self {
|
||||
translate_x: 0.0,
|
||||
translate_y: 0.0,
|
||||
direct_translate_x: 0.0,
|
||||
direct_translate_y: 0.0,
|
||||
zoom: 0.0,
|
||||
camera_position: None,
|
||||
camera_translate: Vector3::zero(),
|
||||
speed,
|
||||
sensitivity,
|
||||
}
|
||||
@ -44,23 +33,19 @@ impl CameraController {
|
||||
};
|
||||
match key {
|
||||
winit::event::VirtualKeyCode::W | winit::event::VirtualKeyCode::Up => {
|
||||
self.translate_y += amount;
|
||||
self.camera_translate.y += amount;
|
||||
true
|
||||
}
|
||||
winit::event::VirtualKeyCode::S | winit::event::VirtualKeyCode::Down => {
|
||||
self.translate_y -= amount;
|
||||
self.camera_translate.y -= amount;
|
||||
true
|
||||
}
|
||||
winit::event::VirtualKeyCode::A | winit::event::VirtualKeyCode::Left => {
|
||||
self.translate_x -= amount;
|
||||
self.camera_translate.x -= amount;
|
||||
true
|
||||
}
|
||||
winit::event::VirtualKeyCode::D | winit::event::VirtualKeyCode::Right => {
|
||||
self.translate_x += amount;
|
||||
true
|
||||
}
|
||||
winit::event::VirtualKeyCode::Space => {
|
||||
self.translate_y = amount;
|
||||
self.camera_translate.x += amount;
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
@ -69,71 +54,22 @@ impl CameraController {
|
||||
|
||||
pub fn process_mouse(
|
||||
&mut self,
|
||||
start_cam_x: f64,
|
||||
start_cam_y: f64,
|
||||
mouse_dx: f64,
|
||||
mouse_dy: f64,
|
||||
width: f64,
|
||||
height: f64,
|
||||
camera: &mut camera::Camera,
|
||||
view_proj: &Matrix4<f64>,
|
||||
initial_camera_position: cgmath::Point3<f64>,
|
||||
delta: Vector2<f64>,
|
||||
camera: &camera::Camera,
|
||||
perspective: &camera::Perspective,
|
||||
) {
|
||||
info!("mouse_dx {} mouse_dy {}", mouse_dx, mouse_dy);
|
||||
let view_proj = camera.calc_view_proj(perspective);
|
||||
let world = camera.project_screen_to_world(&Vector2::new(0.0, 0.0), &view_proj)
|
||||
- camera.project_screen_to_world(&delta, &view_proj);
|
||||
|
||||
let origin = Vector2::new(0.0, 0.0);
|
||||
let screen = Vector2::new(mouse_dx, mouse_dy);
|
||||
let camera_pos = &camera.position.to_vec();
|
||||
let world = Self::screen_to_world(&origin, width, height, camera_pos, &view_proj)
|
||||
- Self::screen_to_world(&screen, width, height, camera_pos, &view_proj);
|
||||
|
||||
info!("world {:?}", world);
|
||||
|
||||
//self.direct_translate_x -= world.x;
|
||||
//self.direct_translate_y += world.y;
|
||||
camera.position.x = start_cam_x - world.x;
|
||||
camera.position.y = start_cam_y + world.y;
|
||||
}
|
||||
|
||||
fn screen_to_world(
|
||||
screen: &Vector2<f64>,
|
||||
width: f64,
|
||||
height: f64,
|
||||
camera_pos: &Vector3<f64>,
|
||||
view_proj: &Matrix4<f64>,
|
||||
) -> Vector4<f64> {
|
||||
let min_depth = 0.0;
|
||||
let max_depth = 1.0;
|
||||
|
||||
let x = 0.0;
|
||||
let y = 0.0;
|
||||
let ox = x + width / 2.0;
|
||||
let oy = y + height / 2.0;
|
||||
let oz = min_depth;
|
||||
let pz = max_depth - min_depth;
|
||||
|
||||
// Adapted from: https://docs.microsoft.com/en-us/windows/win32/direct3d9/viewports-and-clipping#viewport-rectangle
|
||||
let direct_x = Matrix4::from_cols(
|
||||
Vector4::new(width as f64 / 2.0, 0.0, 0.0, 0.0),
|
||||
Vector4::new(0.0, height as f64 / 2.0, 0.0, 0.0),
|
||||
Vector4::new(0.0, 0.0, pz, 0.0),
|
||||
Vector4::new(ox, oy, oz, 1.0),
|
||||
);
|
||||
|
||||
let screen_hom = Vector4::new(screen.x, screen.y, 1.0, 1.0) * camera_pos.z;
|
||||
let result = direct_x.invert().unwrap() * screen_hom;
|
||||
let world_pos = view_proj.invert().unwrap() * result;
|
||||
world_pos
|
||||
}
|
||||
|
||||
pub fn process_touch(&mut self, touch_dx: f64, touch_dy: f64) {
|
||||
self.translate_x += touch_dx as f64 * self.sensitivity;
|
||||
self.translate_y += touch_dy as f64 * self.sensitivity;
|
||||
self.camera_position =
|
||||
Some(initial_camera_position.to_vec() + Vector3::new(-world.x, world.y, 0.0))
|
||||
}
|
||||
|
||||
pub fn process_scroll(&mut self, delta: &winit::event::MouseScrollDelta) {
|
||||
self.zoom = -match delta {
|
||||
// I'm assuming a line is about 100 pixels
|
||||
winit::event::MouseScrollDelta::LineDelta(_, scroll) => *scroll as f64 * 100.0,
|
||||
self.camera_translate.z -= match delta {
|
||||
winit::event::MouseScrollDelta::LineDelta(_, scroll) => *scroll as f64 * 50.0,
|
||||
winit::event::MouseScrollDelta::PixelDelta(winit::dpi::PhysicalPosition {
|
||||
y: scroll,
|
||||
..
|
||||
@ -146,27 +82,20 @@ impl CameraController {
|
||||
camera: &mut crate::render::camera::Camera,
|
||||
dt: std::time::Duration,
|
||||
) {
|
||||
camera.position.x += self.direct_translate_x;
|
||||
camera.position.y += self.direct_translate_y;
|
||||
self.direct_translate_x = 0.0;
|
||||
self.direct_translate_y = 0.0;
|
||||
|
||||
let dt = dt.as_secs_f64() * self.speed;
|
||||
|
||||
let dy = self.translate_y * dt;
|
||||
camera.position.y += dy;
|
||||
let dx = self.translate_x * dt;
|
||||
camera.position.x += dx;
|
||||
if let Some(position) = self.camera_position {
|
||||
camera.position = Point3::from_vec(position);
|
||||
self.camera_position = None;
|
||||
}
|
||||
|
||||
// Move in/out (aka. "zoom")
|
||||
// Note: this isn't an actual zoom. The camera's position
|
||||
// changes when zooming. I've added this to make it easier
|
||||
// to get closer to an object you want to focus on.
|
||||
let dz = self.zoom * dt;
|
||||
camera.position.z += dz;
|
||||
let delta = self.camera_translate * dt;
|
||||
camera.position += delta;
|
||||
|
||||
self.zoom -= dz;
|
||||
self.translate_x -= dx;
|
||||
self.translate_y -= dy;
|
||||
self.camera_translate -= delta;
|
||||
}
|
||||
}
|
||||
|
||||
127
src/input/mod.rs
127
src/input/mod.rs
@ -1,12 +1,13 @@
|
||||
use cgmath::Vector3;
|
||||
//! Handles the user input which is dispatched by the main event loop.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use cgmath::Vector2;
|
||||
use winit::event::{
|
||||
DeviceEvent, ElementState, KeyboardInput, MouseButton, TouchPhase, WindowEvent,
|
||||
};
|
||||
use winit::window::Window;
|
||||
|
||||
use crate::input::camera_controller::CameraController;
|
||||
use crate::render::camera::Camera;
|
||||
use crate::render::render_state::RenderState;
|
||||
|
||||
mod camera_controller;
|
||||
@ -14,8 +15,8 @@ mod camera_controller;
|
||||
pub struct InputHandler {
|
||||
camera_controller: CameraController,
|
||||
|
||||
last_touch: Option<(f64, f64)>,
|
||||
start_camera_pos: Option<cgmath::Point3<f64>>,
|
||||
last_mouse_position: Option<Vector2<f64>>,
|
||||
initial_camera_position: Option<cgmath::Point3<f64>>,
|
||||
mouse_pressed: bool,
|
||||
target_stroke_width: f32,
|
||||
}
|
||||
@ -25,65 +26,42 @@ impl InputHandler {
|
||||
let camera_controller = CameraController::new(5.0, 100.0);
|
||||
Self {
|
||||
target_stroke_width: 1.0,
|
||||
start_camera_pos: None,
|
||||
last_touch: None,
|
||||
initial_camera_position: None,
|
||||
last_mouse_position: None,
|
||||
mouse_pressed: false,
|
||||
camera_controller,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn device_input(
|
||||
&mut self,
|
||||
event: &DeviceEvent,
|
||||
state: &mut RenderState,
|
||||
window: &Window,
|
||||
) -> bool {
|
||||
pub fn device_input(&mut self, event: &DeviceEvent) -> bool {
|
||||
match event {
|
||||
DeviceEvent::MouseMotion { delta } => {
|
||||
/* if self.mouse_pressed {
|
||||
let view_proj = state.camera.calc_view_proj(&state.perspective);
|
||||
self.camera_controller.process_mouse(
|
||||
delta.0 / window.scale_factor(),
|
||||
delta.1 / window.scale_factor(),
|
||||
state.size.width as f64,
|
||||
state.size.height as f64,
|
||||
&mut state.camera,
|
||||
&view_proj,
|
||||
);
|
||||
}*/
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_input(
|
||||
&mut self,
|
||||
event: &WindowEvent,
|
||||
state: &mut RenderState,
|
||||
window: &Window,
|
||||
) -> bool {
|
||||
fn process_mouse_delta(&mut self, position: Vector2<f64>, state: &mut RenderState) {
|
||||
if let (Some(last_mouse_position), Some(initial_camera_position)) =
|
||||
(self.last_mouse_position, self.initial_camera_position)
|
||||
{
|
||||
let delta = last_mouse_position - position;
|
||||
self.camera_controller.process_mouse(
|
||||
initial_camera_position,
|
||||
delta,
|
||||
&state.camera,
|
||||
&state.perspective,
|
||||
);
|
||||
} else {
|
||||
self.last_mouse_position = Some(position);
|
||||
self.initial_camera_position = Some(state.camera.position);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_input(&mut self, event: &WindowEvent, state: &mut RenderState) -> bool {
|
||||
match event {
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
if self.mouse_pressed {
|
||||
if let Some(start) = self.last_touch {
|
||||
let delta_x = start.0 - position.x;
|
||||
let delta_y = start.1 - position.y;
|
||||
let view_proj = state.camera.calc_view_proj(&state.perspective);
|
||||
self.camera_controller.process_mouse(
|
||||
self.start_camera_pos.unwrap().x,
|
||||
self.start_camera_pos.unwrap().y,
|
||||
delta_x,
|
||||
delta_y,
|
||||
state.size.width as f64,
|
||||
state.size.height as f64,
|
||||
&mut state.camera,
|
||||
&view_proj,
|
||||
);
|
||||
} else {
|
||||
self.last_touch = Some((position.x, position.y));
|
||||
self.start_camera_pos = Some(state.camera.position);
|
||||
}
|
||||
let mouse_position: (f64, f64) = position.to_owned().into();
|
||||
self.process_mouse_delta(Vector2::from(mouse_position), state);
|
||||
}
|
||||
true
|
||||
}
|
||||
@ -107,31 +85,14 @@ impl InputHandler {
|
||||
_ => self.camera_controller.process_keyboard(*key, *state),
|
||||
},
|
||||
WindowEvent::Touch(touch) => {
|
||||
let touch_position: (f64, f64) = touch.location.to_owned().into();
|
||||
match touch.phase {
|
||||
TouchPhase::Started => {
|
||||
self.last_touch = Some((touch.location.x, touch.location.y));
|
||||
self.start_camera_pos = Some(state.camera.position);
|
||||
self.last_mouse_position = Some(Vector2::from(touch_position));
|
||||
self.initial_camera_position = Some(state.camera.position);
|
||||
}
|
||||
TouchPhase::Ended => {
|
||||
self.last_touch = None;
|
||||
self.start_camera_pos = None;
|
||||
}
|
||||
TouchPhase::Moved => {
|
||||
if let Some(start) = self.last_touch {
|
||||
let delta_x = start.0 - touch.location.x;
|
||||
let delta_y = start.1 - touch.location.y;
|
||||
let view_proj = state.camera.calc_view_proj(&state.perspective);
|
||||
self.camera_controller.process_mouse(
|
||||
self.start_camera_pos.unwrap().x,
|
||||
self.start_camera_pos.unwrap().y,
|
||||
delta_x,
|
||||
delta_y,
|
||||
state.size.width as f64,
|
||||
state.size.height as f64,
|
||||
&mut state.camera,
|
||||
&view_proj,
|
||||
);
|
||||
}
|
||||
TouchPhase::Moved | TouchPhase::Ended => {
|
||||
self.process_mouse_delta(Vector2::from(touch_position), state);
|
||||
}
|
||||
TouchPhase::Cancelled => {}
|
||||
}
|
||||
@ -150,8 +111,8 @@ impl InputHandler {
|
||||
self.mouse_pressed = *state == ElementState::Pressed;
|
||||
|
||||
if !self.mouse_pressed {
|
||||
self.last_touch = None;
|
||||
self.start_camera_pos = None;
|
||||
self.last_mouse_position = None;
|
||||
self.initial_camera_position = None;
|
||||
}
|
||||
true
|
||||
}
|
||||
@ -160,22 +121,6 @@ impl InputHandler {
|
||||
}
|
||||
|
||||
pub fn update_state(&mut self, state: &mut RenderState, dt: Duration) {
|
||||
let scene = &mut state.scene;
|
||||
self.camera_controller.update_camera(&mut state.camera, dt);
|
||||
|
||||
// Animate the stroke_width to match target_stroke_width
|
||||
scene.stroke_width =
|
||||
scene.stroke_width + (self.target_stroke_width - scene.stroke_width) / 5.0;
|
||||
|
||||
// Animate the strokes of primitive
|
||||
/* scene.cpu_primitives[0 as usize].width = scene.stroke_width;*/
|
||||
/*
|
||||
scene.cpu_primitives[STROKE_PRIM_ID as usize].color = [
|
||||
(time_secs * 0.8 - 1.6).sin() * 0.1 + 0.1,
|
||||
(time_secs * 0.5 - 1.6).sin() * 0.1 + 0.1,
|
||||
(time_secs - 1.6).sin() * 0.1 + 0.1,
|
||||
1.0,
|
||||
];
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
use crate::render::shaders::ShaderCamera;
|
||||
use cgmath::prelude::*;
|
||||
use cgmath::Matrix4;
|
||||
use cgmath::{Matrix4, Vector2, Vector4};
|
||||
|
||||
use crate::render::shaders::ShaderCamera;
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f64> = cgmath::Matrix4::new(
|
||||
@ -23,6 +24,9 @@ pub struct Camera {
|
||||
pub position: cgmath::Point3<f64>,
|
||||
pub yaw: cgmath::Rad<f64>,
|
||||
pub pitch: cgmath::Rad<f64>,
|
||||
|
||||
pub width: f64,
|
||||
pub height: f64,
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
@ -34,14 +38,23 @@ impl Camera {
|
||||
position: V,
|
||||
yaw: Y,
|
||||
pitch: P,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
position: position.into(),
|
||||
yaw: yaw.into(),
|
||||
pitch: pitch.into(),
|
||||
width: width as f64,
|
||||
height: height as f64,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, width: u32, height: u32) {
|
||||
self.width = width as f64;
|
||||
self.height = height as f64;
|
||||
}
|
||||
|
||||
fn calc_matrix(&self) -> cgmath::Matrix4<f64> {
|
||||
cgmath::Matrix4::look_to_rh(
|
||||
self.position,
|
||||
@ -62,6 +75,34 @@ impl Camera {
|
||||
self.position.to_homogeneous().cast::<f32>().unwrap().into(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn project_screen_to_world(
|
||||
&self,
|
||||
screen: &Vector2<f64>,
|
||||
view_proj: &Matrix4<f64>,
|
||||
) -> Vector4<f64> {
|
||||
let min_depth = 0.0;
|
||||
let max_depth = 1.0;
|
||||
|
||||
let x = 0.0;
|
||||
let y = 0.0;
|
||||
let ox = x + self.width / 2.0;
|
||||
let oy = y + self.height / 2.0;
|
||||
let oz = min_depth;
|
||||
let pz = max_depth - min_depth;
|
||||
|
||||
// Adapted from: https://docs.microsoft.com/en-us/windows/win32/direct3d9/viewports-and-clipping#viewport-rectangle
|
||||
let direct_x = Matrix4::from_cols(
|
||||
Vector4::new(self.width as f64 / 2.0, 0.0, 0.0, 0.0),
|
||||
Vector4::new(0.0, self.height as f64 / 2.0, 0.0, 0.0),
|
||||
Vector4::new(0.0, 0.0, pz, 0.0),
|
||||
Vector4::new(ox, oy, oz, 1.0),
|
||||
);
|
||||
|
||||
let homogenous = Vector4::new(screen.x, screen.y, 1.0, 1.0) * self.position.z;
|
||||
|
||||
view_proj.invert().unwrap() * direct_x.invert().unwrap() * homogenous
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Perspective {
|
||||
|
||||
@ -19,16 +19,6 @@ use super::shaders;
|
||||
use super::shaders::*;
|
||||
use super::texture::Texture;
|
||||
|
||||
pub struct SceneParams {
|
||||
pub stroke_width: f32,
|
||||
}
|
||||
|
||||
impl Default for SceneParams {
|
||||
fn default() -> Self {
|
||||
SceneParams { stroke_width: 1.0 }
|
||||
}
|
||||
}
|
||||
|
||||
const INDEX_FORMAT: wgpu::IndexFormat = wgpu::IndexFormat::Uint16; // Must match IndexDataType
|
||||
const VERTEX_BUFFER_SIZE: BufferAddress = 1024 * 1024 * 8;
|
||||
const INDICES_BUFFER_SIZE: BufferAddress = 1024 * 1024 * 8;
|
||||
@ -68,16 +58,6 @@ pub struct RenderState {
|
||||
|
||||
pub camera: camera::Camera,
|
||||
pub perspective: camera::Perspective,
|
||||
|
||||
pub scene: SceneParams,
|
||||
}
|
||||
|
||||
impl SceneParams {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
..SceneParams::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderState {
|
||||
@ -259,7 +239,13 @@ impl RenderState {
|
||||
None
|
||||
};
|
||||
|
||||
let camera = camera::Camera::new((0.0, 5.0, 5000.0), cgmath::Deg(-90.0), cgmath::Deg(-0.0));
|
||||
let camera = camera::Camera::new(
|
||||
(0.0, 5.0, 5000.0),
|
||||
cgmath::Deg(-90.0),
|
||||
cgmath::Deg(-0.0),
|
||||
size.width,
|
||||
size.height,
|
||||
);
|
||||
let projection = camera::Perspective::new(
|
||||
surface_config.width,
|
||||
surface_config.height,
|
||||
@ -281,7 +267,6 @@ impl RenderState {
|
||||
multisampling_texture,
|
||||
depth_texture,
|
||||
sample_count,
|
||||
scene: SceneParams::new(),
|
||||
globals_uniform_buffer,
|
||||
tiles_uniform_buffer,
|
||||
fps_meter: FPSMeter::new(),
|
||||
@ -320,6 +305,7 @@ impl RenderState {
|
||||
self.surface.configure(&self.device, &self.surface_config);
|
||||
|
||||
self.perspective.resize(new_size.width, new_size.height);
|
||||
self.camera.resize(new_size.width, new_size.height);
|
||||
|
||||
// Re-configure depth buffer
|
||||
self.depth_texture = Texture::create_depth_texture(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user