2017-09-17 19:12:45 +02:00
|
|
|
/**
|
|
|
|
* Creates a Animation object
|
|
|
|
* @param apiAnimation GameLib.D3.API.Animation
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Animation = function(
|
|
|
|
apiAnimation
|
|
|
|
) {
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiAnimation)) {
|
|
|
|
apiAnimation = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiAnimation instanceof GameLib.D3.Animation) {
|
|
|
|
return 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,
|
2017-09-18 15:39:40 +02:00
|
|
|
apiAnimation.applyToMeshWhenDone,
|
2017-10-03 14:50:34 +02:00
|
|
|
apiAnimation.meshes,
|
2017-09-17 19:12:45 +02:00
|
|
|
apiAnimation.parentEntity
|
|
|
|
);
|
|
|
|
|
|
|
|
this.functionType = GameLib.D3.Animation.ANIMATION_FUNCTION_TYPE_ROTATION;
|
|
|
|
|
|
|
|
this.editor = null;
|
|
|
|
|
2017-09-18 15:39:40 +02:00
|
|
|
/**
|
|
|
|
* This indicates whether an animation is currently in process - for blocking to take effect
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.inProcess = false;
|
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
GameLib.Component.call(
|
|
|
|
this,
|
2017-10-03 14:50:34 +02:00
|
|
|
GameLib.Component.COMPONENT_ANIMATION,
|
|
|
|
{
|
|
|
|
'meshes' : [GameLib.D3.Mesh]
|
|
|
|
}
|
2017-09-17 19:12:45 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Animation.prototype = Object.create(GameLib.D3.API.Animation.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() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
var instance = {
|
|
|
|
rotation : null,
|
|
|
|
translation : null,
|
|
|
|
scale : null
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.rotationFn) {
|
|
|
|
instance.rotation = new Function(
|
|
|
|
'data',
|
|
|
|
this.rotationFn
|
|
|
|
).bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.translationFn) {
|
|
|
|
instance.translation = new Function(
|
|
|
|
'data',
|
|
|
|
this.translationFn
|
|
|
|
).bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.scaleFn) {
|
|
|
|
instance.scale = new Function(
|
|
|
|
'data',
|
|
|
|
this.scaleFn
|
|
|
|
).bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
/**
|
|
|
|
* Return true here to indicate that even though the compilation failed, the instance will be fine and
|
|
|
|
* this component loaded fine.
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
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,
|
2017-09-18 15:39:40 +02:00
|
|
|
this.applyToMeshWhenDone,
|
2017-10-03 14:50:34 +02:00
|
|
|
this.meshes.map(
|
|
|
|
function(mesh) {
|
|
|
|
return GameLib.Utils.IdOrNull(mesh);
|
|
|
|
}
|
|
|
|
),
|
2017-09-17 19:12:45 +02:00
|
|
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts from an Object Animation to a GameLib.D3.Animation
|
|
|
|
* @param objectAnimation Object
|
|
|
|
* @returns {GameLib.D3.Animation}
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-10-09 12:45:29 +02:00
|
|
|
GameLib.D3.Animation.FromObject = function(objectAnimation) {
|
2017-09-17 19:12:45 +02:00
|
|
|
var apiAnimation = GameLib.D3.API.Animation.FromObject(objectAnimation);
|
|
|
|
return new GameLib.D3.Animation(
|
|
|
|
apiAnimation
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Animation.prototype.launchEditor = function(){
|
|
|
|
|
2017-10-09 12:45:29 +02:00
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.GET_CODER_IMPLEMENTATION,
|
|
|
|
null,
|
|
|
|
function(coder) {
|
|
|
|
this.coder = coder;
|
|
|
|
this.coder.isNotCodeMirrorThrow();
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
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);
|
2017-10-03 14:50:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.Animation.prototype.addMesh = function(mesh) {
|
2017-10-05 14:35:22 +02:00
|
|
|
|
|
|
|
GameLib.Utils.PushUnique(this.meshes, mesh);
|
|
|
|
|
2017-10-03 14:50:34 +02:00
|
|
|
GameLib.Event.Emit(
|
|
|
|
GameLib.Event.ANIMATION_MESH_ADDED,
|
|
|
|
{
|
|
|
|
animation : this,
|
|
|
|
mesh : mesh
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-10-05 14:35:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-09-17 19:12:45 +02:00
|
|
|
};
|