r3-legacy/src/r3-d3-api-z-animation.js

290 lines
7.2 KiB
JavaScript

/**
* Creates a Animation object
* @param apiAnimation R3.D3.API.Animation
* @constructor
*/
R3.D3.Animation = function(
apiAnimation
) {
if (R3.Utils.UndefinedOrNull(apiAnimation)) {
apiAnimation = {};
}
R3.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 = R3.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;
R3.Component.call(
this,
{
'meshes' : [R3.D3.Mesh]
}
);
};
R3.D3.Animation.prototype = Object.create(R3.Component.prototype);
R3.D3.Animation.prototype.constructor = R3.D3.Animation;
R3.D3.Animation.ANIMATION_FUNCTION_TYPE_ROTATION = 1;
R3.D3.Animation.ANIMATION_FUNCTION_TYPE_TRANSLATION = 2;
R3.D3.Animation.ANIMATION_FUNCTION_TYPE_SCALE = 3;
R3.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(
R3.Event.ANIMATION_COMPILE_FAILED,
{
component : this
}
)
}
R3.Component.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
R3.D3.Animation.prototype.updateInstance = function(property, item) {
if (property === 'rotationFn') {
try {
this.instance.rotation = new Function('data', this.rotationFn).bind(this);
this.publish(
R3.Event.ANIMATION_COMPILE_SUCCESS,
{
component : this,
type : R3.D3.Animation.ANIMATION_FUNCTION_TYPE_ROTATION
}
)
} catch (error) {
console.error(error);
this.publish(
R3.Event.ANIMATION_COMPILE_FAILED,
{
component : this
}
)
}
}
if (property === 'translationFn') {
try {
this.instance.translation = new Function('data', this.translationFn).bind(this);
this.publish(
R3.Event.ANIMATION_COMPILE_SUCCESS,
{
component : this,
type : R3.D3.Animation.ANIMATION_FUNCTION_TYPE_TRANSLATION
}
);
} catch (error) {
console.error(error);
this.publish(
R3.Event.ANIMATION_COMPILE_FAILED,
{
component : this
}
)
}
}
if (property === 'scaleFn') {
try {
this.instance.scale = new Function('data', this.scaleFn).bind(this);
this.publish(
R3.Event.ANIMATION_COMPILE_SUCCESS,
{
component : this,
type : R3.D3.Animation.ANIMATION_FUNCTION_TYPE_SCALE
}
)
} catch (error) {
console.error(error);
this.publish(
R3.Event.ANIMATION_COMPILE_FAILED,
{
component : this
}
)
}
}
if (property === 'meshes') {
// R3.Event.Emit(
// R3.Event.ANIMATION_MESH_ADDED,
// {
// animation : this,
// mesh : item
// }
// )
}
R3.Component.prototype.updateInstance.call(this, property);
};
/**
* Converts a R3.D3.Animation to a new R3.D3.API.Animation
* @returns {R3.D3.API.Animation}
*/
R3.D3.Animation.prototype.toApiObject = function() {
return new R3.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 R3.Utils.IdOrNull(mesh);
}
),
R3.Utils.IdOrNull(this.parentEntity)
);
};
R3.D3.Animation.prototype.launchEditor = function(){
R3.Event.Emit(
R3.Event.GET_RUNTIME,
null,
function(runtime) {
this.coder = runtime.coder;
this.coder.isNotCodeMirrorThrow();
}.bind(this)
);
var property = null;
if (this.functionType === R3.D3.Animation.ANIMATION_FUNCTION_TYPE_ROTATION) {
this.rotationFn = '//when the animation is complete, return true\nreturn true;';
property = 'rotationFn';
}
if (this.functionType === R3.D3.Animation.ANIMATION_FUNCTION_TYPE_TRANSLATION) {
this.translationFn = '//when the animation is complete, return true\nreturn true;';
property = 'translationFn';
}
if (this.functionType === R3.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');
}
};
R3.D3.Animation.prototype.closeEditor = function(){
var dom = this.editor.getWrapperElement();
dom.parentElement.removeChild(dom);
};
// R3.D3.Animation.prototype.addMesh = function(mesh) {
//
// R3.Utils.PushUnique(this.meshes, mesh);
//
// R3.Event.Emit(
// R3.Event.ANIMATION_MESH_ADDED,
// {
// animation : this,
// mesh : mesh
// }
// )
//
// };
//
// R3.D3.Animation.prototype.removeMesh = function(mesh) {
//
// var index = this.meshes.indexOf(mesh);
//
// this.meshes.splice(index, 1);
//
// R3.Event.Emit(
// R3.Event.ANIMATION_MESH_REMOVED,
// {
// animation : this,
// mesh : mesh
// }
// )
//
// };