Merge pull request #130 from juliandescottes/layers-backward-compatibility

Layers:Added backward compatibility for previous models
This commit is contained in:
grosbouddha 2013-10-04 15:03:27 -07:00
commit 93ba8c70a0

View File

@ -26,38 +26,53 @@
}, },
deserializePiskel : function (json) { deserializePiskel : function (json) {
var piskel = null;
var data = JSON.parse(json); var data = JSON.parse(json);
if (data.modelVersion == Constants.MODEL_VERSION) { if (data.modelVersion == Constants.MODEL_VERSION) {
var pData = data.piskel; var pData = data.piskel;
var layers = pData.layers.map(function (serializedLayer) { piskel = new pskl.model.Piskel(pData.width, pData.height);
return pskl.utils.Serializer.deserializeLayer(serializedLayer);
}); pData.layers.forEach(function (serializedLayer) {
var piskel = new pskl.model.Piskel(pData.width, pData.height); var layer = pskl.utils.Serializer.deserializeLayer(serializedLayer);
layers.forEach(function (layer) {
piskel.addLayer(layer); piskel.addLayer(layer);
}); });
return piskel;
} else { } else {
// pre-layer implementation adapter piskel = pskl.utils.Serializer.backwardDeserializer_(data);
} }
return piskel;
}, },
deserializeLayer : function (json) { deserializeLayer : function (json) {
var lData = JSON.parse(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); var layer = new pskl.model.Layer(lData.name);
frames.forEach(function (frame) {
lData.frames.forEach(function (serializedFrame) {
var frame = pskl.utils.Serializer.deserializeFrame(serializedFrame);
layer.addFrame(frame); layer.addFrame(frame);
}); });
return layer; return layer;
}, },
deserializeFrame : function (json) { deserializeFrame : function (json) {
var framePixelGrid = JSON.parse(json); var framePixelGrid = JSON.parse(json);
return pskl.model.Frame.fromPixelGrid(framePixelGrid); return pskl.model.Frame.fromPixelGrid(framePixelGrid);
},
/**
* Deserialize old piskel framesheets. Initially piskels were stored as arrays of frames : "[[pixelGrid],[pixelGrid],[pixelGrid]]".
*/
backwardDeserializer_ : function (frames) {
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;
} }
}; };
})(); })();