Skip to content

Commit 9930b9b

Browse files
committed
Avoid exposing Pointer and PointersIter from ndk
1 parent 83cdb56 commit 9930b9b

File tree

3 files changed

+202
-63
lines changed

3 files changed

+202
-63
lines changed

android-activity/src/game_activity/input.rs

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::convert::TryInto;
1818
use crate::activity_impl::ffi::{GameActivityKeyEvent, GameActivityMotionEvent};
1919
use crate::input::{
2020
Axis, ButtonState, Class, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState,
21-
MotionAction, MotionEventFlags, Source, ToolType,
21+
MotionAction, MotionEventFlags, Pointer, PointersIter, Source, ToolType,
2222
};
2323

2424
// Note: try to keep this wrapper API compatible with the AInputEvent API if possible
@@ -116,9 +116,11 @@ impl<'a> MotionEvent<'a> {
116116
#[inline]
117117
pub fn pointers(&self) -> PointersIter<'_> {
118118
PointersIter {
119-
event: self,
120-
next_index: 0,
121-
count: self.pointer_count(),
119+
inner: PointersIterImpl {
120+
event: self,
121+
next_index: 0,
122+
count: self.pointer_count(),
123+
},
122124
}
123125
}
124126

@@ -130,7 +132,9 @@ impl<'a> MotionEvent<'a> {
130132
if index >= self.pointer_count() {
131133
panic!("Pointer index {} is out of bounds", index);
132134
}
133-
Pointer { event: self, index }
135+
Pointer {
136+
inner: PointerImpl { event: self, index },
137+
}
134138
}
135139

136140
/*
@@ -251,12 +255,12 @@ impl<'a> MotionEvent<'a> {
251255

252256
/// A view into the data of a specific pointer in a motion event.
253257
#[derive(Debug)]
254-
pub struct Pointer<'a> {
258+
pub(crate) struct PointerImpl<'a> {
255259
event: &'a MotionEvent<'a>,
256260
index: usize,
257261
}
258262

259-
impl<'a> Pointer<'a> {
263+
impl<'a> PointerImpl<'a> {
260264
#[inline]
261265
pub fn pointer_index(&self) -> usize {
262266
self.index
@@ -274,16 +278,6 @@ impl<'a> Pointer<'a> {
274278
pointer.axisValues[axis as u32 as usize]
275279
}
276280

277-
#[inline]
278-
pub fn orientation(&self) -> f32 {
279-
self.axis_value(Axis::Orientation)
280-
}
281-
282-
#[inline]
283-
pub fn pressure(&self) -> f32 {
284-
self.axis_value(Axis::Pressure)
285-
}
286-
287281
#[inline]
288282
pub fn raw_x(&self) -> f32 {
289283
let pointer = &self.event.ga_event.pointers[self.index];
@@ -296,41 +290,6 @@ impl<'a> Pointer<'a> {
296290
pointer.rawY
297291
}
298292

299-
#[inline]
300-
pub fn x(&self) -> f32 {
301-
self.axis_value(Axis::X)
302-
}
303-
304-
#[inline]
305-
pub fn y(&self) -> f32 {
306-
self.axis_value(Axis::Y)
307-
}
308-
309-
#[inline]
310-
pub fn size(&self) -> f32 {
311-
self.axis_value(Axis::Size)
312-
}
313-
314-
#[inline]
315-
pub fn tool_major(&self) -> f32 {
316-
self.axis_value(Axis::ToolMajor)
317-
}
318-
319-
#[inline]
320-
pub fn tool_minor(&self) -> f32 {
321-
self.axis_value(Axis::ToolMinor)
322-
}
323-
324-
#[inline]
325-
pub fn touch_major(&self) -> f32 {
326-
self.axis_value(Axis::TouchMajor)
327-
}
328-
329-
#[inline]
330-
pub fn touch_minor(&self) -> f32 {
331-
self.axis_value(Axis::TouchMinor)
332-
}
333-
334293
#[inline]
335294
pub fn tool_type(&self) -> ToolType {
336295
let pointer = &self.event.ga_event.pointers[self.index];
@@ -341,19 +300,21 @@ impl<'a> Pointer<'a> {
341300

342301
/// An iterator over the pointers in a [`MotionEvent`].
343302
#[derive(Debug)]
344-
pub struct PointersIter<'a> {
303+
pub(crate) struct PointersIterImpl<'a> {
345304
event: &'a MotionEvent<'a>,
346305
next_index: usize,
347306
count: usize,
348307
}
349308

350-
impl<'a> Iterator for PointersIter<'a> {
309+
impl<'a> Iterator for PointersIterImpl<'a> {
351310
type Item = Pointer<'a>;
352311
fn next(&mut self) -> Option<Pointer<'a>> {
353312
if self.next_index < self.count {
354313
let ptr = Pointer {
355-
event: self.event,
356-
index: self.next_index,
314+
inner: PointerImpl {
315+
event: self.event,
316+
index: self.next_index,
317+
},
357318
};
358319
self.next_index += 1;
359320
Some(ptr)
@@ -368,7 +329,7 @@ impl<'a> Iterator for PointersIter<'a> {
368329
}
369330
}
370331

371-
impl<'a> ExactSizeIterator for PointersIter<'a> {
332+
impl<'a> ExactSizeIterator for PointersIterImpl<'a> {
372333
fn len(&self) -> usize {
373334
self.count - self.next_index
374335
}

android-activity/src/input.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,3 +816,109 @@ impl<'a> InputIterator<'a> {
816816
self.inner.next(callback)
817817
}
818818
}
819+
820+
/// A view into the data of a specific pointer in a motion event.
821+
#[derive(Debug)]
822+
pub struct Pointer<'a> {
823+
pub(crate) inner: PointerImpl<'a>,
824+
}
825+
826+
impl<'a> Pointer<'a> {
827+
#[inline]
828+
pub fn pointer_index(&self) -> usize {
829+
self.inner.pointer_index()
830+
}
831+
832+
#[inline]
833+
pub fn pointer_id(&self) -> i32 {
834+
self.inner.pointer_id()
835+
}
836+
837+
#[inline]
838+
pub fn axis_value(&self, axis: Axis) -> f32 {
839+
self.inner.axis_value(axis)
840+
}
841+
842+
#[inline]
843+
pub fn orientation(&self) -> f32 {
844+
self.axis_value(Axis::Orientation)
845+
}
846+
847+
#[inline]
848+
pub fn pressure(&self) -> f32 {
849+
self.axis_value(Axis::Pressure)
850+
}
851+
852+
#[inline]
853+
pub fn raw_x(&self) -> f32 {
854+
self.inner.raw_x()
855+
}
856+
857+
#[inline]
858+
pub fn raw_y(&self) -> f32 {
859+
self.inner.raw_y()
860+
}
861+
862+
#[inline]
863+
pub fn x(&self) -> f32 {
864+
self.axis_value(Axis::X)
865+
}
866+
867+
#[inline]
868+
pub fn y(&self) -> f32 {
869+
self.axis_value(Axis::Y)
870+
}
871+
872+
#[inline]
873+
pub fn size(&self) -> f32 {
874+
self.axis_value(Axis::Size)
875+
}
876+
877+
#[inline]
878+
pub fn tool_major(&self) -> f32 {
879+
self.axis_value(Axis::ToolMajor)
880+
}
881+
882+
#[inline]
883+
pub fn tool_minor(&self) -> f32 {
884+
self.axis_value(Axis::ToolMinor)
885+
}
886+
887+
#[inline]
888+
pub fn touch_major(&self) -> f32 {
889+
self.axis_value(Axis::TouchMajor)
890+
}
891+
892+
#[inline]
893+
pub fn touch_minor(&self) -> f32 {
894+
self.axis_value(Axis::TouchMinor)
895+
}
896+
897+
#[inline]
898+
pub fn tool_type(&self) -> ToolType {
899+
self.inner.tool_type()
900+
}
901+
}
902+
903+
/// An iterator over the pointers in a [`MotionEvent`].
904+
#[derive(Debug)]
905+
pub struct PointersIter<'a> {
906+
pub(crate) inner: PointersIterImpl<'a>,
907+
}
908+
909+
impl<'a> Iterator for PointersIter<'a> {
910+
type Item = Pointer<'a>;
911+
fn next(&mut self) -> Option<Pointer<'a>> {
912+
self.inner.next()
913+
}
914+
915+
fn size_hint(&self) -> (usize, Option<usize>) {
916+
self.inner.size_hint()
917+
}
918+
}
919+
920+
impl<'a> ExactSizeIterator for PointersIter<'a> {
921+
fn len(&self) -> usize {
922+
self.inner.len()
923+
}
924+
}

android-activity/src/native_activity/input.rs

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use std::marker::PhantomData;
22

3-
pub use ndk::event::{Pointer, PointersIter};
4-
53
use crate::input::{
6-
ButtonState, Class, EdgeFlags, KeyAction, Keycode, MetaState, MotionAction, MotionEventFlags,
7-
Source,
4+
Axis, ButtonState, Class, EdgeFlags, KeyAction, Keycode, MetaState, MotionAction,
5+
MotionEventFlags, Pointer, PointersIter, Source, ToolType,
86
};
97

108
/// A motion event
@@ -99,15 +97,23 @@ impl<'a> MotionEvent<'a> {
9997
/// An iterator over the pointers in this motion event
10098
#[inline]
10199
pub fn pointers(&self) -> PointersIter<'_> {
102-
self.ndk_event.pointers()
100+
PointersIter {
101+
inner: PointersIterImpl {
102+
ndk_pointers_iter: self.ndk_event.pointers(),
103+
},
104+
}
103105
}
104106

105107
/// The pointer at a given pointer index. Panics if the pointer index is out of bounds.
106108
///
107109
/// If you need to loop over all the pointers, prefer the [`pointers()`](Self::pointers) method.
108110
#[inline]
109111
pub fn pointer_at_index(&self, index: usize) -> Pointer<'_> {
110-
self.ndk_event.pointer_at_index(index)
112+
Pointer {
113+
inner: PointerImpl {
114+
ndk_pointer: self.ndk_event.pointer_at_index(index),
115+
},
116+
}
111117
}
112118

113119
/*
@@ -224,6 +230,72 @@ impl<'a> MotionEvent<'a> {
224230
}
225231
}
226232

233+
/// A view into the data of a specific pointer in a motion event.
234+
#[derive(Debug)]
235+
pub(crate) struct PointerImpl<'a> {
236+
ndk_pointer: ndk::event::Pointer<'a>,
237+
}
238+
239+
impl<'a> PointerImpl<'a> {
240+
#[inline]
241+
pub fn pointer_index(&self) -> usize {
242+
self.ndk_pointer.pointer_index()
243+
}
244+
245+
#[inline]
246+
pub fn pointer_id(&self) -> i32 {
247+
self.ndk_pointer.pointer_id()
248+
}
249+
250+
#[inline]
251+
pub fn axis_value(&self, axis: Axis) -> f32 {
252+
let value: u32 = axis.into();
253+
let ndk_axis = value.try_into().unwrap();
254+
self.ndk_pointer.axis_value(ndk_axis)
255+
}
256+
257+
#[inline]
258+
pub fn raw_x(&self) -> f32 {
259+
self.ndk_pointer.raw_x()
260+
}
261+
262+
#[inline]
263+
pub fn raw_y(&self) -> f32 {
264+
self.ndk_pointer.raw_y()
265+
}
266+
267+
#[inline]
268+
pub fn tool_type(&self) -> ToolType {
269+
let value: u32 = self.ndk_pointer.tool_type().into();
270+
value.try_into().unwrap()
271+
}
272+
}
273+
274+
/// An iterator over the pointers in a [`MotionEvent`].
275+
#[derive(Debug)]
276+
pub(crate) struct PointersIterImpl<'a> {
277+
ndk_pointers_iter: ndk::event::PointersIter<'a>,
278+
}
279+
280+
impl<'a> Iterator for PointersIterImpl<'a> {
281+
type Item = Pointer<'a>;
282+
fn next(&mut self) -> Option<Pointer<'a>> {
283+
self.ndk_pointers_iter.next().map(|ndk_pointer| Pointer {
284+
inner: PointerImpl { ndk_pointer },
285+
})
286+
}
287+
288+
fn size_hint(&self) -> (usize, Option<usize>) {
289+
self.ndk_pointers_iter.size_hint()
290+
}
291+
}
292+
293+
impl<'a> ExactSizeIterator for PointersIterImpl<'a> {
294+
fn len(&self) -> usize {
295+
self.ndk_pointers_iter.len()
296+
}
297+
}
298+
227299
/// A key event
228300
///
229301
/// For general discussion of key events in Android, see [the relevant

0 commit comments

Comments
 (0)