diff options
Diffstat (limited to 'js/tools/drawing-tool.js')
-rw-r--r-- | js/tools/drawing-tool.js | 370 |
1 files changed, 370 insertions, 0 deletions
diff --git a/js/tools/drawing-tool.js b/js/tools/drawing-tool.js new file mode 100644 index 00000000..603f63a3 --- /dev/null +++ b/js/tools/drawing-tool.js | |||
@@ -0,0 +1,370 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No 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 | |||
7 | var Montage = require("montage/core/core").Montage, | ||
8 | ToolBase = require("js/tools/ToolBase").toolBase, | ||
9 | DrawingToolBase = require("js/tools/drawing-tool-base").DrawingToolBase, | ||
10 | Keyboard = require("js/mediators/keyboard-mediator").Keyboard; | ||
11 | |||
12 | exports.DrawingTool = Montage.create(ToolBase, { | ||
13 | drawingFeedback: { value: { mode: "Draw2D", type: "" } }, // Default Value | ||
14 | _canOperateOnStage: { value: true }, | ||
15 | |||
16 | _canDraw: { value: true }, | ||
17 | _isDrawing: { value: false }, | ||
18 | _hasDraw: { value: false }, | ||
19 | _isSpace: { value: false }, | ||
20 | |||
21 | _downPoint: { value: { "x": null, "y": null} }, | ||
22 | _upPoint: { value: { "x": null, "y": null} }, | ||
23 | |||
24 | _mouseDownHitRec: { value: null }, | ||
25 | _mouseUpHitRec: { value: null }, | ||
26 | |||
27 | _currentX: { value: null }, | ||
28 | _currentY: { value: null }, | ||
29 | _currentDX: { value: 0 }, | ||
30 | _currentDY: { value: 0 }, | ||
31 | |||
32 | isDrawing: { | ||
33 | get: function () { return this._isDrawing; }, | ||
34 | set: function (value) { this._isDrawing = value; } | ||
35 | }, | ||
36 | |||
37 | downPoint: { | ||
38 | get: function () { return this._downPoint; }, | ||
39 | set: function (value) { this._downPoint = value; } | ||
40 | }, | ||
41 | |||
42 | upPoint: { | ||
43 | get: function () { return this._upPoint; }, | ||
44 | set: function (value) { this._upPoint = value; } | ||
45 | }, | ||
46 | |||
47 | mouseDownHitRec: { | ||
48 | get: function () { return this._mouseDownHitRec; }, | ||
49 | set: function (value) { this._mouseDownHitRec = value; } | ||
50 | }, | ||
51 | |||
52 | mouseUpHitRec: { | ||
53 | get: function () { return this._mouseUpHitRec; }, | ||
54 | set: function (value) { this._mouseUpHitRec = value; } | ||
55 | }, | ||
56 | |||
57 | currentX: { | ||
58 | get: function () { return this._currentX; }, | ||
59 | set: function (value) { this._currentX = value; } | ||
60 | }, | ||
61 | |||
62 | currentY: { | ||
63 | get: function () { return this._currentY; }, | ||
64 | set: function (value) { this._currentY = value; } | ||
65 | }, | ||
66 | |||
67 | /** | ||
68 | * PUBLIC METHODS | ||
69 | */ | ||
70 | startDraw: { | ||
71 | value: function (event) { | ||
72 | var snapData, point; | ||
73 | |||
74 | this._isDrawing = true; | ||
75 | this.mouseUpHitRec = null; | ||
76 | |||
77 | point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(event.pageX, event.pageY)); | ||
78 | snapData = DrawingToolBase.getInitialSnapPoint(point.x, point.y); //event.layerX, event.layerY); | ||
79 | this.mouseDownHitRec = snapData[0]; | ||
80 | this.downPoint.x = snapData[1]; | ||
81 | this.downPoint.y = snapData[2]; | ||
82 | } | ||
83 | }, | ||
84 | |||
85 | /** | ||
86 | * This base method will perform stage feedback drawing if the mouse is down. | ||
87 | * Otherwise it will only perform the snapping feedback | ||
88 | */ | ||
89 | doDraw: { | ||
90 | value: function (event) { | ||
91 | var point; | ||
92 | point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(event.pageX, event.pageY)); | ||
93 | |||
94 | if (this.drawingFeedback.mode === "Draw3D") { | ||
95 | var do3DSnap; | ||
96 | this.application.ninja.stage.clearDrawingCanvas(); | ||
97 | |||
98 | do3DSnap = false; | ||
99 | |||
100 | if (this._isSpace) { | ||
101 | this._currentDX = point.x - this._currentX; | ||
102 | this._currentDY = point.y - this._currentY; | ||
103 | this.HandleSpaceKeyDown(); | ||
104 | } else { | ||
105 | this._currentX = point.x; | ||
106 | this._currentY = point.y; | ||
107 | } | ||
108 | |||
109 | if (event.shiftKey) { | ||
110 | this.HandleShiftKeyDown(); | ||
111 | } else if (event.altKey) { | ||
112 | this.HandleAltKeyDown(); | ||
113 | } else { | ||
114 | do3DSnap = event.ctrlKey || event.metaKey; | ||
115 | this.mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(point.x, point.y, do3DSnap, this.mouseDownHitRec); | ||
116 | this._doDraw(); | ||
117 | } | ||
118 | |||
119 | DrawingToolBase.drawSnapLastHit(); | ||
120 | } else if (this.drawingFeedback.mode === "Draw2D") { | ||
121 | if(this._isSpace) { | ||
122 | this._currentDX = point.x - this._currentX; | ||
123 | this._currentDY = point.y - this._currentY; | ||
124 | |||
125 | this.downPoint.x += this._currentDX; | ||
126 | this.downPoint.y += this._currentDY; | ||
127 | this.currentX += this._currentDX; | ||
128 | this.currentY += this._currentDY; | ||
129 | |||
130 | DrawingToolBase.draw2DRectangle(this.downPoint.x,this.downPoint.y,this.currentX - this.downPoint.x,this.currentY - this.downPoint.y); | ||
131 | } else { | ||
132 | this._currentX = point.x; | ||
133 | this._currentY = point.y; | ||
134 | |||
135 | DrawingToolBase.draw2DRectangle(this.downPoint.x,this.downPoint.y,point.x - this.downPoint.x,point.y - this.downPoint.y); | ||
136 | } | ||
137 | |||
138 | } | ||
139 | |||
140 | } | ||
141 | }, | ||
142 | |||
143 | _doDraw: { | ||
144 | value: function () { | ||
145 | if (this.mouseDownHitRec !== null) { | ||
146 | DrawingToolBase.stageComponent = this.application.ninja.stage; | ||
147 | DrawingToolBase.drawRectangle(this.mouseDownHitRec, this.mouseUpHitRec); | ||
148 | } | ||
149 | } | ||
150 | }, | ||
151 | |||
152 | endDraw: { | ||
153 | value: function (event) { | ||
154 | this.application.ninja.stage.clearDrawingCanvas(); | ||
155 | |||
156 | this.mouseDownHitRec = null; | ||
157 | this.mouseUpHitRec = null; | ||
158 | this.downPoint.x = null; | ||
159 | this.downPoint.y = null; | ||
160 | this._isDrawing = false; | ||
161 | |||
162 | if (this.drawingFeedback.mode === "Draw3D") { | ||
163 | DrawingToolBase.cleanupSnap(); | ||
164 | } | ||
165 | } | ||
166 | }, | ||
167 | |||
168 | // Used when ESC is hit to cancel the current drawing. | ||
169 | cancelDraw: { | ||
170 | value: function() { | ||
171 | this.application.ninja.stage.clearDrawingCanvas(); | ||
172 | |||
173 | if(this.drawingFeedback.mode === "Draw3D") { | ||
174 | this._isDrawing = false; | ||
175 | DrawingToolBase.cleanupSnap(); | ||
176 | } | ||
177 | } | ||
178 | }, | ||
179 | |||
180 | doSnap: { | ||
181 | value: function (event) { | ||
182 | var point; | ||
183 | point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(event.pageX, event.pageY)); | ||
184 | this.application.ninja.stage.clearDrawingCanvas(); | ||
185 | this.mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(point.x, point.y, true, this.mouseDownHitRec); | ||
186 | } | ||
187 | }, | ||
188 | |||
189 | drawLastSnap: { | ||
190 | value: function () { | ||
191 | DrawingToolBase.drawSnapLastHit(); | ||
192 | } | ||
193 | }, | ||
194 | |||
195 | getMouseDownPos: { | ||
196 | value: function () { | ||
197 | return DrawingToolBase.getHitRecPos(this.mouseDownHitRec); | ||
198 | } | ||
199 | }, | ||
200 | |||
201 | getMouseUpPos: { | ||
202 | value: function () { | ||
203 | return DrawingToolBase.getHitRecPos(this.mouseUpHitRec); | ||
204 | } | ||
205 | }, | ||
206 | |||
207 | getDrawingData: { | ||
208 | value: function (event) { | ||
209 | return DrawingToolBase.getCompletePoints(this.mouseDownHitRec, this.mouseUpHitRec); | ||
210 | } | ||
211 | }, | ||
212 | |||
213 | /** | ||
214 | * SHIFT/ALT/SPACE Key Handlers | ||
215 | */ | ||
216 | HandleKeyPress: { | ||
217 | value: function(event) { | ||
218 | if(event.shiftKey) { | ||
219 | this.HandleShiftKeyDown(event); | ||
220 | } else if(event.altKey) { | ||
221 | this.HandleAltKeyDown(event); | ||
222 | } else if (event.keyCode === Keyboard.SPACE) { | ||
223 | event.preventDefault(); | ||
224 | this.HandleSpaceKeyDown(event); | ||
225 | } | ||
226 | } | ||
227 | }, | ||
228 | |||
229 | HandleKeyUp: { | ||
230 | value: function(event) { | ||
231 | if(event.keyCode === 16) { | ||
232 | this.HandleShiftKeyUp(event); | ||
233 | } else if(event.keyCode === 18) { | ||
234 | this.HandleAltKeyUp(event); | ||
235 | } else if (event.keyCode === Keyboard.SPACE) { | ||
236 | event.preventDefault(); | ||
237 | this.HandleSpaceUp(event); | ||
238 | } | ||
239 | } | ||
240 | }, | ||
241 | |||
242 | HandleShiftKeyDown: { | ||
243 | value: function (event) { | ||
244 | if (this._isDrawing) { | ||
245 | var square = this.toSquare(this.downPoint.x, this.currentX, this.downPoint.y, this.currentY); | ||
246 | this.mouseUpHitRec = DrawingToolBase.getUpdatedSnapPoint(square[0] + square[2], square[1] + square[3], false, this.mouseDownHitRec); | ||
247 | this._doDraw(); | ||
248 | } | ||