Skip to content
Merged
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
37 changes: 36 additions & 1 deletion neothesia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use neothesia_core::{config, render};
use wgpu_jumpstart::{Gpu, Surface, TransformUniform};
use winit::{
application::ApplicationHandler,
event::WindowEvent,
event::{ElementState, MouseButton, TouchPhase, WindowEvent},
event_loop::{EventLoop, EventLoopProxy},
keyboard::NamedKey,
};
Expand Down Expand Up @@ -71,6 +71,41 @@ impl Neothesia {
_window_id: winit::window::WindowId,
event: WindowEvent,
) {
if let WindowEvent::Touch(touch) = &event {

// 1) Feed a CursorMoved equivalent (important for hit testing)
let cursor_ev = WindowEvent::CursorMoved {
device_id: touch.device_id,
position: touch.location,
// If your winit version requires extra fields here (e.g. modifiers),
// the compiler will tell you. Add them based on the type error.
};

self.context.window_state.window_event(&cursor_ev);
self.game_scene.window_event(&mut self.context, &cursor_ev);

// 2) Feed a synthetic left mouse press/release on touch start/end
let maybe_state = match touch.phase {
TouchPhase::Started => Some(ElementState::Pressed),
TouchPhase::Ended | TouchPhase::Cancelled => Some(ElementState::Released),
TouchPhase::Moved => None,
};

if let Some(state) = maybe_state {
let mouse_ev = WindowEvent::MouseInput {
device_id: touch.device_id,
state,
button: MouseButton::Left,
// Same note: if your winit version requires modifiers here, add them.
};

self.context.window_state.window_event(&mouse_ev);
self.game_scene.window_event(&mut self.context, &mouse_ev);
}

// Don’t also pass through the raw Touch event.
return;
}
self.context.window_state.window_event(&event);

match &event {
Expand Down
29 changes: 28 additions & 1 deletion neothesia/src/utils/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use winit::{

use winit::{
dpi::{LogicalSize, PhysicalSize},
event::WindowEvent,
event::{WindowEvent, TouchPhase},
};


pub struct WindowState {
pub physical_size: PhysicalSize<u32>,
pub logical_size: LogicalSize<f32>,
Expand Down Expand Up @@ -91,6 +92,25 @@ impl WindowState {
} => {
self.right_mouse_btn = *state == ElementState::Pressed;
}
WindowEvent::Touch(touch) => {
// Touch location is a PhysicalPosition<f64>
self.cursor_physical_position = touch.location;
self.cursor_logical_position = touch.location.to_logical(self.scale_factor);

match touch.phase {
TouchPhase::Started => {
// Finger down -> treat like left button press
self.left_mouse_btn = true;
}
TouchPhase::Ended | TouchPhase::Cancelled => {
// Finger up/cancel -> treat like left button release
self.left_mouse_btn = false;
}
TouchPhase::Moved => {
// Just movement; button state unchanged
}
}
}
_ => {}
}
}
Expand Down Expand Up @@ -160,6 +180,9 @@ impl WinitEvent for WindowEvent {
button,
..
} => button == &btn,
Self::Touch(touch) => {
btn == MouseButton::Left && matches!(touch.phase, TouchPhase::Started)
}
_ => false,
}
}
Expand All @@ -171,6 +194,10 @@ impl WinitEvent for WindowEvent {
button,
..
} => button == &btn,
Self::Touch(touch) => {
btn == MouseButton::Left
&& matches!(touch.phase, TouchPhase::Ended | TouchPhase::Cancelled)
}
_ => false,
}
}
Expand Down