diff options
Diffstat (limited to 'js/tools/PenTool.js')
-rwxr-xr-x | js/tools/PenTool.js | 445 |
1 files changed, 355 insertions, 90 deletions
diff --git a/js/tools/PenTool.js b/js/tools/PenTool.js index 3813123a..e296d0e5 100755 --- a/js/tools/PenTool.js +++ b/js/tools/PenTool.js | |||
@@ -33,10 +33,10 @@ exports.PenTool = Montage.create(ShapeTool, { | |||
33 | _parentNode: { enumerable: false, value: null, writable: true }, | 33 | _parentNode: { enumerable: false, value: null, writable: true }, |
34 | _toolsPropertiesContainer: { enumerable: false, value: null, writable: true }, | 34 | _toolsPropertiesContainer: { enumerable: false, value: null, writable: true }, |
35 | 35 | ||
36 | //set this to true if you want to keep making subpaths after closing current subpath (debugging only) | 36 | //set this to true if you want to keep making subpaths after closing current subpath (debugging only...should always be true) |
37 | _makeMultipleSubpaths: { value: true, writable: true }, | 37 | _makeMultipleSubpaths: { value: true, writable: true }, |
38 | 38 | ||
39 | //set this to false if you don't want the mouse move handler being called when the mouse is not down (debugging only) | 39 | //set this to false if you don't want the mouse move handler being called when the mouse is not down (debugging only...should always be true) |
40 | _trackMouseMoveWhenUp: {value: true, writable: false}, | 40 | _trackMouseMoveWhenUp: {value: true, writable: false}, |
41 | 41 | ||
42 | //whether the user has held down the Alt key | 42 | //whether the user has held down the Alt key |
@@ -46,7 +46,6 @@ exports.PenTool = Montage.create(ShapeTool, { | |||
46 | _isEscapeDown: {value: false, writable: true }, | 46 | _isEscapeDown: {value: false, writable: true }, |
47 | 47 | ||
48 | //whether we have just started a new path (may set true in mousedown, and always set false in mouse up | 48 | //whether we have just started a new path (may set true in mousedown, and always set false in mouse up |
49 | //todo this seems to be unnecessary | ||
50 | _isNewPath: {value: false, writable: true}, | 49 | _isNewPath: {value: false, writable: true}, |
51 | 50 | ||
52 | //whether we have clicked one of the endpoints after entering the pen tool in ENTRY_SELECT_PATH edit mode | 51 | //whether we have clicked one of the endpoints after entering the pen tool in ENTRY_SELECT_PATH edit mode |
@@ -73,7 +72,10 @@ exports.PenTool = Montage.create(ShapeTool, { | |||
73 | 72 | ||
74 | //the center of the subpath center in stageworld space | 73 | //the center of the subpath center in stageworld space |
75 | _selectedSubpathCanvasCenter: {value: null, writable: true}, | 74 | _selectedSubpathCanvasCenter: {value: null, writable: true}, |
76 | 75 | ||
76 | //this flag is set true by the Configure(true) and set false by Configure(false) or handleSelectionChange | ||
77 | _doesSelectionChangeNeedHandling: {value: false, writable: true}, | ||
78 | |||
77 | //constants used for picking points --- todo: these should be user-settable parameters | 79 | //constants used for picking points --- todo: these should be user-settable parameters |
78 | _PICK_POINT_RADIUS: { value: 4, writable: false }, | 80 | _PICK_POINT_RADIUS: { value: 4, writable: false }, |
79 | _DISPLAY_ANCHOR_RADIUS: { value: 5, writable: false }, | 81 | _DISPLAY_ANCHOR_RADIUS: { value: 5, writable: false }, |
@@ -95,6 +97,12 @@ exports.PenTool = Montage.create(ShapeTool, { | |||
95 | ENTRY_SELECT_PATH: { value: 2, writable: false}, | 97 | ENTRY_SELECT_PATH: { value: 2, writable: false}, |
96 | _entryEditMode: {value: this.ENTRY_SELECT_NONE, writable: true}, | 98 | _entryEditMode: {value: this.ENTRY_SELECT_NONE, writable: true}, |
97 | 99 | ||
100 | //constants used for determining whether a subtool has been selected (mutually exclusive i.e. cannot be OR-ed) | ||
101 | SUBTOOL_NONE: {value: 0, writable: false}, | ||
102 | SUBTOOL_PENPLUS: {value: 1, writable: false}, | ||
103 | SUBTOOL_PENMINUS: {value: 2, writable: false}, | ||
104 | _subtool: {value: this.SUBTOOL_NONE, writable: true}, | ||
105 | |||
98 | //constants used for limiting size of the subpath canvas | 106 | //constants used for limiting size of the subpath canvas |
99 | _MAX_CANVAS_DIMENSION: {value: 3000, writable: false}, | 107 | _MAX_CANVAS_DIMENSION: {value: 3000, writable: false}, |
100 | 108 | ||
@@ -179,6 +187,71 @@ exports.PenTool = Montage.create(ShapeTool, { | |||
179 | } | 187 | } |
180 | }, | 188 | }, |
181 | 189 | ||
190 | _removeSelectedSubpathAndCanvas:{ | ||
191 | value: function(removeSelectedSubpath){ | ||
192 | if (removeSelectedSubpath){ | ||
193 | this._selectedSubpath.clearAllAnchors(); //perhaps unnecessary | ||
194 | this._selectedSubpath = null; | ||
195 | if (this._subtool === this.SUBTOOL_NONE){ | ||
196 | if (this._entryEditMode === this.ENTRY_SELECT_PATH){ | ||
197 | this._entryEditMode = this.ENTRY_SELECT_NONE; | ||
198 | } | ||
199 | } | ||
200 | } else { | ||
201 | this._selectedSubpath.setCanvas(null); | ||
202 | } | ||
203 | //clear the canvas | ||
204 | this.application.ninja.stage.clearDrawingCanvas();//stageManagerModule.stageManager.clearDrawingCanvas(); | ||
205 | |||
206 | //undo/redo...go through ElementController and NJEvent | ||
207 | var els = []; | ||
208 | ElementController.removeElement(this._selectedSubpathCanvas); | ||
209 | els.push(this._selectedSubpathCanvas); | ||
210 | NJevent( "elementsRemoved", els ); | ||
211 | this._selectedSubpathCanvas = null; | ||
212 | } | ||
213 | }, | ||
214 | |||
215 | _removeSelectedAnchorPoint:{ | ||
216 | value: function(){ | ||
217 | this._hoveredAnchorIndex=-1; | ||
218 | this._selectedSubpath.removeAnchor(this._selectedSubpath.getSelectedAnchorIndex()); | ||
219 | if (this._selectedSubpath.getNumAnchors()===1){ | ||
220 | //convert the remaining anchor point to stage world coords | ||
221 | var xDelta = snapManager.getStageWidth()*0.5; | ||
222 | var yDelta = snapManager.getStageHeight()*0.5; | ||
223 | var anchor = this._selectedSubpath.getAnchor(0); | ||
224 | var swPos = ViewUtils.localToStageWorld([anchor.getPosX(),anchor.getPosY(),anchor.getPosZ()], this._selectedSubpathCanvas); | ||
225 | anchor.setPos(swPos[0]+xDelta, swPos[1]+yDelta, swPos[2]); | ||
226 | swPos = ViewUtils.localToStageWorld([anchor.getPrevX(),anchor.getPrevY(),anchor.getPrevZ()], this._selectedSubpathCanvas); | ||
227 | anchor.setPrevPos(swPos[0]+xDelta, swPos[1]+yDelta, swPos[2]); | ||
228 | swPos = ViewUtils.localToStageWorld([anchor.getNextX(),anchor.getNextY(),anchor.getNextZ()], this._selectedSubpathCanvas); | ||
229 | anchor.setNextPos(swPos[0]+xDelta, swPos[1]+yDelta, swPos[2]); | ||
230 | } | ||
231 | //clear the canvas | ||
232 | this.application.ninja.stage.clearDrawingCanvas();//stageManagerModule.stageManager.clearDrawingCanvas(); | ||
233 | var removeSelectedSubpath=true; | ||
234 | var newNumAnchors = this._selectedSubpath.getNumAnchors(); | ||
235 | if (newNumAnchors>1) { | ||
236 | this._selectedSubpath.createSamples(false); | ||
237 | this.PrepareSelectedSubpathForRendering(); | ||
238 | this.ShowSelectedSubpath(); | ||
239 | } | ||
240 | else { | ||
241 | //since we have 0 or 1 anchors, we will remove the selected canvas (as the path does not exist) | ||
242 | if (newNumAnchors===0){ | ||
243 | removeSelectedSubpath = true; | ||
244 | } else{ | ||
245 | removeSelectedSubpath = false; //don't remove the selected subpath if there is still one anchor | ||
246 | } | ||
247 | this._removeSelectedSubpathAndCanvas(removeSelectedSubpath); | ||
248 | } | ||
249 | if (!removeSelectedSubpath){ | ||
250 | this.DrawSubpathAnchors(this._selectedSubpath); | ||
251 | } | ||
252 | } | ||
253 | }, | ||
254 | |||
182 | // ********************************************************************************************************** | 255 | // ********************************************************************************************************** |
183 | // Mouse down handler | 256 | // Mouse down handler |
184 | // IF the selected subpath is null, it means we're going to start a new subpath | 257 | // IF the selected subpath is null, it means we're going to start a new subpath |
@@ -204,7 +277,7 @@ exports.PenTool = Montage.create(ShapeTool, { | |||
204 | value: function (event) { | 277 | value: function (event) { |
205 | //ignore any right or middle clicks | 278 | //ignore any right or middle clicks |
206 | if (event.button !== 0) { | 279 | if (event.button !== 0) { |
207 | //NOTE: this will work on Webkit only...IE has different codes (left: 1, middle: 4, right: 2) | 280 | //todo NOTE: this will work on Webkit only...IE has different codes (left: 1, middle: 4, right: 2) |
208 | return; | 281 | return; |
209 | } | 282 | } |
210 | 283 | ||
@@ -215,11 +288,16 @@ exports.PenTool = Montage.create(ShapeTool, { | |||
215 | 288 | ||
216 | //assume we are not starting a new path as we will set this to true if we create a new Subpath() | 289 | //assume we are not starting a new path as we will set this to true if we create a new Subpath() |
217 | this._isNewPath = false; | 290 | this._isNewPath = false; |
218 | 291 | ||
219 | //if we had closed the selected subpath previously, or if we have not yet started anything, create a subpath | 292 | //if we had closed the selected subpath previously, or if we have not yet started anything, create a subpath |
220 | if (this._entryEditMode !== this.ENTRY_SELECT_PATH && this._selectedSubpath && this._selectedSubpath.getIsClosed() && this._makeMultipleSubpaths) { | 293 | if (this._entryEditMode !== this.ENTRY_SELECT_PATH && this._selectedSubpath && this._selectedSubpath.getIsClosed() && this._makeMultipleSubpaths) { |
221 | this._selectedSubpath = null; | 294 | this._selectedSubpath = null; |
222 | } | 295 | } |
296 | |||
297 | if (this._subtool !== this.SUBTOOL_NONE && this._selectedSubpath===null) { | ||
298 | //do nothing because the pen plus and pen minus subtools need a selected subpath | ||
299 | return; | ||
300 | } | ||
223 | if (this._selectedSubpath === null) { | 301 | if (this._selectedSubpath === null) { |
224 | this._selectedSubpath = new SubPath(); | 302 | this._selectedSubpath = new SubPath(); |
225 | this._selectedSubpathCanvas = null; | 303 | this._selectedSubpathCanvas = null; |
@@ -255,12 +333,24 @@ exports.PenTool = Montage.create(ShapeTool, { | |||
255 | colorArray = [1,1,1,0]; | 333 | colorArray = [1,1,1,0]; |
256 | } | 334 | } |
257 | this._selectedSubpath.setFillColor(colorArray); | 335 | this._selectedSubpath.setFillColor(colorArray); |
258 | } | 336 | } //if the selectedSubpath was null and needed to be constructed |
259 | 337 | ||
260 | //build the hit record for the current mouse position (on the stage or the plane of the path canvas) | 338 | //build the hit record for the current mouse position (on the stage or the plane of the path canvas) |
261 | var hitRec = this.getHitRecord(event.pageX, event.pageY, false); | 339 | var hitRec = this.getHitRecord(event.pageX, event.pageY, false); |
262 | 340 | var globalMousePos=null, localMousePos=null, stageWorldMousePos = null, drawingCanvas=null; | |
263 | if (this._selectedSubpathCanvas === null){ | 341 | if (!this._selectedSubpathCanvas){ |
342 | drawingCanvas = this.application.ninja.currentDocument.model.documentRoot;//ViewUtils.getStageElement(); | ||
343 | stageWorldMousePos = hitRec.calculateStageWorldPoint(); | ||
344 | stageWorldMousePos[0]+= snapManager.getStageWidth()*0.5; | ||
345 | stageWorldMousePos[1]+= snapManager.getStageHeight()*0.5; | ||
346 | localMousePos = stageWorldMousePos; //since the subpath points are in stage world space, set the 'localMousePos' to be stage world as well | ||
347 | } | ||
348 | else { | ||
349 | globalMousePos = hitRec.getScreenPoint(); | ||
350 | localMousePos = ViewUtils.globalToLocal(globalMousePos, this._selectedSubpathCanvas); | ||
351 | } | ||
352 | |||
353 | if (this._selectedSubpathCanvas === null && this._subtool===this.SUBTOOL_NONE){ | ||
264 | //IF this is the first anchor point of the selected subpath | 354 | //IF this is the first anchor point of the selected subpath |
265 | // Store the plane mat and drag plane of this hit record (will be used for creating a canvas) | 355 | // Store the plane mat and drag plane of this hit record (will be used for creating a canvas) |
266 | // Add the mouse position (in stage world space) as an anchor point | 356 | // Add the mouse position (in stage world space) as an anchor point |
@@ -269,18 +359,36 @@ exports.PenTool = Montage.create(ShapeTool, { | |||
269 | this._selectedSubpathPlaneMat = hitRec.getPlaneMatrix(); | 359 | this._selectedSubpathPlaneMat = hitRec.getPlaneMatrix(); |
270 | } | 360 | } |
271 | 361 | ||
272 | //calculate the stage world position from the hit record | 362 | //check if the mouse click location is close to the existing anchor |
273 | var swMousePos = hitRec.calculateStageWorldPoint(); | 363 | var indexAndCode = this._selectedSubpath.pickAnchor(stageWorldMousePos[0], stageWorldMousePos[1], stageWorldMousePos[2], this._PICK_POINT_RADIUS); |
274 | swMousePos[0]+= snapManager.getStageWidth()*0.5; swMousePos[1]+= snapManager.getStageHeight()*0.5; | 364 | if (indexAndCode[0]>=0){ |
275 | 365 | //the anchor point was hit, so we do not add another anchor | |
276 | this._selectedSubpath.addAnchor(new AnchorPoint()); | 366 | switch(indexAndCode[1]){ |
277 | var newAnchor = this._selectedSubpath.getAnchor(this._selectedSubpath.getSelectedAnchorIndex()); | 367 | case this._selectedSubpath.SEL_ANCHOR: |
278 | newAnchor.setPos(swMousePos[0], swMousePos[1], swMousePos[2]); | 368 | this._editMode = this.EDIT_ANCHOR; |
279 | newAnchor.setPrevPos(swMousePos[0], swMousePos[1], swMousePos[2]); | 369 | break; |
280 | newAnchor.setNextPos(swMousePos[0], swMousePos[1], swMousePos[2]); | 370 | case this._selectedSubpath.SEL_PREV: |
281 | //set the mode so that dragging will update the next and previous locations | 371 | this._editMode = this.EDIT_PREV; |
282 | this._editMode = this.EDIT_PREV_NEXT; | 372 | break; |
283 | } | 373 | case this._selectedSubpath.SEL_NEXT: |
374 | this._editMode = this.EDIT_NEXT; | ||
375 | break; | ||
376 | default: | ||
377 | this._editMode = this.EDIT_ANCHOR; | ||
378 | console.log("WARNING picked anchor point with incorrect mode"); | ||
379 | break; | ||
380 | } | ||
381 | |||
382 | } else { | ||
383 | this._selectedSubpath.addAnchor(new AnchorPoint()); | ||
384 | var newAnchor = this._selectedSubpath.getAnchor(this._selectedSubpath.getSelectedAnchorIndex()); | ||
385 |