diff options
Diffstat (limited to 'js/helper-classes/RDGE/GLBrushStroke.js')
-rw-r--r-- | js/helper-classes/RDGE/GLBrushStroke.js | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/GLBrushStroke.js b/js/helper-classes/RDGE/GLBrushStroke.js new file mode 100644 index 00000000..89292ad8 --- /dev/null +++ b/js/helper-classes/RDGE/GLBrushStroke.js | |||
@@ -0,0 +1,242 @@ | |||
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 VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; | ||
8 | |||
9 | |||
10 | /////////////////////////////////////////////////////////////////////// | ||
11 | // Class GLBrushStroke | ||
12 | // representation a sequence points (polyline) created by brush tool. | ||
13 | // Derived from class GLGeomObj | ||
14 | /////////////////////////////////////////////////////////////////////// | ||
15 | function GLBrushStroke() { | ||
16 | /////////////////////////////////////////////////// | ||
17 | // Instance variables | ||
18 | /////////////////////////////////////////////////// | ||
19 | this._Points = []; | ||
20 | this._BBoxMin = [0, 0, 0]; | ||
21 | this._BBoxMax = [0, 0, 0]; | ||
22 | this._dirty = true; | ||
23 | |||
24 | //whether or not to use the canvas drawing to stroke/fill | ||
25 | this._useCanvasDrawing = true; | ||
26 | |||
27 | //the X and Y location of this subpath's canvas in stage world space of Ninja | ||
28 | this._canvasX = 0; | ||
29 | this._canvasY = 0; | ||
30 | |||
31 | //stroke information | ||
32 | this._strokeWidth = 0.0; | ||
33 | this._strokeColor = [0.4, 0.4, 0.4, 1.0]; | ||
34 | this._strokeMaterial; | ||
35 | this._strokeStyle = "Solid"; | ||
36 | |||
37 | //drawing context | ||
38 | this._world = null; | ||
39 | |||
40 | //tool that owns this brushstroke | ||
41 | this._drawingTool = null; | ||
42 | this._planeMat = null; | ||
43 | this._planeMatInv = null; | ||
44 | this._planeCenter = null; | ||
45 | |||
46 | // initialize the inherited members | ||
47 | this.inheritedFrom = GLGeomObj; | ||
48 | this.inheritedFrom(); | ||
49 | |||
50 | ///////////////////////////////////////////////////////// | ||
51 | // Property Accessors/Setters | ||
52 | ///////////////////////////////////////////////////////// | ||
53 | this.setWorld = function (world) { this._world = world; } | ||
54 | this.getWorld = function () { return this._world; } | ||
55 | this.geomType = function () { return this.GEOM_TYPE_CUBIC_BEZIER; } | ||
56 | this.setDrawingTool = function (tool) {this._drawingTool = tool;} | ||
57 | this.getDrawingTool = function () {return this._drawingTool;} | ||
58 | this.setPlaneMatrix = function(planeMat){this._planeMat = planeMat;} | ||
59 | this.setPlaneMatrixInverse = function(planeMatInv){this._planeMatInv = planeMatInv;} | ||
60 | this.setPlaneCenter = function(pc){this._planeCenter = pc;} | ||
61 | |||
62 | this.getCanvasX = function(){return this._canvasX;} | ||
63 | this.getCanvasY = function(){return this._canvasY;} | ||
64 | this.setCanvasX = function(cx){this._canvasX=cx;} | ||
65 | this.setCanvasY = function(cy){this._canvasY=cy;} | ||
66 | |||
67 | |||
68 | this.getNumPoints = function () { return this._Points.length; } | ||
69 | this.getPoint = function (index) { return this._Points[index]; } | ||
70 | this.addPoint = function (anchorPt) { this._Points.push(anchorPt); this._dirty=true; } | ||
71 | this.insertPoint = function(pt, index){ this._Points.splice(index, 0, pt); this._dirty=true;} | ||
72 | this.isDirty = function(){return this._dirty;} | ||
73 | this.makeDirty = function(){this._dirty=true;} | ||
74 | |||
75 | this.getBBoxMin = function () { return this._BBoxMin; } | ||
76 | this.getBBoxMax = function () { return this._BBoxMax; } | ||
77 | |||
78 | this.getStrokeWidth = function () { return this._strokeWidth; } | ||
79 | this.setStrokeWidth = function (w) { this._strokeWidth = w; this._dirty=true;} | ||
80 | this.getStrokeMaterial = function () { return this._strokeMaterial; } | ||
81 | this.setStrokeMaterial = function (m) { this._strokeMaterial = m; } | ||
82 | this.getStrokeColor = function () { return this._strokeColor; } | ||
83 | this.setStrokeColor = function (c) { this._strokeColor = c; } | ||
84 | this.getStrokeStyle = function () { return this._strokeStyle; } | ||
85 | this.setStrokeStyle = function (s) { this._strokeStyle = s; } | ||
86 | |||
87 | this.setWidth = function () { }//NO-OP for now | ||
88 | this.setHeight = function () {}//NO-OP for now | ||
89 | |||
90 | |||
91 | //remove and return anchor at specified index, return null on error | ||
92 | this.removePoint = function (index) { | ||
93 | var retAnchor = null; | ||
94 | if (index < this._Points.length) { | ||
95 | retPt = this._Points.splice(index, 1); | ||
96 | this._dirty=true; | ||
97 | } | ||
98 | return retPoint; | ||
99 | } | ||
100 | |||
101 | //remove all the points | ||
102 | this.clear = function () { this._Points = []; this._dirty=true;} | ||
103 | |||
104 | this.translate = function (tx, ty, tz) { | ||
105 | for (var i=0;i<this._Points.length;i++){ | ||
106 | this._Points[i][0]+=tx; | ||
107 | this._Points[i][1]+=ty; | ||
108 | this._Points[i][2]+=tz; | ||
109 | } | ||
110 | } | ||
111 | |||
112 | this.computeMetaGeometry = function(){ | ||
113 | if (this._dirty){ | ||
114 | // *** compute the bounding box ********* | ||
115 | this._BBoxMin = [Infinity, Infinity, Infinity]; | ||
116 | this._BBoxMax = [-Infinity, -Infinity, -Infinity]; | ||
117 | var numPoints = this._Points.length; | ||
118 | if (numPoints === 0) { | ||
119 | this._BBoxMin = [0, 0, 0]; | ||
120 | this._BBoxMax = [0, 0, 0]; | ||
121 | } else { | ||
122 | for (var i=0;i<numPoints;i++){ | ||
123 | var pt = this._Points[i]; | ||
124 | for (var d = 0; d < 3; d++) { | ||
125 | if (this._BBoxMin[d] > pt[d]) { | ||
126 | this._BBoxMin[d] = pt[d]; | ||
127 | } | ||
128 | if (this._BBoxMax[d] < pt[d]) { | ||
129 | this._BBoxMax[d] = pt[d]; | ||
130 | } | ||
131 | }//for every dimension d from 0 to 2 | ||
132 | } | ||
133 | } | ||
134 | //increase the bbox given the stroke width | ||
135 | for (var d = 0; d < 3; d++) { | ||
136 | this._BBoxMin[d]-= this._strokeWidth/2; | ||
137 | this._BBoxMax[d]+= this._strokeWidth/2; | ||
138 | }//for every dimension d from 0 to 2 | ||
139 | } | ||
140 | this._dirty = false; | ||
141 | } | ||
142 | |||
143 | //render | ||
144 | // specify how to render the subpath in Canvas2D | ||
145 | this.render = function () { | ||
146 | // get the world | ||
147 | var world = this.getWorld(); | ||
148 | if (!world) throw( "null world in brushstroke render" ); | ||
149 | |||
150 | // get the context | ||
151 | var ctx = world.get2DContext(); | ||
152 | if (!ctx) throw ("null context in brushstroke render") | ||
153 | |||
154 | var numPoints = this.getNumPoints(); | ||
155 | if (numPoints === 0) | ||
156 | return; //nothing to do for empty paths | ||
157 | |||
158 | ctx.save(); | ||
159 | |||
160 | this.computeMetaGeometry(); | ||
161 | var bboxMin = this.getBBoxMin(); | ||
162 | var bboxMax = this.getBBoxMax(); | ||
163 | var bboxWidth = bboxMax[0] - bboxMin[0]; | ||
164 | var bboxHeight = bboxMax[1] - bboxMin[1]; | ||
165 | ctx.clearRect(0, 0, bboxWidth, bboxHeight); | ||
166 | /* | ||
167 | ctx.lineWidth = this._strokeWidth; | ||
168 | ctx.strokeStyle = "black"; | ||
169 | if (this._strokeColor) | ||
170 | ctx.strokeStyle = MathUtils.colorToHex( this._strokeColor ); | ||
171 | ctx.fillStyle = "blue"; | ||
172 | if (this._fillColor) | ||
173 | ctx.fillStyle = MathUtils.colorToHex( this._fillColor ); | ||
174 | var lineCap = ['butt','round','square']; | ||
175 | ctx.lineCap = lineCap[1]; | ||
176 | ctx.beginPath(); | ||
177 | var firstPoint = this._Points[0]; | ||
178 | ctx.moveTo(firstPoint[0]-bboxMin[0], firstPoint[1]-bboxMin[1]); | ||
179 | for (var i = 1; i < numPoints; i++) { | ||
180 | var pt = this._Points[i]; | ||
181 | ctx.lineTo(pt[0]-bboxMin[0], pt[1]-bboxMin[1]); | ||
182 | } | ||
183 | ctx.stroke(); | ||
184 | */ | ||
185 | var R2 = this._strokeWidth; | ||
186 | var R = R2*0.5; | ||
187 | var hardness = 0.25; //for a pencil, this is always 1 //TODO get hardness parameter from user interface | ||
188 | var innerRadius = (hardness*R)-1; | ||
189 | if (innerRadius<1) | ||
190 | innerRadius=1; | ||
191 | |||
192 | for (var i = 0; i < numPoints; i++) { | ||
193 | var pt = this._Points[i]; | ||
194 | ctx.globalCompositeOperation = 'source-over'; | ||
195 | var x = pt[0]-bboxMin[0]; | ||
196 | var y = pt[1]-bboxMin[1]; | ||
197 | var r = ctx.createRadialGradient(x, y, innerRadius, x, y, R); | ||
198 | r.addColorStop(0, 'rgba(255,0,0,0.5)'); | ||
199 | //r.addColorStop(0.5, 'rgba(255,0,0,0.5)'); // prevent aggregation of semi-opaque pixels | ||
200 | r.addColorStop(1, 'rgba(255,0,0,0.0)'); | ||
201 | ctx.fillStyle = r; | ||
202 | //ctx.fillRect(x-R, y-R, R2, R2); | ||
203 | ctx.arc(x, y, R, 0, 2 * Math.PI, false); | ||
204 | ctx.fill(); | ||
205 | //ctx.globalCompositeOperation = 'source-in'; | ||
206 | //ctx.rect(x-R, y-R, R2, R2); | ||
207 | } | ||
208 | ctx.restore(); | ||
209 | } //render() | ||
210 | |||
211 | |||
212 | this.export = function() | ||
213 | { | ||
214 | var rtnStr = "type: " + this.geomType() + "\n"; | ||
215 | return rtnStr; | ||
216 | } | ||
217 | |||
218 | this.import = function( importStr ) | ||
219 | { | ||
220 | } | ||
221 | |||
222 | this.collidesWithPoint = function (x, y, z) { | ||
223 | if (x < this._BBoxMin[0]) return false; | ||
224 | if (x > this._BBoxMax[0]) return false; | ||
225 | if (y < this._BBoxMin[1]) return false; | ||
226 | if (y > this._BBoxMax[1]) return false; | ||
227 | if (z < this._BBoxMin[2]) return false; | ||
228 | if (z > this._BBoxMax[2]) return false; | ||
229 | |||
230 |