aboutsummaryrefslogtreecommitdiff
path: root/js/tools/LineTool.js
diff options
context:
space:
mode:
authorValerio Virgillito2012-06-06 17:04:34 -0700
committerValerio Virgillito2012-06-06 17:04:34 -0700
commit6442dcc0cc4088d28558a9c121c6a91cba3ff078 (patch)
tree8f2f2db6097104e44c8d87e11466d1f3124c7f3a /js/tools/LineTool.js
parent488ae8d98deb30009cb8b5b2da464fe52940050b (diff)
parent47e0e1f047e83b71c3f86812a45d33904201cc7a (diff)
downloadninja-6442dcc0cc4088d28558a9c121c6a91cba3ff078.tar.gz
Merge branch 'refs/heads/master' into element-model-fixes
Conflicts: js/tools/LineTool.js js/tools/ShapeTool.js Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'js/tools/LineTool.js')
-rwxr-xr-xjs/tools/LineTool.js108
1 files changed, 60 insertions, 48 deletions
diff --git a/js/tools/LineTool.js b/js/tools/LineTool.js
index 56d03e81..40475908 100755
--- a/js/tools/LineTool.js
+++ b/js/tools/LineTool.js
@@ -54,38 +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
82 var styles = document.application.njUtils.stylesFromDraw(canvas, w, h, drawData);
83 this.application.ninja.elementMediator.addElements(canvas, styles);
84 } else {
85 canvas = this._targetedElement;
86 canvas.elementModel.controller = ShapesController;
87 if(!canvas.elementModel.shapeModel) {
88 canvas.elementModel.shapeModel = Montage.create(ShapeModel);
89 } 96 }
90 } 97 }
91 } 98 }
@@ -102,16 +109,18 @@ exports.LineTool = Montage.create(ShapeTool, {
102 109
103 onAddElements: { 110 onAddElements: {
104 value: function(el) { 111 value: function(el) {
105 var drawData, xAdj = 0, yAdj = 0, w, h, slope = this._getSlope(); 112 var xAdj = 0, yAdj = 0, w, h, slope = this._getSlope();
106 113
107 if(drawData = this.getDrawingData()) { 114 if(this.drawData) {
108 // set the dimensions 115 // set the dimensions
109 w = ~~drawData.width; 116 w = Math.floor(this.drawData.width);
110 h = ~~drawData.height; 117 h = Math.floor(this.drawData.height);
111 if(slope === "horizontal") { 118 if(slope === "horizontal") {
112 h = Math.max(this._strokeSize, 1); 119 h = Math.max(this._strokeSize, 1);
120 w = Math.max(w, 1);
113 } else if(slope === "vertical") { 121 } else if(slope === "vertical") {
114 w = Math.max(this._strokeSize, 1); 122 w = Math.max(this._strokeSize, 1);
123 h = Math.max(h, 1);
115 } else { 124 } else {
116 // 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
117 var theta = Math.atan(slope); 126 var theta = Math.atan(slope);
@@ -122,7 +131,7 @@ exports.LineTool = Montage.create(ShapeTool, {
122 h += ~~(yAdj*2); 131 h += ~~(yAdj*2);
123 } 132 }
124 133
125 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);
126 } 135 }
127 } 136 }
128 }, 137 },
@@ -131,28 +140,31 @@ exports.LineTool = Montage.create(ShapeTool, {
131 value: function() { 140 value: function() {
132 var hitRec0 = this._mouseDownHitRec, 141 var hitRec0 = this._mouseDownHitRec,
133 hitRec1 = this._mouseUpHitRec, 142 hitRec1 = this._mouseUpHitRec,
134 slope; 143 slope,
144 dx,
145 dy;
135 146
136 if (hitRec0 && hitRec1) 147 if (hitRec0 && hitRec1) {
137 {
138 var p0 = hitRec0.getLocalPoint(), 148 var p0 = hitRec0.getLocalPoint(),
139 p1 = hitRec1.getLocalPoint(); 149 p1 = hitRec1.getLocalPoint();
140 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
141 // check for divide by 0 for vertical line: 158 // check for divide by 0 for vertical line:
142 if( Math.round(p0[0] - p1[0]) === 0 ) 159 if(dx === 0) {
143 {
144 // vertical line 160 // vertical line
145 slope = "vertical"; 161 slope = "vertical";
146 } 162 } else if (dy === 0) {
147 else if (Math.round(p0[1] - p1[1]) === 0 )
148 {
149 // horizontal line 163 // horizontal line
150 slope = "horizontal"; 164 slope = "horizontal";
151 } 165 } else {
152 else
153 {
154 // 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
155 slope = (p0[1] - p1[1])/(p0[0] - p1[0]); 167 slope = dy/dx;
156 } 168 }
157 } 169 }
158 170