2012-09-04 16:10:16 +04:00
|
|
|
(function () {
|
2014-05-15 01:52:32 +04:00
|
|
|
|
2015-04-14 19:02:33 +03:00
|
|
|
var ns = $.namespace('pskl.controller');
|
2014-05-15 01:52:32 +04:00
|
|
|
|
2017-01-15 14:35:38 +03:00
|
|
|
ns.DrawingController = function (piskelController, 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
|
|
|
|
2014-11-23 23:37:34 +03:00
|
|
|
this.dragHandler = new ns.drawing.DragHandler(this);
|
|
|
|
|
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
|
|
|
|
2015-04-14 19:02:33 +03:00
|
|
|
var cfg = {
|
|
|
|
'zoom': this.calculateZoom_(),
|
|
|
|
'supportGridRendering' : true,
|
|
|
|
'height' : this.getContainerHeight_(),
|
|
|
|
'width' : this.getContainerWidth_(),
|
|
|
|
'xOffset' : 0,
|
|
|
|
'yOffset' : 0
|
2012-09-16 20:48:32 +04:00
|
|
|
};
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2015-04-14 19:02:33 +03:00
|
|
|
this.overlayRenderer = new pskl.rendering.frame.CachedFrameRenderer(this.container, cfg, ['canvas-overlay']);
|
|
|
|
this.renderer = new pskl.rendering.frame.CachedFrameRenderer(this.container, cfg, ['drawing-canvas']);
|
|
|
|
this.onionSkinRenderer = pskl.rendering.OnionSkinRenderer.createInContainer(this.container, cfg, piskelController);
|
|
|
|
this.layersRenderer = new pskl.rendering.layer.LayersRenderer(this.container, cfg, 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)
|
2014-06-20 01:33:57 +04:00
|
|
|
.add(this.layersRenderer)
|
|
|
|
.add(this.onionSkinRenderer);
|
2012-09-14 02:33:46 +04:00
|
|
|
|
|
|
|
// State of drawing controller:
|
|
|
|
this.isClicked = 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-12-06 01:12:48 +04:00
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
$(window).resize($.proxy(this.startResizeTimer_, this));
|
2012-09-16 20:48:32 +04:00
|
|
|
|
2015-09-13 22:32:45 +03:00
|
|
|
$.subscribe(Events.USER_SETTINGS_CHANGED, this.onUserSettingsChange_.bind(this));
|
|
|
|
$.subscribe(Events.FRAME_SIZE_CHANGED, this.onFrameSizeChange_.bind(this));
|
2013-06-19 21:01:12 +04:00
|
|
|
|
2015-10-10 20:32:25 +03:00
|
|
|
var shortcuts = pskl.service.keyboard.Shortcuts;
|
|
|
|
pskl.app.shortcutService.registerShortcut(shortcuts.MISC.RESET_ZOOM, this.resetZoom_.bind(this));
|
2016-07-26 18:28:18 +03:00
|
|
|
pskl.app.shortcutService.registerShortcut(shortcuts.MISC.INCREASE_ZOOM, this.updateZoom_.bind(this, 1));
|
|
|
|
pskl.app.shortcutService.registerShortcut(shortcuts.MISC.DECREASE_ZOOM, this.updateZoom_.bind(this, -1));
|
2017-06-11 00:12:11 +03:00
|
|
|
pskl.app.shortcutService.registerShortcut(shortcuts.MISC.OFFSET_UP, this.updateOffset_.bind(this, 'up'));
|
|
|
|
pskl.app.shortcutService.registerShortcut(shortcuts.MISC.OFFSET_RIGHT, this.updateOffset_.bind(this, 'right'));
|
|
|
|
pskl.app.shortcutService.registerShortcut(shortcuts.MISC.OFFSET_DOWN, this.updateOffset_.bind(this, 'down'));
|
|
|
|
pskl.app.shortcutService.registerShortcut(shortcuts.MISC.OFFSET_LEFT, this.updateOffset_.bind(this, 'left'));
|
2014-05-15 01:52:32 +04:00
|
|
|
|
2014-12-18 23:42:03 +03:00
|
|
|
window.setTimeout(function () {
|
|
|
|
this.afterWindowResize_();
|
|
|
|
this.resetZoom_();
|
|
|
|
}.bind(this), 100);
|
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));
|
2013-11-15 03:32:18 +04:00
|
|
|
|
2015-01-24 02:00:08 +03:00
|
|
|
if (pskl.utils.UserAgent.isChrome || pskl.utils.UserAgent.isIE11) {
|
2013-11-15 03:32:18 +04:00
|
|
|
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
|
|
|
|
2014-04-03 00:37:01 +04:00
|
|
|
window.addEventListener('mouseup', this.onMouseup_.bind(this));
|
|
|
|
window.addEventListener('mousemove', this.onMousemove_.bind(this));
|
2014-05-17 00:36:23 +04:00
|
|
|
window.addEventListener('keyup', this.onKeyup_.bind(this));
|
2016-06-04 11:29:24 +03:00
|
|
|
window.addEventListener('touchstart', this.onTouchstart_.bind(this));
|
|
|
|
window.addEventListener('touchmove' , this.onTouchmove_.bind(this));
|
|
|
|
window.addEventListener('touchend', this.onTouchend_.bind(this));
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
// Deactivate right click:
|
|
|
|
body.contextmenu(this.onCanvasContextMenu_);
|
2014-12-18 23:42:03 +03:00
|
|
|
|
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);
|
2013-12-19 02:37:17 +04:00
|
|
|
};
|
2013-11-01 18:39:42 +04:00
|
|
|
|
|
|
|
ns.DrawingController.prototype.afterWindowResize_ = function () {
|
2013-12-06 01:12:48 +04:00
|
|
|
var initialWidth = this.compositeRenderer.getDisplaySize().width;
|
2014-12-18 23:42:03 +03:00
|
|
|
|
2013-11-01 20:12:59 +04:00
|
|
|
this.compositeRenderer.setDisplaySize(this.getContainerWidth_(), this.getContainerHeight_());
|
2013-12-06 01:12:48 +04:00
|
|
|
this.centerColumnWrapperHorizontally_();
|
|
|
|
var ratio = this.compositeRenderer.getDisplaySize().width / initialWidth;
|
|
|
|
var newZoom = ratio * this.compositeRenderer.getZoom();
|
|
|
|
this.compositeRenderer.setZoom(newZoom);
|
|
|
|
|
|
|
|
$.publish(Events.ZOOM_CHANGED);
|
2013-12-19 02:37:17 +04:00
|
|
|
};
|
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) {
|
2015-04-14 19:02:33 +03:00
|
|
|
if (settingsName == pskl.UserSettings.SHOW_GRID) {
|
2013-11-01 18:39:42 +04:00
|
|
|
console.warn('DrawingController:onUserSettingsChange_ not implemented !');
|
2014-07-04 21:17:02 +04:00
|
|
|
} else if (settingsName == pskl.UserSettings.ONION_SKIN || settingsName == pskl.UserSettings.LAYER_PREVIEW) {
|
2014-06-20 01:33:57 +04:00
|
|
|
this.onionSkinRenderer.clear();
|
|
|
|
this.onionSkinRenderer.flush();
|
|
|
|
this.layersRenderer.clear();
|
|
|
|
this.layersRenderer.flush();
|
|
|
|
this.render();
|
2013-06-17 22:24:27 +04:00
|
|
|
}
|
2013-12-19 02:37:17 +04:00
|
|
|
};
|
2013-06-17 22:24:27 +04:00
|
|
|
|
2015-02-17 04:06:52 +03:00
|
|
|
ns.DrawingController.prototype.onFrameSizeChange_ = function () {
|
2013-11-02 03:00:38 +04:00
|
|
|
this.compositeRenderer.setDisplaySize(this.getContainerWidth_(), this.getContainerHeight_());
|
2014-05-08 22:45:05 +04:00
|
|
|
this.centerColumnWrapperHorizontally_();
|
2013-12-06 01:12:48 +04:00
|
|
|
this.compositeRenderer.setZoom(this.calculateZoom_());
|
|
|
|
this.compositeRenderer.setOffset(0, 0);
|
|
|
|
$.publish(Events.ZOOM_CHANGED);
|
2013-11-02 03:00:38 +04:00
|
|
|
};
|
|
|
|
|
2016-06-04 11:29:24 +03:00
|
|
|
ns.DrawingController.prototype.onTouchstart_ = function (event) {
|
|
|
|
this.onMousedown_(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.DrawingController.prototype.onTouchmove_ = function (event) {
|
|
|
|
this.onMousemove_(event);
|
|
|
|
event.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.DrawingController.prototype.onTouchend_ = function (event) {
|
|
|
|
this.onMouseup_(event);
|
|
|
|
};
|
|
|
|
|
2012-09-14 02:33:46 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.onMousedown_ = function (event) {
|
2014-08-21 02:50:59 +04:00
|
|
|
$.publish(Events.MOUSE_EVENT, [event, this]);
|
2013-12-06 01:12:48 +04:00
|
|
|
var frame = this.piskelController.getCurrentFrame();
|
2014-08-21 02:50:59 +04:00
|
|
|
var coords = this.getSpriteCoordinates(event.clientX, event.clientY);
|
2015-11-11 16:05:28 +03:00
|
|
|
if (event.changedTouches && event.changedTouches[0]) {
|
2015-11-05 18:12:27 +03:00
|
|
|
coords = this.getSpriteCoordinates(event.changedTouches[0].clientX, event.changedTouches[0].clientY);
|
2015-11-03 05:34:13 +03:00
|
|
|
}
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2014-11-23 23:37:34 +03:00
|
|
|
this.isClicked = true;
|
|
|
|
|
2013-12-06 01:12:48 +04:00
|
|
|
if (event.button === Constants.MIDDLE_BUTTON) {
|
2014-11-23 23:37:34 +03:00
|
|
|
this.dragHandler.startDrag(event.clientX, event.clientY);
|
2017-03-02 03:39:28 +03:00
|
|
|
} else if (event.altKey && !this.currentToolBehavior.supportsAlt()) {
|
|
|
|
this.currentToolBehavior.hideHighlightedPixel(this.overlayFrame);
|
|
|
|
this.isPickingColor = true;
|
2013-12-06 01:12:48 +04:00
|
|
|
} else {
|
2013-12-19 02:37:17 +04:00
|
|
|
this.currentToolBehavior.hideHighlightedPixel(this.overlayFrame);
|
2014-12-19 01:57:34 +03:00
|
|
|
$.publish(Events.TOOL_PRESSED);
|
2013-12-06 01:12:48 +04:00
|
|
|
this.currentToolBehavior.applyToolAt(
|
|
|
|
coords.x,
|
|
|
|
coords.y,
|
|
|
|
frame,
|
|
|
|
this.overlayFrame,
|
|
|
|
event
|
|
|
|
);
|
|
|
|
}
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
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) {
|
2014-05-17 00:36:23 +04:00
|
|
|
this._clientX = event.clientX;
|
|
|
|
this._clientY = event.clientY;
|
2015-11-11 16:05:28 +03:00
|
|
|
if (event.changedTouches && event.changedTouches[0]) {
|
2015-11-05 18:12:27 +03:00
|
|
|
this._clientX = event.changedTouches[0].clientX;
|
|
|
|
this._clientY = event.changedTouches[0].clientY;
|
2015-11-03 05:34:13 +03:00
|
|
|
}
|
2014-05-17 00:36:23 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var currentTime = new Date().getTime();
|
|
|
|
// Throttling of the mousemove event:
|
2013-12-06 01:12:48 +04:00
|
|
|
|
2015-04-14 19:02:33 +03:00
|
|
|
if ((currentTime - this.previousMousemoveTime) > Constants.MOUSEMOVE_THROTTLING) {
|
2014-05-17 00:36:23 +04:00
|
|
|
this.moveTool_(this._clientX, this._clientY, event);
|
2013-08-10 14:11:16 +04:00
|
|
|
this.previousMousemoveTime = currentTime;
|
|
|
|
}
|
|
|
|
};
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2014-05-17 00:36:23 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.onKeyup_ = function (event) {
|
|
|
|
this.moveTool_(this._clientX, this._clientY, event);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.DrawingController.prototype.moveTool_ = function (x, y, event) {
|
2014-08-21 02:50:59 +04:00
|
|
|
var coords = this.getSpriteCoordinates(x, y);
|
2014-05-17 00:36:23 +04:00
|
|
|
var currentFrame = this.piskelController.getCurrentFrame();
|
|
|
|
|
|
|
|
if (this.isClicked) {
|
2015-09-16 21:27:51 +03:00
|
|
|
if (pskl.app.mouseStateService.isMiddleButtonPressed()) {
|
2014-11-23 23:37:34 +03:00
|
|
|
this.dragHandler.updateDrag(x, y);
|
2017-03-02 03:39:28 +03:00
|
|
|
} else if (this.isPickingColor) {
|
|
|
|
// Nothing to do on mousemove when picking a color with ALT+click.
|
2014-11-23 23:37:34 +03:00
|
|
|
} else {
|
|
|
|
$.publish(Events.MOUSE_EVENT, [event, this]);
|
|
|
|
this.currentToolBehavior.moveToolAt(
|
|
|
|
coords.x | 0,
|
|
|
|
coords.y | 0,
|
|
|
|
currentFrame,
|
|
|
|
this.overlayFrame,
|
|
|
|
event
|
|
|
|
);
|
|
|
|
}
|
2014-05-17 00:36:23 +04:00
|
|
|
} else {
|
|
|
|
this.currentToolBehavior.moveUnactiveToolAt(
|
|
|
|
coords.x,
|
|
|
|
coords.y,
|
|
|
|
currentFrame,
|
|
|
|
this.overlayFrame,
|
|
|
|
event
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$.publish(Events.CURSOR_MOVED, [coords.x, coords.y]);
|
2014-05-17 00:40:09 +04:00
|
|
|
};
|
2014-05-17 00:36:23 +04:00
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
ns.DrawingController.prototype.onMousewheel_ = function (jQueryEvent) {
|
2015-09-13 23:44:59 +03:00
|
|
|
var evt = jQueryEvent.originalEvent;
|
2014-11-01 16:00:35 +03:00
|
|
|
// Ratio between wheelDeltaY (mousewheel event) and deltaY (wheel event) is -40
|
2015-01-24 02:00:08 +03:00
|
|
|
var delta;
|
2016-10-10 19:14:06 +03:00
|
|
|
if (pskl.utils.UserAgent.isIE11) {
|
2015-09-13 23:44:59 +03:00
|
|
|
delta = evt.wheelDelta;
|
2015-01-24 02:00:08 +03:00
|
|
|
} else if (pskl.utils.UserAgent.isFirefox) {
|
2015-09-13 23:44:59 +03:00
|
|
|
delta = -40 * evt.deltaY;
|
2016-10-10 19:14:06 +03:00
|
|
|
} else {
|
|
|
|
delta = evt.wheelDeltaY;
|
2015-01-24 02:00:08 +03:00
|
|
|
}
|
2016-10-10 19:14:06 +03:00
|
|
|
|
|
|
|
delta = delta || 0;
|
2016-07-26 18:28:18 +03:00
|
|
|
var modifier = (delta / 120);
|
2015-09-13 23:44:59 +03:00
|
|
|
|
|
|
|
if (pskl.utils.UserAgent.isMac ? evt.metaKey : evt.ctrlKey) {
|
|
|
|
modifier = modifier * 5;
|
|
|
|
// prevent default to prevent the default browser UI resize
|
|
|
|
evt.preventDefault();
|
|
|
|
}
|
|
|
|
|
2016-07-30 01:01:32 +03:00
|
|
|
var coords = this.getSpriteCoordinates(evt.clientX, evt.clientY);
|
|
|
|
this.updateZoom_(modifier, coords);
|
2013-10-30 01:16:39 +04:00
|
|
|
};
|
|
|
|
|
2017-06-11 00:12:11 +03:00
|
|
|
/**
|
|
|
|
* Update the current viewport offset of 1 pixel in the provided direction.
|
|
|
|
* Direction can be one of 'up', 'right', 'down', 'left'.
|
|
|
|
* Callback for the OFFSET_${DIR} shortcuts.
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.updateOffset_ = function (direction) {
|
|
|
|
var off = this.getOffset();
|
|
|
|
if (direction === 'up') {
|
|
|
|
off.y -= 1;
|
|
|
|
} else if (direction === 'right') {
|
|
|
|
off.x += 1;
|
|
|
|
} else if (direction === 'down') {
|
|
|
|
off.y += 1;
|
|
|
|
} else if (direction === 'left') {
|
|
|
|
off.x -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setOffset(
|
|
|
|
off.x,
|
|
|
|
off.y
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-07-30 01:01:32 +03:00
|
|
|
/**
|
|
|
|
* Update the current zoom level by a given multiplier.
|
|
|
|
*
|
|
|
|
* @param {Number} zoomMultiplier: factor by which the zoom should be modified. Negative
|
|
|
|
* values will decrease the zoom, positive values will increase it.
|
|
|
|
* @param {Object} centerCoords, optional:
|
|
|
|
* - {Number} x: x coordinate of the desired center the zoomed canvas
|
|
|
|
* - {Number} y: y coordinate of the desired center the zoomed canvas
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.updateZoom_ = function (zoomMultiplier, centerCoords) {
|
|
|
|
if (zoomMultiplier === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-18 02:30:21 +03:00
|
|
|
var off = this.getOffset();
|
|
|
|
var oldWidth = this.getContainerWidth_() / this.renderer.getZoom();
|
|
|
|
var oldHeight = this.getContainerHeight_() / this.renderer.getZoom();
|
2016-07-30 01:01:32 +03:00
|
|
|
|
|
|
|
var step = zoomMultiplier * this.getZoomStep_();
|
2015-01-24 02:00:08 +03:00
|
|
|
this.setZoom_(this.renderer.getZoom() + step);
|
2016-07-30 01:01:32 +03:00
|
|
|
|
2016-08-16 10:53:14 +03:00
|
|
|
if (typeof centerCoords === 'object') {
|
2016-07-30 01:01:32 +03:00
|
|
|
var xRatio = (centerCoords.x - off.x) / oldWidth;
|
|
|
|
var yRatio = (centerCoords.y - off.y) / oldHeight;
|
|
|
|
var newWidth = this.getContainerWidth_() / this.renderer.getZoom();
|
|
|
|
var newHeight = this.getContainerHeight_() / this.renderer.getZoom();
|
|
|
|
this.setOffset(
|
|
|
|
off.x - ((newWidth - oldWidth) * xRatio),
|
|
|
|
off.y - ((newHeight - oldHeight) * yRatio)
|
|
|
|
);
|
|
|
|
}
|
2015-01-24 02:00:08 +03:00
|
|
|
};
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.onMouseup_ = function (event) {
|
2017-03-02 03:39:28 +03:00
|
|
|
if (!this.isClicked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-23 23:37:34 +03:00
|
|
|
var coords = this.getSpriteCoordinates(event.clientX, event.clientY);
|
2015-11-11 16:05:28 +03:00
|
|
|
if (event.changedTouches && event.changedTouches[0]) {
|
2015-11-05 18:12:27 +03:00
|
|
|
coords = this.getSpriteCoordinates(event.changedTouches[0].clientX, event.changedTouches[0].clientY);
|
2015-11-03 05:34:13 +03:00
|
|
|
}
|
2013-04-09 09:24:07 +04:00
|
|
|
|
2017-03-02 03:39:28 +03:00
|
|
|
// 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-08 20:44:06 +04:00
|
|
|
|
2017-03-02 03:39:28 +03:00
|
|
|
this.isClicked = false;
|
2014-11-23 23:37:34 +03:00
|
|
|
|
2017-03-02 03:39:28 +03:00
|
|
|
var isMiddleButton = pskl.app.mouseStateService.isMiddleButtonPressed();
|
|
|
|
var isMiddleClick = isMiddleButton && !this.dragHandler.isDragging();
|
|
|
|
var isMiddleDrag = isMiddleButton && this.dragHandler.isDragging();
|
|
|
|
|
|
|
|
if (this.isPickingColor || isMiddleClick) {
|
|
|
|
// Picking color after ALT+click or middle mouse button click.
|
|
|
|
this.pickColorAt_(coords);
|
|
|
|
this.isPickingColor = false;
|
|
|
|
} else if (isMiddleDrag) {
|
|
|
|
// Stop the drag handler after a middle button drag action.
|
|
|
|
this.dragHandler.stopDrag();
|
|
|
|
} else {
|
|
|
|
// Regular tool click, release the current tool.
|
|
|
|
this.currentToolBehavior.releaseToolAt(
|
|
|
|
coords.x,
|
|
|
|
coords.y,
|
|
|
|
this.piskelController.getCurrentFrame(),
|
|
|
|
this.overlayFrame,
|
|
|
|
event
|
|
|
|
);
|
|
|
|
$.publish(Events.TOOL_RELEASED);
|
|
|
|
}
|
|
|
|
|
|
|
|
$.publish(Events.MOUSE_EVENT, [event, this]);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a COLOR selection event for the color contained at the provided coordinates.
|
|
|
|
* No-op if the coordinate is outside of the drawing canvas.
|
|
|
|
* @param {Object} coords {x: Number, y: Number}
|
|
|
|
*/
|
|
|
|
ns.DrawingController.prototype.pickColorAt_ = function (coords) {
|
|
|
|
var frame = this.piskelController.getCurrentFrame();
|
|
|
|
if (!frame.containsPixel(coords.x, coords.y)) {
|
|
|
|
return;
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
2017-03-02 03:39:28 +03:00
|
|
|
|
|
|
|
var color = pskl.utils.intToColor(frame.getPixel(coords.x, coords.y));
|
|
|
|
var isRightButton = pskl.app.mouseStateService.isRightButtonPressed();
|
|
|
|
var evt = isRightButton ? Events.SELECT_SECONDARY_COLOR : Events.SELECT_PRIMARY_COLOR;
|
|
|
|
$.publish(evt, [color]);
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
2014-07-13 02:21:36 +04:00
|
|
|
* Translate absolute x,y screen coordinates into sprite coordinates
|
|
|
|
* @param {Number} screenX
|
|
|
|
* @param {Number} screenY
|
|
|
|
* @return {Object} {x:Number, y:Number}
|
2013-08-10 14:11:16 +04:00
|
|
|
*/
|
2014-07-13 02:21:36 +04:00
|
|
|
ns.DrawingController.prototype.getSpriteCoordinates = function(screenX, screenY) {
|
|
|
|
return this.renderer.getCoordinates(screenX, screenY);
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
|
|
|
|
2014-08-21 02:50:59 +04:00
|
|
|
ns.DrawingController.prototype.getScreenCoordinates = function(spriteX, spriteY) {
|
|
|
|
return this.renderer.reverseCoordinates(spriteX, spriteY);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2014-07-04 21:17:02 +04:00
|
|
|
if (pskl.UserSettings.get(pskl.UserSettings.ONION_SKIN)) {
|
2014-06-20 01:33:57 +04:00
|
|
|
this.onionSkinRenderer.render();
|
2014-07-04 21:17:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pskl.UserSettings.get(pskl.UserSettings.LAYER_PREVIEW)) {
|
2014-06-20 01:33:57 +04:00
|
|
|
this.layersRenderer.render();
|
|
|
|
}
|
|
|
|
|
2013-11-01 20:12:59 +04:00
|
|
|
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() {
|
2015-04-14 19:02:33 +03:00
|
|
|
var frameHeight = this.piskelController.getCurrentFrame().getHeight();
|
|
|
|
var frameWidth = this.piskelController.getCurrentFrame().getWidth();
|
2013-09-26 09:47:11 +04:00
|
|
|
|
2015-04-14 19:02:33 +03: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 () {
|
2015-04-14 19:02:33 +03:00
|
|
|
var leftSectionWidth = $('.left-column').outerWidth(true);
|
|
|
|
var rightSectionWidth = $('.right-column').outerWidth(true);
|
|
|
|
var toolsContainerWidth = $('#tool-section').outerWidth(true);
|
|
|
|
var settingsContainerWidth = $('#application-action-section').outerWidth(true);
|
|
|
|
|
|
|
|
var usedWidth = leftSectionWidth + rightSectionWidth + toolsContainerWidth + settingsContainerWidth;
|
|
|
|
var availableWidth = $('#main-wrapper').width() - usedWidth;
|
2013-12-06 01:12:48 +04:00
|
|
|
|
2014-03-30 23:55:29 +04:00
|
|
|
var comfortMargin = 10;
|
|
|
|
return availableWidth - comfortMargin;
|
2013-10-30 01:16:39 +04:00
|
|
|
};
|
2013-06-19 21:01:12 +04:00
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
ns.DrawingController.prototype.getContainerHeight_ = function () {
|
2014-12-18 23:42:03 +03:00
|
|
|
return this.getAvailableHeight_();
|
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 () {
|
2014-12-18 23:42:03 +03:00
|
|
|
return this.getAvailableWidth_();
|
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;
|
|
|
|
};
|
|
|
|
|
2014-11-23 23:37:34 +03:00
|
|
|
ns.DrawingController.prototype.getOffset = function () {
|
|
|
|
return this.compositeRenderer.getOffset();
|
|
|
|
};
|
|
|
|
|
2013-11-02 02:11:11 +04:00
|
|
|
ns.DrawingController.prototype.setOffset = function (x, y) {
|
|
|
|
this.compositeRenderer.setOffset(x, y);
|
2013-12-06 01:12:48 +04:00
|
|
|
$.publish(Events.ZOOM_CHANGED);
|
2013-10-30 01:16:39 +04:00
|
|
|
};
|
2014-11-23 23:37:34 +03:00
|
|
|
|
|
|
|
ns.DrawingController.prototype.resetZoom_ = function () {
|
|
|
|
this.setZoom_(this.calculateZoom_());
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.DrawingController.prototype.getZoomStep_ = function () {
|
2015-09-13 23:44:59 +03:00
|
|
|
return Math.max(0.1, this.renderer.getZoom() / 15);
|
2014-11-23 23:37:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.DrawingController.prototype.setZoom_ = function (zoom) {
|
|
|
|
this.compositeRenderer.setZoom(zoom);
|
|
|
|
$.publish(Events.ZOOM_CHANGED);
|
|
|
|
};
|
|
|
|
|
2015-04-14 19:02:33 +03:00
|
|
|
})();
|