2012-09-04 16:10:16 +04:00
|
|
|
(function () {
|
2012-09-14 02:33:46 +04:00
|
|
|
var ns = $.namespace("pskl.controller");
|
2013-11-19 10:40:35 +04:00
|
|
|
ns.DrawingController = function (piskelController, paletteController,container) {
|
2012-09-14 02:33:46 +04:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2013-09-22 23:02:43 +04:00
|
|
|
this.piskelController = piskelController;
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-11-19 10:40:35 +04:00
|
|
|
this.paletteController = paletteController;
|
|
|
|
|
2012-09-14 02:33:46 +04:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2013-09-22 23:02:43 +04:00
|
|
|
this.overlayFrame = pskl.model.Frame.createEmptyFromFrame(piskelController.getCurrentFrame());
|
2012-09-14 02:33:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.container = container;
|
2012-09-16 20:48:32 +04:00
|
|
|
|
|
|
|
// TODO(vincz): Store user prefs in a localstorage string ?
|
|
|
|
var renderingOptions = {
|
2013-11-01 18:39:42 +04:00
|
|
|
"zoom": this.calculateZoom_(),
|
2013-10-30 01:16:39 +04:00
|
|
|
"supportGridRendering" : true,
|
|
|
|
"height" : this.getContainerHeight_(),
|
|
|
|
"width" : this.getContainerWidth_(),
|
2013-11-01 18:39:42 +04:00
|
|
|
"xOffset" : 0,
|
|
|
|
"yOffset" : 0
|
2012-09-16 20:48:32 +04:00
|
|
|
};
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
this.overlayRenderer = new pskl.rendering.frame.CachedFrameRenderer(this.container, renderingOptions, ["canvas-overlay"]);
|
|
|
|
this.renderer = new pskl.rendering.frame.CachedFrameRenderer(this.container, renderingOptions, ["drawing-canvas"]);
|
2013-11-01 20:12:59 +04:00
|
|
|
this.layersRenderer = new pskl.rendering.layer.LayersRenderer(this.container, renderingOptions, piskelController);
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-11-01 20:12:59 +04:00
|
|
|
this.compositeRenderer = new pskl.rendering.CompositeRenderer();
|
|
|
|
this.compositeRenderer
|
2013-11-01 18:39:42 +04:00
|
|
|
.add(this.overlayRenderer)
|
|
|
|
.add(this.renderer)
|
2013-11-01 20:12:59 +04:00
|
|
|
.add(this.layersRenderer);
|
2012-09-14 02:33:46 +04:00
|
|
|
|
|
|
|
// State of drawing controller:
|
|
|
|
this.isClicked = false;
|
|
|
|
this.isRightClicked = false;
|
|
|
|
this.previousMousemoveTime = 0;
|
|
|
|
this.currentToolBehavior = null;
|
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 () {
|
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
|
|
|
this.currentToolBehavior = toolBehavior;
|
2013-08-06 01:34:11 +04:00
|
|
|
this.overlayFrame.clear();
|
2012-09-11 01:26:12 +04:00
|
|
|
}, this));
|
2013-11-23 22:50:53 +04:00
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
$(window).resize($.proxy(this.startResizeTimer_, this));
|
2012-09-16 20:48:32 +04:00
|
|
|
|
2013-06-19 21:01:12 +04:00
|
|
|
$.subscribe(Events.USER_SETTINGS_CHANGED, $.proxy(this.onUserSettingsChange_, this));
|
2013-11-01 18:39:42 +04:00
|
|
|
$.subscribe(Events.FRAME_SIZE_CHANGED, $.proxy(this.onFrameSizeChanged_, this));
|
2013-06-19 21:01:12 +04:00
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
this.centerColumnWrapperHorizontally_();
|
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));
|
2013-11-15 03:32:18 +04:00
|
|
|
|
|
|
|
if (pskl.utils.UserAgent.isChrome) {
|
|
|
|
this.container.on('mousewheel', $.proxy(this.onMousewheel_, this));
|
|
|
|
} else {
|
|
|
|
this.container.on('wheel', $.proxy(this.onMousewheel_, this));
|
|
|
|
}
|
2013-10-30 01:16:39 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
body.mouseup($.proxy(this.onMouseup_, this));
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
// Deactivate right click:
|
|
|
|
body.contextmenu(this.onCanvasContextMenu_);
|
2012-09-14 02:33:46 +04:00
|
|
|
};
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
ns.DrawingController.prototype.startResizeTimer_ = function () {
|
|
|
|
if (this.resizeTimer) {
|
|
|
|
window.clearInterval(this.resizeTimer);
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
2013-11-01 18:39:42 +04:00
|
|
|
this.resizeTimer = window.setTimeout($.proxy(this.afterWindowResize_, this), 200);
|
|
|
|
},
|
|
|
|
|
|
|
|
ns.DrawingController.prototype.afterWindowResize_ = function () {
|
2013-11-01 20:12:59 +04:00
|
|
|
this.compositeRenderer.setDisplaySize(this.getContainerWidth_(), this.getContainerHeight_());
|
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-11-01 18:39:42 +04:00
|
|
|
console.warn('DrawingController:onUserSettingsChange_ not implemented !');
|
2013-06-17 22:24:27 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-11-02 03:00:38 +04:00
|
|
|
ns.DrawingController.prototype.onFrameSizeChanged_ = function () {
|
|
|
|
this.compositeRenderer.setZoom(this.calculateZoom_());
|
|
|
|
this.compositeRenderer.setDisplaySize(this.getContainerWidth_(), this.getContainerHeight_());
|
|
|
|
};
|
|
|
|
|
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;
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
if(event.button == 2) { // right click
|
|
|
|
this.isRightClicked = true;
|
|
|
|
}
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
var coords = this.renderer.getCoordinates(event.clientX, event.clientY);
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
this.currentToolBehavior.applyToolAt(
|
2013-11-01 18:39:42 +04:00
|
|
|
coords.x,
|
|
|
|
coords.y,
|
2013-08-10 14:11:16 +04:00
|
|
|
this.getCurrentColor_(),
|
2013-09-22 23:02:43 +04:00
|
|
|
this.piskelController.getCurrentFrame(),
|
2013-08-10 14:11:16 +04:00
|
|
|
this.overlayFrame,
|
|
|
|
this.wrapEvtInfo_(event)
|
2013-09-29 01:52:51 +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:
|
2013-11-01 19:27:23 +04:00
|
|
|
if ((currentTime - this.previousMousemoveTime) > Constants.MOUSEMOVE_THROTTLING ) {
|
2013-11-01 18:39:42 +04:00
|
|
|
var coords = this.renderer.getCoordinates(event.clientX, event.clientY);
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
if (this.isClicked) {
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
this.currentToolBehavior.moveToolAt(
|
2013-11-01 18:39:42 +04:00
|
|
|
coords.x,
|
|
|
|
coords.y,
|
2012-09-08 20:44:06 +04:00
|
|
|
this.getCurrentColor_(),
|
2013-09-22 23:02:43 +04:00
|
|
|
this.piskelController.getCurrentFrame(),
|
2013-04-09 09:24:07 +04:00
|
|
|
this.overlayFrame,
|
|
|
|
this.wrapEvtInfo_(event)
|
2012-09-08 20:44:06 +04:00
|
|
|
);
|
2013-09-29 01:52:51 +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(
|
2013-11-01 18:39:42 +04:00
|
|
|
coords.x,
|
|
|
|
coords.y,
|
2013-08-10 14:11:16 +04:00
|
|
|
this.getCurrentColor_(),
|
2013-09-22 23:02:43 +04:00
|
|
|
this.piskelController.getCurrentFrame(),
|
2013-08-10 14:11:16 +04:00
|
|
|
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-10-30 01:16:39 +04:00
|
|
|
ns.DrawingController.prototype.onMousewheel_ = function (jQueryEvent) {
|
|
|
|
var event = jQueryEvent.originalEvent;
|
2013-11-02 02:11:11 +04:00
|
|
|
var delta = event.wheelDeltaY || (-2 * event.deltaY);
|
2013-11-01 18:39:42 +04:00
|
|
|
var currentZoom = this.renderer.getZoom();
|
2013-10-30 01:16:39 +04:00
|
|
|
if (delta > 0) {
|
2013-11-01 20:12:59 +04:00
|
|
|
this.compositeRenderer.setZoom(currentZoom + 1);
|
2013-10-30 01:16:39 +04:00
|
|
|
} else if (delta < 0) {
|
2013-11-01 20:12:59 +04:00
|
|
|
this.compositeRenderer.setZoom(currentZoom - 1);
|
2013-10-30 01:16:39 +04:00
|
|
|
}
|
2013-11-02 02:11:11 +04:00
|
|
|
pskl.app.minimapController.onDrawingControllerMove_();
|
2013-10-30 01:16:39 +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-11-01 18:39:42 +04:00
|
|
|
var coords = this.renderer.getCoordinates(event.clientX, event.clientY);
|
2013-08-10 14:11:16 +04:00
|
|
|
//console.log("mousemove: col: " + spriteCoordinate.col + " - row: " + spriteCoordinate.row);
|
|
|
|
this.currentToolBehavior.releaseToolAt(
|
2013-11-01 18:39:42 +04:00
|
|
|
coords.x,
|
|
|
|
coords.y,
|
2013-08-10 14:11:16 +04:00
|
|
|
this.getCurrentColor_(),
|
2013-09-22 23:02:43 +04:00
|
|
|
this.piskelController.getCurrentFrame(),
|
2013-08-10 14:11:16 +04:00
|
|
|
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;
|
2013-09-29 01:52:51 +04:00
|
|
|
};
|
2013-08-10 14:11:16 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.getSpriteCoordinates = function(event) {
|
2013-11-01 18:39:42 +04:00
|
|
|
return this.renderer.getCoordinates(event.clientX, event.clientY);
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.getCurrentColor_ = function () {
|
|
|
|
if(this.isRightClicked) {
|
2013-11-19 10:40:35 +04:00
|
|
|
return this.paletteController.getSecondaryColor();
|
2013-08-10 14:11:16 +04:00
|
|
|
} else {
|
2013-11-19 10:40:35 +04:00
|
|
|
return this.paletteController.getPrimaryColor();
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
2013-09-29 01:52:51 +04:00
|
|
|
}
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2012-09-04 16:10:16 +04:00
|
|
|
|
2012-09-09 02:40:05 +04:00
|
|
|
ns.DrawingController.prototype.render = function () {
|
2013-11-01 18:39:42 +04:00
|
|
|
var currentFrame = this.piskelController.getCurrentFrame();
|
|
|
|
if (!currentFrame.isSameSize(this.overlayFrame)) {
|
|
|
|
this.overlayFrame = pskl.model.Frame.createEmptyFromFrame(currentFrame);
|
2012-09-09 03:12:54 +04:00
|
|
|
}
|
2012-09-04 16:10:16 +04:00
|
|
|
|
2013-11-01 20:12:59 +04:00
|
|
|
this.layersRenderer.render();
|
|
|
|
this.renderer.render(currentFrame);
|
|
|
|
this.overlayRenderer.render(this.overlayFrame);
|
2012-09-14 02:33:46 +04:00
|
|
|
};
|
|
|
|
|
2012-09-16 20:48:32 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2013-10-30 01:16:39 +04:00
|
|
|
ns.DrawingController.prototype.calculateZoom_ = function() {
|
|
|
|
var frameHeight = this.piskelController.getCurrentFrame().getHeight(),
|
|
|
|
frameWidth = this.piskelController.getCurrentFrame().getWidth();
|
2013-09-26 09:47:11 +04:00
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
return Math.min(this.getAvailableWidth_()/frameWidth, this.getAvailableHeight_()/frameHeight);
|
2013-09-26 09:47:11 +04:00
|
|
|
};
|
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
ns.DrawingController.prototype.getAvailableHeight_ = function () {
|
|
|
|
return $('#main-wrapper').height();
|
2013-09-26 09:47:11 +04:00
|
|
|
};
|
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
ns.DrawingController.prototype.getAvailableWidth_ = function () {
|
|
|
|
var leftSectionWidth = $('.left-column').outerWidth(true),
|
|
|
|
rightSectionWidth = $('.right-column').outerWidth(true),
|
|
|
|
availableWidth = $('#main-wrapper').width() - leftSectionWidth - rightSectionWidth;
|
|
|
|
return availableWidth;
|
|
|
|
};
|
2013-06-19 21:01:12 +04:00
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
ns.DrawingController.prototype.getContainerHeight_ = function () {
|
|
|
|
return this.calculateZoom_() * this.piskelController.getCurrentFrame().getHeight();
|
2012-09-16 20:48:32 +04:00
|
|
|
};
|
2013-06-14 15:17:20 +04:00
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
ns.DrawingController.prototype.getContainerWidth_ = function () {
|
|
|
|
return this.calculateZoom_() * this.piskelController.getCurrentFrame().getWidth();
|
2012-09-16 20:48:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2013-11-01 18:39:42 +04:00
|
|
|
ns.DrawingController.prototype.centerColumnWrapperHorizontally_ = function() {
|
|
|
|
var containerHeight = this.getContainerHeight_();
|
|
|
|
var verticalGapInPixel = Math.floor(($('#main-wrapper').height() - containerHeight) / 2);
|
2013-06-19 21:01:12 +04:00
|
|
|
$('#column-wrapper').css({
|
2013-11-01 18:39:42 +04:00
|
|
|
'top': verticalGapInPixel + 'px'
|
2013-06-19 21:01:12 +04:00
|
|
|
});
|
2012-09-16 20:48:32 +04:00
|
|
|
};
|
2013-10-30 01:16:39 +04:00
|
|
|
|
2013-11-02 02:11:11 +04:00
|
|
|
ns.DrawingController.prototype.getRenderer = function () {
|
|
|
|
return this.compositeRenderer;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.DrawingController.prototype.setOffset = function (x, y) {
|
|
|
|
this.compositeRenderer.setOffset(x, y);
|
|
|
|
pskl.app.minimapController.onDrawingControllerMove_();
|
2013-10-30 01:16:39 +04:00
|
|
|
};
|
2012-09-04 16:10:16 +04:00
|
|
|
})();
|