starling-framework
Version:
A fast, productive library for 2D cross-platform development.
434 lines (419 loc) • 16.2 kB
JavaScript
// Class: starling.animation.Tween
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 $import = require("./../../import_stub").default;
var $bind = require("./../../bind_stub").default;
var $extend = require("./../../extend_stub").default;
function starling_animation_IAnimatable() {return require("./../../starling/animation/IAnimatable");}
function starling_events_EventDispatcher() {return require("./../../starling/events/EventDispatcher");}
function Reflect() {return require("./../../Reflect");}
function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");}
function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));}
function openfl__$Vector_Vector_$Impl_$() {return require("./../../openfl/_Vector/Vector_Impl_");}
function haxe_Log() {return require("./../../haxe/Log");}
function starling_utils_Color() {return require("./../../starling/utils/Color");}
function Std() {return require("./../../Std");}
function starling_animation_Transitions() {return require("./../../starling/animation/Transitions");}
function HxOverrides() {return require("./../../HxOverrides");}
// Constructor
var Tween = function(target,time,transition) {
if(transition == null) {
transition = "linear";
}
(starling_events_EventDispatcher().default).call(this);
this.reset(target,time,transition);
}
// Meta
Tween.__name__ = "starling.animation.Tween";
Tween.__isInterface__ = false;
Tween.__interfaces__ = [(starling_animation_IAnimatable().default)];
Tween.__super__ = (starling_events_EventDispatcher().default);
Tween.prototype = $extend((starling_events_EventDispatcher().default).prototype, {
reset: function(target,time,transition) {
if(transition == null) {
transition = "linear";
}
this.__target = target;
this.__currentTime = 0.0;
this.__totalTime = Math.max(0.0001,time);
this.__progress = 0.0;
this.__delay = this.__repeatDelay = 0.0;
this.__onStart = this.__onUpdate = this.__onRepeat = this.__onComplete = null;
this.__onStartArgs = this.__onUpdateArgs = this.__onRepeatArgs = this.__onCompleteArgs = null;
this.__roundToInt = this.__reverse = false;
this.__repeatCount = 1;
this.__currentCycle = -1;
this.__nextTween = null;
if(typeof(transition) == "string") {
this.set_transition(transition);
} else if((Reflect().default).isFunction(transition)) {
this.set_transitionFunc(transition);
} else {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Transition must be either a string or a function"));
}
if(this.__properties != null) {
this.__properties.length = 0;
} else {
this.__properties = (openfl__$Vector_Vector_$Impl_$().default)._new();
}
if(this.__startValues != null) {
this.__startValues.length = 0;
} else {
this.__startValues = (openfl__$Vector_Vector_$Impl_$().default)._new();
}
if(this.__endValues != null) {
this.__endValues.length = 0;
} else {
this.__endValues = (openfl__$Vector_Vector_$Impl_$().default)._new();
}
if(this.__updateFuncs != null) {
this.__updateFuncs.length = 0;
} else {
this.__updateFuncs = (openfl__$Vector_Vector_$Impl_$().default)._new();
}
return this;
},
animate: function(property,endValue) {
if(this.__target == null) {
return;
}
var pos = this.__properties.length;
var updateFunc = this.getUpdateFuncFromProperty(property);
(openfl__$Vector_Vector_$Impl_$().default).set(this.__properties,pos,Tween.getPropertyName(property));
(openfl__$Vector_Vector_$Impl_$().default).set(this.__startValues,pos,NaN);
(openfl__$Vector_Vector_$Impl_$().default).set(this.__endValues,pos,endValue);
(openfl__$Vector_Vector_$Impl_$().default).set(this.__updateFuncs,pos,updateFunc);
},
scaleTo: function(factor) {
this.animate("scaleX",factor);
this.animate("scaleY",factor);
},
moveTo: function(x,y) {
this.animate("x",x);
this.animate("y",y);
},
fadeTo: function(alpha) {
this.animate("alpha",alpha);
},
rotateTo: function(angle,type) {
if(type == null) {
type = "rad";
}
this.animate("rotation#" + type,angle);
},
advanceTime: function(time) {
if(time == 0 || this.__repeatCount == 1 && this.__currentTime == this.__totalTime) {
return;
}
var i;
var previousTime = this.__currentTime;
var restTime = this.__totalTime - this.__currentTime;
var carryOverTime = time > restTime ? time - restTime : 0.0;
this.__currentTime += time;
if(this.__currentTime <= 0) {
return;
} else if(this.__currentTime > this.__totalTime) {
this.__currentTime = this.__totalTime;
}
if(this.__currentCycle < 0 && previousTime <= 0 && this.__currentTime > 0) {
this.__currentCycle++;
if(this.__onStart != null) {
if(this.__onStartArgs != null) {
(Reflect().default).callMethod(this.__onStart,this.__onStart,this.__onStartArgs);
} else {
this.__onStart();
}
}
}
var ratio = this.__currentTime / this.__totalTime;
var reversed = this.__reverse && this.__currentCycle % 2 == 1;
var numProperties = this.__startValues.length;
this.__progress = reversed ? this.__transitionFunc(1.0 - ratio) : this.__transitionFunc(ratio);
if(this.__progress != this.__progress) {
this.__progress = 0;
}
var _g = 0;
var _g1 = numProperties;
while(_g < _g1) {
var i1 = _g++;
if(this.__startValues[i1] != this.__startValues[i1]) {
(openfl__$Vector_Vector_$Impl_$().default).set(this.__startValues,i1,(Reflect().default).getProperty(this.__target,this.__properties[i1]));
}
var updateFunc = this.__updateFuncs[i1];
updateFunc(this.__properties[i1],this.__startValues[i1],this.__endValues[i1]);
}
if(this.__onUpdate != null) {
if(this.__onUpdateArgs != null) {
(Reflect().default).callMethod(this.__onUpdate,this.__onUpdate,this.__onUpdateArgs);
} else {
this.__onUpdate();
}
}
if(previousTime < this.__totalTime && this.__currentTime >= this.__totalTime) {
if(this.__repeatCount == 0 || this.__repeatCount > 1) {
this.__currentTime = -this.__repeatDelay;
this.__currentCycle++;
if(this.__repeatCount > 1) {
this.__repeatCount--;
}
if(this.__onRepeat != null) {
if(this.__onRepeatArgs != null) {
(Reflect().default).callMethod(this.__onRepeat,this.__onRepeat,this.__onRepeatArgs);
} else {
this.__onRepeat();
}
}
} else {
var onComplete = this.__onComplete;
var onCompleteArgs = this.__onCompleteArgs;
this.dispatchEventWith("removeFromJuggler");
if(onComplete != null) {
if(onCompleteArgs != null) {
(Reflect().default).callMethod(onComplete,onComplete,onCompleteArgs);
} else {
onComplete();
}
}
if(this.__currentTime == 0) {
carryOverTime = 0;
}
}
}
if(carryOverTime != 0) {
this.advanceTime(carryOverTime);
}
},
getUpdateFuncFromProperty: function(property) {
var updateFunc;
var hint = Tween.getPropertyHint(property);
if(hint == null) {
updateFunc = $bind(this,this.updateStandard);
} else {
switch(hint) {
case "deg":
updateFunc = $bind(this,this.updateDeg);
break;
case "rad":
updateFunc = $bind(this,this.updateRad);
break;
case "rgb":
updateFunc = $bind(this,this.updateRgb);
break;
default:
(haxe_Log().default).trace("[Starling] Ignoring unknown property hint: " + hint,{ fileName : "../src/starling/animation/Tween.hx", lineNumber : 321, className : "starling.animation.Tween", methodName : "getUpdateFuncFromProperty"});
updateFunc = $bind(this,this.updateStandard);
}
}
return updateFunc;
},
updateStandard: function(property,startValue,endValue) {
var newValue = startValue + this.__progress * (endValue - startValue);
if(this.__roundToInt) {
newValue = Math.round(newValue);
}
(Reflect().default).setProperty(this.__target,property,newValue);
},
updateRgb: function(property,startValue,endValue) {
(Reflect().default).setProperty(this.__target,property,(starling_utils_Color().default).interpolate((Std().default).int(startValue),(Std().default).int(endValue),this.__progress));
},
updateRad: function(property,startValue,endValue) {
this.updateAngle(Math.PI,property,startValue,endValue);
},
updateDeg: function(property,startValue,endValue) {
this.updateAngle(180,property,startValue,endValue);
},
updateAngle: function(pi,property,startValue,endValue) {
while(Math.abs(endValue - startValue) > pi) if(startValue < endValue) {
endValue -= 2.0 * pi;
} else {
endValue += 2.0 * pi;
}
this.updateStandard(property,startValue,endValue);
},
getEndValue: function(property) {
var this1 = this.__properties;
var index = (Array.prototype.indexOf.call)(this1,property,0);
if(index == -1) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("The property '" + property + "' is not animated"));
} else {
return this.__endValues[index];
}
},
animatesProperty: function(property) {
var this1 = this.__properties;
return (Array.prototype.indexOf.call)(this1,property,0) != -1;
},
get_isComplete: function() {
if(this.__currentTime >= this.__totalTime) {
return this.__repeatCount == 1;
} else {
return false;
}
},
get_target: function() {
return this.__target;
},
get_transition: function() {
return this.__transitionName;
},
set_transition: function(value) {
this.__transitionName = value;
this.__transitionFunc = (starling_animation_Transitions().default).getTransition(value);
if(this.__transitionFunc == null) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid transiton: " + value));
}
return value;
},
get_transitionFunc: function() {
return this.__transitionFunc;
},
set_transitionFunc: function(value) {
this.__transitionName = "custom";
this.__transitionFunc = value;
return value;
},
get_totalTime: function() {
return this.__totalTime;
},
get_currentTime: function() {
return this.__currentTime;
},
get_progress: function() {
return this.__progress;
},
get_delay: function() {
return this.__delay;
},
set_delay: function(value) {
this.__currentTime = this.__currentTime + this.__delay - value;
this.__delay = value;
return value;
},
get_repeatCount: function() {
return this.__repeatCount;
},
set_repeatCount: function(value) {
return this.__repeatCount = value;
},
get_repeatDelay: function() {
return this.__repeatDelay;
},
set_repeatDelay: function(value) {
return this.__repeatDelay = value;
},
get_reverse: function() {
return this.__reverse;
},
set_reverse: function(value) {
return this.__reverse = value;
},
get_roundToInt: function() {
return this.__roundToInt;
},
set_roundToInt: function(value) {
return this.__roundToInt = value;
},
get_onStart: function() {
return this.__onStart;
},
set_onStart: function(value) {
return this.__onStart = value;
},
get_onUpdate: function() {
return this.__onUpdate;
},
set_onUpdate: function(value) {
return this.__onUpdate = value;
},
get_onRepeat: function() {
return this.__onRepeat;
},
set_onRepeat: function(value) {
return this.__onRepeat = value;
},
get_onComplete: function() {
return this.__onComplete;
},
set_onComplete: function(value) {
return this.__onComplete = value;
},
get_onStartArgs: function() {
return this.__onStartArgs;
},
set_onStartArgs: function(value) {
return this.__onStartArgs = value;
},
get_onUpdateArgs: function() {
return this.__onUpdateArgs;
},
set_onUpdateArgs: function(value) {
return this.__onUpdateArgs = value;
},
get_onRepeatArgs: function() {
return this.__onRepeatArgs;
},
set_onRepeatArgs: function(value) {
return this.__onRepeatArgs = value;
},
get_onCompleteArgs: function() {
return this.__onCompleteArgs;
},
set_onCompleteArgs: function(value) {
return this.__onCompleteArgs = value;
},
get_nextTween: function() {
return this.__nextTween;
},
set_nextTween: function(value) {
return this.__nextTween = value;
}
});
Tween.prototype.__class__ = Tween.prototype.constructor = $hxClasses["starling.animation.Tween"] = Tween;
// Init
Object.defineProperties(Tween.prototype,{ isComplete : { get : function () { return this.get_isComplete (); }}, target : { get : function () { return this.get_target (); }}, transition : { get : function () { return this.get_transition (); }, set : function (v) { return this.set_transition (v); }}, transitionFunc : { get : function () { return this.get_transitionFunc (); }, set : function (v) { return this.set_transitionFunc (v); }}, totalTime : { get : function () { return this.get_totalTime (); }}, currentTime : { get : function () { return this.get_currentTime (); }}, progress : { get : function () { return this.get_progress (); }}, delay : { get : function () { return this.get_delay (); }, set : function (v) { return this.set_delay (v); }}, repeatCount : { get : function () { return this.get_repeatCount (); }, set : function (v) { return this.set_repeatCount (v); }}, repeatDelay : { get : function () { return this.get_repeatDelay (); }, set : function (v) { return this.set_repeatDelay (v); }}, reverse : { get : function () { return this.get_reverse (); }, set : function (v) { return this.set_reverse (v); }}, roundToInt : { get : function () { return this.get_roundToInt (); }, set : function (v) { return this.set_roundToInt (v); }}, onStart : { get : function () { return this.get_onStart (); }, set : function (v) { return this.set_onStart (v); }}, onUpdate : { get : function () { return this.get_onUpdate (); }, set : function (v) { return this.set_onUpdate (v); }}, onRepeat : { get : function () { return this.get_onRepeat (); }, set : function (v) { return this.set_onRepeat (v); }}, onComplete : { get : function () { return this.get_onComplete (); }, set : function (v) { return this.set_onComplete (v); }}, onStartArgs : { get : function () { return this.get_onStartArgs (); }, set : function (v) { return this.set_onStartArgs (v); }}, onUpdateArgs : { get : function () { return this.get_onUpdateArgs (); }, set : function (v) { return this.set_onUpdateArgs (v); }}, onRepeatArgs : { get : function () { return this.get_onRepeatArgs (); }, set : function (v) { return this.set_onRepeatArgs (v); }}, onCompleteArgs : { get : function () { return this.get_onCompleteArgs (); }, set : function (v) { return this.set_onCompleteArgs (v); }}, nextTween : { get : function () { return this.get_nextTween (); }, set : function (v) { return this.set_nextTween (v); }}});
// Statics
Tween.getPropertyHint = function(property) {
if(property.indexOf("color") != -1 || property.indexOf("Color") != -1) {
return "rgb";
}
var hintMarkerIndex = property.indexOf("#");
if(hintMarkerIndex != -1) {
return (HxOverrides().default).substr(property,hintMarkerIndex + 1,null);
} else {
return null;
}
}
Tween.getPropertyName = function(property) {
var hintMarkerIndex = property.indexOf("#");
if(hintMarkerIndex != -1) {
return property.substring(0,hintMarkerIndex);
} else {
return property;
}
}
Tween.fromPool = function(target,time,transition) {
if(transition == null) {
transition = "linear";
}
if(Tween.sTweenPool.length != 0) {
return (openfl__$Vector_Vector_$Impl_$().default).pop(Tween.sTweenPool).reset(target,time,transition);
} else {
return new Tween(target,time,transition);
}
}
Tween.toPool = function(tween) {
tween.__onStart = tween.__onUpdate = tween.__onRepeat = tween.__onComplete = null;
tween.__onStartArgs = tween.__onUpdateArgs = tween.__onRepeatArgs = tween.__onCompleteArgs = null;
tween.__target = null;
tween.__transitionFunc = null;
tween.removeEventListeners();
(openfl__$Vector_Vector_$Impl_$().default).push(Tween.sTweenPool,tween);
}
Tween.HINT_MARKER = "#"
Tween.sTweenPool = (openfl__$Vector_Vector_$Impl_$().default)._new()
// Export
exports.default = Tween;