2012-09-05 00:40:54 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.model");
|
2012-09-16 20:48:32 +04:00
|
|
|
ns.FrameSheet = function (height, width) {
|
2012-09-06 02:23:24 +04:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2012-09-05 00:40:54 +04:00
|
|
|
this.frames = [];
|
2012-09-11 01:26:12 +04:00
|
|
|
this.currentFrameIndex = 0;
|
2012-09-05 00:40:54 +04:00
|
|
|
};
|
|
|
|
|
2012-09-20 02:43:39 +04:00
|
|
|
ns.FrameSheet.prototype.getHeight = function () {
|
|
|
|
return this.height;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameSheet.prototype.getWidth = function () {
|
|
|
|
return this.width;
|
|
|
|
};
|
|
|
|
|
2012-09-05 00:40:54 +04:00
|
|
|
ns.FrameSheet.prototype.addEmptyFrame = function () {
|
2012-09-06 02:23:24 +04:00
|
|
|
this.addFrame(ns.Frame.createEmpty(this.width, this.height));
|
2012-09-05 00:40:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameSheet.prototype.addFrame = function (frame) {
|
|
|
|
this.frames.push(frame);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameSheet.prototype.getFrameCount = function () {
|
|
|
|
return this.frames.length;
|
|
|
|
};
|
|
|
|
|
2012-09-11 01:26:12 +04:00
|
|
|
ns.FrameSheet.prototype.getCurrentFrame = function () {
|
|
|
|
return this.frames[this.currentFrameIndex];
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameSheet.prototype.setCurrentFrameIndex = function (index) {
|
|
|
|
this.currentFrameIndex = index;
|
2012-09-12 14:01:47 +04:00
|
|
|
$.publish(Events.CURRENT_FRAME_SET, [this.getCurrentFrame()]);
|
|
|
|
$.publish(Events.FRAMESHEET_RESET); // Is it no to overkill to have this here ?
|
2012-09-11 01:26:12 +04:00
|
|
|
};
|
|
|
|
|
2012-09-05 00:40:54 +04:00
|
|
|
ns.FrameSheet.prototype.getUsedColors = function() {
|
|
|
|
var colors = {};
|
|
|
|
for (var frameIndex=0; frameIndex < this.frames.length; frameIndex++) {
|
|
|
|
var frame = this.frames[frameIndex];
|
|
|
|
for (var i = 0, width = frame.getWidth(); i < width ; i++) {
|
|
|
|
var line = frame[i];
|
|
|
|
for (var j = 0, height = frame.getHeight() ; j < height ; j++) {
|
|
|
|
var pixel = frame.getPixel(i, j);
|
|
|
|
colors[pixel] = pixel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return colors;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Could be used to pass around model using long GET param (good enough for simple models) and
|
|
|
|
// do some temporary locastorage
|
|
|
|
ns.FrameSheet.prototype.serialize = function() {
|
2012-09-05 02:09:42 +04:00
|
|
|
var serializedFrames = [];
|
|
|
|
for (var i = 0 ; i < this.frames.length ; i++) {
|
|
|
|
serializedFrames.push(this.frames[i].serialize());
|
|
|
|
}
|
|
|
|
return '[' + serializedFrames.join(",") + ']';
|
2012-09-05 00:40:54 +04:00
|
|
|
//return JSON.stringify(frames);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-09-21 01:57:26 +04:00
|
|
|
* Load a framesheet from a model that might have been persisted in db / localstorage
|
2012-09-05 00:40:54 +04:00
|
|
|
* Overrides existing frames.
|
|
|
|
* @param {String} serialized
|
|
|
|
*/
|
|
|
|
ns.FrameSheet.prototype.deserialize = function (serialized) {
|
2012-09-05 02:09:42 +04:00
|
|
|
try {
|
2013-05-28 01:42:53 +04:00
|
|
|
this.load(JSON.parse(serialized));
|
2012-09-05 02:09:42 +04:00
|
|
|
} catch (e) {
|
2013-05-28 01:42:53 +04:00
|
|
|
throw "Could not load serialized framesheet : " + e.message;
|
2012-09-05 02:09:42 +04:00
|
|
|
}
|
2012-09-05 00:40:54 +04:00
|
|
|
};
|
|
|
|
|
2012-09-21 01:57:26 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a framesheet from a model that might have been persisted in db / localstorage
|
|
|
|
* Overrides existing frames.
|
|
|
|
* @param {String} serialized
|
|
|
|
*/
|
|
|
|
ns.FrameSheet.prototype.load = function (framesheet) {
|
2013-05-28 01:42:53 +04:00
|
|
|
this.frames = [];
|
|
|
|
for (var i = 0 ; i < framesheet.length ; i++) {
|
|
|
|
var frameCfg = framesheet[i];
|
|
|
|
this.addFrame(new ns.Frame(frameCfg));
|
|
|
|
}
|
2012-09-21 01:57:26 +04:00
|
|
|
|
2013-05-28 01:42:53 +04:00
|
|
|
if (this.hasFrameAtIndex(0)) {
|
|
|
|
this.height = this.getFrameByIndex(0).getHeight();
|
|
|
|
this.width = this.getFrameByIndex(0).getWidth();
|
|
|
|
this.setCurrentFrameIndex(0);
|
|
|
|
$.publish(Events.FRAME_SIZE_CHANGED);
|
|
|
|
}
|
2012-09-21 01:57:26 +04:00
|
|
|
|
2013-05-28 01:42:53 +04:00
|
|
|
$.publish(Events.FRAMESHEET_RESET);
|
2012-09-21 01:57:26 +04:00
|
|
|
};
|
|
|
|
|
2012-09-05 02:42:25 +04:00
|
|
|
|
|
|
|
ns.FrameSheet.prototype.hasFrameAtIndex = function(index) {
|
|
|
|
return (index >= 0 && index < this.getFrameCount());
|
|
|
|
};
|
|
|
|
|
2012-09-05 00:40:54 +04:00
|
|
|
ns.FrameSheet.prototype.getFrameByIndex = function(index) {
|
|
|
|
if (isNaN(index)) {
|
|
|
|
throw "Bad argument value for getFrameByIndex method: <" + index + ">";
|
|
|
|
}
|
|
|
|
|
2012-09-05 02:42:25 +04:00
|
|
|
if (!this.hasFrameAtIndex(index)) {
|
2012-09-05 00:40:54 +04:00
|
|
|
throw "Out of bound index for frameSheet object.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.frames[index];
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameSheet.prototype.removeFrameByIndex = function(index) {
|
2012-09-05 02:42:25 +04:00
|
|
|
if(!this.hasFrameAtIndex(index)) {
|
2012-09-05 00:40:54 +04:00
|
|
|
throw "Out of bound index for frameSheet object.";
|
|
|
|
}
|
2012-09-05 02:42:25 +04:00
|
|
|
this.frames.splice(index, 1);
|
2012-09-15 01:43:49 +04:00
|
|
|
|
|
|
|
// Current frame index might not be valid anymore
|
|
|
|
if (!this.hasFrameAtIndex(this.currentFrameIndex)) {
|
|
|
|
// if not select last frame available
|
|
|
|
this.setCurrentFrameIndex(this.getFrameCount() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$.publish(Events.FRAMESHEET_RESET);
|
2012-09-05 00:40:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameSheet.prototype.duplicateFrameByIndex = function(index) {
|
|
|
|
var frame = this.getFrameByIndex(index);
|
|
|
|
this.frames.splice(index + 1, 0, frame.clone());
|
|
|
|
};
|
2012-09-08 01:14:25 +04:00
|
|
|
|
2012-09-08 04:50:59 +04:00
|
|
|
ns.FrameSheet.prototype.moveFrame = function(originIndex, destinationIndex) {
|
2013-06-13 02:04:39 +04:00
|
|
|
this.frames.splice(destinationIndex, 0, this.frames.splice(originIndex, 1)[0]);
|
2012-09-08 04:50:59 +04:00
|
|
|
};
|
|
|
|
|
2012-09-08 01:14:25 +04:00
|
|
|
ns.FrameSheet.prototype.swapFrames = function(indexFrame1, indexFrame2) {
|
|
|
|
if(isNaN(indexFrame1) || isNaN(indexFrame1) ||
|
2013-05-28 01:42:53 +04:00
|
|
|
(!this.hasFrameAtIndex(indexFrame1) && !this.hasFrameAtIndex(indexFrame2))) {
|
|
|
|
throw "Bad indexes for swapFrames Framesheet function.";
|
2012-09-08 01:14:25 +04:00
|
|
|
}
|
|
|
|
if(indexFrame1 == indexFrame2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var swapFrame = this.frames[indexFrame1];
|
|
|
|
this.frames[indexFrame1] = this.frames[indexFrame2];
|
|
|
|
this.frames[indexFrame2] = swapFrame;
|
|
|
|
}
|
|
|
|
};
|
2012-09-05 00:40:54 +04:00
|
|
|
})();
|