Skip to content

Commit 5a00f2c

Browse files
committed
Merge pull request BabylonJS#1146 from nockawa/engine2d
Canvas2D: Bug fixing time!
2 parents 0341116 + 31eee99 commit 5a00f2c

File tree

4 files changed

+35
-38
lines changed

4 files changed

+35
-38
lines changed

src/Canvas2d/babylon.canvas2d.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,15 @@
123123
}
124124
this.__engineData = engine.getOrAddExternalDataWithFactory("__BJSCANVAS2D__", k => new Canvas2DEngineBoundData());
125125
this._cachingStrategy = cachingstrategy;
126-
this._depthLevel = 0;
127-
this._hierarchyMaxDepth = 100;
128-
this._hierarchyLevelZFactor = 1 / this._hierarchyMaxDepth;
129-
this._hierarchyLevelMaxSiblingCount = 1000;
130-
this._hierarchySiblingZDelta = this._hierarchyLevelZFactor / this._hierarchyLevelMaxSiblingCount;
131126
this._primPointerInfo = new PrimitivePointerInfo();
132127
this._capturedPointers = new StringDictionary<Prim2DBase>();
133128
this._pickStartingPosition = Vector2.Zero();
134129

135130
this.setupGroup2D(this, null, name, Vector2.Zero(), size, this._cachingStrategy===Canvas2D.CACHESTRATEGY_ALLGROUPS ? Group2D.GROUPCACHEBEHAVIOR_DONTCACHEOVERRIDE : Group2D.GROUPCACHEBEHAVIOR_FOLLOWCACHESTRATEGY);
136131

132+
this._hierarchyLevelMaxSiblingCount = 100;
133+
this._hierarchyDepthOffset = 0;
134+
this._siblingDepthOffset = 1 / this._hierarchyLevelMaxSiblingCount;
137135
this._scene = scene;
138136
this._engine = engine;
139137
this._renderingSize = new Size(0, 0);
@@ -168,6 +166,10 @@
168166
this._setupInteraction(enableInteraction);
169167
}
170168

169+
public get hierarchyLevelMaxSiblingCount(): number {
170+
return this._hierarchyLevelMaxSiblingCount;
171+
}
172+
171173
private _setupInteraction(enable: boolean) {
172174
// No change detection
173175
if (enable === this._interactionEnabled) {
@@ -509,12 +511,11 @@
509511
ii.findFirstOnly = false;
510512
this.intersect(ii);
511513

512-
if (ii.isIntersected) {
513-
let iprim = ii.topMostIntersectedPrimitive.prim;
514-
if (iprim.actionManager) {
514+
if (ii.isPrimIntersected(prim) !== null) {
515+
if (prim.actionManager) {
515516
if (this._pickStartingTime !== 0 && ((new Date().getTime() - this._pickStartingTime) > ActionManager.LongPressDelay) && (Math.abs(this._pickStartingPosition.x - ii.pickPosition.x) < ActionManager.DragMovementThreshold && Math.abs(this._pickStartingPosition.y - ii.pickPosition.y) < ActionManager.DragMovementThreshold)) {
516517
this._pickStartingTime = 0;
517-
iprim.actionManager.processTrigger(ActionManager.OnLongPressTrigger, ActionEvent.CreateNewFromPrimitive(prim, ppi.primitivePointerPos, eventData));
518+
prim.actionManager.processTrigger(ActionManager.OnLongPressTrigger, ActionEvent.CreateNewFromPrimitive(prim, ppi.primitivePointerPos, eventData));
518519
}
519520
}
520521
}
@@ -740,23 +741,6 @@
740741
}
741742
}
742743

743-
/**
744-
* Read-only property that return the Z delta to apply for each sibling primitives inside of a given one.
745-
* Sibling Primitives are defined in a specific order, the first ones will be draw below the next ones.
746-
* This property define the Z value to apply between each sibling Primitive. Current implementation allows 1000 Siblings Primitives per level.
747-
* @returns The Z Delta
748-
*/
749-
public get hierarchySiblingZDelta(): number {
750-
return this._hierarchySiblingZDelta;
751-
}
752-
753-
/**
754-
* Return the Z Factor that will be applied for each new hierarchy level.
755-
* @returns The Z Factor
756-
*/
757-
public get hierarchyLevelZFactor(): number {
758-
return this._hierarchyLevelZFactor;
759-
}
760744

761745
private __engineData: Canvas2DEngineBoundData;
762746
private _interactionEnabled: boolean;
@@ -782,10 +766,7 @@
782766
private _isScreeSpace: boolean;
783767
private _cachedCanvasGroup: Group2D;
784768
private _cachingStrategy: number;
785-
private _hierarchyMaxDepth: number;
786-
private _hierarchyLevelZFactor: number;
787769
private _hierarchyLevelMaxSiblingCount: number;
788-
private _hierarchySiblingZDelta: number;
789770
private _groupCacheMaps: MapTexture[];
790771
private _beforeRenderObserver: Observer<Scene>;
791772
private _afterRenderObserver: Observer<Scene>;

src/Canvas2d/babylon.prim2dBase.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@
272272
return this.intersectedPrimitives && this.intersectedPrimitives.length > 0;
273273
}
274274

275+
public isPrimIntersected(prim: Prim2DBase): Vector2 {
276+
for (let cur of this.intersectedPrimitives) {
277+
if (cur.prim === prim) {
278+
return cur.intersectionLocation;
279+
}
280+
}
281+
return null;
282+
}
283+
275284
// Internals, don't use
276285
public _exit(firstLevel: boolean) {
277286
if (firstLevel) {
@@ -709,11 +718,9 @@
709718
}
710719

711720
private addChild(child: Prim2DBase) {
712-
child._siblingDepthOffset = (this._children.length + 1) * this.owner.hierarchySiblingZDelta;
713-
child._depthLevel = this._depthLevel + 1;
714-
child._hierarchyDepthOffset = child._depthLevel * this.owner.hierarchyLevelZFactor;
721+
child._hierarchyDepthOffset = this._hierarchyDepthOffset + ((this._children.length + 1) * this._siblingDepthOffset);
722+
child._siblingDepthOffset = this._siblingDepthOffset / this.owner.hierarchyLevelMaxSiblingCount;
715723
this._children.push(child);
716-
717724
}
718725

719726
public dispose(): boolean {
@@ -746,7 +753,7 @@
746753
}
747754

748755
public getActualZOffset(): number {
749-
return this._zOrder || 1 - (this._siblingDepthOffset + this._hierarchyDepthOffset);
756+
return this._zOrder || (1 - this._hierarchyDepthOffset);
750757
}
751758

752759
protected onPrimBecomesDirty() {
@@ -756,7 +763,7 @@
756763
}
757764

758765
public _needPrepare(): boolean {
759-
return this._visibilityChanged && (this._modelDirty || (this._instanceDirtyFlags !== 0) || (this._globalTransformProcessStep !== this._globalTransformStep));
766+
return this._visibilityChanged || this._modelDirty || (this._instanceDirtyFlags !== 0) || (this._globalTransformProcessStep !== this._globalTransformStep);
760767
}
761768

762769
public _prepareRender(context: Render2DContext) {
@@ -875,9 +882,8 @@
875882
protected _children: Array<Prim2DBase>;
876883
private _renderGroup: Group2D;
877884
private _hierarchyDepth: number;
878-
protected _depthLevel: number;
879-
private _hierarchyDepthOffset: number;
880-
private _siblingDepthOffset: number;
885+
protected _hierarchyDepthOffset: number;
886+
protected _siblingDepthOffset: number;
881887
private _zOrder: number;
882888
private _levelVisible: boolean;
883889
public _pointerEventObservable: Observable<PrimitivePointerInfo>;

src/Canvas2d/babylon.sprite2d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@
174174
return res;
175175
}
176176

177+
protected levelIntersect(intersectInfo: IntersectInfo2D): boolean {
178+
// If we've made it so far it means the boundingInfo intersection test succeed, the Sprite2D is shaped the same, so we always return true
179+
return true;
180+
}
181+
177182
protected setupSprite2D(owner: Canvas2D, parent: Prim2DBase, id: string, position: Vector2, texture: Texture, spriteSize: Size, spriteLocation: Vector2, invertY: boolean) {
178183
this.setupRenderablePrim2D(owner, parent, id, position, true);
179184
this.texture = texture;

src/Canvas2d/babylon.text2d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@
237237
return text2d;
238238
}
239239

240+
protected levelIntersect(intersectInfo: IntersectInfo2D): boolean {
241+
// For now I can't do something better that boundingInfo is a hit, detecting an intersection on a particular letter would be possible, but do we really need it? Not for now...
242+
return true;
243+
}
244+
240245
protected createModelRenderCache(modelKey: string, isTransparent: boolean): ModelRenderCache {
241246
let renderCache = new Text2DRenderCache(this.owner.engine, modelKey, isTransparent);
242247
return renderCache;

0 commit comments

Comments
 (0)