aboutsummaryrefslogtreecommitdiff
path: root/js/tools
diff options
context:
space:
mode:
Diffstat (limited to 'js/tools')
-rw-r--r--js/tools/BrushTool.js14
-rwxr-xr-xjs/tools/PenTool.js355
2 files changed, 281 insertions, 88 deletions
diff --git a/js/tools/BrushTool.js b/js/tools/BrushTool.js
index 0be378fd..f85978ad 100644
--- a/js/tools/BrushTool.js
+++ b/js/tools/BrushTool.js
@@ -39,7 +39,7 @@ exports.BrushTool = Montage.create(ShapeTool, {
39 //view options 39 //view options
40 _brushStrokeCanvas: {value: null, writable: true}, 40 _brushStrokeCanvas: {value: null, writable: true},
41 _brushStrokePlaneMat: {value: null, writable: true}, 41 _brushStrokePlaneMat: {value: null, writable: true},
42 _draggingPlane: {value: null, writable: true}, 42 dragPlane: {value: null, writable: true},
43 43
44 //the current brush stroke 44 //the current brush stroke
45 _selectedBrushStroke: {value: null, writable: true}, 45 _selectedBrushStroke: {value: null, writable: true},
@@ -67,7 +67,7 @@ exports.BrushTool = Montage.create(ShapeTool, {
67 67
68 var point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(x,y)); 68 var point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(x,y));
69 var unsnappedpos = DrawingToolBase.getHitRecPos(snapManager.snap(point.x, point.y, false)); 69 var unsnappedpos = DrawingToolBase.getHitRecPos(snapManager.snap(point.x, point.y, false));
70 this._draggingPlane = snapManager.getDragPlane(); 70 this.dragPlane = snapManager.getDragPlane();
71 71
72 snapManager.enableElementSnap(elemSnap); 72 snapManager.enableElementSnap(elemSnap);
73 snapManager.enableGridSnap(gridSnap); 73 snapManager.enableGridSnap(gridSnap);
@@ -100,7 +100,7 @@ exports.BrushTool = Montage.create(ShapeTool, {
100 } 100 }
101 var tmpPoint = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(x,y)); 101 var tmpPoint = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(x,y));
102 var hitRec = snapManager.snap(tmpPoint.x, tmpPoint.y, false); 102 var hitRec = snapManager.snap(tmpPoint.x, tmpPoint.y, false);
103 this._draggingPlane = snapManager.getDragPlane(); 103 this.dragPlane = snapManager.getDragPlane();
104 if (this._selectedSubpathCanvas){ 104 if (this._selectedSubpathCanvas){
105 snapManager.popWorkingPlane(); 105 snapManager.popWorkingPlane();
106 } 106 }
@@ -225,9 +225,11 @@ exports.BrushTool = Montage.create(ShapeTool, {
225 this._hasDraw = false; 225 this._hasDraw = false;
226 226
227 //finish giving enough info. to the brush stroke 227 //finish giving enough info. to the brush stroke
228 this._selectedBrushStroke.setPlaneMatrix(this._brushStrokePlaneMat); 228 if (this._selectedBrushStroke){
229 this._selectedBrushStroke.setPlaneMatrixInverse(glmat4.inverse(this._brushStrokePlaneMat,[])); 229 this._selectedBrushStroke.setPlaneMatrix(this._brushStrokePlaneMat);
230 this._selectedBrushStroke.setDragPlane(this._draggingPlane); 230 this._selectedBrushStroke.setPlaneMatrixInverse(glmat4.inverse(this._brushStrokePlaneMat,[]));
231 this._selectedBrushStroke.setDragPlane(this.dragPlane);
232 }
231 233
232 //display the previously drawn stroke in a separate canvas 234 //display the previously drawn stroke in a separate canvas
233 this.RenderCurrentBrushStroke(); 235 this.RenderCurrentBrushStroke();
diff --git a/js/tools/PenTool.js b/js/tools/PenTool.js
index 3813123a..1536a334 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
@@ -95,6 +95,12 @@ exports.PenTool = Montage.create(ShapeTool, {
95 ENTRY_SELECT_PATH: { value: 2, writable: false}, 95 ENTRY_SELECT_PATH: { value: 2, writable: false},
96 _entryEditMode: {value: this.ENTRY_SELECT_NONE, writable: true}, 96 _entryEditMode: {value: this.ENTRY_SELECT_NONE, writable: true},
97 97
98 //constants used for determining whether a subtool has been selected (mutually exclusive i.e. cannot be OR-ed)
99 SUBTOOL_NONE: {value: 0, writable: false},
100 SUBTOOL_PENPLUS: {value: 1, writable: false},
101 SUBTOOL_PENMINUS: {value: 2, writable: false},
102 _subtool: {value: this.SUBTOOL_NONE, writable: true},
103
98 //constants used for limiting size of the subpath canvas 104 //constants used for limiting size of the subpath canvas
99 _MAX_CANVAS_DIMENSION: {value: 3000, writable: false}, 105 _MAX_CANVAS_DIMENSION: {value: 3000, writable: false},
100 106
@@ -179,6 +185,71 @@ exports.PenTool = Montage.create(ShapeTool, {
179 } 185 }
180 }, 186 },
181 187
188 _removeSelectedSubpathAndCanvas:{
189 value: function(removeSelectedSubpath){
190 if (removeSelectedSubpath){
191 this._selectedSubpath.clearAllAnchors(); //perhaps unnecessary
192 this._selectedSubpath = null;
193 if (this._subtool === this.SUBTOOL_NONE){
194 if (this._entryEditMode === this.ENTRY_SELECT_PATH){
195 this._entryEditMode = this.ENTRY_SELECT_NONE;
196 }
197 }
198 } else {
199 this._selectedSubpath.setCanvas(null);
200 }
201 //clear the canvas
202 this.application.ninja.stage.clearDrawingCanvas();//stageManagerModule.stageManager.clearDrawingCanvas();
203
204 //undo/redo...go through ElementController and NJEvent
205 var els = [];
206 ElementController.removeElement(this._selectedSubpathCanvas);
207 els.push(this._selectedSubpathCanvas);
208 NJevent( "elementsRemoved", els );
209 this._selectedSubpathCanvas = null;
210 }
211 },
212
213 _removeSelectedAnchorPoint:{
214 value: function(){
215 this._hoveredAnchorIndex=-1;
216 this._selectedSubpath.removeAnchor(this._selectedSubpath.getSelectedAnchorIndex());
217 if (this._selectedSubpath.getNumAnchors()===1){
218 //convert the remaining anchor point to stage world coords
219 var xDelta = snapManager.getStageWidth()*0.5;
220 var yDelta = snapManager.getStageHeight()*0.5;
221 var anchor = this._selectedSubpath.getAnchor(0);
222 var swPos = ViewUtils.localToStageWorld([anchor.getPosX(),anchor.getPosY(),anchor.getPosZ()], this._selectedSubpathCanvas);
223 anchor.setPos(swPos[0]+xDelta, swPos[1]+yDelta, swPos[2]);
224 swPos = ViewUtils.localToStageWorld([anchor.getPrevX(),anchor.getPrevY(),anchor.getPrevZ()], this._selectedSubpathCanvas);
225 anchor.setPrevPos(swPos[0]+xDelta, swPos[1]+yDelta, swPos[2]);
226 swPos = ViewUtils.localToStageWorld([anchor.getNextX(),anchor.getNextY(),anchor.getNextZ()], this._selectedSubpathCanvas);
227 anchor.setNextPos(swPos[0]+xDelta, swPos[1]+yDelta, swPos[2]);
228 }
229 //clear the canvas
230 this.application.ninja.stage.clearDrawingCanvas();//stageManagerModule.stageManager.clearDrawingCanvas();
231 var removeSelectedSubpath=true;
232 var newNumAnchors = this._selectedSubpath.getNumAnchors();
233 if (newNumAnchors>1) {
234 this._selectedSubpath.createSamples(false);
235 this.PrepareSelectedSubpathForRendering();
236 this.ShowSelectedSubpath();
237 }
238 else {
239 //since we have 0 or 1 anchors, we will remove the selected canvas (as the path does not exist)
240 if (newNumAnchors===0){
241 removeSelectedSubpath = true;
242 } else{
243 removeSelectedSubpath = false; //don't remove the selected subpath if there is still one anchor
244 }
245 this._removeSelectedSubpathAndCanvas(removeSelectedSubpath);
246 }
247 if (!removeSelectedSubpath){
248 this.DrawSubpathAnchors(this._selectedSubpath);
249 }
250 }
251 },
252
182 // ********************************************************************************************************** 253 // **********************************************************************************************************
183 // Mouse down handler 254 // Mouse down handler
184 // IF the selected subpath is null, it means we're going to start a new subpath 255 // IF the selected subpath is null, it means we're going to start a new subpath
@@ -204,7 +275,7 @@ exports.PenTool = Montage.create(ShapeTool, {
204 value: function (event) { 275 value: function (event) {
205 //ignore any right or middle clicks 276 //ignore any right or middle clicks
206 if (event.button !== 0) { 277 if (event.button !== 0) {
207 //NOTE: this will work on Webkit only...IE has different codes (left: 1, middle: 4, right: 2) 278 //todo NOTE: this will work on Webkit only...IE has different codes (left: 1, middle: 4, right: 2)
208 return; 279 return;
209 } 280 }
210 281
@@ -220,6 +291,11 @@ exports.PenTool = Montage.create(ShapeTool, {
220 if (this._entryEditMode !== this.ENTRY_SELECT_PATH && this._selectedSubpath && this._selectedSubpath.getIsClosed() && this._makeMultipleSubpaths) { 291 if (this._entryEditMode !== this.ENTRY_SELECT_PATH && this._selectedSubpath && this._selectedSubpath.getIsClosed() && this._makeMultipleSubpaths) {
221 this._selectedSubpath = null; 292 this._selectedSubpath = null;
222 } 293 }
294
295 if (this._subtool !== this.SUBTOOL_NONE && this._selectedSubpath===null) {
296 //do nothing because the pen plus and pen minus subtools need a selected subpath
297 return;
298 }
223 if (this._selectedSubpath === null) { 299 if (this._selectedSubpath === null) {
224 this._selectedSubpath = new SubPath(); 300 this._selectedSubpath = new SubPath();
225 this._selectedSubpathCanvas = null; 301 this._selectedSubpathCanvas = null;
@@ -255,12 +331,24 @@ exports.PenTool = Montage.create(ShapeTool, {
255 colorArray = [1,1,1,0]; 331 colorArray = [1,1,1,0];
256 } 332 }
257 this._selectedSubpath.setFillColor(colorArray); 333 this._selectedSubpath.setFillColor(colorArray);
258 } 334 } //if the selectedSubpath was null and needed to be constructed
259 335
260 //build the hit record for the current mouse position (on the stage or the plane of the path canvas) 336 //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); 337 var hitRec = this.getHitRecord(event.pageX, event.pageY, false);
262 338 var globalMousePos=null, localMousePos=null, stageWorldMousePos = null, drawingCanvas=null;
263 if (this._selectedSubpathCanvas === null){ 339 if (!this._selectedSubpathCanvas){
340 drawingCanvas = this.application.ninja.currentDocument.model.documentRoot;//ViewUtils.getStageElement();
341 stageWorldMousePos = hitRec.calculateStageWorldPoint();
342 stageWorldMousePos[0]+= snapManager.getStageWidth()*0.5;
343 stageWorldMousePos[1]+= snapManager.getStageHeight()*0.5;
344 localMousePos = stageWorldMousePos; //since the subpath points are in stage world space, set the 'localMousePos' to be stage world as well
345 }
346 else {
347 globalMousePos = hitRec.getScreenPoint();
348 localMousePos = ViewUtils.globalToLocal(globalMousePos, this._selectedSubpathCanvas);
349 }
350
351 if (this._selectedSubpathCanvas === null && this._subtool===this.SUBTOOL_NONE){
264 //IF this is the first anchor point of the selected subpath 352 //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) 353 // 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 354 // Add the mouse position (in stage world space) as an anchor point
@@ -269,18 +357,36 @@ exports.PenTool = Montage.create(ShapeTool, {
269 this._selectedSubpathPlaneMat = hitRec.getPlaneMatrix(); 357 this._selectedSubpathPlaneMat = hitRec.getPlaneMatrix();
270 } 358 }
271 359
272 //calculate the stage world position from the hit record 360 //check if the mouse click location is close to the existing anchor
273 var swMousePos = hitRec.calculateStageWorldPoint(); 361 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; 362 if (indexAndCode[0]>=0){
275 363 //the anchor point was hit, so we do not add another anchor
276 this._selectedSubpath.addAnchor(new AnchorPoint());