diff options
author | Pushkar Joshi | 2012-06-07 09:53:44 -0700 |
---|---|---|
committer | Pushkar Joshi | 2012-06-07 09:53:44 -0700 |
commit | 1a40c4a3e1817004d1ce1c600ae4857fdd94a97a (patch) | |
tree | 25836d69f77472f60bab9a14c23865a3899a60d8 /js/tools/LineTool.js | |
parent | 763baf19f032c6fbae18963402b79d5d5510d923 (diff) | |
parent | 47e0e1f047e83b71c3f86812a45d33904201cc7a (diff) | |
download | ninja-1a40c4a3e1817004d1ce1c600ae4857fdd94a97a.tar.gz |
Merge branch 'master' into brushtool
Diffstat (limited to 'js/tools/LineTool.js')
-rwxr-xr-x | js/tools/LineTool.js | 110 |
1 files changed, 61 insertions, 49 deletions
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, { | |||
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 | document.application.njUtils.createModelWithShape(canvas, "Line"); | ||
89 | |||
90 | var styles = document.application.njUtils.stylesFromDraw(canvas, w, h, this.drawData); | ||
91 | this.application.ninja.elementMediator.addElements(canvas, styles); | ||
70 | } else { | 92 | } else { |
71 | // else make the line's stroke fit inside the canvas by growing the canvas | 93 | canvas = this._targetedElement; |
72 | var theta = Math.atan(slope); | 94 | canvas.elementModel.controller = ShapesController; |
73 | xAdj = Math.abs((this._strokeSize/2)*Math.sin(theta)); | 95 | if(!canvas.elementModel.shapeModel) { |
74 | yAdj = Math.abs((this._strokeSize/2)*Math.cos(theta)); | 96 | 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 | } | 97 | } |
91 | } | 98 | } |
92 | } | 99 | } |
@@ -103,16 +110,18 @@ exports.LineTool = Montage.create(ShapeTool, { | |||
103 | 110 | ||
104 | onAddElements: { | 111 | onAddElements: { |
105 | value: function(el) { | 112 | value: function(el) { |
106 | var drawData, xAdj = 0, yAdj = 0, w, h, slope = this._getSlope(); | 113 | var xAdj = 0, yAdj = 0, w, h, slope = this._getSlope(); |
107 | 114 | ||
108 | if(drawData = this.getDrawingData()) { | 115 | if(this.drawData) { |
109 | // set the dimensions | 116 | // set the dimensions |
110 | w = ~~drawData.width; | 117 | w = Math.floor(this.drawData.width); |
111 | h = ~~drawData.height; | 118 | h = Math.floor(this.drawData.height); |
112 | if(slope === "horizontal") { | 119 | if(slope === "horizontal") { |
113 | h = Math.max(this._strokeSize, 1); | 120 | h = Math.max(this._strokeSize, 1); |
121 | w = Math.max(w, 1); | ||
114 | } else if(slope === "vertical") { | 122 | } else if(slope === "vertical") { |
115 | w = Math.max(this._strokeSize, 1); | 123 | w = Math.max(this._strokeSize, 1); |
124 | h = Math.max(h, 1); | ||
116 | } else { | 125 | } else { |
117 | // else make the line's stroke fit inside the canvas by growing the canvas | 126 | // else make the line's stroke fit inside the canvas by growing the canvas |
118 | var theta = Math.atan(slope); | 127 | var theta = Math.atan(slope); |
@@ -123,7 +132,7 @@ exports.LineTool = Montage.create(ShapeTool, { | |||
123 | h += ~~(yAdj*2); | 132 | h += ~~(yAdj*2); |
124 | } | 133 | } |
125 | 134 | ||
126 | this.RenderShape(w, h, drawData.planeMat, drawData.midPt, el, slope, xAdj, yAdj); | 135 | this.RenderShape(w, h, this.drawData.planeMat, this.drawData.midPt, el, slope, xAdj, yAdj); |
127 | } | 136 | } |
128 | } | 137 | } |
129 | }, | 138 | }, |
@@ -132,28 +141,31 @@ exports.LineTool = Montage.create(ShapeTool, { | |||
132 | value: function() { | 141 | value: function() { |
133 | var hitRec0 = this._mouseDownHitRec, | 142 | var hitRec0 = this._mouseDownHitRec, |
134 | hitRec1 = this._mouseUpHitRec, | 143 | hitRec1 = this._mouseUpHitRec, |
135 | slope; | 144 | slope, |
145 | dx, | ||
146 | dy; | ||
136 | 147 | ||
137 | if (hitRec0 && hitRec1) | 148 | if (hitRec0 && hitRec1) { |
138 | { | ||
139 | var p0 = hitRec0.getLocalPoint(), | 149 | var p0 = hitRec0.getLocalPoint(), |
140 | p1 = hitRec1.getLocalPoint(); | 150 | p1 = hitRec1.getLocalPoint(); |
141 | 151 | ||
152 | dx = Math.floor(p0[0] - p1[0]); | ||
153 | dy = Math.floor(p0[1] - p1[1]); | ||
154 | |||
155 | if( (dx === 0) && (dy === 0) ) { | ||
156 | return null; | ||
157 | } | ||
158 | |||
142 | // check for divide by 0 for vertical line: | 159 | // check for divide by 0 for vertical line: |
143 | if( Math.round(p0[0] - p1[0]) === 0 ) | 160 | if(dx === 0) { |
144 | { | ||
145 | // vertical line | 161 | // vertical line |
146 | slope = "vertical"; | 162 | slope = "vertical"; |
147 | } | 163 | } else if (dy === 0) { |
148 | else if (Math.round(p0[1] - p1[1]) === 0 ) | ||
149 | { | ||
150 | // horizontal line | 164 | // horizontal line |
151 | slope = "horizontal"; | 165 | slope = "horizontal"; |
152 | } | 166 | } else { |
153 | else | ||
154 | { | ||
155 | // if slope is positive, draw a line from top-left to bottom-right | 167 | // if slope is positive, draw a line from top-left to bottom-right |
156 | slope = (p0[1] - p1[1])/(p0[0] - p1[0]); | 168 | slope = dy/dx; |
157 | } | 169 | } |
158 | } | 170 | } |
159 | 171 | ||