diff --git a/js/cam/assemblers/AssemblerPost.js b/js/cam/assemblers/AssemblerPost.js index 909853d11289f6da50ea7dfc6134e16184839049..6ebcef602240091aa1ee1a8e8da96f74465542f8 100644 --- a/js/cam/assemblers/AssemblerPost.js +++ b/js/cam/assemblers/AssemblerPost.js @@ -221,7 +221,7 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti var cellIndex = cell.getAbsoluteIndex(); if (!self.shouldPickUpStock){ - data += self._postGetStock(cellIndex, lastIndex, cellPosition, cell.materialName, settings, exporter, context); + data += self._postGetStock(cellIndex, lastIndex, cellPosition, cell.getMaterialID(), settings, exporter, context); } else { // var thisStockPosition = _.clone(stockPosition); // if (multStockPositions) { @@ -233,7 +233,7 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti // data += self._postMoveXY(exporter, stockPosition.x-wcs.x, stockPosition.y-wcs.y); // data += self._postMoveToStock(exporter, thisStockPosition, rapidHeight, wcs, safeHeight); } - data += self._postReleaseStock(cellIndex, cellPosition, cell.materialName, settings, exporter, context); + data += self._postReleaseStock(cellIndex, cellPosition, cell.getMaterialID(), settings, exporter, context); data += "\n"; lastIndex = cellIndex.clone(); }); diff --git a/js/cam/assemblers/StockComponent.js b/js/cam/assemblers/StockComponent.js index 120a2723dd592b84afaae5d91be0d865e083418f..208f37a54e6686f9e14005a8575e4efae4485b30 100644 --- a/js/cam/assemblers/StockComponent.js +++ b/js/cam/assemblers/StockComponent.js @@ -34,13 +34,13 @@ define(['underscore', 'cam', 'three', 'component', 'lattice', 'threeModel'], return this.object3D.position.clone(); }; - StockComponent.prototype.setMaterial = function(materialName){ - this.cell.changeMaterial(materialName); + StockComponent.prototype.setMaterial = function(materialID){ + this.cell.changeMaterial(materialID); three.render(); }; StockComponent.prototype.getMaterial = function(){ - return this.cell.getMaterialName(); + return this.cell.getMaterialID(); }; @@ -79,7 +79,7 @@ define(['underscore', 'cam', 'three', 'component', 'lattice', 'threeModel'], StockComponent.prototype.toJSON = function(){ var json = Component.prototype.toJSON.call(this); json.description = { - materialName: this.cell.materialName, + materialID: this.getMaterial(), length: this.cell.getLength() }; return json; diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js index ae633d42d1b7caef1c0ffbd20a3b79dcd78dca58..ffbc741705d0b023d1ed506bb4ff71d05f7aebc7 100644 --- a/js/cells/DMACell.js +++ b/js/cells/DMACell.js @@ -12,7 +12,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' if (json.index) this.index = new THREE.Vector3(json.index.x, json.index.y, json.index.z); if (superCell) this.superCell = superCell; - this.materialName = json.materialName || appState.get("materialType"); + this.material = materials.getMaterialForId(json.materialID || appState.get("materialType")); this.isTransparent = false; //object 3d is parent to all 3d elements owned by cell: cell mesh and wireframe, parts, beams, nodes, etc @@ -157,7 +157,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 && !this.material) return;//cell may be deleted by now if (state) material = materials.getDeleteMaterial(); else material = this.getMaterial(true); if (!material) return;//no material object found @@ -223,31 +223,28 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' this.setMode(mode); }; - DMACell.prototype.getMaterialName = function(){ - return this.materialName; + DMACell.prototype.getMaterialID = function(){ + return this.material.id; + }; + + DMACell.prototype.setMaterial = function(material){ + this.material = material; + this.object3D.children[0].material = this.getMaterial(true); }; DMACell.prototype.getMaterial = function(returnTHREEObject){ - if (!this.materialName) { + if (!this.material) { console.warn("no material type set for cell"); return null; } - var material = materials.getMaterialForId(this.materialName, returnTHREEObject, this.isTransparent); - if (!material) { - console.warn("no material object found of type " + this.materialName); - return null; - } - return material; + if (!returnTHREEObject) return this.material; + if (this.isTransparent) return this.material.getTransparentMaterial(); + return this.material.getThreeMaterial(); }; - DMACell.prototype.setMaterial = function(material){ - this.object3D.children[0].material = material; - }; - - DMACell.prototype.changeMaterial = function(materialName, materialObject){ - this.materialName = materialName; - if (materialObject === undefined) materialObject = materials.getMaterialForId(materialName).threeMaterial; - this.object3D.children[0].material = materialObject; + DMACell.prototype.changeMaterial = function(materialID, material){ + if (material === undefined) material = materials.getMaterialForId(materialID); + this.setMaterial(material); }; DMACell.prototype.setWireframeVisibility = function(visible, mode){ @@ -466,7 +463,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' this.nodes = null; this.beams = null; this.superCell = null; - this.materialName = null; + this.material = null; this.index = null; this.length = null; }; @@ -481,9 +478,9 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', ' DMACell.prototype.toJSON = function(){ var data = { - materialName: this.materialName + materialID: this.getMaterialID() }; -// if (materials.getMaterialForID(this.materialName).sparseCells) return data;//material definition in material composites +// if (this.material.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 9720a4d8c3472606db5c045fbee35664ce713f68..408b55a0ac46a6c825e98e5be85275e9c861e7b7 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.getMaterialForId(this.materialName).dimensions; + var dimensions = this.material.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/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js index 925316bcd81da1413e5b3eb706b6de71e16b8966..2baea8101c173a1a427f4e4777f0c1a48d8aa649 100644 --- a/js/cells/supercells/DMASuperCell.js +++ b/js/cells/supercells/DMASuperCell.js @@ -64,7 +64,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'], DMASuperCell.prototype._makeSubCellForIndex = function(json, callback){ var subclassFile = lattice.getCellSubclassFile(); - if (json.materialName && json.materialName.substr(0,5) == "super") subclassFile = "compositeCell"; + if (json.materialID && json.materialID.substr(0,5) == "super") subclassFile = "compositeCell"; var self = this; require([subclassFile], function(CellSubclass){ var cell = new CellSubclass(json, self); diff --git a/js/cells/supercells/GIKSuperCell.js b/js/cells/supercells/GIKSuperCell.js index 68e35c3a36172248b5039f4c4bf2ec6ed8193584..7bb1b16003dc209f8557daae72da7f1c122fc8b1 100644 --- a/js/cells/supercells/GIKSuperCell.js +++ b/js/cells/supercells/GIKSuperCell.js @@ -29,7 +29,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell', }; GIKSuperCell.prototype._makeSubCellForIndex = function(json, callback){ - json.materialName = this.materialName; + json.materialID = this.material.id; if (lattice.get("latticeType") == "dnaBricks"){ callback(new DNABrickCell(json, this)); return; diff --git a/js/lattice/Lattice.js b/js/lattice/Lattice.js index 2a174d16f39beeb12b2faba0bef7b457707eb0cd..d7b3ee4490e6d56b1cdfc2548c9909993d61f8df 100644 --- a/js/lattice/Lattice.js +++ b/js/lattice/Lattice.js @@ -235,7 +235,9 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre reinitAllCellsOfTypes: function(types){//when material definition is changed this._loopCells(this.sparseCells, function(cell, x, y, z, self){ - if (cell && cell.materialName.substr(0,5) == "super" && types.indexOf(cell.materialName) > -1){ + if (!cell) return; + var material = cell.getMaterial(); + if (material && material.isComposite() && types.indexOf(material.getID()) > -1){ //re-init cell; var json = cell.toJSON(); json.index = cell.getIndex(); diff --git a/js/lattice/LatticeBase.js b/js/lattice/LatticeBase.js index 11f27ccd123eb72a377f6017f8e248f1bf8a4321..a48fefcbdbc12cd7c3cf64a84f74d7eff8d89426 100644 --- a/js/lattice/LatticeBase.js +++ b/js/lattice/LatticeBase.js @@ -95,7 +95,7 @@ define(['underscore', 'backbone', 'appState', 'globals', 'plist', 'three', 'thre makeCellForLatticeType: function(json, callback){ var subclassFile = this.getCellSubclassFile(); - if (json.materialName && json.materialName.substr(0,5) == "super") subclassFile = "compositeCell"; + if (json.materialID && json.materialID.substr(0,5) == "super") subclassFile = "compositeCell"; require([subclassFile], function(CellSubclass){ var cell = new CellSubclass(json); if (callback) callback(cell); diff --git a/js/materials/DMAMaterial.js b/js/materials/DMAMaterial.js index c695d4bba1cbcb491c13b98c1d2c033917c1ab21..a2b90abe5829b91e987f85905b0c2e87261415c2 100644 --- a/js/materials/DMAMaterial.js +++ b/js/materials/DMAMaterial.js @@ -5,17 +5,16 @@ define(['underscore', 'appState'], function(_, appState){ - function DMAMaterial(json){ + function DMAMaterial(json, id){ + this.id = id; this.set(json); } - DMAMaterial.prototype.set = function(data){ - - //check if colors have changed - var oldColor = this.color; - var oldAltColor = this.altColor; + DMAMaterial.prototype.getID = function(){ + return this.id; + }; - var edited = false; + DMAMaterial.prototype.set = function(data){ var self = this; _.each(_.keys(data), function(key){ @@ -23,11 +22,16 @@ define(['underscore', 'appState'], function(_, appState){ else self[key] = data[key]; }); - if (!this.threeMaterial || oldColor != this.color || oldAltColor != this.altColor) this.changeColorScheme(); + //check if colors have changed + var oldColor = this.color; + var oldAltColor = this.altColor; + if (!this.threeMaterial || + (data.color && oldColor != data.color) || + (data.altColor && oldAltColor != data.altColor)) this.changeColorScheme();//don't need to set edited flag for this, render will handle it - if (!data.noDelete) this.noDelete = false; + if (data.noDelete === undefined) this.noDelete = false; - return edited; + return false;//composite materials have edited flag to trigger upstream changes }; DMAMaterial.prototype.changeColorScheme = function(state){ @@ -51,17 +55,17 @@ define(['underscore', 'appState'], function(_, appState){ return new THREE.MeshLambertMaterial({color:color}); }; - DMAMaterial.prototype.getThreeMaterial = function(id){ + DMAMaterial.prototype.getThreeMaterial = function(){ if (!this.threeMaterial) { - console.warn("no transparentMaterial found for material " + id); + console.warn("no transparentMaterial found for material " + this.id); return null; } return this.threeMaterial; }; - DMAMaterial.prototype.getTransparentMaterial = function(id){ + DMAMaterial.prototype.getTransparentMaterial = function(){ if (!this.transparentMaterial) { - console.warn("no transparentMaterial found for material " + id); + console.warn("no transparentMaterial found for material " + this.id); return null; } return this.transparentMaterial; diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js index 8e6c888353231f34c1ad9927ea3fb24f7bfc73a8..50e86911365f9f09315ca68e1412db091180510f 100644 --- a/js/materials/DMAMaterials.js +++ b/js/materials/DMAMaterials.js @@ -28,31 +28,13 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel', 'ma + function newMaterial(data){ - function setMaterial(id, data){ - var material = getMaterialForId(id); + } - var edited = false; - if (!material) { - materialsList[id] = new DMAMaterial(data); - return; - } else { - if (data.elementaryChildren) data.properties = getPropertiesFromChildren(data.elementaryChildren); - edited = material.set(data); - } + //material objects edited through set() - if (edited){ - var allChangedMaterialsList = getAllParentComposites(id); - allChangedMaterialsList.push(id); - _.each(allChangedMaterialsList, function(key){ - materialsList[key].compositeChildren = getChildCellTypes(materialsList[key].sparseCells, false); - materialsList[key].elementaryChildren = getChildCellTypes(materialsList[key].sparseCells, true); - }); - - lattice.reinitAllCellsOfTypes(allChangedMaterialsList); - } - } function deleteMaterial(id){ if (materialsList[id] === undefined){ @@ -82,13 +64,41 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel', 'ma return material.getThreeMaterial(id); } + + + + function setMaterial(id, data){ + var material = getMaterialForId(id); + + var edited = false; + if (!material) { + materialsList[id] = new DMAMaterial(data); + return; + } else { + if (data.elementaryChildren) data.properties = getPropertiesFromChildren(data.elementaryChildren); + edited = material.set(data); + } + + if (edited){ + var allChangedMaterialsList = getAllParentComposites(id); + allChangedMaterialsList.push(id); + + _.each(allChangedMaterialsList, function(key){ + materialsList[key].compositeChildren = getChildCellTypes(materialsList[key].sparseCells, false); + materialsList[key].elementaryChildren = getChildCellTypes(materialsList[key].sparseCells, true); + }); + + lattice.reinitAllCellsOfTypes(allChangedMaterialsList); + } + } + + + var materialNameIndex = 1; function getMaterialCopy(id){ var material = getMaterialForId(id); - if (material){ - return JSON.parse(JSON.stringify(material.toJSON())); - } + if (material) return material.clone(); return { name: "Material " + materialNameIndex++, color: '#000000', @@ -138,14 +148,14 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel', 'ma 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); + var isComposite = cell.materialID.substr(0,5) == "super"; + if ((elementaryTypes && !isComposite) || (!elementaryTypes && isComposite)) children.push(cell.materialID); if (isComposite){ - if (elementaryTypes && materialsList[cell.materialName].elementaryChildren) { - Array.prototype.push.apply(children, materialsList[cell.materialName].elementaryChildren); + if (elementaryTypes && materialsList[cell.materialID].elementaryChildren) { + Array.prototype.push.apply(children, materialsList[cell.materialID].elementaryChildren); } - else if (!elementaryTypes && materialsList[cell.materialName].compositeChildren) { - Array.prototype.push.apply(children, materialsList[cell.materialName].compositeChildren); + else if (!elementaryTypes && materialsList[cell.materialID].compositeChildren) { + Array.prototype.push.apply(children, materialsList[cell.materialID].compositeChildren); } } }); diff --git a/js/menus/MaterialEditorMenuView.js b/js/menus/MaterialEditorMenuView.js index 3cdaf15ac53ce73757f29b993543b2854130151e..2b0608944a1c20e89220686655d27105597e43ac 100644 --- a/js/menus/MaterialEditorMenuView.js +++ b/js/menus/MaterialEditorMenuView.js @@ -50,7 +50,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'materials', 'text!materi }, saveExitMenu: function(){ - if (this.material.name == "") this.material.name = "Material " + materialNameIndex++; + if (this.material.name == "") this.material.name = "Material " + materials.getNextMaterialID(); materials.setMaterial(this.materialID, _.clone(this.material)); return true; }, diff --git a/js/menus/templates/EditComponentMenuView.html b/js/menus/templates/EditComponentMenuView.html index 6a430099fc20714c4b9c8f26b5794291a16cd0ce..54669e7ec8930b4f5ce3e503f22064d01a677f7e 100644 --- a/js/menus/templates/EditComponentMenuView.html +++ b/js/menus/templates/EditComponentMenuView.html @@ -62,7 +62,7 @@ Parent: <% } else { %> Material: <div class="btn-group"> - <button data-toggle="dropdown" class="btn dropdown-toggle" type="button"><%= materials[thisComponent.description.materialName].name %><span class="caret"></span></button> + <button data-toggle="dropdown" class="btn dropdown-toggle" type="button"><%= materials[thisComponent.description.materialID].name %><span class="caret"></span></button> <ul role="menu" class="dropdown-menu"> <% _.each(allMaterials[materialClass], function(material, key){ %> <li><a class="stockMaterial dropdownSelector" data-id="<%= key %>" href="#"> diff --git a/js/plists/CamPList.js b/js/plists/CamPList.js index 1dc4550ea21c27e8b85855d0853220e667566ea6..f90ea3bd57c1b0b6b1feecd76bb8b6bf581b6d31 100644 --- a/js/plists/CamPList.js +++ b/js/plists/CamPList.js @@ -218,7 +218,7 @@ define(['three'], function(THREE){ z: 0 }, description: { - materialName: "fiberGlass", + materialID: "fiberGlass", length: 4 } }, @@ -244,7 +244,7 @@ define(['three'], function(THREE){ z: 0 }, description: { - materialName: "brass", + materialID: "brass", length: 4 } }