From 91ca400176072acef3becabe40be0e665032f991 Mon Sep 17 00:00:00 2001 From: Sigmoidle Date: Thu, 29 Feb 2024 19:46:29 +0000 Subject: [PATCH 1/4] Change surface_usage Removed wgpu::TextureUsages::COPY_SRC --- comfy-wgpu/src/device.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/comfy-wgpu/src/device.rs b/comfy-wgpu/src/device.rs index b44fcb2..0bdbcb5 100644 --- a/comfy-wgpu/src/device.rs +++ b/comfy-wgpu/src/device.rs @@ -94,8 +94,7 @@ pub async fn create_graphics_context(window: &Window) -> GraphicsContext { }; #[cfg(not(target_arch = "wasm32"))] - let surface_usage = - wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC; + let surface_usage = wgpu::TextureUsages::RENDER_ATTACHMENT; #[cfg(target_arch = "wasm32")] let surface_usage = wgpu::TextureUsages::RENDER_ATTACHMENT; From 7d87911d883cf5a72093904570a2fd130b7ecc73 Mon Sep 17 00:00:00 2001 From: Sigmoidle Date: Fri, 1 Mar 2024 11:42:23 +0000 Subject: [PATCH 2/4] Try 1 at adding touch support Untested --- comfy-core/src/global_state.rs | 7 ++++++- comfy-core/src/input.rs | 15 +++++++++++++++ comfy/src/game_loop.rs | 17 ++++++++++++++--- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/comfy-core/src/global_state.rs b/comfy-core/src/global_state.rs index 72ae993..9d6819b 100644 --- a/comfy-core/src/global_state.rs +++ b/comfy-core/src/global_state.rs @@ -1,5 +1,7 @@ use std::sync::atomic::{AtomicU32, AtomicU64, AtomicUsize, Ordering}; +use winit::event::TouchPhase; + use crate::*; static EGUI_CONTEXT: Lazy = Lazy::new(egui::Context::default); @@ -49,7 +51,6 @@ pub fn inc_assets_loaded(newly_loaded_count: usize) { ASSETS_LOADED.fetch_add(newly_loaded_count, Ordering::SeqCst); } - pub fn frame_time() -> f32 { f32::from_bits(FRAME_TIME.load(Ordering::SeqCst)) } @@ -103,6 +104,10 @@ pub struct GlobalState { pub mouse_locked: bool, pub cursor_hidden: bool, + pub touch_location: Vec2, + pub touch_id: u64, + pub touch_phase: HashSet, + pub egui_scale_factor: f32, pub frame: u32, diff --git a/comfy-core/src/input.rs b/comfy-core/src/input.rs index 4062532..cf1558f 100644 --- a/comfy-core/src/input.rs +++ b/comfy-core/src/input.rs @@ -1,3 +1,5 @@ +use winit::event::TouchPhase; + use crate::*; pub fn mouse_wheel() -> (f32, f32) { @@ -36,6 +38,19 @@ pub fn is_key_down(keycode: KeyCode) -> bool { GLOBAL_STATE.borrow().pressed.contains(&keycode) } +pub fn get_touch_location() -> Vec2 { + GLOBAL_STATE.borrow().touch_location +} + +pub fn get_touch_id() -> u64 { + GLOBAL_STATE.borrow().touch_id +} + +pub fn get_touch_phase() -> Option { + let touch_phase = &GLOBAL_STATE.borrow().touch_phase; + touch_phase.iter().next().cloned() +} + #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum MouseButton { Left, diff --git a/comfy/src/game_loop.rs b/comfy/src/game_loop.rs index 0550883..01160d5 100644 --- a/comfy/src/game_loop.rs +++ b/comfy/src/game_loop.rs @@ -171,6 +171,7 @@ pub async fn run_comfy_main_async( global_state.just_released.clear(); global_state.mouse_just_pressed.clear(); global_state.mouse_just_released.clear(); + global_state.touch_phase.clear(); global_state.mouse_wheel = (0.0, 0.0); engine @@ -181,7 +182,6 @@ pub async fn run_comfy_main_async( .set_cursor_visible(!global_state.cursor_hidden); } - set_frame_time(frame_start.elapsed().as_secs_f32()); inc_frame_num(); @@ -230,6 +230,17 @@ pub async fn run_comfy_main_async( } } + WindowEvent::Touch(touch) => { + let mut state = GLOBAL_STATE.borrow_mut(); + + state.touch_location = Vec2 { + x: touch.location.x as f32, + y: touch.location.y as f32, + }; + state.touch_id = touch.id; + state.touch_phase.insert(touch.phase); + } + WindowEvent::CursorMoved { position, .. } => { GLOBAL_STATE.borrow_mut().mouse_position = vec2(position.x as f32, position.y as f32); @@ -290,8 +301,8 @@ pub async fn run_comfy_main_async( } WindowEvent::Resized(physical_size) => { - if physical_size.width > min_resolution.0 && - physical_size.height > min_resolution.1 + if physical_size.width > min_resolution.0 + && physical_size.height > min_resolution.1 { engine.resize(uvec2( physical_size.width, From 182e6592fdb21bab276aab57de832d7aee8ce560 Mon Sep 17 00:00:00 2001 From: Sigmoidle Date: Fri, 1 Mar 2024 14:56:40 +0000 Subject: [PATCH 3/4] Try 2: Added multi-touch support Untested --- comfy-core/src/global_state.rs | 6 +----- comfy-core/src/input.rs | 15 ++------------- comfy/src/game_loop.rs | 33 +++++++++++++++++++++++---------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/comfy-core/src/global_state.rs b/comfy-core/src/global_state.rs index 9d6819b..deba109 100644 --- a/comfy-core/src/global_state.rs +++ b/comfy-core/src/global_state.rs @@ -1,7 +1,5 @@ use std::sync::atomic::{AtomicU32, AtomicU64, AtomicUsize, Ordering}; -use winit::event::TouchPhase; - use crate::*; static EGUI_CONTEXT: Lazy = Lazy::new(egui::Context::default); @@ -104,9 +102,7 @@ pub struct GlobalState { pub mouse_locked: bool, pub cursor_hidden: bool, - pub touch_location: Vec2, - pub touch_id: u64, - pub touch_phase: HashSet, + pub touch_locations: HashMap, pub egui_scale_factor: f32, diff --git a/comfy-core/src/input.rs b/comfy-core/src/input.rs index cf1558f..bc4c1ce 100644 --- a/comfy-core/src/input.rs +++ b/comfy-core/src/input.rs @@ -1,5 +1,3 @@ -use winit::event::TouchPhase; - use crate::*; pub fn mouse_wheel() -> (f32, f32) { @@ -38,17 +36,8 @@ pub fn is_key_down(keycode: KeyCode) -> bool { GLOBAL_STATE.borrow().pressed.contains(&keycode) } -pub fn get_touch_location() -> Vec2 { - GLOBAL_STATE.borrow().touch_location -} - -pub fn get_touch_id() -> u64 { - GLOBAL_STATE.borrow().touch_id -} - -pub fn get_touch_phase() -> Option { - let touch_phase = &GLOBAL_STATE.borrow().touch_phase; - touch_phase.iter().next().cloned() +pub fn get_touch_location() -> HashMap { + GLOBAL_STATE.borrow().touch_locations.clone() } #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] diff --git a/comfy/src/game_loop.rs b/comfy/src/game_loop.rs index 01160d5..929faa7 100644 --- a/comfy/src/game_loop.rs +++ b/comfy/src/game_loop.rs @@ -171,7 +171,6 @@ pub async fn run_comfy_main_async( global_state.just_released.clear(); global_state.mouse_just_pressed.clear(); global_state.mouse_just_released.clear(); - global_state.touch_phase.clear(); global_state.mouse_wheel = (0.0, 0.0); engine @@ -230,15 +229,29 @@ pub async fn run_comfy_main_async( } } - WindowEvent::Touch(touch) => { - let mut state = GLOBAL_STATE.borrow_mut(); - - state.touch_location = Vec2 { - x: touch.location.x as f32, - y: touch.location.y as f32, - }; - state.touch_id = touch.id; - state.touch_phase.insert(touch.phase); + WindowEvent::Touch(touch_event) => { + match touch_event.phase { + winit::event::TouchPhase::Started + | winit::event::TouchPhase::Moved => { + GLOBAL_STATE + .borrow_mut() + .touch_locations + .insert( + touch_event.id, + Vec2 { + x: touch_event.location.x as f32, + y: touch_event.location.y as f32, + }, + ); + } + winit::event::TouchPhase::Ended + | winit::event::TouchPhase::Cancelled => { + GLOBAL_STATE + .borrow_mut() + .touch_locations + .remove(&touch_event.id); + } + } } WindowEvent::CursorMoved { position, .. } => { From 05dfbf46c6d37cfa8f878818a042d19a600eb606 Mon Sep 17 00:00:00 2001 From: Sigmoidle Date: Fri, 1 Mar 2024 15:20:07 +0000 Subject: [PATCH 4/4] Updated get_touch_locations() api It now returns a vector of Vec2 --- comfy-core/src/input.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comfy-core/src/input.rs b/comfy-core/src/input.rs index bc4c1ce..0a4ad7c 100644 --- a/comfy-core/src/input.rs +++ b/comfy-core/src/input.rs @@ -36,8 +36,8 @@ pub fn is_key_down(keycode: KeyCode) -> bool { GLOBAL_STATE.borrow().pressed.contains(&keycode) } -pub fn get_touch_location() -> HashMap { - GLOBAL_STATE.borrow().touch_locations.clone() +pub fn get_touch_locations() -> Vec { + GLOBAL_STATE.borrow().touch_locations.clone().into_values().collect() } #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]