diff --git a/src/starling/events/Touch.hx b/src/starling/events/Touch.hx index 5814606..fdc49af 100644 --- a/src/starling/events/Touch.hx +++ b/src/starling/events/Touch.hx @@ -63,22 +63,34 @@ class Touch { "previousGlobalY": {get: untyped __js__("function () { return this.get_previousGlobalY (); }")}, "startGlobalX": {get: untyped __js__("function () { return this.get_startGlobalX (); }")}, "startGlobalY": {get: untyped __js__("function () { return this.get_startGlobalY (); }")}, - "globalX": {get: untyped __js__("function () { return this.get_globalX (); }"), - set: untyped __js__("function (v) { return this.set_globalX (v); }")}, - "globalY": {get: untyped __js__("function () { return this.get_globalY (); }"), - set: untyped __js__("function (v) { return this.set_globalY (v); }")}, - "tapCount": {get: untyped __js__("function () { return this.get_tapCount (); }"), - set: untyped __js__("function (v) { return this.set_tapCount (v); }")}, + "globalX": { + get: untyped __js__("function () { return this.get_globalX (); }"), + set: untyped __js__("function (v) { return this.set_globalX (v); }") + }, + "globalY": { + get: untyped __js__("function () { return this.get_globalY (); }"), + set: untyped __js__("function (v) { return this.set_globalY (v); }") + }, + "tapCount": { + get: untyped __js__("function () { return this.get_tapCount (); }"), + set: untyped __js__("function (v) { return this.set_tapCount (v); }") + }, "phase": {get: untyped __js__("function () { return this.get_phase (); }"), set: untyped __js__("function (v) { return this.set_phase (v); }")}, "target": {get: untyped __js__("function () { return this.get_target (); }"), set: untyped __js__("function (v) { return this.set_target (v); }")}, - "timestamp": {get: untyped __js__("function () { return this.get_timestamp (); }"), - set: untyped __js__("function (v) { return this.set_timestamp (v); }")}, - "pressure": {get: untyped __js__("function () { return this.get_pressure (); }"), - set: untyped __js__("function (v) { return this.set_pressure (v); }")}, + "timestamp": { + get: untyped __js__("function () { return this.get_timestamp (); }"), + set: untyped __js__("function (v) { return this.set_timestamp (v); }") + }, + "pressure": { + get: untyped __js__("function () { return this.get_pressure (); }"), + set: untyped __js__("function (v) { return this.set_pressure (v); }") + }, "width": {get: untyped __js__("function () { return this.get_width (); }"), set: untyped __js__("function (v) { return this.set_width (v); }")}, "height": {get: untyped __js__("function () { return this.get_height (); }"), set: untyped __js__("function (v) { return this.set_height (v); }")}, - "cancelled": {get: untyped __js__("function () { return this.get_cancelled (); }"), - set: untyped __js__("function (v) { return this.set_cancelled (v); }")}, + "cancelled": { + get: untyped __js__("function () { return this.get_cancelled (); }"), + set: untyped __js__("function (v) { return this.set_cancelled (v); }") + }, "duration": {get: untyped __js__("function () { return this.get_duration (); }")}, "bubbleChain": {get: untyped __js__("function () { return this.get_bubbleChain (); }")}, }); diff --git a/src/starling/events/TouchEvent.hx b/src/starling/events/TouchEvent.hx index 487754f..94b1b7c 100644 --- a/src/starling/events/TouchEvent.hx +++ b/src/starling/events/TouchEvent.hx @@ -11,46 +11,51 @@ package starling.events; import starling.display.DisplayObject; -/** A TouchEvent is triggered either by touch or mouse input. - * - *

In Starling, both touch events and mouse events are handled through the same class: - * TouchEvent. To process user input from a touch screen or the mouse, you have to register - * an event listener for events of the type TouchEvent.TOUCH. This is the only - * event type you need to handle; the long list of mouse event types as they are used in - * conventional Flash are mapped to so-called "TouchPhases" instead.

+/** + * A TouchEvent is triggered either by touch or mouse input. * - *

The difference between mouse input and touch input is that

- * - * - * - * Which objects receive touch events? + *

In Starling, both touch events and mouse events are handled through the same class: + * TouchEvent. To process user input from a touch screen or the mouse, you have to register + * an event listener for events of the type TouchEvent.TOUCH. This is the only + * event type you need to handle; the long list of mouse event types as they are used in + * conventional Flash are mapped to so-called "TouchPhases" instead.

* - *

In Starling, any display object receives touch events, as long as the - * touchable property of the object and its parents is enabled. There - * is no "InteractiveObject" class in Starling.

- * - * How to work with individual touches - * - *

The event contains a list of all touches that are currently present. Each individual - * touch is stored in an object of type "Touch". Since you are normally only interested in - * the touches that occurred on top of certain objects, you can query the event for touches - * with a specific target:

+ *

The difference between mouse input and touch input is that

* - * var touches:Array.<Touch> = touchEvent.getTouches(this); - * - *

This will return all touches of "this" or one of its children. When you are not using - * multitouch, you can also access the touch object directly, like this:

+ * * - * var touch:Touch = touchEvent.getTouch(this); - * - * @see Touch - * @see TouchPhase + * Which objects receive touch events? + * + *

In Starling, any display object receives touch events, as long as the + * touchable property of the object and its parents is enabled. There + * is no "InteractiveObject" class in Starling.

+ * + * How to work with individual touches + * + *

The event contains a list of all touches that are currently present. Each individual + * touch is stored in an object of type "Touch". Since you are normally only interested in + * the touches that occurred on top of certain objects, you can query the event for touches + * with a specific target:

+ * + * var touches:Array. = touchEvent.getTouches(this); + * + *

This will return all touches of "this" or one of its children. When you are not using + * multitouch, you can also access the touch object directly, like this:

+ * + * var touch:Touch = touchEvent.getTouch(this); + * + * @see Touch + * @see TouchPhase */ class TouchEvent extends Event { - /** Event type for touch or mouse input. */ + /** + * Event type for touch or mouse input. + * + * @return The string "touch". + */ public static inline var TOUCH:String = "touch"; @:noCompletion private var __shiftKey:Bool; @@ -72,7 +77,15 @@ class TouchEvent extends Event { } #end - /** Creates a new TouchEvent instance. */ + /** + * Creates a new TouchEvent instance. + * + * @param type The type of the event. + * @param touches An array of touches related to this event, or null if there are none. + * @param shiftKey Indicates if the shift key was pressed when the event occurred. + * @param ctrlKey Indicates if the ctrl key was pressed when the event occurred. (Mac OS: Cmd or Ctrl) + * @param bubbles Indicates if the event bubbles. + */ public function new(type:String, touches:Array = null, shiftKey:Bool = false, ctrlKey:Bool = false, bubbles:Bool = true) { super(type, bubbles, touches); @@ -110,32 +123,38 @@ class TouchEvent extends Event { } } - /** Returns a list of touches that originated over a certain target. If you pass an + /** + * Returns a list of touches that originated over a certain target. If you pass an * out-Array, the touches will be added to this Array instead of creating - * a new object. */ + * a new object. + * + * @param target The display object that was touched. + * @param phase The phase the touch must be in, or null if you don't care. + * @param out An array to which the touches will be added. If null, a new array will be created. + * @return An array of touches that originated over the specified target. + */ public function getTouches(target:DisplayObject, phase:String = null, out:Array = null):Array { if (out == null) out = new Array(); - var allTouches:Array = cast data; - var numTouches:Int = allTouches.length; - for (i in 0...numTouches) { - var touch:Touch = allTouches[i]; - var correctTarget:Bool = touch.isTouching(target); - var correctPhase:Bool = (phase == null || phase == touch.phase); + var allTouches:Array = cast data; - if (correctTarget && correctPhase) - out[out.length] = touch; // avoiding 'push' + for (touch in allTouches) { + if (touch.isTouching(target) && (phase == null || phase == touch.phase)) { + out.push(touch); + } } + return out; } - /** Returns a touch that originated over a certain target. + /** + * Returns a touch that originated over a certain target. * - * @param target The object that was touched; may also be a parent of the actual - * touch-target. - * @param phase The phase the touch must be in, or null if you don't care. - * @param id The ID of the requested touch, or -1 if you don't care. + * @param target The object that was touched; may also be a parent of the actual touch-target. + * @param phase The phase the touch must be in, or null if you don't care. + * @param id The ID of the requested touch, or -1 if you don't care. + * @return The touch that originated over the specified target, or null if no such touch exists. */ public function getTouch(target:DisplayObject, phase:String = null, id:Int = -1):Touch { getTouches(target, phase, sTouches); @@ -164,33 +183,44 @@ class TouchEvent extends Event { return null; } - /** Indicates if a target is currently being touched or hovered over. */ + /** + * Indicates if a target is currently being touched or hovered over. + * + * @param target The display object to check for interactions. + * @return A boolean value indicating whether the target is currently being touched or hovered over. + */ public function interactsWith(target:DisplayObject):Bool { - var result:Bool = false; getTouches(target, null, sTouches); - var i:Int = sTouches.length - 1; - while (i >= 0) { + var numTouches:Int = sTouches.length; + for (i in 0...numTouches) { if (sTouches[i].phase != TouchPhase.ENDED) { - result = true; - break; + #if (haxe_ver >= 4.0) + sTouches.resize(0); + #else + ArrayUtil.resize(sTouches, 0); + #end + return true; } - --i; } - #if (haxe_ver >= 4.0) - sTouches.resize(0); + #if (haxe_ver >= 4.0) + sTouches.resize(0); #else ArrayUtil.resize(sTouches, 0); #end - return result; + return false; } // custom dispatching - /** @private + /** + * @private * Dispatches the event along a custom bubble chain. During the lifetime of the event, - * each object is visited only once. */ + * each object is visited only once. + * + * @param chain An array of EventDispatcher objects representing the bubble chain. + */ public function dispatch(chain:Array):Void { if (chain != null && chain.length != 0) { var chainLength:Int = bubbles ? chain.length : 1; @@ -198,12 +228,10 @@ class TouchEvent extends Event { setTarget(chain[0]); for (i in 0...chainLength) { - if (chain[i] == null) - continue; var chainElement:EventDispatcher = cast(chain[i], EventDispatcher); - if (__visitedObjects.indexOf(chainElement) == -1) { + if (chainElement != null && __visitedObjects.indexOf(chainElement) == -1) { var stopPropagation:Bool = chainElement.__invokeEvent(this); - __visitedObjects[__visitedObjects.length] = chainElement; + __visitedObjects.push(chainElement); if (stopPropagation) break; } @@ -215,32 +243,48 @@ class TouchEvent extends Event { // properties - /** The time the event occurred (in seconds since application launch). */ + /** + * The time the event occurred (in seconds since application launch). + * + * @return The time the event occurred, in seconds since application launch. + */ public var timestamp(get, never):Float; - private function get_timestamp():Float { + private inline function get_timestamp():Float { return __timestamp; } - /** All touches that are currently available. */ + /** + * All touches that are currently available. + * + * @return An array of all currently available touches. + */ public var touches(get, never):Array; - private function get_touches():Array { + private inline function get_touches():Array { var touches:Array = cast data; - return touches.concat(); + return touches.copy(); } - /** Indicates if the shift key was pressed when the event occurred. */ + /** + * Indicates if the shift key was pressed when the event occurred. + * + * @return A boolean value indicating whether the shift key was pressed. + */ public var shiftKey(get, never):Bool; - private function get_shiftKey():Bool { + private inline function get_shiftKey():Bool { return __shiftKey; } - /** Indicates if the ctrl key was pressed when the event occurred. (Mac OS: Cmd or Ctrl) */ + /** + * Indicates if the ctrl key was pressed when the event occurred. (Mac OS: Cmd or Ctrl) + * + * @return A boolean value indicating whether the ctrl key was pressed. + */ public var ctrlKey(get, never):Bool; - private function get_ctrlKey():Bool { + private inline function get_ctrlKey():Bool { return __ctrlKey; } }