diff --git a/Gruntfile.js b/Gruntfile.js index 5efebdc..0806c48 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -20,7 +20,12 @@ module.exports = function(grunt) { "eqnull": true },*/ options: { - indent:2 + indent:2, + undef : true, + latedef : true, + browser : true, + jquery : true, + globals : {'pskl':true, 'Events':true, 'Constants':true, 'console' : true, 'module':true} }, files: [ 'Gruntfile.js', diff --git a/js/Constants.js b/js/Constants.js index 0279d71..5b333e5 100644 --- a/js/Constants.js +++ b/js/Constants.js @@ -8,6 +8,8 @@ var Constants = { MAX_HEIGHT : 128, MAX_WIDTH : 128, + PREVIEW_FILM_SIZE : 120, + DEFAULT_PEN_COLOR : '#000000', TRANSPARENT_COLOR : 'TRANSPARENT', diff --git a/js/controller/DrawingController.js b/js/controller/DrawingController.js index 785afd3..fdedeb2 100644 --- a/js/controller/DrawingController.js +++ b/js/controller/DrawingController.js @@ -25,8 +25,6 @@ this.renderer = new pskl.rendering.FrameRenderer(this.container, renderingOptions, "drawing-canvas"); this.overlayRenderer = new pskl.rendering.FrameRenderer(this.container, renderingOptions, "canvas-overlay"); - this.renderer.init(framesheet.getCurrentFrame()); - this.overlayRenderer.init(this.overlayFrame); // State of drawing controller: this.isClicked = false; @@ -35,12 +33,18 @@ this.currentToolBehavior = null; this.primaryColor = Constants.DEFAULT_PEN_COLOR; this.secondaryColor = Constants.TRANSPARENT_COLOR; + }; + ns.DrawingController.prototype.init = function () { + this.renderer.render(this.framesheet.getCurrentFrame()); + this.overlayRenderer.render(this.overlayFrame); + this.initMouseBehavior(); $.subscribe(Events.TOOL_SELECTED, $.proxy(function(evt, toolBehavior) { console.log("Tool selected: ", toolBehavior); this.currentToolBehavior = toolBehavior; + this.overlayFrame.clear(); }, this)); /** diff --git a/js/controller/PreviewFilmController.js b/js/controller/PreviewFilmController.js index c338d98..b4e6f0f 100644 --- a/js/controller/PreviewFilmController.js +++ b/js/controller/PreviewFilmController.js @@ -7,7 +7,9 @@ this.dpi = this.calculateDPI_(); this.redrawFlag = true; + }; + ns.PreviewFilmController.prototype.init = function() { $.subscribe(Events.TOOL_RELEASED, this.flagForRedraw_.bind(this)); $.subscribe(Events.FRAMESHEET_RESET, this.flagForRedraw_.bind(this)); $.subscribe(Events.FRAMESHEET_RESET, this.refreshDPI_.bind(this)); @@ -16,8 +18,6 @@ this.updateScrollerOverflows(); }; - ns.PreviewFilmController.prototype.init = function() {}; - ns.PreviewFilmController.prototype.addFrame = function () { this.framesheet.addEmptyFrame(); this.framesheet.setCurrentFrameIndex(this.framesheet.getFrameCount() - 1); @@ -155,7 +155,7 @@ // is to make this update function (#createPreviewTile) less aggressive. var renderingOptions = {"dpi": this.dpi }; var currentFrameRenderer = new pskl.rendering.FrameRenderer($(canvasContainer), renderingOptions, "tile-view"); - currentFrameRenderer.init(currentFrame); + currentFrameRenderer.render(currentFrame); previewTileRoot.appendChild(canvasContainer); @@ -207,11 +207,14 @@ * Calculate the preview DPI depending on the framesheet size */ ns.PreviewFilmController.prototype.calculateDPI_ = function () { - var previewSize = 120, - framePixelHeight = this.framesheet.getCurrentFrame().getHeight(), - framePixelWidth = this.framesheet.getCurrentFrame().getWidth(); - // TODO (julz) : should have a utility to get a Size from framesheet easily (what about empty framesheets though ?) + var curFrame = this.framesheet.getCurrentFrame(), + frameHeight = curFrame.getHeight(), + frameWidth = curFrame.getWidth(), + maxFrameDim = Math.max(frameWidth, frameHeight); - return pskl.PixelUtils.calculateDPI(previewSize, previewSize, framePixelHeight, framePixelWidth); + var previewHeight = Constants.PREVIEW_FILM_SIZE * frameHeight / maxFrameDim; + var previewWidth = Constants.PREVIEW_FILM_SIZE * frameWidth / maxFrameDim; + + return pskl.PixelUtils.calculateDPI(previewHeight, previewWidth, frameHeight, frameWidth) || 1; }; })(); \ No newline at end of file diff --git a/js/controller/ToolController.js b/js/controller/ToolController.js index 5e0448b..4b38b2f 100644 --- a/js/controller/ToolController.js +++ b/js/controller/ToolController.js @@ -22,6 +22,19 @@ this.previousSelectedTool = this.toolInstances.simplePen; }; + /** + * @public + */ + ns.ToolController.prototype.init = function() { + this.createToolMarkup_(); + + // Initialize tool: + // Set SimplePen as default selected tool: + this.selectTool_(this.toolInstances.simplePen); + // Activate listener on tool panel: + $("#tool-section").click($.proxy(this.onToolIconClicked_, this)); + }; + /** * @private */ @@ -53,18 +66,27 @@ var clickedTool = target.closest(".tool-icon"); if(clickedTool.length) { - for(var tool in this.toolInstances) { - if (this.toolInstances[tool].toolId == clickedTool.data().toolId) { - this.selectTool_(this.toolInstances[tool]); + var toolId = clickedTool.data().toolId; + var tool = this.getToolById_(toolId); + if (tool) { + this.selectTool_(tool); - // Show tool as selected: - $('#tool-section .tool-icon.selected').removeClass('selected'); - clickedTool.addClass('selected'); - } + // Show tool as selected: + $('#tool-section .tool-icon.selected').removeClass('selected'); + clickedTool.addClass('selected'); } } }; + ns.ToolController.prototype.getToolById_ = function (toolId) { + for(var key in this.toolInstances) { + if (this.toolInstances[key].toolId == toolId) { + return this.toolInstances[key]; + } + } + return null; + }; + /** * @private */ @@ -82,18 +104,4 @@ } $('#tools-container').html(toolMarkup); }; - - /** - * @public - */ - ns.ToolController.prototype.init = function() { - - this.createToolMarkup_(); - - // Initialize tool: - // Set SimplePen as default selected tool: - this.selectTool_(this.toolInstances.simplePen); - // Activate listener on tool panel: - $("#tool-section").click($.proxy(this.onToolIconClicked_, this)); - }; })(); \ No newline at end of file diff --git a/js/drawingtools/selectiontools/BaseSelect.js b/js/drawingtools/selectiontools/BaseSelect.js index a38d419..334171d 100644 --- a/js/drawingtools/selectiontools/BaseSelect.js +++ b/js/drawingtools/selectiontools/BaseSelect.js @@ -63,7 +63,6 @@ */ ns.BaseSelect.prototype.releaseToolAt = function(col, row, color, frame, overlay) { if(this.mode == "select") { - this.onSelectEnd_(col, row, color, frame, overlay); } else if(this.mode == "moveSelection") { @@ -90,6 +89,17 @@ } }; + /** + * For each pixel in the selection draw it in white transparent on the tool overlay + * @protected + */ + ns.BaseSelect.prototype.drawSelectionOnOverlay_ = function (selection, overlay) { + var pixels = selection.pixels; + for(var i=0, l=pixels.length; i size=widthxheight @@ -126,7 +119,7 @@ for (i=0;i