diff --git a/android-activity/src/game_activity/input.rs b/android-activity/src/game_activity/input.rs index 2f53612..fdf8326 100644 --- a/android-activity/src/game_activity/input.rs +++ b/android-activity/src/game_activity/input.rs @@ -15,8 +15,8 @@ use crate::activity_impl::ffi::{GameActivityKeyEvent, GameActivityMotionEvent}; use crate::input::{ - Axis, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, MotionAction, - MotionEventFlags, Pointer, PointersIter, Source, ToolType, + Axis, Button, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, + MotionAction, MotionEventFlags, Pointer, PointersIter, Source, ToolType, }; // Note: try to keep this wrapper API compatible with the AInputEvent API if possible @@ -67,6 +67,18 @@ impl<'a> MotionEvent<'a> { action.into() } + /// Returns which button has been modified during a press or release action. + /// + /// For actions other than [`MotionAction::ButtonPress`] and + /// [`MotionAction::ButtonRelease`] the returned value is undefined. + /// + /// See [the MotionEvent docs](https://developer.android.com/reference/android/view/MotionEvent#getActionButton()) + #[inline] + pub fn action_button(&self) -> Button { + let action = self.ga_event.actionButton as u32; + action.into() + } + /// Returns the pointer index of an `Up` or `Down` event. /// /// Pointer indices can change per motion event. For an identifier that stays the same, see diff --git a/android-activity/src/input.rs b/android-activity/src/input.rs index 326219c..85a0b9f 100644 --- a/android-activity/src/input.rs +++ b/android-activity/src/input.rs @@ -238,6 +238,37 @@ pub enum MotionAction { __Unknown(u32), } +/// Identifies buttons that are associated with motion events. +/// +/// See [the NDK +/// docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-47) +/// +/// # Android Extensible Enum +/// +/// This is a runtime [extensible enum](`crate#android-extensible-enums`) and +/// should be handled similar to a `#[non_exhaustive]` enum to maintain +/// forwards compatibility. +/// +/// This implements `Into` and `From` for converting to/from Android +/// SDK integer values. +/// +#[derive(Copy, Clone, Debug, PartialEq, Eq, num_enum::FromPrimitive, num_enum::IntoPrimitive)] +#[non_exhaustive] +#[repr(u32)] +pub enum Button { + Back = ndk_sys::AMOTION_EVENT_BUTTON_BACK, + Forward = ndk_sys::AMOTION_EVENT_BUTTON_FORWARD, + Primary = ndk_sys::AMOTION_EVENT_BUTTON_PRIMARY, + Secondary = ndk_sys::AMOTION_EVENT_BUTTON_SECONDARY, + StylusPrimary = ndk_sys::AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, + StylusSecondary = ndk_sys::AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, + Tertiary = ndk_sys::AMOTION_EVENT_BUTTON_TERTIARY, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(u32), +} + /// An axis of a motion event. /// /// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-32) diff --git a/android-activity/src/native_activity/input.rs b/android-activity/src/native_activity/input.rs index 551b18d..fcab382 100644 --- a/android-activity/src/native_activity/input.rs +++ b/android-activity/src/native_activity/input.rs @@ -1,8 +1,8 @@ use std::marker::PhantomData; use crate::input::{ - Axis, ButtonState, EdgeFlags, KeyAction, Keycode, MetaState, MotionAction, MotionEventFlags, - Pointer, PointersIter, Source, ToolType, + Axis, Button, ButtonState, EdgeFlags, KeyAction, Keycode, MetaState, MotionAction, + MotionEventFlags, Pointer, PointersIter, Source, ToolType, }; /// A motion event @@ -54,10 +54,24 @@ impl<'a> MotionEvent<'a> { // XXX: we use `AMotionEvent_getAction` directly since we have our own // `MotionAction` enum that we share between backends, which may also // capture unknown variants added in new versions of Android. - let action = unsafe { ndk_sys::AMotionEvent_getAction(self.ndk_event.ptr().as_ptr()) as u32 }; + let action = + unsafe { ndk_sys::AMotionEvent_getAction(self.ndk_event.ptr().as_ptr()) as u32 }; action.into() } + /// Returns which button has been modified during a press or release action. + /// + /// For actions other than [`MotionAction::ButtonPress`] and + /// [`MotionAction::ButtonRelease`] the returned value is undefined. + /// + /// See [the MotionEvent docs](https://developer.android.com/reference/android/view/MotionEvent#getActionButton()) + #[inline] + pub fn action_button(&self) -> Button { + let action_button = + unsafe { ndk_sys::AMotionEvent_getActionButton(self.ndk_event.ptr().as_ptr()) as u32 }; + action_button.into() + } + /// Returns the pointer index of an `Up` or `Down` event. /// /// Pointer indices can change per motion event. For an identifier that stays the same, see