diff --git a/js/lattice/CompositeEditorLattice.js b/js/lattice/CompositeEditorLattice.js index d4783e734f9ecd24e6cf6b5cb33b680d4a1b0bdd..0a9b17aa9258eac1a3efa86d2910240fc14ba68e 100644 --- a/js/lattice/CompositeEditorLattice.js +++ b/js/lattice/CompositeEditorLattice.js @@ -77,7 +77,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre } var id = this.get("id"); var data = this.toJSONForSave(bounds); - materials.setMaterial(id, data); + materials.newMaterial(data); }, toJSONForSave: function(bounds){ diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js index 037dd4f7c2661a3c10b8cb44df8b94be3195a1a2..bdda21dfefc6ccfa700a0605960a8dd46c336af4 100644 --- a/js/lattice/LatticeBase.js +++ b/js/lattice/LatticeBase.js @@ -96,7 +96,8 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre makeCellForLatticeType: function(json, callback){ var subclassFile = this.getCellSubclassFile(); require(['materials'], function(materials){ - if (json.materialID && materials.isComposite(json.materialID)) subclassFile = "compositeCell"; + var materialID = json.materialID || appState.get("materialType"); + if (materials.isComposite(materialID)) subclassFile = "compositeCell"; require([subclassFile], function(CellSubclass){ var cell = new CellSubclass(json); if (callback) callback(cell); diff --git a/js/main.js b/js/main.js index 6c1e304e796383885204e6f09849c5ca0be4c036..a4491884a7d7d487b7789860e0081f16f7f1fc79 100644 --- a/js/main.js +++ b/js/main.js @@ -120,6 +120,7 @@ require.config({ materialsPlist: 'plists/MaterialsPlist', materials: 'materials/DMAMaterials', material: 'materials/DMAMaterial', + compositeMaterial: 'materials/DMACompositeMaterial', //UI navbar: 'menus/Navbar', diff --git a/js/materials/DMACompositeMaterial.js b/js/materials/DMACompositeMaterial.js index 0bc338f9a6e24d8d1435582382268cfe819a0a64..695ba038cf985ec0e689dfef35f931c5852e43f8 100644 --- a/js/materials/DMACompositeMaterial.js +++ b/js/materials/DMACompositeMaterial.js @@ -24,14 +24,25 @@ define(['material'], function(DMAMaterial){ return true; }; + DMACompositeMaterial.prototype.isCompositeChild = function(id){ + return this.compositeChildren.indexOf(id)>-1; + }; + DMACompositeMaterial.prototype.toJSON = function(){ return { - name: this.name, - color: this.color, - altColor: this.altColor, - noDelete: this.noDelete, - properties: this.properties - } + name: this.name, + color: this.color, + altColor: this.altColor, + noDelete: this.noDelete, + properties: this.properties, + cellsMin: this.cellsMin, + cellsMax: this.cellsMax, + compositeChildren: this.compositeChildren, + elementaryChildren: this.elementaryChildren, + sparseCells: this.sparseCells + } }; + return DMACompositeMaterial; + }); \ No newline at end of file diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js index f5375bf7ad2132a7df1d1f07cc8539215ffacffb..fce8698ad2a3a32aebe86ba153abbaf4d7614d9e 100644 --- a/js/materials/DMAMaterials.js +++ b/js/materials/DMAMaterials.js @@ -3,8 +3,8 @@ */ //everything is a top level material with a threeMaterial object -define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeModel', 'material'], - function(_, THREE, appState, lattice, materialsPlist, three, DMAMaterial){ +define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeModel', 'material', 'compositeMaterial'], + function(_, THREE, appState, lattice, materialsPlist, three, DMAMaterial, DMACompositeMaterial){ var materialsList = { @@ -17,6 +17,16 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo noDelete: true }); + var compositeID = 0; + var materialID = 0; + + function getNextCompositeID(){ + return "super" + compositeID++; + } + + function getNextMaterialID(){ + return "material" + materialID++; + } var listener = {}; _.extend(listener, Backbone.Events); @@ -31,13 +41,15 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo function newMaterial(data, noAdd){ + var material, id; if (data.sparseCells) { - console.warn("you are trying to init a composite material as a regular material"); - return null; + id = data.id || getNextCompositeID(); + material = new DMACompositeMaterial(data, id); + } else { + id = data.id || getNextMaterialID(); + material = new DMAMaterial(data, id); } - var id = data.id || getNextMaterialID(); - var material = new DMAMaterial(data, id); if (noAdd) return material;//in the new material menu, you may init a material before saving changes materialsList[id] = material; @@ -118,8 +130,8 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo } function getCompositeKeys(){ - return _.filter(materialsList, function(material){ - return material.isComposite(); + return _.filter(_.keys(materialsList), function(key){ + return isComposite(key); }); } @@ -134,7 +146,7 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo var parentComposites = []; _.each(materialsList, function(material, key){ if (key == id) return; - if (material.compositeChildren && material.compositeChildren.indexOf(id)>-1){ + if (material.compositeChildren && material.isCompositeChild(id)){ parentComposites.push(key); } }); @@ -143,9 +155,10 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo function getChildCellTypes(cells, elementaryTypes){//deep search to find all sub sub components var children = []; + var _isComposite = isComposite; loopCells(cells, function(cell){ if (!cell) return; - var isComposite = isComposite(cell.materialID); + var isComposite = _isComposite(cell.getMaterialID()); if ((elementaryTypes && !isComposite) || (!elementaryTypes && isComposite)) children.push(cell.materialID); if (isComposite){ if (elementaryTypes && materialsList[cell.materialID].elementaryChildren) { @@ -189,7 +202,7 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo - + //events function setToDefaultMaterial(triggerEvent){ var materialClass = appState.get("materialClass"); @@ -209,14 +222,6 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo return newMaterials; } - - - - - - - - function changeColorScheme(){ var state = appState.get("realisticColorScheme"); _.each(materialsList, function(material, name){ @@ -236,20 +241,6 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo } - - var compositeID = 0; - var materialID = 0; - - function getNextCompositeID(){ - return "super" + compositeID++; - } - - function getNextMaterialID(){ - return "material" + materialID++; - } - - - return { list: materialsList, newMaterial: newMaterial, diff --git a/js/menus/templates/MaterialMenuView.html b/js/menus/templates/MaterialMenuView.html index db8ff1850f1dffb1b2f1cb7b5ed9061ea4defb7e..2306310cd1093ca496704878b7aa4e4bef6348fa 100644 --- a/js/menus/templates/MaterialMenuView.html +++ b/js/menus/templates/MaterialMenuView.html @@ -33,7 +33,6 @@ Materials:<br/> <input id="realisticColorScheme" data-property="realisticColorScheme" type="checkbox" <% if (realisticColorScheme){ %> checked="checked"<% } %> value="" data-toggle="checkbox" class="appState custom-checkbox"> <span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span> Use realistic color scheme</label><br/> -<br/> <% if (!inSimMode){ %> Composite Materials:<br/> <% if ((cellType == "octa" && connectionType != "vertex") || cellType == "tetra"){ %> diff --git a/js/models/AppState.js b/js/models/AppState.js index cb15349cc78a8c30d1ab57aafd0b12ed9bedf37d..28bfb7085f3fbb085c8eb8cc2aa8fc27cd3e788f 100644 --- a/js/models/AppState.js +++ b/js/models/AppState.js @@ -134,15 +134,14 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'], require(['materials'], function(materials){ var materialType = self.get("materialType"); //verify that correct class is in sync - if (materials.isComposite(materialType)) { - if (materials.isComposite(self.previous("materialType"))) return; + if (!materials.isComposite(materialType)) { + if (!materials.isComposite(self.previous("materialType"))) return; //re init highlighter require([self.lattice.getHighlighterFile()], function(HighlighterClass){ globals.highlighter = new HighlighterClass(); }); return; } - //composite material require(['superCellHighlighter'], function(SuperCellHighlighter){ globals.highlighter = new SuperCellHighlighter();