diff --git a/js/materials/DMACompositeMaterial.js b/js/materials/DMACompositeMaterial.js index d2b7657e4d50aaecd6716ebfc982686fca4199d9..706287ff9a1f88ce31378eebe155df055b42ab0e 100644 --- a/js/materials/DMACompositeMaterial.js +++ b/js/materials/DMACompositeMaterial.js @@ -43,36 +43,34 @@ define(['material'], function(DMAMaterial){ console.log("composite material edited"); var self = this; require(['materials'], function(materials){ - var compositeChildren = []; - var elementaryChildren = []; - var firstGen = self.getFirstGenChildMaterials(materials); - elementaryChildren = _.clone(firstGen.elementaryCells); - compositeChildren = _.clone(firstGen.compositeCells); - - _.each(firstGen.compositeCells, function(compositeID){ - var material = materials.getMaterialForId(compositeID); - compositeChildren.concat(material.getCompositeChildren()); - elementaryChildren.concat(material.getElementaryChildren()); - }); - compositeChildren = _.uniq(compositeChildren);//remove duplicates - elementaryChildren = _.uniq(elementaryChildren); - - self.compositeChildren = compositeChildren; - self.elementaryChildren = elementaryChildren; - - //getParentComposites - if (!_.isEqual(self.getCompositeChildren(), compositeChildren)){ + var changed = self.recalcChildren(materials); - } - if (!_.isEqual(self.getElementaryChildren(), elementaryChildren)){ - self.properties = self.getPropertiesFromElements(elementaryChildren) + var parentComposites = self.getParentComposites(materials); + if (changed){ + _.each(parentComposites, function(parentID){ + var parent = materials.getMaterialForId(parentID); + parent.recalcChildren(materials); + }); } }); } return edited; }; + DMACompositeMaterial.prototype.getParentComposites = function(materials){ + var parentComposites = []; + var id = this.getID(); + _.each(materials.compositeMaterialsList, function(material, key){ + if (key == id) return; + var compositeChildren = material.getCompositeChildren(); + if (compositeChildren.indexOf(id) >= 0){ + parentComposites.push(key); + } + }); + return parentComposites; + }; + DMACompositeMaterial.prototype.getFirstGenChildMaterials = function(materials){ var compositeChildrenFirstGen = []; var elementaryChildrenFirstGen = []; @@ -88,25 +86,46 @@ define(['material'], function(DMAMaterial){ return {compositeCells:compositeChildrenFirstGen, elementaryCells:elementaryChildrenFirstGen}; }; - DMACompositeMaterial.prototype.recalcChildren = function(){ + DMACompositeMaterial.prototype.recalcChildren = function(materials){ + var compositeChildren = []; + var elementaryChildren = []; - }; + var firstGen = this.getFirstGenChildMaterials(materials); + elementaryChildren = _.clone(firstGen.elementaryCells); + compositeChildren = _.clone(firstGen.compositeCells); - DMACompositeMaterial.prototype.getPropertiesFromElements = function(elementaryChildren){ - var properties = []; - return properties; - }; + _.each(firstGen.compositeCells, function(compositeID){ + var material = materials.getMaterialForId(compositeID); + compositeChildren.concat(material.getCompositeChildren()); + elementaryChildren.concat(material.getElementaryChildren()); + }); + + compositeChildren = _.uniq(compositeChildren); + elementaryChildren = _.uniq(elementaryChildren); + + var changed = !_.isEqual(this.getElementaryChildren(), elementaryChildren); + if (changed){ + this.properties = this.getPropertiesFromElements(elementaryChildren, materials); + } + + changed |= !_.isEqual(this.getCompositeChildren(), compositeChildren); - DMACompositeMaterial.prototype._setCompositeChildren = function(compositeChildren){ this.compositeChildren = compositeChildren; - }; + this.elementaryChildren = elementaryChildren; - DMACompositeMaterial.prototype.getCompositeChildren = function(){ - return this.compositeChildren; + return changed; }; - DMACompositeMaterial.prototype._setElementaryChildren = function(elementaryChildren){ + DMACompositeMaterial.prototype.getPropertiesFromElements = function(elementaryChildren, materials){ + var properties = {}; + _.each(elementaryChildren, function(childID){ + if (materials.getMaterialForId(childID).getProperties().conductive) properties.conductive = true; + }); + return properties; + }; + DMACompositeMaterial.prototype.getCompositeChildren = function(){ + return this.compositeChildren; }; DMACompositeMaterial.prototype.getElementaryChildren = function(){ @@ -147,7 +166,8 @@ define(['material'], function(DMAMaterial){ cellsMax: this.cellsMax, compositeChildren: this.compositeChildren, elementaryChildren: this.elementaryChildren, - sparseCells: this.sparseCells + sparseCells: this.sparseCells, + isComposite: true }); }; diff --git a/js/materials/DMAMaterial.js b/js/materials/DMAMaterial.js index e98e20dfb39f6d52250b84cc0240ebface77d1fd..cb69b991743277814ee8713699af576841f495a7 100644 --- a/js/materials/DMAMaterial.js +++ b/js/materials/DMAMaterial.js @@ -39,13 +39,13 @@ define(['underscore', 'appState'], function(_, appState){ var changed = false; var self = this; _.each(["name", "color", "altColor"], function(key){ - if (data[key] && data[key] != self[key]) { + if (data[key] !== undefined && data[key] != self[key]) { self[key] = data[key]; changed = true; } }); - if (this.name = "" && data.name === undefined){ + if (this.name == "" && data.name == ""){ this.name = this.generateMaterialName(); changed = true; } @@ -142,10 +142,6 @@ define(['underscore', 'appState'], function(_, appState){ return !this.noDelete; }; -// DMAMaterial.prototype.clone = function(){ -// return JSON.parse(JSON.stringify(this.toJSON())); -// }; - DMAMaterial.prototype.toJSON = function(){ return { name: this.name, diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js index 4e6b7148ba6a93372abc192f6520d60aad810cc7..6f8c4a5cba8c39a5903057119590fe52f678dc72 100644 --- a/js/materials/DMAMaterials.js +++ b/js/materials/DMAMaterials.js @@ -17,7 +17,7 @@ define(['underscore', 'backbone', 'three', 'appState', 'lattice', 'materialsPlis return "material" + materialID++; } - return new Backbone.Model.extend({ + var Materials = Backbone.Model.extend({ initialize: function(){ this.materialsList = {}; @@ -32,7 +32,7 @@ define(['underscore', 'backbone', 'three', 'appState', 'lattice', 'materialsPlis this.listenTo(appState, "change:realisticColorScheme", this.changeColorScheme); this.listenTo(appState, "change:materialClass", this.loadMaterialClass); - this.listenTo(appState, "change:materialType", this.setMaterialDefaults); + this.listenTo(appState, "change:materialType", this.setMaterialsDefaults); this.loadMaterialClass(); }, @@ -59,6 +59,16 @@ define(['underscore', 'backbone', 'three', 'appState', 'lattice', 'materialsPlis appState.set("materialType", id); }, + setMaterialsDefaults: function(){ + var materialID = appState.get("materialType"); + appState.set("superCellIndex", new THREE.Vector3(0,0,0)); + if (this.getMaterialForId(materialID).getDimensions){ + appState.set("superCellRange", this.getMaterialForId(materialID).getDimensions()); + } else if (lattice.get("connectionType") == "gik"){ + appState.set("superCellRange", new THREE.Vector3(appState.get("gikLength"), 1, 1)); + } + }, + @@ -143,11 +153,11 @@ define(['underscore', 'backbone', 'three', 'appState', 'lattice', 'materialsPlis setMaterial: function(id, data){ var material = this.getMaterialForId(id); + if (!material) return this.newMaterial(data); if (material.isComposite()){ console.warn("use setCompositeMaterial()"); return this.setCompositeMaterial(id, data); } - if (!material) return this.newMaterial(data); var edited = material.set(data); if (edited) myConsole.write("materials.setMaterial(" + id + ", " + JSON.stringify(material.toJSON()) + "}"); @@ -163,23 +173,6 @@ define(['underscore', 'backbone', 'three', 'appState', 'lattice', 'materialsPlis return material; }, - updateParentProperties: function(parents){ - //update properties of all composites containing this element - var allChangedMaterialsList = getAllParentComposites(id); - }, - - upDataParentComposition: function(parents){ - 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); - }, - getDeleteMaterial: function(){ return this.getMaterialForId("deleteMaterial").getThreeMaterial(); }, @@ -191,90 +184,23 @@ define(['underscore', 'backbone', 'three', 'appState', 'lattice', 'materialsPlis return false; } return material.isComposite(); - } - - - }); - - - - - function getCompositeKeys(){ - return _.keys(compositeMaterialsList); - } - - function getVaildAvailableCompositeKeys(id){//for "available materials" list in composite editor - var compositeKeys = getCompositeKeys(); - var invalidKeys = getAllParentComposites(id); - invalidKeys.push(id); - return _.difference(compositeKeys, invalidKeys); - } + }, - function getAllParentComposites(id){ - var parentComposites = []; - _.each(materialsList, function(material, key){ - if (key == id) return; - if (material.compositeChildren && material.isCompositeChild(id)){ - parentComposites.push(key); - } - }); - return parentComposites; - } + changeColorScheme: function(){ + var state = appState.get("realisticColorScheme"); + _.each(this.materialsList, function(material){ + material.changeColorScheme(state); + }); + three.render(); + }, - 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.getMaterialID()); - if ((elementaryTypes && !isComposite) || (!elementaryTypes && isComposite)) children.push(cell.getMaterialID()); - if (isComposite){ - if (elementaryTypes && materialsList[cell.materialID].elementaryChildren) { - Array.prototype.push.apply(children, materialsList[cell.materialID].elementaryChildren); - } - else if (!elementaryTypes && materialsList[cell.materialID].compositeChildren) { - Array.prototype.push.apply(children, materialsList[cell.materialID].compositeChildren); - } + toJSON: function(){ + return { + materials: this.materialsList, + compositeMaterials: this.compositeMaterialsList } - }); - if (children.length == 0) return null; - return _.uniq(children);//remove duplicates - } - - function getPropertiesFromChildren(children){ - var properties = {}; - var self = this; - _.each(children, function(childID){ - if (self.getMaterialForId(childID).getProperties().conductive) properties.conductive = true; - }); - return properties; - } - - - - - - //events - - - - - - function changeColorScheme(){ - var state = appState.get("realisticColorScheme"); - _.each(materialsList, function(material, name){ - material.changeColorScheme(state); - }); - three.render(); - } - - function setMaterialDefaults(){ - var materialType = appState.get("materialType"); - appState.set("superCellIndex", new THREE.Vector3(0,0,0)); - if (materialsList[materialType].getDimensions){ - appState.set("superCellRange", materialsList[materialType].getDimensions()); - } else if (lattice.get("connectionType") == "gik"){ - appState.set("superCellRange", new THREE.Vector3(appState.get("gikLength"), 1, 1)); } - } + }); + + return new Materials(); }); \ No newline at end of file diff --git a/js/menus/MaterialMenuView.js b/js/menus/MaterialMenuView.js index 16cb6afb6f5003ac2ed73ef29b51ec8ad0293a43..0527108dc8d96c88fce4be472e6210eefe540cb3 100644 --- a/js/menus/MaterialMenuView.js +++ b/js/menus/MaterialMenuView.js @@ -61,7 +61,7 @@ define(['jquery', 'underscore', 'menuParent', 'materialsPlist', 'plist', 'lattic }, _makeTemplateJSON: function(){ - return _.extend(lattice.toJSON(), this.model.toJSON(), materialsPlist, plist, globals, {inSimMode:false, materials:materials.list, compositeMaterialsKeys:materials.getCompositeKeys()}); + return _.extend(lattice.toJSON(), this.model.toJSON(), materialsPlist, plist, globals, materials.toJSON(), {inSimMode:false}); }, template: _.template(template) diff --git a/js/menus/templates/MaterialMenuView.html b/js/menus/templates/MaterialMenuView.html index 04565702c5533dbbe0317a77e49eac04acc4bcbd..c27a56308aeb9b933b0233b5cc517dfc9708c802 100644 --- a/js/menus/templates/MaterialMenuView.html +++ b/js/menus/templates/MaterialMenuView.html @@ -10,17 +10,8 @@ </div><br/><br/> <% } %> Materials:<br/> -<% _.each(_.keys(allMaterials[materialClass]), function(key){ %> - <label class="radio colorSwatches"> - <input type="radio" <%if (key == materialType){ %>checked<%}%> name="materialType" value="<%= key %>" data-toggle="radio" class="custom-radio appState"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span> - <div class="materialColorSwatch"> - <div style="background-color:<% if(realisticColorScheme){ %><%= materials[key].color %><% }else{ %><%= materials[key].altColor %><% } %>"></div> - <span><span class="materialListTitle"><%= materials[key].name %></span><a data-id="<%= key %>" class="editMaterial">Edit...</a></span></div> - </label> -<% }); %> -<% _.each(materials, function(material, key){ - if (key.substr(0,5) == "super") return; - if (material.noDelete) return; %> +<% _.each(materials, function(material, key){ %> + <% if (key == "deleteMaterial") return; %> <label class="radio colorSwatches"> <input type="radio" <%if (key == materialType){ %>checked<%}%> name="materialType" value="<%= key %>" data-toggle="radio" class="custom-radio appState"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span> <div class="materialColorSwatch"> @@ -38,15 +29,15 @@ Use realistic color scheme</label><br/> <% if ((cellType == "octa" && connectionType != "vertex") || cellType == "tetra"){ %> not available for this lattice type <% } else { %> - <% _.each(compositeMaterialsKeys, function(key){ %> + <% _.each(compositeMaterials, function(material, key){ %> <label class="radio colorSwatches"> <input type="radio" <%if (key == materialType){ %>checked<%}%> name="materialType" value="<%= key %>" data-toggle="radio" class="custom-radio appState"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span> <div class="materialColorSwatch"> - <div style="background-color:<% if(realisticColorScheme){ %><%= materials[key].color %><% }else{ %><%= materials[key].altColor %><% } %>"></div> - <span><span class="materialListTitle"><%= materials[key].name %></span><a data-id="<%= key %>" class="editComposite">Edit...</a></span></div> + <div style="background-color:<% if(realisticColorScheme){ %><%= material.color %><% }else{ %><%= material.altColor %><% } %>"></div> + <span><span class="materialListTitle"><%= material.name %></span><a data-id="<%= key %>" class="editComposite">Edit...</a></span></div> </label> <% }); %><br/> - <% if (materialType.substr(0,5) == "super") { %> + <% if (materialType.isComposite) { %> Offset (xyz): <input data-property="superCellIndex" data-key="x" value="<%= superCellIndex.x %>" placeholder="X" class="form-control intInput appState" type="text"> <input data-property="superCellIndex" data-key="y" value="<%= superCellIndex.y %>" placeholder="Y" class="form-control intInput appState" type="text"> <input data-property="superCellIndex" data-key="z" value="<%= superCellIndex.z %>" placeholder="Z" class="form-control intInput appState" type="text"><br/><br/> diff --git a/js/models/AppState.js b/js/models/AppState.js index c9b3b9bb5729397f7f8e55ea55767ad4d0d8b57d..ebba2c729c2d8781383d8c9d8e450afbeacb88a4 100644 --- a/js/models/AppState.js +++ b/js/models/AppState.js @@ -129,14 +129,14 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist', 'globals'], }, _materialTypeChanged: function(){ - var self = this; 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 (self.previous("materialType") && !materials.isComposite(self.previous("materialType"))) return; //re init highlighter + if (!self.lattice.getHighlighterFile) return; require([self.lattice.getHighlighterFile()], function(HighlighterClass){ globals.highlighter = new HighlighterClass(); });