refactor: simplify position using anchor

This commit is contained in:
cha0s 2019-04-19 16:25:08 -05:00
parent 64b3022c44
commit a04dea6d95

View File

@ -65,8 +65,9 @@ export class Pictured extends decorate(Trait) {
const {uri} = this._images[key];
const imagePromise = Image.load(uri).then((image) => {
const sprite = this.sprites[key] = new Sprite(image);
sprite.anchor = [0.5, 0.5];
// Calculate any offset.
sprite.position = this.viewPositionFor(key);
sprite.position = this.offsetFor(key);
// Set current image upfront.
const isCurrentImage = key === this.entity.currentImage;
if (isCurrentImage) {
@ -106,17 +107,6 @@ export class Pictured extends decorate(Trait) {
return this._images[key].size;
}
viewPositionFor(key) {
const image = this._images[key];
if (!image) {
return [0, 0];
}
const size = this.sizeFor(key);
const halfway = Vector.scale(size, -0.5);
const offset = this.offsetFor(key);
return Vector.add(halfway, offset);
}
hooks() {
return {
@ -126,15 +116,17 @@ export class Pictured extends decorate(Trait) {
if (!image) {
return [0, 0, 0, 0];
}
const viewPosition = this.viewPositionFor(key);
const viewPosition = this.offsetFor(key);
const position = Vector.add(this.entity.position, viewPosition);
return Rectangle.compose(position, this.sizeFor(key));
}
},
}
}
listeners() {
return {
currentImageChanged: (oldKey) => {
// Bounding box update.
this.entity.updateVisibleBoundingBox();
@ -155,6 +147,7 @@ export class Pictured extends decorate(Trait) {
}
this.loadImagesIfPossible();
},
};
}