From 545f3af29e9a7762ecc712035d0f231d9a6e5285 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Sat, 15 Apr 2023 11:37:53 +0300 Subject: [PATCH] [WIP] mouse shader example on android w/ touch --- examples/runners/wgpu/src/graphics.rs | 40 +++++++++++++++++++++++++-- examples/runners/wgpu/src/lib.rs | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/examples/runners/wgpu/src/graphics.rs b/examples/runners/wgpu/src/graphics.rs index 9a5bd43263..c857de5bc8 100644 --- a/examples/runners/wgpu/src/graphics.rs +++ b/examples/runners/wgpu/src/graphics.rs @@ -3,7 +3,10 @@ use crate::maybe_watch; use super::Options; use shared::ShaderConstants; use winit::{ - event::{ElementState, Event, KeyboardInput, MouseButton, VirtualKeyCode, WindowEvent}, + event::{ + ElementState, Event, KeyboardInput, MouseButton, Touch, TouchPhase, VirtualKeyCode, + WindowEvent, + }, event_loop::{ControlFlow, EventLoop, EventLoopBuilder}, window::Window, }; @@ -297,7 +300,40 @@ async fn run( } } Event::WindowEvent { - event: WindowEvent::CursorMoved { position, .. }, + event: + WindowEvent::Touch(Touch { + phase: phase @ (TouchPhase::Started | TouchPhase::Ended | TouchPhase::Cancelled), + .. + }), + .. + } => { + // FIXME(eddyb) deduplicate! + let button = MouseButton::Left; + let mask = 1 << mouse_button_index(button); + match phase { + TouchPhase::Started => { + mouse_button_pressed |= mask; + mouse_button_press_since_last_frame |= mask; + + if button == MouseButton::Left { + drag_start_x = cursor_x; + drag_start_y = cursor_y; + drag_end_x = cursor_x; + drag_end_y = cursor_y; + } + } + TouchPhase::Moved => unreachable!(), + TouchPhase::Ended | TouchPhase::Cancelled => mouse_button_pressed &= !mask, + } + } + Event::WindowEvent { + event: + WindowEvent::CursorMoved { position, .. } + | WindowEvent::Touch(Touch { + phase: TouchPhase::Moved, + location: position, + .. + }), .. } => { cursor_x = position.x as f32; diff --git a/examples/runners/wgpu/src/lib.rs b/examples/runners/wgpu/src/lib.rs index 4bdb7cd9cc..c19515d61b 100644 --- a/examples/runners/wgpu/src/lib.rs +++ b/examples/runners/wgpu/src/lib.rs @@ -153,7 +153,7 @@ fn maybe_watch( #[derive(StructOpt)] #[structopt(name = "example-runner-wgpu")] pub struct Options { - #[structopt(short, long, default_value = "Sky")] + #[structopt(short, long, default_value = "Mouse")] shader: RustGPUShader, }