| 56 | } |
| 57 | |
| 58 | pub fn process_event(&mut self, event: &Event<()>, camera: &mut Camera) -> bool { |
| 59 | match event { |
| 60 | Event::WindowEvent { event, .. } => match event { |
| 61 | WindowEvent::CursorMoved { position, .. } => { |
| 62 | let mouse_pos = Vec2::new(position.x, position.y).numcast().unwrap(); |
| 63 | let delta = mouse_pos - self.last_mouse_pos; |
| 64 | self.last_mouse_pos = mouse_pos; |
| 65 | |
| 66 | if !self.mousewheel_pressed { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if self.first { |
| 71 | self.last_mouse_pos = mouse_pos; |
| 72 | self.first = false; |
| 73 | } |
| 74 | |
| 75 | if self.shift_pressed { |
| 76 | let change = Vec2::new(-delta.x, delta.y) * self.sensitivity * 0.05; |
| 77 | camera.lookat += change; |
| 78 | camera.origin += change; |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | self.yaw += delta.x * self.sensitivity; |
| 83 | self.pitch += delta.y * self.sensitivity; |
| 84 | self.pitch = self.pitch.min(89.0).max(-89.0); |
| 85 | |
| 86 | let yaw_rad = self.yaw.to_radians(); |
| 87 | let pitch_rad = self.pitch.to_radians(); |
| 88 | |
| 89 | let dir = Vec3::new( |
| 90 | yaw_rad.cos() * pitch_rad.cos(), |
| 91 | pitch_rad.sin(), |
| 92 | yaw_rad.sin() * pitch_rad.cos(), |
| 93 | ); |
| 94 | camera.origin = camera.lookat + dir; |
| 95 | true |
| 96 | } |
| 97 | WindowEvent::MouseWheel { delta, .. } => { |
| 98 | let zoom = match delta { |
| 99 | MouseScrollDelta::LineDelta(x, y) => Vec2::new(*x, *y), |
| 100 | _ => panic!(), |
| 101 | }; |
| 102 | |
| 103 | camera.fov -= zoom.y; |
| 104 | camera.fov = camera.fov.clamp(1.0, 60.0); |
| 105 | true |
| 106 | } |
| 107 | WindowEvent::MouseInput { state, button, .. } => { |
| 108 | if *button == MouseButton::Middle { |
| 109 | self.mousewheel_pressed = *state == ElementState::Pressed; |
| 110 | } |
| 111 | false |
| 112 | } |
| 113 | WindowEvent::Resized(size) => { |
| 114 | camera.aspect_ratio = size.width as f32 / size.height as f32; |
| 115 | true |