From 71c4f9c2fcddb7af835049cb953d7841d42f3528 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Thu, 25 Jun 2015 11:46:47 -0700
Subject: [PATCH] separate materials and methods in dmamaterials

---
 js/cells/DMACell.js                  | 12 ++--
 js/cells/supercells/CompositeCell.js |  2 +-
 js/lattice/CompositeEditorLattice.js |  7 ++-
 js/lattice/Lattice.js                |  2 +-
 js/materials/DMAMaterials.js         | 92 +++++++++++++++++++++-------
 js/menus/CompositeMenuView.js        |  2 +-
 js/menus/MaterialEditorMenuView.js   |  2 +-
 js/menus/MaterialMenuView.js         |  4 +-
 js/models/FileSaver.js               |  4 +-
 9 files changed, 88 insertions(+), 39 deletions(-)

diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 8a7f399b..a389c5e7 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 622282f8..f90d0a6b 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 9c4aaac5..bdc6d957 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 025370a2..a674844b 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 8c90f7fe..6b9ee78e 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 03555636..afb1fbf7 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 ce9bbd79..7c2c09d6 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 b7e6f364..c3ab007a 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 a7f9f087..e4cbaf9c 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
-- 
GitLab