Skip to content

Commit

Permalink
Fix ObservablePoint binding using newly created inner class Live2dMod…
Browse files Browse the repository at this point in the history
…elObserver
  • Loading branch information
dannylin0711 committed Apr 29, 2024
1 parent ae9bad9 commit 81e57a1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Live2DModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { InternalModel, ModelSettings, MotionPriority } from "@/cubism-comm
import type { MotionManagerOptions } from "@/cubism-common/MotionManager";
import type { Live2DFactoryOptions } from "@/factory/Live2DFactory";
import { Live2DFactory } from "@/factory/Live2DFactory";
import type { Rectangle, Renderer, Texture, Ticker } from "pixi.js";
import type { Rectangle, Renderer, Texture, Ticker, Observer } from "pixi.js";
import { Matrix, ObservablePoint, Point } from "pixi.js";
import { Container } from "pixi.js";
import { Automator, type AutomatorOptions } from "./Automator";
Expand All @@ -27,6 +27,16 @@ export type Live2DConstructor = { new (options?: Live2DModelOptions): Live2DMode
* @emits {@link Live2DModelEvents}
*/
export class Live2DModel<IM extends InternalModel = InternalModel> extends Container {
Live2DModelObserver = class implements Observer<ObservablePoint> {
parent: Live2DModel;
_onUpdate: (point?: ObservablePoint | undefined) => void;

constructor(parent: Live2DModel) {
this.parent = parent;
this._onUpdate = parent.onAnchorChange.bind(parent);
}
}

/**
* Creates a Live2DModel from given source.
* @param source - Can be one of: settings file URL, settings JSON object, ModelSettings instance.
Expand Down Expand Up @@ -107,7 +117,8 @@ export class Live2DModel<IM extends InternalModel = InternalModel> extends Conta
* The anchor behaves like the one in `PIXI.Sprite`, where `(0, 0)` means the top left
* and `(1, 1)` means the bottom right.
*/
anchor = new ObservablePoint(this.onAnchorChange, this, 0, 0) as ObservablePoint<any>; // cast the type because it breaks the casting of Live2DModel

anchor = new ObservablePoint(new this.Live2DModelObserver(this), 0, 0) as ObservablePoint; // cast the type because it breaks the casting of Live2DModel

/**
* An ID of Gl context that syncs with `renderer.CONTEXT_UID`. Used to check if the GL context has changed.
Expand Down

0 comments on commit 81e57a1

Please sign in to comment.