From 158b8ababf584b0ed9998fc65612c9b22aadb227 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Sun, 14 Jun 2015 09:47:51 -0700
Subject: [PATCH] hierarchical working

---
 js/cells/CubeCell.js                 |  1 -
 js/cells/DMACell.js                  | 10 +++++-----
 js/cells/supercells/CompositeCell.js | 12 +++++++++---
 js/cells/supercells/DMASuperCell.js  |  6 +++---
 js/menus/Ribbon.js                   |  2 +-
 js/models/AppState.js                |  4 ++--
 js/parts/DMAPart.js                  |  6 +++---
 7 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/js/cells/CubeCell.js b/js/cells/CubeCell.js
index 39db47d4..81d38571 100644
--- a/js/cells/CubeCell.js
+++ b/js/cells/CubeCell.js
@@ -10,7 +10,6 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
 
     function CubeCell(json, superCell){
         DMACell.call(this, json, superCell);
-        console.log("init cube cell");
     }
     CubeCell.prototype = Object.create(DMACell.prototype);
 
diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index bcb3706f..59d365a3 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -26,7 +26,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
             if (!superCell || superCell === undefined) three.sceneAdd(this.object3D);//add object3d as child of scene if top level of hierarchy
         } else this.hide();//stock cell
 
-        if (!this.cells && ( !superCell || superCell === undefined)) this.setMode();
+        if (!this.cells) this.setMode();
     }
 
 
@@ -61,7 +61,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
         var geometry = this._getGeometry();
 
         var meshes = [];
-        var mesh = new THREE.Mesh(geometry, this.getMaterial());
+        var mesh = new THREE.Mesh(geometry, this.getMaterial(true));
         mesh.name = this._getMeshName();
         meshes.push(mesh);
 
@@ -156,7 +156,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
 
         var material;
         if (state) material = globals.materials.deleteMaterial;
-        else  material = this.getMaterial();
+        else  material = this.getMaterial(true);
         if (!material) return;//cell may be deleted by now
         if (this.object3D.children[0].material == material) return;
 
@@ -221,7 +221,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
         this.setMode(mode);
     };
 
-    DMACell.prototype.getMaterial = function(){
+    DMACell.prototype.getMaterial = function(returnTHREEObject){
         if (!this.material) return null;
         var materialClass = lattice.get("materialClass");
         if (!globals.materials[materialClass]) {
@@ -246,7 +246,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals'],
 
         switch(mode) {
             case "supercell":
-                if (!this.superCell) mode = "cell";//top level item
+                if (!this.superCell && !this.cells) mode = "cell";//top level item
                 setVisiblity();
                 break;
             case "cell":
diff --git a/js/cells/supercells/CompositeCell.js b/js/cells/supercells/CompositeCell.js
index 0fa3d900..46d2b7f4 100644
--- a/js/cells/supercells/CompositeCell.js
+++ b/js/cells/supercells/CompositeCell.js
@@ -8,12 +8,11 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell',
 
     CompositeCell = function(json, superCell){
         DMASuperCell.call(this, json, superCell);
-        console.log("initComposite");
     };
     CompositeCell.prototype = Object.create(DMASuperCell.prototype);
 
     CompositeCell.prototype._getGeometry = function(){
-        var dimensions = appState.get("superCellRange");
+        var dimensions = globals.materials.compositeMaterials[this.material].dimensions;
         var geo = new THREE.BoxGeometry(dimensions.x, dimensions.y, dimensions.z);
         geo.applyMatrix(new THREE.Matrix4().makeTranslation(dimensions.x/2-0.5, dimensions.y/2-0.5, dimensions.z/2-0.5));
         return geo;
@@ -27,13 +26,20 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'superCell',
         return wireframe;
     };
 
-    CompositeCell.prototype.getMaterial = function(){
+    CompositeCell.prototype.getMaterial = function(returnTHREEObject){
         if (!this.material) return null;
         var material = globals.materials.compositeMaterials[this.material];
         if (!material){
             console.warn("no material "+ this.material + " found");
             return null;
         }
+        if (returnTHREEObject){
+            if (material.material) return material.material;
+            else {
+                material.material = new THREE.MeshLambertMaterial({color:material.color, shading:THREE.FlatShading});
+                return material.material;
+            }
+        }
         return material;
     };
 
diff --git a/js/cells/supercells/DMASuperCell.js b/js/cells/supercells/DMASuperCell.js
index 84984e07..79eda598 100644
--- a/js/cells/supercells/DMASuperCell.js
+++ b/js/cells/supercells/DMASuperCell.js
@@ -15,9 +15,9 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
 
         var material = this.getMaterial();
         var range = material.dimensions || appState.get("superCellRange");
-        this.cells = this._makeChildCells(range, this.getMaterial());
+        this.cells = this._makeChildCells(range, material);
 
-        if (!superCell || superCell === undefined) this.setMode(null, function(){
+        DMACell.prototype.setMode.call(this, null, function(){
             three.conditionalRender();
         });//don't pass a call down to children again
     }
@@ -59,7 +59,6 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     DMASuperCell.prototype._makeSubCellForIndex = function(json, callback){
         var subclassFile = lattice.getCellSubclassFile();
         if (json.material && json.material.substr(0,5) == "super") subclassFile = "compositeCell";
-        console.log(subclassFile);
         var self = this;
         require([subclassFile], function(CellSubclass){
             var cell = new CellSubclass(json, self);
@@ -100,6 +99,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'cell'],
     };
 
     DMASuperCell.prototype._loopCells = function(callback){
+        console.log(this.cells);
         var cells = this.cells;
         if (!cells || cells === undefined) return;
         for (var x=0;x<cells.length;x++){
diff --git a/js/menus/Ribbon.js b/js/menus/Ribbon.js
index c530aa71..d02e3f38 100644
--- a/js/menus/Ribbon.js
+++ b/js/menus/Ribbon.js
@@ -61,7 +61,7 @@ define(['jquery', 'underscore', 'backbone', 'plist', 'lattice'], function($, _,
         template: _.template('\
             <div class="btn-toolbar">\
                 <div class="btn-group">\
-                  <!--<a data-type="supercell" class="btn btn-primary btn-ribbon ribbonCellMode<% if (cellMode == "supercell"){ %> ribbon-selected<% } %>" href="#">Super</a>-->\
+                  <a data-type="supercell" class="btn btn-primary btn-ribbon ribbonCellMode<% if (cellMode == "supercell"){ %> ribbon-selected<% } %>" href="#">Super</a>\
                   <a data-type="cell" class="btn btn-primary btn-ribbon ribbonCellMode<% if (cellMode == "cell"){ %> ribbon-selected<% } %>" href="#"><img data-type="cell" src="assets/imgs/cell-sm.png"></a>\
                   <% if (allPartTypes[cellType][connectionType]){ %>\
                   <a data-type="part" class="btn btn-primary btn-ribbon ribbonCellMode<% if (cellMode == "part"){ %> ribbon-selected<% } %>" href="#"><img data-type="part" src="assets/imgs/part-sm.png"></a>\
diff --git a/js/models/AppState.js b/js/models/AppState.js
index f384d2ed..a6059661 100644
--- a/js/models/AppState.js
+++ b/js/models/AppState.js
@@ -87,10 +87,10 @@ define(['underscore', 'backbone', 'threeModel', 'three', 'plist'], function(_, B
         },
 
         _updateCellMode: function(currentTab){
-            if (currentTab == "lattice" || currentTab == "import") this.set("cellMode", "cell");
+//            if (currentTab == "lattice" || currentTab == "import") this.set("cellMode", "cell");
             //else if (currentTab == "import") this.set("cellMode", "cell");
             //else if (currentTab == "sketch") this.set("cellMode", "cell");
-            else if (currentTab == "part") this.set("cellMode", "part");
+            if (currentTab == "part") this.set("cellMode", "part");
         },
 
         _navChanged: function(){
diff --git a/js/parts/DMAPart.js b/js/parts/DMAPart.js
index 9a46ede3..73d1b4ac 100644
--- a/js/parts/DMAPart.js
+++ b/js/parts/DMAPart.js
@@ -14,7 +14,7 @@ define(['underscore', 'three'], function(_, THREE){
     }
 
     DMAPart.prototype._buildMesh = function(){
-        var mesh = this._translatePart(this._rotatePart(new THREE.Mesh(this._getGeometry(), this.getMaterial())));
+        var mesh = this._translatePart(this._rotatePart(new THREE.Mesh(this._getGeometry(), this.getMaterial(true))));
         mesh.name = "part";
         return mesh;
     };
@@ -27,8 +27,8 @@ define(['underscore', 'three'], function(_, THREE){
         return mesh;
     };
 
-    DMAPart.prototype.getMaterial = function(){
-        return this.parentCell.getMaterial();
+    DMAPart.prototype.getMaterial = function(returnTHREEObject){
+        return this.parentCell.getMaterial(returnTHREEObject);
     };
 
     DMAPart.prototype.setMaterial = function(material){
-- 
GitLab