Merge pull request #122 from fornwall/pointer-away-from-ndk

Avoid exposing Pointer and PointersIter from ndk
This commit is contained in:
Robert Bragg
2023-09-26 21:41:44 +01:00
committed by GitHub
3 changed files with 202 additions and 63 deletions
+18 -57
View File
@@ -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<Pointer<'a>> {
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
}
+106
View File
@@ -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<Pointer<'a>> {
self.inner.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a> ExactSizeIterator for PointersIter<'a> {
fn len(&self) -> usize {
self.inner.len()
}
}
+78 -6
View File
@@ -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<Pointer<'a>> {
self.ndk_pointers_iter.next().map(|ndk_pointer| Pointer {
inner: PointerImpl { ndk_pointer },
})
}
fn size_hint(&self) -> (usize, Option<usize>) {
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