Bladeren bron

snapping observable

Trevor Baron 7 jaren geleden
bovenliggende
commit
f980c1f309

+ 6 - 0
src/Gizmos/babylon.axisDragGizmo.ts

@@ -10,6 +10,11 @@ module BABYLON {
          */
         public snapDistance = 0;
         /**
+         * Event that fires each time the gizmo snaps to a new location.
+         * * snapDistance is the the change in distance
+         */
+        public snapObservable = new Observable<{snapDistance:number}>();
+        /**
          * Creates an AxisDragGizmo
          * @param gizmoLayer The utility layer the gizmo will be added to
          * @param dragAxis The axis which the gizmo will be able to drag on
@@ -68,6 +73,7 @@ module BABYLON {
                             event.delta.normalizeToRef(tmpVector);
                             tmpVector.scaleInPlace(this.snapDistance*dragSteps);
                             this.attachedMesh.position.addInPlace(tmpVector);
+                            this.snapObservable.notifyObservers({snapDistance: this.snapDistance*dragSteps});
                         }
                     }
                 }

+ 14 - 2
src/Gizmos/babylon.axisScaleGizmo.ts

@@ -10,6 +10,11 @@ module BABYLON {
          */
         public snapDistance = 0;
         /**
+         * Event that fires each time the gizmo snaps to a new location.
+         * * snapDistance is the the change in distance
+         */
+        public snapObservable = new Observable<{snapDistance:number}>();
+        /**
          * Creates an AxisScaleGizmo
          * @param gizmoLayer The utility layer the gizmo will be added to
          * @param dragAxis The axis which the gizmo will be able to scale on
@@ -60,14 +65,17 @@ module BABYLON {
                 
                 if(this.attachedMesh){
                     // Snapping logic
+                    var snapped = false;
+                    var dragSteps = 0;
                     if(this.snapDistance == 0){
                         dragAxis.scaleToRef(event.dragDistance, tmpVector);
                     }else{
-                        currentSnapDragDistance+=event.dragDistance
+                        currentSnapDragDistance+=event.dragDistance;
                         if(Math.abs(currentSnapDragDistance)>this.snapDistance){
-                            var dragSteps = Math.floor(currentSnapDragDistance/this.snapDistance);
+                            dragSteps = Math.floor(currentSnapDragDistance/this.snapDistance);
                             currentSnapDragDistance = currentSnapDragDistance % this.snapDistance;
                             dragAxis.scaleToRef(this.snapDistance*dragSteps, tmpVector);
+                            snapped = true;
                         }else{
                             tmpVector.scaleInPlace(0);
                         }
@@ -88,6 +96,10 @@ module BABYLON {
                     }else{
                         this.attachedMesh.scaling.subtractInPlace(tmpVector);
                     }
+
+                    if(snapped){
+                        this.snapObservable.notifyObservers({snapDistance: this.snapDistance*dragSteps});
+                    }
                 }
             })
 

+ 10 - 0
src/Gizmos/babylon.planeRotationGizmo.ts

@@ -10,6 +10,11 @@ module BABYLON {
          * Rotation distance in radians that the gizmo will snap to (Defult: 0)
          */
         public snapDistance = 0;
+        /**
+         * Event that fires each time the gizmo snaps to a new location.
+         * * snapDistance is the the change in distance
+         */
+        public snapObservable = new Observable<{snapDistance:number}>();
 
         /**
          * Creates a PlaneRotationGizmo
@@ -92,12 +97,14 @@ module BABYLON {
                     if (halfCircleSide) angle = -angle;
                     
                     // Snapping logic
+                    var snapped = false;
                     if(this.snapDistance != 0){
                         currentSnapDragDistance+=angle
                         if(Math.abs(currentSnapDragDistance)>this.snapDistance){
                             var dragSteps = Math.floor(currentSnapDragDistance/this.snapDistance);
                             currentSnapDragDistance = currentSnapDragDistance % this.snapDistance;
                             angle = this.snapDistance*dragSteps;
+                            snapped = true;
                         }else{
                             angle = 0;
                         }
@@ -110,6 +117,9 @@ module BABYLON {
                      this.attachedMesh.rotationQuaternion.multiplyToRef(amountToRotate,this.attachedMesh.rotationQuaternion);
 
                     lastDragPosition = event.dragPlanePoint;
+                    if(snapped){
+                        this.snapObservable.notifyObservers({snapDistance: angle});
+                    }
                 }
             })