From e28eb9158a50d7e6d97dbc68066e591ac600c241 Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Tue, 5 Jun 2012 21:40:44 -0700 Subject: removing all model creators. The elementModel is now a getter that will create a new model when needed. Signed-off-by: Valerio Virgillito --- js/tools/LineTool.js | 1 - 1 file changed, 1 deletion(-) (limited to 'js/tools/LineTool.js') diff --git a/js/tools/LineTool.js b/js/tools/LineTool.js index b2b48383..56d03e81 100755 --- a/js/tools/LineTool.js +++ b/js/tools/LineTool.js @@ -78,7 +78,6 @@ exports.LineTool = Montage.create(ShapeTool, { } 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); -- cgit v1.2.3 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/LineTool.js | 110 ++++++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 49 deletions(-) (limited to 'js/tools/LineTool.js') 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; } } -- cgit v1.2.3