Skip to content

Commit

Permalink
Avoid exposing Pointer and PointersIter from ndk
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Sep 26, 2023
1 parent 83cdb56 commit 5560a71
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 64 deletions.
77 changes: 19 additions & 58 deletions android-activity/src/game_activity/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,7 +37,7 @@ pub enum InputEvent<'a> {
/// javadoc](https://developer.android.com/reference/android/view/MotionEvent).
#[derive(Clone, Debug)]
pub struct MotionEvent<'a> {
ga_event: &'a GameActivityMotionEvent,
pub(crate) ga_event: &'a GameActivityMotionEvent,
}

impl<'a> MotionEvent<'a> {
Expand Down Expand Up @@ -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(),
},
}
}

Expand All @@ -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 },
}
}

/*
Expand Down Expand Up @@ -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
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
106 changes: 106 additions & 0 deletions android-activity/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
84 changes: 78 additions & 6 deletions android-activity/src/native_activity/input.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -99,15 +97,23 @@ 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.
///
/// 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),
},
}
}

/*
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5560a71

Please sign in to comment.