Просмотр исходного кода

Remove callbacks

nodes and scene changed to allow events based on mesh/camera/light
deletion.
Raanan Weber 10 лет назад
Родитель
Сommit
0cf9882fda

+ 1 - 2
Babylon/Cameras/babylon.camera.js

@@ -261,8 +261,7 @@ var BABYLON;
         };
         Camera.prototype.dispose = function () {
             // Remove from scene
-            var index = this.getScene().cameras.indexOf(this);
-            this.getScene().cameras.splice(index, 1);
+            var index = this.getScene().removeCamera(this);
             for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {
                 this._postProcesses[this._postProcessesTakenIndices[i]].dispose(this);
             }

+ 1 - 2
Babylon/Cameras/babylon.camera.ts

@@ -333,8 +333,7 @@
 
         public dispose(): void {
             // Remove from scene
-            var index = this.getScene().cameras.indexOf(this);
-            this.getScene().cameras.splice(index, 1);
+            var index = this.getScene().removeCamera(this);
 
             // Postprocesses
             for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {

+ 1 - 2
Babylon/Lights/babylon.light.js

@@ -61,8 +61,7 @@ var BABYLON;
                 this._shadowGenerator = null;
             }
             // Remove from scene
-            var index = this.getScene().lights.indexOf(this);
-            this.getScene().lights.splice(index, 1);
+            var index = this.getScene().removeLight(this);
         };
         return Light;
     })(BABYLON.Node);

+ 1 - 2
Babylon/Lights/babylon.light.ts

@@ -92,8 +92,7 @@
             }
 
             // Remove from scene
-            var index = this.getScene().lights.indexOf(this);
-            this.getScene().lights.splice(index, 1);
+            var index = this.getScene().removeLight(this);
         }
     }
 } 

+ 1 - 5
Babylon/Mesh/babylon.abstractMesh.js

@@ -752,11 +752,7 @@ var BABYLON;
             // SubMeshes
             this.releaseSubMeshes();
             // Remove from scene
-            var index = this.getScene().meshes.indexOf(this);
-            if (index != -1) {
-                // Remove from the scene if mesh found 
-                this.getScene().meshes.splice(index, 1);
-            }
+            var index = this.getScene().removeMesh(this);
             if (!doNotRecurse) {
                 for (index = 0; index < this.getScene().particleSystems.length; index++) {
                     if (this.getScene().particleSystems[index].emitter == this) {

+ 1 - 5
Babylon/Mesh/babylon.abstractMesh.ts

@@ -895,11 +895,7 @@
             this.releaseSubMeshes();
 
             // Remove from scene
-            var index = this.getScene().meshes.indexOf(this);
-            if (index != -1) {
-                // Remove from the scene if mesh found 
-                this.getScene().meshes.splice(index, 1);
-            }
+            var index = this.getScene().removeMesh(this);
 
             if (!doNotRecurse) {
                 // Particles

+ 33 - 0
Babylon/babylon.scene.js

@@ -536,6 +536,39 @@ var BABYLON;
                 this.onNewMeshAdded(newMesh, position, this);
             }
         };
+        Scene.prototype.removeMesh = function (toRemove) {
+            var index = this.meshes.indexOf(toRemove);
+            if (index != -1) {
+                // Remove from the scene if mesh found 
+                this.meshes.splice(index, 1);
+            }
+            if (this.onMeshRemoved) {
+                this.onMeshRemoved(toRemove);
+            }
+            return index;
+        };
+        Scene.prototype.removeLight = function (toRemove) {
+            var index = this.lights.indexOf(toRemove);
+            if (index != -1) {
+                // Remove from the scene if mesh found 
+                this.lights.splice(index, 1);
+            }
+            if (this.onLightRemoved) {
+                this.onLightRemoved(toRemove);
+            }
+            return index;
+        };
+        Scene.prototype.removeCamera = function (toRemove) {
+            var index = this.cameras.indexOf(toRemove);
+            if (index != -1) {
+                // Remove from the scene if mesh found 
+                this.cameras.splice(index, 1);
+            }
+            if (this.onCameraRemoved) {
+                this.onCameraRemoved(toRemove);
+            }
+            return index;
+        };
         Scene.prototype.addLight = function (newLight) {
             var position = this.lights.push(newLight);
             if (this.onNewLightAdded) {

+ 39 - 0
Babylon/babylon.scene.ts

@@ -103,6 +103,7 @@
         */
         public lights = new Array<Light>();
         public onNewLightAdded: (newLight?: Light, positionInArray?: number, scene?: Scene) => void;
+        public onLightRemoved: (removedLight?: Light) => void;
 
         // Cameras
         /**
@@ -112,6 +113,7 @@
         */
         public cameras = new Array<Camera>();
         public onNewCameraAdded: (newCamera?: Camera, positionInArray?: number, scene?: Scene) => void;
+        public onCameraRemoved: (removedCamera?: Camera) => void;
         public activeCameras = new Array<Camera>();
         public activeCamera: Camera;
 
@@ -123,6 +125,7 @@
         */
         public meshes = new Array<AbstractMesh>();
         public onNewMeshAdded: (newMesh?: AbstractMesh, positionInArray?: number, scene?: Scene) => void;
+        public onMeshRemoved: (removedMesh?: AbstractMesh) => void;
 
         // Geometries
         private _geometries = new Array<Geometry>();
@@ -725,6 +728,42 @@
             }
         }
 
+        public removeMesh(toRemove: AbstractMesh) : number {
+            var index = this.meshes.indexOf(toRemove);
+            if (index != -1) {
+                // Remove from the scene if mesh found 
+                this.meshes.splice(index, 1);
+            }
+            if (this.onMeshRemoved) {
+                this.onMeshRemoved(toRemove);
+            }
+            return index;
+        }
+
+        public removeLight(toRemove: Light) : number {
+            var index = this.lights.indexOf(toRemove);
+            if (index != -1) {
+                // Remove from the scene if mesh found 
+                this.lights.splice(index, 1);
+            }
+            if (this.onLightRemoved) {
+                this.onLightRemoved(toRemove);
+            }
+            return index;
+        }
+
+        public removeCamera(toRemove: Camera) : number {
+            var index = this.cameras.indexOf(toRemove);
+            if (index != -1) {
+                // Remove from the scene if mesh found 
+                this.cameras.splice(index, 1);
+            }
+            if (this.onCameraRemoved) {
+                this.onCameraRemoved(toRemove);
+            }
+            return index;
+        }
+
         public addLight(newLight: Light) {
             var position = this.lights.push(newLight);
             if (this.onNewLightAdded) {