2013-11-02 02:11:11 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace('pskl.controller');
|
|
|
|
|
|
|
|
ns.MinimapController = function (piskelController, animationController, drawingController, container) {
|
|
|
|
this.piskelController = piskelController;
|
|
|
|
this.animationController = animationController;
|
|
|
|
this.drawingController = drawingController;
|
2014-12-18 23:42:03 +03:00
|
|
|
this.container = container.parent();
|
2013-11-02 02:11:11 +04:00
|
|
|
|
|
|
|
this.isClicked = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.MinimapController.prototype.init = function () {
|
2013-11-02 14:24:02 +04:00
|
|
|
// Create minimap DOM elements
|
2013-11-02 02:11:11 +04:00
|
|
|
this.cropFrame = document.createElement('DIV');
|
|
|
|
this.cropFrame.className = 'minimap-crop-frame';
|
|
|
|
this.cropFrame.style.display = 'none';
|
|
|
|
$(this.container).append(this.cropFrame);
|
2013-11-02 14:24:02 +04:00
|
|
|
|
|
|
|
// Init mouse events
|
|
|
|
$(this.container).mousedown(this.onMinimapMousedown_.bind(this));
|
|
|
|
$('body').mousemove(this.onMinimapMousemove_.bind(this));
|
|
|
|
$('body').mouseup(this.onMinimapMouseup_.bind(this));
|
2013-12-06 01:12:48 +04:00
|
|
|
|
|
|
|
$.subscribe(Events.ZOOM_CHANGED, $.proxy(this.renderMinimap_, this));
|
2013-11-02 02:11:11 +04:00
|
|
|
};
|
|
|
|
|
2013-12-06 01:12:48 +04:00
|
|
|
ns.MinimapController.prototype.renderMinimap_ = function () {
|
2013-11-02 02:11:11 +04:00
|
|
|
var zoomRatio = this.getDrawingAreaZoomRatio_();
|
2014-12-18 23:42:03 +03:00
|
|
|
console.log('zoomRatio', zoomRatio);
|
|
|
|
console.log('this.animationController.getZoom()', this.animationController.getZoom());
|
2013-11-02 02:11:11 +04:00
|
|
|
if (zoomRatio > 1) {
|
|
|
|
this.displayCropFrame_(zoomRatio, this.drawingController.getRenderer().getOffset());
|
|
|
|
} else {
|
|
|
|
this.hideCropFrame_();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.MinimapController.prototype.displayCropFrame_ = function (ratio, offset) {
|
|
|
|
this.cropFrame.style.display = 'block';
|
2014-12-18 23:42:03 +03:00
|
|
|
|
|
|
|
var containerHeight = this.container.height();
|
|
|
|
var containerWidth = this.container.width();
|
|
|
|
var displaySize = this.drawingController.getRenderer().getDisplaySize();
|
|
|
|
var width = displaySize.width / ratio;
|
|
|
|
var height = displaySize.height / ratio;
|
|
|
|
this.cropFrame.style.width = Math.min(width, containerWidth) + 'px';
|
|
|
|
this.cropFrame.style.height = Math.min(height, containerHeight) + 'px';
|
|
|
|
|
|
|
|
|
|
|
|
var containerSize = Math.max(containerHeight, containerWidth);
|
|
|
|
var margin = this.drawingController.renderer.margin;
|
|
|
|
|
|
|
|
var frame = this.piskelController.getCurrentFrame();
|
|
|
|
var framePreviewWidth = frame.getWidth() * this.animationController.getZoom();
|
|
|
|
var framePreviewHeight = frame.getHeight() * this.animationController.getZoom();
|
|
|
|
|
|
|
|
var left = (containerSize - Math.max(width, framePreviewWidth))/2;
|
|
|
|
left += offset.x * this.animationController.getZoom();
|
|
|
|
left = Math.max(0, left);
|
|
|
|
this.cropFrame.style.left = left + 'px';
|
|
|
|
|
|
|
|
var top = (containerSize - Math.max(height, framePreviewHeight))/2;
|
|
|
|
top += offset.y * this.animationController.getZoom();
|
|
|
|
top = Math.max(0, top);
|
|
|
|
this.cropFrame.style.top = top + 'px';
|
|
|
|
|
2013-11-02 02:11:11 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.MinimapController.prototype.hideCropFrame_ = function () {
|
|
|
|
this.cropFrame.style.display = 'none';
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.MinimapController.prototype.onMinimapMousemove_ = function (evt) {
|
|
|
|
if (this.isClicked) {
|
|
|
|
if (this.getDrawingAreaZoomRatio_() > 1) {
|
|
|
|
var coords = this.getCoordinatesCenteredAround_(evt.clientX, evt.clientY);
|
|
|
|
this.drawingController.setOffset(coords.x, coords.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.MinimapController.prototype.onMinimapMousedown_ = function (evt) {
|
|
|
|
this.isClicked = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.MinimapController.prototype.onMinimapMouseup_ = function (evt) {
|
|
|
|
this.isClicked = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.MinimapController.prototype.getCoordinatesCenteredAround_ = function (x, y) {
|
2014-05-08 22:45:05 +04:00
|
|
|
var frameCoords = this.animationController.getCoordinates(x, y);
|
2013-11-02 02:11:11 +04:00
|
|
|
var zoomRatio = this.getDrawingAreaZoomRatio_();
|
|
|
|
var frameWidth = this.piskelController.getCurrentFrame().getWidth();
|
|
|
|
var frameHeight = this.piskelController.getCurrentFrame().getHeight();
|
|
|
|
|
|
|
|
var width = frameWidth / zoomRatio;
|
|
|
|
var height = frameHeight / zoomRatio;
|
|
|
|
|
|
|
|
return {
|
|
|
|
x : frameCoords.x - (width/2),
|
|
|
|
y : frameCoords.y - (height/2)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.MinimapController.prototype.getDrawingAreaZoomRatio_ = function () {
|
|
|
|
var drawingAreaZoom = this.drawingController.getRenderer().getZoom();
|
2014-12-18 23:42:03 +03:00
|
|
|
var frame = this.piskelController.getCurrentFrame();
|
|
|
|
var dim = Math.max(frame.getHeight(), frame.getWidth());
|
|
|
|
var drawingAreaSize = dim * drawingAreaZoom;
|
|
|
|
|
|
|
|
var containerHeight = this.container.height();
|
|
|
|
var containerWidth = this.container.width();
|
|
|
|
|
|
|
|
var containerSize = Math.max(containerHeight, containerWidth);
|
|
|
|
|
|
|
|
var zoomRatio = drawingAreaSize / containerSize;
|
2013-11-02 02:11:11 +04:00
|
|
|
|
|
|
|
return zoomRatio;
|
|
|
|
};
|
|
|
|
})();
|