Move hidden frames info to Piskel model

This commit is contained in:
Julian Descottes 2018-10-07 13:49:35 +02:00
parent 65f4fd0f27
commit af9095b934
8 changed files with 53 additions and 64 deletions

View File

@ -122,6 +122,16 @@
l.addFrameAt(this.createEmptyFrame_(), index); l.addFrameAt(this.createEmptyFrame_(), index);
}.bind(this)); }.bind(this));
this.onFrameAddedAt_(index);
};
ns.PiskelController.prototype.onFrameAddedAt_ = function (index) {
this.piskel.hiddenFrames = this.piskel.hiddenFrames.map(function (hiddenIndex) {
if (hiddenIndex >= index) {
return hiddenIndex + 1;
}
});
this.setCurrentFrameIndex(index); this.setCurrentFrameIndex(index);
}; };
@ -135,6 +145,13 @@
this.getLayers().forEach(function (l) { this.getLayers().forEach(function (l) {
l.removeFrameAt(index); l.removeFrameAt(index);
}); });
this.piskel.hiddenFrames = this.piskel.hiddenFrames.map(function (hiddenIndex) {
if (hiddenIndex > index) {
return hiddenIndex - 1;
}
});
// Current frame index is impacted if the removed frame was before the current frame // Current frame index is impacted if the removed frame was before the current frame
if (this.currentFrameIndex >= index && this.currentFrameIndex > 0) { if (this.currentFrameIndex >= index && this.currentFrameIndex > 0) {
this.setCurrentFrameIndex(this.currentFrameIndex - 1); this.setCurrentFrameIndex(this.currentFrameIndex - 1);
@ -149,40 +166,52 @@
this.getLayers().forEach(function (l) { this.getLayers().forEach(function (l) {
l.duplicateFrameAt(index); l.duplicateFrameAt(index);
}); });
this.setCurrentFrameIndex(index + 1); this.onFrameAddedAt_(index + 1);
}; };
ns.PiskelController.prototype.toggleFrameAt = function (index) { ns.PiskelController.prototype.toggleFrameAt = function (index) {
this.getLayers().forEach(function (l) { var hiddenFrames = this.piskel.hiddenFrames;
l.toggleFrameAt(index); if (hiddenFrames.indexOf(index) === -1) {
}); hiddenFrames.push(index);
} else {
hiddenFrames = hiddenFrames.filter(function (i) {
return i !== index;
});
}
// Keep the hiddenFrames array sorted.
this.piskel.hiddenFrames = hiddenFrames.sort();
}; };
ns.PiskelController.prototype.moveFrame = function (fromIndex, toIndex) { ns.PiskelController.prototype.moveFrame = function (fromIndex, toIndex) {
this.getLayers().forEach(function (l) { this.getLayers().forEach(function (l) {
l.moveFrame(fromIndex, toIndex); l.moveFrame(fromIndex, toIndex);
}); });
this.piskel.hiddenFrames = this.piskel.hiddenFrames.map(function (index) {
if (index === fromIndex) {
return toIndex;
}
var direction = fromIndex < toIndex ? 1 : -1;
var max = Math.max(fromIndex, toIndex);
var min = Math.min(fromIndex, toIndex);
if (index >= min && index <= max) {
return index - direction;
}
});
}; };
ns.PiskelController.prototype.hasVisibleFrameAt = function (index) { ns.PiskelController.prototype.hasVisibleFrameAt = function (index) {
var frame = this.getCurrentLayer().getFrameAt(index); return this.piskel.hiddenFrames.indexOf(index) === -1;
return frame ? frame.isVisible() : false;
}; };
ns.PiskelController.prototype.getVisibleFrameIndexes = function () { ns.PiskelController.prototype.getVisibleFrameIndexes = function () {
var frameIndexes = this.getCurrentLayer().getFrames() return this.getCurrentLayer().getFrames().map(function (frame, index) {
/* Replace each frame with its index return index;
or -1 if it's not visible */ }).filter(function (index) {
.map( return this.piskel.hiddenFrames.indexOf(index) === -1;
function(frame, idx) { }.bind(this));
return (frame.visible) ? idx : -1;
})
/* Filter out invisible frames */
.filter(
function(index) {
return index >= 0;
});
return frameIndexes;
}; };
ns.PiskelController.prototype.getFrameCount = function () { ns.PiskelController.prototype.getFrameCount = function () {

View File

@ -9,7 +9,6 @@
this.version = 0; this.version = 0;
this.pixels = ns.Frame.createEmptyPixelGrid_(width, height); this.pixels = ns.Frame.createEmptyPixelGrid_(width, height);
this.stateIndex = 0; this.stateIndex = 0;
this.visible = true;
} else { } else {
throw 'Bad arguments in pskl.model.Frame constructor : ' + width + ', ' + height; throw 'Bad arguments in pskl.model.Frame constructor : ' + width + ', ' + height;
} }
@ -152,12 +151,4 @@
ns.Frame.prototype.isSameSize = function (otherFrame) { ns.Frame.prototype.isSameSize = function (otherFrame) {
return this.getHeight() == otherFrame.getHeight() && this.getWidth() == otherFrame.getWidth(); return this.getHeight() == otherFrame.getHeight() && this.getWidth() == otherFrame.getWidth();
}; };
ns.Frame.prototype.toggleVisibility = function () {
this.visible = !this.visible;
};
ns.Frame.prototype.isVisible = function () {
return this.visible;
};
})(); })();

View File

@ -110,15 +110,6 @@
} }
}; };
ns.Layer.prototype.toggleFrameAt = function (index) {
var frame = this.frames[index];
if (frame) {
frame.toggleVisibility();
} else {
console.error('Frame not found in toggleFrameAt (at %s)', index);
}
};
ns.Layer.prototype.size = function () { ns.Layer.prototype.size = function () {
return this.frames.length; return this.frames.length;
}; };

View File

@ -16,6 +16,7 @@
this.descriptor = descriptor; this.descriptor = descriptor;
this.savePath = null; this.savePath = null;
this.fps = fps; this.fps = fps;
this.hiddenFrames = [];
} else { } else {
throw 'Missing arguments in Piskel constructor : ' + Array.prototype.join.call(arguments, ','); throw 'Missing arguments in Piskel constructor : ' + Array.prototype.join.call(arguments, ',');
} }

View File

@ -74,13 +74,8 @@
image.src = chunk.base64PNG; image.src = chunk.base64PNG;
return deferred.promise; return deferred.promise;
})).then(function () { })).then(function () {
var hiddenFrames = this.hiddenFrames;
frames.forEach(function (frame) { frames.forEach(function (frame) {
layer.addFrame(frame); layer.addFrame(frame);
var currentIndex = layer.getFrames().length - 1;
if (hiddenFrames.indexOf(currentIndex) != -1) {
frame.visible = false;
}
}); });
this.layers_[index] = layer; this.layers_[index] = layer;
this.onLayerLoaded_(); this.onLayerLoaded_();
@ -98,6 +93,7 @@
this.layers_.forEach(function (layer) { this.layers_.forEach(function (layer) {
this.piskel_.addLayer(layer); this.piskel_.addLayer(layer);
}.bind(this)); }.bind(this));
this.piskel_.hiddenFrames = this.hiddenFrames;
this.callback_(this.piskel_); this.callback_(this.piskel_);
} }
}; };

View File

@ -21,15 +21,6 @@
var serializedLayers = piskel.getLayers().map(function (l) { var serializedLayers = piskel.getLayers().map(function (l) {
return pskl.utils.serialization.Serializer.serializeLayer(l); return pskl.utils.serialization.Serializer.serializeLayer(l);
}); });
var frames = piskel.getLayerAt(0).getFrames();
var hiddenFrames = frames.map(function (frame, index) {
if (frame.visible) {
return -1;
}
return index;
}).filter(function (frameIndex) {
return frameIndex !== -1;
});
return JSON.stringify({ return JSON.stringify({
modelVersion : Constants.MODEL_VERSION, modelVersion : Constants.MODEL_VERSION,
@ -40,7 +31,7 @@
height : piskel.getHeight(), height : piskel.getHeight(),
width : piskel.getWidth(), width : piskel.getWidth(),
layers : serializedLayers, layers : serializedLayers,
hiddenFrames : hiddenFrames, hiddenFrames : piskel.hiddenFrames,
} }
}); });
}, },

View File

@ -104,6 +104,7 @@
var descriptor = new pskl.model.piskel.Descriptor(descriptorName, descriptorDescription); var descriptor = new pskl.model.piskel.Descriptor(descriptorName, descriptorDescription);
var piskel = new pskl.model.Piskel(width, height, fps, descriptor); var piskel = new pskl.model.Piskel(width, height, fps, descriptor);
piskel.hiddenFrames = hiddenFrames;
var loadedLayers = 0; var loadedLayers = 0;
var loadLayerImage = function(layer, cb) { var loadLayerImage = function(layer, cb) {
@ -112,10 +113,6 @@
var frames = pskl.utils.FrameUtils.createFramesFromSpritesheet(this, layer.frameCount); var frames = pskl.utils.FrameUtils.createFramesFromSpritesheet(this, layer.frameCount);
frames.forEach(function (frame) { frames.forEach(function (frame) {
layer.model.addFrame(frame); layer.model.addFrame(frame);
var currentIndex = layer.model.getFrames().length - 1;
if (hiddenFrames.indexOf(currentIndex) != -1) {
frame.visible = false;
}
}); });
loadedLayers++; loadedLayers++;

View File

@ -102,14 +102,7 @@
} }
var frames = pskl.app.piskelController.getLayerAt(0).getFrames(); var frames = pskl.app.piskelController.getLayerAt(0).getFrames();
var hiddenFrames = frames.map(function (frame, index) { var hiddenFrames = piskel.hiddenFrames;
if (frame.visible) {
return -1;
}
return index;
}).filter(function (frameIndex) {
return frameIndex !== -1;
});
var serializedHiddenFrames = hiddenFrames.join('-'); var serializedHiddenFrames = hiddenFrames.join('-');
var bytes = ns.ArrayBufferSerializer.calculateRequiredBytes( var bytes = ns.ArrayBufferSerializer.calculateRequiredBytes(