2019-03-19 18:05:58 -05:00
|
|
|
import {hasGraphics} from './has-graphics';
|
|
|
|
import {Container} from './container';
|
|
|
|
import {Image} from './image';
|
|
|
|
import {Renderable} from './renderable';
|
|
|
|
import {Sprite} from './sprite';
|
|
|
|
|
|
|
|
export class AnimationView extends Renderable {
|
|
|
|
|
|
|
|
constructor(animation) {
|
|
|
|
super();
|
|
|
|
this.animation = animation;
|
|
|
|
this.container = new Container();
|
2019-03-21 00:09:17 -05:00
|
|
|
this.onAnimationChanged = this.onAnimationChanged.bind(this);
|
2019-03-19 18:05:58 -05:00
|
|
|
if (hasGraphics) {
|
|
|
|
Image.load(this.animation.imageUri).then((image) => {
|
|
|
|
this.sprite = new Sprite(image);
|
|
|
|
this.container.addChild(this.sprite);
|
|
|
|
this.resetSourceRectangle();
|
|
|
|
});
|
|
|
|
}
|
2019-03-21 00:09:17 -05:00
|
|
|
animation.on(
|
|
|
|
['directionChanged', 'indexChanged'],
|
|
|
|
this.onAnimationChanged
|
|
|
|
);
|
2019-03-19 18:05:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
2019-03-21 00:09:17 -05:00
|
|
|
animation.off(
|
|
|
|
['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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|