lib/mnit: update doc and style of the `input` module
[nit.git] / lib / android / input_events.nit
index a87104d..45cc8a1 100644 (file)
@@ -17,7 +17,7 @@
 # Pointer and hardware key events
 module input_events
 
-import mnit_input
+import mnit::input
 import android
 
 in "C header" `{
@@ -57,11 +57,6 @@ private extern class NativeAndroidMotionEvent `{AInputEvent *`}
                return AMotionEvent_getPointerCount(self);
        `}
 
-       # Did this motion event just started?
-       fun just_went_down: Bool `{
-               return (AMotionEvent_getAction(self) & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_DOWN;
-       `}
-
        fun edge: Int `{
                return AMotionEvent_getEdgeFlags(self);
        `}
@@ -80,6 +75,13 @@ end
 private extern class AMotionEventAction `{ int32_t `}
        fun action: Int `{ return self & AMOTION_EVENT_ACTION_MASK; `}
 
+       # Pointer index concerned by this action
+       #
+       # Require: `is_pointer_down or is_pointer_up`
+       fun pointer_index: Int `{
+               return (self & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
+       `}
+
        fun is_down: Bool do return action == 0
        fun is_up: Bool do return action == 1
        fun is_move: Bool do return action == 2
@@ -101,26 +103,24 @@ class AndroidMotionEvent
 
        private var native: NativeAndroidMotionEvent
 
-       private var pointers_cache: nullable Array[AndroidPointerEvent] = null
-
        # Pointers (or fingers) composing this motion event
-       fun pointers: Array[AndroidPointerEvent]
-       do
-               if pointers_cache != null then
-                       return pointers_cache.as(not null)
-               else
-                       var pointers = new Array[AndroidPointerEvent]
-                       var pointers_count = native.pointers_count
-                       for i in [0 .. pointers_count [do
-                               var pointer_event = new AndroidPointerEvent(self, i)
-                               pointers.add(pointer_event)
-                       end
-                       pointers_cache = pointers
-                       return pointers
+       var pointers: Array[AndroidPointerEvent] is lazy do
+               return [for i in native.pointers_count.times do new AndroidPointerEvent(self, i)]
+       end
+
+       # The pointer (or finger) causing this event
+       var acting_pointer: AndroidPointerEvent is lazy do
+               var action = native.action
+               var index = 0
+
+               if action.is_pointer_down or action.is_pointer_up then
+                       index = native.action.pointer_index
                end
+
+               return new AndroidPointerEvent(self, index)
        end
 
-       redef fun just_went_down: Bool do return native.just_went_down
+       redef fun just_went_down do return native.action.is_down or native.action.is_pointer_down
 
        # Was the top edge of the screen intersected by this event?
        fun touch_to_edge: Bool do return native.edge == 1
@@ -160,13 +160,13 @@ class AndroidPointerEvent
 
        private var pointer_index: Int
 
-       redef fun x: Float do return native_x(motion_event.native, pointer_index)
+       redef fun x do return native_x(motion_event.native, pointer_index)
 
        private fun native_x(motion_event: NativeAndroidMotionEvent, pointer_index: Int): Float `{
                return AMotionEvent_getX(motion_event, pointer_index);
        `}
 
-       redef fun y: Float do return native_y(motion_event.native, pointer_index)
+       redef fun y do return native_y(motion_event.native, pointer_index)
 
        private fun native_y(motion_event: NativeAndroidMotionEvent, pointer_index: Int): Float `{
                return AMotionEvent_getY(motion_event, pointer_index);
@@ -182,16 +182,15 @@ class AndroidPointerEvent
        redef fun pressed
        do
                var action = motion_event.native.action
-               return action.is_down or action.is_move
+               return action.is_down or action.is_move or action.is_pointer_down
        end
 
-       redef fun depressed do return not pressed
+       redef fun is_move do return motion_event.acting_pointer == self and
+               motion_event.native.action.is_move
 
        # Does this pointer just began touching the screen?
-       fun just_went_down: Bool
-       do
-               return motion_event.down_pointer == self
-       end
+       fun just_went_down: Bool do return motion_event.acting_pointer == self and
+               motion_event.just_went_down
 
        # Unique id of this pointer since the beginning of the gesture
        fun pointer_id: Int do return native_pointer_id(motion_event.native, pointer_index)
@@ -208,8 +207,8 @@ extern class AndroidKeyEvent `{AInputEvent *`}
 
        private fun action: Int `{ return AKeyEvent_getAction(self); `}
 
-       redef fun is_down: Bool do return action == 0
-       redef fun is_up: Bool do return action == 1
+       redef fun is_down do return action == 0
+       redef fun is_up do return action == 1
 
        # Hardware code of the key raising this event
        fun key_code: Int `{ return AKeyEvent_getKeyCode(self); `}