diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index 8a7f399b10ae53da749cdea0c28f091ebde1a6de..a389c5e7db1632cfdcf1c467cd55b2663aa17acc 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -152,7 +152,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' DMACell.prototype.setDeleteMode = function(state){ var material; if (!state && !this.materialName) return;//cell may be deleted by now - if (state) material = materials.deleteMaterial.threeMaterial; + if (state) material = materials.list.deleteMaterial.threeMaterial; else material = this.getMaterial(true); if (!material) return;//no material object found if (this.object3D.children[0].material == material) return; @@ -223,16 +223,16 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' console.warn("no material type set for cell"); return null; } - if (!returnTHREEObject) return materials[this.materialName]; - if (!materials[this.materialName]) { + if (!returnTHREEObject) return materials.list[this.materialName]; + if (!materials.list[this.materialName]) { console.warn("no material object found of type " + this.materialName); return null; } - if (!materials[this.materialName].threeMaterial){ + if (!materials.list[this.materialName].threeMaterial){ console.warn("no three material object found for type "+ this.materialName); return null; } - return materials[this.materialName].threeMaterial; + return materials.list[this.materialName].threeMaterial; }; DMACell.prototype.setOpacity = function(opacity){ @@ -416,7 +416,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' var data = { materialName: this.materialName }; -// if (materials[this.materialName].sparseCells) return data;//material definition in material composites +// if (materials.list[this.materialName].sparseCells) return data;//material definition in material composites // if (this.cells) data.cells = this.cells; return data; }; diff --git a/js/cells/supercells/CompositeCell.js b/js/cells/supercells/CompositeCell.js index 622282f820f8b936ff2ffba5eeb5efe53be42bb7..f90d0a6b162efba58ab6eb001696640475fc9b90 100644 --- a/js/cells/supercells/CompositeCell.js +++ b/js/cells/supercells/CompositeCell.js @@ -12,7 +12,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell', CompositeCell.prototype = Object.create(DMASuperCell.prototype); CompositeCell.prototype._getGeometry = function(){ - var dimensions = materials[this.materialName].dimensions; + var dimensions = materials.list[this.materialName].dimensions; var geo = new THREE.BoxGeometry(dimensions.x*lattice.xScale(), dimensions.y*lattice.yScale(), dimensions.z*lattice.zScale()); geo.applyMatrix(new THREE.Matrix4().makeTranslation((dimensions.x/2-0.5)*lattice.xScale(), (dimensions.y/2-0.5)*lattice.yScale(), (dimensions.z/2-0.5)*lattice.zScale())); return geo; diff --git a/js/lattice/CompositeEditorLattice.js b/js/lattice/CompositeEditorLattice.js index 9c4aaac5104e24faa08e597cd59c91012bbfaeef..bdc6d95771a867095c418813f8f0dae37561ab7f 100644 --- a/js/lattice/CompositeEditorLattice.js +++ b/js/lattice/CompositeEditorLattice.js @@ -16,12 +16,13 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre defaults: _.extend(LatticeBase.prototype.defaults, { name: "", - color: null, + color: null }), __initialize: function(options, callback){ if (!options.id || options.id == "") this.set("id", "super" + this.cid); if (!options.color || options.color == "") this.set("color", makeRandomColor(), {silent:true}); + if (callback) callback(this); }, @@ -54,13 +55,15 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre this.set("color", makeRandomColor()); }, + makeNewCompositeMaterial: function(dimensions){ if (this.get("numCells") == 0) { console.warn("no cells in this composite"); return; } var id = this.get("id"); - materials.setMaterial(id, this.toJSONForSave(dimensions)); + var data = this.toJSONForSave(dimensions); + materials.setMaterial(id, data); }, toJSONForSave: function(dimensions){ diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index 025370a2b8b2dee46d5530922f97c11bc6a2bc56..a674844b0f2b8462d5b030d17f54db43ea03a9ea 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -174,7 +174,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre } self.compositeEditor = new CompositeEditorLattice(_.extend({id:id}, _.omit(data, "sparseCells")), null, function(_self){ var cells = null; - if (data) cells = data.sparseCells; + if (data) cells = JSON.parse(JSON.stringify(data.sparseCells)); _self._updateLatticeType(cells, self._getSubclassForLatticeType()); appState.set("currentNav", "navComposite"); }); diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js index 8c90f7fef8376c0c9af433f17eb3651247f242e2..6b9ee78e2837369be54da71d53213a648275d9cd 100644 --- a/js/materials/DMAMaterials.js +++ b/js/materials/DMAMaterials.js @@ -5,32 +5,58 @@ //everything is a top level material with a threeMaterial object define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], function(_, THREE, appState, lattice, plist, three){ - var materials = { + var materialsList = { deleteMaterial: { color: "#ff0000", threeMaterial:new THREE.MeshLambertMaterial({color:"#ff0000", shading:THREE.FlatShading}) - }, - setMaterial: function(id, data){ - if (id && data === null) return deleteMaterial(id); - if (!materials[id]) materials[id] = {}; - var oldColor = materials[id].color; - - _.each(_.keys(data), function(key){//todo trigger change on all instances - if (data[key] && data[key].x) materials[id][key] = new THREE.Vector3(data[key].x, data[key].y, data[key].z); - else materials[id][key] = data[key]; - }); - - if (materials[id].threeMaterial || oldColor != materials[id].color) changeSingleMaterialColorScheme(id); - return false; } }; + + + + + function setMaterial(id, data){ + if (id && data === null) return deleteMaterial(id); + if (!materialsList[id]) materialsList[id] = {}; + var oldColor = materialsList[id].color; + + var edited = false; + if (materialsList[id].sparseCells) edited = _.isEqual(data.sparseCells, materialsList[id].sparseCells); + + _.each(_.keys(data), function(key){ + if (data[key] && data[key].x) materialsList[id][key] = new THREE.Vector3(data[key].x, data[key].y, data[key].z); + else materialsList[id][key] = data[key]; + }); + + if (edited){ + //todo trigger change on all instances + var allChangedmaterialsList = findAllChangedmaterialsList([id]); + } + + if (materialsList[id].threeMaterial || oldColor != materialsList[id].color) changeSingleMaterialColorScheme(id); + return false; + } + + + function findAllChangedmaterialsList(runningList){ +// _.each(getCompositeKeys(), function(key){ +// if (materialsList[key].compositeChildren.indexOf()) +// }); + } + + function getCompositeKeys(){ + return _.filter(_.keys(materialsList), function(key){ + return key.substr(0,5) == "super"; + }); + } + function deleteMaterial(id){ - if (materials[id].noDelete) { + if (materialsList[id].noDelete) { console.warn("no delete flag on this material type"); return false; } - delete materials[id];//todo check if being used first + delete materialsList[id];//todo check if being used first var deleted = true; if (deleted) loadMaterialClass();//set to defaults return deleted; @@ -46,11 +72,20 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu loadMaterialClass(); + + + + + + + + + function loadMaterialClass(){ var materialClass = appState.get("materialClass"); var newDefaultType = _.keys(plist.allMaterials[materialClass])[0]; - if (!materials[newDefaultType]) _.extend(materials, parseClassFromDefinitions(plist.allMaterials[materialClass])); - if (!materials[newDefaultType]) console.warn("material type " + newDefaultType + " not in definition for " + materialClass); + if (!materialsList[newDefaultType]) _.extend(materialsList, parseClassFromDefinitions(plist.allMaterials[materialClass])); + if (!materialsList[newDefaultType]) console.warn("material type " + newDefaultType + " not in definition for " + materialClass); appState.set("materialType", newDefaultType, {silent:true});//set to default silently } @@ -66,9 +101,17 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu return newMaterials; } + + + + + + + + function changeColorScheme(){ var state = appState.get("realisticColorScheme"); - _.each(_.keys(materials), function(name){ + _.each(_.keys(materialsList), function(name){ if (name == "setMaterial") return; changeSingleMaterialColorScheme(name, state); }); @@ -77,7 +120,7 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu function changeSingleMaterialColorScheme(name, state){ if (!state) state = appState.get("realisticColorScheme"); - var materialInfo = materials[name]; + var materialInfo = materialsList[name]; var color = getMaterialColorForState(state, materialInfo, name); if (materialInfo.threeMaterial) materialInfo.threeMaterial.color = new THREE.Color(color); else materialInfo.threeMaterial = makeMaterialObject(color); @@ -97,12 +140,15 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu function setMaterialDefaults(){ var materialType = appState.get("materialType"); appState.set("superCellIndex", new THREE.Vector3(0,0,0)); - if (materials[materialType].dimensions){ - appState.set("superCellRange", materials[materialType].dimensions.clone()); + if (materialsList[materialType].dimensions){ + appState.set("superCellRange", materialsList[materialType].dimensions.clone()); } else if (lattice.get("connectionType") == "gik"){ appState.set("superCellRange", new THREE.Vector3(appState.get("gikLength"), 1, 1)); } } - return materials; + return { + list:materialsList, + setMaterial: setMaterial + }; }); \ No newline at end of file diff --git a/js/menus/CompositeMenuView.js b/js/menus/CompositeMenuView.js index 03555636ad32c7d2d60adf3cea617d407e6d7d9b..afb1fbf7ee19f1783c6cded220e0b1490fdcf764 100644 --- a/js/menus/CompositeMenuView.js +++ b/js/menus/CompositeMenuView.js @@ -90,7 +90,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma return _.extend(this.model.toJSON(), plist, globals, lattice.compositeEditor.toJSON(), { dimensions: dimensions, - materials: materials + materials: materials.list }); }, diff --git a/js/menus/MaterialEditorMenuView.js b/js/menus/MaterialEditorMenuView.js index ce9bbd7912018d107e7a62efa1d0c1d012712f71..7c2c09d69259426903a41b45675a6a2c403c0a41 100644 --- a/js/menus/MaterialEditorMenuView.js +++ b/js/menus/MaterialEditorMenuView.js @@ -20,7 +20,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'materials', 'text!materi _initialize: function(){ //bind events - this.material = _.clone(materials[this.model.get("materialType")]); + this.material = _.clone(materials.list[this.model.get("materialType")]); }, getPropertyOwner: function($target){ diff --git a/js/menus/MaterialMenuView.js b/js/menus/MaterialMenuView.js index b7e6f3648123b5806d44b15d52e1a3ab053efb64..c3ab007a61fddc3130a9f6e831049c98418b926f 100644 --- a/js/menus/MaterialMenuView.js +++ b/js/menus/MaterialMenuView.js @@ -32,7 +32,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma e.stopPropagation(); e.preventDefault(); var id = $(e.target).data("id"); - lattice.setToCompositeMode(id, materials[id]); + lattice.setToCompositeMode(id,materials.list[id]); }, _editMaterial: function(e){ @@ -69,7 +69,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma }, _makeTemplateJSON: function(){ - return _.extend(lattice.toJSON(), this.model.toJSON(), plist, globals, {materials:materials}); + return _.extend(lattice.toJSON(), this.model.toJSON(), plist, globals, {materials:materials.list}); }, template: _.template(template) diff --git a/js/models/FileSaver.js b/js/models/FileSaver.js index a7f9f0872df256aceda0e01e4cf746d1c0cf4cba..e4cbaf9c149a2da4711762a5cee400ca064b5967 100644 --- a/js/models/FileSaver.js +++ b/js/models/FileSaver.js @@ -61,14 +61,14 @@ define(['underscore', 'fileSaverLib', 'lattice', 'materials', 'ribbon', 'menuWra function _getMaterialsDataToSave(){ var data = {}; - _.each(_.keys(materials), function(key){ + _.each(_.keys(materials.list), function(key){ data[key] = _getMaterialDataToSave(key); }); return data; } function _getMaterialDataToSave(id){ - return _.omit(materials[id], "threeMaterial"); + return _.omit(materials.list[id], "threeMaterial"); } function loadFile(data){//parsed json todo make this better - load composite