2012-08-31 12:45:07 +04:00
|
|
|
|
2012-09-03 03:24:55 +04:00
|
|
|
$.namespace("pskl");
|
|
|
|
|
|
|
|
pskl.FrameSheetModel = (function() {
|
2012-08-27 04:05:13 +04:00
|
|
|
|
|
|
|
var inst;
|
|
|
|
var frames = [];
|
|
|
|
var width;
|
|
|
|
var height;
|
|
|
|
|
2012-08-31 12:45:07 +04:00
|
|
|
/**
|
2012-09-03 22:45:25 +04:00
|
|
|
* Create empty frame of dimension [width * height] with Constants.TRANSPARENT_COLOR
|
|
|
|
* as a default value.
|
|
|
|
*
|
2012-08-31 12:45:07 +04:00
|
|
|
* @private
|
|
|
|
*/
|
2012-08-27 04:05:13 +04:00
|
|
|
var createEmptyFrame_ = function() {
|
2012-09-03 22:45:25 +04:00
|
|
|
var emptyFrame = []; //new Array(width);
|
2012-08-27 04:05:13 +04:00
|
|
|
for (var columnIndex=0; columnIndex < width; columnIndex++) {
|
2012-09-03 22:45:25 +04:00
|
|
|
var columnArray = [];
|
|
|
|
for(var heightIndex = 0; heightIndex < height; heightIndex++) {
|
|
|
|
columnArray.push(Constants.TRANSPARENT_COLOR);
|
|
|
|
}
|
|
|
|
emptyFrame[columnIndex] = columnArray;
|
2012-08-27 04:05:13 +04:00
|
|
|
}
|
2012-09-03 22:45:25 +04:00
|
|
|
return emptyFrame;
|
2012-08-31 12:45:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var requestLocalStorageSave_ = function() {
|
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
validate: function() {
|
|
|
|
return true; // I'm always right dude
|
|
|
|
},
|
|
|
|
|
2012-08-29 12:15:18 +04:00
|
|
|
getAllPixels : function () {
|
|
|
|
var pixels = [];
|
|
|
|
for (var i = 0 ; i < frames.length ; i++) {
|
2012-08-29 12:46:25 +04:00
|
|
|
pixels = pixels.concat(this._getFramePixels(frames[i]));
|
2012-08-29 12:15:18 +04:00
|
|
|
}
|
|
|
|
return pixels;
|
|
|
|
},
|
|
|
|
|
2012-08-29 12:46:25 +04:00
|
|
|
_getFramePixels : function (frame) {
|
2012-08-29 12:15:18 +04:00
|
|
|
var pixels = [];
|
|
|
|
for (var i = 0 ; i < frame.length ; i++) {
|
|
|
|
var line = frame[i];
|
|
|
|
for (var j = 0 ; j < line.length ; j++) {
|
|
|
|
pixels.push(line[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pixels;
|
2012-08-29 10:39:03 +04:00
|
|
|
},
|
|
|
|
|
2012-09-04 00:30:53 +04:00
|
|
|
getUsedColors: function() {
|
|
|
|
var colors = {};
|
|
|
|
for (var frameIndex=0; frameIndex < frames.length; frameIndex++) {
|
|
|
|
var currentFrame = frames[frameIndex];
|
|
|
|
for (var i = 0 ; i < currentFrame.length ; i++) {
|
|
|
|
var line = currentFrame[i];
|
|
|
|
for (var j = 0 ; j < line.length ; j++) {
|
|
|
|
colors[line[j]] = line[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return colors;
|
|
|
|
},
|
|
|
|
|
2012-08-29 02:57:55 +04:00
|
|
|
// Could be used to pass around model using long GET param (good enough for simple models) and
|
2012-08-27 04:05:13 +04:00
|
|
|
// do some temporary locastorage
|
|
|
|
serialize: function() {
|
2012-08-29 02:57:55 +04:00
|
|
|
return JSON.stringify(frames);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a framesheet from a string that might have been persisted in db / localstorage
|
|
|
|
* Overrides existing frames.
|
|
|
|
* @param {String} serialized
|
|
|
|
*/
|
2012-08-29 03:00:45 +04:00
|
|
|
deserialize : function (serialized) {
|
2012-08-29 02:57:55 +04:00
|
|
|
try {
|
2012-08-29 03:14:28 +04:00
|
|
|
frames = JSON.parse(serialized);
|
2012-09-04 00:30:53 +04:00
|
|
|
$.publish(Events.FRAMESHEET_RESET);
|
2012-08-29 02:57:55 +04:00
|
|
|
} catch (e) {
|
2012-08-29 21:32:23 +04:00
|
|
|
throw "Could not load serialized framesheet." + e.message
|
2012-08-29 02:57:55 +04:00
|
|
|
}
|
2012-08-27 04:05:13 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
addEmptyFrame: function() {
|
|
|
|
frames.push(createEmptyFrame_());
|
|
|
|
},
|
|
|
|
|
|
|
|
getFrameCount: function() {
|
|
|
|
return frames.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
getFrameByIndex: function(index) {
|
|
|
|
if (isNaN(index)) {
|
|
|
|
throw "Bad argument value for getFrameByIndex method: <" + index + ">"
|
|
|
|
} else if (index < 0 || index > frames.length) {
|
|
|
|
throw "Out of bound index for frameSheet object."
|
|
|
|
}
|
|
|
|
|
|
|
|
return frames[index];
|
|
|
|
},
|
|
|
|
|
|
|
|
removeFrameByIndex: function(index) {
|
|
|
|
if(index < 0 || index > inst.getFrameCount()) {
|
|
|
|
throw "Bad index value for removeFrameByIndex.";
|
|
|
|
}
|
|
|
|
frames.splice(index, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
duplicateFrameByIndex: function(frameToDuplicateIndex) {
|
|
|
|
var frame = inst.getFrameByIndex(frameToDuplicateIndex);
|
|
|
|
var clonedFrame = [];
|
|
|
|
for(var i=0, l=frame.length; i<l; i++) {
|
|
|
|
clonedFrame.push(frame[i].slice(0));
|
|
|
|
}
|
|
|
|
frames.splice(frameToDuplicateIndex + 1, 0, clonedFrame);
|
|
|
|
},
|
|
|
|
|
|
|
|
getInstance: function(width_, height_) {
|
|
|
|
|
|
|
|
if (isNaN(width_) || isNaN(height_)) {
|
|
|
|
throw "Bad FrameSheetModel initialization in getInstance method.";
|
|
|
|
}
|
|
|
|
|
|
|
|
inst = this;
|
|
|
|
|
|
|
|
width = width_;
|
|
|
|
height = height_;
|
|
|
|
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|