Skip to content

Commit 8e13fce

Browse files
authored
chore: Touch to Mouse translation cleanup (#356)
1 parent 51720a7 commit 8e13fce

1 file changed

Lines changed: 45 additions & 38 deletions

File tree

neothesia/src/main.rs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,43 +69,11 @@ impl Neothesia {
6969
&mut self,
7070
event_loop: &winit::event_loop::ActiveEventLoop,
7171
_window_id: winit::window::WindowId,
72-
event: WindowEvent,
72+
event: &WindowEvent,
7373
) {
74-
// Touch event to mouse event translation
75-
// TODO: Why is this needed if we have Touch event handling implemented in utils/window.rs?
76-
if let WindowEvent::Touch(touch) = &event {
77-
let cursor_ev = WindowEvent::CursorMoved {
78-
device_id: touch.device_id,
79-
position: touch.location,
80-
};
81-
82-
self.context.window_state.window_event(&cursor_ev);
83-
self.game_scene.window_event(&mut self.context, &cursor_ev);
84-
85-
let maybe_state = match touch.phase {
86-
TouchPhase::Started => Some(ElementState::Pressed),
87-
TouchPhase::Ended | TouchPhase::Cancelled => Some(ElementState::Released),
88-
TouchPhase::Moved => None,
89-
};
90-
91-
if let Some(state) = maybe_state {
92-
let mouse_ev = WindowEvent::MouseInput {
93-
device_id: touch.device_id,
94-
state,
95-
button: MouseButton::Left,
96-
};
97-
98-
self.context.window_state.window_event(&mouse_ev);
99-
self.game_scene.window_event(&mut self.context, &mouse_ev);
100-
}
101-
102-
// Don’t pass through the raw Touch event.
103-
return;
104-
}
105-
106-
self.context.window_state.window_event(&event);
74+
self.context.window_state.window_event(event);
10775

108-
match &event {
76+
match event {
10977
// Windows sets size to 0 on minimise
11078
WindowEvent::Resized(ps) if ps.width > 0 && ps.height > 0 => {
11179
self.surface.resize_swap_chain(
@@ -157,7 +125,7 @@ impl Neothesia {
157125
}
158126

159127
if !event.redraw_requested() {
160-
self.game_scene.window_event(&mut self.context, &event);
128+
self.game_scene.window_event(&mut self.context, event);
161129
}
162130
}
163131

@@ -330,8 +298,47 @@ impl ApplicationHandler<NeothesiaEvent> for NeothesiaBootstrap {
330298
window_id: winit::window::WindowId,
331299
event: WindowEvent,
332300
) {
333-
if let Some(app) = self.0.as_mut() {
334-
app.window_event(event_loop, window_id, event)
301+
let Some(app) = self.0.as_mut() else {
302+
return;
303+
};
304+
305+
let mut on_event = |event: WindowEvent| {
306+
app.window_event(event_loop, window_id, &event);
307+
};
308+
309+
// Touch event to mouse event translation (temporary until we get touch support)
310+
if let WindowEvent::Touch(touch) = &event {
311+
// TODO: What to do with touch.id? We somehow want to ignore multitouch
312+
313+
match touch.phase {
314+
TouchPhase::Started => {
315+
// Touch might happen anywhere on the screen, so send moved event
316+
on_event(WindowEvent::CursorMoved {
317+
device_id: touch.device_id,
318+
position: touch.location,
319+
});
320+
on_event(WindowEvent::MouseInput {
321+
device_id: touch.device_id,
322+
state: ElementState::Pressed,
323+
button: MouseButton::Left,
324+
});
325+
}
326+
TouchPhase::Ended | TouchPhase::Cancelled => {
327+
on_event(WindowEvent::MouseInput {
328+
device_id: touch.device_id,
329+
state: ElementState::Released,
330+
button: MouseButton::Left,
331+
});
332+
}
333+
TouchPhase::Moved => {
334+
on_event(WindowEvent::CursorMoved {
335+
device_id: touch.device_id,
336+
position: touch.location,
337+
});
338+
}
339+
}
340+
} else {
341+
app.window_event(event_loop, window_id, &event)
335342
}
336343
}
337344

0 commit comments

Comments
 (0)