piskel/js/utils/Serializer.js
jdescottes 6528c7724b Issue 24 : Layers
!! NOT STABLE !!
Initial implementation. No UI update yet.
Check js/model/Piskel.js and js/model/Layer.js for an overview of the new
API.

Piskels can be saved on the existing service.

Previous piskels cannot be loaded. This should be fixed soon.
2013-09-22 21:02:43 +02:00

64 lines
No EOL
1.9 KiB
JavaScript

(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(),
fps : piskel.getFps(),
layers : serializedLayers
}
});
},
serializeLayer : function (layer) {
var serializedFrames = layer.getFrames().map(function (f) {
return f.serialize();
});
return JSON.stringify({
name : layer.getName(),
frames : serializedFrames
});
},
deserializePiskel : function (json) {
var data = JSON.parse(json);
if (data.modelVersion == Constants.MODEL_VERSION) {
var pData = data.piskel;
var layers = pData.layers.map(function (serializedLayer) {
return pskl.utils.Serializer.deserializeLayer(serializedLayer);
});
var piskel = new pskl.model.Piskel(pData.width, pData.height, pData.fps);
layers.forEach(function (layer) {
piskel.addLayer(layer);
});
return piskel;
} else {
// pre-layer implementation adapter
}
},
deserializeLayer : function (json) {
var lData = JSON.parse(json);
var frames = lData.frames.map(function (serializedFrame) {
return pskl.utils.Serializer.deserializeFrame(serializedFrame);
});
var layer = new pskl.model.Layer(lData.name);
frames.forEach(function (frame) {
layer.addFrame(frame);
});
return layer;
},
deserializeFrame : function (json) {
var framePixelGrid = JSON.parse(json);
return pskl.model.Frame.fromPixelGrid(framePixelGrid);
}
};
})();