to API Object

beta.r3js.org
cybafelo 2019-10-27 10:58:42 +01:00
parent 1535237170
commit 7c9c8d19bb
7 changed files with 141 additions and 105 deletions

View File

@ -167,7 +167,8 @@ R3.Event.Emit = function(
console.error('failed to execute client callback error:', error2); console.error('failed to execute client callback error:', error2);
} }
} }
return;
} }
if (clientCallback) { if (clientCallback) {

View File

@ -924,12 +924,12 @@ R3.Component.prototype.toApiObject = function() {
/** /**
* Check if this property is an array of something * Check if this property is an array of something
*/ */
if (this[property].isArray) { if (this[property] instanceof Array) {
/** /**
* Quick sanity check that the apiObject also thinks this is an array * Quick sanity check that the apiObject also thinks this is an array
*/ */
if (!arrayObject[property].isArray) { if (!apiObject[property] instanceof Array) {
throw new Error('The API Object ' + apiObject + ' does not seem to think ' + property + ' is an array'); throw new Error('The API Object ' + apiObject + ' does not seem to think ' + property + ' is an array');
} }
@ -944,7 +944,7 @@ R3.Component.prototype.toApiObject = function() {
throw new Error('Please don\'t store Runtime Objects in Arrays'); throw new Error('Please don\'t store Runtime Objects in Arrays');
} }
if (item.isArray) { if (item instanceof Array) {
result[index] = item.reduce( result[index] = item.reduce(
function (subResult, subItem) { function (subResult, subItem) {
subResult.push(this.getPropertyValue(subItem)); subResult.push(this.getPropertyValue(subItem));

View File

@ -16,8 +16,6 @@ R3.API.Controls = function(
apiComponent.canvas = null; apiComponent.canvas = null;
} }
this.canvas = apiComponent.canvas; this.canvas = apiComponent.canvas;
this.previousCanvas = this.canvas;
}; };
R3.API.Controls.prototype = Object.create(R3.API.Component.prototype); R3.API.Controls.prototype = Object.create(R3.API.Component.prototype);

View File

@ -36,6 +36,7 @@ R3.API.CustomCode = function(
"\t * Set some initialization code here\n" + "\t * Set some initialization code here\n" +
"\t */\n" + "\t */\n" +
"\tthis.totalTime = 0;\n" + "\tthis.totalTime = 0;\n" +
"\tproject.cameras[1].position.y = 10;\n" +
"\n" + "\n" +
"\tthis.initialized = true;\n" + "\tthis.initialized = true;\n" +
"}\n" + "}\n" +
@ -44,7 +45,8 @@ R3.API.CustomCode = function(
"\n" + "\n" +
"\tthis.totalTime += project.clock.delta;\n" + "\tthis.totalTime += project.clock.delta;\n" +
"\t\n" + "\t\n" +
"\tproject.cameras[1].position.x = 4 * Math.sin(this.totalTime);\n" + "\tproject.cameras[1].position.x = 15 * Math.sin(this.totalTime * 0.1);\n" +
"\tproject.cameras[1].position.z = 15 * Math.cos(this.totalTime * 0.1);\n" +
"\tproject.cameras[1].lookAt.x = 0;\n" + "\tproject.cameras[1].lookAt.x = 0;\n" +
"\tproject.cameras[1].lookAt.y = 0;\n" + "\tproject.cameras[1].lookAt.y = 0;\n" +
"\tproject.cameras[1].lookAt.z = 0;\n" + "\tproject.cameras[1].lookAt.z = 0;\n" +

View File

@ -32,10 +32,17 @@ R3.API.Quaternion = function(
this.w = apiComponent.w; this.w = apiComponent.w;
if (R3.Utils.UndefinedOrNull(apiComponent.axis)) { if (R3.Utils.UndefinedOrNull(apiComponent.axis)) {
var name = 'Quaternion Axis';
if (this.parent && this.parent.name) {
name = this.parent.name + ' - Quaternion Axis';
}
apiComponent.axis = new R3.API.Vector3( apiComponent.axis = new R3.API.Vector3(
{ {
parent : this.parent, parent : this.parent,
name : this.parent.name + ' - Quaternion Axis', name : name,
register : this.register register : this.register
} }
); );

View File

@ -57,45 +57,57 @@ R3.D3.API.Composer = function(
if (R3.Utils.UndefinedOrNull(apiComponent.passes)) { if (R3.Utils.UndefinedOrNull(apiComponent.passes)) {
apiComponent.passes = this.renderer.scenes.reduce( var passes = [];
function(result, scene) {
result.push( if (this.renderer && this.renderer.scenes) {
new R3.D3.API.Pass.Render( /**
{ * Construct default render passes
parent : this, */
scene : scene, passes = this.renderer.scenes.reduce(
name : this.name + ' - Render Pass', function(result, scene) {
camera : this.camera
}
)
);
return result; result.push(
}.bind(this), new R3.D3.API.Pass.Render(
[] {
); parent : this,
scene : scene,
name : this.name + ' - Render Pass',
camera : this.camera
}
)
);
apiComponent.passes.push( return result;
new R3.D3.API.Pass.Bloom( }.bind(this),
{ []
parent : this, );
name : this.name + ' - Bloom Pass', }
renderer : this.renderer
}
)
)
apiComponent.passes.push( if (this.renderer) {
new R3.D3.API.Pass.FXAA( passes.push(
{ new R3.D3.API.Pass.Bloom(
parent : this, {
name : this.name + ' - FXAA Pass', parent: this,
renderer : this.renderer name: this.name + ' - Bloom Pass',
} renderer: this.renderer
) }
); )
);
}
if (this.renderer) {
passes.push(
new R3.D3.API.Pass.FXAA(
{
parent: this,
name: this.name + ' - FXAA Pass',
renderer: this.renderer
}
)
);
}
apiComponent.passes = passes;
} }
this.passes = apiComponent.passes; this.passes = apiComponent.passes;

View File

@ -273,8 +273,13 @@ R3.System.Storage.prototype.save = function(data, callback, errorCallback) {
var saved = []; var saved = [];
var failed = []; var failed = [];
if (typeof XMLHttpRequest === 'undefined') {
console.log('Implement server side save here');
return;
}
if (this.savedSubscription || this.savedErrorSubscription) { if (this.savedSubscription || this.savedErrorSubscription) {
console.warn('another save is in progress'); console.warn('Another save is in progress');
return; return;
} }
@ -350,6 +355,7 @@ R3.System.Storage.prototype.save = function(data, callback, errorCallback) {
); );
var apiUrl = null; var apiUrl = null;
var apiAuthorization = null;
var event = R3.Event.GET_API_URL; var event = R3.Event.GET_API_URL;
@ -361,98 +367,108 @@ R3.System.Storage.prototype.save = function(data, callback, errorCallback) {
event, event,
null, null,
function(urlData) { function(urlData) {
apiUrl = urlData.apiUrl apiUrl = urlData.apiUrl;
apiAuthorization = urlData.apiAuthorization;
} }
); );
var apiObjects = Object.keys(data.component.idToObject).reduce( try {
function(result, componentId) { var apiObjects = Object.keys(data.component.idToObject).reduce(
var component = R3.EntityManager.Instance.findComponentById(componentId); function (result, componentId) {
var apiObject = component.toApiObject(); var component = R3.EntityManager.Instance.findComponentById(componentId);
result.push(apiObject); var apiObject = component.toApiObject();
return result; result.push(apiObject);
}.bind(this), return result;
[]
); }.bind(this),
[]
);
} catch (error) {
this.savedSubscription.remove();
this.savedErrorSubscription.remove();
this.savedSubscription = null;
this.savedErrorSubscription = null;
throw new Error('An error occurred during save:' + error.message);
if (typeof XMLHttpRequest === 'undefined') {
console.log('Implement server side save here');
return;
} }
apiObjects.map( try {
function(apiObject) {
var xhr = new XMLHttpRequest(); apiObjects.map(
xhr.open( function(apiObject) {
'POST',
apiUrl + '/component/create'
);
xhr.setRequestHeader("Accept", "application/json"); var xhr = new XMLHttpRequest();
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("x-api-authorization", apiAuthorization);
xhr.setRequestHeader('x-api-user-token', this.apiUserToken);
xhr.onload = function() { xhr.open(
'POST',
apiUrl + '/component/create'
);
try { xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("x-api-authorization", apiAuthorization);
xhr.setRequestHeader('x-api-user-token', this.apiUserToken);
xhr.onload = function() {
var response = JSON.parse(xhr.responseText); var response = JSON.parse(xhr.responseText);
} catch (error) { if (response.result === 'success') {
R3.Event.Emit( R3.Event.Emit(
R3.Event.SAVE_COMPONENT_ERROR, R3.Event.COMPONENT_SAVED,
{ {
message: this.responseText, message: response.message || 'Successfully saved the component',
component : apiObject component : apiObject
} }
); );
} } else {
if (response.result === 'success') { R3.Event.Emit(
R3.Event.SAVE_COMPONENT_ERROR,
{
message: response.message || 'The server responded but failed to save the component',
component : apiObject
}
)
R3.Event.Emit( }
R3.Event.COMPONENT_SAVED,
{
message: response.message || 'Successfully saved the component',
component : apiObject
}
);
} else { };
R3.Event.Emit( var data = JSON.stringify(apiObject);
R3.Event.SAVE_COMPONENT_ERROR,
{
message: response.message || 'The server responded but failed to save the component',
component : apiObject
}
)
} xhr.send(
{
component : data
}
);
}
);
}; } catch (error) {
var data = JSON.stringify(apiObject); R3.Event.Emit(
R3.Event.SAVE_COMPONENT_ERROR,
xhr.send( {
{ message: this.responseText,
component : data component : apiObject
} }
); );
}
);
}
}; };