From e4e3ee77aa01f9b6e7592306ebfa9f123a60c624 Mon Sep 17 00:00:00 2001 From: Nivesh Rajbhandari Date: Wed, 6 Jun 2012 13:59:37 -0700 Subject: Do not create any tags or shapes with width or height of 0. Signed-off-by: Nivesh Rajbhandari --- js/tools/FillTool.js | 1 + js/tools/InkBottleTool.js | 1 + js/tools/LineTool.js | 110 +++++++++++++++++++++++------------------ js/tools/SelectionTool.js | 2 + js/tools/ShapeTool.js | 46 +++++++++-------- js/tools/TagTool.js | 12 +++-- js/tools/drawing-tool.js | 3 ++ js/tools/modifier-tool-base.js | 2 + 8 files changed, 103 insertions(+), 74 deletions(-) diff --git a/js/tools/FillTool.js b/js/tools/FillTool.js index 69807bc3..e08ec1da 100755 --- a/js/tools/FillTool.js +++ b/js/tools/FillTool.js @@ -70,6 +70,7 @@ exports.FillTool = Montage.create(ModifierToolBase, { // Called by modifier tool base's HandleLeftButtonDown after updating selection (if needed) startDraw: { value: function(event) { + this.drawData = null; this.isDrawing = true; if(this._canColor && this.application.ninja.selectedElements.length) { diff --git a/js/tools/InkBottleTool.js b/js/tools/InkBottleTool.js index dff0b0fa..c5640b10 100755 --- a/js/tools/InkBottleTool.js +++ b/js/tools/InkBottleTool.js @@ -65,6 +65,7 @@ exports.InkBottleTool = Montage.create(ModifierToolBase, { // Called by modifier tool base's HandleLeftButtonDown after updating selection (if needed) startDraw: { value: function(event) { + this.drawData = null; this.isDrawing = true; if(this._canColor && this.application.ninja.selectedElements.length) diff --git a/js/tools/LineTool.js b/js/tools/LineTool.js index b2b48383..327d0054 100755 --- a/js/tools/LineTool.js +++ b/js/tools/LineTool.js @@ -54,39 +54,46 @@ exports.LineTool = Montage.create(ShapeTool, { HandleLeftButtonUp: { value: function (event) { - var slope = this._getSlope(), drawData = this.getDrawingData(); - - if(drawData) { - var canvas, xAdj = 0, yAdj = 0, w, h; - if(!this._useExistingCanvas()) { - if(drawData = this.getDrawingData()) { - // set the dimensions - w = ~~drawData.width; - h = ~~drawData.height; - if(slope === "horizontal") { - h = Math.max(this._strokeSize, 1); - } else if(slope === "vertical") { - w = Math.max(this._strokeSize, 1); + var slope = this._getSlope(), + canvas, + xAdj = 0, + yAdj = 0, + w, + h; + + if(slope) { + this.drawData = this.getDrawingData(); + if(this.drawData) { + w = Math.floor(this.drawData.width); + h = Math.floor(this.drawData.height); + if(!this._useExistingCanvas()) { + // set the dimensions + if(slope === "horizontal") { + h = Math.max(this._strokeSize, 1); + w = Math.max(w, 1); + } else if(slope === "vertical") { + w = Math.max(this._strokeSize, 1); + h = Math.max(h, 1); + } else { + // else make the line's stroke fit inside the canvas by growing the canvas + var theta = Math.atan(slope); + xAdj = Math.abs((this._strokeSize/2)*Math.sin(theta)); + yAdj = Math.abs((this._strokeSize/2)*Math.cos(theta)); + + w += ~~(xAdj*2); + h += ~~(yAdj*2); + } + + canvas = document.application.njUtils.make("canvas", {"data-RDGE-id": NJUtils.generateRandom()}, this.application.ninja.currentDocument); + document.application.njUtils.createModelWithShape(canvas, "Line"); + + var styles = document.application.njUtils.stylesFromDraw(canvas, w, h, this.drawData); + this.application.ninja.elementMediator.addElements(canvas, styles); } else { - // else make the line's stroke fit inside the canvas by growing the canvas - var theta = Math.atan(slope); - xAdj = Math.abs((this._strokeSize/2)*Math.sin(theta)); - yAdj = Math.abs((this._strokeSize/2)*Math.cos(theta)); - - w += ~~(xAdj*2); - h += ~~(yAdj*2); - } - - canvas = document.application.njUtils.make("canvas", {"data-RDGE-id": NJUtils.generateRandom()}, this.application.ninja.currentDocument); - document.application.njUtils.createModelWithShape(canvas, "Line"); - - var styles = document.application.njUtils.stylesFromDraw(canvas, w, h, drawData); - this.application.ninja.elementMediator.addElements(canvas, styles); - } else { - canvas = this._targetedElement; - canvas.elementModel.controller = ShapesController; - if(!canvas.elementModel.shapeModel) { - canvas.elementModel.shapeModel = Montage.create(ShapeModel); + canvas = this._targetedElement; + canvas.elementModel.controller = ShapesController; + if(!canvas.elementModel.shapeModel) { + canvas.elementModel.shapeModel = Montage.create(ShapeModel); } } } @@ -103,16 +110,18 @@ exports.LineTool = Montage.create(ShapeTool, { onAddElements: { value: function(el) { - var drawData, xAdj = 0, yAdj = 0, w, h, slope = this._getSlope(); + var xAdj = 0, yAdj = 0, w, h, slope = this._getSlope(); - if(drawData = this.getDrawingData()) { + if(this.drawData) { // set the dimensions - w = ~~drawData.width; - h = ~~drawData.height; + w = Math.floor(this.drawData.width); + h = Math.floor(this.drawData.height); if(slope === "horizontal") { h = Math.max(this._strokeSize, 1); + w = Math.max(w, 1); } else if(slope === "vertical") { w = Math.max(this._strokeSize, 1); + h = Math.max(h, 1); } else { // else make the line's stroke fit inside the canvas by growing the canvas var theta = Math.atan(slope); @@ -123,7 +132,7 @@ exports.LineTool = Montage.create(ShapeTool, { h += ~~(yAdj*2); } - this.RenderShape(w, h, drawData.planeMat, drawData.midPt, el, slope, xAdj, yAdj); + this.RenderShape(w, h, this.drawData.planeMat, this.drawData.midPt, el, slope, xAdj, yAdj); } } }, @@ -132,28 +141,31 @@ exports.LineTool = Montage.create(ShapeTool, { value: function() { var hitRec0 = this._mouseDownHitRec, hitRec1 = this._mouseUpHitRec, - slope; + slope, + dx, + dy; - if (hitRec0 && hitRec1) - { + if (hitRec0 && hitRec1) { var p0 = hitRec0.getLocalPoint(), p1 = hitRec1.getLocalPoint(); + dx = Math.floor(p0[0] - p1[0]); + dy = Math.floor(p0[1] - p1[1]); + + if( (dx === 0) && (dy === 0) ) { + return null; + } + // check for divide by 0 for vertical line: - if( Math.round(p0[0] - p1[0]) === 0 ) - { + if(dx === 0) { // vertical line slope = "vertical"; - } - else if (Math.round(p0[1] - p1[1]) === 0 ) - { + } else if (dy === 0) { // horizontal line slope = "horizontal"; - } - else - { + } else { // if slope is positive, draw a line from top-left to bottom-right - slope = (p0[1] - p1[1])/(p0[0] - p1[0]); + slope = dy/dx; } } diff --git a/js/tools/SelectionTool.js b/js/tools/SelectionTool.js index 7b72a857..ed92b893 100755 --- a/js/tools/SelectionTool.js +++ b/js/tools/SelectionTool.js @@ -49,6 +49,8 @@ var SelectionTool = exports.SelectionTool = Montage.create(ModifierToolBase, { startDraw: { value: function(event) { + this.drawData = null; + if(!this.application.ninja.selectedElements.length) { this._isSelecting = true; diff --git a/js/tools/ShapeTool.js b/js/tools/ShapeTool.js index 367ab78d..8886285d 100755 --- a/js/tools/ShapeTool.js +++ b/js/tools/ShapeTool.js @@ -55,24 +55,30 @@ exports.ShapeTool = Montage.create(DrawingTool, { HandleLeftButtonUp: { value: function (event) { - var canvas, drawData = this.getDrawingData(); - - if(drawData) { - if(!this._useExistingCanvas()) { - canvas = document.application.njUtils.make("canvas", {"data-RDGE-id": NJUtils.generateRandom()}, this.application.ninja.currentDocument); - document.application.njUtils.createModelWithShape(canvas); - - var styles = document.application.njUtils.stylesFromDraw(canvas, ~~drawData.width, ~~drawData.height, drawData); - this.application.ninja.elementMediator.addElements(canvas, styles); - } else { - canvas = this._targetedElement; - if (!canvas.getAttribute( "data-RDGE-id" )) - canvas.setAttribute( "data-RDGE-id", NJUtils.generateRandom() ); - canvas.elementModel.controller = ShapesController; - if(!canvas.elementModel.shapeModel) { - canvas.elementModel.shapeModel = Montage.create(ShapeModel); + var canvas, w, h; + this.drawData = this.getDrawingData(); + + if(this.drawData) { + w = Math.floor(this.drawData.width); + h = Math.floor(this.drawData.height); + + if( (w > 0) && (h > 0) ) { + if(!this._useExistingCanvas()) { + canvas = document.application.njUtils.make("canvas", {"data-RDGE-id": NJUtils.generateRandom()}, this.application.ninja.currentDocument); + document.application.njUtils.createModelWithShape(canvas); + + var styles = document.application.njUtils.stylesFromDraw(canvas, w, h, this.drawData); + this.application.ninja.elementMediator.addElements(canvas, styles); + } else { + canvas = this._targetedElement; + if (!canvas.getAttribute( "data-RDGE-id" )) + canvas.setAttribute( "data-RDGE-id", NJUtils.generateRandom() ); + canvas.elementModel.controller = ShapesController; + if(!canvas.elementModel.shapeModel) { + canvas.elementModel.shapeModel = Montage.create(ShapeModel); + } + this.RenderShape(w, h, this.drawData.planeMat, this.drawData.midPt, canvas); } - this.RenderShape(drawData.width, drawData.height, drawData.planeMat, drawData.midPt, canvas); } } @@ -87,10 +93,8 @@ exports.ShapeTool = Montage.create(DrawingTool, { onAddElements: { value: function(el) { - var drawData; - - if(drawData = this.getDrawingData()) { - this.RenderShape(drawData.width, drawData.height, drawData.planeMat, drawData.midPt, el); + if(this.drawData) { + this.RenderShape(this.drawData.width, this.drawData.height, this.drawData.planeMat, this.drawData.midPt, el); } } }, diff --git a/js/tools/TagTool.js b/js/tools/TagTool.js index 5520fa89..4ce27e36 100755 --- a/js/tools/TagTool.js +++ b/js/tools/TagTool.js @@ -51,16 +51,20 @@ exports.TagTool = Montage.create(DrawingTool, { HandleLeftButtonUp: { value: function(event) { + var w, h; if(this._escape) { this._escape = false; return; } if(this._hasDraw) { - var drawData = this.getDrawingData(); - - if(drawData) { - this.insertElement(drawData); + this.drawData = this.getDrawingData(); + if(this.drawData) { + w = Math.floor(this.drawData.width); + h = Math.floor(this.drawData.height); + if( (w > 0) && (h > 0) ) { + this.insertElement(this.drawData); + } } this._hasDraw = false; diff --git a/js/tools/drawing-tool.js b/js/tools/drawing-tool.js index d906fdf9..9ba4501c 100755 --- a/js/tools/drawing-tool.js +++ b/js/tools/drawing-tool.js @@ -64,6 +64,8 @@ exports.DrawingTool = Montage.create(ToolBase, { set: function (value) { this._currentY = value; } }, + drawData: { value: null }, + /** * PUBLIC METHODS */ @@ -74,6 +76,7 @@ exports.DrawingTool = Montage.create(ToolBase, { this._isDrawing = true; this.mouseDownHitRec = null; this.mouseUpHitRec = null; + this.drawData = null; point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(event.pageX, event.pageY)); snapData = DrawingToolBase.getInitialSnapPoint(point.x, point.y, this._targetedElement); //event.layerX, event.layerY); diff --git a/js/tools/modifier-tool-base.js b/js/tools/modifier-tool-base.js index 6aea77aa..6c569f0a 100755 --- a/js/tools/modifier-tool-base.js +++ b/js/tools/modifier-tool-base.js @@ -451,6 +451,8 @@ exports.ModifierToolBase = Montage.create(DrawingTool, { startDraw: { value: function(event) { + this.drawData = null; + if(this._target) { this.isDrawing = true; -- cgit v1.2.3