272 lines
6.8 KiB
JavaScript
272 lines
6.8 KiB
JavaScript
/**
|
|
* Creates a Animation object
|
|
* @param apiAnimation GameLib.D3.API.Animation
|
|
* @constructor
|
|
*/
|
|
GameLib.D3.Animation = function(
|
|
apiAnimation
|
|
) {
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiAnimation)) {
|
|
apiAnimation = {};
|
|
}
|
|
|
|
GameLib.D3.API.Animation.call(
|
|
this,
|
|
apiAnimation.id,
|
|
apiAnimation.name,
|
|
apiAnimation.rotationSpeed,
|
|
apiAnimation.translationSpeed,
|
|
apiAnimation.scaleSpeed,
|
|
apiAnimation.rotationFn,
|
|
apiAnimation.translationFn,
|
|
apiAnimation.scaleFn,
|
|
apiAnimation.blocking,
|
|
apiAnimation.applyToMeshWhenDone,
|
|
apiAnimation.meshes,
|
|
apiAnimation.parentEntity
|
|
);
|
|
|
|
this.functionType = GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_ROTATION;
|
|
|
|
this.editor = null;
|
|
|
|
/**
|
|
* This indicates whether an animation is currently in process - for blocking to take effect
|
|
* @type {boolean}
|
|
*/
|
|
this.inProcess = false;
|
|
|
|
GameLib.Component.call(
|
|
this,
|
|
{
|
|
'meshes' : [GameLib.D3.Mesh]
|
|
}
|
|
);
|
|
};
|
|
|
|
GameLib.D3.Animation.prototype = Object.create(GameLib.Component.prototype);
|
|
GameLib.D3.Animation.prototype.constructor = GameLib.D3.Animation;
|
|
|
|
GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_ROTATION = 1;
|
|
GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_TRANSLATION = 2;
|
|
GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_SCALE = 3;
|
|
|
|
GameLib.D3.Animation.prototype.createInstance = function() {
|
|
|
|
this.instance = {
|
|
rotation : null,
|
|
translation : null,
|
|
scale : null
|
|
};
|
|
|
|
try {
|
|
if (this.rotationFn) {
|
|
this.instance.rotation = new Function(
|
|
'data',
|
|
this.rotationFn
|
|
).bind(this);
|
|
}
|
|
|
|
if (this.translationFn) {
|
|
this.instance.translation = new Function(
|
|
'data',
|
|
this.translationFn
|
|
).bind(this);
|
|
}
|
|
|
|
if (this.scaleFn) {
|
|
this.instance.scale = new Function(
|
|
'data',
|
|
this.scaleFn
|
|
).bind(this);
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
this.publish(
|
|
GameLib.Event.ANIMATION_COMPILE_FAILED,
|
|
{
|
|
component : this
|
|
}
|
|
)
|
|
}
|
|
|
|
GameLib.Component.prototype.createInstance.call(this);
|
|
};
|
|
|
|
/**
|
|
* Updates the instance with the current state
|
|
*/
|
|
GameLib.D3.Animation.prototype.updateInstance = function() {
|
|
|
|
try {
|
|
|
|
if (this.rotationFn) {
|
|
this.instance.rotation = new Function('data', this.rotationFn).bind(this);
|
|
this.publish(
|
|
GameLib.Event.ANIMATION_COMPILE_SUCCESS,
|
|
{
|
|
component : this,
|
|
type : GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_ROTATION
|
|
}
|
|
)
|
|
}
|
|
|
|
if (this.translationFn) {
|
|
this.instance.translation = new Function('data', this.translationFn).bind(this);
|
|
this.publish(
|
|
GameLib.Event.ANIMATION_COMPILE_SUCCESS,
|
|
{
|
|
component : this,
|
|
type : GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_TRANSLATION
|
|
}
|
|
)
|
|
}
|
|
|
|
if (this.scaleFn) {
|
|
this.instance.scale = new Function('data', this.scaleFn).bind(this);
|
|
this.publish(
|
|
GameLib.Event.ANIMATION_COMPILE_SUCCESS,
|
|
{
|
|
component : this,
|
|
type : GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_SCALE
|
|
}
|
|
)
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error(error);
|
|
this.publish(
|
|
GameLib.Event.ANIMATION_COMPILE_FAILED,
|
|
{
|
|
component : this
|
|
}
|
|
)
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Converts a GameLib.D3.Animation to a new GameLib.D3.API.Animation
|
|
* @returns {GameLib.D3.API.Animation}
|
|
*/
|
|
GameLib.D3.Animation.prototype.toApiObject = function() {
|
|
|
|
return new GameLib.D3.API.Animation(
|
|
this.id,
|
|
this.name,
|
|
this.rotationSpeed,
|
|
this.translationSpeed,
|
|
this.scaleSpeed,
|
|
this.rotationFn,
|
|
this.translationFn,
|
|
this.scaleFn,
|
|
this.blocking,
|
|
this.applyToMeshWhenDone,
|
|
this.meshes.map(
|
|
function(mesh) {
|
|
return GameLib.Utils.IdOrNull(mesh);
|
|
}
|
|
),
|
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
|
);
|
|
|
|
};
|
|
|
|
/**
|
|
* Converts from an Object Animation to a GameLib.D3.Animation
|
|
* @param objectAnimation Object
|
|
* @returns {GameLib.D3.Animation}
|
|
* @constructor
|
|
*/
|
|
GameLib.D3.Animation.FromObject = function(objectAnimation) {
|
|
var apiAnimation = GameLib.D3.API.Animation.FromObject(objectAnimation);
|
|
return new GameLib.D3.Animation(
|
|
apiAnimation
|
|
);
|
|
};
|
|
|
|
GameLib.D3.Animation.prototype.launchEditor = function(){
|
|
|
|
GameLib.Event.Emit(
|
|
GameLib.Event.GET_RUNTIME,
|
|
null,
|
|
function(runtime) {
|
|
this.coder = runtime.coder;
|
|
this.coder.isNotCodeMirrorThrow();
|
|
}.bind(this)
|
|
);
|
|
|
|
var property = null;
|
|
|
|
if (this.functionType === GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_ROTATION) {
|
|
this.rotationFn = '//when the animation is complete, return true\nreturn true;';
|
|
property = 'rotationFn';
|
|
}
|
|
|
|
if (this.functionType === GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_TRANSLATION) {
|
|
this.translationFn = '//when the animation is complete, return true\nreturn true;';
|
|
property = 'translationFn';
|
|
}
|
|
|
|
if (this.functionType === GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_SCALE) {
|
|
this.scaleFn = '//when the animation is complete, return true\nreturn true;';
|
|
property = 'scaleFn';
|
|
}
|
|
|
|
if (property) {
|
|
this.editor = this.coder.instance(
|
|
document.body,
|
|
{
|
|
value : this[property],
|
|
mode : 'javascript',
|
|
lineNumbers : true,
|
|
scrollbarStyle : 'overlay'
|
|
}
|
|
);
|
|
|
|
this.editor.on('change', function(){
|
|
|
|
this[property] = this.editor.getValue();
|
|
|
|
this.updateInstance();
|
|
|
|
}.bind(this))
|
|
} else {
|
|
console.warn('invalid function type selected');
|
|
}
|
|
};
|
|
|
|
GameLib.D3.Animation.prototype.closeEditor = function(){
|
|
var dom = this.editor.getWrapperElement();
|
|
dom.parentElement.removeChild(dom);
|
|
};
|
|
|
|
GameLib.D3.Animation.prototype.addMesh = function(mesh) {
|
|
|
|
GameLib.Utils.PushUnique(this.meshes, mesh);
|
|
|
|
GameLib.Event.Emit(
|
|
GameLib.Event.ANIMATION_MESH_ADDED,
|
|
{
|
|
animation : this,
|
|
mesh : mesh
|
|
}
|
|
)
|
|
|
|
};
|
|
|
|
GameLib.D3.Animation.prototype.removeMesh = function(mesh) {
|
|
|
|
var index = this.meshes.indexOf(mesh);
|
|
|
|
this.meshes.splice(index, 1);
|
|
|
|
GameLib.Event.Emit(
|
|
GameLib.Event.ANIMATION_MESH_REMOVED,
|
|
{
|
|
animation : this,
|
|
mesh : mesh
|
|
}
|
|
)
|
|
|
|
}; |