diff --git a/js/lattice/CompositeEditorLattice.js b/js/lattice/CompositeEditorLattice.js index bdc6d95771a867095c418813f8f0dae37561ab7f..9cba622da1aadff397d0f0aa2bdf04080687c57d 100644 --- a/js/lattice/CompositeEditorLattice.js +++ b/js/lattice/CompositeEditorLattice.js @@ -82,23 +82,14 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre sparseCells: JSON.parse(JSON.stringify(this.sparseCells)), cellsMin: cellsMin, cellsMax: cellsMax, - elementaryChildren: this._getChildCellTypes(true), - compositeChildren: this._getChildCellTypes(false), + elementaryChildren: materials.getChildCellTypes(this.sparseCells, true), + compositeChildren: materials.getChildCellTypes(this.sparseCells, false), dimensions: _dimensions }; return data; }, - _getChildCellTypes: function(elementaryTypes){ - var children = []; - this._loopCells(this.sparseCells, function(cell){ - if (!cell) return; - var isComposite = cell.materialName.substr(0,5) == "super"; - if ((elementaryTypes && !isComposite) || (!elementaryTypes && isComposite)) children.push(cell.materialName); - }); - if (children.length == 0) return null; - return _.uniq(children);//remove duplicates - }, + diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index a674844b0f2b8462d5b030d17f54db43ea03a9ea..f36df0bc58fc09878c402882b74f609130194b94 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 = JSON.parse(JSON.stringify(data.sparseCells)); + if (data) cells = data.sparseCells; _self._updateLatticeType(cells, self._getSubclassForLatticeType()); appState.set("currentNav", "navComposite"); }); @@ -199,6 +199,21 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre getUItarget: function(){ if (this.inCompositeMode()) return this.compositeEditor; return this; + }, + + reinitAllCellsOfTypes: function(types){ + this._loopCells(this.sparseCells, function(cell, x, y, z, self){ + if (cell && cell.materialName.substr(0,5) == "super" && types.indexOf(cell.materialName) > -1){ + //re-init cell; + var json = cell.toJSON(); + json.index = cell.getIndex(); + self.makeCellForLatticeType(json, function(newCell){ + console.log(newCell); + self.sparseCells[x][y][z] = newCell; + cell.destroy(); + }); + } + }); } }); diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js index f1b5019034c159605b28b6bee015ab187cd419ad..540b1b3e820eae07c155a730760063ce4d7f8006 100644 --- a/js/lattice/LatticeBase.js +++ b/js/lattice/LatticeBase.js @@ -503,8 +503,8 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre var cellsMin = this.get("cellsMin"); this._loopCells(sparseCells, function(cell, x, y, z, self){ if (cell) { - cell.index = (new THREE.Vector3(x, y, z)).add(cellsMin); - self.addCellWithJson(cell, new THREE.Vector3(x, y, z)); + var json = _.extend({index: (new THREE.Vector3(x, y, z)).add(cellsMin)}, cell); + self.addCellWithJson(json, new THREE.Vector3(x, y, z)); } }); }, diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js index 5f40bb49291a153c62573bfd1881cf7aa3ba493f..e8539bb56ca280d3259401df75cc3420e2e1f74a 100644 --- a/js/materials/DMAMaterials.js +++ b/js/materials/DMAMaterials.js @@ -32,19 +32,25 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu var oldColor = materialsList[id].color; var edited = false; - if (materialsList[id].sparseCells) edited = _.isEqual(data.sparseCells, materialsList[id].sparseCells); + 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 (materialsList[id].threeMaterial || oldColor != materialsList[id].color) changeSingleMaterialColorScheme(id); if (edited){ - //todo trigger change on all instances - var allChangedmaterialsList = findAllChangedmaterialsList([id]); - } + var allChangedMaterialsList = getAllParentComposites(id); + allChangedMaterialsList.push(id); - if (materialsList[id].threeMaterial || oldColor != materialsList[id].color) changeSingleMaterialColorScheme(id); + _.each(allChangedMaterialsList, function(key){ + materialsList[key].compositeChildren = getChildCellTypes(materialsList[key].sparseCells, false); + materialsList[key].elementaryChildren = getChildCellTypes(materialsList[key].sparseCells, true); + }); + + lattice.reinitAllCellsOfTypes(allChangedMaterialsList); + } return false; } @@ -68,19 +74,13 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu - 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 getVaildCompositeKeys(id){//for "available materials" list in composite editor + function getVaildAvailableCompositeKeys(id){//for "available materials" list in composite editor var compositeKeys = getCompositeKeys(); var invalidKeys = getAllParentComposites(id); invalidKeys.push(id); @@ -89,15 +89,44 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu function getAllParentComposites(id){ var parentComposites = []; - _.each(_.keys(materialsList), function(key){ - if (materialsList[key].compositeChildren && materialsList[key].compositeChildren.indexOf(id)>-1){ + _.each(materialsList, function(material, key){ + if (key == id) return; + if (material.compositeChildren && material.compositeChildren.indexOf(id)>-1){ parentComposites.push(key); } }); return parentComposites; } - + function getChildCellTypes(cells, elementaryTypes){//deep search to find all sub sub components + var children = []; + loopCells(cells, function(cell){ + if (!cell) return; + var isComposite = cell.materialName.substr(0,5) == "super"; + if ((elementaryTypes && !isComposite) || (!elementaryTypes && isComposite)) children.push(cell.materialName); + if (isComposite){ + if (elementaryTypes && materialsList[cell.materialName].elementaryChildren) { + Array.prototype.push.apply(children, materialsList[cell.materialName].elementaryChildren); + } + else if (!elementaryTypes && materialsList[cell.materialName].compositeChildren) { + Array.prototype.push.apply(children, materialsList[cell.materialName].compositeChildren); + } + } + }); + if (children.length == 0) return null; + return _.uniq(children);//remove duplicates + } + + function loopCells(cells, callback){ + for (var x=0;x<cells.length;x++){ + for (var y=0;y<cells[0].length;y++){ + for (var z=0;z<cells[0][0].length;z++){ + callback(cells[x][y][z], x, y, z); + } + } + } + } + @@ -180,6 +209,7 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu list:materialsList, setMaterial: setMaterial, getCompositeKeys: getCompositeKeys, - getVaildCompositeKeys: getVaildCompositeKeys + getVaildAvailableCompositeKeys: getVaildAvailableCompositeKeys, + getChildCellTypes:getChildCellTypes }; }); \ No newline at end of file diff --git a/js/menus/CompositeMenuView.js b/js/menus/CompositeMenuView.js index 9d30fedea3967be70de469a8c7e856cf905cfa13..32276511e592167fbbed81bf399d934228997f58 100644 --- a/js/menus/CompositeMenuView.js +++ b/js/menus/CompositeMenuView.js @@ -78,7 +78,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma this._exit(); return; } - var deleted = materials.setMaterial(this.get("id"), null); + var deleted = materials.setMaterial(lattice.compositeEditor.get("id"), null); if (deleted) this._exit(); }, @@ -91,7 +91,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'lattice', 'globals', 'ma { dimensions: dimensions, materials: materials.list, - validCompositeMaterials: materials.getVaildCompositeKeys(lattice.compositeEditor.get("id")) + validCompositeMaterials: materials.getVaildAvailableCompositeKeys(lattice.compositeEditor.get("id")) }); },