Browse Source

Merge pull request #1318 from nockawa/master

Intersection/Interaction supported in Cached modes
Loïc Baumann 9 years ago
parent
commit
5f9e291be9

+ 6 - 0
src/Canvas2d/babylon.canvas2d.ts

@@ -1352,6 +1352,12 @@
                 else {
                 else {
                     let sprite = new Sprite2D(map, { parent: parent, id: `__cachedSpriteOfGroup__${group.id}`, x: group.actualPosition.x, y: group.actualPosition.y, spriteSize: node.contentSize, spriteLocation: node.pos });
                     let sprite = new Sprite2D(map, { parent: parent, id: `__cachedSpriteOfGroup__${group.id}`, x: group.actualPosition.x, y: group.actualPosition.y, spriteSize: node.contentSize, spriteLocation: node.pos });
                     sprite.origin = group.origin.clone();
                     sprite.origin = group.origin.clone();
+                    sprite.addExternalData("__cachedGroup__", group);
+                    sprite.pointerEventObservable.add((e, s) => {
+                        if (group.pointerEventObservable !== null) {
+                            group.pointerEventObservable.notifyObservers(e, s.mask);
+                        }
+                    });
                     res.sprite = sprite;
                     res.sprite = sprite;
                 }
                 }
             }
             }

+ 14 - 7
src/Canvas2d/babylon.group2d.ts

@@ -119,7 +119,7 @@
         }
         }
 
 
         static _createCachedCanvasGroup(owner: Canvas2D): Group2D {
         static _createCachedCanvasGroup(owner: Canvas2D): Group2D {
-            var g = new Group2D({ parent: owner, id: "__cachedCanvasGroup__", position: Vector2.Zero(), origin: Vector2.Zero(), size: null, isVisible: true });
+            var g = new Group2D({ parent: owner, id: "__cachedCanvasGroup__", position: Vector2.Zero(), origin: Vector2.Zero(), size: null, isVisible: true, isPickable: false });
             return g;
             return g;
 
 
         }
         }
@@ -837,23 +837,30 @@
         protected handleGroupChanged(prop: Prim2DPropInfo) {
         protected handleGroupChanged(prop: Prim2DPropInfo) {
             // This method is only for cachedGroup
             // This method is only for cachedGroup
             let rd = this._renderableData;
             let rd = this._renderableData;
+            if (!rd) {
+                return;
+            }
 
 
-            if (!this.isCachedGroup || !rd._cacheRenderSprite) {
+            let cachedSprite = rd._cacheRenderSprite;
+            if (!this.isCachedGroup || !cachedSprite) {
                 return;
                 return;
             }
             }
 
 
             // For now we only support these property changes
             // For now we only support these property changes
             // TODO: add more! :)
             // TODO: add more! :)
             if (prop.id === Prim2DBase.actualPositionProperty.id) {
             if (prop.id === Prim2DBase.actualPositionProperty.id) {
-                rd._cacheRenderSprite.actualPosition = this.actualPosition.clone();
+                cachedSprite.actualPosition = this.actualPosition.clone();
+                if (cachedSprite.position != null) {
+                    cachedSprite.position = cachedSprite.actualPosition.clone();
+                }
             } else if (prop.id === Prim2DBase.rotationProperty.id) {
             } else if (prop.id === Prim2DBase.rotationProperty.id) {
-                rd._cacheRenderSprite.rotation = this.rotation;
+                cachedSprite.rotation = this.rotation;
             } else if (prop.id === Prim2DBase.scaleProperty.id) {
             } else if (prop.id === Prim2DBase.scaleProperty.id) {
-                rd._cacheRenderSprite.scale = this.scale;
+                cachedSprite.scale = this.scale;
             } else if (prop.id === Prim2DBase.originProperty.id) {
             } else if (prop.id === Prim2DBase.originProperty.id) {
-                rd._cacheRenderSprite.origin = this.origin.clone();
+                cachedSprite.origin = this.origin.clone();
             } else if (prop.id === Group2D.actualSizeProperty.id) {
             } else if (prop.id === Group2D.actualSizeProperty.id) {
-                rd._cacheRenderSprite.size = this.actualSize.clone();
+                cachedSprite.size = this.actualSize.clone();
                 //console.log(`[${this._globalTransformProcessStep}] Sync Sprite ${this.id}, width: ${this.actualSize.width}, height: ${this.actualSize.height}`);
                 //console.log(`[${this._globalTransformProcessStep}] Sync Sprite ${this.id}, width: ${this.actualSize.width}, height: ${this.actualSize.height}`);
             }
             }
         }
         }

+ 20 - 0
src/Canvas2d/babylon.prim2dBase.ts

@@ -2237,6 +2237,7 @@
          * Get the global transformation matrix of the primitive
          * Get the global transformation matrix of the primitive
          */
          */
         public get globalTransform(): Matrix {
         public get globalTransform(): Matrix {
+            this._updateLocalTransform();
             return this._globalTransform;
             return this._globalTransform;
         }
         }
 
 
@@ -2262,6 +2263,7 @@
          * Get invert of the global transformation matrix of the primitive
          * Get invert of the global transformation matrix of the primitive
          */
          */
         public get invGlobalTransform(): Matrix {
         public get invGlobalTransform(): Matrix {
+            this._updateLocalTransform();
             return this._invGlobalTransform;
             return this._invGlobalTransform;
         }
         }
 
 
@@ -2409,6 +2411,24 @@
                 return false;
                 return false;
             }
             }
 
 
+            let id = this.id;
+            if (id!=null && id.indexOf("__cachedSpriteOfGroup__") === 0) {
+                let ownerGroup = this.getExternalData<Group2D>("__cachedGroup__");
+                return ownerGroup.intersect(intersectInfo);
+            }
+
+            // If we're testing a cachedGroup, we must reject pointer outside its levelBoundingInfo because children primitives could be partially clipped outside so we must not accept them as intersected when it's the case (because they're not visually visible).
+            let isIntersectionTest = false;
+            if (this instanceof Group2D) {
+                let g = <Group2D><any>this;
+                isIntersectionTest = g.isCachedGroup;
+            }
+            if (isIntersectionTest && !this.levelBoundingInfo.doesIntersect(intersectInfo._localPickPosition)) {
+                // Important to call this before each return to allow a good recursion next time this intersectInfo is reused
+                intersectInfo._exit(firstLevel);
+                return false;
+            }
+
             // Fast rejection test with boundingInfo
             // Fast rejection test with boundingInfo
             if (this.isPickable && !this.boundingInfo.doesIntersect(intersectInfo._localPickPosition)) {
             if (this.isPickable && !this.boundingInfo.doesIntersect(intersectInfo._localPickPosition)) {
                 // Important to call this before each return to allow a good recursion next time this intersectInfo is reused
                 // Important to call this before each return to allow a good recursion next time this intersectInfo is reused

+ 1 - 1
src/Canvas2d/babylon.renderablePrim2d.ts

@@ -434,7 +434,7 @@
                     usedCount += td._partData.usedElementCount;
                     usedCount += td._partData.usedElementCount;
                 }
                 }
 
 
-                if (usedCount === 0) {
+                if (usedCount === 0 && gii.modelRenderCache!=null) {
                     this.renderGroup._renderableData._renderGroupInstancesInfo.remove(gii.modelRenderCache.modelKey);
                     this.renderGroup._renderableData._renderGroupInstancesInfo.remove(gii.modelRenderCache.modelKey);
                     gii.dispose();
                     gii.dispose();
                 }
                 }