From f7b6a35d2aab3aa8ceb88888c11b3d9497c55c33 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Sun, 25 Jan 2015 18:08:29 -0500
Subject: [PATCH] able to rapidly delete cells

---
 js/models/threeModel.js    | 11 +++++++---
 js/threeViews/threeView.js | 43 +++++++++++++++++++++++++++-----------
 2 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/js/models/threeModel.js b/js/models/threeModel.js
index 393f7e20..39ede100 100644
--- a/js/models/threeModel.js
+++ b/js/models/threeModel.js
@@ -52,8 +52,13 @@ function ThreeModel(){
     }
 
     function sceneRemove(object){
-        scene.remove(object);
-        objects.remove(object);
+        console.log(object.parent);
+        var objectToRemove = object;
+        if (object.parent !== THREE.Scene) {
+            objectToRemove = object.parent;
+        }
+        scene.remove(objectToRemove);
+        objects.splice(objects.indexOf(objectToRemove), 1);
     }
 
     function render(){
@@ -61,7 +66,7 @@ function ThreeModel(){
         renderer.render(scene, camera);
     }
 
-    function clearAll(){
+    function clearAll(){x
         var children = scene.children;
         for (var i=children.length;i>=0;i--){
             var object = children[i];
diff --git a/js/threeViews/threeView.js b/js/threeViews/threeView.js
index bdb24588..87d6c414 100644
--- a/js/threeViews/threeView.js
+++ b/js/threeViews/threeView.js
@@ -11,10 +11,13 @@ ThreeView = Backbone.View.extend({
     },
 
     mouseIsDown: false,//store state of mouse click
-    shiftIsDown:false,//used to add many voxels at once
+    shiftIsDown: false,//used to add many voxels at once
+    deleteMode: false,//delete cells instead of adding (space bar)
     mouseProjection: new THREE.Raycaster(),
     highlighter: null,
     currentHighlightedFace: null,
+    currentIntersectedObject: null,
+    basePlane: null,
 
     el: "#threeContainer",
 
@@ -39,7 +42,7 @@ ThreeView = Backbone.View.extend({
 
         this._animate();
 
-        this.drawBasePlane();
+        this.basePlane = this.drawBasePlane();
 
         //init highlighter
         var highlightGeometry = new THREE.Geometry();
@@ -64,19 +67,22 @@ ThreeView = Backbone.View.extend({
 
 //        e.preventDefault();
         var state = e.data.state;
+        console.log(state);
 
         switch(e.keyCode){
             case 16://shift
                 this.shiftIsDown = state;
                 this.controls.enabled = !state;
                 break;
+            case 32://space bar
+                this.deleteMode = state;
             default:
         }
     },
 
     _mouseUp: function(){
         this.mouseIsDown = false;
-        this._addVoxel();
+        this._addRemoveVoxel();
     },
 
     _mouseDown: function(){
@@ -98,10 +104,18 @@ ThreeView = Backbone.View.extend({
         //check if we're intersecting anything
         var intersections = this.mouseProjection.intersectObjects(this.model.objects, true);
         if (intersections.length == 0) {
+            this.currentIntersectedObject == null;
             this._hideHighlighter();
             return;
         }
 
+        this.currentIntersectedObject = intersections[0].object;
+
+        if (this.deleteMode && this.mouseIsDown && this.shiftIsDown){
+            this._addRemoveVoxel();
+            return;
+        }
+
         //check if we've moved to a new face
         var intersection = intersections[0].face;
         if (this.highlighter.visible && this.currentHighlightedFace == intersection) return;
@@ -109,9 +123,6 @@ ThreeView = Backbone.View.extend({
         if (intersection.normal.z<0.99){//only highlight horizontal faces
             this._hideHighlighter();
             return;
-
-            //delete cell if side clicked
-//            window.three.sceneRemove(intersection.object);
         }
 
         //update highlighter
@@ -126,14 +137,21 @@ ThreeView = Backbone.View.extend({
             (new THREE.Vector3()).addVectors(vertices[intersection.b], position), (new THREE.Vector3()).addVectors(vertices[intersection.c], position)];
         this.highlighter.geometry.verticesNeedUpdate = true;
 
-        if (this.mouseIsDown && this.shiftIsDown) this._addVoxel();
+        if (this.mouseIsDown && this.shiftIsDown) this._addRemoveVoxel();
 
         window.three.render();
     },
 
-    _addVoxel: function(){
-        if (!this.highlighter.visible) return;
-        this.lattice.addCell(this.highlighter.geometry.vertices[0]);
+    _addRemoveVoxel: function(){
+
+        if (this.deleteMode){
+            if (this.currentIntersectedObject === this.basePlane) return;
+            window.three.sceneRemove(this.currentIntersectedObject);
+            window.three.render();
+        } else {
+            if (!this.highlighter.visible) return;
+            this.lattice.addCell(this.highlighter.geometry.vertices[0]);
+        }
         this._hideHighlighter();
     },
 
@@ -207,9 +225,10 @@ ThreeView = Backbone.View.extend({
 
         geometry.computeFaceNormals();
 
-        window.three.sceneAdd(new THREE.Mesh(geometry, planeMaterial));
+        var basePlane = new THREE.Mesh(geometry, planeMaterial);
+        window.three.sceneAdd(basePlane);
 
-        return vertices;
+        return basePlane;
     },
 
     scaleBasePlane: function(){
-- 
GitLab