avocado-old/packages/topdown/layers-view.js

35 lines
803 B
JavaScript
Raw Normal View History

2019-03-27 01:52:24 -05:00
import {Container} from '@avocado/graphics';
import {LayerView} from './layer-view';
export class LayersView extends Container {
constructor(layers, renderer) {
super();
this.layers = layers;
2019-03-27 17:15:53 -05:00
this.layerViews = {};
2019-03-27 01:52:24 -05:00
this.renderer = renderer;
2019-04-12 18:58:38 -05:00
this.layers.on('layerAdded', this.onLayerAdded, this);
2019-03-27 17:15:53 -05:00
for (const {index, layer} of layers) {
this.onLayerAdded(layer, index);
2019-03-27 01:52:24 -05:00
}
2019-04-12 18:58:38 -05:00
this.layers.on('layerRemoved', this.onLayerRemoved, this);
2019-03-27 01:52:24 -05:00
}
2019-03-27 17:15:53 -05:00
onLayerAdded(layer, index) {
2019-03-27 01:52:24 -05:00
const layerView = new LayerView(layer, this.renderer);
2019-03-27 17:15:53 -05:00
this.layerViews[index] = layerView;
2019-03-27 01:52:24 -05:00
this.addChild(layerView);
}
2019-03-27 17:15:53 -05:00
onLayerRemoved(layer, index) {
const layerView = this.layerViews[index];
2019-03-27 01:52:24 -05:00
if (!layerView) {
return;
}
this.removeChild(layerView);
}
}