From 4b28d120f8bcff6c1b1ba77312cf9c315edcbd62 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 22 Sep 2015 01:33:10 -0400
Subject: [PATCH] DMAMAterial class

---
 js/main.js                   |   1 +
 js/materials/DMAMaterial.js  | 100 ++++++++++++++++++++++++++++++
 js/materials/DMAMaterials.js | 114 +++++++++++------------------------
 js/models/FileSaver.js       |  10 +--
 4 files changed, 140 insertions(+), 85 deletions(-)
 create mode 100644 js/materials/DMAMaterial.js

diff --git a/js/main.js b/js/main.js
index 313871f1..5fc9b106 100644
--- a/js/main.js
+++ b/js/main.js
@@ -118,6 +118,7 @@ require.config({
 
         //materials
         materials: 'materials/DMAMaterials',
+        material: 'materials/DMAMaterial',
 
         //UI
         navbar: 'menus/Navbar',
diff --git a/js/materials/DMAMaterial.js b/js/materials/DMAMaterial.js
new file mode 100644
index 00000000..de5904a1
--- /dev/null
+++ b/js/materials/DMAMaterial.js
@@ -0,0 +1,100 @@
+/**
+ * Created by aghassaei on 9/22/15.
+ */
+
+
+define(['appState'], function(appState){
+
+    function DMAMaterial(json){
+        this.set(json);
+    }
+
+    DMAMaterial.prototype.set = function(data){
+
+        //check if colors have changed
+        var oldColor = this.color;
+        var oldAltColor = this.altColor;
+
+        var edited = false;
+        if (this.sparseCells) edited = !(_.isEqual(data.sparseCells, this.sparseCells));
+
+        var self = this;
+        _.each(_.keys(data), function(key){
+            if (data[key] && data[key].x) self[key] = new THREE.Vector3(data[key].x, data[key].y, data[key].z);
+            else self[key] = data[key];
+        });
+
+        if (!this.threeMaterial || oldColor != this.color || oldAltColor != this.altColor) this.changeColorScheme();
+
+        if (!data.noDelete) this.noDelete = false;
+    };
+
+    DMAMaterial.prototype.changeColorScheme = function(state){
+        if (!state) state = appState.get("realisticColorScheme");
+        var color = this._getColorForState(state);
+
+        if (this.threeMaterial) this.threeMaterial.color = new THREE.Color(color);
+        else this.threeMaterial = this._makeMaterialObject(color);
+
+        if (this.transparentMaterial) this.transparentMaterial.color = new THREE.Color(color);
+        else this.transparentMaterial = this._makeMaterialObject(color, true);
+    };
+
+    DMAMaterial.prototype._getColorForState = function(state){
+        if (state) return this.color;
+        return this.altColor;
+    };
+
+    DMAMaterial.prototype._makeMaterialObject = function(color, transparent){
+        if (transparent) return new THREE.MeshLambertMaterial({color:color, transparent: true, opacity:0.1});
+        return new THREE.MeshLambertMaterial({color:color});
+    };
+
+    DMAMaterial.prototype.getThreeMaterial = function(id){
+        if (!this.threeMaterial) {
+            console.warn("no transparentMaterial found for material " + id);
+            return null;
+        }
+        return this.threeMaterial;
+    };
+
+    DMAMaterial.prototype.getTransparentMaterial = function(id){
+        if (!this.transparentMaterial) {
+            console.warn("no transparentMaterial found for material " + id);
+            return null;
+        }
+        return this.transparentMaterial;
+    };
+
+    DMAMaterial.prototype.getDimensions = function(){
+        return false;
+    };
+
+    DMAMaterial.prototype.getProperties = function(){
+        return this.properties;
+    };
+
+    DMAMaterial.prototype.canDelete = function(){
+        return !this.noDelete;
+    };
+
+    DMAMaterial.prototype.clone = function(){
+        return JSON.parse(JSON.stringify(this.toJSON()));
+    };
+
+    DMAMaterial.prototype.toJSON = function(){
+        return {
+                name: this.name,
+                color: this.color,
+                altColor: this.altColor,
+                noDelete: this.noDelete,
+                properties: this.properties
+            }
+    };
+
+    DMAMaterial.prototype.destroy = function(){
+
+    };
+
+    return DMAMaterial;
+});
\ No newline at end of file
diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js
index 15ecbf33..8e6c8883 100644
--- a/js/materials/DMAMaterials.js
+++ b/js/materials/DMAMaterials.js
@@ -3,14 +3,15 @@
  */
 
 //everything is a top level material with a threeMaterial object
-define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], function(_, THREE, appState, lattice, plist, three){
+define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel', 'material'],
+    function(_, THREE, appState, lattice, plist, three, DMAMaterial){
 
     var materialsList = {
-        deleteMaterial: {
+        deleteMaterial: new DMAMaterial({
             color: "#ff0000",
-            threeMaterial: makeMaterialObject("#ff0000"),
+            altColor: "#ff0000",
             noDelete: true
-        }
+        })
     };
 
 
@@ -19,7 +20,7 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
 
     listener.listenTo(appState, "change:realisticColorScheme", changeColorScheme);
     listener.listenTo(appState, "change:materialClass", function(){setToDefaultMaterial()});//pass no params
-    listener.listenTo(lattice, "change:connectionType cellType", function(){setToDefaultMaterial()});
+//    listener.listenTo(lattice, "change:connectionType cellType", function(){materialClassChanged()});
     listener.listenTo(appState, "change:materialType", setMaterialDefaults);
 
     setToDefaultMaterial();
@@ -29,23 +30,17 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
 
 
     function setMaterial(id, data){
-        if (!materialsList[id]) materialsList[id] = {};
-
-        //check if colors have changed
-        var oldColor = materialsList[id].color;
-        var oldAltColor = materialsList[id].altColor;
+        var material = getMaterialForId(id);
 
         var edited = false;
-        if (materialsList[id].sparseCells) edited = !(_.isEqual(data.sparseCells, materialsList[id].sparseCells));
-
-        if (data.elementaryChildren) data.properties = getPropertiesFromChildren(data.elementaryChildren);
-
-        _.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 (!material) {
+            materialsList[id] = new DMAMaterial(data);
+            return;
+        } else {
+            if (data.elementaryChildren) data.properties = getPropertiesFromChildren(data.elementaryChildren);
+            edited = material.set(data);
+        }
 
-        if (!materialsList[id].threeMaterial || oldColor != materialsList[id].color || oldAltColor != materialsList[id].altColor) changeSingleMaterialColorScheme(id);
         if (edited){
             var allChangedMaterialsList = getAllParentComposites(id);
             allChangedMaterialsList.push(id);
@@ -64,10 +59,12 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
             console.warn("this material was never saved");
             return true;
         }
-        if (materialsList[id].noDelete) {
+        if (!materialsList[id].canDelete()) {
             console.warn("no delete flag on this material type");
             return false;
         }
+        materialsList[id].destroy();
+        materialsList[id] = null;
         delete materialsList[id];//todo check if being used first (instances)
         var deleted = true;
         if (deleted) setToDefaultMaterial();
@@ -81,19 +78,16 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
             console.warn("no material object found for type "+ id);
             return null;
         }
-        if (!material.threeMaterial){
-            console.warn("no three material object found for type "+ id);
-            return null;
-        }
-        if (transparent) return material.transparentMaterial;
-        return material.threeMaterial;
+        if (transparent) return material.getTransparentMaterial(id);
+        return material.getThreeMaterial(id);
     }
 
     var materialNameIndex = 1;
 
     function getMaterialCopy(id){
-        if (materialsList[id]){
-            return JSON.parse(JSON.stringify(toJSON(id)));
+        var material = getMaterialForId(id);
+        if (material){
+            return JSON.parse(JSON.stringify(material.toJSON()));
         }
         return {
                 name: "Material " + materialNameIndex++,
@@ -104,19 +98,8 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
             };
     }
 
-    function toJSON(id){
-        var material = materialsList[id];
-        return {
-            name: material.name,
-            color: material.color,
-            altColor: material.altColor,
-            noDelete: material.noDelete,
-            properties: material.properties
-        }
-    }
-
     function getDeleteMaterial(){
-        return materialsList.deleteMaterial.threeMaterial;
+        return materialsList.deleteMaterial.getThreeMaterial("deleteMaterial");
     }
 
 
@@ -172,8 +155,9 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
 
     function getPropertiesFromChildren(children){
         var properties = {};
-        _.each(children, function(child){
-            if (materialsList[child].properties.conductive) properties.conductive = true;
+        var self = this;
+        _.each(children, function(childID){
+            if (self.getMaterialForId(childID).getProperties().conductive) properties.conductive = true;
         });
         return properties;
     }
@@ -211,13 +195,9 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
 
     function parseClassFromDefinitions(definitions){
         var newMaterials = {};
-        var state = appState.get("realisticColorScheme");
-        _.each(_.keys(definitions), function(key){
-            newMaterials[key] = definitions[key];
-            var color = getMaterialColorForState(state, definitions[key], key);
-            newMaterials[key].threeMaterial = makeMaterialObject(color);
-            newMaterials[key].transparentMaterial = makeMaterialObject(color, true);
-            newMaterials[key].noDelete = true;//don't delete the predefined materials
+        _.each(definitions, function(data, key){
+            data.noDelete = true;
+            newMaterials[key] = new DMAMaterial(data);
         });
         return newMaterials;
     }
@@ -232,42 +212,17 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
 
     function changeColorScheme(){
         var state = appState.get("realisticColorScheme");
-        _.each(_.keys(materialsList), function(name){
-            if (name == "setMaterial") return;
-            changeSingleMaterialColorScheme(name, state);
+        _.each(materialsList, function(material, name){
+            material.changeColorScheme(state);
         });
         three.render();
     }
 
-    function changeSingleMaterialColorScheme(name, state){
-        if (!state) state = appState.get("realisticColorScheme");
-        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);
-
-        if (materialInfo.transparentMaterial) materialInfo.transparentMaterial.color = new THREE.Color(color);
-        else materialInfo.transparentMaterial = makeMaterialObject(color, true);
-    }
-
-    function getMaterialColorForState(state, definition, key){
-        var color = definition.color;
-        if (!color) console.warn("no color for material type " + key);
-        if (!state && definition.altColor) color = definition.altColor;
-        return color;
-    }
-
-    function makeMaterialObject(color, transparent){
-        if (transparent) return new THREE.MeshLambertMaterial({color:color, transparent: true, opacity:0.1});
-        return new THREE.MeshLambertMaterial({color:color});
-    }
-
     function setMaterialDefaults(){
         var materialType = appState.get("materialType");
         appState.set("superCellIndex", new THREE.Vector3(0,0,0));
-        if (materialsList[materialType].dimensions){
-            appState.set("superCellRange", materialsList[materialType].dimensions.clone());
+        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));
         }
@@ -299,7 +254,6 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
         setToDefaultMaterial: setToDefaultMaterial,
         getDeleteMaterial: getDeleteMaterial,
         getNextCompositeID: getNextCompositeID,
-        getNextMaterialID: getNextMaterialID,
-        toJSON: toJSON
+        getNextMaterialID: getNextMaterialID
     };
 });
\ No newline at end of file
diff --git a/js/models/FileSaver.js b/js/models/FileSaver.js
index 61d79d32..9c62e776 100644
--- a/js/models/FileSaver.js
+++ b/js/models/FileSaver.js
@@ -57,7 +57,7 @@ define(['underscore', 'fileSaverLib', 'lattice', 'materials', 'ribbon', 'menuWra
 
     function saveMaterial(id, material){
         var data = {materials:{}};
-        data.materials[id] = material || _getMaterialDataToSave(id);
+        data.materials[id] = material || _getMaterialDataToSave(material);
         _saveFile(data, data.materials[id].name, "json");
     }
 
@@ -77,14 +77,14 @@ define(['underscore', 'fileSaverLib', 'lattice', 'materials', 'ribbon', 'menuWra
 
     function _getMaterialsDataToSave(){
         var data = {};
-        _.each(_.keys(materials.list), function(key){
-            data[key] = _getMaterialDataToSave(key);
+        _.each(materials.list, function(material, key){
+            data[key] = _getMaterialDataToSave(material);
         });
         return data;
     }
 
-    function _getMaterialDataToSave(id){
-        return materials.toJSON(id);
+    function _getMaterialDataToSave(material){
+        return material.toJSON();
     }
 
     function loadFile(data){//parsed json todo make this better - load composite
-- 
GitLab