aboutsummaryrefslogtreecommitdiff
path: root/js/tools/BrushTool.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/BrushTool.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/BrushTool.js')
-rw-r--r--js/tools/BrushTool.js288
1 files changed, 288 insertions, 0 deletions
diff --git a/js/tools/BrushTool.js b/js/tools/BrushTool.js
new file mode 100644
index 00000000..ce8ffbb9
--- /dev/null
+++ b/js/tools/BrushTool.js
@@ -0,0 +1,288 @@
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 ShapeTool = require("js/tools/ShapeTool").ShapeTool;
8var DrawingToolBase = require("js/tools/drawing-tool-base").DrawingToolBase;
9var defaultEventManager = require("montage/core/event/event-manager").defaultEventManager;
10var Montage = require("montage/core/core").Montage;
11var NJUtils = require("js/lib/NJUtils").NJUtils;
12var ElementMediator = require("js/mediators/element-mediator").ElementMediator;
13var TagTool = require("js/tools/TagTool").TagTool;
14var snapManager = require("js/helper-classes/3D/snap-manager").SnapManager;
15
16exports.BrushTool = Montage.create(ShapeTool, {
17 hasReel: { value: false },
18 _toolID: { value: "brushTool" },
19 _imageID: { value: "brushToolImg" },
20 _toolImageClass: { value: "brushToolUp" },
21 _selectedToolImageClass: { value: "brushToolDown" },
22 _toolTipText: { value: "Brush Tool" },
23 _brushView: { value: null, writable: true },
24
25 _selectedToolClass: { value: "brushToolSpecificProperties" },
26 _penToolProperties: { enumerable: false, value: null, writable: true },
27 _parentNode: { enumerable: false, value: null, writable: true },
28 _toolsPropertiesContainer: { enumerable: false, value: null, writable: true },
29
30 //config options
31 _useWebGL: {value: false, writable: false},
32
33 //view options
34 _brushStrokeCanvas: {value: null, writable: true},
35 _brushStrokePlaneMat: {value: null, writable: true},
36
37 //the current brush stroke
38 _selectedBrushStroke: {value: null, writable: true},
39
40 ShowToolProperties: {
41 value: function () {
42 this._brushView = PenView.create();
43 this._brushView.element = document.getElementById('topPanelContainer').children[0];
44 this._brushView.needsDraw = true;
45 this._brushView.addEventListener(ToolEvents.TOOL_OPTION_CHANGE, this, false);
46 }
47 },
48
49 HandleLeftButtonDown: {
50 value: function (event) {
51 //ignore any right or middle clicks
52 if (event.button !== 0) {
53 //NOTE: this will work on Webkit only...IE has different codes (left: 1, middle: 4, right: 2)
54 return;
55 }
56 if (this._canDraw) {
57 this._isDrawing = true;
58 }
59
60 this.startDraw(event);
61 if (this._brushStrokePlaneMat === null) {
62 this._brushStrokePlaneMat = this.mouseDownHitRec.getPlaneMatrix();
63 }
64 //start a new brush stroke
65 if (this._selectedBrushStroke === null){
66 this._selectedBrushStroke = new GLBrushStroke();
67 }
68 console.log("BrushTool Start");
69 NJevent("enableStageMove");//stageManagerModule.stageManager.enableMouseMove();
70 } //value: function (event) {
71 }, //HandleLeftButtonDown
72
73
74 //need to override this function because the ShapeTool's definition contains a clearDrawingCanvas call - Pushkar
75 // might not need to override once we draw using OpenGL instead of SVG
76 // Also took out all the snapping code for now...need to add that back
77 HandleMouseMove:
78 {
79 value: function (event) {
80 //ignore any right or middle clicks
81 if (event.button !== 0) {
82 //NOTE: this will work on Webkit only...IE has different codes (left: 1, middle: 4, right: 2)
83 return;
84 }
85
86 if (this._isDrawing) {
87 snapManager.enableElementSnap(false);
88 snapManager.enableGridSnap(false);
89 snapManager.enableSnapAlign(false);
90 //this.doDraw(event);
91 //var currMousePos = this.getMouseUpPos();
92 //instead of doDraw call own DrawingTool
93 var point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas, new WebKitPoint(event.pageX, event.pageY));
94 var hitRecSnapPoint = DrawingToolBase.getUpdatedSnapPointNoAppLevelEnabling(point.x, point.y, true, this.mouseDownHitRec);
95 var currMousePos = DrawingToolBase.getHitRecPos(hitRecSnapPoint);
96
97 if (this._selectedBrushStroke){
98 this._selectedBrushStroke.addPoint(currMousePos);
99 }
100
101 this.ShowCurrentBrushStrokeOnStage();
102 } //if (this._isDrawing) {
103
104 //this.drawLastSnap(); //TODO.. is this line necessary if we're not snapping? // Required cleanup for both Draw/Feedbacks
105
106 }//value: function(event)
107 },
108
109
110
111 HandleLeftButtonUp: {
112 value: function (event) {
113 /*var drawData = this.getDrawingData();
114 if (drawData) {
115 if (this._brushStrokePlaneMat === null) {
116 this._brushStrokePlaneMat = drawData.planeMat;
117 }
118 }
119 if (this._isDrawing) {
120 this.doDraw(event);
121 }*/
122 this.endDraw(event);
123
124 this._isDrawing = false;
125 this._hasDraw = false;
126 console.log("BrushTool Stop");
127
128 //TODO get these values from the options
129 if (this._selectedBrushStroke){
130 this._selectedBrushStroke.setStrokeWidth(20);
131 }
132 //display the previously drawn stroke in a separate canvas
133 this.RenderCurrentBrushStroke();
134
135 this._selectedBrushStroke = null;
136 this._brushStrokeCanvas = null;
137 NJevent("disableStageMove");
138 }
139 },
140
141 ShowCurrentBrushStrokeOnStage:{
142 value: function() {
143 //clear the canvas before we draw anything else
144 this.application.ninja.stage.clearDrawingCanvas();
145 if (this._selectedBrushStroke && this._selectedBrushStroke.getNumPoints()>0){
146 this._selectedBrushStroke.computeMetaGeometry();
147 var ctx = this.application.ninja.stage.drawingContext;//stageManagerModule.stageManager.drawingContext;
148 if (ctx === null)
149 throw ("null drawing context in Brushtool::ShowCurrentBrushStrokeOnStage");
150 ctx.save();
151
152 var horizontalOffset = this.application.ninja.stage.userContentLeft;
153 var verticalOffset = this.application.ninja.stage.userContentTop;
154
155 var numPoints = this._selectedBrushStroke.getNumPoints();
156 ctx.lineWidth = 1;
157 ctx.strokeStyle = "black";
158 ctx.beginPath();
159 var pt = this._selectedBrushStroke.getPoint(0);
160 ctx.moveTo(pt[0]+ horizontalOffset,pt[1]+ verticalOffset);
161 for (var i = 1; i < numPoints; i++) {
162 pt = this._selectedBrushStroke.getPoint(i);
163 var x = pt[0]+ horizontalOffset;
164 var y = pt[1]+ verticalOffset;
165 ctx.lineTo(x,y);
166 }
167 ctx.stroke();
168 ctx.restore();
169
170 }
171 }
172 },
173
174 RenderCurrentBrushStroke:{
175 value: function() {
176 if (this._selectedBrushStroke){
177 this._selectedBrushStroke.computeMetaGeometry();
178 var bboxMin = this._selectedBrushStroke.getBBoxMin();
179 var bboxMax = this._selectedBrushStroke.getBBoxMax();
180 var bboxWidth = bboxMax[0] - bboxMin[0];
181 var bboxHeight = bboxMax[1] - bboxMin[1];
182 var bboxMid = Vector.create([0.5 * (bboxMax[0] + bboxMin[0]), 0.5 * (bboxMax[1] + bboxMin[1]), 0.5 * (bboxMax[2] + bboxMin[2])]);
183
184 this._selectedBrushStroke.setCanvasX(bboxMid[0]);
185 this._selectedBrushStroke.setCanvasY(bboxMid[1]);
186
187 //call render shape with the bbox width and height
188 this.RenderShape(bboxWidth, bboxHeight, this._brushStrokePlaneMat, bboxMid, this._brushStrokeCanvas);
189 }
190 }
191 },
192
193
194 RenderShape: {
195 value: function (w, h, planeMat, midPt, canvas) {
196 if ((Math.floor(w) === 0) || (Math.floor(h) === 0)) {
197 return;
198 }
199
200 var left = Math.round(midPt[0] - 0.5 * w);
201 var top = Math.round(midPt[1] - 0.5 * h);
202
203 if (!canvas) {
204 var newCanvas = NJUtils.makeNJElement("canvas", "Brushstroke", "shape", null, true);
205 var elementModel = TagTool.makeElement(w, h, planeMat, midPt, newCanvas);
206 ElementMediator.addElement(newCanvas, elementModel.data, true);
207
208 // create all the GL stuff
209 var world = this.getGLWorld(newCanvas, this._useWebGL);
210 //store a reference to this newly created canvas
211 this._brushStrokeCanvas = newCanvas;
212
213 var brushStroke = this._selectedBrushStroke;
214 if (brushStroke){
215 brushStroke.setWorld(world);
216
217 brushStroke.setPlaneMatrix(planeMat);
218 var planeMatInv = glmat4.inverse( planeMat, [] );
219 brushStroke.setPlaneMatrixInverse(planeMatInv);
220 brushStroke.setPlaneCenter(midPt);
221
222 world.addObject(brushStroke);
223 world.render();
224 //TODO this will not work if there are multiple shapes in the same canvas
225 newCanvas.elementModel.shapeModel.GLGeomObj = brushStroke;
226 }
227 } //if (!canvas) {
228 else {
229
230 var world = null;
231 if (canvas.elementModel.shapeModel && canvas.elementModel.shapeModel.GLWorld) {
232 world = canvas.elementModel.shapeModel.GLWorld;
233 } else {
234 world = this.getGLWorld(canvas, this._useWebGL);//this.options.use3D);//this.CreateGLWorld(planeMat, midPt, canvas, this._useWebGL);//fillMaterial, strokeMaterial);