2012-08-31 12:45:07 +04:00
|
|
|
/**
|
|
|
|
* @require Constants
|
|
|
|
* @require Events
|
|
|
|
*/
|
|
|
|
$.namespace("pskl");
|
|
|
|
|
|
|
|
(function () {
|
2012-08-27 04:05:13 +04:00
|
|
|
|
2012-09-03 03:24:55 +04:00
|
|
|
/**
|
|
|
|
* FrameSheetModel instance.
|
|
|
|
*/
|
|
|
|
var frameSheet,
|
2012-08-27 04:05:13 +04:00
|
|
|
|
2012-09-02 19:49:28 +04:00
|
|
|
// Temporary zoom implementation to easily get bigger canvases to
|
|
|
|
// see how good perform critical algorithms on big canvas.
|
|
|
|
zoom = 1,
|
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
// Configuration:
|
|
|
|
// Canvas size in pixel size (not dpi related)
|
2012-09-02 19:49:28 +04:00
|
|
|
framePixelWidth = 32 * zoom,
|
|
|
|
framePixelHeight = 32 * zoom,
|
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
|
|
|
|
// Scaling factors for a given frameSheet rendering:
|
|
|
|
// Main drawing area:
|
2012-09-02 19:49:28 +04:00
|
|
|
drawingCanvasDpi = Math.ceil(20/ zoom),
|
2012-08-27 04:05:13 +04:00
|
|
|
// Canvas previous in the slideshow:
|
2012-09-02 19:49:28 +04:00
|
|
|
previewTileCanvasDpi = Math.ceil(4 / zoom),
|
2012-08-27 04:05:13 +04:00
|
|
|
// Ainmated canvas preview:
|
2012-09-02 19:49:28 +04:00
|
|
|
previewAnimationCanvasDpi = Math.ceil(8 / zoom),
|
2012-08-27 04:05:13 +04:00
|
|
|
|
|
|
|
// DOM references:
|
|
|
|
drawingAreaContainer,
|
|
|
|
drawingAreaCanvas,
|
|
|
|
previewCanvas,
|
2012-09-03 03:24:55 +04:00
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
// States:
|
|
|
|
isClicked = false,
|
|
|
|
isRightClicked = false,
|
|
|
|
activeFrameIndex = -1,
|
|
|
|
animIndex = 0,
|
2012-09-03 03:24:55 +04:00
|
|
|
penColor = Constants.DEFAULT_PEN_COLOR,
|
2012-08-31 12:45:07 +04:00
|
|
|
currentFrame = null;
|
|
|
|
currentToolBehavior = null,
|
2012-09-02 19:49:28 +04:00
|
|
|
previousMousemoveTime = 0,
|
2012-08-29 22:34:34 +04:00
|
|
|
|
|
|
|
//utility
|
|
|
|
_normalizeColor = function (color) {
|
2012-08-31 12:45:07 +04:00
|
|
|
if(color == undefined || color == Constants.TRANSPARENT_COLOR || color.indexOf("#") == 0) {
|
2012-08-29 22:34:34 +04:00
|
|
|
return color;
|
|
|
|
} else {
|
|
|
|
return "#" + color;
|
|
|
|
}
|
2012-09-03 03:24:55 +04:00
|
|
|
};
|
2012-08-23 02:57:35 +04:00
|
|
|
|
2012-09-03 03:24:55 +04:00
|
|
|
/**
|
|
|
|
* Main application controller
|
|
|
|
*/
|
2012-08-23 02:57:35 +04:00
|
|
|
var piskel = {
|
2012-09-03 03:24:55 +04:00
|
|
|
|
2012-08-23 02:57:35 +04:00
|
|
|
init : function () {
|
2012-09-03 03:24:55 +04:00
|
|
|
frameSheet = pskl.FrameSheetModel.getInstance(framePixelWidth, framePixelHeight);
|
2012-08-30 03:16:13 +04:00
|
|
|
frameSheet.addEmptyFrame();
|
2012-08-31 12:45:07 +04:00
|
|
|
this.setActiveFrame(0);
|
2012-09-03 22:45:25 +04:00
|
|
|
|
2012-09-03 03:24:55 +04:00
|
|
|
pskl.NotificationService.init();
|
|
|
|
pskl.LocalStorageService.init(frameSheet);
|
|
|
|
|
|
|
|
// TODO: Add comments
|
2012-09-03 22:45:25 +04:00
|
|
|
var framesheetId = this.getFramesheetIdFromUrl();
|
|
|
|
if (framesheetId) {
|
|
|
|
$.publish(Events.SHOW_NOTIFICATION, [{"content": "Loading animation with id : [" + framesheetId + "]"}]);
|
|
|
|
this.loadFramesheetFromService(framesheetId);
|
2012-08-30 03:16:13 +04:00
|
|
|
} else {
|
|
|
|
this.finishInit();
|
2012-09-03 03:24:55 +04:00
|
|
|
pskl.LocalStorageService.displayRestoreNotification();
|
2012-08-30 03:16:13 +04:00
|
|
|
}
|
|
|
|
},
|
2012-08-27 04:05:13 +04:00
|
|
|
|
2012-08-30 03:16:13 +04:00
|
|
|
finishInit : function () {
|
2012-08-31 12:45:07 +04:00
|
|
|
|
|
|
|
$.subscribe(Events.TOOL_SELECTED, function(evt, toolBehavior) {
|
|
|
|
console.log("Tool selected: ", toolBehavior);
|
|
|
|
currentToolBehavior = toolBehavior;
|
|
|
|
});
|
|
|
|
|
2012-09-03 03:24:55 +04:00
|
|
|
$.subscribe(Events.COLOR_SELECTED, function(evt, color) {
|
|
|
|
console.log("Color selected: ", color);
|
|
|
|
penColor = color;
|
|
|
|
});
|
|
|
|
|
|
|
|
$.subscribe(Events.REFRESH, function() {
|
|
|
|
piskel.setActiveFrameAndRedraw(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: Move this into their service or behavior files:
|
2012-08-27 04:05:13 +04:00
|
|
|
this.initDrawingArea();
|
2012-09-03 03:39:21 +04:00
|
|
|
this.initPreviewSlideshow();
|
|
|
|
this.initAnimationPreview();
|
2012-08-30 03:16:13 +04:00
|
|
|
this.startAnimation();
|
2012-09-03 03:24:55 +04:00
|
|
|
|
|
|
|
pskl.ToolSelector.init();
|
2012-08-30 03:16:13 +04:00
|
|
|
},
|
|
|
|
|
2012-09-03 22:45:25 +04:00
|
|
|
getFramesheetIdFromUrl : function() {
|
2012-08-30 03:16:13 +04:00
|
|
|
var href = window.location.href;
|
2012-09-03 22:45:25 +04:00
|
|
|
// TODO: Change frameId to framesheetId on the backend
|
2012-08-30 03:16:13 +04:00
|
|
|
if (href.indexOf('frameId=') != -1) {
|
|
|
|
return href.substring(href.indexOf('frameId=')+8);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
loadFramesheetFromService : function (frameId) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
2012-09-03 22:45:25 +04:00
|
|
|
// TODO: Change frameId to framesheetId on the backend
|
2012-09-03 03:24:55 +04:00
|
|
|
xhr.open('GET', Constants.PISKEL_SERVICE_URL + '/get?l=' + frameId, true);
|
2012-08-30 03:16:13 +04:00
|
|
|
xhr.responseType = 'text';
|
|
|
|
|
|
|
|
xhr.onload = function(e) {
|
|
|
|
frameSheet.deserialize(this.responseText);
|
2012-09-03 22:45:25 +04:00
|
|
|
piskel.setActiveFrame(0);
|
2012-09-03 03:24:55 +04:00
|
|
|
$.publish(Events.HIDE_NOTIFICATION);
|
2012-08-30 03:16:13 +04:00
|
|
|
piskel.finishInit();
|
2012-09-03 22:45:25 +04:00
|
|
|
piskel.setActiveFrameAndRedraw(0);
|
2012-08-30 03:16:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = function () {
|
2012-09-03 03:24:55 +04:00
|
|
|
$.publish(Events.HIDE_NOTIFICATION);
|
2012-08-30 03:16:13 +04:00
|
|
|
piskel.finishInit();
|
2012-09-03 22:45:25 +04:00
|
|
|
piskel.setActiveFrameAndRedraw(0);
|
2012-08-30 03:16:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
2012-08-30 01:32:51 +04:00
|
|
|
},
|
2012-08-30 01:01:04 +04:00
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
setActiveFrame: function(index) {
|
|
|
|
activeFrameIndex = index;
|
2012-08-31 12:45:07 +04:00
|
|
|
currentFrame = frameSheet.getFrameByIndex(activeFrameIndex)
|
2012-08-27 04:05:13 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setActiveFrameAndRedraw: function(index) {
|
|
|
|
this.setActiveFrame(index);
|
|
|
|
|
2012-09-03 22:45:25 +04:00
|
|
|
// When redraw engine is refactored, remove the following crap and
|
|
|
|
// trigger an event instead:
|
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
// Update drawing canvas:
|
2012-09-03 22:45:25 +04:00
|
|
|
this.drawFrameToCanvas(currentFrame, drawingAreaCanvas, drawingCanvasDpi);
|
2012-08-27 04:05:13 +04:00
|
|
|
// Update slideshow:
|
|
|
|
this.createPreviews();
|
|
|
|
// Update animation preview:
|
|
|
|
animIndex = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
getActiveFrameIndex: function() {
|
|
|
|
if(-1 == activeFrameIndex) {
|
|
|
|
throw "Bad active frane initialization."
|
|
|
|
}
|
|
|
|
return activeFrameIndex;
|
|
|
|
},
|
|
|
|
|
|
|
|
initDrawingArea : function() {
|
2012-08-31 12:45:07 +04:00
|
|
|
drawingAreaContainer = $('#drawing-canvas-container')[0];
|
2012-08-27 04:05:13 +04:00
|
|
|
|
|
|
|
drawingAreaCanvas = document.createElement("canvas");
|
|
|
|
drawingAreaCanvas.className = 'canvas';
|
|
|
|
drawingAreaCanvas.setAttribute('width', '' + framePixelWidth * drawingCanvasDpi);
|
|
|
|
drawingAreaCanvas.setAttribute('height', '' + framePixelHeight * drawingCanvasDpi);
|
|
|
|
|
|
|
|
drawingAreaContainer.setAttribute('style',
|
|
|
|
'width:' + framePixelWidth * drawingCanvasDpi + 'px; height:' + framePixelHeight * drawingCanvasDpi + 'px;');
|
|
|
|
|
2012-08-29 10:39:03 +04:00
|
|
|
drawingAreaCanvas.setAttribute('oncontextmenu', 'piskel.onCanvasContextMenu(event)');
|
2012-08-27 04:05:13 +04:00
|
|
|
drawingAreaContainer.appendChild(drawingAreaCanvas);
|
|
|
|
|
|
|
|
var body = document.getElementsByTagName('body')[0];
|
2012-08-31 12:45:07 +04:00
|
|
|
body.setAttribute('onmouseup', 'piskel.onDocumentBodyMouseup(event)');
|
2012-08-29 10:39:03 +04:00
|
|
|
drawingAreaContainer.setAttribute('onmousedown', 'piskel.onCanvasMousedown(event)');
|
|
|
|
drawingAreaContainer.setAttribute('onmousemove', 'piskel.onCanvasMousemove(event)');
|
2012-08-31 12:45:07 +04:00
|
|
|
this.drawFrameToCanvas(currentFrame, drawingAreaCanvas, drawingCanvasDpi);
|
2012-08-27 04:05:13 +04:00
|
|
|
},
|
|
|
|
|
2012-08-30 01:32:51 +04:00
|
|
|
initPreviewSlideshow: function() {
|
2012-08-31 12:45:07 +04:00
|
|
|
var addFrameButton = $('#add-frame-button')[0];
|
2012-08-30 01:32:51 +04:00
|
|
|
addFrameButton.addEventListener('mousedown', function() {
|
|
|
|
frameSheet.addEmptyFrame();
|
|
|
|
piskel.setActiveFrameAndRedraw(frameSheet.getFrameCount() - 1);
|
|
|
|
});
|
|
|
|
this.createPreviews();
|
|
|
|
},
|
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
initAnimationPreview : function() {
|
2012-08-25 04:25:44 +04:00
|
|
|
|
2012-08-31 12:45:07 +04:00
|
|
|
var previewAnimationContainer = $('#preview-canvas-container')[0];
|
2012-08-27 04:05:13 +04:00
|
|
|
previewCanvas = document.createElement('canvas');
|
|
|
|
previewCanvas.className = 'canvas';
|
|
|
|
previewAnimationContainer.setAttribute('style',
|
|
|
|
'width:' + framePixelWidth * previewAnimationCanvasDpi + 'px; height:' + framePixelHeight * previewAnimationCanvasDpi + 'px;');
|
|
|
|
previewAnimationContainer.appendChild(previewCanvas);
|
|
|
|
previewCanvas.setAttribute('width', framePixelWidth * previewAnimationCanvasDpi);
|
|
|
|
previewCanvas.setAttribute('height', framePixelHeight * previewAnimationCanvasDpi);
|
2012-08-30 03:16:13 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
startAnimation : function () {
|
|
|
|
var scope = this;
|
2012-08-31 12:45:07 +04:00
|
|
|
var animFPSTuner = $("#preview-fps")[0];
|
2012-08-25 04:25:44 +04:00
|
|
|
var animPreviewFPS = parseInt(animFPSTuner.value, 10);
|
|
|
|
var startPreviewRefresh = function() {
|
|
|
|
return setInterval(scope.refreshAnimatedPreview, 1000/animPreviewFPS);
|
|
|
|
};
|
|
|
|
var refreshUpdater = startPreviewRefresh();
|
|
|
|
|
2012-08-29 15:46:57 +04:00
|
|
|
animFPSTuner.addEventListener('change', function(evt) {
|
2012-08-25 04:25:44 +04:00
|
|
|
window.clearInterval(refreshUpdater);
|
|
|
|
animPreviewFPS = parseInt(animFPSTuner.value, 10);
|
2012-08-31 12:45:07 +04:00
|
|
|
$("#display-fps").html(animPreviewFPS + " fps");
|
2012-08-25 04:25:44 +04:00
|
|
|
refreshUpdater = startPreviewRefresh();
|
|
|
|
});
|
2012-08-23 02:57:35 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
createPreviews : function () {
|
2012-08-31 12:45:07 +04:00
|
|
|
var container = $('#preview-list')[0], previewTile;
|
2012-08-23 02:57:35 +04:00
|
|
|
container.innerHTML = "";
|
2012-08-27 04:05:13 +04:00
|
|
|
for (var i = 0, l = frameSheet.getFrameCount(); i < l ; i++) {
|
2012-08-25 04:25:44 +04:00
|
|
|
previewTile = this.createPreviewTile(i);
|
|
|
|
container.appendChild(previewTile);
|
|
|
|
}
|
|
|
|
},
|
2012-08-23 02:57:35 +04:00
|
|
|
|
2012-08-25 04:25:44 +04:00
|
|
|
createPreviewTile: function(tileNumber) {
|
2012-08-27 04:05:13 +04:00
|
|
|
var previewTileRoot = document.createElement("li");
|
2012-08-25 04:25:44 +04:00
|
|
|
var classname = "preview-tile";
|
2012-08-23 02:57:35 +04:00
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
if (this.getActiveFrameIndex() == tileNumber) {
|
2012-08-25 04:25:44 +04:00
|
|
|
classname += " selected";
|
2012-08-23 02:57:35 +04:00
|
|
|
}
|
2012-08-27 04:05:13 +04:00
|
|
|
previewTileRoot.className = classname;
|
|
|
|
|
|
|
|
var canvasContainer = document.createElement("div");
|
|
|
|
canvasContainer.className = "canvas-container";
|
|
|
|
canvasContainer.setAttribute('style',
|
|
|
|
'width:' + framePixelWidth * previewTileCanvasDpi + 'px; height:' + framePixelHeight * previewTileCanvasDpi + 'px;');
|
|
|
|
|
|
|
|
var canvasBackground = document.createElement("div");
|
|
|
|
canvasBackground.className = "canvas-background";
|
|
|
|
canvasContainer.appendChild(canvasBackground);
|
2012-08-25 04:25:44 +04:00
|
|
|
|
|
|
|
var canvasPreview = document.createElement("canvas");
|
2012-08-27 04:05:13 +04:00
|
|
|
canvasPreview.className = "canvas tile-view"
|
2012-08-25 04:25:44 +04:00
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
canvasPreview.setAttribute('width', framePixelWidth * previewTileCanvasDpi);
|
|
|
|
canvasPreview.setAttribute('height', framePixelHeight * previewTileCanvasDpi);
|
|
|
|
|
|
|
|
previewTileRoot.addEventListener('click', function(evt) {
|
|
|
|
// has not class tile-action:
|
|
|
|
// TODO: let me know when you want to start using a framework :)
|
|
|
|
if(!evt.target.className.match(new RegExp('(\\s|^)'+ 'tile-action' +'(\\s|$)'))) {
|
|
|
|
piskel.setActiveFrameAndRedraw(tileNumber);
|
|
|
|
}
|
|
|
|
});
|
2012-08-25 04:25:44 +04:00
|
|
|
|
|
|
|
var canvasPreviewDuplicateAction = document.createElement("button");
|
|
|
|
canvasPreviewDuplicateAction.className = "tile-action"
|
|
|
|
canvasPreviewDuplicateAction.innerHTML = "dup"
|
2012-08-27 04:05:13 +04:00
|
|
|
|
|
|
|
canvasPreviewDuplicateAction.addEventListener('click', function(evt) {
|
|
|
|
piskel.duplicateFrame(tileNumber);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.drawFrameToCanvas(frameSheet.getFrameByIndex(tileNumber), canvasPreview, previewTileCanvasDpi);
|
|
|
|
canvasContainer.appendChild(canvasPreview);
|
|
|
|
previewTileRoot.appendChild(canvasContainer);
|
|
|
|
previewTileRoot.appendChild(canvasPreviewDuplicateAction);
|
2012-08-25 04:25:44 +04:00
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
if(tileNumber > 0 || frameSheet.getFrameCount() > 1) {
|
2012-08-25 04:25:44 +04:00
|
|
|
var canvasPreviewDeleteAction = document.createElement("button");
|
|
|
|
canvasPreviewDeleteAction.className = "tile-action"
|
|
|
|
canvasPreviewDeleteAction.innerHTML = "del"
|
2012-08-27 04:05:13 +04:00
|
|
|
canvasPreviewDeleteAction.addEventListener('click', function(evt) {
|
2012-08-29 01:30:55 +04:00
|
|
|
piskel.removeFrame(tileNumber);
|
2012-08-27 04:05:13 +04:00
|
|
|
});
|
|
|
|
previewTileRoot.appendChild(canvasPreviewDeleteAction);
|
2012-08-25 04:25:44 +04:00
|
|
|
}
|
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
return previewTileRoot;
|
2012-08-23 02:57:35 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
refreshAnimatedPreview : function () {
|
2012-08-27 04:05:13 +04:00
|
|
|
piskel.drawFrameToCanvas(frameSheet.getFrameByIndex(animIndex), previewCanvas, previewAnimationCanvasDpi);
|
|
|
|
animIndex++;
|
|
|
|
if (animIndex == frameSheet.getFrameCount()) {
|
2012-08-23 02:57:35 +04:00
|
|
|
animIndex = 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
removeFrame: function(frameIndex) {
|
|
|
|
frameSheet.removeFrameByIndex(frameIndex);
|
2012-08-23 02:57:35 +04:00
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
this.setActiveFrameAndRedraw(frameIndex - 1);
|
2012-08-25 04:25:44 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
duplicateFrame: function(frameIndex) {
|
2012-08-27 04:05:13 +04:00
|
|
|
frameSheet.duplicateFrameByIndex(frameIndex);
|
|
|
|
|
|
|
|
this.setActiveFrameAndRedraw(frameIndex + 1);
|
2012-08-25 04:25:44 +04:00
|
|
|
},
|
|
|
|
|
2012-08-23 02:57:35 +04:00
|
|
|
updateCursorInfo : function (event) {
|
|
|
|
var cursor = $('cursorInfo');
|
|
|
|
cursor.style.top = event.clientY + 10 + "px";
|
|
|
|
cursor.style.left = event.clientX + 10 + "px";
|
|
|
|
|
|
|
|
var coordinates = this.getRelativeCoordinates(event.clientX, event.clientY)
|
|
|
|
cursor.innerHTML = [
|
|
|
|
"X : " + coordinates.x,
|
|
|
|
"Y : " + coordinates.y
|
|
|
|
].join(", ");
|
|
|
|
},
|
|
|
|
|
|
|
|
onCanvasMousedown : function (event) {
|
|
|
|
isClicked = true;
|
2012-08-31 12:45:07 +04:00
|
|
|
|
|
|
|
if(event.button == 2) { // right click
|
2012-08-27 04:05:13 +04:00
|
|
|
isRightClicked = true;
|
2012-08-31 12:45:07 +04:00
|
|
|
$.publish(Events.CANVAS_RIGHT_CLICKED);
|
2012-08-27 04:05:13 +04:00
|
|
|
}
|
2012-08-31 12:45:07 +04:00
|
|
|
var spriteCoordinate = this.getSpriteCoordinate(event);
|
2012-09-02 02:44:55 +04:00
|
|
|
currentToolBehavior.applyToolAt(
|
|
|
|
spriteCoordinate.col,
|
|
|
|
spriteCoordinate.row,
|
|
|
|
currentFrame,
|
|
|
|
penColor,
|
|
|
|
drawingAreaCanvas,
|
|
|
|
drawingCanvasDpi);
|
2012-09-03 03:24:55 +04:00
|
|
|
|
|
|
|
$.publish(Events.LOCALSTORAGE_REQUEST);
|
2012-08-27 04:05:13 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
onCanvasMousemove : function (event) {
|
2012-08-31 12:45:07 +04:00
|
|
|
|
2012-08-27 04:05:13 +04:00
|
|
|
//this.updateCursorInfo(event);
|
2012-09-02 19:49:28 +04:00
|
|
|
var currentTime = new Date().getTime();
|
|
|
|
// Throttling of the mousemove event:
|
|
|
|
if ((currentTime - previousMousemoveTime) > 40 ) {
|
|
|
|
if (isClicked) {
|
|
|
|
var spriteCoordinate = this.getSpriteCoordinate(event);
|
|
|
|
currentToolBehavior.moveToolAt(
|
|
|
|
spriteCoordinate.col,
|
|
|
|
spriteCoordinate.row,
|
|
|
|
currentFrame,
|
|
|
|
penColor,
|
|
|
|
drawingAreaCanvas,
|
|
|
|
drawingCanvasDpi);
|
|
|
|
|
|
|
|
// TODO(vincz): Find a way to move that to the model instead of being at the interaction level.
|
|
|
|
// Eg when drawing, it may make sense to have it here. However for a non drawing tool,
|
|
|
|
// you don't need to draw anything when mousemoving and you request useless localStorage.
|
2012-09-03 03:24:55 +04:00
|
|
|
$.publish(Events.LOCALSTORAGE_REQUEST);
|
2012-09-02 19:49:28 +04:00
|
|
|
}
|
|
|
|
previousMousemoveTime = currentTime;
|
2012-08-27 04:05:13 +04:00
|
|
|
}
|
2012-08-23 02:57:35 +04:00
|
|
|
},
|
|
|
|
|
2012-08-31 12:45:07 +04:00
|
|
|
onDocumentBodyMouseup : function (event) {
|
2012-08-27 04:05:13 +04:00
|
|
|
if(isClicked || isRightClicked) {
|
|
|
|
// A mouse button was clicked on the drawing canvas before this mouseup event,
|
|
|
|
// the user was probably drawing on the canvas.
|
|
|
|
// Note: The mousemove movement (and the mouseup) may end up outside
|
|
|
|
// of the drawing canvas.
|
2012-09-02 02:44:55 +04:00
|
|
|
// TODO: Remove that when we have the centralized redraw loop
|
2012-08-27 04:05:13 +04:00
|
|
|
this.createPreviews();
|
|
|
|
}
|
2012-09-02 02:44:55 +04:00
|
|
|
|
2012-08-31 12:45:07 +04:00
|
|
|
if(isRightClicked) {
|
|
|
|
$.publish(Events.CANVAS_RIGHT_CLICK_RELEASED);
|
|
|
|
}
|
2012-08-23 02:57:35 +04:00
|
|
|
isClicked = false;
|
2012-08-27 04:05:13 +04:00
|
|
|
isRightClicked = false;
|
2012-09-02 02:44:55 +04:00
|
|
|
var spriteCoordinate = this.getSpriteCoordinate(event);
|
|
|
|
currentToolBehavior.releaseToolAt(
|
|
|
|
spriteCoordinate.col,
|
|
|
|
spriteCoordinate.row,
|
|
|
|
currentFrame,
|
|
|
|
penColor,
|
|
|
|
drawingAreaCanvas,
|
|
|
|
drawingCanvasDpi);
|
2012-08-23 02:57:35 +04:00
|
|
|
},
|
|
|
|
|
2012-08-31 12:45:07 +04:00
|
|
|
// TODO(vincz/julz): Refactor to make this disappear in a big event-driven redraw loop
|
|
|
|
drawFrameToCanvas: function(frame, canvasElement, dpi) {
|
|
|
|
var color;
|
|
|
|
for(var col = 0, num_col = frame.length; col < num_col; col++) {
|
|
|
|
for(var row = 0, num_row = frame[col].length; row < num_row; row++) {
|
|
|
|
color = _normalizeColor(frame[col][row]);
|
|
|
|
this.drawPixelInCanvas(row, col, color, canvasElement, dpi);
|
|
|
|
}
|
2012-08-30 01:01:04 +04:00
|
|
|
}
|
2012-08-29 22:34:34 +04:00
|
|
|
},
|
|
|
|
|
2012-08-31 12:45:07 +04:00
|
|
|
// TODO(vincz/julz): Refactor to make this disappear in a big event-driven redraw loop
|
2012-08-29 22:34:34 +04:00
|
|
|
drawPixelInCanvas : function (row, col, color, canvas, dpi) {
|
|
|
|
var context = canvas.getContext('2d');
|
2012-08-31 12:45:07 +04:00
|
|
|
if(color == undefined || color == Constants.TRANSPARENT_COLOR) {
|
2012-08-29 22:34:34 +04:00
|
|
|
context.clearRect(col * dpi, row * dpi, dpi, dpi);
|
|
|
|
} else {
|
|
|
|
context.fillStyle = color;
|
|
|
|
context.fillRect(col * dpi, row * dpi, dpi, dpi);
|
|
|
|
}
|
2012-08-27 04:05:13 +04:00
|
|
|
},
|
2012-08-23 02:57:35 +04:00
|
|
|
|
|
|
|
onCanvasContextMenu : function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.cancelBubble = true;
|
|
|
|
return false;
|
|
|
|
},
|
2012-08-29 10:39:03 +04:00
|
|
|
|
2012-08-23 02:57:35 +04:00
|
|
|
getRelativeCoordinates : function (x, y) {
|
2012-08-27 04:05:13 +04:00
|
|
|
var canvasRect = drawingAreaCanvas.getBoundingClientRect();
|
2012-08-23 02:57:35 +04:00
|
|
|
return {
|
|
|
|
x : x - canvasRect.left,
|
|
|
|
y : y - canvasRect.top
|
|
|
|
}
|
2012-08-30 03:16:13 +04:00
|
|
|
},
|
|
|
|
|
2012-08-31 12:45:07 +04:00
|
|
|
getSpriteCoordinate : function(event) {
|
|
|
|
var coord = this.getRelativeCoordinates(event.x, event.y);
|
|
|
|
var coords = this.getRelativeCoordinates(event.clientX, event.clientY);
|
|
|
|
return {
|
|
|
|
"col" : (coords.x - coords.x%drawingCanvasDpi) / drawingCanvasDpi,
|
|
|
|
"row" : (coords.y - coords.y%drawingCanvasDpi) / drawingCanvasDpi
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO(julz): Create package ?
|
2012-08-30 03:16:13 +04:00
|
|
|
storeSheet : function (event) {
|
2012-09-03 03:24:55 +04:00
|
|
|
// TODO Refactor using jquery ?
|
2012-08-30 03:16:13 +04:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
var formData = new FormData();
|
|
|
|
formData.append('framesheet_content', frameSheet.serialize());
|
2012-08-31 12:45:07 +04:00
|
|
|
formData.append('fps_speed', $('#preview-fps').val());
|
2012-09-03 03:24:55 +04:00
|
|
|
xhr.open('POST', Constants.PISKEL_SERVICE_URL + "/store", true);
|
2012-08-30 03:16:13 +04:00
|
|
|
xhr.onload = function(e) {
|
|
|
|
if (this.status == 200) {
|
2012-08-30 03:26:14 +04:00
|
|
|
var baseUrl = window.location.href.replace(window.location.search, "");
|
|
|
|
window.location.href = baseUrl + "?frameId=" + this.responseText;
|
2012-08-30 03:16:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(formData);
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
2012-08-23 02:57:35 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.piskel = piskel;
|
|
|
|
piskel.init();
|
|
|
|
|
2012-09-03 03:24:55 +04:00
|
|
|
})();
|