Skip to content

Commit 96497f9

Browse files
authored
Merge pull request #100 from rust-mobile/rib/pr/game-activity-no-input-deref
game-activity: Remove Deref implementations for Key/MotionEvent types
2 parents 9bb5f9c + c22a545 commit 96497f9

File tree

1 file changed

+39
-55
lines changed
  • android-activity/src/game_activity

1 file changed

+39
-55
lines changed

android-activity/src/game_activity/input.rs

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// by masking bits from the `Source`.
1515

1616
use num_enum::{IntoPrimitive, TryFromPrimitive};
17-
use std::{convert::TryInto, ops::Deref};
17+
use std::convert::TryInto;
1818

1919
use crate::game_activity::ffi::{GameActivityKeyEvent, GameActivityMotionEvent};
2020
use crate::input::{Class, Source};
@@ -114,14 +114,6 @@ pub struct MotionEvent<'a> {
114114
ga_event: &'a GameActivityMotionEvent,
115115
}
116116

117-
impl<'a> Deref for MotionEvent<'a> {
118-
type Target = GameActivityMotionEvent;
119-
120-
fn deref(&self) -> &Self::Target {
121-
self.ga_event
122-
}
123-
}
124-
125117
/// A motion action.
126118
///
127119
/// See [the NDK
@@ -295,7 +287,7 @@ impl<'a> MotionEvent<'a> {
295287
///
296288
#[inline]
297289
pub fn source(&self) -> Source {
298-
let source = self.source as u32;
290+
let source = self.ga_event.source as u32;
299291
source.try_into().unwrap_or(Source::Unknown)
300292
}
301293

@@ -310,15 +302,15 @@ impl<'a> MotionEvent<'a> {
310302
///
311303
#[inline]
312304
pub fn device_id(&self) -> i32 {
313-
self.deviceId
305+
self.ga_event.deviceId
314306
}
315307

316308
/// Returns the motion action associated with the event.
317309
///
318310
/// See [the MotionEvent docs](https://developer.android.com/reference/android/view/MotionEvent#getActionMasked())
319311
#[inline]
320312
pub fn action(&self) -> MotionAction {
321-
let action = self.action as u32 & ndk_sys::AMOTION_EVENT_ACTION_MASK;
313+
let action = self.ga_event.action as u32 & ndk_sys::AMOTION_EVENT_ACTION_MASK;
322314
action.try_into().unwrap()
323315
}
324316

@@ -327,12 +319,12 @@ impl<'a> MotionEvent<'a> {
327319
/// Pointer indices can change per motion event. For an identifier that stays the same, see
328320
/// [`Pointer::pointer_id()`].
329321
///
330-
/// This only has a meaning when the [action](Self::action) is one of [`Up`](MotionAction::Up),
322+
/// This only has a meaning when the [action](self::action) is one of [`Up`](MotionAction::Up),
331323
/// [`Down`](MotionAction::Down), [`PointerUp`](MotionAction::PointerUp),
332324
/// or [`PointerDown`](MotionAction::PointerDown).
333325
#[inline]
334326
pub fn pointer_index(&self) -> usize {
335-
let action = self.action as u32;
327+
let action = self.ga_event.action as u32;
336328
let index = (action & ndk_sys::AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
337329
>> ndk_sys::AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
338330
index as usize
@@ -345,8 +337,8 @@ impl<'a> MotionEvent<'a> {
345337
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getpointerid)
346338
// TODO: look at output with out-of-range pointer index
347339
// Probably -1 though
348-
pub fn pointer_id_for(&self, pointer_index: usize) -> i32 {
349-
unsafe { ndk_sys::AMotionEvent_getPointerId(self.ptr.as_ptr(), pointer_index) }
340+
pub fn pointer_id_for(&self.ga_event, pointer_index: usize) -> i32 {
341+
unsafe { ndk_sys::AMotionEvent_getPointerId(self.ga_event.ptr.as_ptr(), pointer_index) }
350342
}
351343
*/
352344

@@ -355,7 +347,7 @@ impl<'a> MotionEvent<'a> {
355347
/// See [the MotionEvent docs](https://developer.android.com/reference/android/view/MotionEvent#getPointerCount())
356348
#[inline]
357349
pub fn pointer_count(&self) -> usize {
358-
self.pointerCount as usize
350+
self.ga_event.pointerCount as usize
359351
}
360352

361353
/// An iterator over the pointers in this motion event
@@ -370,7 +362,7 @@ impl<'a> MotionEvent<'a> {
370362

371363
/// The pointer at a given pointer index. Panics if the pointer index is out of bounds.
372364
///
373-
/// If you need to loop over all the pointers, prefer the [`pointers()`](Self::pointers) method.
365+
/// If you need to loop over all the pointers, prefer the [`pointers()`](self::pointers) method.
374366
#[inline]
375367
pub fn pointer_at_index(&self, index: usize) -> Pointer<'_> {
376368
if index >= self.pointer_count() {
@@ -386,14 +378,14 @@ impl<'a> MotionEvent<'a> {
386378
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_gethistorysize)
387379
#[inline]
388380
pub fn history_size(&self) -> usize {
389-
unsafe { ndk_sys::AMotionEvent_getHistorySize(self.ptr.as_ptr()) as usize }
381+
unsafe { ndk_sys::AMotionEvent_getHistorySize(self.ga_event.ptr.as_ptr()) as usize }
390382
}
391383
392384
/// An iterator over the historical events contained in this event.
393385
#[inline]
394386
pub fn history(&self) -> HistoricalMotionEventsIter<'_> {
395387
HistoricalMotionEventsIter {
396-
event: self.ptr,
388+
event: self.ga_event.ptr,
397389
next_history_index: 0,
398390
history_size: self.history_size(),
399391
_marker: std::marker::PhantomData,
@@ -407,7 +399,7 @@ impl<'a> MotionEvent<'a> {
407399
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getmetastate)
408400
#[inline]
409401
pub fn meta_state(&self) -> MetaState {
410-
MetaState(self.metaState as u32)
402+
MetaState(self.ga_event.metaState as u32)
411403
}
412404

413405
/// Returns the button state during this event, as a bitfield.
@@ -416,7 +408,7 @@ impl<'a> MotionEvent<'a> {
416408
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getbuttonstate)
417409
#[inline]
418410
pub fn button_state(&self) -> ButtonState {
419-
ButtonState(self.buttonState as u32)
411+
ButtonState(self.ga_event.buttonState as u32)
420412
}
421413

422414
/// Returns the time of the start of this gesture, in the `java.lang.System.nanoTime()` time
@@ -426,7 +418,7 @@ impl<'a> MotionEvent<'a> {
426418
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getdowntime)
427419
#[inline]
428420
pub fn down_time(&self) -> i64 {
429-
self.downTime
421+
self.ga_event.downTime
430422
}
431423

432424
/// Returns a bitfield indicating which edges were touched by this event.
@@ -435,7 +427,7 @@ impl<'a> MotionEvent<'a> {
435427
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getedgeflags)
436428
#[inline]
437429
pub fn edge_flags(&self) -> EdgeFlags {
438-
EdgeFlags(self.edgeFlags as u32)
430+
EdgeFlags(self.ga_event.edgeFlags as u32)
439431
}
440432

441433
/// Returns the time of this event, in the `java.lang.System.nanoTime()` time base
@@ -444,7 +436,7 @@ impl<'a> MotionEvent<'a> {
444436
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_geteventtime)
445437
#[inline]
446438
pub fn event_time(&self) -> i64 {
447-
self.eventTime
439+
self.ga_event.eventTime
448440
}
449441

450442
/// The flags associated with a motion event.
@@ -453,7 +445,7 @@ impl<'a> MotionEvent<'a> {
453445
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getflags)
454446
#[inline]
455447
pub fn flags(&self) -> MotionEventFlags {
456-
MotionEventFlags(self.flags as u32)
448+
MotionEventFlags(self.ga_event.flags as u32)
457449
}
458450

459451
/* Missing from GameActivity currently...
@@ -462,17 +454,17 @@ impl<'a> MotionEvent<'a> {
462454
/// See [the NDK
463455
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getxoffset)
464456
#[inline]
465-
pub fn x_offset(&self) -> f32 {
466-
self.x_offset
457+
pub fn x_offset(&self.ga_event) -> f32 {
458+
self.ga_event.x_offset
467459
}
468460
469461
/// Returns the offset in the y direction between the coordinates and the raw coordinates
470462
///
471463
/// See [the NDK
472464
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getyoffset)
473465
#[inline]
474-
pub fn y_offset(&self) -> f32 {
475-
self.y_offset
466+
pub fn y_offset(&self.ga_event) -> f32 {
467+
self.ga_event.y_offset
476468
}
477469
*/
478470

@@ -482,7 +474,7 @@ impl<'a> MotionEvent<'a> {
482474
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getxprecision)
483475
#[inline]
484476
pub fn x_precision(&self) -> f32 {
485-
self.precisionX
477+
self.ga_event.precisionX
486478
}
487479

488480
/// Returns the precision of the y value of the coordinates
@@ -491,7 +483,7 @@ impl<'a> MotionEvent<'a> {
491483
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getyprecision)
492484
#[inline]
493485
pub fn y_precision(&self) -> f32 {
494-
self.precisionY
486+
self.ga_event.precisionY
495487
}
496488
}
497489

@@ -510,13 +502,13 @@ impl<'a> Pointer<'a> {
510502

511503
#[inline]
512504
pub fn pointer_id(&self) -> i32 {
513-
let pointer = &self.event.pointers[self.index];
505+
let pointer = &self.event.ga_event.pointers[self.index];
514506
pointer.id
515507
}
516508

517509
#[inline]
518510
pub fn axis_value(&self, axis: Axis) -> f32 {
519-
let pointer = &self.event.pointers[self.index];
511+
let pointer = &self.event.ga_event.pointers[self.index];
520512
pointer.axisValues[axis as u32 as usize]
521513
}
522514

@@ -532,13 +524,13 @@ impl<'a> Pointer<'a> {
532524

533525
#[inline]
534526
pub fn raw_x(&self) -> f32 {
535-
let pointer = &self.event.pointers[self.index];
527+
let pointer = &self.event.ga_event.pointers[self.index];
536528
pointer.rawX
537529
}
538530

539531
#[inline]
540532
pub fn raw_y(&self) -> f32 {
541-
let pointer = &self.event.pointers[self.index];
533+
let pointer = &self.event.ga_event.pointers[self.index];
542534
pointer.rawY
543535
}
544536

@@ -579,7 +571,7 @@ impl<'a> Pointer<'a> {
579571

580572
#[inline]
581573
pub fn tool_type(&self) -> ToolType {
582-
let pointer = &self.event.pointers[self.index];
574+
let pointer = &self.event.ga_event.pointers[self.index];
583575
let tool_type = pointer.toolType as u32;
584576
tool_type.try_into().unwrap()
585577
}
@@ -937,14 +929,6 @@ pub struct KeyEvent<'a> {
937929
ga_event: &'a GameActivityKeyEvent,
938930
}
939931

940-
impl<'a> Deref for KeyEvent<'a> {
941-
type Target = GameActivityKeyEvent;
942-
943-
fn deref(&self) -> &Self::Target {
944-
self.ga_event
945-
}
946-
}
947-
948932
/// Key actions.
949933
///
950934
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-27)
@@ -1262,7 +1246,7 @@ impl<'a> KeyEvent<'a> {
12621246
///
12631247
#[inline]
12641248
pub fn source(&self) -> Source {
1265-
let source = self.source as u32;
1249+
let source = self.ga_event.source as u32;
12661250
source.try_into().unwrap_or(Source::Unknown)
12671251
}
12681252

@@ -1277,15 +1261,15 @@ impl<'a> KeyEvent<'a> {
12771261
///
12781262
#[inline]
12791263
pub fn device_id(&self) -> i32 {
1280-
self.deviceId
1264+
self.ga_event.deviceId
12811265
}
12821266

12831267
/// Returns the key action associated with the event.
12841268
///
12851269
/// See [the KeyEvent docs](https://developer.android.com/reference/android/view/KeyEvent#getAction())
12861270
#[inline]
12871271
pub fn action(&self) -> KeyAction {
1288-
let action = self.action as u32;
1272+
let action = self.ga_event.action as u32;
12891273
action.try_into().unwrap()
12901274
}
12911275

@@ -1296,7 +1280,7 @@ impl<'a> KeyEvent<'a> {
12961280
/// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getdowntime)
12971281
#[inline]
12981282
pub fn down_time(&self) -> i64 {
1299-
self.downTime
1283+
self.ga_event.downTime
13001284
}
13011285

13021286
/// Returns the time this event occured. This is on the scale of
@@ -1306,7 +1290,7 @@ impl<'a> KeyEvent<'a> {
13061290
/// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_geteventtime)
13071291
#[inline]
13081292
pub fn event_time(&self) -> i64 {
1309-
self.eventTime
1293+
self.ga_event.eventTime
13101294
}
13111295

13121296
/// Returns the keycode associated with this key event
@@ -1315,7 +1299,7 @@ impl<'a> KeyEvent<'a> {
13151299
/// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getkeycode)
13161300
#[inline]
13171301
pub fn key_code(&self) -> Keycode {
1318-
let keycode = self.keyCode as u32;
1302+
let keycode = self.ga_event.keyCode as u32;
13191303
keycode.try_into().unwrap_or(Keycode::Unknown)
13201304
}
13211305

@@ -1325,7 +1309,7 @@ impl<'a> KeyEvent<'a> {
13251309
/// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getrepeatcount)
13261310
#[inline]
13271311
pub fn repeat_count(&self) -> i32 {
1328-
self.repeatCount
1312+
self.ga_event.repeatCount
13291313
}
13301314

13311315
/// Returns the hardware keycode of a key. This varies from device to device.
@@ -1334,7 +1318,7 @@ impl<'a> KeyEvent<'a> {
13341318
/// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getscancode)
13351319
#[inline]
13361320
pub fn scan_code(&self) -> i32 {
1337-
self.scanCode
1321+
self.ga_event.scanCode
13381322
}
13391323
}
13401324

@@ -1397,7 +1381,7 @@ impl<'a> KeyEvent<'a> {
13971381
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getflags)
13981382
#[inline]
13991383
pub fn flags(&self) -> KeyEventFlags {
1400-
KeyEventFlags(self.flags as u32)
1384+
KeyEventFlags(self.ga_event.flags as u32)
14011385
}
14021386

14031387
/// Returns the state of the modifiers during this key event, represented by a bitmask.
@@ -1406,6 +1390,6 @@ impl<'a> KeyEvent<'a> {
14061390
/// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getmetastate)
14071391
#[inline]
14081392
pub fn meta_state(&self) -> MetaState {
1409-
MetaState(self.metaState as u32)
1393+
MetaState(self.ga_event.metaState as u32)
14101394
}
14111395
}

0 commit comments

Comments
 (0)