File: src/link/Layers.js
/**
* @author Vsevolod Strukchinsky @floatdrop
*/
/**
A class Layers represents a named collection of display objects container (layers). It is the base class of all display objects that act as a container for other objects.
Single level layers inheritance:
@example
// Create three layers, that can be getted by layers.top, layers.middle, layers.bottom
var layers = new LINK.Layers("bottom", "middle", "top");
layers.top.addChild(...);
layers.bottom.addChild(...);
Multi level layers inheritance (be careful with swapping between different levels of layers):
@example
var layers = new LINK.Layers({
"ground": new LINK.Layers("earth",{"grass": new LINK.Layers("leafs", "flowers")),
"sky": new LINK.Layers("birds", "clouds")
}, "ui");
layers.ground.earth.addChild(...);
layers.ground.grass.flowers.addChild(...);
layers.sky.clouds.addChild(...);
layers.ui.addChild(...);
@class Layers
@extends DisplayObjectContainer
@constructor
**/
LINK.Layers = function () {
PIXI.DisplayObjectContainer.call(this);
this.blockedNames = Object.keys(this);
this.sort = false;
for (var argumentIndex in arguments) {
var arg = arguments[argumentIndex];
if (typeof arg === "string") {
this.addLayer(arg);
} else if (arg instanceof Object) {
for (var layersGroupName in arg) {
this[layersGroupName] = arg[layersGroupName];
this.addChild(arg[layersGroupName]);
}
}
}
};
LINK.Layers.constructor = LINK.Layers;
LINK.Layers.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);
/**
* Creates new layer with name layerName above others layers.
* @method addLayer
* @param layerName {String}
* @return DisplayObject
*/
LINK.Layers.prototype.addLayer = function (layerName) {
return this.addLayerAt(layerName, this.children.length);
};
/**
* Creates new layer with name layerName at specified index.
* @method addLayerAt
* @param Layer {DisplayObject}
* @param index {Number}
*/
LINK.Layers.prototype.addLayerAt = function (layerName, index) {
if (layerName in this) {
throw new Error(layerName + " Suplied name already used by " + this[layerName]);
}
var layer = new PIXI.DisplayObjectContainer();
layer.layername = layerName;
this.addChildAt(layer, index);
this[layerName] = layer;
return layer;
};
/**
* Swaps 2 Layers
* @method swapChildren
* @param LayerName {String}
* @param LayerName2 {String}
*/
LINK.Layers.prototype.swapLayers = function (layerName, layerName2) {
var layer = this[layerName];
var layer2 = this[layerName2];
this.swapChildren(layer, layer2);
};
/**
* Returns the Layer with specified name or creates it
* @method getLayer
* @param layerName {String}
* @return DisplayObjectContainer
*/
LINK.Layers.prototype.getLayer = function (layerName) {
if (!this[layerName])
this.addLayer(layerName);
return this[layerName];
};
/**
* Returns the Layer at the specified index
* @method getLayerAt
* @param index {Number}
* @return DisplayObjectContainer
*/
LINK.Layers.prototype.getLayerAt = function (index) {
return this.getChildAt(index);
};
/**
* Removes a layer from the container.
* @method removeLayer
* @param String {LayerName}
*/
LINK.Layers.prototype.removeLayer = function (layerName) {
if (!(layerName in this) || (layerName in this.blockedNames)) return;
PIXI.DisplayObjectContainer.prototype.removeChild.call(this, this[layerName]);
delete this[layerName];
};
/**
* Removes a child from the container.
* @method removeChild
* @param DisplayObject {DisplayObject}
*/
LINK.Layers.prototype.removeChild = function (child) {
if (child.layername && child.layername in this) {
delete this[child.layername];
}
PIXI.DisplayObjectContainer.prototype.removeChild.call(this, child);
};