2019-03-18 20:06:47 -05:00
|
|
|
const PIXI = 'undefined' !== typeof window ? require('pixi.js') : undefined;
|
|
|
|
|
|
|
|
import {Renderable} from './renderable';
|
|
|
|
|
|
|
|
export class Container extends Renderable {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this._children = [];
|
2019-03-24 04:04:39 -05:00
|
|
|
this._childrenIndexes = new Map();
|
2019-03-19 21:02:43 -05:00
|
|
|
this._isDirty = false;
|
2019-03-25 10:18:41 -05:00
|
|
|
this.container = PIXI ? new PIXI.Container() : undefined;
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
addChild(child) {
|
2019-03-19 21:02:43 -05:00
|
|
|
child.parent = this;
|
|
|
|
this.isDirty = true;
|
2019-03-24 04:04:39 -05:00
|
|
|
const index = this._children.push(child) - 1;
|
|
|
|
this._childrenIndexes.set(child, index);
|
2019-03-25 10:18:41 -05:00
|
|
|
this.container.addChild(child.internal);
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|
|
|
|
|
2019-03-19 18:29:11 -05:00
|
|
|
get children() {
|
2019-03-18 20:06:47 -05:00
|
|
|
return this._children;
|
|
|
|
}
|
|
|
|
|
2019-03-30 05:07:39 -05:00
|
|
|
desaturate() {
|
|
|
|
let filter = new PIXI.filters.ColorMatrixFilter();
|
|
|
|
filter.desaturate();
|
|
|
|
this.internal.filters = [filter];
|
|
|
|
}
|
|
|
|
|
2019-03-19 18:29:11 -05:00
|
|
|
destroy() {
|
|
|
|
this.children.forEach((child) => {
|
2019-04-14 16:10:08 -05:00
|
|
|
this.removeChild(child);
|
2019-03-19 18:29:11 -05:00
|
|
|
child.destroy();
|
|
|
|
});
|
|
|
|
super.destroy();
|
|
|
|
}
|
|
|
|
|
2019-03-25 10:18:41 -05:00
|
|
|
get internal() {
|
|
|
|
return this.container;
|
|
|
|
}
|
|
|
|
|
2019-03-19 21:02:43 -05:00
|
|
|
get isDirty() {
|
|
|
|
return this._isDirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
set isDirty(isDirty) {
|
|
|
|
if (this.parent) {
|
|
|
|
this.parent.isDirty = isDirty;
|
|
|
|
}
|
|
|
|
this._isDirty = isDirty;
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:12:31 -05:00
|
|
|
night(intensity = 1) {
|
2019-04-21 20:37:29 -05:00
|
|
|
let filter;
|
|
|
|
const {filters} = this.container;
|
|
|
|
if (
|
|
|
|
filters
|
|
|
|
&& filters.length > 0
|
|
|
|
&& filters[0] instanceof PIXI.filters.ColorMatrixFilter
|
|
|
|
) {
|
|
|
|
filter = filters[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
filter = new PIXI.filters.ColorMatrixFilter();
|
|
|
|
}
|
2019-03-30 06:12:31 -05:00
|
|
|
const nightness = 0.1;
|
|
|
|
const double = nightness * 2;
|
2019-04-10 12:44:11 -05:00
|
|
|
const half = nightness / 2;
|
|
|
|
const redDown = 1 - (intensity * (1 + double));
|
|
|
|
const blueUp = 1 - (intensity * (1 - half));
|
2019-03-30 06:12:31 -05:00
|
|
|
const scale = intensity * nightness;
|
|
|
|
const matrix = [
|
2019-04-10 12:44:11 -05:00
|
|
|
redDown , -scale , 0 , 0, 0,
|
|
|
|
-scale , (1 - intensity), scale , 0, 0,
|
|
|
|
0 , scale , blueUp , 0, 0,
|
|
|
|
0 , 0 , 0 , 1, 0,
|
2019-03-30 06:12:31 -05:00
|
|
|
];
|
|
|
|
filter._loadMatrix(matrix);
|
2019-03-30 05:07:39 -05:00
|
|
|
this.internal.filters = [filter];
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:59:49 -05:00
|
|
|
paused(intensity = 1) {
|
|
|
|
let filter = new PIXI.filters.ColorMatrixFilter();
|
|
|
|
filter.sepia();
|
|
|
|
filter.brightness(1 - intensity, true);
|
|
|
|
this.internal.filters = [filter];
|
|
|
|
}
|
|
|
|
|
2019-03-30 05:07:39 -05:00
|
|
|
removeAllFilters() {
|
|
|
|
this.internal.filters = [];
|
|
|
|
}
|
|
|
|
|
2019-03-24 04:04:39 -05:00
|
|
|
_removeChild(child) {
|
2019-03-18 20:06:47 -05:00
|
|
|
const index = this._children.indexOf(child);
|
|
|
|
if (-1 === index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._children.splice(index, 1);
|
2019-03-25 10:18:41 -05:00
|
|
|
this.container.removeChild(child.internal);
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|
2019-03-27 01:50:05 -05:00
|
|
|
|
2019-03-24 04:04:39 -05:00
|
|
|
removeChild(child) {
|
|
|
|
this._removeChild(child);
|
2019-03-27 01:50:05 -05:00
|
|
|
this._resetChildrenIndexes();
|
2019-03-24 04:04:39 -05:00
|
|
|
}
|
2019-03-18 20:06:47 -05:00
|
|
|
|
|
|
|
removeAllChildren() {
|
|
|
|
for (const child of this._children) {
|
2019-03-24 04:04:39 -05:00
|
|
|
this._removeChild(child);
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|
2019-03-24 04:04:39 -05:00
|
|
|
this._childrenIndexes = new Map();
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|
|
|
|
|
2019-03-27 01:50:05 -05:00
|
|
|
_resetChildrenIndexes() {
|
|
|
|
this._childrenIndexes = new Map();
|
|
|
|
for (const i in this._children) {
|
|
|
|
const child = this._children[i];
|
|
|
|
this._childrenIndexes.set(child, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 05:07:39 -05:00
|
|
|
sepia() {
|
|
|
|
let filter = new PIXI.filters.ColorMatrixFilter();
|
|
|
|
filter.sepia();
|
|
|
|
this.internal.filters = [filter];
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:07:15 -05:00
|
|
|
setFilter(filter) {
|
|
|
|
switch (filter) {
|
|
|
|
case 'bloom':
|
|
|
|
const {AdvancedBloomFilter} = require('@pixi/filter-advanced-bloom');
|
|
|
|
this.container.filters = [new AdvancedBloomFilter({
|
2019-04-21 13:14:46 -05:00
|
|
|
threshold: 0.5,
|
|
|
|
bloomScale: 1.1,
|
|
|
|
brightness: 0.7,
|
|
|
|
blur: 1,
|
|
|
|
quality: 7,
|
2019-04-21 05:07:15 -05:00
|
|
|
})];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 21:02:43 -05:00
|
|
|
tick() {
|
|
|
|
if (!this.isDirty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let needsSort = false;
|
|
|
|
for (const child of this._children) {
|
|
|
|
if (0 !== child.zIndex) {
|
|
|
|
needsSort = true;
|
|
|
|
}
|
|
|
|
if (child instanceof Container) {
|
|
|
|
child.tick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!needsSort) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._children.sort((l, r) => {
|
2019-03-24 04:04:39 -05:00
|
|
|
if (l.zIndex !== r.zIndex) {
|
|
|
|
return l.zIndex - r.zIndex;
|
|
|
|
}
|
|
|
|
const lIndex = this._childrenIndexes.get(l);
|
|
|
|
const rIndex = this._childrenIndexes.get(r);
|
|
|
|
return lIndex - rIndex;
|
2019-03-19 21:02:43 -05:00
|
|
|
});
|
2019-03-25 10:18:41 -05:00
|
|
|
this.container.children = this._children.map((child) => {
|
2019-03-19 21:02:43 -05:00
|
|
|
return child.internal;
|
|
|
|
});
|
2019-03-27 01:50:05 -05:00
|
|
|
this._resetChildrenIndexes();
|
2019-03-19 21:02:43 -05:00
|
|
|
}
|
|
|
|
|
2019-03-18 20:06:47 -05:00
|
|
|
}
|