aboutsummaryrefslogtreecommitdiff
path: root/js/tools/LineTool.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/tools/LineTool.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/tools/LineTool.js')
-rw-r--r--js/tools/LineTool.js251
1 files changed, 251 insertions, 0 deletions
diff --git a/js/tools/LineTool.js b/js/tools/LineTool.js
new file mode 100644
index 00000000..042ba0b2
--- /dev/null
+++ b/js/tools/LineTool.js
@@ -0,0 +1,251 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7var Montage = require("montage/core/core").Montage,
8 ShapeTool = require("js/tools/ShapeTool").ShapeTool,
9 DrawingToolBase = require("js/tools/drawing-tool-base").DrawingToolBase,
10 ElementMediator = require("js/mediators/element-mediator").ElementMediator,
11 NJUtils = require("js/lib/NJUtils").NJUtils,
12 TagTool = require("js/tools/TagTool").TagTool,
13 ShapesController = require("js/controllers/elements/shapes-controller").ShapesController,
14 ShapeModel = require("js/models/shape-model").ShapeModel;
15
16exports.LineTool = Montage.create(ShapeTool, {
17 _toolID: { value: "lineTool" },
18 _imageID: { value: "lineToolImg" },
19 _toolImageClass: { value: "lineToolUp" },
20 _selectedToolImageClass: { value: "lineToolDown" },
21 _toolTipText: { value: "Line Tool (L)" },
22
23 _tmpDrawIndex : { value : 1, writable:true},
24
25 _mode: {value: null, writable:true},
26
27 // Need to keep track of current mouse position for KEY modifiers event which do not have mouse coordinates
28 _currentX: {value: 0, writable: true},
29 _currentY: {value: 0, writable: true},
30 _lineView: {value: null, writable:true},
31 _ovalView: {value: null, writable:true},
32
33 _strokeSize: { value: 1 },
34 _strokeColor: { value: null },
35
36 HandleLeftButtonDown:
37 {
38 value: function (event)
39 {
40 if(this._canDraw) {
41 this._isDrawing = true;
42 }
43
44 this._strokeSize = ShapesController.GetValueInPixels(this.options.strokeSize.value, this.options.strokeSize.units, null);
45 this._strokeColor = this.application.ninja.colorController.colorToolbar.stroke.color.css;
46 this.startDraw(event);
47 }
48 },
49
50 HandleLeftButtonUp:
51 {
52 value: function (event)
53 {
54 var slope = this._getSlope(),
55 drawData = this.getDrawingData();
56
57 if(drawData) {
58 var canvas,
59 xAdj = 0,
60 yAdj = 0,
61 w = ~~drawData.width,
62 h = ~~drawData.height;
63 if(!this._useExistingCanvas())
64 {
65 // set the dimensions
66 if(slope === "horizontal")
67 {
68 h = Math.max(this._strokeSize, 1);
69 }
70 else if(slope === "vertical")
71 {
72 w = Math.max(this._strokeSize, 1);
73 }
74 else
75 {
76 // else make the line's stroke fit inside the canvas by growing the canvas
77 var theta = Math.atan(slope);
78 xAdj = Math.abs((this._strokeSize/2)*Math.sin(theta));
79 yAdj = Math.abs((this._strokeSize/2)*Math.cos(theta));
80
81 w += ~~(xAdj*2);
82 h += ~~(yAdj*2);
83 }
84
85 canvas = NJUtils.makeNJElement("canvas", "Canvas", "shape", null, true);
86 var elementModel = TagTool.makeElement(w, h, drawData.planeMat, drawData.midPt, canvas);
87
88 ElementMediator.addElement(canvas, elementModel.data, true);
89 canvas.elementModel.isShape = true;
90 }
91 else
92 {
93 canvas = this._targetedElement;
94 canvas.elementModel.controller = ShapesController;
95 if(!canvas.elementModel.shapeModel)
96 {
97 canvas.elementModel.shapeModel = Montage.create(ShapeModel);
98 }
99 }
100 this.RenderShape(w, h, drawData.planeMat, drawData.midPt,
101 canvas, slope, xAdj, yAdj);
102 }
103
104 this.endDraw(event);
105
106 this._isDrawing = false;
107 this._hasDraw=false;
108
109
110 this.DrawHandles();
111 }
112 },
113
114 _getSlope: {
115 value: function() {
116 var hitRec0 = this._mouseDownHitRec,
117 hitRec1 = this._mouseUpHitRec,
118 slope;
119
120 if (hitRec0 && hitRec1)
121 {
122 var p0 = hitRec0.getLocalPoint(),
123 p1 = hitRec1.getLocalPoint();
124
125 // check for divide by 0 for vertical line:
126 if( Math.round(p0[0] - p1[0]) === 0 )
127 {
128 // vertical line
129 slope = "vertical";
130 }
131 else if (Math.round(p0[1] - p1[1]) === 0 )
132 {
133 // horizontal line
134 slope = "horizontal";
135 }
136 else
137 {
138 // if slope is positive, draw a line from top-left to bottom-right
139 slope = (p0[1] - p1[1])/(p0[0] - p1[0]);
140 }
141 }
142
143 return slope;
144 }
145 },
146
147 _doDraw: {
148 value: function () {
149 if (this.mouseDownHitRec !== null) {
150 DrawingToolBase.stageComponent = this.application.ninja.stage;
151 DrawingToolBase.drawLine(this.mouseDownHitRec, this.mouseUpHitRec, this._strokeSize, this._strokeColor);
152 }
153 }
154 },
155
156 HandleShiftKeyDown: {
157 value: function (event) {
158 if (this._isDrawing) {
159 var slope = Math.abs((this.downPoint.y - this.currentY)/(this.downPoint.x - this.currentX));
160 // If slope is less than 0.5, make it a horizontal line
161 if(slope < 0.5)
162 {
163 this._mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(this.currentX, this.downPoint.y, false, this._mouseDownHitRec);
164 }
165 // If slope is greater than 2, make it a vertical line
166 else if(slope > 2)
167 {
168 this._mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(this.downPoint.x, this.currentY, false, this._mouseDownHitRec);
169 }
170 // make it a 45 degree line
171 else
172 {
173 var square = this.toSquare(this.downPoint.x, this.currentX, this.downPoint.y, this.currentY);
174 this._mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(square[0] + square[2], square[1] + square[3], false, this._mouseDownHitRec);
175 }
176 this._doDraw();
177 }
178 }
179 },
180
181 HandleShiftKeyUp: {
182 value: function () {
183 if (this._isDrawing) {
184 this.mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(this.currentX, this.currentY, false, this.mouseDownHitRec);
185 this._doDraw();
186 }
187 }
188 },
189
190 RenderShape: {
191 value: function (w, h, planeMat, midPt, canvas, slope, xAdj, yAdj)
192 {
193
194 var strokeStyleIndex = this.options.strokeStyleIndex;
195 var strokeStyle = this.options.strokeStyle;
196 var strokeSize = this._strokeSize;
197
198 var left = Math.round(midPt[0] - 0.5*w);
199 var top = Math.round(midPt[1] - 0.5*h);
200
201 var strokeColor = this.application.ninja.colorController.colorToolbar.stroke.webGlColor;
202 // for default stroke and fill/no materials
203 var strokeMaterial = null;
204
205 var strokeIndex = parseInt(this.options.strokeMaterial);
206 if(strokeIndex > 0)
207 {
208 strokeMaterial = Object.create(MaterialsLibrary.getMaterialAt(strokeIndex-1));
209 }
210
211 var world = this.getGLWorld(canvas, this.options.use3D);
212
213 var xOffset = ((left - canvas.offsetLeft + w/2) - canvas.width/2);
214 var yOffset = (canvas.height/2 - (top - canvas.offsetTop + h/2));
215
216 var line = new GLLine(world, xOffset, yOffset, w, h, slope, strokeSize, strokeColor, strokeMaterial, strokeStyle, xAdj, yAdj);
217
218 world.addObject(line);
219 world.render();
220
221 canvas.elementModel.shapeModel.shapeCount++;
222 if(canvas.elementModel.shapeModel.shapeCount === 1)
223 {
224 canvas.elementModel.selection = "Line";
225 canvas.elementModel.pi = "LinePi";
226 canvas.elementModel.shapeModel.strokeSize = this.options.strokeSize.value + " " + this.options.strokeSize.units;
227 canvas.elementModel.shapeModel.stroke = strokeColor;
228
229 canvas.elementModel.shapeModel.strokeMaterial = strokeMaterial;
230 canvas.elementModel.shapeModel.strokeMaterialIndex = strokeIndex;
231
232 canvas.elementModel.shapeModel.strokeStyleIndex = strokeStyleIndex;
233 canvas.elementModel.shapeModel.strokeStyle = strokeStyle;
234
235 canvas.elementModel.shapeModel.GLGeomObj = line;
236 }
237 else
238 {
239 // TODO - update the shape's info only. shapeModel will likely need an array of shapes.
240 }