mouse movement

This commit is contained in:
Piotr 2020-09-29 21:15:05 +02:00
parent 637ffd06d6
commit 6ab1057199
2 changed files with 39 additions and 26 deletions

View File

@ -57,18 +57,18 @@ impl ExampleClient {
impl Client for ExampleClient { impl Client for ExampleClient {
fn on_keyboard(&mut self, code: &VirtualKeyCode, state: &ElementState) { fn on_keyboard(&mut self, code: &VirtualKeyCode, state: &ElementState) {
match (code, state) { match (code, state) {
(VirtualKeyCode::W, ElementState::Pressed) => { (VirtualKeyCode::W, ElementState::Pressed) => self
self.camera.process_keyboard(CameraMovement::FORWARD, self.delta) .camera
} .process_keyboard(CameraMovement::FORWARD, self.delta),
(VirtualKeyCode::S, ElementState::Pressed) => { (VirtualKeyCode::S, ElementState::Pressed) => self
self.camera.process_keyboard(CameraMovement::BACKWARD, self.delta) .camera
} .process_keyboard(CameraMovement::BACKWARD, self.delta),
(VirtualKeyCode::A, ElementState::Pressed) => { (VirtualKeyCode::A, ElementState::Pressed) => self
self.camera.process_keyboard(CameraMovement::LEFT, self.delta) .camera
} .process_keyboard(CameraMovement::LEFT, self.delta),
(VirtualKeyCode::D, ElementState::Pressed) => { (VirtualKeyCode::D, ElementState::Pressed) => self
self.camera.process_keyboard(CameraMovement::RIGHT, self.delta) .camera
} .process_keyboard(CameraMovement::RIGHT, self.delta),
(VirtualKeyCode::LControl, _) => self (VirtualKeyCode::LControl, _) => self
.camera .camera
.enable_mouse_movement(state == &ElementState::Released), .enable_mouse_movement(state == &ElementState::Released),
@ -152,7 +152,7 @@ impl Client for ExampleClient {
} }
fn update(&mut self, _engine: &Engine, delta: f32) { fn update(&mut self, _engine: &Engine, delta: f32) {
self.delta = delta; self.delta = delta;
println!("{}",delta); println!("{}", delta);
} }
#[cfg(feature = "imgui_inspect")] #[cfg(feature = "imgui_inspect")]

View File

@ -34,9 +34,7 @@ impl TimeStep {
pub fn delta(&mut self) -> f32 { pub fn delta(&mut self) -> f32 {
let current_time = Instant::now(); let current_time = Instant::now();
let delta = current_time.duration_since(self.last_time).as_micros() let delta = current_time.duration_since(self.last_time).as_micros() as f32 * 0.001;
as f32
* 0.001;
self.last_time = current_time; self.last_time = current_time;
self.delta_time = delta; self.delta_time = delta;
delta delta
@ -58,7 +56,6 @@ impl TimeStep {
} }
} }
#[cfg(not(feature = "glfw_obsolete"))] #[cfg(not(feature = "glfw_obsolete"))]
pub struct Engine { pub struct Engine {
title: String, title: String,
@ -72,7 +69,7 @@ impl Default for Engine {
Engine { Engine {
title: String::from("Doppler demo"), title: String::from("Doppler demo"),
size: (1280, 720), size: (1280, 720),
debug_layer: true debug_layer: true,
} }
} }
} }
@ -198,11 +195,10 @@ impl Engine {
Event::NewEvents(_) => { Event::NewEvents(_) => {
// other application-specific logic // other application-specific logic
last_frame = imgui.io_mut().update_delta_time(last_frame); last_frame = imgui.io_mut().update_delta_time(last_frame);
} }
Event::MainEventsCleared => { Event::MainEventsCleared => {
let delta = timestep.delta(); let delta = timestep.delta();
client.update(&self,delta); client.update(&self, delta);
// other application-specific logic // other application-specific logic
platform platform
.prepare_frame(imgui.io_mut(), &gl_window.window()) // step 4 .prepare_frame(imgui.io_mut(), &gl_window.window()) // step 4
@ -211,6 +207,23 @@ impl Engine {
} }
Event::LoopDestroyed => return, Event::LoopDestroyed => return,
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::CursorMoved { position, .. } => {
let xpos = position.x as f32;
let ypos = position.y as f32;
if first_mouse {
last_x = xpos;
last_y = ypos;
first_mouse = false;
}
let xoffset = xpos - last_x;
let yoffset = last_y - ypos; // reversed since y-coordinates go from bottom to top
last_x = xpos;
last_y = ypos;
client.on_mouse_move(xoffset, yoffset);
}
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
input: input:
KeyboardInput { KeyboardInput {