From d12083506b966e70559d5ed3e2f023f132396311 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 14 Feb 2021 23:54:54 -0800 Subject: [PATCH] camera pausing, weird resizing crashes. Not sure what to tackle next. --- src/camera.rs | 73 +++++++++++++++++------------ src/extended_winit_imgui_support.rs | 4 +- src/owned_event.rs | 40 ++++++++-------- 3 files changed, 66 insertions(+), 51 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 599fa1e..d4f778d 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -73,7 +73,7 @@ pub struct CameraController { scroll: f32, speed: f32, sensitivity: f32, - last_frame: Instant, + active: bool, } impl CameraController { @@ -90,7 +90,7 @@ impl CameraController { scroll: 0.0, speed, sensitivity, - last_frame: Instant::now(), + active: true } } @@ -100,38 +100,53 @@ impl CameraController { } else { 0.0 }; - match key { - VirtualKeyCode::W | VirtualKeyCode::Up => { - self.amount_forward = amount; - true - } - VirtualKeyCode::S | VirtualKeyCode::Down => { - self.amount_backward = amount; - true - } - VirtualKeyCode::A | VirtualKeyCode::Left => { - self.amount_left = amount; - true - } - VirtualKeyCode::D | VirtualKeyCode::Right => { - self.amount_right = amount; - true - } - VirtualKeyCode::Space => { - self.amount_up = amount; - true - } - VirtualKeyCode::LShift => { - self.amount_down = amount; - true + let active = match key { + VirtualKeyCode::P => { + if state == ElementState::Pressed { + self.active = !self.active; + } + self.active + }, + _ => {self.active} + }; + if active { + match key { + VirtualKeyCode::W | VirtualKeyCode::Up => { + self.amount_forward = amount; + true + } + VirtualKeyCode::S | VirtualKeyCode::Down => { + self.amount_backward = amount; + true + } + VirtualKeyCode::A | VirtualKeyCode::Left => { + self.amount_left = amount; + true + } + VirtualKeyCode::D | VirtualKeyCode::Right => { + self.amount_right = amount; + true + } + VirtualKeyCode::Space => { + self.amount_up = amount; + true + } + VirtualKeyCode::LShift => { + self.amount_down = amount; + true + } + _ => false, } - _ => false, + } else { + false } } pub fn process_mouse(&mut self, mouse_dx: f64, mouse_dy: f64) { - self.rotate_horizontal = -mouse_dx as f32; - self.rotate_vertical = mouse_dy as f32; + if self.active { + self.rotate_horizontal = -mouse_dx as f32; + self.rotate_vertical = mouse_dy as f32; + } } pub fn update_camera(&mut self, camera: &mut Camera, dt: f32) { diff --git a/src/extended_winit_imgui_support.rs b/src/extended_winit_imgui_support.rs index 7110af2..76aae5e 100644 --- a/src/extended_winit_imgui_support.rs +++ b/src/extended_winit_imgui_support.rs @@ -742,7 +742,7 @@ impl WinitPlatform { /// * window size / dpi factor changes are applied /// * keyboard state is updated /// * mouse state is updated - //#[cfg(any(feature = "winit-22", feature = "winit-23", feature = "winit-24"))] + #[cfg(any(feature = "winit-22", feature = "winit-23", feature = "winit-24"))] pub fn handle_event(&mut self, io: &mut Io, window: &Window, event: &OwnedEvent) { match *event { OwnedEvent::WindowEvent { @@ -979,7 +979,7 @@ impl WinitPlatform { } } - //#[cfg(any(feature = "winit-23", feature = "winit-24"))] + #[cfg(any(feature = "winit-23", feature = "winit-24"))] fn handle_window_event(&mut self, io: &mut Io, window: &Window, event: &OwnedWindowEvent) { match *event { OwnedWindowEvent::Resized(physical_size) => { diff --git a/src/owned_event.rs b/src/owned_event.rs index b0986fe..10f83f1 100644 --- a/src/owned_event.rs +++ b/src/owned_event.rs @@ -3,12 +3,9 @@ use std::path::PathBuf; use gilrs::Event as GilEvent; use legion::world::SubWorld; use legion::*; -use winit_24::dpi::{PhysicalPosition, PhysicalSize}; +use winit_24::dpi::{PhysicalPosition, PhysicalSize, LogicalPosition}; use winit_24::event::DeviceEvent::MouseMotion; -use winit_24::event::{ - AxisId, DeviceEvent, DeviceId, ElementState, Event, KeyboardInput, ModifiersState, MouseButton, - MouseScrollDelta, StartCause, Touch, TouchPhase, WindowEvent, -}; +use winit_24::event::{AxisId, DeviceEvent, DeviceId, ElementState, Event, KeyboardInput, ModifiersState, MouseButton, MouseScrollDelta, StartCause, Touch, TouchPhase, WindowEvent, VirtualKeyCode}; use winit_24::window::{Theme, WindowId, Window}; use crate::camera::{Camera, CameraController}; @@ -266,6 +263,7 @@ pub fn event_dispatch( for (camera_controller) in query.iter_mut(world) { camera_controller.process_mouse(delta.0, delta.1); } + }, OwnedEvent::DeviceEvent { event: winit_24::event::DeviceEvent::Key(keyboard_input), @@ -279,21 +277,23 @@ pub fn event_dispatch( ); } - // match keyboard_input.virtual_keycode.unwrap() { - // VirtualKeyCode::A => { - // if keyboard_input.state == ElementState::Pressed {} - // } - // VirtualKeyCode::S => { - // if keyboard_input.state == ElementState::Pressed {} - // } - // VirtualKeyCode::P => { - // if keyboard_input.state == ElementState::Pressed { - // let data = world.write_resource::().read_compute_buffer(compute_buffer.clone()); - // image::save_buffer(&Path::new("image.png"), data.as_slice(), (image_data.1).0, (image_data.1).1, image::RGBA(8)); - // } - // } - // _ => () - // } + match keyboard_input.virtual_keycode.unwrap() { + VirtualKeyCode::A => { + if keyboard_input.state == ElementState::Pressed { + println!("cursorijf"); + winit_window.set_cursor_position( + LogicalPosition{ x: 100.0, y: 100.0 } + ); + //winit_window.set_cursor_grab(true); + } + } + VirtualKeyCode::S => { + if keyboard_input.state == ElementState::Pressed { + //winit_window.set_cursor_grab(false); + } + } + _ => () + } } _ => {} }