2012-09-05 02:09:42 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.controller");
|
|
|
|
ns.PreviewFilmController = function (framesheet, container, dpi) {
|
|
|
|
|
|
|
|
this.dpi = dpi;
|
|
|
|
this.framesheet = framesheet;
|
|
|
|
this.container = container;
|
2012-09-08 20:44:06 +04:00
|
|
|
|
2012-09-11 03:07:03 +04:00
|
|
|
this.redrawFlag = true;
|
2012-09-09 03:12:54 +04:00
|
|
|
|
2012-09-11 01:26:12 +04:00
|
|
|
$.subscribe(Events.TOOL_RELEASED, this.flagForRedraw_.bind(this));
|
|
|
|
$.subscribe(Events.FRAMESHEET_RESET, this.flagForRedraw_.bind(this));
|
2012-09-05 02:09:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.PreviewFilmController.prototype.init = function() {
|
2012-09-09 03:12:54 +04:00
|
|
|
var addFrameButton = $('#add-frame-button')[0];
|
|
|
|
addFrameButton.addEventListener('mousedown', this.addFrame.bind(this));
|
2012-09-05 02:09:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.PreviewFilmController.prototype.addFrame = function () {
|
|
|
|
this.framesheet.addEmptyFrame();
|
2012-09-11 01:26:12 +04:00
|
|
|
this.framesheet.setCurrentFrameIndex(this.framesheet.getFrameCount() - 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.PreviewFilmController.prototype.flagForRedraw_ = function () {
|
|
|
|
this.redrawFlag = true;
|
2012-09-05 02:09:42 +04:00
|
|
|
};
|
|
|
|
|
2012-09-09 02:40:05 +04:00
|
|
|
ns.PreviewFilmController.prototype.render = function () {
|
2012-09-11 01:26:12 +04:00
|
|
|
if (this.redrawFlag) {
|
|
|
|
// TODO(vincz): Full redraw on any drawing modification, optimize.
|
|
|
|
this.createPreviews_();
|
|
|
|
this.redrawFlag = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.PreviewFilmController.prototype.createPreviews_ = function () {
|
2012-09-09 03:12:54 +04:00
|
|
|
this.container.html("");
|
|
|
|
|
|
|
|
var frameCount = this.framesheet.getFrameCount();
|
|
|
|
|
|
|
|
for (var i = 0, l = frameCount; i < l ; i++) {
|
2012-09-11 01:26:12 +04:00
|
|
|
this.container.append(this.createInterstitialTile_(i));
|
|
|
|
this.container.append(this.createPreviewTile_(i));
|
2012-09-09 03:12:54 +04:00
|
|
|
}
|
|
|
|
this.container.append(this.createInterstitialTile_(frameCount));
|
|
|
|
|
|
|
|
var needDragndropBehavior = !!(frameCount > 1);
|
|
|
|
if(needDragndropBehavior) {
|
|
|
|
this.initDragndropBehavior_();
|
|
|
|
}
|
2012-09-05 02:09:42 +04:00
|
|
|
};
|
|
|
|
|
2012-09-08 04:50:59 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.PreviewFilmController.prototype.createInterstitialTile_ = function (tileNumber) {
|
2012-09-11 01:26:12 +04:00
|
|
|
var interstitialTile = document.createElement("div");
|
|
|
|
interstitialTile.className = "interstitial-tile"
|
|
|
|
interstitialTile.setAttribute("data-tile-type", "interstitial");
|
|
|
|
interstitialTile.setAttribute("data-inject-drop-tile-at", tileNumber);
|
2012-09-08 04:50:59 +04:00
|
|
|
|
2012-09-11 01:26:12 +04:00
|
|
|
return interstitialTile;
|
2012-09-08 04:50:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.PreviewFilmController.prototype.initDragndropBehavior_ = function () {
|
2012-09-08 01:14:25 +04:00
|
|
|
var tiles = $(".preview-tile");
|
2012-09-08 04:50:59 +04:00
|
|
|
// Each preview film tile is draggable.
|
2012-09-08 01:14:25 +04:00
|
|
|
tiles.draggable( {
|
2012-09-08 04:50:59 +04:00
|
|
|
//containment: '.left-nav',
|
2012-09-08 01:14:25 +04:00
|
|
|
stack: '.preview-tile',
|
|
|
|
cursor: 'move',
|
2012-09-08 04:50:59 +04:00
|
|
|
revert: true,
|
|
|
|
start: function(event, ui) {
|
|
|
|
// We only show the fake interstitial tiles when starting the
|
|
|
|
// drag n drop interaction. We hide them when the DnD is done.
|
|
|
|
$('#preview-list').addClass("show-interstitial-tiles");
|
|
|
|
},
|
|
|
|
stop: function() {
|
|
|
|
$('#preview-list').removeClass("show-interstitial-tiles");
|
|
|
|
}
|
2012-09-08 01:14:25 +04:00
|
|
|
});
|
|
|
|
|
2012-09-08 04:50:59 +04:00
|
|
|
|
|
|
|
// Each preview film tile is a drop target. This allow us to swap two tiles.
|
|
|
|
// However, we want to be able to insert a tile between two other tiles.
|
|
|
|
// For that we created fake interstitial tiles that are used as drop targets as well.
|
|
|
|
var droppableTiles = $(".interstitial-tile");
|
|
|
|
$.merge(droppableTiles, tiles);
|
|
|
|
|
|
|
|
droppableTiles.droppable( {
|
2012-09-08 01:14:25 +04:00
|
|
|
accept: ".preview-tile",
|
2012-09-08 04:50:59 +04:00
|
|
|
tolerance: "pointer",
|
2012-09-08 01:14:25 +04:00
|
|
|
activeClass: "droppable-active",
|
|
|
|
hoverClass: "droppable-hover-active",
|
|
|
|
drop: $.proxy(this.onDrop_, this)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.PreviewFilmController.prototype.onDrop_ = function( event, ui ) {
|
2012-09-08 04:50:59 +04:00
|
|
|
var activeFrame;
|
2012-09-08 18:56:07 +04:00
|
|
|
// When we drag from an element, the drag could start from a nested DOM element
|
|
|
|
// inside the drag target. We normalize that by taking the correct ancestor:
|
|
|
|
var originTile = $(event.srcElement).closest(".preview-tile");
|
|
|
|
var originFrameId = parseInt(originTile.data("tile-number"), 10);
|
2012-09-11 01:26:12 +04:00
|
|
|
|
2012-09-08 04:50:59 +04:00
|
|
|
var dropTarget = $(event.target);
|
|
|
|
if(dropTarget.data("tile-type") == "interstitial") {
|
|
|
|
var targetInsertionId = parseInt(dropTarget.data("inject-drop-tile-at"), 10);
|
|
|
|
// In case we drop outside of the tile container
|
|
|
|
if(isNaN(originFrameId) || isNaN(targetInsertionId)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-08 18:56:07 +04:00
|
|
|
//console.log("origin-frame: "+originFrameId+" - targetInsertionId: "+ targetInsertionId)
|
2012-09-08 04:50:59 +04:00
|
|
|
this.framesheet.moveFrame(originFrameId, targetInsertionId);
|
|
|
|
|
|
|
|
activeFrame = targetInsertionId;
|
|
|
|
// The last fake interstitial tile is outside of the framesheet array bound.
|
|
|
|
// It allow us to append after the very last element in this fake slot.
|
|
|
|
// However, when setting back the active frame, we have to make sure the
|
|
|
|
// frame does exist.
|
|
|
|
if(activeFrame > (this.framesheet.getFrameCount() - 1)) {
|
|
|
|
activeFrame = targetInsertionId - 1;
|
|
|
|
}
|
2012-09-11 01:26:12 +04:00
|
|
|
} else {
|
2012-09-08 04:50:59 +04:00
|
|
|
var targetSwapId = parseInt(dropTarget.data("tile-number"), 10);
|
|
|
|
// In case we drop outside of the tile container
|
|
|
|
if(isNaN(originFrameId) || isNaN(targetSwapId)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-09-08 18:56:07 +04:00
|
|
|
//console.log("origin-frame: "+originFrameId+" - targetSwapId: "+ targetSwapId)
|
2012-09-08 04:50:59 +04:00
|
|
|
this.framesheet.swapFrames(originFrameId, targetSwapId);
|
|
|
|
activeFrame = targetSwapId;
|
2012-09-08 01:25:38 +04:00
|
|
|
}
|
2012-09-08 01:14:25 +04:00
|
|
|
|
2012-09-08 04:50:59 +04:00
|
|
|
|
|
|
|
$('#preview-list').removeClass("show-interstitial-tiles");
|
|
|
|
|
2012-09-11 01:26:12 +04:00
|
|
|
this.framesheet.setCurrentFrameIndex(activeFrame);
|
2012-09-08 01:14:25 +04:00
|
|
|
|
|
|
|
// TODO(vincz): move localstorage request to the model layer?
|
|
|
|
$.publish(Events.LOCALSTORAGE_REQUEST);
|
|
|
|
};
|
|
|
|
|
2012-09-08 04:50:59 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* TODO(vincz): clean this giant rendering function & remove listeners.
|
|
|
|
*/
|
2012-09-11 01:26:12 +04:00
|
|
|
ns.PreviewFilmController.prototype.createPreviewTile_ = function(tileNumber) {
|
2012-09-08 17:08:00 +04:00
|
|
|
var currentFrame = this.framesheet.getFrameByIndex(tileNumber);
|
2012-09-08 20:45:18 +04:00
|
|
|
|
2012-09-05 02:09:42 +04:00
|
|
|
var previewTileRoot = document.createElement("li");
|
|
|
|
var classname = "preview-tile";
|
2012-09-08 01:14:25 +04:00
|
|
|
previewTileRoot.setAttribute("data-tile-number", tileNumber);
|
2012-09-05 02:09:42 +04:00
|
|
|
|
2012-09-11 01:26:12 +04:00
|
|
|
if (this.framesheet.getCurrentFrame() == currentFrame) {
|
2012-09-05 02:09:42 +04:00
|
|
|
classname += " selected";
|
|
|
|
}
|
|
|
|
previewTileRoot.className = classname;
|
|
|
|
|
|
|
|
var canvasContainer = document.createElement("div");
|
|
|
|
canvasContainer.className = "canvas-container";
|
2012-09-08 20:45:18 +04:00
|
|
|
|
2012-09-05 02:09:42 +04:00
|
|
|
var canvasBackground = document.createElement("div");
|
|
|
|
canvasBackground.className = "canvas-background";
|
|
|
|
canvasContainer.appendChild(canvasBackground);
|
2012-09-08 17:08:00 +04:00
|
|
|
|
2012-09-11 01:26:12 +04:00
|
|
|
previewTileRoot.addEventListener('click', this.onPreviewClick_.bind(this, tileNumber));
|
2012-09-05 02:09:42 +04:00
|
|
|
|
|
|
|
var canvasPreviewDuplicateAction = document.createElement("button");
|
|
|
|
canvasPreviewDuplicateAction.className = "tile-action"
|
|
|
|
canvasPreviewDuplicateAction.innerHTML = "dup"
|
|
|
|
|
2012-09-11 01:26:12 +04:00
|
|
|
canvasPreviewDuplicateAction.addEventListener('click', this.onAddButtonClick_.bind(this, tileNumber));
|
2012-09-05 02:09:42 +04:00
|
|
|
|
2012-09-08 17:08:00 +04:00
|
|
|
// TODO(vincz): Eventually optimize this part by not recreating a FrameRenderer. Note that the real optim
|
|
|
|
// is to make this update function (#createPreviewTile) less aggressive.
|
2012-09-08 17:34:13 +04:00
|
|
|
var renderingOptions = {"dpi": this.dpi };
|
2012-09-08 20:44:06 +04:00
|
|
|
var currentFrameRenderer = new pskl.rendering.FrameRenderer($(canvasContainer), renderingOptions, "tile-view");
|
2012-09-08 17:08:00 +04:00
|
|
|
currentFrameRenderer.init(currentFrame);
|
|
|
|
|
2012-09-05 02:09:42 +04:00
|
|
|
previewTileRoot.appendChild(canvasContainer);
|
|
|
|
previewTileRoot.appendChild(canvasPreviewDuplicateAction);
|
|
|
|
|
|
|
|
if(tileNumber > 0 || this.framesheet.getFrameCount() > 1) {
|
|
|
|
var canvasPreviewDeleteAction = document.createElement("button");
|
|
|
|
canvasPreviewDeleteAction.className = "tile-action"
|
|
|
|
canvasPreviewDeleteAction.innerHTML = "del"
|
2012-09-11 01:26:12 +04:00
|
|
|
canvasPreviewDeleteAction.addEventListener('click', this.onDeleteButtonClick_.bind(this, tileNumber));
|
2012-09-05 02:09:42 +04:00
|
|
|
previewTileRoot.appendChild(canvasPreviewDeleteAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
return previewTileRoot;
|
|
|
|
};
|
2012-09-11 01:26:12 +04:00
|
|
|
|
|
|
|
ns.PreviewFilmController.prototype.onPreviewClick_ = function (index, evt) {
|
|
|
|
// has not class tile-action:
|
|
|
|
if(!evt.target.classList.contains('tile-action')) {
|
|
|
|
this.framesheet.setCurrentFrameIndex(index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.PreviewFilmController.prototype.onDeleteButtonClick_ = function (index, evt) {
|
|
|
|
this.framesheet.removeFrameByIndex(index);
|
|
|
|
$.publish(Events.FRAMESHEET_RESET);
|
|
|
|
$.publish(Events.LOCALSTORAGE_REQUEST); // Should come from model
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.PreviewFilmController.prototype.onAddButtonClick_ = function (index, evt) {
|
|
|
|
this.framesheet.duplicateFrameByIndex(index);
|
|
|
|
$.publish(Events.LOCALSTORAGE_REQUEST); // Should come from model
|
|
|
|
this.framesheet.setCurrentFrameIndex(index + 1);
|
|
|
|
};
|
2012-09-05 02:09:42 +04:00
|
|
|
})();
|