Skip to content

Commit

Permalink
[phaser] Add Origin mixin, don't call updateSize() on changes to scale.
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Jul 12, 2023
1 parent f759199 commit cb0f78b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 41 deletions.
45 changes: 4 additions & 41 deletions spine-ts/spine-phaser/src/SpineGameObject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SPINE_GAME_OBJECT_TYPE } from "./keys";
import { SpinePlugin } from "./SpinePlugin";
import { ComputedSizeMixin, DepthMixin, FlipMixin, ScrollFactorMixin, TransformMixin, VisibleMixin, AlphaMixin } from "./mixins";
import { ComputedSizeMixin, DepthMixin, FlipMixin, ScrollFactorMixin, TransformMixin, VisibleMixin, AlphaMixin, OriginMixin } from "./mixins";
import { AnimationState, AnimationStateData, Bone, MathUtils, Skeleton, Skin, Vector2 } from "@esotericsoftware/spine-core";

class BaseSpineGameObject extends Phaser.GameObjects.GameObject {
Expand Down Expand Up @@ -107,18 +107,14 @@ export class SkinsAndAnimationBoundsProvider implements SpineGameObjectBoundsPro
*
* See {@link skeletonToPhaserWorldCoordinates}, {@link phaserWorldCoordinatesToSkeleton}, and {@link phaserWorldCoordinatesToBoneLocal.}
*/
export class SpineGameObject extends ComputedSizeMixin(DepthMixin(FlipMixin(ScrollFactorMixin(TransformMixin(VisibleMixin(AlphaMixin(BaseSpineGameObject))))))) {
export class SpineGameObject extends DepthMixin(OriginMixin(ComputedSizeMixin(FlipMixin(ScrollFactorMixin(TransformMixin(VisibleMixin(AlphaMixin(BaseSpineGameObject)))))))) {
blendMode = -1;
skeleton: Skeleton;
animationStateData: AnimationStateData;
animationState: AnimationState;
beforeUpdateWorldTransforms: (object: SpineGameObject) => void = () => { };
afterUpdateWorldTransforms: (object: SpineGameObject) => void = () => { };
private premultipliedAlpha = false;
private _displayOriginX = 0;
private _displayOriginY = 0;
private _scaleX = 1;
private _scaleY = 1;

constructor (scene: Phaser.Scene, private plugin: SpinePlugin, x: number, y: number, dataKey: string, atlasKey: string, public boundsProvider: SpineGameObjectBoundsProvider = new SetupPoseBoundsProvider()) {
super(scene, SPINE_GAME_OBJECT_TYPE);
Expand All @@ -132,44 +128,11 @@ export class SpineGameObject extends ComputedSizeMixin(DepthMixin(FlipMixin(Scro
this.updateSize();
}

public get displayOriginX () {
return this._displayOriginX;
}

public set displayOriginX (value: number) {
this._displayOriginX = value;
}

public get displayOriginY () {
return this._displayOriginY;
}

public set displayOriginY (value: number) {
this._displayOriginY = value;
}

public get scaleX () {
return this._scaleX;
}

public set scaleX (value: number) {
this._scaleX = value;
this.updateSize();
}

public get scaleY () {
return this._scaleY;
}

public set scaleY (value: number) {
this._scaleY = value;
this.updateSize();
}

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...
// 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;
Expand Down
4 changes: 4 additions & 0 deletions spine-ts/spine-phaser/src/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const Flip = components.Flip;
export const ScrollFactor = components.ScrollFactor;
export const Transform = components.Transform;
export const Visible = components.Visible;
export const Origin = components.Origin;
export const Alpha = components.Alpha;

export interface Type<
Expand Down Expand Up @@ -76,5 +77,8 @@ export const TransformMixin: TransformMixin = createMixin<Phaser.GameObjects.Com
type VisibleMixin = Mixin<Phaser.GameObjects.Components.Visible, Phaser.GameObjects.GameObject>;
export const VisibleMixin: VisibleMixin = createMixin<Phaser.GameObjects.Components.Visible>(Visible);

type OriginMixin = Mixin<Phaser.GameObjects.Components.Origin, Phaser.GameObjects.GameObject>;
export const OriginMixin: OriginMixin = createMixin<Phaser.GameObjects.Components.Origin>(Origin);

type AlphaMixin = Mixin<Phaser.GameObjects.Components.Alpha, Phaser.GameObjects.GameObject>;
export const AlphaMixin: AlphaMixin = createMixin<Phaser.GameObjects.Components.Alpha>(Alpha);

0 comments on commit cb0f78b

Please sign in to comment.