array controls for gui, start work on subscribeOnce

beta.r3js.org
Theunis J. Botha 2017-06-22 14:04:03 +02:00
parent 9408afba28
commit c00ad74e99
4 changed files with 166 additions and 78 deletions

View File

@ -10,6 +10,7 @@ GameLib.Event = function() {
* @type {{}} * @type {{}}
*/ */
GameLib.Event.Subscriptions = {}; GameLib.Event.Subscriptions = {};
GameLib.Event.OnceSubscriptions = {};
/** /**
* Events we can subscribe to and publish * Events we can subscribe to and publish
@ -67,6 +68,39 @@ GameLib.Event.prototype.subscribe = function(
} }
}; };
/**
* Stop listening for this event after the callback returns true
* @param eventName
* @param callback
* @returns {{fn, remove: remove}}
*/
GameLib.Event.prototype.subscribeOnce = function(
eventName,
callback
) {
var fn = callback.bind(this);
if (GameLib.Event.OnceSubscriptions.hasOwnProperty(eventName)) {
GameLib.Event.OnceSubscriptions[eventName].push(fn);
} else {
GameLib.Event.OnceSubscriptions[eventName] = [];
GameLib.Event.OnceSubscriptions[eventName].push(fn);
}
var index = GameLib.Event.OnceSubscriptions[eventName].indexOf(fn);
/**
* Return a handle to the caller to allow us to unsubscribe to this event
*/
return {
fn : fn,
remove : function() {
GameLib.Event.Subscriptions[eventName].splice(index, 1);
}
}
};
/** /**
* *
* @param eventName * @param eventName

View File

@ -123,7 +123,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) {
if (objectScene.meshes) { if (objectScene.meshes) {
apiMeshes = objectScene.meshes.map( apiMeshes = objectScene.meshes.map(
function(objectMesh) { function(objectMesh) {
return GameLib.D3.API.Mesh.FromObject(objectMesh); if (objectMesh instanceof Object){
return GameLib.D3.API.Mesh.FromObject(objectMesh);
} else {
return objectMesh;
}
} }
) )
} }
@ -131,7 +135,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) {
if (objectScene.lights) { if (objectScene.lights) {
apiLights = objectScene.lights.map( apiLights = objectScene.lights.map(
function(objectLight) { function(objectLight) {
return GameLib.D3.API.Light.FromObject(objectLight); if (objectLight instanceof Object) {
return GameLib.D3.API.Light.FromObject(objectLight);
} else {
return objectLight;
}
} }
) )
} }
@ -139,7 +147,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) {
if (objectScene.textures) { if (objectScene.textures) {
apiTextures = objectScene.textures.map( apiTextures = objectScene.textures.map(
function(objectTexture) { function(objectTexture) {
return GameLib.D3.API.Texture.FromObject(objectTexture) if (objectTexture instanceof Object) {
return GameLib.D3.API.Texture.FromObject(objectTexture)
} else {
return objectTexture;
}
} }
) )
} }
@ -147,7 +159,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) {
if (objectScene.materials) { if (objectScene.materials) {
apiMaterials = objectScene.materials.map( apiMaterials = objectScene.materials.map(
function(objectMaterial) { function(objectMaterial) {
return GameLib.D3.API.Material.FromObject(objectMaterial) if (objectMaterial instanceof Object) {
return GameLib.D3.API.Material.FromObject(objectMaterial)
} else {
return objectMaterial;
}
} }
) )
} }
@ -155,7 +171,11 @@ GameLib.D3.API.Scene.FromObject = function(objectScene) {
if (objectScene.images) { if (objectScene.images) {
apiImages = objectScene.images.map( apiImages = objectScene.images.map(
function(objectImage) { function(objectImage) {
return GameLib.D3.API.Image.FromObject(objectImage) if (objectImage instanceof Object) {
return GameLib.D3.API.Image.FromObject(objectImage)
} else {
return objectImage;
}
} }
) )
} }

View File

@ -50,8 +50,7 @@ GameLib.D3.Scene = function (
); );
} else { } else {
console.warn('apiMesh not an instance of API.Mesh'); return apiMesh;
throw new Error('apiMesh not an instance of API.Mesh');
} }
}.bind(this) }.bind(this)
@ -86,8 +85,7 @@ GameLib.D3.Scene = function (
); );
} else { } else {
console.warn('apiLight not an instance of API.Light'); return apiLight;
throw new Error('apiLight not an instance of API.Light');
} }
}.bind(this) }.bind(this)
@ -103,12 +101,9 @@ GameLib.D3.Scene = function (
apiTexture apiTexture
); );
// this.idToObject[texture.id] = texture;
return texture; return texture;
} else { } else {
console.warn('apiTexture not an instance of API.Texture'); return apiTexture;
throw new Error('apiTexture not an instance of API.Texture');
} }
}.bind(this) }.bind(this)
@ -124,13 +119,10 @@ GameLib.D3.Scene = function (
apiMaterial apiMaterial
); );
// this.idToObject[material.id] = material;
return material; return material;
} else { } else {
console.warn('apiMaterial not an instance of API.Material'); return apiMaterial;
throw new Error('apiMaterial not an instance of API.Material');
} }
}.bind(this) }.bind(this)
@ -146,13 +138,9 @@ GameLib.D3.Scene = function (
apiImage apiImage
); );
// this.idToObject[image.id] = image;
return image; return image;
} else { } else {
console.warn('apiImage not an instance of API.Image'); return apiImage;
throw new Error('apiImage not an instance of API.Image');
} }
}.bind(this) }.bind(this)
@ -165,14 +153,6 @@ GameLib.D3.Scene = function (
); );
} }
/**
* TODO : Refactor (linking ?)
* @type {GameLib.D3.Scene}
*/
// this.idToObject[this.id] = this;
//
// this.linkObjects(this.idToObject);
this.sceneInstanceSubscriptions = []; this.sceneInstanceSubscriptions = [];
this.objectInstanceSubscriptions = []; this.objectInstanceSubscriptions = [];
@ -214,29 +194,30 @@ GameLib.D3.Scene.prototype.createInstance = function() {
instance.quaternion = this.quaternion.instance; instance.quaternion = this.quaternion.instance;
this.meshes.map(function(mesh){ // this.meshes.map(
if (mesh instanceof GameLib.D3.Mesh) { // function(mesh){
if (GameLib.Utils.UndefinedOrNull(mesh.instance)){ // if (mesh instanceof GameLib.D3.Mesh) {
console.warn('No mesh instance'); // if (GameLib.Utils.UndefinedOrNull(mesh.instance)){
throw new Error('No mesh instance'); // this.subscribe()
} // }
instance.add(mesh.instance); // instance.add(mesh.instance);
} else { // } else {
console.log('Unable to add mesh which should be loaded at this point'); // console.log('Unable to add mesh which should be loaded at this point');
} // }
}); // }.bind(this)
// );
this.lights.map(function(light){ //
if (light instanceof GameLib.D3.Light) { // this.lights.map(function(light){
if (GameLib.Utils.UndefinedOrNull(light.instance)){ // if (light instanceof GameLib.D3.Light) {
console.warn('No light instance'); // if (GameLib.Utils.UndefinedOrNull(light.instance)){
throw new Error('No light instance'); // console.warn('No light instance');
} // throw new Error('No light instance');
instance.add(light.instance) // }
} else { // instance.add(light.instance)
console.log('Unable to add light which should be loaded at this point'); // } else {
} // console.log('Unable to add light which should be loaded at this point');
}); // }
// });
return instance; return instance;
}; };

View File

@ -608,8 +608,7 @@ GameLib.GUI.prototype.buildControl = function(folder, object, property, entityMa
handles.push(folder.add(object, property, 0, 1.0).step(0.001).name(property).listen()); handles.push(folder.add(object, property, 0, 1.0).step(0.001).name(property).listen());
} else if ( } else if (
property === 'shininess' || property === 'shininess' ||
property === 'fov'|| property === 'fov'
property === 'envMapIntensity'
) { ) {
handles.push(folder.add(object, property, -255, 255).step(1).name(property).listen()); handles.push(folder.add(object, property, -255, 255).step(1).name(property).listen());
} else if ( } else if (
@ -626,9 +625,10 @@ GameLib.GUI.prototype.buildControl = function(folder, object, property, entityMa
} else if ( } else if (
property === 'near' || property === 'near' ||
property === 'distanceGrain' || property === 'distanceGrain' ||
property === 'bumpScale' property === 'bumpScale' ||
property === 'envMapIntensity'
) { ) {
handles.push(folder.add(object, property, 0, 100).step(0.001).name(property).listen()); handles.push(folder.add(object, property, -10, 100).step(0.001).name(property).listen());
} else if ( } else if (
property === 'heightOffset' || property === 'heightOffset' ||
property === 'rotationFactor' property === 'rotationFactor'
@ -797,7 +797,11 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di
'w', 'w',
-100, -100,
100 100
).name(property + '.w').listen().step(0.1).onChange(function(){object.updateInstance()}); ).name(property + '.w').listen().step(0.1).onChange(
function() {
object.updateInstance();
}
);
} }
folder.add( folder.add(
@ -806,8 +810,8 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di
-100, -100,
100 100
).name(property + '.x').listen().step(0.1).onChange( ).name(property + '.x').listen().step(0.1).onChange(
function(){ function() {
object.updateInstance() object.updateInstance();
} }
); );
@ -816,7 +820,11 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di
'y', 'y',
-100, -100,
100 100
).name(property + '.y').listen().step(0.1).onChange(function(){object.updateInstance()}); ).name(property + '.y').listen().step(0.1).onChange(
function() {
object.updateInstance();
}
);
if ( if (
dimension === 3 || dimension === 3 ||
@ -827,9 +835,11 @@ GameLib.GUI.prototype.buildVectorControl = function(folder, object, property, di
'z', 'z',
-100, -100,
100 100
).name(property + '.z').listen().step(0.1).onChange(function () { ).name(property + '.z').listen().step(0.1).onChange(
object.updateInstance() function() {
}); object.updateInstance();
}
);
} }
}; };
@ -918,24 +928,37 @@ GameLib.GUI.prototype.buildSelectControl = function(folder, object, property, en
/** /**
* *
*/ */
GameLib.GUI.prototype.buildCurrentSelectionControlFromArray = function( GameLib.GUI.prototype.buildArrayManager = function(
folder, folder,
object, object,
property, property,
name constructor,
entityManager
) { ) {
name = name || 'current ' + property; var array = object[property];
object['current' + property] = null; var addArrayItem = function(item, index){
var options = object[property].reduce( var controller = folder.add(
function(result, obj){ {
if (obj instanceof Object) { 'remove' : function() {
result[obj.name] = obj; object[property].splice(object[property].indexOf(item), 1);
} else { folder.remove(controller);
console.log('not an object'); }
} },
'remove'
).name('remove ' + property + '[' + index + '] - ' + item.name);
};
array.map(addArrayItem);
var idToObject = {};
var selectionObject = entityManager.queryComponents(constructor).reduce(
function(result, component) {
result[component.name] = component;
idToObject[component.id] = component;
return result; return result;
}, },
{ {
@ -943,7 +966,35 @@ GameLib.GUI.prototype.buildCurrentSelectionControlFromArray = function(
} }
); );
folder.add(object, 'current' + property, options).name(name).listen() var activeSelection = function(__object, __property) {
var object = __object;
var property = __property;
return {
component : null,
add : function() {
if (object[property].indexOf(activeSelection.component) === -1) {
object[property].push(activeSelection.component);
addArrayItem(activeSelection.component, object[property].length - 1)
}
}
};
}(object, property);
folder.add(activeSelection, 'component', selectionObject).name('select ' + property).onChange(
function(value){
if (value === 'null') {
activeSelection['component'] = null;
} else {
activeSelection['component'] = idToObject[value];
}
}
).listen();
folder.add(activeSelection, 'add').name('add to ' + property);
}; };
GameLib.GUI.prototype.buildEntitySelectionControlFromArray = function( GameLib.GUI.prototype.buildEntitySelectionControlFromArray = function(
@ -1081,10 +1132,12 @@ GameLib.GUI.prototype.build = function(entityManager) {
) )
} else if (object.linkedObjects[property] instanceof Array) { } else if (object.linkedObjects[property] instanceof Array) {
console.log('ignored array : ' + property); console.log('ignored array : ' + property);
this.buildCurrentSelectionControlFromArray( this.buildArrayManager(
folder, folder,
object, object,
property property,
object.linkedObjects[property],
entityManager
) )
} else { } else {
this.buildSelectControl(folder, object, property, entityManager, object.linkedObjects[property], parentObject); this.buildSelectControl(folder, object, property, entityManager, object.linkedObjects[property], parentObject);