From 88811bb73ca90602a3cda4fc0411523286e4de18 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 27 Oct 2015 18:45:02 -0400
Subject: [PATCH] materials api

---
 css/main.css                       |  1 +
 js/API/MaterialsAPI.js             | 11 ++++++++---
 js/materials/DMAMaterials.js       | 30 +++++++++++++++++++-----------
 js/menus/MaterialEditorMenuView.js |  2 +-
 4 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/css/main.css b/css/main.css
index 5cae6e4a..6ac6ff3d 100644
--- a/css/main.css
+++ b/css/main.css
@@ -587,6 +587,7 @@ label {
     border-bottom-width: 0;
     border-top-width: 0;
     overflow: auto;
+    overflow-x: hidden;
     resize: vertical;
     padding: 8px 20px;
     font-family: Courier, monospace;
diff --git a/js/API/MaterialsAPI.js b/js/API/MaterialsAPI.js
index da2e0a2a..ee7a1f24 100644
--- a/js/API/MaterialsAPI.js
+++ b/js/API/MaterialsAPI.js
@@ -3,12 +3,15 @@
  */
 
 
-define(['materials'], function(materials){
+define(['materials', 'console'], function(materials, myConsole){
 
+    function _printGetter(output){
+        myConsole.log(output);
+    }
 
     return {
 
-        init: function(json){//create a new material
+        newMaterial: function(json){//create a new material
             return materials.newMaterial(json);//return DMAMaterial object
         },
 
@@ -17,7 +20,9 @@ define(['materials'], function(materials){
         },
 
         getMaterialForId: function(id){
-            return materials.getMaterialForId(id);
+            var output = materials.getMaterialID(id);
+            _printGetter(output);
+            return output;
         },
 
         getMaterials: function(){
diff --git a/js/materials/DMAMaterials.js b/js/materials/DMAMaterials.js
index c844d3cb..71ff7eb7 100644
--- a/js/materials/DMAMaterials.js
+++ b/js/materials/DMAMaterials.js
@@ -3,8 +3,8 @@
  */
 
 //everything is a top level material with a threeMaterial object
-define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeModel', 'material', 'compositeMaterial'],
-    function(_, THREE, appState, lattice, materialsPlist, three, DMAMaterial, DMACompositeMaterial){
+define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeModel', 'material', 'compositeMaterial', 'console'],
+    function(_, THREE, appState, lattice, materialsPlist, three, DMAMaterial, DMACompositeMaterial, myConsole){
 
 
     var materialsList = {
@@ -15,10 +15,10 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo
         color: "#ff0000",
         altColor: "#ff0000",
         noDelete: true
-    });
+    }, {silent:true} );
 
-    var compositeID = 0;
-    var materialID = 0;
+    var compositeID = 1;
+    var materialID = 1;
 
     function getNextCompositeID(){
         return "super" + compositeID++;
@@ -40,7 +40,8 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo
 
 
 
-    function newMaterial(data, noAdd){
+    function newMaterial(data, options){
+        options = options || {};
         var material, id;
         if (data.sparseCells) {
             id = data.id || getNextCompositeID();
@@ -50,9 +51,10 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo
             material = new DMAMaterial(data, id);
         }
 
-        if (noAdd) return material;//in the new material menu, you may init a material before saving changes
+        if (options.noAdd) return material;//in the new material menu, you may init a material before saving changes
 
         materialsList[id] = material;
+        if (!options.silent) myConsole.write("materials.newMaterial(" + JSON.stringify(material.toJSON()) + "}");
         return material;
     }
 
@@ -61,11 +63,14 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo
             console.warn("this material was never saved");
             return true;
         }
-        if (!materialsList[id].canDelete()) {
-            console.warn("no delete flag on this material type");
+        var material = materialsList[id];
+        if (!material.canDelete()) {
+            myConsole.warn("noDelete flag on this material type, deleteMaterial operation cancelled");
             return false;
         }
-        materialsList[id].destroy();
+        myConsole.write("materials.deleteMaterial(" + id + "}");
+        myConsole.log(JSON.stringify(material.toJSON()));
+        material.destroy();
         materialsList[id] = null;
         delete materialsList[id];//todo check if being used first (instances)
         var deleted = true;
@@ -90,6 +95,8 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo
             edited = material.set(data);
         }
 
+        myConsole.write("materials.setMaterial(" + id + ", " + JSON.stringify(material.toJSON()) + "}");
+
         if (edited){
             var allChangedMaterialsList = getAllParentComposites(id);
             allChangedMaterialsList.push(id);
@@ -217,7 +224,8 @@ define(['underscore', 'three', 'appState', 'lattice', 'materialsPlist', 'threeMo
         var newMaterials = {};
         _.each(definitions, function(data, key){
             data.noDelete = true;
-            newMaterials[key] = new DMAMaterial(data, key);
+            data.id = key;
+            newMaterial(data, {silent:true});
         });
         return newMaterials;
     }
diff --git a/js/menus/MaterialEditorMenuView.js b/js/menus/MaterialEditorMenuView.js
index f44ab0bf..ec82398a 100644
--- a/js/menus/MaterialEditorMenuView.js
+++ b/js/menus/MaterialEditorMenuView.js
@@ -26,7 +26,7 @@ define(['jquery', 'underscore', 'menuParent', 'plist', 'materials', 'text!menus/
             var json = {};
             if (material) json = material.toJSON();
 
-            this.material = materials.newMaterial(json, true);
+            this.material = materials.newMaterial(json, {noAdd: true});
         },
 
         getPropertyOwner: function($target){
-- 
GitLab