Nicer way of moving around, still very WIP

master
Wynd 2024-11-05 01:23:56 +02:00
parent d1cd09b52a
commit e1028d3d89
1 changed files with 35 additions and 8 deletions

View File

@ -1,5 +1,5 @@
use winit::{ use winit::{
event::{ElementState, KeyEvent, MouseScrollDelta, WindowEvent}, event::{ElementState, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent},
keyboard::{KeyCode, PhysicalKey}, keyboard::{KeyCode, PhysicalKey},
}; };
@ -7,23 +7,48 @@ use crate::camera::Camera;
pub struct CameraController { pub struct CameraController {
speed: f32, speed: f32,
cursor: (f64, f64),
forward: f32, forward: f32,
cursor_start: (f32, f32),
movement: (f32, f32),
is_moving: bool,
} }
impl CameraController { impl CameraController {
pub fn new(speed: f32) -> Self { pub fn new(speed: f32) -> Self {
Self { Self {
speed, speed,
cursor: (0.0, 0.0),
forward: 0.0, forward: 0.0,
cursor_start: (0.0, 0.0),
movement: (0.0, 0.0),
is_moving: false,
} }
} }
pub fn process_events(&mut self, event: &WindowEvent) -> bool { pub fn process_events(&mut self, event: &WindowEvent) -> bool {
match event { match event {
WindowEvent::MouseInput { state, button, .. } => {
if state.is_pressed() && *button == MouseButton::Left {
self.is_moving = true;
return true;
}
else {
self.is_moving = false;
self.movement = (0.0, 0.0);
}
false
}
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
self.cursor = (position.x, position.y); if !self.is_moving {
self.cursor_start = (position.x as f32, position.y as f32);
}
else {
let x = -self.cursor_start.0 + position.x as f32;
let x = x * 0.000001;
let y = self.cursor_start.1 - position.y as f32;
let y = y * 0.000001;
self.movement = (x, y);
}
true true
} }
WindowEvent::MouseWheel { delta, .. } => match delta { WindowEvent::MouseWheel { delta, .. } => match delta {
@ -50,11 +75,13 @@ impl CameraController {
camera.eye -= forward_norm * self.speed; camera.eye -= forward_norm * self.speed;
} }
camera.eye.x = (self.cursor.0 as f32 / 1920.0) - 0.5; if self.is_moving {
camera.eye.y = (-self.cursor.1 as f32 / 1080.0) + 0.5; camera.eye.x += self.movement.0;
camera.eye.y += self.movement.1;
camera.target.x = (self.cursor.0 as f32 / 1920.0) - 0.5; camera.target.x += self.movement.0;
camera.target.y = (-self.cursor.1 as f32 / 1080.0) + 0.5; camera.target.y += self.movement.1;
}
self.forward = 0.0; self.forward = 0.0;
} }