62 lines
1.1 KiB
JavaScript
62 lines
1.1 KiB
JavaScript
import {
|
|
hasGraphics,
|
|
Container,
|
|
Image,
|
|
Renderable,
|
|
Sprite,
|
|
} from '@avocado/graphics';
|
|
|
|
export class AnimationView extends Renderable {
|
|
|
|
constructor(animation) {
|
|
super();
|
|
this.animation = animation;
|
|
this.container = new Container();
|
|
if (hasGraphics) {
|
|
Image.load(this.animation.imageUri).then((image) => {
|
|
this.sprite = new Sprite(image);
|
|
this.container.addChild(this.sprite);
|
|
this.resetSourceRectangle();
|
|
});
|
|
}
|
|
animation.on(
|
|
['directionChanged', 'indexChanged'],
|
|
this.onAnimationChanged,
|
|
this
|
|
);
|
|
}
|
|
|
|
destroy() {
|
|
this.animation.off(
|
|
['directionChanged', 'indexChanged'],
|
|
this.onAnimationChanged
|
|
);
|
|
if (this.sprite) {
|
|
this.sprite.destroy();
|
|
}
|
|
}
|
|
|
|
get internal() {
|
|
return this.container.internal;
|
|
}
|
|
|
|
onAnimationChanged() {
|
|
if (this.sprite) {
|
|
this.resetSourceRectangle();
|
|
}
|
|
}
|
|
|
|
reset() {
|
|
this.animation.reset();
|
|
}
|
|
|
|
resetSourceRectangle() {
|
|
this.sprite.sourceRectangle = this.animation.sourceRectangle;
|
|
}
|
|
|
|
tick() {
|
|
this.animation.tick();
|
|
}
|
|
|
|
}
|