1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
var ShapeTool = require("js/tools/ShapeTool").ShapeTool;
var DrawingToolBase = require("js/tools/drawing-tool-base").DrawingToolBase;
var defaultEventManager = require("montage/core/event/event-manager").defaultEventManager;
var Montage = require("montage/core/core").Montage;
var NJUtils = require("js/lib/NJUtils").NJUtils;
var ElementMediator = require("js/mediators/element-mediator").ElementMediator;
var TagTool = require("js/tools/TagTool").TagTool;
var snapManager = require("js/helper-classes/3D/snap-manager").SnapManager;
exports.BrushTool = Montage.create(ShapeTool, {
hasReel: { value: false },
_toolID: { value: "brushTool" },
_imageID: { value: "brushToolImg" },
_toolImageClass: { value: "brushToolUp" },
_selectedToolImageClass: { value: "brushToolDown" },
_toolTipText: { value: "Brush Tool" },
_brushView: { value: null, writable: true },
_selectedToolClass: { value: "brushToolSpecificProperties" },
_penToolProperties: { enumerable: false, value: null, writable: true },
_parentNode: { enumerable: false, value: null, writable: true },
_toolsPropertiesContainer: { enumerable: false, value: null, writable: true },
//config options
_useWebGL: {value: false, writable: false},
//view options
_brushStrokeCanvas: {value: null, writable: true},
_brushStrokePlaneMat: {value: null, writable: true},
//the current brush stroke
_selectedBrushStroke: {value: null, writable: true},
ShowToolProperties: {
value: function () {
this._brushView = PenView.create();
this._brushView.element = document.getElementById('topPanelContainer').children[0];
this._brushView.needsDraw = true;
this._brushView.addEventListener(ToolEvents.TOOL_OPTION_CHANGE, this, false);
}
},
HandleLeftButtonDown: {
value: function (event) {
//ignore any right or middle clicks
if (event.button !== 0) {
//NOTE: this will work on Webkit only...IE has different codes (left: 1, middle: 4, right: 2)
return;
}
if (this._canDraw) {
this._isDrawing = true;
}
this.startDraw(event);
if (this._brushStrokePlaneMat === null) {
this._brushStrokePlaneMat = this.mouseDownHitRec.getPlaneMatrix();
}
//start a new brush stroke
if (this._selectedBrushStroke === null){
this._selectedBrushStroke = new GLBrushStroke();
}
console.log("BrushTool Start");
NJevent("enableStageMove");//stageManagerModule.stageManager.enableMouseMove();
} //value: function (event) {
}, //HandleLeftButtonDown
//need to override this function because the ShapeTool's definition contains a clearDrawingCanvas call - Pushkar
// might not need to override once we draw using OpenGL instead of SVG
// Also took out all the snapping code for now...need to add that back
HandleMouseMove:
{
value: function (event) {
//ignore any right or middle clicks
if (event.button !== 0) {
//NOTE: this will work on Webkit only...IE has different codes (left: 1, middle: 4, right: 2)
return;
}
if (this._isDrawing) {
snapManager.enableElementSnap(false);
snapManager.enableGridSnap(false);
snapManager.enableSnapAlign(false);
//this.doDraw(event);
//var currMousePos = this.getMouseUpPos();
//instead of doDraw call own DrawingTool
var point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(event.pageX, event.pageY));
var hitRecSnapPoint = DrawingToolBase.getUpdatedSnapPointNoAppLevelEnabling(point.x, point.y, true, this.mouseDownHitRec);
var currMousePos = DrawingToolBase.getHitRecPos(hitRecSnapPoint);
if (this._selectedBrushStroke){
this._selectedBrushStroke.addPoint(currMousePos);
}
this.ShowCurrentBrushStrokeOnStage();
} //if (this._isDrawing) {
//this.drawLastSnap(); //TODO.. is this line necessary if we're not snapping? // Required cleanup for both Draw/Feedbacks
}//value: function(event)
},
HandleLeftButtonUp: {
value: function (event) {
/*var drawData = this.getDrawingData();
if (drawData) {
if (this._brushStrokePlaneMat === null) {
this._brushStrokePlaneMat = drawData.planeMat;
}
}
if (this._isDrawing) {
this.doDraw(event);
}*/
this.endDraw(event);
this._isDrawing = false;
this._hasDraw = false;
console.log("BrushTool Stop");
//TODO get these values from the options
if (this._selectedBrushStroke){
this._selectedBrushStroke.setStrokeWidth(20);
}
//display the previously drawn stroke in a separate canvas
this.RenderCurrentBrushStroke();
this._selectedBrushStroke = null;
this._brushStrokeCanvas = null;
NJevent("disableStageMove");
}
},
ShowCurrentBrushStrokeOnStage:{
value: function() {
//clear the canvas before we draw anything else
this.application.ninja.stage.clearDrawingCanvas();
if (this._selectedBrushStroke && this._selectedBrushStroke.getNumPoints()>0){
this._selectedBrushStroke.computeMetaGeometry();
var ctx = this.application.ninja.stage.drawingContext;//stageManagerModule.stageManager.drawingContext;
if (ctx === null)
throw ("null drawing context in Brushtool::ShowCurrentBrushStrokeOnStage");
ctx.save();
var horizontalOffset = this.application.ninja.stage.userContentLeft;
var verticalOffset = this.application.ninja.stage.userContentTop;
var numPoints = this._selectedBrushStroke.getNumPoints();
ctx.lineWidth = 1;
ctx.strokeStyle = "black";
ctx.beginPath();
var pt = this._selectedBrushStroke.getPoint(0);
ctx.moveTo(pt[0]+ horizontalOffset,pt[1]+ verticalOffset);
for (var i = 1; i < numPoints; i++) {
pt = this._selectedBrushStroke.getPoint(i);
var x = pt[0]+ horizontalOffset;
var y = pt[1]+ verticalOffset;
ctx.lineTo(x,y);
}
ctx.stroke();
ctx.restore();
}
}
},
RenderCurrentBrushStroke:{
value: function() {
if (this._selectedBrushStroke){
this._selectedBrushStroke.computeMetaGeometry();
var bboxMin = this._selectedBrushStroke.getBBoxMin();
var bboxMax = this._selectedBrushStroke.getBBoxMax();
var bboxWidth = bboxMax[0] - bboxMin[0];
var bboxHeight = bboxMax[1] - bboxMin[1];
var bboxMid = Vector.create([0.5 * (bboxMax[0] + bboxMin[0]), 0.5 * (bboxMax[1] + bboxMin[1]), 0.5 * (bboxMax[2] + bboxMin[2])]);
this._selectedBrushStroke.setCanvasX(bboxMid[0]);
this._selectedBrushStroke.setCanvasY(bboxMid[1]);
//call render shape with the bbox width and height
this.RenderShape(bboxWidth, bboxHeight, this._brushStrokePlaneMat, bboxMid, this._brushStrokeCanvas);
}
}
},
RenderShape: {
value: function (w, h, planeMat, midPt, canvas) {
if ((Math.floor(w) === 0) || (Math.floor(h) === 0)) {
return;
}
var left = Math.round(midPt[0] - 0.5 * w);
var top = Math.round(midPt[1] - 0.5 * h);
if (!canvas) {
var newCanvas = NJUtils.makeNJElement("canvas", "Brushstroke", "shape", null, true);
var elementModel = TagTool.makeElement(w, h, planeMat, midPt, newCanvas);
ElementMediator.addElement(newCanvas, elementModel.data, true);
// create all the GL stuff
var world = this.getGLWorld(newCanvas, this._useWebGL);
//store a reference to this newly created canvas
this._brushStrokeCanvas = newCanvas;
var brushStroke = this._selectedBrushStroke;
if (brushStroke){
brushStroke.setWorld(world);
brushStroke.setPlaneMatrix(planeMat);
var planeMatInv = glmat4.inverse( planeMat, [] );
brushStroke.setPlaneMatrixInverse(planeMatInv);
brushStroke.setPlaneCenter(midPt);
world.addObject(brushStroke);
world.render();
//TODO this will not work if there are multiple shapes in the same canvas
newCanvas.elementModel.shapeModel.GLGeomObj = brushStroke;
}
} //if (!canvas) {
else {
var world = null;
if (canvas.elementModel.shapeModel && canvas.elementModel.shapeModel.GLWorld) {
world = canvas.elementModel.shapeModel.GLWorld;
} else {
world = this.getGLWorld(canvas, this._useWebGL);//this.options.use3D);//this.CreateGLWorld(planeMat, midPt, canvas, this._useWebGL);//fillMaterial, strokeMaterial);
}
if (this._entryEditMode !== this.ENTRY_SELECT_CANVAS){
//update the left and top of the canvas element
var canvasArray=[canvas];
ElementMediator.setProperty(canvasArray, "left", [parseInt(left)+"px"],"Changing", "brushTool");
ElementMediator.setProperty(canvasArray, "top", [parseInt(top) + "px"],"Changing", "brushTool");
canvas.width = w;
canvas.height = h;
//update the viewport and projection to reflect the new canvas width and height
world.setViewportFromCanvas(canvas);
if (this._useWebGL){
var cam = world.renderer.cameraManager().getActiveCamera();
cam.setPerspective(world.getFOV(), world.getAspect(), world.getZNear(), world.getZFar());
}
}
var brushStroke = this._selectedBrushStroke;
if (brushStroke){
brushStroke.setDrawingTool(this);
brushStroke.setPlaneMatrix(planeMat);
var planeMatInv = glmat4.inverse( planeMat, [] );
brushStroke.setPlaneMatrixInverse(planeMatInv);
brushStroke.setPlaneCenter(midPt);
brushStroke.setWorld(world);
world.addIfNewObject(brushStroke);
//world.addObject(subpath);
world.render();
//TODO this will not work if there are multiple shapes in the same canvas
canvas.elementModel.shapeModel.GLGeomObj = brushStroke;
}
} //else of if (!canvas) {
} //value: function (w, h, planeMat, midPt, canvas) {
}, //RenderShape: {
Configure: {
value: function (wasSelected) {
if (wasSelected) {
console.log("Picked BrushTool");
//todo these calls have no effect because the drawing-tool-base (in getInitialSnapPoint) overrides them with what's set at the application level
snapManager.enableElementSnap(false);
snapManager.enableGridSnap(false);
snapManager.enableSnapAlign(false);
}
else {
console.log("Left BrushTool");
}
}
}
});
|