2012-08-31 08:45:07 +00:00
|
|
|
/**
|
|
|
|
* @require Constants
|
|
|
|
* @require Events
|
|
|
|
*/
|
|
|
|
$.namespace("pskl");
|
|
|
|
|
|
|
|
(function () {
|
2012-08-27 00:05:13 +00:00
|
|
|
|
2012-09-02 23:24:55 +00:00
|
|
|
/**
|
|
|
|
* FrameSheetModel instance.
|
|
|
|
*/
|
2012-09-04 22:09:42 +00:00
|
|
|
var frameSheet,
|
2012-08-27 00:05:13 +00:00
|
|
|
|
2012-09-02 15:49:28 +00:00
|
|
|
// Temporary zoom implementation to easily get bigger canvases to
|
|
|
|
// see how good perform critical algorithms on big canvas.
|
|
|
|
zoom = 1,
|
|
|
|
|
2012-08-27 00:05:13 +00:00
|
|
|
// Configuration:
|
|
|
|
// Canvas size in pixel size (not dpi related)
|
2012-09-02 15:49:28 +00:00
|
|
|
framePixelWidth = 32 * zoom,
|
|
|
|
framePixelHeight = 32 * zoom,
|
|
|
|
|
2012-08-27 00:05:13 +00:00
|
|
|
|
|
|
|
// Scaling factors for a given frameSheet rendering:
|
|
|
|
// Main drawing area:
|
2012-09-02 15:49:28 +00:00
|
|
|
drawingCanvasDpi = Math.ceil(20/ zoom),
|
2012-08-27 00:05:13 +00:00
|
|
|
// Canvas previous in the slideshow:
|
2012-09-02 15:49:28 +00:00
|
|
|
previewTileCanvasDpi = Math.ceil(4 / zoom),
|
2012-08-27 00:05:13 +00:00
|
|
|
// Ainmated canvas preview:
|
2012-09-02 15:49:28 +00:00
|
|
|
previewAnimationCanvasDpi = Math.ceil(8 / zoom),
|
2012-08-27 00:05:13 +00:00
|
|
|
|
|
|
|
// DOM references:
|
|
|
|
drawingAreaContainer,
|
|
|
|
drawingAreaCanvas,
|
|
|
|
previewCanvas,
|
2012-09-02 23:24:55 +00:00
|
|
|
|
2012-08-27 00:05:13 +00:00
|
|
|
// States:
|
|
|
|
isClicked = false,
|
|
|
|
isRightClicked = false,
|
|
|
|
activeFrameIndex = -1,
|
|
|
|
animIndex = 0,
|
2012-09-02 23:24:55 +00:00
|
|
|
penColor = Constants.DEFAULT_PEN_COLOR,
|
2012-08-31 08:45:07 +00:00
|
|
|
currentFrame = null;
|
|
|
|
currentToolBehavior = null,
|
2012-09-03 20:31:56 +00:00
|
|
|
previousMousemoveTime = 0;
|
2012-08-22 22:57:35 +00:00
|
|
|
|
2012-09-02 23:24:55 +00:00
|
|
|
/**
|
|
|
|
* Main application controller
|
|
|
|
*/
|
2012-08-22 22:57:35 +00:00
|
|
|
var piskel = {
|
2012-09-02 23:24:55 +00:00
|
|
|
|
2012-08-22 22:57:35 +00:00
|
|
|
init : function () {
|
2012-09-05 22:23:24 +00:00
|
|
|
frameSheet = new pskl.model.FrameSheet(framePixelWidth, framePixelHeight);
|
|
|
|
frameSheet.addEmptyFrame();
|
2012-09-04 22:09:42 +00:00
|
|
|
|
2012-09-04 20:40:54 +00:00
|
|
|
this.drawingController = new pskl.controller.DrawingController(
|
2012-09-05 22:23:24 +00:00
|
|
|
frameSheet.getFrameByIndex(0),
|
2012-09-04 20:18:00 +00:00
|
|
|
$('#drawing-canvas-container')[0],
|
|
|
|
drawingCanvasDpi
|
|
|
|
);
|
|
|
|
|
2012-08-31 08:45:07 +00:00
|
|
|
this.setActiveFrame(0);
|
2012-09-04 22:09:42 +00:00
|
|
|
|
|
|
|
this.animationController = new pskl.controller.AnimatedPreviewController(
|
|
|
|
frameSheet,
|
|
|
|
$('#preview-canvas-container')[0],
|
|
|
|
previewAnimationCanvasDpi
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
this.previewsController = new pskl.controller.PreviewFilmController(
|
|
|
|
frameSheet,
|
|
|
|
$('#preview-list')[0],
|
|
|
|
previewTileCanvasDpi
|
|
|
|
);
|
|
|
|
|
|
|
|
this.animationController.init();
|
|
|
|
this.previewsController.init();
|
2012-09-03 18:45:25 +00:00
|
|
|
|
2012-09-02 23:24:55 +00:00
|
|
|
pskl.NotificationService.init();
|
|
|
|
pskl.LocalStorageService.init(frameSheet);
|
|
|
|
|
|
|
|
// TODO: Add comments
|
2012-09-03 18:45:25 +00:00
|
|
|
var framesheetId = this.getFramesheetIdFromUrl();
|
|
|
|
if (framesheetId) {
|
|
|
|
$.publish(Events.SHOW_NOTIFICATION, [{"content": "Loading animation with id : [" + framesheetId + "]"}]);
|
|
|
|
this.loadFramesheetFromService(framesheetId);
|
2012-08-29 23:16:13 +00:00
|
|
|
} else {
|
|
|
|
this.finishInit();
|
2012-09-02 23:24:55 +00:00
|
|
|
pskl.LocalStorageService.displayRestoreNotification();
|
2012-08-29 23:16:13 +00:00
|
|
|
}
|
|
|
|
},
|
2012-08-27 00:05:13 +00:00
|
|
|
|
2012-08-29 23:16:13 +00:00
|
|
|
finishInit : function () {
|
2012-08-31 08:45:07 +00:00
|
|
|
|
|
|
|
$.subscribe(Events.TOOL_SELECTED, function(evt, toolBehavior) {
|
|
|
|
console.log("Tool selected: ", toolBehavior);
|
|
|
|
currentToolBehavior = toolBehavior;
|
|
|
|
});
|
|
|
|
|
2012-09-02 23:24:55 +00: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 00:05:13 +00:00
|
|
|
this.initDrawingArea();
|
2012-09-02 23:24:55 +00:00
|
|
|
|
|
|
|
pskl.ToolSelector.init();
|
2012-09-03 20:30:53 +00:00
|
|
|
pskl.Palette.init(frameSheet);
|
2012-08-29 23:16:13 +00:00
|
|
|
},
|
|
|
|
|
2012-09-03 18:45:25 +00:00
|
|
|
getFramesheetIdFromUrl : function() {
|
2012-08-29 23:16:13 +00:00
|
|
|
var href = window.location.href;
|
2012-09-03 18:45:25 +00:00
|
|
|
// TODO: Change frameId to framesheetId on the backend
|
2012-08-29 23:16:13 +00:00
|
|
|
if (href.indexOf('frameId=') != -1) {
|
|
|
|
return href.substring(href.indexOf('frameId=')+8);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
loadFramesheetFromService : function (frameId) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
2012-09-03 18:45:25 +00:00
|
|
|
// TODO: Change frameId to framesheetId on the backend
|
2012-09-02 23:24:55 +00:00
|
|
|
xhr.open('GET', Constants.PISKEL_SERVICE_URL + '/get?l=' + frameId, true);
|
2012-08-29 23:16:13 +00:00
|
|
|
xhr.responseType = 'text';
|
|
|
|
|
|
|
|
xhr.onload = function(e) {
|
|
|
|
frameSheet.deserialize(this.responseText);
|
2012-09-03 18:45:25 +00:00
|
|
|
piskel.setActiveFrame(0);
|
2012-09-02 23:24:55 +00:00
|
|
|
$.publish(Events.HIDE_NOTIFICATION);
|
2012-08-29 23:16:13 +00:00
|
|
|
piskel.finishInit();
|
2012-09-03 18:45:25 +00:00
|
|
|
piskel.setActiveFrameAndRedraw(0);
|
2012-08-29 23:16:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = function () {
|
2012-09-02 23:24:55 +00:00
|
|
|
$.publish(Events.HIDE_NOTIFICATION);
|
2012-08-29 23:16:13 +00:00
|
|
|
piskel.finishInit();
|
2012-09-03 18:45:25 +00:00
|
|
|
piskel.setActiveFrameAndRedraw(0);
|
2012-08-29 23:16:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
2012-08-29 21:32:51 +00:00
|
|
|
},
|
2012-08-29 21:01:04 +00:00
|
|
|
|
2012-08-27 00:05:13 +00:00
|
|
|
setActiveFrame: function(index) {
|
|
|
|
activeFrameIndex = index;
|
2012-09-04 20:18:00 +00:00
|
|
|
this.drawingController.frame = frameSheet.getFrameByIndex(index);
|
2012-08-27 00:05:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setActiveFrameAndRedraw: function(index) {
|
|
|
|
this.setActiveFrame(index);
|
2012-09-04 22:09:42 +00:00
|
|
|
this.redraw();
|
|
|
|
},
|
2012-09-03 18:45:25 +00:00
|
|
|
|
2012-09-04 22:09:42 +00:00
|
|
|
redraw : function () {
|
2012-08-27 00:05:13 +00:00
|
|
|
// Update drawing canvas:
|
2012-09-04 20:18:00 +00:00
|
|
|
this.drawingController.renderFrame();
|
2012-08-27 00:05:13 +00:00
|
|
|
// Update slideshow:
|
2012-09-04 22:09:42 +00:00
|
|
|
this.previewsController.createPreviews();
|
2012-08-27 00:05:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getActiveFrameIndex: function() {
|
|
|
|
if(-1 == activeFrameIndex) {
|
2012-09-04 22:09:42 +00:00
|
|
|
throw "Bad active frame initialization."
|
2012-08-27 00:05:13 +00:00
|
|
|
}
|
|
|
|
return activeFrameIndex;
|
|
|
|
},
|
|
|
|
|
|
|
|
initDrawingArea : function() {
|
2012-08-31 08:45:07 +00:00
|
|
|
drawingAreaContainer = $('#drawing-canvas-container')[0];
|
2012-09-04 20:18:00 +00:00
|
|
|
document.body.addEventListener('mouseup', this.onMouseup.bind(this));
|
|
|
|
drawingAreaContainer.addEventListener('mousedown', this.onMousedown.bind(this));
|
|
|
|
drawingAreaContainer.addEventListener('mousemove', this.onMousemove.bind(this));
|
2012-09-04 12:10:16 +00:00
|
|
|
drawingAreaContainer.style.width = framePixelWidth * drawingCanvasDpi + "px";
|
|
|
|
drawingAreaContainer.style.height = framePixelHeight * drawingCanvasDpi + "px";
|
2012-09-04 20:18:00 +00:00
|
|
|
drawingAreaContainer.addEventListener('contextmenu', this.onCanvasContextMenu);
|
2012-08-27 00:05:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
removeFrame: function(frameIndex) {
|
|
|
|
frameSheet.removeFrameByIndex(frameIndex);
|
2012-09-05 05:59:58 +00:00
|
|
|
var activeFrameIndex = frameIndex ? frameIndex - 1 : 0;
|
|
|
|
this.setActiveFrameAndRedraw(activeFrameIndex);
|
2012-08-25 00:25:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
duplicateFrame: function(frameIndex) {
|
2012-08-27 00:05:13 +00:00
|
|
|
frameSheet.duplicateFrameByIndex(frameIndex);
|
|
|
|
this.setActiveFrameAndRedraw(frameIndex + 1);
|
2012-08-25 00:25:44 +00:00
|
|
|
},
|
|
|
|
|
2012-09-04 20:18:00 +00:00
|
|
|
onMousedown : function (event) {
|
2012-08-22 22:57:35 +00:00
|
|
|
isClicked = true;
|
2012-08-31 08:45:07 +00:00
|
|
|
|
|
|
|
if(event.button == 2) { // right click
|
2012-08-27 00:05:13 +00:00
|
|
|
isRightClicked = true;
|
2012-08-31 08:45:07 +00:00
|
|
|
$.publish(Events.CANVAS_RIGHT_CLICKED);
|
2012-08-27 00:05:13 +00:00
|
|
|
}
|
2012-08-31 08:45:07 +00:00
|
|
|
var spriteCoordinate = this.getSpriteCoordinate(event);
|
2012-09-01 22:44:55 +00:00
|
|
|
currentToolBehavior.applyToolAt(
|
2012-09-04 12:10:16 +00:00
|
|
|
spriteCoordinate.col,
|
|
|
|
spriteCoordinate.row,
|
|
|
|
penColor,
|
2012-09-04 20:18:00 +00:00
|
|
|
this.drawingController
|
2012-09-04 12:10:16 +00:00
|
|
|
);
|
2012-09-02 23:24:55 +00:00
|
|
|
|
|
|
|
$.publish(Events.LOCALSTORAGE_REQUEST);
|
2012-08-27 00:05:13 +00:00
|
|
|
},
|
|
|
|
|
2012-09-04 20:18:00 +00:00
|
|
|
onMousemove : function (event) {
|
2012-09-02 15:49:28 +00:00
|
|
|
var currentTime = new Date().getTime();
|
|
|
|
// Throttling of the mousemove event:
|
|
|
|
if ((currentTime - previousMousemoveTime) > 40 ) {
|
|
|
|
if (isClicked) {
|
|
|
|
var spriteCoordinate = this.getSpriteCoordinate(event);
|
|
|
|
currentToolBehavior.moveToolAt(
|
2012-09-04 12:10:16 +00:00
|
|
|
spriteCoordinate.col,
|
|
|
|
spriteCoordinate.row,
|
|
|
|
penColor,
|
2012-09-04 20:18:00 +00:00
|
|
|
this.drawingController
|
2012-09-04 12:10:16 +00:00
|
|
|
);
|
2012-09-02 15:49:28 +00:00
|
|
|
|
|
|
|
// 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-02 23:24:55 +00:00
|
|
|
$.publish(Events.LOCALSTORAGE_REQUEST);
|
2012-09-02 15:49:28 +00:00
|
|
|
}
|
|
|
|
previousMousemoveTime = currentTime;
|
2012-08-27 00:05:13 +00:00
|
|
|
}
|
2012-08-22 22:57:35 +00:00
|
|
|
},
|
|
|
|
|
2012-09-04 20:18:00 +00:00
|
|
|
onMouseup : function (event) {
|
2012-08-27 00:05:13 +00: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-05 22:14:32 +00:00
|
|
|
if(isRightClicked) {
|
|
|
|
$.publish(Events.CANVAS_RIGHT_CLICK_RELEASED);
|
|
|
|
}
|
|
|
|
isClicked = false;
|
|
|
|
isRightClicked = false;
|
|
|
|
var spriteCoordinate = this.getSpriteCoordinate(event);
|
|
|
|
currentToolBehavior.releaseToolAt(
|
2012-09-01 22:44:55 +00:00
|
|
|
spriteCoordinate.col,
|
|
|
|
spriteCoordinate.row,
|
|
|
|
penColor,
|
2012-09-05 21:59:53 +00:00
|
|
|
this.drawingController
|
2012-09-04 12:10:16 +00:00
|
|
|
);
|
2012-09-05 22:23:24 +00:00
|
|
|
|
2012-09-05 22:14:32 +00:00
|
|
|
// TODO: Remove that when we have the centralized redraw loop
|
|
|
|
this.previewsController.createPreviews();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 22:57:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onCanvasContextMenu : function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.cancelBubble = true;
|
|
|
|
return false;
|
|
|
|
},
|
2012-08-29 06:39:03 +00:00
|
|
|
|
2012-08-22 22:57:35 +00:00
|
|
|
getRelativeCoordinates : function (x, y) {
|
2012-09-04 22:13:45 +00:00
|
|
|
var canvasRect = $(".drawing-canvas")[0].getBoundingClientRect();
|
2012-08-22 22:57:35 +00:00
|
|
|
return {
|
|
|
|
x : x - canvasRect.left,
|
|
|
|
y : y - canvasRect.top
|
|
|
|
}
|
2012-08-29 23:16:13 +00:00
|
|
|
},
|
|
|
|
|
2012-08-31 08:45:07 +00: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-29 23:16:13 +00:00
|
|
|
storeSheet : function (event) {
|
2012-09-02 23:24:55 +00:00
|
|
|
// TODO Refactor using jquery ?
|
2012-08-29 23:16:13 +00:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
var formData = new FormData();
|
|
|
|
formData.append('framesheet_content', frameSheet.serialize());
|
2012-08-31 08:45:07 +00:00
|
|
|
formData.append('fps_speed', $('#preview-fps').val());
|
2012-09-02 23:24:55 +00:00
|
|
|
xhr.open('POST', Constants.PISKEL_SERVICE_URL + "/store", true);
|
2012-08-29 23:16:13 +00:00
|
|
|
xhr.onload = function(e) {
|
|
|
|
if (this.status == 200) {
|
2012-08-29 23:26:14 +00:00
|
|
|
var baseUrl = window.location.href.replace(window.location.search, "");
|
|
|
|
window.location.href = baseUrl + "?frameId=" + this.responseText;
|
2012-08-29 23:16:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(formData);
|
|
|
|
|
2012-09-04 21:48:02 +00:00
|
|
|
if(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2012-08-29 23:16:13 +00:00
|
|
|
return false;
|
2012-08-22 22:57:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.piskel = piskel;
|
|
|
|
piskel.init();
|
|
|
|
|
2012-09-02 23:24:55 +00:00
|
|
|
})();
|