2012-09-04 16:10:16 +04:00
|
|
|
(function () {
|
2012-09-14 02:33:46 +04:00
|
|
|
var ns = $.namespace("pskl.controller");
|
2012-09-16 20:48:32 +04:00
|
|
|
ns.DrawingController = function (framesheet, container) {
|
2012-09-14 02:33:46 +04:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
this.framesheet = framesheet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
this.overlayFrame = pskl.model.Frame.createEmptyFromFrame(framesheet.getCurrentFrame());
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.container = container;
|
2012-09-16 20:48:32 +04:00
|
|
|
|
|
|
|
// TODO(vincz): Store user prefs in a localstorage string ?
|
|
|
|
var renderingOptions = {
|
|
|
|
"dpi": this.calculateDPI_(),
|
2013-06-18 01:06:17 +04:00
|
|
|
"supportGridRendering" : true
|
2012-09-16 20:48:32 +04:00
|
|
|
};
|
2012-09-14 02:33:46 +04:00
|
|
|
|
|
|
|
this.renderer = new pskl.rendering.FrameRenderer(this.container, renderingOptions, "drawing-canvas");
|
|
|
|
this.overlayRenderer = new pskl.rendering.FrameRenderer(this.container, renderingOptions, "canvas-overlay");
|
|
|
|
|
|
|
|
|
|
|
|
// State of drawing controller:
|
|
|
|
this.isClicked = false;
|
|
|
|
this.isRightClicked = false;
|
|
|
|
this.previousMousemoveTime = 0;
|
|
|
|
this.currentToolBehavior = null;
|
|
|
|
this.primaryColor = Constants.DEFAULT_PEN_COLOR;
|
|
|
|
this.secondaryColor = Constants.TRANSPARENT_COLOR;
|
2013-08-06 01:34:11 +04:00
|
|
|
};
|
2012-09-14 02:33:46 +04:00
|
|
|
|
2013-08-06 01:34:11 +04:00
|
|
|
ns.DrawingController.prototype.init = function () {
|
|
|
|
this.renderer.render(this.framesheet.getCurrentFrame());
|
|
|
|
this.overlayRenderer.render(this.overlayFrame);
|
|
|
|
|
2012-09-14 02:33:46 +04:00
|
|
|
this.initMouseBehavior();
|
|
|
|
|
|
|
|
$.subscribe(Events.TOOL_SELECTED, $.proxy(function(evt, toolBehavior) {
|
2012-09-11 01:26:12 +04:00
|
|
|
console.log("Tool selected: ", toolBehavior);
|
|
|
|
this.currentToolBehavior = toolBehavior;
|
2013-08-06 01:34:11 +04:00
|
|
|
this.overlayFrame.clear();
|
2012-09-11 01:26:12 +04:00
|
|
|
}, this));
|
|
|
|
|
2013-04-09 03:32:30 +04:00
|
|
|
/**
|
|
|
|
* TODO(grosbouddha): Primary/secondary color state are kept in this general controller.
|
|
|
|
* Find a better place to store that. Perhaps PaletteController?
|
|
|
|
*/
|
|
|
|
$.subscribe(Events.PRIMARY_COLOR_SELECTED, $.proxy(function(evt, color) {
|
|
|
|
console.log("Primary color selected: ", color);
|
|
|
|
this.primaryColor = color;
|
|
|
|
$.publish(Events.PRIMARY_COLOR_UPDATED, [color]);
|
|
|
|
}, this));
|
|
|
|
$.subscribe(Events.SECONDARY_COLOR_SELECTED, $.proxy(function(evt, color) {
|
|
|
|
console.log("Secondary color selected: ", color);
|
|
|
|
this.secondaryColor = color;
|
|
|
|
$.publish(Events.SECONDARY_COLOR_UPDATED, [color]);
|
2012-09-11 01:26:12 +04:00
|
|
|
}, this));
|
2012-09-16 20:48:32 +04:00
|
|
|
|
|
|
|
$(window).resize($.proxy(this.startDPIUpdateTimer_, this));
|
|
|
|
|
2013-06-19 21:01:12 +04:00
|
|
|
$.subscribe(Events.USER_SETTINGS_CHANGED, $.proxy(this.onUserSettingsChange_, this));
|
2012-09-16 20:48:32 +04:00
|
|
|
$.subscribe(Events.FRAME_SIZE_CHANGED, $.proxy(this.updateDPI_, this));
|
2013-06-19 21:01:12 +04:00
|
|
|
|
|
|
|
this.updateDPI_();
|
2012-09-14 02:33:46 +04:00
|
|
|
};
|
2012-09-08 17:08:00 +04:00
|
|
|
|
2012-09-14 02:33:46 +04:00
|
|
|
ns.DrawingController.prototype.initMouseBehavior = function() {
|
|
|
|
var body = $('body');
|
2013-08-10 14:11:16 +04:00
|
|
|
this.container.mousedown($.proxy(this.onMousedown_, this));
|
|
|
|
this.container.mousemove($.proxy(this.onMousemove_, this));
|
|
|
|
body.mouseup($.proxy(this.onMouseup_, this));
|
|
|
|
|
|
|
|
// Deactivate right click:
|
|
|
|
body.contextmenu(this.onCanvasContextMenu_);
|
2012-09-14 02:33:46 +04:00
|
|
|
};
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2012-09-16 20:48:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
ns.DrawingController.prototype.startDPIUpdateTimer_ = function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
if (this.dpiUpdateTimer) {
|
|
|
|
window.clearInterval(this.dpiUpdateTimer);
|
|
|
|
}
|
|
|
|
this.dpiUpdateTimer = window.setTimeout($.proxy(this.updateDPI_, this), 200);
|
2012-09-16 20:48:32 +04:00
|
|
|
},
|
|
|
|
|
2013-06-17 22:24:27 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.onUserSettingsChange_ = function (evt, settingsName, settingsValue) {
|
|
|
|
if(settingsName == pskl.UserSettings.SHOW_GRID) {
|
2013-06-19 21:01:12 +04:00
|
|
|
this.updateDPI_();
|
2013-06-17 22:24:27 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-14 02:33:46 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.onMousedown_ = function (event) {
|
2013-08-10 14:11:16 +04:00
|
|
|
this.isClicked = true;
|
|
|
|
|
|
|
|
if(event.button == 2) { // right click
|
|
|
|
this.isRightClicked = true;
|
|
|
|
$.publish(Events.CANVAS_RIGHT_CLICKED);
|
|
|
|
}
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var coords = this.getSpriteCoordinates(event);
|
|
|
|
|
|
|
|
this.currentToolBehavior.applyToolAt(
|
|
|
|
coords.col, coords.row,
|
|
|
|
this.getCurrentColor_(),
|
|
|
|
this.framesheet.getCurrentFrame(),
|
|
|
|
this.overlayFrame,
|
|
|
|
this.wrapEvtInfo_(event)
|
|
|
|
);
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
$.publish(Events.LOCALSTORAGE_REQUEST);
|
|
|
|
};
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
2012-09-14 02:33:46 +04:00
|
|
|
* @private
|
|
|
|
*/
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.DrawingController.prototype.onMousemove_ = function (event) {
|
|
|
|
var currentTime = new Date().getTime();
|
|
|
|
// Throttling of the mousemove event:
|
|
|
|
if ((currentTime - this.previousMousemoveTime) > 40 ) {
|
|
|
|
var coords = this.getSpriteCoordinates(event);
|
|
|
|
if (this.isClicked) {
|
|
|
|
|
|
|
|
this.currentToolBehavior.moveToolAt(
|
2012-09-11 01:26:12 +04:00
|
|
|
coords.col, coords.row,
|
2012-09-08 20:44:06 +04:00
|
|
|
this.getCurrentColor_(),
|
2012-09-11 01:26:12 +04:00
|
|
|
this.framesheet.getCurrentFrame(),
|
2013-04-09 09:24:07 +04:00
|
|
|
this.overlayFrame,
|
|
|
|
this.wrapEvtInfo_(event)
|
2012-09-08 20:44:06 +04:00
|
|
|
);
|
2013-08-10 14:11:16 +04: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.
|
|
|
|
$.publish(Events.LOCALSTORAGE_REQUEST);
|
|
|
|
} else {
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
this.currentToolBehavior.moveUnactiveToolAt(
|
|
|
|
coords.col, coords.row,
|
|
|
|
this.getCurrentColor_(),
|
|
|
|
this.framesheet.getCurrentFrame(),
|
|
|
|
this.overlayFrame,
|
|
|
|
this.wrapEvtInfo_(event)
|
|
|
|
);
|
2012-09-08 20:44:06 +04:00
|
|
|
}
|
2013-08-10 14:11:16 +04:00
|
|
|
this.previousMousemoveTime = currentTime;
|
|
|
|
}
|
|
|
|
};
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.onMouseup_ = function (event) {
|
|
|
|
if(this.isClicked || this.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.
|
2013-04-09 09:24:07 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
this.isClicked = false;
|
|
|
|
this.isRightClicked = false;
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var coords = this.getSpriteCoordinates(event);
|
|
|
|
//console.log("mousemove: col: " + spriteCoordinate.col + " - row: " + spriteCoordinate.row);
|
|
|
|
this.currentToolBehavior.releaseToolAt(
|
|
|
|
coords.col, coords.row,
|
|
|
|
this.getCurrentColor_(),
|
|
|
|
this.framesheet.getCurrentFrame(),
|
|
|
|
this.overlayFrame,
|
|
|
|
this.wrapEvtInfo_(event)
|
|
|
|
);
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
$.publish(Events.TOOL_RELEASED);
|
|
|
|
}
|
|
|
|
};
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.wrapEvtInfo_ = function (event) {
|
|
|
|
var evtInfo = {};
|
|
|
|
if (event.button === 0) {
|
|
|
|
evtInfo.button = Constants.LEFT_BUTTON;
|
|
|
|
} else if (event.button == 2) {
|
|
|
|
evtInfo.button = Constants.RIGHT_BUTTON;
|
|
|
|
}
|
|
|
|
return evtInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.getRelativeCoordinates = function (clientX, clientY) {
|
|
|
|
var canvasPageOffset = this.container.offset();
|
|
|
|
return {
|
|
|
|
x : clientX - canvasPageOffset.left,
|
|
|
|
y : clientY - canvasPageOffset.top
|
2012-09-08 20:44:06 +04:00
|
|
|
};
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.getSpriteCoordinates = function(event) {
|
|
|
|
var coords = this.getRelativeCoordinates(event.clientX, event.clientY);
|
|
|
|
return this.renderer.convertPixelCoordinatesIntoSpriteCoordinate(coords);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.getCurrentColor_ = function () {
|
|
|
|
if(this.isRightClicked) {
|
|
|
|
return this.secondaryColor;
|
|
|
|
} else {
|
|
|
|
return this.primaryColor;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.onCanvasContextMenu_ = function (event) {
|
|
|
|
if ($(event.target).closest('#drawing-canvas-container').length) {
|
|
|
|
// Deactivate right click on drawing canvas only.
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
event.cancelBubble = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2012-09-04 16:10:16 +04:00
|
|
|
|
2012-09-09 02:40:05 +04:00
|
|
|
ns.DrawingController.prototype.render = function () {
|
2012-09-14 02:33:46 +04:00
|
|
|
this.renderFrame();
|
|
|
|
this.renderOverlay();
|
2012-09-09 02:40:05 +04:00
|
|
|
};
|
|
|
|
|
2012-09-14 02:33:46 +04:00
|
|
|
ns.DrawingController.prototype.renderFrame = function () {
|
2012-09-11 01:26:12 +04:00
|
|
|
var frame = this.framesheet.getCurrentFrame();
|
|
|
|
var serializedFrame = frame.serialize();
|
2012-09-09 03:12:54 +04:00
|
|
|
if (this.serializedFrame != serializedFrame) {
|
2012-09-21 01:57:26 +04:00
|
|
|
if (!frame.isSameSize(this.overlayFrame)) {
|
|
|
|
this.overlayFrame = pskl.model.Frame.createEmptyFromFrame(frame);
|
|
|
|
}
|
2012-09-14 02:33:46 +04:00
|
|
|
this.serializedFrame = serializedFrame;
|
|
|
|
this.renderer.render(frame);
|
2012-09-09 03:12:54 +04:00
|
|
|
}
|
2012-09-14 02:33:46 +04:00
|
|
|
};
|
2012-09-04 16:10:16 +04:00
|
|
|
|
2012-09-14 02:33:46 +04:00
|
|
|
ns.DrawingController.prototype.renderOverlay = function () {
|
2012-09-09 03:12:54 +04:00
|
|
|
var serializedOverlay = this.overlayFrame.serialize();
|
|
|
|
if (this.serializedOverlay != serializedOverlay) {
|
2012-09-14 02:33:46 +04:00
|
|
|
this.serializedOverlay = serializedOverlay;
|
2012-09-09 04:34:54 +04:00
|
|
|
this.overlayRenderer.render(this.overlayFrame);
|
2012-09-09 03:12:54 +04:00
|
|
|
}
|
2012-09-14 02:33:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.DrawingController.prototype.forceRendering_ = function () {
|
|
|
|
this.serializedFrame = this.serializedOverlay = null;
|
2012-09-16 20:48:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.calculateDPI_ = function() {
|
2013-06-19 21:01:12 +04:00
|
|
|
var availableViewportHeight = $('#main-wrapper').height(),
|
2013-08-10 14:11:16 +04:00
|
|
|
leftSectionWidth = $('.left-column').outerWidth(true),
|
|
|
|
rightSectionWidth = $('.right-column').outerWidth(true),
|
|
|
|
availableViewportWidth = $('#main-wrapper').width() - leftSectionWidth - rightSectionWidth,
|
|
|
|
framePixelHeight = this.framesheet.getCurrentFrame().getHeight(),
|
|
|
|
framePixelWidth = this.framesheet.getCurrentFrame().getWidth();
|
2013-06-19 21:01:12 +04:00
|
|
|
|
|
|
|
if (pskl.UserSettings.get(pskl.UserSettings.SHOW_GRID)) {
|
|
|
|
availableViewportWidth = availableViewportWidth - (framePixelWidth * Constants.GRID_STROKE_WIDTH);
|
|
|
|
availableViewportHeight = availableViewportHeight - (framePixelHeight * Constants.GRID_STROKE_WIDTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dpi = pskl.PixelUtils.calculateDPI(
|
|
|
|
availableViewportHeight, availableViewportWidth, framePixelHeight, framePixelWidth);
|
2013-06-14 15:17:20 +04:00
|
|
|
|
2012-09-16 20:48:32 +04:00
|
|
|
return dpi;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.updateDPI_ = function() {
|
|
|
|
var dpi = this.calculateDPI_();
|
|
|
|
this.renderer.updateDPI(dpi);
|
|
|
|
this.overlayRenderer.updateDPI(dpi);
|
2013-06-19 21:01:12 +04:00
|
|
|
|
|
|
|
var currentFrameHeight = this.framesheet.getCurrentFrame().getHeight();
|
|
|
|
var canvasHeight = currentFrameHeight * dpi;
|
|
|
|
if (pskl.UserSettings.get(pskl.UserSettings.SHOW_GRID)) {
|
2013-08-10 14:11:16 +04:00
|
|
|
canvasHeight += Constants.GRID_STROKE_WIDTH * currentFrameHeight;
|
2013-06-19 21:01:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var verticalGapInPixel = Math.floor(($('#main-wrapper').height() - canvasHeight) / 2);
|
|
|
|
$('#column-wrapper').css({
|
|
|
|
'top': verticalGapInPixel + 'px',
|
|
|
|
'height': canvasHeight + 'px'
|
|
|
|
});
|
|
|
|
|
2012-09-16 20:48:32 +04:00
|
|
|
this.forceRendering_();
|
|
|
|
};
|
2012-09-04 16:10:16 +04:00
|
|
|
})();
|