Refactor input such that it works on web

This commit is contained in:
Maximilian Ammann 2021-12-06 20:21:14 +01:00
parent 586baa17d6
commit 465b1efc93
3 changed files with 58 additions and 37 deletions

View File

@ -1,15 +1,17 @@
use log::info;
use log::{info, trace};
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder};
use crate::platform::Instant;
use crate::render::state::State;
mod fps_meter;
mod platform;
mod render;
#[cfg(target_arch = "wasm32")]
mod web;
mod render;
async fn setup(window: Window, event_loop: EventLoop<()>) {
info!("== mapr ==");
@ -20,10 +22,7 @@ async fn setup(window: Window, event_loop: EventLoop<()>) {
let mut state = State::new(&window).await;
// Important: This kick-starts the rendering loop
// window.request_redraw();
let mut last_render_time = std::time::Instant::now();
let mut last_render_time = Instant::now();
event_loop.run(move |event, _, control_flow| {
match event {
@ -31,21 +30,24 @@ async fn setup(window: Window, event_loop: EventLoop<()>) {
ref event,
.. // We're not using device_id currently
} => {
state.input(event);
trace!("{:?}", event);
state.device_input(event);
}
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => {
if !state.window_input(event) {
match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
} => *control_flow = ControlFlow::Exit,
WindowEvent::Resized(physical_size) => {
@ -57,10 +59,10 @@ async fn setup(window: Window, event_loop: EventLoop<()>) {
}
_ => {}
}
}
}
Event::RedrawRequested(_) => {
let now = std::time::Instant::now();
let now = Instant::now();
let dt = now - last_render_time;
last_render_time = now;
state.update(dt);

View File

@ -1,6 +1,7 @@
use std::f32::consts::FRAC_PI_2;
use cgmath::prelude::*;
use log::info;
#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
@ -122,6 +123,7 @@ impl CameraController {
};
match key {
winit::event::VirtualKeyCode::W | winit::event::VirtualKeyCode::Up => {
info!("W/Up Pressed");
self.amount_forward = amount;
true
}

View File

@ -5,12 +5,12 @@ use std::ops::Range;
use lyon::tessellation::VertexBuffers;
use vector_tile::parse_tile_reader;
use wgpu::util::DeviceExt;
use winit::event::{DeviceEvent, ElementState, KeyboardInput, WindowEvent};
use winit::event::{DeviceEvent, ElementState, KeyboardInput, MouseButton, WindowEvent};
use winit::window::Window;
use crate::fps_meter::FPSMeter;
use crate::render::camera;
use crate::render::camera::{CameraUniform};
use crate::render::camera::CameraUniform;
use crate::render::tesselation::TileMask;
use super::piplines::*;
@ -334,7 +334,13 @@ impl State {
};
let camera = camera::Camera::new((0.0, 5.0, 5000.0), cgmath::Deg(-90.0), cgmath::Deg(-0.0));
let projection = camera::Projection::new(surface_config.width, surface_config.height, cgmath::Deg(45.0), 0.1, 10000.0);
let projection = camera::Projection::new(
surface_config.width,
surface_config.height,
cgmath::Deg(45.0),
0.1,
10000.0,
);
let camera_controller = camera::CameraController::new(4000.0, 0.4);
let mut camera_uniform = CameraUniform::new();
@ -367,7 +373,7 @@ impl State {
projection,
camera_controller,
camera_uniform,
mouse_pressed: false
mouse_pressed: false,
}
}
@ -401,24 +407,8 @@ impl State {
}
}
pub fn input(&mut self, event: &DeviceEvent) -> bool {
pub fn device_input(&mut self, event: &DeviceEvent) -> bool {
match event {
DeviceEvent::Key(KeyboardInput {
virtual_keycode: Some(key),
state,
..
}) => self.camera_controller.process_keyboard(*key, *state),
DeviceEvent::MouseWheel { delta, .. } => {
self.camera_controller.process_scroll(delta);
true
}
DeviceEvent::Button {
button: 1, // Left Mouse Button
state,
} => {
self.mouse_pressed = *state == ElementState::Pressed;
true
}
DeviceEvent::MouseMotion { delta } => {
if self.mouse_pressed {
self.camera_controller.process_mouse(delta.0, delta.1);
@ -429,6 +419,33 @@ impl State {
}
}
pub fn window_input(&mut self, event: &WindowEvent) -> bool {
match event {
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state,
virtual_keycode: Some(key),
..
},
..
} => self.camera_controller.process_keyboard(*key, *state),
WindowEvent::MouseWheel { delta, .. } => {
self.camera_controller.process_scroll(delta);
true
}
WindowEvent::MouseInput {
button: MouseButton::Left, // Left Mouse Button
state,
..
} => {
self.mouse_pressed = *state == ElementState::Pressed;
true
}
_ => false,
}
}
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let frame = self.surface.get_current_texture()?;
let scene = &mut self.scene;
@ -533,8 +550,8 @@ impl State {
let time_secs = self.fps_meter.time_secs as f32;
self.camera_controller.update_camera(&mut self.camera, dt);
self.camera_uniform.update_view_proj(&self.camera, &self.projection);
self.camera_uniform
.update_view_proj(&self.camera, &self.projection);
// Animate the stroke_width to match target_stroke_width
scene.stroke_width =