From 777623ee27bc7eb0b66cd478bbc651c3f31cf2bd Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 30 Jan 2015 18:34:19 -0500
Subject: [PATCH] delete parts

---
 js/fea/dmaCell.js          | 13 +++++++++----
 js/fea/dmaPart.js          |  9 ++++++++-
 js/threeViews/threeView.js |  3 ++-
 3 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/js/fea/dmaCell.js b/js/fea/dmaCell.js
index e6a48f3e..8acd45aa 100644
--- a/js/fea/dmaCell.js
+++ b/js/fea/dmaCell.js
@@ -70,6 +70,11 @@
         return parts;
     };
 
+    DMACell.prototype.removePart = function(index){
+        this.parts[index].destroy();
+        this.parts[index] = null;
+    };
+
     DMACell.prototype._buildCellMesh = function(position, zIndex){//abstract mesh representation of cell
 
         var mesh;
@@ -96,12 +101,12 @@
         if (mode == "cell"){
             this._setCellMeshVisibility(true);
             _.each(this.parts, function(part){
-                part.hide();
+                if (part) part.hide();
             });
         } else if (mode == "part"){
             this._setCellMeshVisibility(false);
             _.each(this.parts, function(part){
-                part.show();
+                if (part) part.show();
             });
         } else {
             console.warn("unrecognized draw mode for cell");
@@ -130,7 +135,7 @@
         this.position = this._calcPosition(scale, this.indices);
         this._setMeshPosition(this.cellMesh, this.position);
         _.each(this.parts, function(part){
-                part.changeScale(scale, this.position);
+                if (part) part.changeScale(scale, this.position);
          });
     };
 
@@ -155,7 +160,7 @@
             this.cellMesh = null;
         }
         _.each(this.parts, function(part){
-            part.destroy();
+            if (part) part.destroy();
         });
         this.indices = null;
         this.scale = null;
diff --git a/js/fea/dmaPart.js b/js/fea/dmaPart.js
index dae36c09..b6c0489b 100644
--- a/js/fea/dmaPart.js
+++ b/js/fea/dmaPart.js
@@ -117,7 +117,14 @@
     };
 
     DMAPart.prototype.unhighlight = function(){
-        this.mesh.material.color.setRGB(0.9619657144369509, 0.6625466032079207, 0.20799727886007258);
+        if (this.mesh) this.mesh.material.color.setRGB(0.9619657144369509, 0.6625466032079207, 0.20799727886007258);
+    };
+
+    DMAPart.prototype.removeFromCell = function(){//send message back to parent cell to destroy this
+        if (this.parentCell) {
+            this.parentCell.removePart(this.type);
+            window.three.render();
+        }
     };
 
     DMAPart.prototype.destroy = function(){
diff --git a/js/threeViews/threeView.js b/js/threeViews/threeView.js
index 25f693c4..8146283b 100644
--- a/js/threeViews/threeView.js
+++ b/js/threeViews/threeView.js
@@ -90,7 +90,8 @@ ThreeView = Backbone.View.extend({
 
     _mouseUp: function(){
         this.mouseIsDown = false;
-        this._addRemoveVoxel(!this.appState.get("deleteMode"));
+        if (this.currentIntersectedPart) this.currentIntersectedPart.removeFromCell();
+        else this._addRemoveVoxel(!this.appState.get("deleteMode"));
     },
 
     _mouseDown: function(){
-- 
GitLab