starling-framework
Version:
A fast, productive library for 2D cross-platform development.
359 lines (342 loc) • 12.9 kB
JavaScript
// Class: starling.display.MovieClip
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 starling_animation_IAnimatable() {return require("./../../starling/animation/IAnimatable");}
function starling_display_Image() {return require("./../../starling/display/Image");}
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 starling_display__$MovieClip_MovieClipFrame() {return require("./../../starling/display/_MovieClip/MovieClipFrame");}
function openfl_errors_IllegalOperationError() {return $import(require("openfl/errors/IllegalOperationError"));}
// Constructor
var MovieClip = function(textures,fps) {
if(fps == null) {
fps = 12;
}
this.__soundTransform = null;
if(textures.length > 0) {
(starling_display_Image().default).call(this,textures[0]);
this.init(textures,fps);
} else {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Empty texture array"));
}
}
// Meta
MovieClip.__name__ = ["starling","display","MovieClip"];
MovieClip.__interfaces__ = [(starling_animation_IAnimatable().default)];
MovieClip.__super__ = (starling_display_Image().default);
MovieClip.prototype = $extend((starling_display_Image().default).prototype, {
init: function(textures,fps) {
if(fps <= 0) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid fps: " + fps));
}
var numFrames = textures.length;
this.__defaultFrameDuration = 1.0 / fps;
this.__loop = true;
this.__playing = true;
this.__currentTime = 0.0;
this.__currentFrameID = 0;
this.__wasStopped = true;
this.__frames = (openfl__$Vector_Vector_$Impl_$().default)._new();
var _g1 = 0;
var _g = numFrames;
while(_g1 < _g) {
var i = _g1++;
(openfl__$Vector_Vector_$Impl_$().default).set(this.__frames,i,new (starling_display__$MovieClip_MovieClipFrame().default)(textures[i],this.__defaultFrameDuration,this.__defaultFrameDuration * i));
}
},
addFrame: function(texture,sound,duration) {
if(duration == null) {
duration = -1;
}
this.addFrameAt(this.get_numFrames(),texture,sound,duration);
},
addFrameAt: function(frameID,texture,sound,duration) {
if(duration == null) {
duration = -1;
}
if(frameID < 0 || frameID > this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
if(duration < 0) {
duration = this.__defaultFrameDuration;
}
var frame = new (starling_display__$MovieClip_MovieClipFrame().default)(texture,duration);
frame.sound = sound;
(openfl__$Vector_Vector_$Impl_$().default).insertAt(this.__frames,frameID,frame);
if(frameID == this.get_numFrames()) {
var prevStartTime = frameID > 0 ? this.__frames[frameID - 1].startTime : 0.0;
var prevDuration = frameID > 0 ? this.__frames[frameID - 1].duration : 0.0;
frame.startTime = prevStartTime + prevDuration;
} else {
this.updateStartTimes();
}
},
removeFrameAt: function(frameID) {
if(frameID < 0 || frameID >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
if(this.get_numFrames() == 1) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_IllegalOperationError().default)("Movie clip must not be empty"));
}
(openfl__$Vector_Vector_$Impl_$().default).removeAt(this.__frames,frameID);
if(frameID != this.get_numFrames()) {
this.updateStartTimes();
}
},
getFrameTexture: function(frameID) {
if(frameID < 0 || frameID >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
return this.__frames[frameID].texture;
},
setFrameTexture: function(frameID,texture) {
if(frameID < 0 || frameID >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
this.__frames[frameID].texture = texture;
},
getFrameSound: function(frameID) {
if(frameID < 0 || frameID >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
return this.__frames[frameID].sound;
},
setFrameSound: function(frameID,sound) {
if(frameID < 0 || frameID >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
this.__frames[frameID].sound = sound;
},
getFrameAction: function(frameID) {
if(frameID < 0 || frameID >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
return this.__frames[frameID].action;
},
setFrameAction: function(frameID,action) {
if(frameID < 0 || frameID >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
this.__frames[frameID].action = action;
},
getFrameDuration: function(frameID) {
if(frameID < 0 || frameID >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
return this.__frames[frameID].duration;
},
setFrameDuration: function(frameID,duration) {
if(frameID < 0 || frameID >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
this.__frames[frameID].duration = duration;
this.updateStartTimes();
},
reverseFrames: function() {
var this1 = this.__frames;
Array.prototype.reverse.call(this1);
this.__currentTime = this.get_totalTime() - this.__currentTime;
this.__currentFrameID = this.get_numFrames() - this.__currentFrameID - 1;
this.updateStartTimes();
},
play: function() {
this.__playing = true;
},
pause: function() {
this.__playing = false;
},
stop: function() {
this.__playing = false;
this.__wasStopped = true;
this.set_currentFrame(0);
},
updateStartTimes: function() {
var numFrames = this.get_numFrames();
var prevFrame = this.__frames[0];
prevFrame.startTime = 0;
var _g1 = 1;
var _g = numFrames;
while(_g1 < _g) {
var i = _g1++;
this.__frames[i].startTime = prevFrame.startTime + prevFrame.duration;
prevFrame = this.__frames[i];
}
},
advanceTime: function(passedTime) {
if(!this.__playing) {
return;
}
var frame = this.__frames[this.__currentFrameID];
if(this.__wasStopped) {
this.__wasStopped = false;
frame.playSound(this.__soundTransform);
if(frame.action != null) {
frame.executeAction(this,this.__currentFrameID);
this.advanceTime(passedTime);
return;
}
}
if(this.__currentTime == this.get_totalTime()) {
if(this.__loop) {
this.__currentTime = 0.0;
this.__currentFrameID = 0;
frame = this.__frames[0];
frame.playSound(this.__soundTransform);
this.set_texture(frame.texture);
if(frame.action != null) {
frame.executeAction(this,this.__currentFrameID);
this.advanceTime(passedTime);
return;
}
} else {
return;
}
}
var finalFrameID = this.__frames.length - 1;
var dispatchCompleteEvent = false;
var frameAction = null;
var previousFrameID = this.__currentFrameID;
var restTimeInFrame = 0;
var changedFrame;
while(this.__currentTime + passedTime >= frame.get_endTime()) {
changedFrame = false;
restTimeInFrame = frame.duration - this.__currentTime + frame.startTime;
passedTime -= restTimeInFrame;
this.__currentTime = frame.startTime + frame.duration;
if(this.__currentFrameID == finalFrameID) {
if(this.hasEventListener("complete")) {
dispatchCompleteEvent = true;
} else if(this.__loop) {
this.__currentTime = 0;
this.__currentFrameID = 0;
changedFrame = true;
} else {
return;
}
} else {
this.__currentFrameID += 1;
changedFrame = true;
}
frame = this.__frames[this.__currentFrameID];
frameAction = frame.action;
if(changedFrame) {
frame.playSound(this.__soundTransform);
}
if(dispatchCompleteEvent) {
this.set_texture(frame.texture);
this.dispatchEventWith("complete");
this.advanceTime(passedTime);
return;
} else if(frameAction != null) {
this.set_texture(frame.texture);
frame.executeAction(this,this.__currentFrameID);
this.advanceTime(passedTime);
return;
}
}
if(previousFrameID != this.__currentFrameID) {
this.set_texture(this.__frames[this.__currentFrameID].texture);
}
this.__currentTime += passedTime;
},
get_numFrames: function() {
return this.__frames.length;
},
get_totalTime: function() {
var lastFrame = this.__frames[this.__frames.length - 1];
return lastFrame.startTime + lastFrame.duration;
},
get_currentTime: function() {
return this.__currentTime;
},
set_currentTime: function(value) {
if(value < 0 || value > this.get_totalTime()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid time: " + value));
}
var lastFrameID = this.__frames.length - 1;
this.__currentTime = value;
this.__currentFrameID = 0;
while(this.__currentFrameID < lastFrameID && this.__frames[this.__currentFrameID + 1].startTime <= value) ++this.__currentFrameID;
var frame = this.__frames[this.__currentFrameID];
this.set_texture(frame.texture);
return value;
},
get_loop: function() {
return this.__loop;
},
set_loop: function(value) {
return this.__loop = value;
},
get_muted: function() {
return this.__muted;
},
set_muted: function(value) {
return this.__muted = value;
},
get_soundTransform: function() {
return this.__soundTransform;
},
set_soundTransform: function(value) {
return this.__soundTransform = value;
},
get_currentFrame: function() {
return this.__currentFrameID;
},
set_currentFrame: function(value) {
if(value < 0 || value >= this.get_numFrames()) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid frame id"));
}
this.set_currentTime(this.__frames[value].startTime);
return value;
},
get_fps: function() {
return 1.0 / this.__defaultFrameDuration;
},
set_fps: function(value) {
if(value <= 0) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid fps: " + value));
}
var newFrameDuration = 1.0 / value;
var acceleration = newFrameDuration / this.__defaultFrameDuration;
this.__currentTime *= acceleration;
this.__defaultFrameDuration = newFrameDuration;
var _g1 = 0;
var _g = this.get_numFrames();
while(_g1 < _g) {
var i = _g1++;
this.__frames[i].duration *= acceleration;
}
this.updateStartTimes();
return value;
},
get_isPlaying: function() {
if(this.__playing) {
if(!this.__loop) {
return this.__currentTime < this.get_totalTime();
} else {
return true;
}
} else {
return false;
}
},
get_isComplete: function() {
if(!this.__loop) {
return this.__currentTime >= this.get_totalTime();
} else {
return false;
}
}
});
MovieClip.prototype.__class__ = MovieClip.prototype.constructor = $hxClasses["starling.display.MovieClip"] = MovieClip;
// Init
Object.defineProperties(MovieClip.prototype,{ "numFrames" : { get : function () { return this.get_numFrames (); }}, "totalTime" : { get : function () { return this.get_totalTime (); }}, "currentTime" : { get : function () { return this.get_currentTime (); }, set : function (v) { return this.set_currentTime (v); }}, "loop" : { get : function () { return this.get_loop (); }, set : function (v) { return this.set_loop (v); }}, "muted" : { get : function () { return this.get_muted (); }, set : function (v) { return this.set_muted (v); }}, "soundTransform" : { get : function () { return this.get_soundTransform (); }, set : function (v) { return this.set_soundTransform (v); }}, "currentFrame" : { get : function () { return this.get_currentFrame (); }, set : function (v) { return this.set_currentFrame (v); }}, "fps" : { get : function () { return this.get_fps (); }, set : function (v) { return this.set_fps (v); }}, "isPlaying" : { get : function () { return this.get_isPlaying (); }}, "isComplete" : { get : function () { return this.get_isComplete (); }}});
// Statics
// Export
exports.default = MovieClip;