diff options
Diffstat (limited to 'js/tools/LineTool.js')
-rwxr-xr-x | js/tools/LineTool.js | 109 |
1 files changed, 60 insertions, 49 deletions
diff --git a/js/tools/LineTool.js b/js/tools/LineTool.js index 3f452a06..07429bc0 100755 --- a/js/tools/LineTool.js +++ b/js/tools/LineTool.js | |||
@@ -54,39 +54,45 @@ exports.LineTool = Montage.create(ShapeTool, { | |||
54 | 54 | ||
55 | HandleLeftButtonUp: { | 55 | HandleLeftButtonUp: { |
56 | value: function (event) { | 56 | value: function (event) { |
57 | var slope = this._getSlope(), drawData = this.getDrawingData(); | 57 | var slope = this._getSlope(), |
58 | 58 | canvas, | |
59 | if(drawData) { | 59 | xAdj = 0, |
60 | var canvas, xAdj = 0, yAdj = 0, w, h; | 60 | yAdj = 0, |
61 | if(!this._useExistingCanvas()) { | 61 | w, |
62 | if(drawData = this.getDrawingData()) { | 62 | h; |
63 | // set the dimensions | 63 | |
64 | w = ~~drawData.width; | 64 | if(slope) { |
65 | h = ~~drawData.height; | 65 | this.drawData = this.getDrawingData(); |
66 | if(slope === "horizontal") { | 66 | if(this.drawData) { |
67 | h = Math.max(this._strokeSize, 1); | 67 | w = Math.floor(this.drawData.width); |
68 | } else if(slope === "vertical") { | 68 | h = Math.floor(this.drawData.height); |
69 | w = Math.max(this._strokeSize, 1); | 69 | if(!this._useExistingCanvas()) { |
70 | // set the dimensions | ||
71 | if(slope === "horizontal") { | ||
72 | h = Math.max(this._strokeSize, 1); | ||
73 | w = Math.max(w, 1); | ||
74 | } else if(slope === "vertical") { | ||
75 | w = Math.max(this._strokeSize, 1); | ||
76 | h = Math.max(h, 1); | ||
77 | } else { | ||
78 | // else make the line's stroke fit inside the canvas by growing the canvas | ||
79 | var theta = Math.atan(slope); | ||
80 | xAdj = Math.abs((this._strokeSize/2)*Math.sin(theta)); | ||
81 | yAdj = Math.abs((this._strokeSize/2)*Math.cos(theta)); | ||
82 | |||
83 | w += ~~(xAdj*2); | ||
84 | h += ~~(yAdj*2); | ||
85 | } | ||
86 | |||
87 | canvas = document.application.njUtils.make("canvas", {"data-RDGE-id": NJUtils.generateRandom()}, this.application.ninja.currentDocument); | ||
88 | |||
89 | var styles = document.application.njUtils.stylesFromDraw(canvas, w, h, this.drawData); | ||
90 | this.application.ninja.elementMediator.addElements(canvas, styles); | ||
70 | } else { | 91 | } else { |
71 | // else make the line's stroke fit inside the canvas by growing the canvas | 92 | canvas = this._targetedElement; |
72 | var theta = Math.atan(slope); | 93 | canvas.elementModel.controller = ShapesController; |
73 | xAdj = Math.abs((this._strokeSize/2)*Math.sin(theta)); | 94 | if(!canvas.elementModel.shapeModel) { |
74 | yAdj = Math.abs((this._strokeSize/2)*Math.cos(theta)); | 95 | canvas.elementModel.shapeModel = Montage.create(ShapeModel); |
75 | |||
76 | w += ~~(xAdj*2); | ||
77 | h += ~~(yAdj*2); | ||
78 | } | ||
79 | |||
80 | canvas = document.application.njUtils.make("canvas", {"data-RDGE-id": NJUtils.generateRandom()}, this.application.ninja.currentDocument); | ||
81 | document.application.njUtils.createModelWithShape(canvas, "Line"); | ||
82 | |||
83 | var styles = document.application.njUtils.stylesFromDraw(canvas, w, h, drawData); | ||
84 | this.application.ninja.elementMediator.addElements(canvas, styles); | ||
85 | } else { | ||
86 | canvas = this._targetedElement; | ||
87 | canvas.elementModel.controller = ShapesController; | ||
88 | if(!canvas.elementModel.shapeModel) { | ||
89 | canvas.elementModel.shapeModel = Montage.create(ShapeModel); | ||
90 | } | 96 | } |
91 | } | 97 | } |
92 | } | 98 | } |
@@ -103,16 +109,18 @@ exports.LineTool = Montage.create(ShapeTool, { | |||
103 | 109 | ||
104 | onAddElements: { | 110 | onAddElements: { |
105 | value: function(el) { | 111 | value: function(el) { |
106 | var drawData, xAdj = 0, yAdj = 0, w, h, slope = this._getSlope(); | 112 | var xAdj = 0, yAdj = 0, w, h, slope = this._getSlope(); |
107 | 113 | ||
108 | if(drawData = this.getDrawingData()) { | 114 | if(this.drawData) { |
109 | // set the dimensions | 115 | // set the dimensions |
110 | w = ~~drawData.width; | 116 | w = Math.floor(this.drawData.width); |
111 | h = ~~drawData.height; | 117 | h = Math.floor(this.drawData.height); |
112 | if(slope === "horizontal") { | 118 | if(slope === "horizontal") { |
113 | h = Math.max(this._strokeSize, 1); | 119 | h = Math.max(this._strokeSize, 1); |
120 | w = Math.max(w, 1); | ||
114 | } else if(slope === "vertical") { | 121 | } else if(slope === "vertical") { |
115 | w = Math.max(this._strokeSize, 1); | 122 | w = Math.max(this._strokeSize, 1); |
123 | h = Math.max(h, 1); | ||
116 | } else { | 124 | } else { |
117 | // else make the line's stroke fit inside the canvas by growing the canvas | 125 | // else make the line's stroke fit inside the canvas by growing the canvas |
118 | var theta = Math.atan(slope); | 126 | var theta = Math.atan(slope); |
@@ -123,7 +131,7 @@ exports.LineTool = Montage.create(ShapeTool, { | |||
123 | h += ~~(yAdj*2); | 131 | h += ~~(yAdj*2); |
124 | } | 132 | } |
125 | 133 | ||
126 | this.RenderShape(w, h, drawData.planeMat, drawData.midPt, el, slope, xAdj, yAdj); | 134 | this.RenderShape(w, h, this.drawData.planeMat, this.drawData.midPt, el, slope, xAdj, yAdj); |
127 | } | 135 | } |
128 | } | 136 | } |
129 | }, | 137 | }, |
@@ -132,28 +140,31 @@ exports.LineTool = Montage.create(ShapeTool, { | |||
132 | value: function() { | 140 | value: function() { |
133 | var hitRec0 = this._mouseDownHitRec, | 141 | var hitRec0 = this._mouseDownHitRec, |
134 | hitRec1 = this._mouseUpHitRec, | 142 | hitRec1 = this._mouseUpHitRec, |
135 | slope; | 143 | slope, |
144 | dx, | ||
145 | dy; | ||
136 | 146 | ||
137 | if (hitRec0 && hitRec1) | 147 | if (hitRec0 && hitRec1) { |
138 | { | ||
139 | var p0 = hitRec0.getLocalPoint(), | 148 | var p0 = hitRec0.getLocalPoint(), |
140 | p1 = hitRec1.getLocalPoint(); | 149 | p1 = hitRec1.getLocalPoint(); |
141 | 150 | ||
151 | dx = Math.floor(p0[0] - p1[0]); | ||
152 | dy = Math.floor(p0[1] - p1[1]); | ||
153 | |||
154 | if( (dx === 0) && (dy === 0) ) { | ||
155 | return null; | ||
156 | } | ||
157 | |||
142 | // check for divide by 0 for vertical line: | 158 | // check for divide by 0 for vertical line: |
143 | if( Math.round(p0[0] - p1[0]) === 0 ) | 159 | if(dx === 0) { |
144 | { | ||
145 | // vertical line | 160 | // vertical line |
146 | slope = "vertical"; | 161 | slope = "vertical"; |
147 | } | 162 | } else if (dy === 0) { |
148 | else if (Math.round(p0[1] - p1[1]) === 0 ) | ||
149 | { | ||
150 | // horizontal line | 163 | // horizontal line |
151 | slope = "horizontal"; | 164 | slope = "horizontal"; |
152 | } | 165 | } else { |
153 | else | ||
154 | { | ||
155 | // if slope is positive, draw a line from top-left to bottom-right | 166 | // if slope is positive, draw a line from top-left to bottom-right |
156 | slope = (p0[1] - p1[1])/(p0[0] - p1[0]); | 167 | slope = dy/dx; |
157 | } | 168 | } |
158 | } | 169 | } |
159 | 170 | ||