UNPKG

starling-framework

Version:

A fast, productive library for 2D cross-platform development.

483 lines (467 loc) 18.5 kB
// Class: starling.events.TouchProcessor var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this $global.Object.defineProperty(exports, "__esModule", {value: true}); var __map_reserved = {}; // Imports var $hxClasses = require("./../../hxClasses_stub").default; var $hxEnums = require("./../../hxEnums_stub").default; var $bind = require("./../../bind_stub").default; var $import = require("./../../import_stub").default; function openfl__$Vector_Vector_$Impl_$() {return require("./../../openfl/_Vector/Vector_Impl_");} function openfl__$Vector_VectorIterator() {return require("./../../openfl/_Vector/VectorIterator");} function starling_events_TouchData() {return require("./../../starling/events/TouchData");} function js_Boot() {return require("./../../js/Boot");} function Reflect() {return require("./../../Reflect");} function starling_events_Touch() {return require("./../../starling/events/Touch");} function starling_utils_Pool() {return require("./../../starling/utils/Pool");} function starling_utils_MathUtil() {return require("./../../starling/utils/MathUtil");} function starling_core_Starling() {return require("./../../starling/core/Starling");} function starling_events_TouchMarker() {return require("./../../starling/events/TouchMarker");} function openfl_Lib() {return $import(require("openfl/Lib"));} function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");} function openfl_geom_Point() {return $import(require("openfl/geom/Point"));} function starling_events_TouchEvent() {return require("./../../starling/events/TouchEvent");} // Constructor var TouchProcessor = function(stage) { this.__systemGestureMargins = [15,15,15,0]; this.__systemGestureTouchID = -1; this.__multitapDistance = 25; this.__multitapTime = 0.3; this.__ctrlDown = false; this.__shiftDown = false; this.__root = this.__stage = stage; this.__elapsedTime = 0.0; this.__currentTouches = (openfl__$Vector_Vector_$Impl_$().default)._new(); this.__queue = (openfl__$Vector_Vector_$Impl_$().default)._new(); this.__lastTaps = (openfl__$Vector_Vector_$Impl_$().default)._new(); this.__touchEvent = new (starling_events_TouchEvent().default)("touch"); this.__stage.addEventListener("keyDown",$bind(this,this.onKey)); this.__stage.addEventListener("keyUp",$bind(this,this.onKey)); this.monitorInterruptions(true); } // Meta TouchProcessor.__name__ = "starling.events.TouchProcessor"; TouchProcessor.__isInterface__ = false; TouchProcessor.prototype = { dispose: function() { this.monitorInterruptions(false); this.__stage.removeEventListener("keyDown",$bind(this,this.onKey)); this.__stage.removeEventListener("keyUp",$bind(this,this.onKey)); if(this.__touchMarker != null) { this.__touchMarker.dispose(); } }, advanceTime: function(passedTime) { if(this.__isProcessing) { return; } else { this.__isProcessing = true; } var i; var touch; var numIterations = 0; var touchData; this.__elapsedTime += passedTime; TouchProcessor.sUpdatedTouches.length = 0; if(this.__lastTaps.length > 0) { var i1 = this.__lastTaps.length - 1; while(i1 >= 0) { if(this.__elapsedTime - this.__lastTaps[i1].get_timestamp() > this.__multitapTime) { (openfl__$Vector_Vector_$Impl_$().default).removeAt(this.__lastTaps,i1); } --i1; } } while(this.__queue.length > 0 || numIterations == 0) { ++numIterations; var touch1 = new (openfl__$Vector_VectorIterator().default)(this.__currentTouches); while(touch1.hasNext()) { var touch2 = touch1.next(); if(touch2.get_phase() == "began" || touch2.get_phase() == "moved") { touch2.set_phase("stationary"); } } while(this.__queue.length > 0 && !this.containsTouchWithID(TouchProcessor.sUpdatedTouches,this.__queue[this.__queue.length - 1].id)) { touchData = (openfl__$Vector_Vector_$Impl_$().default).pop(this.__queue); touch = this.createOrUpdateTouch(touchData); (openfl__$Vector_Vector_$Impl_$().default).set(TouchProcessor.sUpdatedTouches,TouchProcessor.sUpdatedTouches.length,touch); (starling_events_TouchData().default).toPool(touchData); } i = this.__currentTouches.length - 1; while(i >= 0) { touch = this.__currentTouches[i]; if(touch.get_phase() == "hover" && !this.containsTouchWithID(TouchProcessor.sUpdatedTouches,touch.get_id())) { TouchProcessor.sHelperPoint.setTo(touch.get_globalX(),touch.get_globalY()); if(touch.get_target() != this.__root.hitTest(TouchProcessor.sHelperPoint)) { (openfl__$Vector_Vector_$Impl_$().default).set(TouchProcessor.sUpdatedTouches,TouchProcessor.sUpdatedTouches.length,touch); } } --i; } this.processTouches(TouchProcessor.sUpdatedTouches,this.__shiftDown,this.__ctrlDown); i = this.__currentTouches.length - 1; while(i >= 0) { if(this.__currentTouches[i].get_phase() == "ended") { (openfl__$Vector_Vector_$Impl_$().default).removeAt(this.__currentTouches,i); } --i; } TouchProcessor.sUpdatedTouches.length = 0; } this.__isProcessing = false; if(this.__cancelRequested) { this.cancelTouches(); } }, processTouches: function(touches,shiftDown,ctrlDown) { var touch; TouchProcessor.sHoveringTouchData.length = 0; this.__touchEvent.resetTo("touch",this.__currentTouches,shiftDown,ctrlDown); var touch1 = new (openfl__$Vector_VectorIterator().default)(touches); while(touch1.hasNext()) { var touch2 = touch1.next(); if(touch2.get_phase() == "hover" && touch2.get_target() != null) { TouchProcessor.sHoveringTouchData[TouchProcessor.sHoveringTouchData.length] = { touch : touch2, target : touch2.get_target(), bubbleChain : touch2.get_bubbleChain()}; } if(touch2.get_phase() == "hover" || touch2.get_phase() == "began") { TouchProcessor.sHelperPoint.setTo(touch2.get_globalX(),touch2.get_globalY()); if(this.__occlusionTest != null && this.__occlusionTest(touch2.get_globalX(),touch2.get_globalY())) { touch2.set_target(null); } else { touch2.set_target(this.__root.hitTest(TouchProcessor.sHelperPoint)); } } } var _g = 0; var _g1 = TouchProcessor.sHoveringTouchData; while(_g < _g1.length) { var touchData = _g1[_g]; ++_g; if(((js_Boot().default).__cast((Reflect().default).field(touchData,"touch") , (starling_events_Touch().default))).get_target() != (Reflect().default).field(touchData,"target")) { this.__touchEvent.dispatch((Reflect().default).field(touchData,"bubbleChain")); } } var touch3 = new (openfl__$Vector_VectorIterator().default)(touches); while(touch3.hasNext()) { var touch4 = touch3.next(); touch4.dispatchEvent(this.__touchEvent); } this.__touchEvent.resetTo("touch"); }, checkForSystemGesture: function(touchID,phase,globalX,globalY) { if(!this.__discardSystemGestures || phase == "hover") { return false; } if(this.__systemGestureTouchID == touchID) { if(phase == "ended") { this.__systemGestureTouchID = -1; } return true; } else if(this.__systemGestureTouchID >= 0) { return false; } else if(phase == "began") { var screenBounds = this.__stage.getScreenBounds(this.__stage,(starling_utils_Pool().default).getRectangle()); var isGesture = globalX < screenBounds.get_left() + this.__systemGestureMargins[2] || globalX > screenBounds.get_right() - this.__systemGestureMargins[3] || globalY < screenBounds.get_top() + this.__systemGestureMargins[0] || globalY > screenBounds.get_bottom() - this.__systemGestureMargins[1]; (starling_utils_Pool().default).putRectangle(screenBounds); if(isGesture) { this.__systemGestureTouchID = touchID; } return isGesture; } else { return false; } }, enqueue: function(touchID,phase,globalX,globalY,pressure,width,height) { if(height == null) { height = 1.0; } if(width == null) { width = 1.0; } if(pressure == null) { pressure = 1.0; } if(this.checkForSystemGesture(touchID,phase,globalX,globalY)) { return; } this.queue_unshift(touchID,phase,globalX,globalY,pressure,width,height); if(this.__ctrlDown && this.__touchMarker != null && touchID == 0) { this.__touchMarker.moveMarker(globalX,globalY,this.__shiftDown); this.queue_unshift(1,phase,this.__touchMarker.get_mockX(),this.__touchMarker.get_mockY()); } }, queue_unshift: function(touchID,phase,globalX,globalY,pressure,width,height) { if(height == null) { height = 1.0; } if(width == null) { width = 1.0; } if(pressure == null) { pressure = 1.0; } (openfl__$Vector_Vector_$Impl_$().default).unshift(this.__queue,(starling_events_TouchData().default).fromPool(touchID,phase,globalX,globalY,pressure,width,height)); }, enqueueMouseLeftStage: function() { var mouse = this.getCurrentTouch(0); if(mouse == null || mouse.get_phase() != "hover") { return; } var offset = 1; var exitX = mouse.get_globalX(); var exitY = mouse.get_globalY(); var distLeft = mouse.get_globalX(); var distRight = this.__stage.get_stageWidth() - distLeft; var distTop = mouse.get_globalY(); var distBottom = this.__stage.get_stageHeight() - distTop; var minDist = (starling_utils_MathUtil().default).minValues([distLeft,distRight,distTop,distBottom]); if(minDist == distLeft) { exitX = -offset; } else if(minDist == distRight) { exitX = this.__stage.get_stageWidth() + offset; } else if(minDist == distTop) { exitY = -offset; } else { exitY = this.__stage.get_stageHeight() + offset; } this.enqueue(0,"hover",exitX,exitY); }, cancelTouches: function() { if(this.__isProcessing) { this.__cancelRequested = true; return; } else { this.__cancelRequested = false; } if(this.__currentTouches.length > 0) { var touch = new (openfl__$Vector_VectorIterator().default)(this.__currentTouches); while(touch.hasNext()) { var touch1 = touch.next(); if(touch1.get_phase() == "began" || touch1.get_phase() == "moved" || touch1.get_phase() == "stationary") { touch1.set_phase("ended"); touch1.set_cancelled(true); } } this.processTouches(this.__currentTouches,this.__shiftDown,this.__ctrlDown); } this.__currentTouches.length = 0; while(this.__queue.length > 0) (starling_events_TouchData().default).toPool((openfl__$Vector_Vector_$Impl_$().default).pop(this.__queue)); }, createOrUpdateTouch: function(touchData) { var touch = this.getCurrentTouch(touchData.id); if(touch == null) { touch = new (starling_events_Touch().default)(touchData.id); this.addCurrentTouch(touch); } touch.update(this.__elapsedTime,touchData.phase,touchData.globalX,touchData.globalY,touchData.pressure,touchData.width,touchData.height); if(touchData.phase == "began") { this.updateTapCount(touch); } return touch; }, updateTapCount: function(touch) { var nearbyTap = null; var minSqDist = this.__multitapDistance * this.__multitapDistance; var tap = new (openfl__$Vector_VectorIterator().default)(this.__lastTaps); while(tap.hasNext()) { var tap1 = tap.next(); var sqDist = Math.pow(tap1.get_globalX() - touch.get_globalX(),2) + Math.pow(tap1.get_globalY() - touch.get_globalY(),2); if(sqDist <= minSqDist) { nearbyTap = tap1; break; } } if(nearbyTap != null) { touch.set_tapCount(nearbyTap.get_tapCount() + 1); var this1 = this.__lastTaps; (openfl__$Vector_Vector_$Impl_$().default).removeAt(this.__lastTaps,(Array.prototype.indexOf.call)(this1,nearbyTap,0)); } else { touch.set_tapCount(1); } (openfl__$Vector_Vector_$Impl_$().default).set(this.__lastTaps,this.__lastTaps.length,touch.clone()); }, addCurrentTouch: function(touch) { var i = this.__currentTouches.length - 1; while(i >= 0) { if(this.__currentTouches[i].get_id() == touch.get_id()) { (openfl__$Vector_Vector_$Impl_$().default).removeAt(this.__currentTouches,i); } --i; } (openfl__$Vector_Vector_$Impl_$().default).set(this.__currentTouches,this.__currentTouches.length,touch); }, getCurrentTouch: function(touchID) { var touch = new (openfl__$Vector_VectorIterator().default)(this.__currentTouches); while(touch.hasNext()) { var touch1 = touch.next(); if(touch1.get_id() == touchID) { return touch1; } } return null; }, containsTouchWithID: function(touches,touchID) { var touch = new (openfl__$Vector_VectorIterator().default)(touches); while(touch.hasNext()) { var touch1 = touch.next(); if(touch1.get_id() == touchID) { return true; } } return false; }, setSystemGestureMargins: function(topMargin,bottomMargin,leftMargin,rightMargin) { if(rightMargin == null) { rightMargin = 0; } if(leftMargin == null) { leftMargin = 0; } if(bottomMargin == null) { bottomMargin = 10; } if(topMargin == null) { topMargin = 10; } this.__systemGestureMargins[0] = topMargin; this.__systemGestureMargins[1] = bottomMargin; this.__systemGestureMargins[2] = leftMargin; this.__systemGestureMargins[3] = rightMargin; }, get_simulateMultitouch: function() { return this.__simulateMultitouch; }, set_simulateMultitouch: function(value) { var _gthis = this; if(this.get_simulateMultitouch() == value) { return value; } this.__simulateMultitouch = value; var target = (starling_core_Starling().default).get_current(); var createTouchMarker = null; createTouchMarker = function() { target.removeEventListener("context3DCreate",createTouchMarker); if(_gthis.__touchMarker == null) { _gthis.__touchMarker = new (starling_events_TouchMarker().default)(); _gthis.__touchMarker.set_visible(false); _gthis.__stage.addChild(_gthis.__touchMarker); } }; if(value && this.__touchMarker == null) { if((starling_core_Starling().default).get_current().get_contextValid()) { createTouchMarker(); } else { target.addEventListener("context3DCreate",createTouchMarker); } } else if(!value && this.__touchMarker != null) { this.__touchMarker.removeFromParent(true); this.__touchMarker = null; } return value; }, get_multitapTime: function() { return this.__multitapTime; }, set_multitapTime: function(value) { return this.__multitapTime = value; }, get_multitapDistance: function() { return this.__multitapDistance; }, set_multitapDistance: function(value) { return this.__multitapDistance = value; }, get_root: function() { return this.__root; }, set_root: function(value) { return this.__root = value; }, get_stage: function() { return this.__stage; }, get_numCurrentTouches: function() { return this.__currentTouches.length; }, set_occlusionTest: function(value) { return this.__occlusionTest = value; }, get_occlusionTest: function() { return this.__occlusionTest; }, get_discardSystemGestures: function() { return this.__discardSystemGestures; }, set_discardSystemGestures: function(value) { if(this.__discardSystemGestures != value) { this.__discardSystemGestures = value; this.__systemGestureTouchID = -1; } return value; }, onKey: function(event) { if(event.get_keyCode() == 17 || event.get_keyCode() == 15) { var wasCtrlDown = this.__ctrlDown; this.__ctrlDown = event.type == "keyDown"; if(this.__touchMarker != null && wasCtrlDown != this.__ctrlDown) { this.__touchMarker.set_visible(this.__ctrlDown); this.__touchMarker.moveCenter(this.__stage.get_stageWidth() / 2,this.__stage.get_stageHeight() / 2); var mouseTouch = this.getCurrentTouch(0); var mockedTouch = this.getCurrentTouch(1); if(mouseTouch != null) { this.__touchMarker.moveMarker(mouseTouch.get_globalX(),mouseTouch.get_globalY()); } if(wasCtrlDown && mockedTouch != null && mockedTouch.get_phase() != "ended") { this.queue_unshift(1,"ended",mockedTouch.get_globalX(),mockedTouch.get_globalY()); } else if(this.__ctrlDown && mouseTouch != null) { if(mouseTouch.get_phase() == "hover" || mouseTouch.get_phase() == "ended") { this.queue_unshift(1,"hover",this.__touchMarker.get_mockX(),this.__touchMarker.get_mockY()); } else { this.queue_unshift(1,"began",this.__touchMarker.get_mockX(),this.__touchMarker.get_mockY()); } } } } else if(event.get_keyCode() == 16) { this.__shiftDown = event.type == "keyDown"; } }, monitorInterruptions: function(enable) { try { if(enable) { (openfl_Lib().default).current.stage.addEventListener("deactivate",$bind(this,this.onInterruption),false,0,true); } else { (openfl_Lib().default).current.stage.removeEventListener("deactivate",$bind(this,this.onInterruption)); } } catch( e ) { var e1 = ((e) instanceof (js__$Boot_HaxeError().default)) ? e.val : e; } }, onInterruption: function(event) { this.cancelTouches(); } }; TouchProcessor.prototype.__class__ = TouchProcessor.prototype.constructor = $hxClasses["starling.events.TouchProcessor"] = TouchProcessor; // Init { var tmp = TouchProcessor.prototype; var tmp1 = { get : function () { return this.get_simulateMultitouch (); }, set : function (v) { return this.set_simulateMultitouch (v); }}; var tmp2 = { get : function () { return this.get_multitapTime (); }, set : function (v) { return this.set_multitapTime (v); }}; var tmp3 = { get : function () { return this.get_multitapDistance (); }, set : function (v) { return this.set_multitapDistance (v); }}; var tmp4 = { get : function () { return this.get_root (); }, set : function (v) { return this.set_root (v); }}; var tmp5 = { get : function () { return this.get_stage (); }}; var tmp6 = { get : function () { return this.get_numCurrentTouches (); }}; var tmp7 = { get : function () { return this.get_occlusionTest (); }, set : function (v) { return this.set_occlusionTest (v); }}; var tmp8 = function () { return this.get_discardSystemGestures (); } var tmp9 = function (v) { return this.set_discardSystemGestures (v); } global.Object.defineProperties(tmp,{ simulateMultitouch : tmp1, multitapTime : tmp2, multitapDistance : tmp3, root : tmp4, stage : tmp5, numCurrentTouches : tmp6, occlusionTest : tmp7, discardSystemGestures : { get : tmp8, set : tmp9}}); }; // Statics TouchProcessor.sUpdatedTouches = (openfl__$Vector_Vector_$Impl_$().default)._new() TouchProcessor.sHoveringTouchData = [] TouchProcessor.sHelperPoint = new (openfl_geom_Point().default)() TouchProcessor.TOP = 0 TouchProcessor.BOTTOM = 1 TouchProcessor.LEFT = 2 TouchProcessor.RIGHT = 3 // Export exports.default = TouchProcessor;