UNPKG

actuate

Version:

Actuate is a flexible, fast "tween" library for animations

307 lines (291 loc) 9.27 kB
// Class: motion.actuators.SimpleActuator 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 $import = require("./../../import_stub").default; var $extend = require("./../../extend_stub").default; function motion_actuators_GenericActuator() {return require("./../../motion/actuators/GenericActuator");} function openfl_Lib() {return $import(require("openfl/Lib"));} function Reflect() {return require("./../../Reflect");} function motion_actuators_PropertyDetails() {return require("./../../motion/actuators/PropertyDetails");} function Std() {return require("./../../Std");} function openfl_display_DisplayObject() {return $import(require("openfl/display/DisplayObject"));} // Constructor var SimpleActuator = function(target,duration,properties) { this.active = true; this.propertyDetails = []; this.sendChange = false; this.paused = false; this.cacheVisible = false; this.initialized = false; this.setVisible = false; this.toggleVisible = false; this.startTime = (openfl_Lib().default).getTimer() / 1000; (motion_actuators_GenericActuator().default).call(this,target,duration,properties); if(!SimpleActuator.addedEvent) { SimpleActuator.addedEvent = true; (openfl_Lib().default).current.stage.addEventListener("enterFrame",SimpleActuator.stage_onEnterFrame); } } // Meta SimpleActuator.__name__ = ["motion","actuators","SimpleActuator"]; SimpleActuator.__isInterface__ = false; SimpleActuator.__super__ = (motion_actuators_GenericActuator().default); SimpleActuator.prototype = $extend((motion_actuators_GenericActuator().default).prototype, { reverse: function(value) { var ga = (motion_actuators_GenericActuator().default).prototype.reverse.call(this,value); var startTime = 0; startTime = (openfl_Lib().default).getTimer() / 1000; this.update(startTime); return ga; }, apply: function() { (motion_actuators_GenericActuator().default).prototype.apply.call(this); if(this.toggleVisible && (Reflect().default).hasField(this.properties,"alpha")) { if(this.getField(this.target,"visible") != null) { this.setField(this.target,"visible",(Reflect().default).field(this.properties,"alpha") > 0); } } }, autoVisible: function(value) { if(value == null) { value = true; } this._autoVisible = value; if(!value) { this.toggleVisible = false; if(this.setVisible) { this.setField(this.target,"visible",this.cacheVisible); } } return this; }, delay: function(duration) { this._delay = duration; this.timeOffset = this.startTime + duration; return this; }, getField: function(target,propertyName) { var value = null; if((Reflect().default).hasField(target,propertyName)) { value = (Reflect().default).field(target,propertyName); } else { value = (Reflect().default).getProperty(target,propertyName); } return value; }, initialize: function() { var details; var start; var _g = 0; var _g1 = (Reflect().default).fields(this.properties); while(_g < _g1.length) { var i = _g1[_g]; ++_g; var isField = true; if((Reflect().default).hasField(this.target,i) && !(this.target.__properties__ && this.target.__properties__["set_" + i])) { start = (Reflect().default).field(this.target,i); } else { isField = false; start = (Reflect().default).getProperty(this.target,i); } if(typeof(start) == "number") { var value = this.getField(this.properties,i); if(start == null) { start = 0; } if(value == null) { value = 0; } details = new (motion_actuators_PropertyDetails().default)(this.target,i,start,value - start,isField); this.propertyDetails.push(details); } } this.detailsLength = this.propertyDetails.length; this.initialized = true; }, move: function() { this.toggleVisible = (Reflect().default).hasField(this.properties,"alpha") && (Std().default)["is"](this.target,(openfl_display_DisplayObject().default)); if(this.toggleVisible && this.properties.alpha != 0 && !this.getField(this.target,"visible")) { this.setVisible = true; this.cacheVisible = this.getField(this.target,"visible"); this.setField(this.target,"visible",true); } this.timeOffset = this.startTime; SimpleActuator.actuators.push(this); ++SimpleActuator.actuatorsLength; }, onUpdate: function(handler,parameters) { this._onUpdate = handler; if(parameters == null) { this._onUpdateParams = []; } else { this._onUpdateParams = parameters; } this.sendChange = true; return this; }, pause: function() { if(!this.paused) { this.paused = true; (motion_actuators_GenericActuator().default).prototype.pause.call(this); this.pauseTime = (openfl_Lib().default).getTimer(); } }, resume: function() { if(this.paused) { this.paused = false; this.timeOffset += ((openfl_Lib().default).getTimer() - this.pauseTime) / 1000; (motion_actuators_GenericActuator().default).prototype.resume.call(this); } }, setField: function(target,propertyName,value) { if((Reflect().default).hasField(target,propertyName) && !(target.__properties__ && target.__properties__["set_" + propertyName])) { (Reflect().default).setField(target,propertyName,value); } else { (Reflect().default).setProperty(target,propertyName,value); } }, setProperty: function(details,value) { if(details.isField) { (Reflect().default).setField(details.target,details.propertyName,value); } else { (Reflect().default).setProperty(details.target,details.propertyName,value); } }, stop: function(properties,complete,sendEvent) { if(this.active) { if(properties == null) { this.active = false; if(complete) { this.apply(); } this.complete(sendEvent); return; } var _g = 0; var _g1 = (Reflect().default).fields(properties); while(_g < _g1.length) { var i = _g1[_g]; ++_g; if((Reflect().default).hasField(this.properties,i)) { this.active = false; if(complete) { this.apply(); } this.complete(sendEvent); return; } } } }, update: function(currentTime) { if(!this.paused) { var details; var easing; var i; var tweenPosition = (currentTime - this.timeOffset) / this.duration; if(tweenPosition > 1) { tweenPosition = 1; } if(!this.initialized) { this.initialize(); } if(!this.special) { easing = this._ease.calculate(tweenPosition); var _g1 = 0; var _g = this.detailsLength; while(_g1 < _g) { var i1 = _g1++; details = this.propertyDetails[i1]; this.setProperty(details,details.start + details.change * easing); } } else { if(!this._reverse) { easing = this._ease.calculate(tweenPosition); } else { easing = this._ease.calculate(1 - tweenPosition); } var endValue; var _g11 = 0; var _g2 = this.detailsLength; while(_g11 < _g2) { var i2 = _g11++; details = this.propertyDetails[i2]; if(this._smartRotation && (details.propertyName == "rotation" || details.propertyName == "rotationX" || details.propertyName == "rotationY" || details.propertyName == "rotationZ")) { var rotation = details.change % 360; if(rotation > 180) { rotation -= 360; } else if(rotation < -180) { rotation += 360; } endValue = details.start + rotation * easing; } else { endValue = details.start + details.change * easing; } if(!this._snapping) { this.setProperty(details,endValue); } else { this.setProperty(details,Math.round(endValue)); } } } if(tweenPosition == 1) { if(this._repeat == 0) { this.active = false; if(this.toggleVisible && this.getField(this.target,"alpha") == 0) { this.setField(this.target,"visible",false); } this.complete(true); return; } else { if(this._onRepeat != null) { this.callMethod(this._onRepeat,this._onRepeatParams); } if(this._reflect) { this._reverse = !this._reverse; } this.startTime = currentTime; this.timeOffset = this.startTime + this._delay; if(this._repeat > 0) { this._repeat--; } } } if(this.sendChange) { this.change(); } } } }); SimpleActuator.prototype.__class__ = SimpleActuator.prototype.constructor = $hxClasses["motion.actuators.SimpleActuator"] = SimpleActuator; // Init // Statics SimpleActuator.stage_onEnterFrame = function(event) { var currentTime = (openfl_Lib().default).getTimer() / 1000; var actuator; var j = 0; var cleanup = false; var _g1 = 0; var _g = SimpleActuator.actuatorsLength; while(_g1 < _g) { var i = _g1++; actuator = SimpleActuator.actuators[j]; if(actuator != null && actuator.active) { if(currentTime >= actuator.timeOffset) { actuator.update(currentTime); } ++j; } else { SimpleActuator.actuators.splice(j,1); --SimpleActuator.actuatorsLength; } } } SimpleActuator.actuators = [] SimpleActuator.actuatorsLength = 0 SimpleActuator.addedEvent = false // Export exports.default = SimpleActuator;