Improve handling of touch

This commit is contained in:
Maximilian Ammann 2021-12-19 16:41:06 +01:00
parent b31de71054
commit 4359e04810
3 changed files with 34 additions and 36 deletions

View File

@ -157,8 +157,8 @@ impl CameraController {
} }
pub fn process_touch(&mut self, touch_dx: f64, touch_dy: f64) { pub fn process_touch(&mut self, touch_dx: f64, touch_dy: f64) {
self.rotate_horizontal = touch_dx as f32; self.amount_right += touch_dx as f32;
self.rotate_vertical = touch_dy as f32; self.amount_up += touch_dy as f32;
} }
pub fn process_scroll(&mut self, delta: &winit::event::MouseScrollDelta) { pub fn process_scroll(&mut self, delta: &winit::event::MouseScrollDelta) {
@ -166,9 +166,9 @@ impl CameraController {
// I'm assuming a line is about 100 pixels // I'm assuming a line is about 100 pixels
winit::event::MouseScrollDelta::LineDelta(_, scroll) => scroll * 100.0, winit::event::MouseScrollDelta::LineDelta(_, scroll) => scroll * 100.0,
winit::event::MouseScrollDelta::PixelDelta(winit::dpi::PhysicalPosition { winit::event::MouseScrollDelta::PixelDelta(winit::dpi::PhysicalPosition {
y: scroll, y: scroll,
.. ..
}) => *scroll as f32, }) => *scroll as f32,
}; };
} }

View File

@ -1,16 +1,17 @@
use std::cmp; use std::cmp;
use std::io::Cursor; use std::io::Cursor;
use std::ops::Range; use std::ops::Range;
use log::warn;
use log::warn;
use lyon::tessellation::VertexBuffers; use lyon::tessellation::VertexBuffers;
use vector_tile::parse_tile_reader;
use wgpu::util::DeviceExt;
use wgpu::{Extent3d, Limits}; use wgpu::{Extent3d, Limits};
use wgpu::util::DeviceExt;
use winit::dpi::PhysicalSize; use winit::dpi::PhysicalSize;
use winit::event::{DeviceEvent, ElementState, KeyboardInput, MouseButton, TouchPhase, WindowEvent}; use winit::event::{DeviceEvent, ElementState, KeyboardInput, MouseButton, TouchPhase, WindowEvent};
use winit::window::Window; use winit::window::Window;
use vector_tile::parse_tile_reader;
use crate::fps_meter::FPSMeter; use crate::fps_meter::FPSMeter;
use crate::io::static_database; use crate::io::static_database;
use crate::render::camera; use crate::render::camera;
@ -44,7 +45,7 @@ impl Default for SceneParams {
} }
} }
const INDEX_FORMAT: wgpu::IndexFormat = wgpu::IndexFormat::Uint32; const INDEX_FORMAT: wgpu::IndexFormat = wgpu::IndexFormat::Uint32;
type IndexDataType = u32; // Must match INDEX_FORMAT type IndexDataType = u32; // Must match INDEX_FORMAT
const PRIM_BUFFER_LEN: usize = 256; const PRIM_BUFFER_LEN: usize = 256;
@ -55,7 +56,6 @@ const MASK_FILL_PRIM_ID: u32 = 3;
const SECOND_TILE_STROKE_PRIM_ID: u32 = 5; const SECOND_TILE_STROKE_PRIM_ID: u32 = 5;
pub struct State { pub struct State {
instance: wgpu::Instance, instance: wgpu::Instance,
device: wgpu::Device, device: wgpu::Device,
@ -151,7 +151,7 @@ impl State {
println!("Using static database from {}", static_database::get_source_path()); println!("Using static database from {}", static_database::get_source_path());
let tile = parse_tile_reader(&mut Cursor::new(static_database::get_tile(2179, 1421,12).unwrap().contents())).expect("failed to load tile"); let tile = parse_tile_reader(&mut Cursor::new(static_database::get_tile(2179, 1421, 12).unwrap().contents())).expect("failed to load tile");
let (tile_stroke_range, tile_fill_range) = ( let (tile_stroke_range, tile_fill_range) = (
tile.tesselate_stroke(&mut geometry, STROKE_PRIM_ID), tile.tesselate_stroke(&mut geometry, STROKE_PRIM_ID),
//tile.empty_range(&mut geometry, STROKE_PRIM_ID), //tile.empty_range(&mut geometry, STROKE_PRIM_ID),
@ -159,7 +159,7 @@ impl State {
); );
// tile right to it // tile right to it
let tile = parse_tile_reader(&mut Cursor::new(static_database::get_tile(2180, 1421,12).unwrap().contents())).expect("failed to load tile"); let tile = parse_tile_reader(&mut Cursor::new(static_database::get_tile(2180, 1421, 12).unwrap().contents())).expect("failed to load tile");
let (tile2_stroke_range, tile2_fill_range) = ( let (tile2_stroke_range, tile2_fill_range) = (
tile.tesselate_stroke(&mut geometry, SECOND_TILE_STROKE_PRIM_ID), tile.tesselate_stroke(&mut geometry, SECOND_TILE_STROKE_PRIM_ID),
//tile.empty_range(&mut geometry, STROKE_PRIM_ID), //tile.empty_range(&mut geometry, STROKE_PRIM_ID),
@ -191,7 +191,6 @@ impl State {
max_storage_textures_per_shader_stage: 4, max_storage_textures_per_shader_stage: 4,
..wgpu::Limits::default() ..wgpu::Limits::default()
} }
} else { } else {
Limits { Limits {
..wgpu::Limits::default() ..wgpu::Limits::default()
@ -391,9 +390,9 @@ impl State {
surface_config.height, surface_config.height,
cgmath::Deg(45.0), cgmath::Deg(45.0),
0.1, 0.1,
10000.0, 100000.0,
); );
let camera_controller = camera::CameraController::new(4000.0, 0.4); let camera_controller = camera::CameraController::new(3000.0, 0.2);
Self { Self {
instance, instance,
@ -425,7 +424,7 @@ impl State {
camera_controller, camera_controller,
mouse_pressed: false, mouse_pressed: false,
tile2_stroke_range, tile2_stroke_range,
suspended: false // Initially the app is not suspended suspended: false, // Initially the app is not suspended
} }
} }
@ -474,12 +473,12 @@ impl State {
} }
} }
pub fn device_input(&mut self, event: &DeviceEvent) -> bool { pub fn device_input(&mut self, event: &DeviceEvent, window: &Window) -> bool {
match event { match event {
DeviceEvent::MouseMotion { delta } => { DeviceEvent::MouseMotion { delta } => {
if self.mouse_pressed { if self.mouse_pressed {
warn!("mouse {}", delta.0); warn!("mouse {}", delta.0);
self.camera_controller.process_mouse(delta.0, delta.1); self.camera_controller.process_mouse(delta.0 / window.scale_factor(), delta.1 / window.scale_factor());
} }
true true
} }
@ -487,15 +486,15 @@ impl State {
} }
} }
pub fn window_input(&mut self, event: &WindowEvent) -> bool { pub fn window_input(&mut self, event: &WindowEvent, window: &Window) -> bool {
match event { match event {
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
input: input:
KeyboardInput { KeyboardInput {
state, state,
virtual_keycode: Some(key), virtual_keycode: Some(key),
.. ..
}, },
.. ..
} => match key { } => match key {
winit::event::VirtualKeyCode::Z => { winit::event::VirtualKeyCode::Z => {
@ -509,7 +508,6 @@ impl State {
_ => self.camera_controller.process_keyboard(*key, *state), _ => self.camera_controller.process_keyboard(*key, *state),
}, },
WindowEvent::Touch(touch) => { WindowEvent::Touch(touch) => {
match touch.phase { match touch.phase {
TouchPhase::Started => { TouchPhase::Started => {
self.scene.last_touch = Some((touch.location.x, touch.location.y)) self.scene.last_touch = Some((touch.location.x, touch.location.y))
@ -518,8 +516,8 @@ impl State {
if let Some(start) = self.scene.last_touch { if let Some(start) = self.scene.last_touch {
let delta_x = start.0 - touch.location.x; let delta_x = start.0 - touch.location.x;
let delta_y = start.1 - touch.location.y; let delta_y = start.1 - touch.location.y;
warn!("touch {} {}", delta_x, delta_y); warn!("touch {} {} {}", delta_x, delta_y, window.scale_factor());
self.camera_controller.process_touch(delta_x, delta_y); self.camera_controller.process_touch(delta_x / window.scale_factor(), delta_y / window.scale_factor());
} }
self.scene.last_touch = Some((touch.location.x, touch.location.y)) self.scene.last_touch = Some((touch.location.x, touch.location.y))
@ -678,13 +676,13 @@ impl State {
// Animate the strokes of primitive // Animate the strokes of primitive
scene.cpu_primitives[STROKE_PRIM_ID as usize].width = scene.stroke_width; scene.cpu_primitives[STROKE_PRIM_ID as usize].width = scene.stroke_width;
/* scene.cpu_primitives[STROKE_PRIM_ID as usize].color = [ /* scene.cpu_primitives[STROKE_PRIM_ID as usize].color = [
(time_secs * 0.8 - 1.6).sin() * 0.1 + 0.1, (time_secs * 0.8 - 1.6).sin() * 0.1 + 0.1,
(time_secs * 0.5 - 1.6).sin() * 0.1 + 0.1, (time_secs * 0.5 - 1.6).sin() * 0.1 + 0.1,
(time_secs - 1.6).sin() * 0.1 + 0.1, (time_secs - 1.6).sin() * 0.1 + 0.1,
1.0, 1.0,
]; ];
*/ */
self.fps_meter.update_and_print() self.fps_meter.update_and_print()
} }

View File

@ -19,14 +19,14 @@ pub async fn setup(window: winit::window::Window, event_loop: EventLoop<()>) {
.. // We're not using device_id currently .. // We're not using device_id currently
} => { } => {
trace!("{:?}", event); trace!("{:?}", event);
state.device_input(event); state.device_input(event, &window);
} }
Event::WindowEvent { Event::WindowEvent {
ref event, ref event,
window_id, window_id,
} if window_id == window.id() => { } if window_id == window.id() => {
if !state.window_input(event) { if !state.window_input(event, &window) {
match event { match event {
WindowEvent::CloseRequested WindowEvent::CloseRequested
| WindowEvent::KeyboardInput { | WindowEvent::KeyboardInput {