From c34a996df7c358fb3d258f1cd5fb0545a78cf689 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 24 Feb 2015 14:53:43 -0500
Subject: [PATCH] mobing stuff around

---
 js/fea/DmaCell.js            |  4 --
 js/models/BasePlane.js       | 12 -----
 js/models/Lattice.js         |  8 +++-
 js/threeViews/Highlighter.js | 91 +++++++++++++++++++++++++-----------
 js/threeViews/ThreeView.js   | 10 ++--
 5 files changed, 78 insertions(+), 47 deletions(-)

diff --git a/js/fea/DmaCell.js b/js/fea/DmaCell.js
index 112f7c1a..ef565e17 100644
--- a/js/fea/DmaCell.js
+++ b/js/fea/DmaCell.js
@@ -76,10 +76,6 @@ DMACell.prototype.getIndex = function(){
     return _.clone(this.indices);
 };
 
-DMACell.prototype.canRemove = function(){
-    return true;//tells highlighter that a cell is something that can be deleted
-};
-
 DMACell.prototype.destroy = function(){
     if (this.cellMesh) {
         window.three.sceneRemove(this.cellMesh, "cell");
diff --git a/js/models/BasePlane.js b/js/models/BasePlane.js
index e3af6785..8ed76cbf 100644
--- a/js/models/BasePlane.js
+++ b/js/models/BasePlane.js
@@ -58,10 +58,6 @@ BasePlane = Backbone.Model.extend({
         return window.lattice.getIndexForPosition(face.geometry.vertices[0]);
     },
 
-    canRemove: function(){
-        return false;//tells highlighter that the baseplane is not something to be deleted
-    },
-
     //subclasses handle getHighlighterVertices
 
     ///////////////////////////////////////////////////////////////////////////////////
@@ -191,10 +187,6 @@ OctaBasePlane = BasePlane.extend({
             vertex.add(position);
         });
         return newVertices;
-    },
-
-    highlighterFaces: function(){
-        return [new THREE.Face3(0,1,2), new THREE.Face3(0,0,0)];
     }
 
 });
@@ -255,10 +247,6 @@ SquareBasePlane = BasePlane.extend({
         vertices.push(new THREE.Vector3(index.x*scale, (index.y+1)*scale, index.z*scale));
 
         return vertices;
-    },
-
-    highlighterFaces: function(){
-        return [new THREE.Face3(0,1,2), new THREE.Face3(0,2,3)];
     }
 
 
diff --git a/js/models/Lattice.js b/js/models/Lattice.js
index 88d27094..c3705b85 100644
--- a/js/models/Lattice.js
+++ b/js/models/Lattice.js
@@ -13,7 +13,8 @@ Lattice = Backbone.Model.extend({
         cellsMin: {x:0, y:0, z:0},//min position of cells matrix
         cellsMax: {x:0, y:0, z:0},//max position of cells matrix
         numCells: 0,
-        basePlane: null//plane to build from
+        basePlane: null,//plane to build from
+        highlighter: null//highlights buildable surfaces
     },
 
     //pass in fillGeometry
@@ -287,6 +288,7 @@ Lattice = Backbone.Model.extend({
         this.clearCells();
         if (this._undo) this._undo();
         if (this.get("basePlane")) this.get("basePlane").destroy();
+        if (this.get("highlighter")) this.get("highlighter").destroy();
         if (cellType == "octa"){
             if (connectionType == "face"){
                 _.extend(this, this.OctaFaceLattice);
@@ -335,6 +337,7 @@ Lattice = Backbone.Model.extend({
             this.listenTo(this, "change:columnSeparation", this._changeColSeparation);
 
             this.set("basePlane", new OctaBasePlane({scale:this.get("scale")}));
+            this.set("highlighter", new OctaFaceHighlighter());
             this.set("columnSeparation", 0.0);
         },
 
@@ -398,6 +401,7 @@ Lattice = Backbone.Model.extend({
             this.listenTo(this, "change:columnSeparation", this._changeColSeparation);
 
             this.set("basePlane", new OctaBasePlane({scale:this.get("scale")}));
+            this.set("highlighter", new OctaFaceHighlighter());
             this.set("columnSeparation", 0.0);
         },
 
@@ -461,6 +465,7 @@ Lattice = Backbone.Model.extend({
             //bind events
 
             this.set("basePlane", new SquareBasePlane({scale:this.get("scale")}));
+            this.set("highlighter", new CubeHighlighter());
         },
 
         getIndexForPosition: function(absPosition){
@@ -512,6 +517,7 @@ Lattice = Backbone.Model.extend({
             //bind events
 
             this.set("basePlane", new SquareBasePlane({scale:this.get("scale")}));
+            this.set("highlighter", new CubeHighlighter());
         },
 
         getIndexForPosition: function(absPosition){
diff --git a/js/threeViews/Highlighter.js b/js/threeViews/Highlighter.js
index ac62a221..e0f62b90 100644
--- a/js/threeViews/Highlighter.js
+++ b/js/threeViews/Highlighter.js
@@ -7,12 +7,8 @@ Highlighter = Backbone.View.extend({
     mesh: null,
     highlightedObject: null,
 
-    initialize: function(){
+    commonInit: function(geometry){
 
-        var geometry = new THREE.Geometry();
-        //can't change size of faces or vertices buffers dynamically
-        geometry.vertices = [new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0)];
-        geometry.faces = this._getFacesArray();
         geometry.dynamic = true;
         this.mesh = new THREE.Mesh(geometry,
             new THREE.MeshBasicMaterial({
@@ -22,20 +18,16 @@ Highlighter = Backbone.View.extend({
                 color:0xffffff,
                 vertexColors:THREE.FaceColors
             }));
+
         window.three.sceneAdd(this.mesh, null);
         this.hide();
 
         //bind events
-        this.listenTo(window.appState, "change:cellType change:connectionType", this.updateHighlighterFaces);
     },
 
-    _getFacesArray: function(){
-        return window.lattice.get("basePlane").highlighterFaces();
-    },
-
-    updateHighlighterFaces: function(){
-        this.mesh.geometry.faces = this._getFacesArray();
-    },
+    ///////////////////////////////////////////////////////////////////////////////////
+    /////////////////////////////VISIBILITY////////////////////////////////////////////
+    ///////////////////////////////////////////////////////////////////////////////////
 
     hide: function(){
         this._setVisibility(false);
@@ -53,6 +45,10 @@ Highlighter = Backbone.View.extend({
         this.mesh.visible = visible;
     },
 
+    isVisible: function(){
+        return this.mesh.visible;
+    },
+
     highlight: function(intersection){
         if (!intersection.object) return;
         var highlightable = intersection.object;
@@ -66,21 +62,22 @@ Highlighter = Backbone.View.extend({
             return;
         }
 
-        for (var i=0;i<newVertices.length;i++){
-            this.mesh.geometry.vertices[i] = newVertices[i];
-        }
-        this.mesh.geometry.verticesNeedUpdate = true;
+        var geometry = this.mesh.geometry;
+        if (geometry.vertices.length != newVertices.length) console.warn("vertices array is changing size");
+        geometry.vertices = newVertices;
+        geometry.verticesNeedUpdate = true;
 //        this.mesh.geometry.normalsNeedUpdate = true;
 //        this.mesh.geometry.computeFaceNormals();
 //        this.mesh.geometry.computeVertexNormals();
-        this.mesh.geometry.computeBoundingSphere();
+        geometry.computeBoundingSphere();
         this.show(true);
     },
 
-    isVisible: function(){
-        return this.mesh.visible;
-    },
+    ///////////////////////////////////////////////////////////////////////////////////
+    /////////////////////////////ADD REMOVE////////////////////////////////////////////
+    ///////////////////////////////////////////////////////////////////////////////////
 
+    //todo this could be more than just z additions
     _getNextCellPosition: function(index){//add one to z index
         index.z += 1;
         return index;
@@ -92,14 +89,54 @@ Highlighter = Backbone.View.extend({
             if (!this.isVisible() || !this.highlightedObject) return;
             window.lattice.addCellAtIndex(this._getNextCellPosition(this.highlightedObject.getIndex(this.mesh)));
         } else {
-            if (!this.highlightedObject || !this.highlightedObject.canRemove()) return;
-            if (this.highlightedObject instanceof DMACell){
-                window.lattice.removeCell(this.highlightedObject);
-                return;
-            }
-//            window.lattice.removeCellAtIndex(this.highlightedObject.getIndex(this.mesh));
+            if (!this.highlightedObject) return;
+            if (!(this.highlightedObject instanceof DMACell)) return;
+            window.lattice.removeCell(this.highlightedObject);
         }
         this.hide();
         this.highlightedObject = null;
+    },
+
+    destroy: function(){
+        window.three.sceneRemove(this.mesh, null);
+        this.mesh = null;
+        this.highlightedObject = null;
+        this.stopListening();
+    }
+});
+
+///////////////////////////////////////////////////////////////////////////////////
+/////////////////////////////OCTA FACE/////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////
+
+OctaFaceHighlighter = Highlighter.extend({
+
+    initialize: function(){
+
+        var geometry = new THREE.Geometry();
+        //can't change size of faces or vertices buffers dynamically
+        geometry.vertices = [new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0)];
+        geometry.faces = [new THREE.Face3(0,1,2)];
+
+        this.commonInit(geometry);
+    }
+
+});
+
+///////////////////////////////////////////////////////////////////////////////////
+/////////////////////////////CUBE /////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////
+
+CubeHighlighter = Highlighter.extend({
+
+    initialize: function(){
+
+        var geometry = new THREE.Geometry();
+        //can't change size of faces or vertices buffers dynamically
+        geometry.vertices = [new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0)];
+        geometry.faces = [new THREE.Face3(0,1,2), new THREE.Face3(0,2,3)];
+
+        this.commonInit(geometry);
     }
+
 });
\ No newline at end of file
diff --git a/js/threeViews/ThreeView.js b/js/threeViews/ThreeView.js
index 1a2e0990..e1533513 100644
--- a/js/threeViews/ThreeView.js
+++ b/js/threeViews/ThreeView.js
@@ -30,15 +30,15 @@ ThreeView = Backbone.View.extend({
 
         //bind events
         this.listenTo(this.appState, "change:deleteMode change:extrudeMode change:shift", this._setControlsEnabled);
+        this.listenTo(window.lattice, "change:highlighter", this._saveHighlighter);
+
+        this._saveHighlighter();//need a reference to the highlighter
 
         this.controls = new THREE.OrbitControls(this.model.camera, this.$el.get(0));
         this.controls.addEventListener('change', this.model.render);
 
         this.$el.append(this.model.domElement);//render only once
 
-        //init highlighter
-        this.highlighter = new Highlighter();
-
         this.model.render();
         this._animate();
     },
@@ -124,6 +124,10 @@ ThreeView = Backbone.View.extend({
     ///////////////////////////////INTERSECTIONS////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////////
 
+    _saveHighlighter: function(){
+        this.highlighter = window.lattice.get("highlighter");
+    },
+
     _setNoPartIntersections: function(){
         if (this.currentIntersectedPart){
             this.currentIntersectedPart.unhighlight();
-- 
GitLab