Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Add Touch Support #93

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion comfy-core/src/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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))
}
Expand Down Expand Up @@ -103,6 +102,8 @@ pub struct GlobalState {
pub mouse_locked: bool,
pub cursor_hidden: bool,

pub touch_locations: HashMap<u64, Vec2>,

pub egui_scale_factor: f32,

pub frame: u32,
Expand Down
4 changes: 4 additions & 0 deletions comfy-core/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub fn is_key_down(keycode: KeyCode) -> bool {
GLOBAL_STATE.borrow().pressed.contains(&keycode)
}

pub fn get_touch_locations() -> Vec<Vec2> {
GLOBAL_STATE.borrow().touch_locations.clone().into_values().collect()
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum MouseButton {
Left,
Expand Down
3 changes: 1 addition & 2 deletions comfy-wgpu/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
30 changes: 27 additions & 3 deletions comfy/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,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();

Expand Down Expand Up @@ -230,6 +229,31 @@ pub async fn run_comfy_main_async(
}
}

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, .. } => {
GLOBAL_STATE.borrow_mut().mouse_position =
vec2(position.x as f32, position.y as f32);
Expand Down Expand Up @@ -290,8 +314,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,
Expand Down
Loading