moved Frame and FrameSheet to model package

This commit is contained in:
juliandescottes 2012-09-04 22:40:54 +02:00
parent 69a03a3416
commit a567992a1d
6 changed files with 94 additions and 116 deletions

View File

@ -73,10 +73,10 @@
<script src="js/lib/jsColor_1_4_0/jscolor.js"></script>
<!-- Application libraries-->
<script src="js/rendering/Frame.js"></script>
<script src="js/rendering/drawingController.js"></script>
<script src="js/model/Frame.js"></script>
<script src="js/model/FrameSheet.js"></script>
<script src="js/controller/DrawingController.js"></script>
<script src="js/rendering/FrameRenderer.js"></script>
<script src="js/FrameSheetModel.js"></script>
<script src="js/LocalStorageService.js"></script>
<script src="js/Palette.js"></script>
<script src="js/Notification.js"></script>

View File

@ -1,105 +0,0 @@
$.namespace("pskl");
pskl.FrameSheetModel = (function() {
var inst;
var frames = [];
var width;
var height;
/**
* @private
*/
var requestLocalStorageSave_ = function() {
};
return {
validate: function() {
return true; // I'm always right dude
},
getUsedColors: function() {
var colors = {};
for (var frameIndex=0; frameIndex < frames.length; frameIndex++) {
var frame = 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
serialize: function() {
return JSON.stringify(frames);
},
/**
* Load a framesheet from a string that might have been persisted in db / localstorage
* Overrides existing frames.
* @param {String} serialized
*/
deserialize : function (serialized) {
try {
frames = JSON.parse(serialized);
$.publish(Events.FRAMESHEET_RESET);
} catch (e) {
throw "Could not load serialized framesheet." + e.message
}
},
addEmptyFrame : function () {
this.addFrame(pskl.rendering.Frame.createEmpty(width, height));
},
addFrame: function(frame) {
frames.push(frame);
},
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(index) {
var frame = inst.getFrameByIndex(index);
frames.splice(index + 1, 0, frame.clone());
},
getInstance: function(width_, height_) {
if (isNaN(width_) || isNaN(height_)) {
throw "Bad FrameSheetModel initialization in getInstance method.";
}
inst = this;
width = width_;
height = height_;
return inst;
}
}
})();

View File

@ -1,17 +1,17 @@
(function () {
var ns = $.namespace("pskl.rendering");
var ns = $.namespace("pskl.controller");
ns.DrawingController = function (frame, container, dpi) {
this.dpi = dpi;
// Public
this.frame = frame;
this.overlay = ns.Frame.createEmptyFromFrame(frame);
this.overlay = pskl.model.Frame.createEmptyFromFrame(frame);
// Private
this.container = container;
this.mainCanvas = this.createMainCanvas();
this.overlayCanvas = this.createOverlayCanvas();
this.renderer = new ns.FrameRenderer();
this.renderer = new pskl.rendering.FrameRenderer();
};
ns.DrawingController.prototype.renderFrame = function () {
@ -27,7 +27,7 @@
};
ns.DrawingController.prototype.clearOverlay = function () {
this.overlay = ns.Frame.createEmptyFromFrame(this.frame);
this.overlay = pskl.model.Frame.createEmptyFromFrame(this.frame);
this.overlayCanvas.getContext("2d").clearRect(0, 0, this.overlayCanvas.width, this.overlayCanvas.height);
};

View File

@ -1,5 +1,5 @@
(function () {
var ns = $.namespace("pskl.rendering");
var ns = $.namespace("pskl.model");
ns.Frame = function (pixels) {
this.pixels = pixels;
};

83
js/model/FrameSheet.js Normal file
View File

@ -0,0 +1,83 @@
(function () {
var ns = $.namespace("pskl.model");
ns.FrameSheet = function () {
this.frames = [];
};
ns.FrameSheet.prototype.validate = function () {
throw "FrameSheet.prototype.validate not implemented"
};
ns.FrameSheet.prototype.addEmptyFrame = function () {
this.addFrame(ns.Frame.createEmpty(width, height));
};
ns.FrameSheet.prototype.addFrame = function (frame) {
this.frames.push(frame);
};
ns.FrameSheet.prototype.getFrameCount = function () {
return this.frames.length;
};
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() {
throw "FrameSheet.prototype.serialize not implemented"
//return JSON.stringify(frames);
};
/**
* Load a framesheet from a string that might have been persisted in db / localstorage
* Overrides existing frames.
* @param {String} serialized
*/
ns.FrameSheet.prototype.deserialize = function (serialized) {
throw "FrameSheet.prototype.deserialize not implemented"
// try {
// frames = JSON.parse(serialized);
// $.publish(Events.FRAMESHEET_RESET);
// } catch (e) {
// throw "Could not load serialized framesheet." + e.message
// }
};
ns.FrameSheet.prototype.getFrameByIndex = function(index) {
if (isNaN(index)) {
throw "Bad argument value for getFrameByIndex method: <" + index + ">";
}
if (index < 0 || index > this.getFrameCount()) {
throw "Out of bound index for frameSheet object.";
}
return this.frames[index];
};
ns.FrameSheet.prototype.removeFrameByIndex = function(index) {
if(index < 0 || index > this.getFrameCount()) {
throw "Out of bound index for frameSheet object.";
}
frames.splice(index, 1);
};
ns.FrameSheet.prototype.duplicateFrameByIndex = function(index) {
var frame = this.getFrameByIndex(index);
this.frames.splice(index + 1, 0, frame.clone());
};
})();

View File

@ -50,9 +50,9 @@ $.namespace("pskl");
var piskel = {
init : function () {
var emptyFrame = pskl.rendering.Frame.createEmpty(framePixelWidth, framePixelHeight);
var emptyFrame = pskl.model.Frame.createEmpty(framePixelWidth, framePixelHeight);
this.drawingController = new pskl.rendering.DrawingController(
this.drawingController = new pskl.controller.DrawingController(
emptyFrame,
$('#drawing-canvas-container')[0],
drawingCanvasDpi
@ -60,7 +60,7 @@ $.namespace("pskl");
renderer = new pskl.rendering.FrameRenderer();
frameSheet = pskl.FrameSheetModel.getInstance(framePixelWidth, framePixelHeight);
frameSheet = new pskl.model.FrameSheet();
frameSheet.addFrame(emptyFrame);
this.setActiveFrame(0);