From 3062ecaf7073a12442fb04ec217f7e672aaebc94 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Wed, 10 Jul 2024 11:08:44 -0400 Subject: [PATCH] TouchEvent: Replace Vector With Array --- src/starling/events/TouchEvent.hx | 386 +++++++++++++++--------------- 1 file changed, 195 insertions(+), 191 deletions(-) diff --git a/src/starling/events/TouchEvent.hx b/src/starling/events/TouchEvent.hx index 62da70a6..487754f8 100644 --- a/src/starling/events/TouchEvent.hx +++ b/src/starling/events/TouchEvent.hx @@ -7,11 +7,8 @@ // in accordance with the terms of the accompanying license agreement. // // ================================================================================================= - package starling.events; -import openfl.Vector; - import starling.display.DisplayObject; /** A TouchEvent is triggered either by touch or mouse input. @@ -42,7 +39,7 @@ import starling.display.DisplayObject; * the touches that occurred on top of certain objects, you can query the event for touches * with a specific target:

* - * var touches:Vector.<Touch> = touchEvent.getTouches(this); + * 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:

@@ -52,191 +49,198 @@ import starling.display.DisplayObject; * @see Touch * @see TouchPhase */ -class TouchEvent extends Event -{ - /** Event type for touch or mouse input. */ - public static inline var TOUCH:String = "touch"; - - @:noCompletion private var __shiftKey:Bool; - @:noCompletion private var __ctrlKey:Bool; - @:noCompletion private var __timestamp:Float; - @:noCompletion private var __visitedObjects:Vector; - - /** Helper object. */ - private static var sTouches:Vector = new Vector(); - - #if commonjs - private static function __init__ () { - - untyped Object.defineProperties (TouchEvent.prototype, { - "timestamp": { get: untyped __js__ ("function () { return this.get_timestamp (); }") }, - "touches": { get: untyped __js__ ("function () { return this.get_touches (); }") }, - "shiftKey": { get: untyped __js__ ("function () { return this.get_shiftKey (); }") }, - "ctrlKey": { get: untyped __js__ ("function () { return this.get_ctrlKey (); }") }, - }); - - } - #end - - /** Creates a new TouchEvent instance. */ - public function new(type:String, touches:Vector=null, shiftKey:Bool=false, - ctrlKey:Bool=false, bubbles:Bool=true) - { - super(type, bubbles, touches); - - __shiftKey = shiftKey; - __ctrlKey = ctrlKey; - __visitedObjects = new Vector(); - - updateTimestamp(touches); - } - - /** @private */ - @:allow(starling.events.TouchProcessor) private function resetTo(type:String, touches:Vector=null, shiftKey:Bool=false, - ctrlKey:Bool=false, bubbles:Bool=true):TouchEvent - { - super.reset(type, bubbles, touches); - - __shiftKey = shiftKey; - __ctrlKey = ctrlKey; - __visitedObjects.length = 0; - updateTimestamp(touches); - - return this; - } - - private function updateTimestamp(touches:Vector):Void - { - __timestamp = -1.0; - var numTouches:Int = touches != null ? touches.length : 0; - - for (i in 0...numTouches) - { - if (touches[i].timestamp > __timestamp) - __timestamp = touches[i].timestamp; +class TouchEvent extends Event { + /** Event type for touch or mouse input. */ + public static inline var TOUCH:String = "touch"; + + @:noCompletion private var __shiftKey:Bool; + @:noCompletion private var __ctrlKey:Bool; + @:noCompletion private var __timestamp:Float; + @:noCompletion private var __visitedObjects:Array; + + /** Helper object. */ + private static var sTouches:Array = new Array(); + + #if commonjs + private static function __init__() { + untyped Object.defineProperties(TouchEvent.prototype, { + "timestamp": {get: untyped __js__("function () { return this.get_timestamp (); }")}, + "touches": {get: untyped __js__("function () { return this.get_touches (); }")}, + "shiftKey": {get: untyped __js__("function () { return this.get_shiftKey (); }")}, + "ctrlKey": {get: untyped __js__("function () { return this.get_ctrlKey (); }")}, + }); + } + #end + + /** Creates a new TouchEvent instance. */ + public function new(type:String, touches:Array = null, shiftKey:Bool = false, ctrlKey:Bool = false, bubbles:Bool = true) { + super(type, bubbles, touches); + + __shiftKey = shiftKey; + __ctrlKey = ctrlKey; + __visitedObjects = new Array(); + + updateTimestamp(touches); + } + + /** @private */ + @:allow(starling.events.TouchProcessor) private function resetTo(type:String, touches:Array = null, shiftKey:Bool = false, ctrlKey:Bool = false, + bubbles:Bool = true):TouchEvent { + super.reset(type, bubbles, touches); + + __shiftKey = shiftKey; + __ctrlKey = ctrlKey; + #if (haxe_ver >= 4.0) + __visitedObjects.resize(0); + #else + ArrayUtil.resize(__visitedObjects, 0); + #end + updateTimestamp(touches); + + return this; + } + + private function updateTimestamp(touches:Array):Void { + __timestamp = -1.0; + var numTouches:Int = touches != null ? touches.length : 0; + + for (i in 0...numTouches) { + if (touches[i].timestamp > __timestamp) + __timestamp = touches[i].timestamp; + } + } + + /** 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. */ + 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); + + if (correctTarget && correctPhase) + out[out.length] = touch; // avoiding 'push' + } + return out; + } + + /** 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. + */ + public function getTouch(target:DisplayObject, phase:String = null, id:Int = -1):Touch { + getTouches(target, phase, sTouches); + var numTouches:Int = sTouches.length; + + if (numTouches > 0) { + var touch:Touch = null; + + if (id < 0) + touch = sTouches[0]; + else { + for (i in 0...numTouches) + if (sTouches[i].id == id) { + touch = sTouches[i]; + break; + } + } + + #if (haxe_ver >= 4.0) + sTouches.resize(0); + #else + ArrayUtil.resize(sTouches, 0); + #end + return touch; + } else + return null; + } + + /** Indicates if a 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) { + if (sTouches[i].phase != TouchPhase.ENDED) { + result = true; + break; + } + --i; + } + + #if (haxe_ver >= 4.0) + sTouches.resize(0); + #else + ArrayUtil.resize(sTouches, 0); + #end + return result; + } + + // custom dispatching + + /** @private + * Dispatches the event along a custom bubble chain. During the lifetime of the event, + * each object is visited only once. */ + public function dispatch(chain:Array):Void { + if (chain != null && chain.length != 0) { + var chainLength:Int = bubbles ? chain.length : 1; + var previousTarget:EventDispatcher = target; + 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) { + var stopPropagation:Bool = chainElement.__invokeEvent(this); + __visitedObjects[__visitedObjects.length] = chainElement; + if (stopPropagation) + break; + } + } + + setTarget(previousTarget); + } + } + + // properties + + /** The time the event occurred (in seconds since application launch). */ + public var timestamp(get, never):Float; + + private function get_timestamp():Float { + return __timestamp; + } + + /** All touches that are currently available. */ + public var touches(get, never):Array; + + private function get_touches():Array { + var touches:Array = cast data; + return touches.concat(); + } + + /** Indicates if the shift key was pressed when the event occurred. */ + public var shiftKey(get, never):Bool; + + private function get_shiftKey():Bool { + return __shiftKey; + } + + /** Indicates if the ctrl key was pressed when the event occurred. (Mac OS: Cmd or Ctrl) */ + public var ctrlKey(get, never):Bool; + + private function get_ctrlKey():Bool { + return __ctrlKey; } - } - - /** Returns a list of touches that originated over a certain target. If you pass an - * out-vector, the touches will be added to this vector instead of creating - * a new object. */ - public function getTouches(target:DisplayObject, phase:String=null, - out:Vector=null):Vector - { - if (out == null) out = new Vector(); - var allTouches:Vector = 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); - - if (correctTarget && correctPhase) - out[out.length] = touch; // avoiding 'push' - } - return out; - } - - /** 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. - */ - public function getTouch(target:DisplayObject, phase:String=null, id:Int=-1):Touch - { - getTouches(target, phase, sTouches); - var numTouches:Int = sTouches.length; - - if (numTouches > 0) - { - var touch:Touch = null; - - if (id < 0) touch = sTouches[0]; - else - { - for (i in 0...numTouches) - if (sTouches[i].id == id) { touch = sTouches[i]; break; } - } - - sTouches.length = 0; - return touch; - } - else return null; - } - - /** Indicates if a 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) - { - if (sTouches[i].phase != TouchPhase.ENDED) - { - result = true; - break; - } - --i; - } - - sTouches.length = 0; - return result; - } - - // custom dispatching - - /** @private - * Dispatches the event along a custom bubble chain. During the lifetime of the event, - * each object is visited only once. */ - public function dispatch(chain:Vector):Void - { - if (chain != null && chain.length != 0) - { - var chainLength:Int = bubbles ? chain.length : 1; - var previousTarget:EventDispatcher = target; - 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) - { - var stopPropagation:Bool = chainElement.__invokeEvent(this); - __visitedObjects[__visitedObjects.length] = chainElement; - if (stopPropagation) break; - } - } - - setTarget(previousTarget); - } - } - - // properties - - /** The time the event occurred (in seconds since application launch). */ - public var timestamp(get, never):Float; - private function get_timestamp():Float { return __timestamp; } - - /** All touches that are currently available. */ - public var touches(get, never):Vector; - private function get_touches():Vector - { - var touches:Vector = cast data; - return touches.concat(); - } - - /** Indicates if the shift key was pressed when the event occurred. */ - public var shiftKey(get, never):Bool; - private function get_shiftKey():Bool { return __shiftKey; } - - /** Indicates if the ctrl key was pressed when the event occurred. (Mac OS: Cmd or Ctrl) */ - public var ctrlKey(get, never):Bool; - private function get_ctrlKey():Bool { return __ctrlKey; } -} \ No newline at end of file +}