2013-09-22 23:02:43 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace('pskl.model');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {Number} width
|
|
|
|
* @param {Number} height
|
|
|
|
*/
|
2013-10-01 00:00:31 +04:00
|
|
|
ns.Piskel = function (width, height) {
|
|
|
|
if (width && height) {
|
2013-09-22 23:02:43 +04:00
|
|
|
/** @type {Array} */
|
|
|
|
this.layers = [];
|
|
|
|
|
|
|
|
/** @type {Number} */
|
|
|
|
this.width = width;
|
|
|
|
|
|
|
|
/** @type {Number} */
|
|
|
|
this.height = height;
|
|
|
|
} else {
|
|
|
|
throw 'Missing arguments in Piskel constructor : ' + Array.prototype.join.call(arguments, ",");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-15 02:03:29 +04:00
|
|
|
/**
|
|
|
|
* Create a piskel instance from an existing set of (non empty) layers
|
|
|
|
* Layers should all be synchronized : same number of frames, same dimensions
|
|
|
|
* @param {Array<pskl.model.Layer>} layers
|
|
|
|
* @return {pskl.model.Piskel}
|
|
|
|
*/
|
|
|
|
ns.Piskel.fromLayers = function (layers) {
|
|
|
|
var piskel = null;
|
|
|
|
if (layers.length > 0 && layers[0].length() > 0) {
|
|
|
|
var sampleFrame = layers[0].getFrameAt(0);
|
|
|
|
piskel = new pskl.model.Piskel(sampleFrame.getWidth(), sampleFrame.getHeight());
|
|
|
|
layers.forEach(piskel.addLayer.bind(piskel));
|
|
|
|
} else {
|
|
|
|
throw 'Piskel.fromLayers expects array of non empty pskl.model.Layer as first argument';
|
|
|
|
}
|
|
|
|
return piskel;
|
|
|
|
};
|
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
ns.Piskel.prototype.getLayers = function () {
|
|
|
|
return this.layers;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Piskel.prototype.getHeight = function () {
|
|
|
|
return this.height;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Piskel.prototype.getWidth = function () {
|
|
|
|
return this.width;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Piskel.prototype.getLayers = function () {
|
|
|
|
return this.layers;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Piskel.prototype.getLayerAt = function (index) {
|
|
|
|
return this.layers[index];
|
|
|
|
};
|
|
|
|
|
2013-09-25 02:11:12 +04:00
|
|
|
ns.Piskel.prototype.getLayersByName = function (name) {
|
|
|
|
return this.layers.filter(function (l) {
|
|
|
|
return l.getName() == name;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
ns.Piskel.prototype.addLayer = function (layer) {
|
|
|
|
this.layers.push(layer);
|
|
|
|
};
|
|
|
|
|
2013-09-25 02:11:12 +04:00
|
|
|
ns.Piskel.prototype.moveLayerUp = function (layer) {
|
|
|
|
var index = this.layers.indexOf(layer);
|
|
|
|
if (index > -1 && index < this.layers.length-1) {
|
|
|
|
this.layers[index] = this.layers[index+1];
|
|
|
|
this.layers[index+1] = layer;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Piskel.prototype.moveLayerDown = function (layer) {
|
|
|
|
var index = this.layers.indexOf(layer);
|
|
|
|
if (index > 0) {
|
|
|
|
this.layers[index] = this.layers[index-1];
|
|
|
|
this.layers[index-1] = layer;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-22 23:02:43 +04:00
|
|
|
ns.Piskel.prototype.removeLayer = function (layer) {
|
|
|
|
var index = this.layers.indexOf(layer);
|
|
|
|
if (index != -1) {
|
|
|
|
this.layers.splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Piskel.prototype.removeLayerAt = function (index) {
|
|
|
|
this.layers.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|