Browse Source

Merge pull request #8965 from CedricGuillemet/gizmoIsHovered

ishovered check for gizmos
David Catuhe 5 years ago
parent
commit
694afae1d2

+ 1 - 0
dist/preview release/what's new.md

@@ -23,6 +23,7 @@
 - Refactored React refs from old string API to React.createRef() API ([belfortk](https://github.com/belfortk))
 - Scale on one axis for `BoundingBoxGizmo` ([cedricguillemet](https://github.com/cedricguillemet))
 - Camera gizmo ([cedricguillemet](https://github.com/cedricguillemet))
+- gizmo isHovered boolean ([cedricguillemet](https://github.com/cedricguillemet))
 - Node support (Transform, Bone) for gizmos ([cedricguillemet](https://github.com/cedricguillemet))
 - Simplified code contributions by fully automating the dev setup with gitpod ([nisarhassan12](https://github.com/nisarhassan12))
 - Add a `CascadedShadowMap.IsSupported` method and log an error instead of throwing an exception when CSM is not supported ([Popov72](https://github.com/Popov72))

+ 2 - 2
src/Gizmos/axisDragGizmo.ts

@@ -137,8 +137,8 @@ export class AxisDragGizmo extends Gizmo {
             if (this._customMeshSet) {
                 return;
             }
-            var isHovered = pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1);
-            var material = isHovered ? this._hoverMaterial : this._coloredMaterial;
+            this._isHovered = !!(pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1));
+            var material = this._isHovered ? this._hoverMaterial : this._coloredMaterial;
             this._rootMesh.getChildMeshes().forEach((m) => {
                 m.material = material;
                 if ((<LinesMesh>m).color) {

+ 2 - 2
src/Gizmos/axisScaleGizmo.ts

@@ -144,8 +144,8 @@ export class AxisScaleGizmo extends Gizmo {
             if (this._customMeshSet) {
                 return;
             }
-            var isHovered = pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1);
-            var material = isHovered ? this._hoverMaterial : this._coloredMaterial;
+            this._isHovered = !!(pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1));
+            var material = this._isHovered ? this._hoverMaterial : this._coloredMaterial;
             this._rootMesh.getChildMeshes().forEach((m) => {
                 m.material = material;
                 if ((<LinesMesh>m).color) {

+ 2 - 2
src/Gizmos/cameraGizmo.ts

@@ -44,8 +44,8 @@ export class CameraGizmo extends Gizmo {
                 return;
             }
 
-            var isHovered = pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1);
-            if (isHovered && pointerInfo.event.button === 0) {
+            this._isHovered = !!(pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1));
+            if (this._isHovered && pointerInfo.event.button === 0) {
                 this.onClickedObservable.notifyObservers(this._camera);
             }
         }, PointerEventTypes.POINTERDOWN);

+ 13 - 0
src/Gizmos/gizmo.ts

@@ -28,6 +28,11 @@ export class Gizmo implements IDisposable {
     protected _scaleRatio = 1;
 
     /**
+     * boolean updated by pointermove when a gizmo mesh is hovered
+     */
+    protected _isHovered = false;
+
+    /**
      * Ratio for the scale of the gizmo (Default: 1)
      */
     public set scaleRatio(value: number) {
@@ -37,6 +42,14 @@ export class Gizmo implements IDisposable {
     public get scaleRatio() {
         return this._scaleRatio;
     }
+
+    /**
+     * True when the mouse pointer is hovered a gizmo mesh
+     */
+    public get isHovered() {
+        return this._isHovered;
+    }
+
     /**
      * If a custom mesh has been set (Default: false)
      */

+ 15 - 0
src/Gizmos/gizmoManager.ts

@@ -72,6 +72,21 @@ export class GizmoManager implements IDisposable {
     }
 
     /**
+     * True when the mouse pointer is hovering a gizmo mesh
+     */
+    public get isHovered() {
+        var hovered = false;
+        for (var key in this.gizmos) {
+            var gizmo = <Nullable<Gizmo>>((<any>this.gizmos)[key]);
+            if (gizmo && gizmo.isHovered) {
+                hovered = true;
+                break;
+            }
+        }
+        return hovered;
+    }
+
+    /**
      * Instatiates a gizmo manager
      * @param scene the scene to overlay the gizmos on top of
      * @param thickness display gizmo axis thickness

+ 2 - 2
src/Gizmos/lightGizmo.ts

@@ -53,8 +53,8 @@ export class LightGizmo extends Gizmo {
                 return;
             }
 
-            var isHovered = pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1);
-            if (isHovered && pointerInfo.event.button === 0) {
+            this._isHovered = !!(pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1));
+            if (this._isHovered && pointerInfo.event.button === 0) {
                 this.onClickedObservable.notifyObservers(this._light);
             }
         }, PointerEventTypes.POINTERDOWN);

+ 2 - 2
src/Gizmos/planeDragGizmo.ts

@@ -121,8 +121,8 @@ export class PlaneDragGizmo extends Gizmo {
             if (this._customMeshSet) {
                 return;
             }
-            var isHovered = pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1);
-            var material = isHovered ? this._hoverMaterial : this._coloredMaterial;
+            this._isHovered = !!(pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1));
+            var material = this._isHovered ? this._hoverMaterial : this._coloredMaterial;
             this._rootMesh.getChildMeshes().forEach((m) => {
                 m.material = material;
             });

+ 2 - 2
src/Gizmos/planeRotationGizmo.ts

@@ -181,8 +181,8 @@ export class PlaneRotationGizmo extends Gizmo {
             if (this._customMeshSet) {
                 return;
             }
-            var isHovered = pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1);
-            var material = isHovered ? hoverMaterial : coloredMaterial;
+            this._isHovered = !!(pointerInfo.pickInfo && (this._rootMesh.getChildMeshes().indexOf(<Mesh>pointerInfo.pickInfo.pickedMesh) != -1));
+            var material = this._isHovered ? hoverMaterial : coloredMaterial;
             this._rootMesh.getChildMeshes().forEach((m) => {
                 m.material = material;
                 if ((<LinesMesh>m).color) {

+ 12 - 0
src/Gizmos/positionGizmo.ts

@@ -87,6 +87,18 @@ export class PositionGizmo extends Gizmo {
             }
         });
     }
+
+    /**
+     * True when the mouse pointer is hovering a gizmo mesh
+     */
+    public get isHovered() {
+        var hovered = false;
+        [this.xGizmo, this.yGizmo, this.zGizmo, this.xPlaneGizmo, this.yPlaneGizmo, this.zPlaneGizmo].forEach((gizmo) => {
+            hovered = hovered || gizmo.isHovered;
+        });
+        return hovered;
+    }
+
     /**
      * Creates a PositionGizmo
      * @param gizmoLayer The utility layer the gizmo will be added to

+ 12 - 0
src/Gizmos/rotationGizmo.ts

@@ -65,6 +65,18 @@ export class RotationGizmo extends Gizmo {
             }
         });
     }
+
+    /**
+     * True when the mouse pointer is hovering a gizmo mesh
+     */
+    public get isHovered() {
+        var hovered = false;
+        [this.xGizmo, this.yGizmo, this.zGizmo].forEach((gizmo) => {
+            hovered = hovered || gizmo.isHovered;
+        });
+        return hovered;
+    }
+
     /**
      * Creates a RotationGizmo
      * @param gizmoLayer The utility layer the gizmo will be added to

+ 11 - 0
src/Gizmos/scaleGizmo.ts

@@ -77,6 +77,17 @@ export class ScaleGizmo extends Gizmo {
     }
 
     /**
+     * True when the mouse pointer is hovering a gizmo mesh
+     */
+    public get isHovered() {
+        var hovered = false;
+        [this.xGizmo, this.yGizmo, this.zGizmo].forEach((gizmo) => {
+            hovered = hovered || gizmo.isHovered;
+        });
+        return hovered;
+    }
+
+    /**
      * Creates a ScaleGizmo
      * @param gizmoLayer The utility layer the gizmo will be added to
      * @param thickness display gizmo axis thickness