Skip to content

Commit 2a917ca

Browse files
committed
Expose MotionEvent::action_button() state
This exposes the button associated with a button press or release action.
1 parent add58db commit 2a917ca

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

android-activity/src/game_activity/input.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use crate::activity_impl::ffi::{GameActivityKeyEvent, GameActivityMotionEvent};
1717
use crate::input::{
18-
Axis, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, MotionAction,
19-
MotionEventFlags, Pointer, PointersIter, Source, ToolType,
18+
Axis, Button, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState,
19+
MotionAction, MotionEventFlags, Pointer, PointersIter, Source, ToolType,
2020
};
2121

2222
// Note: try to keep this wrapper API compatible with the AInputEvent API if possible
@@ -67,6 +67,18 @@ impl<'a> MotionEvent<'a> {
6767
action.into()
6868
}
6969

70+
/// Returns which button has been modified during a press or release action.
71+
///
72+
/// For actions other than [`MotionAction::ButtonPress`] and
73+
/// [`MotionAction::ButtonRelease`] the returned value is undefined.
74+
///
75+
/// See [the MotionEvent docs](https://developer.android.com/reference/android/view/MotionEvent#getActionButton())
76+
#[inline]
77+
pub fn action_button(&self) -> Button {
78+
let action = self.ga_event.actionButton as u32;
79+
action.into()
80+
}
81+
7082
/// Returns the pointer index of an `Up` or `Down` event.
7183
///
7284
/// Pointer indices can change per motion event. For an identifier that stays the same, see

android-activity/src/input.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,37 @@ pub enum MotionAction {
238238
__Unknown(u32),
239239
}
240240

241+
/// Identifies buttons that are associated with motion events.
242+
///
243+
/// See [the NDK
244+
/// docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-47)
245+
///
246+
/// # Android Extensible Enum
247+
///
248+
/// This is a runtime [extensible enum](`crate#android-extensible-enums`) and
249+
/// should be handled similar to a `#[non_exhaustive]` enum to maintain
250+
/// forwards compatibility.
251+
///
252+
/// This implements `Into<u32>` and `From<u32>` for converting to/from Android
253+
/// SDK integer values.
254+
///
255+
#[derive(Copy, Clone, Debug, PartialEq, Eq, num_enum::FromPrimitive, num_enum::IntoPrimitive)]
256+
#[non_exhaustive]
257+
#[repr(u32)]
258+
pub enum Button {
259+
Back = ndk_sys::AMOTION_EVENT_BUTTON_BACK,
260+
Forward = ndk_sys::AMOTION_EVENT_BUTTON_FORWARD,
261+
Primary = ndk_sys::AMOTION_EVENT_BUTTON_PRIMARY,
262+
Secondary = ndk_sys::AMOTION_EVENT_BUTTON_SECONDARY,
263+
StylusPrimary = ndk_sys::AMOTION_EVENT_BUTTON_STYLUS_PRIMARY,
264+
StylusSecondary = ndk_sys::AMOTION_EVENT_BUTTON_STYLUS_SECONDARY,
265+
Tertiary = ndk_sys::AMOTION_EVENT_BUTTON_TERTIARY,
266+
267+
#[doc(hidden)]
268+
#[num_enum(catch_all)]
269+
__Unknown(u32),
270+
}
271+
241272
/// An axis of a motion event.
242273
///
243274
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-32)

android-activity/src/native_activity/input.rs

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

33
use crate::input::{
4-
Axis, ButtonState, EdgeFlags, KeyAction, Keycode, MetaState, MotionAction, MotionEventFlags,
5-
Pointer, PointersIter, Source, ToolType,
4+
Axis, Button, ButtonState, EdgeFlags, KeyAction, Keycode, MetaState, MotionAction,
5+
MotionEventFlags, Pointer, PointersIter, Source, ToolType,
66
};
77

88
/// A motion event
@@ -54,10 +54,24 @@ impl<'a> MotionEvent<'a> {
5454
// XXX: we use `AMotionEvent_getAction` directly since we have our own
5555
// `MotionAction` enum that we share between backends, which may also
5656
// capture unknown variants added in new versions of Android.
57-
let action = unsafe { ndk_sys::AMotionEvent_getAction(self.ndk_event.ptr().as_ptr()) as u32 };
57+
let action =
58+
unsafe { ndk_sys::AMotionEvent_getAction(self.ndk_event.ptr().as_ptr()) as u32 };
5859
action.into()
5960
}
6061

62+
/// Returns which button has been modified during a press or release action.
63+
///
64+
/// For actions other than [`MotionAction::ButtonPress`] and
65+
/// [`MotionAction::ButtonRelease`] the returned value is undefined.
66+
///
67+
/// See [the MotionEvent docs](https://developer.android.com/reference/android/view/MotionEvent#getActionButton())
68+
#[inline]
69+
pub fn action_button(&self) -> Button {
70+
let action_button =
71+
unsafe { ndk_sys::AMotionEvent_getActionButton(self.ndk_event.ptr().as_ptr()) as u32 };
72+
action_button.into()
73+
}
74+
6175
/// Returns the pointer index of an `Up` or `Down` event.
6276
///
6377
/// Pointer indices can change per motion event. For an identifier that stays the same, see

0 commit comments

Comments
 (0)