2012-09-06 22:18:59 +00:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.model");
|
|
|
|
|
|
|
|
ns.Frame = function (pixels) {
|
2012-09-08 15:54:41 +00:00
|
|
|
this.pixels = pixels;
|
2012-09-08 14:50:29 +00:00
|
|
|
this.previousStates = [this.getPixels()];
|
2012-09-06 22:18:59 +00:00
|
|
|
this.stateIndex = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.createEmpty = function (width, height) {
|
2013-05-27 21:42:53 +00:00
|
|
|
var pixels = ns.Frame.createEmptyPixelGrid_(width, height);
|
2012-09-10 21:32:34 +00:00
|
|
|
return new ns.Frame(pixels);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.createEmptyPixelGrid_ = function (width, height) {
|
2012-09-06 22:18:59 +00:00
|
|
|
var pixels = []; //new Array(width);
|
|
|
|
for (var columnIndex=0; columnIndex < width; columnIndex++) {
|
|
|
|
var columnArray = [];
|
|
|
|
for(var heightIndex = 0; heightIndex < height; heightIndex++) {
|
|
|
|
columnArray.push(Constants.TRANSPARENT_COLOR);
|
|
|
|
}
|
|
|
|
pixels[columnIndex] = columnArray;
|
2013-05-27 21:42:53 +00:00
|
|
|
}
|
2012-09-10 21:32:34 +00:00
|
|
|
return pixels;
|
2012-09-06 22:18:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.createEmptyFromFrame = function (frame) {
|
|
|
|
return ns.Frame.createEmpty(frame.getWidth(), frame.getHeight());
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.clone = function () {
|
2012-09-08 15:41:09 +00:00
|
|
|
return new ns.Frame(this.getPixels());
|
2012-09-06 22:18:59 +00:00
|
|
|
};
|
|
|
|
|
2012-09-08 14:50:29 +00:00
|
|
|
/**
|
|
|
|
* Returns a copy of the pixels used by the frame
|
|
|
|
*/
|
|
|
|
ns.Frame.prototype.getPixels = function () {
|
2013-05-27 21:42:53 +00:00
|
|
|
return this.clonePixels_(this.pixels);
|
2012-09-08 14:50:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the passed pixels into the frame.
|
|
|
|
*/
|
|
|
|
ns.Frame.prototype.setPixels = function (pixels) {
|
|
|
|
this.pixels = this.clonePixels_(pixels);
|
|
|
|
};
|
|
|
|
|
2012-09-10 21:32:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
ns.Frame.prototype.clear = function () {
|
|
|
|
var pixels = ns.Frame.createEmptyPixelGrid_(this.getWidth(), this.getHeight());
|
|
|
|
this.setPixels(pixels);
|
|
|
|
};
|
|
|
|
|
2012-09-08 14:50:29 +00:00
|
|
|
/**
|
|
|
|
* Clone a set of pixels. Should be static utility method
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.Frame.prototype.clonePixels_ = function (pixels) {
|
|
|
|
var clonedPixels = [];
|
|
|
|
for (var col = 0 ; col < pixels.length ; col++) {
|
2012-09-08 16:15:53 +00:00
|
|
|
clonedPixels[col] = pixels[col].slice(0 , pixels[col].length);
|
2012-09-06 22:18:59 +00:00
|
|
|
}
|
2012-09-08 14:50:29 +00:00
|
|
|
return clonedPixels;
|
2012-09-06 22:18:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.serialize = function () {
|
|
|
|
return JSON.stringify(this.pixels);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.setPixel = function (col, row, color) {
|
|
|
|
this.pixels[col][row] = color;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.getPixel = function (col, row) {
|
|
|
|
return this.pixels[col][row];
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.getWidth = function () {
|
|
|
|
return this.pixels.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.getHeight = function () {
|
|
|
|
return this.pixels[0].length;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.containsPixel = function (col, row) {
|
2012-09-06 22:56:31 +00:00
|
|
|
return col >= 0 && row >= 0 && col < this.pixels.length && row < this.pixels[0].length;
|
2012-09-06 22:18:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.saveState = function () {
|
|
|
|
// remove all states past current state
|
|
|
|
this.previousStates.length = this.stateIndex + 1;
|
|
|
|
// push new state
|
2012-09-08 14:50:29 +00:00
|
|
|
this.previousStates.push(this.getPixels());
|
2012-09-06 22:18:59 +00:00
|
|
|
// set the stateIndex to latest saved state
|
|
|
|
this.stateIndex = this.previousStates.length - 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.loadPreviousState = function () {
|
2012-09-06 22:56:31 +00:00
|
|
|
if (this.stateIndex > 0) {
|
2012-09-06 22:18:59 +00:00
|
|
|
this.stateIndex--;
|
2012-09-08 14:50:29 +00:00
|
|
|
this.setPixels(this.previousStates[this.stateIndex]);
|
2012-09-06 22:56:31 +00:00
|
|
|
}
|
2012-09-06 22:18:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.Frame.prototype.loadNextState = function () {
|
|
|
|
if (this.stateIndex < this.previousStates.length - 1) {
|
|
|
|
this.stateIndex++;
|
2012-09-08 14:50:29 +00:00
|
|
|
this.setPixels(this.previousStates[this.stateIndex]);
|
2012-09-06 22:18:59 +00:00
|
|
|
}
|
|
|
|
};
|
2012-09-20 21:57:26 +00:00
|
|
|
|
|
|
|
ns.Frame.prototype.isSameSize = function (otherFrame) {
|
|
|
|
return this.getHeight() == otherFrame.getHeight() && this.getWidth() == otherFrame.getWidth();
|
|
|
|
};
|
2012-09-04 12:10:16 +00:00
|
|
|
})();
|