From 9930b9bf90a9fd547aaf71e5f78a98e13c2a4439 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Tue, 26 Sep 2023 08:26:20 +0200 Subject: [PATCH] Avoid exposing Pointer and PointersIter from ndk --- android-activity/src/game_activity/input.rs | 75 +++---------- android-activity/src/input.rs | 106 ++++++++++++++++++ android-activity/src/native_activity/input.rs | 84 +++++++++++++- 3 files changed, 202 insertions(+), 63 deletions(-) diff --git a/android-activity/src/game_activity/input.rs b/android-activity/src/game_activity/input.rs index b5d5407..957e24b 100644 --- a/android-activity/src/game_activity/input.rs +++ b/android-activity/src/game_activity/input.rs @@ -18,7 +18,7 @@ use std::convert::TryInto; use crate::activity_impl::ffi::{GameActivityKeyEvent, GameActivityMotionEvent}; use crate::input::{ Axis, ButtonState, Class, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, - MotionAction, MotionEventFlags, Source, ToolType, + MotionAction, MotionEventFlags, Pointer, PointersIter, Source, ToolType, }; // Note: try to keep this wrapper API compatible with the AInputEvent API if possible @@ -116,9 +116,11 @@ impl<'a> MotionEvent<'a> { #[inline] pub fn pointers(&self) -> PointersIter<'_> { PointersIter { - event: self, - next_index: 0, - count: self.pointer_count(), + inner: PointersIterImpl { + event: self, + next_index: 0, + count: self.pointer_count(), + }, } } @@ -130,7 +132,9 @@ impl<'a> MotionEvent<'a> { if index >= self.pointer_count() { panic!("Pointer index {} is out of bounds", index); } - Pointer { event: self, index } + Pointer { + inner: PointerImpl { event: self, index }, + } } /* @@ -251,12 +255,12 @@ impl<'a> MotionEvent<'a> { /// A view into the data of a specific pointer in a motion event. #[derive(Debug)] -pub struct Pointer<'a> { +pub(crate) struct PointerImpl<'a> { event: &'a MotionEvent<'a>, index: usize, } -impl<'a> Pointer<'a> { +impl<'a> PointerImpl<'a> { #[inline] pub fn pointer_index(&self) -> usize { self.index @@ -274,16 +278,6 @@ impl<'a> Pointer<'a> { pointer.axisValues[axis as u32 as usize] } - #[inline] - pub fn orientation(&self) -> f32 { - self.axis_value(Axis::Orientation) - } - - #[inline] - pub fn pressure(&self) -> f32 { - self.axis_value(Axis::Pressure) - } - #[inline] pub fn raw_x(&self) -> f32 { let pointer = &self.event.ga_event.pointers[self.index]; @@ -296,41 +290,6 @@ impl<'a> Pointer<'a> { pointer.rawY } - #[inline] - pub fn x(&self) -> f32 { - self.axis_value(Axis::X) - } - - #[inline] - pub fn y(&self) -> f32 { - self.axis_value(Axis::Y) - } - - #[inline] - pub fn size(&self) -> f32 { - self.axis_value(Axis::Size) - } - - #[inline] - pub fn tool_major(&self) -> f32 { - self.axis_value(Axis::ToolMajor) - } - - #[inline] - pub fn tool_minor(&self) -> f32 { - self.axis_value(Axis::ToolMinor) - } - - #[inline] - pub fn touch_major(&self) -> f32 { - self.axis_value(Axis::TouchMajor) - } - - #[inline] - pub fn touch_minor(&self) -> f32 { - self.axis_value(Axis::TouchMinor) - } - #[inline] pub fn tool_type(&self) -> ToolType { let pointer = &self.event.ga_event.pointers[self.index]; @@ -341,19 +300,21 @@ impl<'a> Pointer<'a> { /// An iterator over the pointers in a [`MotionEvent`]. #[derive(Debug)] -pub struct PointersIter<'a> { +pub(crate) struct PointersIterImpl<'a> { event: &'a MotionEvent<'a>, next_index: usize, count: usize, } -impl<'a> Iterator for PointersIter<'a> { +impl<'a> Iterator for PointersIterImpl<'a> { type Item = Pointer<'a>; fn next(&mut self) -> Option> { if self.next_index < self.count { let ptr = Pointer { - event: self.event, - index: self.next_index, + inner: PointerImpl { + event: self.event, + index: self.next_index, + }, }; self.next_index += 1; Some(ptr) @@ -368,7 +329,7 @@ impl<'a> Iterator for PointersIter<'a> { } } -impl<'a> ExactSizeIterator for PointersIter<'a> { +impl<'a> ExactSizeIterator for PointersIterImpl<'a> { fn len(&self) -> usize { self.count - self.next_index } diff --git a/android-activity/src/input.rs b/android-activity/src/input.rs index 743244f..55390f8 100644 --- a/android-activity/src/input.rs +++ b/android-activity/src/input.rs @@ -816,3 +816,109 @@ impl<'a> InputIterator<'a> { self.inner.next(callback) } } + +/// A view into the data of a specific pointer in a motion event. +#[derive(Debug)] +pub struct Pointer<'a> { + pub(crate) inner: PointerImpl<'a>, +} + +impl<'a> Pointer<'a> { + #[inline] + pub fn pointer_index(&self) -> usize { + self.inner.pointer_index() + } + + #[inline] + pub fn pointer_id(&self) -> i32 { + self.inner.pointer_id() + } + + #[inline] + pub fn axis_value(&self, axis: Axis) -> f32 { + self.inner.axis_value(axis) + } + + #[inline] + pub fn orientation(&self) -> f32 { + self.axis_value(Axis::Orientation) + } + + #[inline] + pub fn pressure(&self) -> f32 { + self.axis_value(Axis::Pressure) + } + + #[inline] + pub fn raw_x(&self) -> f32 { + self.inner.raw_x() + } + + #[inline] + pub fn raw_y(&self) -> f32 { + self.inner.raw_y() + } + + #[inline] + pub fn x(&self) -> f32 { + self.axis_value(Axis::X) + } + + #[inline] + pub fn y(&self) -> f32 { + self.axis_value(Axis::Y) + } + + #[inline] + pub fn size(&self) -> f32 { + self.axis_value(Axis::Size) + } + + #[inline] + pub fn tool_major(&self) -> f32 { + self.axis_value(Axis::ToolMajor) + } + + #[inline] + pub fn tool_minor(&self) -> f32 { + self.axis_value(Axis::ToolMinor) + } + + #[inline] + pub fn touch_major(&self) -> f32 { + self.axis_value(Axis::TouchMajor) + } + + #[inline] + pub fn touch_minor(&self) -> f32 { + self.axis_value(Axis::TouchMinor) + } + + #[inline] + pub fn tool_type(&self) -> ToolType { + self.inner.tool_type() + } +} + +/// An iterator over the pointers in a [`MotionEvent`]. +#[derive(Debug)] +pub struct PointersIter<'a> { + pub(crate) inner: PointersIterImpl<'a>, +} + +impl<'a> Iterator for PointersIter<'a> { + type Item = Pointer<'a>; + fn next(&mut self) -> Option> { + self.inner.next() + } + + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } +} + +impl<'a> ExactSizeIterator for PointersIter<'a> { + fn len(&self) -> usize { + self.inner.len() + } +} diff --git a/android-activity/src/native_activity/input.rs b/android-activity/src/native_activity/input.rs index ca32b7f..8a2d0b2 100644 --- a/android-activity/src/native_activity/input.rs +++ b/android-activity/src/native_activity/input.rs @@ -1,10 +1,8 @@ use std::marker::PhantomData; -pub use ndk::event::{Pointer, PointersIter}; - use crate::input::{ - ButtonState, Class, EdgeFlags, KeyAction, Keycode, MetaState, MotionAction, MotionEventFlags, - Source, + Axis, ButtonState, Class, EdgeFlags, KeyAction, Keycode, MetaState, MotionAction, + MotionEventFlags, Pointer, PointersIter, Source, ToolType, }; /// A motion event @@ -99,7 +97,11 @@ impl<'a> MotionEvent<'a> { /// An iterator over the pointers in this motion event #[inline] pub fn pointers(&self) -> PointersIter<'_> { - self.ndk_event.pointers() + PointersIter { + inner: PointersIterImpl { + ndk_pointers_iter: self.ndk_event.pointers(), + }, + } } /// The pointer at a given pointer index. Panics if the pointer index is out of bounds. @@ -107,7 +109,11 @@ impl<'a> MotionEvent<'a> { /// If you need to loop over all the pointers, prefer the [`pointers()`](Self::pointers) method. #[inline] pub fn pointer_at_index(&self, index: usize) -> Pointer<'_> { - self.ndk_event.pointer_at_index(index) + Pointer { + inner: PointerImpl { + ndk_pointer: self.ndk_event.pointer_at_index(index), + }, + } } /* @@ -224,6 +230,72 @@ impl<'a> MotionEvent<'a> { } } +/// A view into the data of a specific pointer in a motion event. +#[derive(Debug)] +pub(crate) struct PointerImpl<'a> { + ndk_pointer: ndk::event::Pointer<'a>, +} + +impl<'a> PointerImpl<'a> { + #[inline] + pub fn pointer_index(&self) -> usize { + self.ndk_pointer.pointer_index() + } + + #[inline] + pub fn pointer_id(&self) -> i32 { + self.ndk_pointer.pointer_id() + } + + #[inline] + pub fn axis_value(&self, axis: Axis) -> f32 { + let value: u32 = axis.into(); + let ndk_axis = value.try_into().unwrap(); + self.ndk_pointer.axis_value(ndk_axis) + } + + #[inline] + pub fn raw_x(&self) -> f32 { + self.ndk_pointer.raw_x() + } + + #[inline] + pub fn raw_y(&self) -> f32 { + self.ndk_pointer.raw_y() + } + + #[inline] + pub fn tool_type(&self) -> ToolType { + let value: u32 = self.ndk_pointer.tool_type().into(); + value.try_into().unwrap() + } +} + +/// An iterator over the pointers in a [`MotionEvent`]. +#[derive(Debug)] +pub(crate) struct PointersIterImpl<'a> { + ndk_pointers_iter: ndk::event::PointersIter<'a>, +} + +impl<'a> Iterator for PointersIterImpl<'a> { + type Item = Pointer<'a>; + fn next(&mut self) -> Option> { + self.ndk_pointers_iter.next().map(|ndk_pointer| Pointer { + inner: PointerImpl { ndk_pointer }, + }) + } + + fn size_hint(&self) -> (usize, Option) { + self.ndk_pointers_iter.size_hint() + } +} + +impl<'a> ExactSizeIterator for PointersIterImpl<'a> { + fn len(&self) -> usize { + self.ndk_pointers_iter.len() + } +} + /// A key event /// /// For general discussion of key events in Android, see [the relevant