浏览代码

Canvas2D: fix pointerEventObservable enter/leave/over/out

 - Some event was fired twice.
 - Event wasn't fire for leave/out when the pointer was out of the Canvas2D
 - overPrim property changed is now called the proper amount of time
nockawa 8 年之前
父节点
当前提交
f8c4747bc3
共有 1 个文件被更改,包括 29 次插入24 次删除
  1. 29 24
      canvas2D/src/Engine/babylon.canvas2d.ts

+ 29 - 24
canvas2D/src/Engine/babylon.canvas2d.ts

@@ -550,22 +550,26 @@
 
             // Fast rejection: test if the mouse pointer is outside the canvas's bounding Info
             if (!isCapture && !this.levelBoundingInfo.doesIntersect(ii.pickPosition)) {
-                this._previousIntersectionList = this._actualIntersectionList;
-                this._actualIntersectionList = null;
-                this._previousOverPrimitive = this._actualOverPrimitive;
-                this._actualOverPrimitive = null;
-                return;
+                // Reset intersection info as we don't hit anything
+                ii.intersectedPrimitives = new Array<PrimitiveIntersectedInfo>();
+                ii.topMostIntersectedPrimitive = null;
+            } else {
+                // The pointer is inside the Canvas, do an intersection test
+                this.intersect(ii);
             }
 
-            this.intersect(ii);
-
-            this._previousIntersectionList = this._actualIntersectionList;
-            this._actualIntersectionList = ii.intersectedPrimitives;
-            this._previousOverPrimitive = this._actualOverPrimitive;
-            this._actualOverPrimitive = ii.topMostIntersectedPrimitive;
+            {
+                // Update prev/actual intersection info, fire "overPrim" property change if needed
+                this._previousIntersectionList = this._actualIntersectionList;
+                this._actualIntersectionList = ii.intersectedPrimitives;
+                this._previousOverPrimitive = this._actualOverPrimitive;
+                this._actualOverPrimitive = ii.topMostIntersectedPrimitive;
 
-            if ((!this._actualOverPrimitive && !this._previousOverPrimitive) || !(this._actualOverPrimitive && this._previousOverPrimitive && this._actualOverPrimitive.prim === this._previousOverPrimitive.prim)) {
-                this.onPropertyChanged("overPrim", this._previousOverPrimitive ? this._previousOverPrimitive.prim : null, this._actualOverPrimitive ? this._actualOverPrimitive.prim : null);
+                let prev = (this._previousOverPrimitive != null) ? this._previousOverPrimitive.prim : null;
+                let actual = (this._actualOverPrimitive != null) ? this._actualOverPrimitive.prim : null;
+                if (prev !== actual) {
+                    this.onPropertyChanged("overPrim", this._previousOverPrimitive ? this._previousOverPrimitive.prim : null, this._actualOverPrimitive ? this._actualOverPrimitive.prim : null);
+                }
             }
 
             this._intersectionRenderId = this.scene.getRenderId();
@@ -672,17 +676,18 @@
                     this._updatePrimPointerPos(cur);
                 }
 
-                // Trigger a PointerEnter corresponding to the PointerOver
-                if (mask === PrimitivePointerInfo.PointerOver) {
-                    this._debugExecObserver(cur, PrimitivePointerInfo.PointerEnter);
-                    cur._pointerEventObservable.notifyObservers(ppi, PrimitivePointerInfo.PointerEnter);
-                }
-
-                // Trigger a PointerLeave corresponding to the PointerOut
-                else if (mask === PrimitivePointerInfo.PointerOut) {
-                    this._debugExecObserver(cur, PrimitivePointerInfo.PointerLeave);
-                    cur._pointerEventObservable.notifyObservers(ppi, PrimitivePointerInfo.PointerLeave);
-                }
+                // NOTE TO MYSELF, this is commented right now because it doesn't seemed needed but I can't figure out why I put this code in the first place
+                //// Trigger a PointerEnter corresponding to the PointerOver
+                //if (mask === PrimitivePointerInfo.PointerOver) {
+                //    this._debugExecObserver(cur, PrimitivePointerInfo.PointerEnter);
+                //    cur._pointerEventObservable.notifyObservers(ppi, PrimitivePointerInfo.PointerEnter);
+                //}
+
+                //// Trigger a PointerLeave corresponding to the PointerOut
+                //else if (mask === PrimitivePointerInfo.PointerOut) {
+                //    this._debugExecObserver(cur, PrimitivePointerInfo.PointerLeave);
+                //    cur._pointerEventObservable.notifyObservers(ppi, PrimitivePointerInfo.PointerLeave);
+                //}
 
                 // Loop to the parent
                 cur = cur.parent;