avocado-old/packages/timing/animation-view.js

64 lines
1.1 KiB
JavaScript
Raw Normal View History

import {
hasGraphics,
Container,
Image,
Renderable,
Sprite,
} from '@avocado/graphics';
2019-03-19 18:05:58 -05:00
export class AnimationView extends Renderable {
constructor(animation) {
super();
this.animation = animation;
this.container = new Container();
if (hasGraphics) {
2020-06-21 11:22:10 -05:00
if (this.animation.imageUri) {
Image.load(this.animation.imageUri).then((image) => {
this.sprite = new Sprite(image);
this.container.addChild(this.sprite);
this.resetSourceRectangle();
});
}
2019-03-19 18:05:58 -05:00
}
2019-03-21 00:09:17 -05:00
animation.on(
['directionChanged', 'indexChanged'],
2019-04-12 18:58:38 -05:00
this.onAnimationChanged,
this
2019-03-21 00:09:17 -05:00
);
2019-03-19 18:05:58 -05:00
}
destroy() {
2019-03-21 01:32:39 -05:00
this.animation.off(
2019-03-21 00:09:17 -05:00
['directionChanged', 'indexChanged'],
this.onAnimationChanged
);
2019-03-19 18:05:58 -05:00
if (this.sprite) {
this.sprite.destroy();
}
}
get internal() {
return this.container.internal;
}
2019-03-21 00:09:17 -05:00
onAnimationChanged() {
if (this.sprite) {
this.resetSourceRectangle();
}
}
2019-03-19 18:05:58 -05:00
reset() {
this.animation.reset();
}
resetSourceRectangle() {
this.sprite.sourceRectangle = this.animation.sourceRectangle;
}
tick() {
this.animation.tick();
}
}