avocado-old/packages/graphics/traits/visible.trait.js

105 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-03-18 20:07:05 -05:00
import {compose} from '@avocado/core';
2019-04-14 20:21:52 -05:00
import {StateProperty, Trait} from '@avocado/entity';
import {Rectangle, Vector} from '@avocado/math';
2019-03-18 20:07:05 -05:00
import {Container} from '../container';
import {hasGraphics} from '../has-graphics';
2019-03-18 20:07:05 -05:00
const decorate = compose(
StateProperty('isVisible', {
track: true,
}),
2019-03-18 20:07:05 -05:00
);
2019-04-14 18:42:13 -05:00
export class Visible extends decorate(Trait) {
2019-03-18 20:07:05 -05:00
static defaultParams() {
return {
trackPosition: true,
};
}
2019-04-12 00:36:30 -05:00
static defaultState() {
return {
isVisible: true,
};
}
2019-03-19 11:25:42 -05:00
initialize() {
if (hasGraphics) {
this._container = new Container();
this._container.isVisible = this.entity.isVisible;
}
2019-03-19 11:03:48 -05:00
this.trackPosition = this.params.get('trackPosition');
2019-03-18 20:07:05 -05:00
}
2019-03-20 23:23:34 -05:00
destroy() {
if (this._container) {
this._container.destroy();
}
}
2019-03-18 20:07:05 -05:00
get container() {
return this._container;
}
2019-04-14 18:42:13 -05:00
get visibleBoundingBox() {
2019-03-26 17:17:09 -05:00
// Collect all bounding boxes.
2019-04-14 18:42:13 -05:00
const visibleBoundingBoxes = this.entity.invokeHookFlat(
'visibleBoundingBoxes'
2019-03-26 17:17:09 -05:00
);
2019-04-14 18:42:13 -05:00
if (0 === visibleBoundingBoxes.length) {
2019-03-26 17:17:09 -05:00
return [0, 0, 0, 0];
}
let unifiedBoundingBox = [0, 0, 0, 0];
2019-04-14 18:42:13 -05:00
for (const visibleBoundingBox of visibleBoundingBoxes) {
2019-03-26 17:17:09 -05:00
unifiedBoundingBox = Rectangle.united(
unifiedBoundingBox,
2019-04-14 18:42:13 -05:00
visibleBoundingBox,
2019-03-26 17:17:09 -05:00
);
}
return unifiedBoundingBox;
}
2019-03-19 11:03:48 -05:00
shouldSynchronizePosition() {
return this._container && this.trackPosition;
2019-03-19 11:03:48 -05:00
}
synchronizePosition() {
2019-04-12 15:53:44 -05:00
if (!this.entity.is('positioned')) {
2019-03-19 11:48:46 -05:00
return;
2019-03-19 11:25:42 -05:00
}
2019-04-11 12:20:24 -05:00
this.entity.container.position = this.entity.position;
2019-03-19 21:27:41 -05:00
this.entity.container.zIndex = this.entity.y;
2019-03-19 11:03:48 -05:00
}
2019-03-18 20:07:05 -05:00
listeners() {
const listeners = {
isVisibleChanged: () => {
this.entity.container.visible = this.entity.isVisible;
},
};
2019-03-19 11:03:48 -05:00
if (this.shouldSynchronizePosition()) {
listeners.traitAdded = (type) => {
if (-1 === [
2019-04-14 18:42:13 -05:00
'visible',
'positioned',
].indexOf(type)) {
2019-03-19 11:03:48 -05:00
return;
2019-03-18 20:07:05 -05:00
}
2019-03-19 11:03:48 -05:00
this.synchronizePosition();
2019-03-19 10:21:00 -05:00
};
2019-03-18 20:07:05 -05:00
}
2019-03-19 10:21:00 -05:00
return listeners;
2019-03-18 20:07:05 -05:00
}
tick(elapsed) {
if (this.shouldSynchronizePosition()) {
this.synchronizePosition();
}
}
2019-03-18 20:07:05 -05:00
}