diff --git a/spine-ts/index.html b/spine-ts/index.html index 2dfa3567e..8ac653b04 100644 --- a/spine-ts/index.html +++ b/spine-ts/index.html @@ -72,6 +72,9 @@
  • Bounds test - (v3)
  • +
  • + Move origin - (v3) +
  • Visibility test - (v3)
  • diff --git a/spine-ts/spine-phaser-v3/example/block.png b/spine-ts/spine-phaser-v3/example/block.png new file mode 100644 index 000000000..3d3ffd206 Binary files /dev/null and b/spine-ts/spine-phaser-v3/example/block.png differ diff --git a/spine-ts/spine-phaser-v3/example/move-origin.html b/spine-ts/spine-phaser-v3/example/move-origin.html new file mode 100644 index 000000000..022cf31b6 --- /dev/null +++ b/spine-ts/spine-phaser-v3/example/move-origin.html @@ -0,0 +1,147 @@ + + + + + + + + + + Spine Phaser Example + + + +

    Move origin

    +

    Demonstrate moving origin of Spine Gameobject, behaves like a sprite

    + + + diff --git a/spine-ts/spine-phaser-v3/src/SpineGameObject.ts b/spine-ts/spine-phaser-v3/src/SpineGameObject.ts index 843796917..75433a7c5 100644 --- a/spine-ts/spine-phaser-v3/src/SpineGameObject.ts +++ b/spine-ts/spine-phaser-v3/src/SpineGameObject.ts @@ -68,6 +68,19 @@ export interface SpineGameObjectBoundsProvider { }; } +/** A bounds provider that provides a fixed size given by the user. */ +export class AABBRectangleBoundsProvider implements SpineGameObjectBoundsProvider { + constructor ( + private x: number, + private y: number, + private width: number, + private height: number, + ) { } + calculateBounds () { + return { x: this.x, y: this.y, width: this.width, height: this.height }; + } +} + /** A bounds provider that calculates the bounding box from the setup pose. */ export class SetupPoseBoundsProvider implements SpineGameObjectBoundsProvider { /** @@ -215,6 +228,8 @@ export class SpineGameObject extends DepthMixin( beforeUpdateWorldTransforms: (object: SpineGameObject) => void = () => { }; afterUpdateWorldTransforms: (object: SpineGameObject) => void = () => { }; private premultipliedAlpha = false; + private offsetX = 0; + private offsetY = 0; constructor ( scene: Phaser.Scene, @@ -239,13 +254,11 @@ export class SpineGameObject extends DepthMixin( updateSize () { if (!this.skeleton) return; let bounds = this.boundsProvider.calculateBounds(this); - // For some reason the TS compiler and the ComputedSize mixin don't work well together and we have - // to cast to any. - let self = this as any; - self.width = bounds.width; - self.height = bounds.height; - this.displayOriginX = -bounds.x; - this.displayOriginY = -bounds.y; + this.width = bounds.width; + this.height = bounds.height; + this.setDisplayOrigin(-bounds.x, -bounds.y); + this.offsetX = -bounds.x; + this.offsetY = -bounds.y; } /** Converts a point from the skeleton coordinate system to the Phaser world coordinate system. */ @@ -355,17 +368,21 @@ export class SpineGameObject extends DepthMixin( d = transform.d, tx = transform.tx, ty = transform.ty; + + let offsetX = (src.offsetX - src.displayOriginX) * src.scaleX; + let offsetY = (src.offsetY - src.displayOriginY) * src.scaleY; + sceneRenderer.drawSkeleton( - this.skeleton, - this.premultipliedAlpha, + src.skeleton, + src.premultipliedAlpha, -1, -1, (vertices, numVertices, stride) => { for (let i = 0; i < numVertices; i += stride) { let vx = vertices[i]; let vy = vertices[i + 1]; - vertices[i] = vx * a + vy * c + tx; - vertices[i + 1] = vx * b + vy * d + ty; + vertices[i] = vx * a + vy * c + tx + offsetX; + vertices[i + 1] = vx * b + vy * d + ty + offsetY; } } ); diff --git a/spine-ts/spine-phaser-v4/example/block.png b/spine-ts/spine-phaser-v4/example/block.png new file mode 100644 index 000000000..3d3ffd206 Binary files /dev/null and b/spine-ts/spine-phaser-v4/example/block.png differ diff --git a/spine-ts/spine-phaser-v4/example/move-origin.html b/spine-ts/spine-phaser-v4/example/move-origin.html new file mode 100644 index 000000000..ac5a7199c --- /dev/null +++ b/spine-ts/spine-phaser-v4/example/move-origin.html @@ -0,0 +1,147 @@ + + + + + + + + + + Spine Phaser Example + + + +

    Move origin

    +

    Demonstrate moving origin of Spine Gameobject, behaves like a sprite

    + + + diff --git a/spine-ts/spine-phaser-v4/src/SpineGameObject.ts b/spine-ts/spine-phaser-v4/src/SpineGameObject.ts index 1c117bac1..adffcf87c 100644 --- a/spine-ts/spine-phaser-v4/src/SpineGameObject.ts +++ b/spine-ts/spine-phaser-v4/src/SpineGameObject.ts @@ -68,6 +68,19 @@ export interface SpineGameObjectBoundsProvider { }; } +/** A bounds provider that provides a fixed size given by the user. */ +export class AABBRectangleBoundsProvider implements SpineGameObjectBoundsProvider { + constructor ( + private x: number, + private y: number, + private width: number, + private height: number, + ) { } + calculateBounds () { + return { x: this.x, y: this.y, width: this.width, height: this.height }; + } +} + /** A bounds provider that calculates the bounding box from the setup pose. */ export class SetupPoseBoundsProvider implements SpineGameObjectBoundsProvider { /** @@ -215,6 +228,8 @@ export class SpineGameObject extends DepthMixin( beforeUpdateWorldTransforms: (object: SpineGameObject) => void = () => { }; afterUpdateWorldTransforms: (object: SpineGameObject) => void = () => { }; private premultipliedAlpha = false; + private offsetX = 0; + private offsetY = 0; constructor ( scene: Phaser.Scene, @@ -239,13 +254,11 @@ export class SpineGameObject extends DepthMixin( updateSize () { if (!this.skeleton) return; let bounds = this.boundsProvider.calculateBounds(this); - // For some reason the TS compiler and the ComputedSize mixin don't work well together and we have - // to cast to any. - let self = this as any; - self.width = bounds.width; - self.height = bounds.height; - this.displayOriginX = -bounds.x; - this.displayOriginY = -bounds.y; + this.width = bounds.width; + this.height = bounds.height; + this.setDisplayOrigin(-bounds.x, -bounds.y); + this.offsetX = -bounds.x; + this.offsetY = -bounds.y; } /** Converts a point from the skeleton coordinate system to the Phaser world coordinate system. */ @@ -374,6 +387,10 @@ export class SpineGameObject extends DepthMixin( d = transform.d, tx = transform.tx, ty = transform.ty; + + let offsetX = (src.offsetX - src.displayOriginX) * src.scaleX; + let offsetY = (src.offsetY - src.displayOriginY) * src.scaleY; + sceneRenderer.drawSkeleton( src.skeleton, src.premultipliedAlpha, @@ -383,8 +400,8 @@ export class SpineGameObject extends DepthMixin( for (let i = 0; i < numVertices; i += stride) { let vx = vertices[i]; let vy = vertices[i + 1]; - vertices[i] = vx * a + vy * c + tx; - vertices[i + 1] = vx * b + vy * d + ty; + vertices[i] = vx * a + vy * c + tx + offsetX; + vertices[i + 1] = vx * b + vy * d + ty + offsetY; } } );