Skip to content

Commit

Permalink
Remove TouchPhase from Android touch handling
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Aug 23, 2024
1 parent 2a62376 commit b07d223
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,43 +318,37 @@ impl EventLoop {
InputEvent::MotionEvent(motion_event) => {
let window_id = window::WindowId(WindowId);
let device_id = event::DeviceId(DeviceId(motion_event.device_id()));

let phase = match motion_event.action() {
MotionAction::Down | MotionAction::PointerDown => {
Some(event::TouchPhase::Started)
},
MotionAction::Up | MotionAction::PointerUp => Some(event::TouchPhase::Ended),
MotionAction::Move => Some(event::TouchPhase::Moved),
MotionAction::Cancel => Some(event::TouchPhase::Cancelled),
_ => {
None // TODO mouse events
let action = motion_event.action();

let pointers: Option<
Box<dyn Iterator<Item = android_activity::input::Pointer<'_>>>,
> = match action {
MotionAction::Down
| MotionAction::PointerDown
| MotionAction::Up
| MotionAction::PointerUp => Some(Box::new(std::iter::once(
motion_event.pointer_at_index(motion_event.pointer_index()),
))),
MotionAction::Move | MotionAction::Cancel => {
Some(Box::new(motion_event.pointers()))
},
// TODO mouse events
_ => None,
};
if let Some(phase) = phase {
let pointers: Box<dyn Iterator<Item = android_activity::input::Pointer<'_>>> =
match phase {
event::TouchPhase::Started | event::TouchPhase::Ended => {
Box::new(std::iter::once(
motion_event.pointer_at_index(motion_event.pointer_index()),
))
},
event::TouchPhase::Moved | event::TouchPhase::Cancelled => {
Box::new(motion_event.pointers())
},
};

if let Some(pointers) = pointers {
for pointer in pointers {
let position =
PhysicalPosition { x: pointer.x() as _, y: pointer.y() as _ };
trace!(
"Input event {device_id:?}, {phase:?}, loc={position:?}, \
"Input event {device_id:?}, {action:?}, loc={position:?}, \
pointer={pointer:?}"
);
let finger_id = event::FingerId(FingerId(pointer.pointer_id()));
let force = Some(Force::Normalized(pointer.pressure() as f64));

match phase {
event::TouchPhase::Started => {
match action {
MotionAction::Down | MotionAction::PointerDown => {
let event = event::WindowEvent::PointerEntered {
device_id,
position,
Expand All @@ -369,16 +363,16 @@ impl EventLoop {
};
app.window_event(&self.window_target, window_id, event);
},
event::TouchPhase::Moved => {
MotionAction::Move => {
let event = event::WindowEvent::PointerMoved {
device_id,
position,
source: event::PointerSource::Touch { finger_id, force },
};
app.window_event(&self.window_target, window_id, event);
},
event::TouchPhase::Ended | event::TouchPhase::Cancelled => {
if let event::TouchPhase::Ended = phase {
MotionAction::Up | MotionAction::PointerUp | MotionAction::Cancel => {
if let MotionAction::Up | MotionAction::PointerUp = action {
let event = event::WindowEvent::PointerButton {
device_id,
state: event::ElementState::Released,
Expand All @@ -395,6 +389,7 @@ impl EventLoop {
};
app.window_event(&self.window_target, window_id, event);
},
_ => unreachable!(),
}
}
}
Expand Down

0 comments on commit b07d223

Please sign in to comment.