From de80ee5719b62bff9295007a9a72daa6620b6766 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 22 Sep 2015 00:32:39 -0400
Subject: [PATCH] composite editing

---
 js/cells/DMACell.js                  | 16 ++++-------
 js/cells/supercells/CompositeCell.js |  2 +-
 js/materials/DMAMaterials.js         | 43 ++++++++++++++++++++++++----
 js/menus/CompositeMenuView.js        |  3 +-
 js/menus/MaterialEditorMenuView.js   | 13 +--------
 js/models/FileSaver.js               |  2 +-
 todo                                 |  5 ++--
 7 files changed, 51 insertions(+), 33 deletions(-)

diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 7b34b746..df655407 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -232,18 +232,12 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', '
             console.warn("no material type set for cell");
             return null;
         }
-        if (!materials.list[this.materialName]) {
-            console.warn("no material object found of type ");
-            console.warn(this.materialName);
+        var material = materials.getMaterialForId(this.materialName, returnTHREEObject, this.isTransparent);
+        if (!material) {
+            console.warn("no material object found of type " + id);
             return null;
         }
-        if (!returnTHREEObject) return materials.list[this.materialName];
-        if (!materials.list[this.materialName].threeMaterial){
-            console.warn("no three material object found for type "+ this.materialName);
-            return null;
-        }
-        if (this.isTransparent) return materials.list[this.materialName].transparentMaterial;
-        return materials.list[this.materialName].threeMaterial;
+        return material;
     };
 
     DMACell.prototype.setMaterial = function(material){
@@ -489,7 +483,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', '
         var data = {
             materialName: this.materialName
         };
-//        if (materials.list[this.materialName].sparseCells) return data;//material definition in material composites
+//        if (materials.getMaterialForID(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 0547bbee..9720a4d8 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.list[this.materialName].dimensions;
+        var dimensions = materials.getMaterialForId(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/materials/DMAMaterials.js b/js/materials/DMAMaterials.js
index 9d0d99ad..bf181022 100644
--- a/js/materials/DMAMaterials.js
+++ b/js/materials/DMAMaterials.js
@@ -57,8 +57,6 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
 
             lattice.reinitAllCellsOfTypes(allChangedMaterialsList);
         }
-
-        return false;
     }
 
     function deleteMaterial(id){
@@ -76,8 +74,41 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
         return deleted;
     }
 
-    function getMaterialForId(id){
-        return materialsList[id];
+    function getMaterialForId(id, returnTHREEObject, transparent){
+        var material = materialsList[id];
+        if (!returnTHREEObject) return material;
+        if (!material.threeMaterial){
+            console.warn("no three material object found for type "+ id);
+            return null;
+        }
+        if (transparent) return material.transparentMaterial;
+        return material.threeMaterial;
+    }
+
+    var materialNameIndex = 1;
+
+    function getMaterialCopy(id){
+        if (materialsList[id]){
+            return JSON.parse(JSON.stringify(toJSON(id)));
+        }
+        return {
+                name: "Material " + materialNameIndex++,
+                color: '#000000',
+                altColor: '#000000',
+                noDelete: false,
+                properties: {}
+            };
+    }
+
+    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(){
@@ -257,12 +288,14 @@ define(['underscore', 'three', 'appState', 'lattice', 'plist', 'threeModel'], fu
         setMaterial: setMaterial,
         deleteMaterial: deleteMaterial,
         getMaterialForId: getMaterialForId,
+        getMaterialCopy: getMaterialCopy,
         getCompositeKeys: getCompositeKeys,
         getVaildAvailableCompositeKeys: getVaildAvailableCompositeKeys,
         getChildCellTypes:getChildCellTypes,
         setToDefaultMaterial: setToDefaultMaterial,
         getDeleteMaterial: getDeleteMaterial,
         getNextCompositeID: getNextCompositeID,
-        getNextMaterialID: getNextMaterialID
+        getNextMaterialID: getNextMaterialID,
+        toJSON: toJSON
     };
 });
\ No newline at end of file
diff --git a/js/menus/CompositeMenuView.js b/js/menus/CompositeMenuView.js
index 8ae3da36..3daed9ec 100644
--- a/js/menus/CompositeMenuView.js
+++ b/js/menus/CompositeMenuView.js
@@ -78,8 +78,9 @@ define(['jquery', 'underscore', 'menuParent', 'compositeEditorLattice', 'plist',
 
         _saveCompositeToFile: function(e){
             e.preventDefault();
+            var self = this;
             require(['fileSaver'], function(fileSaver){
-                fileSaver.saveMaterial(this.composite.get("id"), this.composite.toJSONForSave(bounds));
+                fileSaver.saveMaterial(self.composite.get("id"), self.composite.toJSONForSave(bounds));//todo save children too
             });
         },
 
diff --git a/js/menus/MaterialEditorMenuView.js b/js/menus/MaterialEditorMenuView.js
index d7e0a224..3cdaf15a 100644
--- a/js/menus/MaterialEditorMenuView.js
+++ b/js/menus/MaterialEditorMenuView.js
@@ -6,8 +6,6 @@
 define(['jquery', 'underscore', 'menuParent', 'plist', 'materials', 'text!materialEditorMenuTemplate', 'fileSaver'],
     function($, _, MenuParentView, plist, materials, template, fileSaver){
 
-    var materialNameIndex = 1;
-
     return MenuParentView.extend({
 
         events: {
@@ -25,16 +23,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'materials', 'text!materi
             if (id == "new") id = materials.getNextMaterialID();
             this.materialID = id;
 
-            var material = _.clone(materials.getMaterialForId(options.myObject));//todo do this in dmamaterials
-            if (!material) material = {
-                name: "Material " + materialNameIndex++,
-                color: '#000000',
-                altColor: '#000000',
-                noDelete: false,
-                properties: {}
-            };
-            this.material = material;
-
+            this.material = materials.getMaterialCopy(id);
         },
 
         getPropertyOwner: function($target){
diff --git a/js/models/FileSaver.js b/js/models/FileSaver.js
index 5015d443..61d79d32 100644
--- a/js/models/FileSaver.js
+++ b/js/models/FileSaver.js
@@ -84,7 +84,7 @@ define(['underscore', 'fileSaverLib', 'lattice', 'materials', 'ribbon', 'menuWra
     }
 
     function _getMaterialDataToSave(id){
-        return _.omit(materials.list[id], "threeMaterial", "transparentMaterial");
+        return materials.toJSON(id);
     }
 
     function loadFile(data){//parsed json todo make this better - load composite
diff --git a/todo b/todo
index 4f82b6a5..891900bd 100644
--- a/todo
+++ b/todo
@@ -1,10 +1,11 @@
 import stl fill - offset, scale, rotation
 
-hierarchical
+composite
     - origin
     - rotation
     - change bounding box
-    - placement ui 3D
+    - placement ui 3D could be better
+    - need material class
 
 material transformations -> change material of a cell, change in bulk
 
-- 
GitLab