2013-09-22 19:02:43 +00:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace('pskl.utils');
|
|
|
|
ns.Serializer = {
|
|
|
|
serializePiskel : function (piskel) {
|
|
|
|
var serializedLayers = piskel.getLayers().map(function (l) {
|
|
|
|
return pskl.utils.Serializer.serializeLayer(l);
|
|
|
|
});
|
|
|
|
return JSON.stringify({
|
|
|
|
modelVersion : Constants.MODEL_VERSION,
|
|
|
|
piskel : {
|
|
|
|
height : piskel.getHeight(),
|
|
|
|
width : piskel.getWidth(),
|
|
|
|
layers : serializedLayers
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
serializeLayer : function (layer) {
|
|
|
|
var serializedFrames = layer.getFrames().map(function (f) {
|
|
|
|
return f.serialize();
|
|
|
|
});
|
|
|
|
return JSON.stringify({
|
|
|
|
name : layer.getName(),
|
|
|
|
frames : serializedFrames
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-10-09 22:06:11 +00:00
|
|
|
deserializePiskel : function (piskelString) {
|
|
|
|
var piskelData = JSON.parse(piskelString);
|
|
|
|
return this.createPiskelFromData(piskelData);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to deserializePiskel, but dealing directly with a parsed piskel
|
|
|
|
* @param {Object} piskelData JSON.parse of a serialized piskel
|
|
|
|
* @return {Piskel} a piskel
|
|
|
|
*/
|
|
|
|
createPiskel : function (piskelData) {
|
2013-10-04 20:59:42 +00:00
|
|
|
var piskel = null;
|
2013-10-09 22:06:11 +00:00
|
|
|
if (piskelData.modelVersion == Constants.MODEL_VERSION) {
|
|
|
|
var pData = piskelData.piskel;
|
2013-10-04 20:59:42 +00:00
|
|
|
piskel = new pskl.model.Piskel(pData.width, pData.height);
|
|
|
|
|
|
|
|
pData.layers.forEach(function (serializedLayer) {
|
|
|
|
var layer = pskl.utils.Serializer.deserializeLayer(serializedLayer);
|
2013-09-22 19:02:43 +00:00
|
|
|
piskel.addLayer(layer);
|
|
|
|
});
|
|
|
|
} else {
|
2013-10-09 22:06:11 +00:00
|
|
|
piskel = pskl.utils.Serializer.backwardDeserializer_(piskelData);
|
2013-09-22 19:02:43 +00:00
|
|
|
}
|
2013-10-04 20:59:42 +00:00
|
|
|
|
|
|
|
return piskel;
|
2013-09-22 19:02:43 +00:00
|
|
|
},
|
|
|
|
|
2013-10-09 22:06:11 +00:00
|
|
|
deserializeLayer : function (layerString) {
|
|
|
|
var lData = JSON.parse(layerString);
|
2013-09-22 19:02:43 +00:00
|
|
|
var layer = new pskl.model.Layer(lData.name);
|
2013-10-04 20:59:42 +00:00
|
|
|
|
|
|
|
lData.frames.forEach(function (serializedFrame) {
|
|
|
|
var frame = pskl.utils.Serializer.deserializeFrame(serializedFrame);
|
2013-09-22 19:02:43 +00:00
|
|
|
layer.addFrame(frame);
|
|
|
|
});
|
2013-10-04 20:59:42 +00:00
|
|
|
|
2013-09-22 19:02:43 +00:00
|
|
|
return layer;
|
|
|
|
},
|
|
|
|
|
2013-10-09 22:06:11 +00:00
|
|
|
deserializeFrame : function (frameString) {
|
|
|
|
var framePixelGrid = JSON.parse(frameString);
|
2013-09-22 19:02:43 +00:00
|
|
|
return pskl.model.Frame.fromPixelGrid(framePixelGrid);
|
2013-10-04 20:59:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserialize old piskel framesheets. Initially piskels were stored as arrays of frames : "[[pixelGrid],[pixelGrid],[pixelGrid]]".
|
|
|
|
*/
|
2013-10-04 21:55:45 +00:00
|
|
|
backwardDeserializer_ : function (frames) {
|
2013-10-04 20:59:42 +00:00
|
|
|
var layer = new pskl.model.Layer('Layer 1');
|
|
|
|
frames.forEach(function (frame) {
|
|
|
|
layer.addFrame(pskl.model.Frame.fromPixelGrid(frame));
|
|
|
|
});
|
|
|
|
var width = layer.getFrameAt(0).getWidth(), height = layer.getFrameAt(0).getHeight();
|
|
|
|
var piskel = new pskl.model.Piskel(width, height);
|
|
|
|
piskel.addLayer(layer);
|
|
|
|
|
|
|
|
return piskel;
|
2013-09-22 19:02:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|