aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/elements/shapes-controller.js
diff options
context:
space:
mode:
authorNivesh Rajbhandari2012-04-16 16:06:24 -0700
committerNivesh Rajbhandari2012-04-16 16:06:24 -0700
commitc253192a08b499ea7be46fa5438d273e51f7ec5a (patch)
tree18a1f0e3679c0eb993a9dedb537035d3861f49ac /js/controllers/elements/shapes-controller.js
parente19376c54eedd1f1c457ba405b2f110be376a559 (diff)
parent4b900ea5cd6bb77eb30cec8c03b9ec9fa662c1e9 (diff)
downloadninja-c253192a08b499ea7be46fa5438d273e51f7ec5a.tar.gz
Merge branch 'refs/heads/ninja-internal' into WebGLFixes
Diffstat (limited to 'js/controllers/elements/shapes-controller.js')
-rwxr-xr-xjs/controllers/elements/shapes-controller.js502
1 files changed, 366 insertions, 136 deletions
diff --git a/js/controllers/elements/shapes-controller.js b/js/controllers/elements/shapes-controller.js
index d72d9c14..14cdc473 100755
--- a/js/controllers/elements/shapes-controller.js
+++ b/js/controllers/elements/shapes-controller.js
@@ -6,20 +6,81 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
6 6
7var Montage = require("montage/core/core").Montage, 7var Montage = require("montage/core/core").Montage,
8 CanvasController = require("js/controllers/elements/canvas-controller").CanvasController, 8 CanvasController = require("js/controllers/elements/canvas-controller").CanvasController,
9 njModule = require("js/lib/NJUtils"); 9 njModule = require("js/lib/NJUtils"),
10 10 World = require("js/lib/drawing/world").World,
11var World = require("js/lib/drawing/world").World; 11 MaterialsModel = require("js/models/materials-model").MaterialsModel;
12var MaterialsModel = require("js/models/materials-model").MaterialsModel;
13 12
14exports.ShapesController = Montage.create(CanvasController, { 13exports.ShapesController = Montage.create(CanvasController, {
15 14
16 setProperty: { 15 setProperty: {
17 value: function(el, p, value) { 16 value: function(el, p, value, eventType, source) {
18 var val = parseInt(value); 17 var val = parseInt(value),
18 canvas,
19 m,
20 color;
19 switch(p) { 21 switch(p) {
20 case "strokeSize": 22 case "strokeSize":
21 // TODO - For now, just handling px units. 23 // TODO - For now, just handling px units.
22 this.setShapeProperty(el, "strokeSize", value); 24 this.setShapeProperty(el, "strokeSize", value);
25 // TODO - For now, just handle Line, Rectangle and Oval. Eventually, move this into each class's
26 // setStrokeWidth code like SubPath and BrushStroke do.
27 var geomType = el.elementModel.shapeModel.GLGeomObj.geomType();
28 if( (geomType > 0) && (geomType < 4) )
29 {
30 // Changing stroke size should grow/shrink the shape from the center.
31 var delta = ~~(val - el.elementModel.shapeModel.GLGeomObj.getStrokeWidth()),
32 l = this.application.ninja.elementMediator.getProperty(el, "left", parseInt),
33 t = this.application.ninja.elementMediator.getProperty(el, "top", parseInt),
34 w = this.application.ninja.elementMediator.getProperty(el, "width", parseInt),
35 h = this.application.ninja.elementMediator.getProperty(el, "height", parseInt);
36
37 if(geomType === 3)
38 {
39 var slope = el.elementModel.shapeModel.slope;
40 // set the dimensions
41 if(slope === "horizontal")
42 {
43 h = Math.max(val, 1);
44 t -= ~~(delta/2);
45 }
46 else if(slope === "vertical")
47 {
48 w = Math.max(val, 1);
49 l -= ~~(delta/2);
50 }
51 else
52 {
53 var oldXAdj = el.elementModel.shapeModel.GLGeomObj.getXAdj();
54 var oldYAdj = el.elementModel.shapeModel.GLGeomObj.getYAdj();
55 var theta = Math.atan(el.elementModel.shapeModel.slope);
56 var xAdj = Math.abs((val/2)*Math.sin(theta));
57 var yAdj = Math.abs((val/2)*Math.cos(theta));
58 var dX = ~~(xAdj*2 - oldXAdj*2);
59 var dY = ~~(yAdj*2 - oldYAdj*2);
60
61 l -= dX;
62 t -= dY;
63 w += dX*2;
64 h += dY*2;
65
66 el.elementModel.shapeModel.GLGeomObj.setXAdj(xAdj);
67 el.elementModel.shapeModel.GLGeomObj.setYAdj(yAdj);
68 }
69 }
70 else
71 {
72 l -= ~~(delta/2);
73 t -= ~~(delta/2);
74 w += delta;
75 h += delta;
76 }
77 this.application.ninja.elementMediator.setProperties([el],
78 { "left": [l + "px"],
79 "top": [t + "px"],
80 "width": [w + "px"],
81 "height": [h + "px"] },
82 eventType, source );
83 }
23 el.elementModel.shapeModel.GLGeomObj.setStrokeWidth(val); 84 el.elementModel.shapeModel.GLGeomObj.setStrokeWidth(val);
24 el.elementModel.shapeModel.GLGeomObj.buildBuffers(); 85 el.elementModel.shapeModel.GLGeomObj.buildBuffers();
25 el.elementModel.shapeModel.GLWorld.render(); 86 el.elementModel.shapeModel.GLWorld.render();
@@ -69,40 +130,91 @@ exports.ShapesController = Montage.create(CanvasController, {
69 el.elementModel.shapeModel.GLWorld.render(); 130 el.elementModel.shapeModel.GLWorld.render();
70 break; 131 break;
71 case "useWebGl": 132 case "useWebGl":
72 var canvas = njModule.NJUtils.makeNJElement("canvas", "Canvas", "shape", el.className, true); 133 canvas = njModule.NJUtils.makeNJElement("canvas", "Canvas", "shape", el.className, true);
73 canvas.setAttribute("data-RDGE-id", njModule.NJUtils.generateRandom()); 134 canvas.setAttribute("data-RDGE-id", njModule.NJUtils.generateRandom());
74 canvas.width = el.width; 135 canvas.width = el.width;
75 canvas.height = el.height; 136 canvas.height = el.height;
76 this.application.ninja.elementMediator.replaceElement(el, canvas); 137 canvas.elementModel = el.elementModel;
77 NJevent("elementDeleted", el); 138 this.toggleWebGlMode(canvas, value);
78 el = canvas; 139 this.application.ninja.elementMediator.replaceElement(canvas, el);
79 this.toggleWebGlMode(el, value); 140 break;
80 el.elementModel.shapeModel.GLWorld.render();
81 this.application.ninja.selectionController.selectElement(el);
82 return;
83 case "strokeMaterial": 141 case "strokeMaterial":
84 var sm = Object.create(MaterialsModel.getMaterial(value)); 142 m = Object.create(MaterialsModel.getMaterial(value));
85 if(sm) 143 if(m)
86 { 144 {
87 el.elementModel.shapeModel.GLGeomObj.setStrokeMaterial(sm); 145 el.elementModel.shapeModel.GLGeomObj.setStrokeMaterial(m);
88 el.elementModel.shapeModel.strokeMaterial = sm; 146 color = this.getMaterialColor(value);
147 if(color)
148 {
149 el.elementModel.shapeModel.GLGeomObj.setStrokeColor(color);
150 }
89 el.elementModel.shapeModel.GLGeomObj.buildBuffers(); 151 el.elementModel.shapeModel.GLGeomObj.buildBuffers();
90 el.elementModel.shapeModel.GLWorld.render(); 152 el.elementModel.shapeModel.GLWorld.render();
91 } 153 }
92 break; 154 break;
93 case "fillMaterial": 155 case "fillMaterial":
94 var fm = Object.create(MaterialsModel.getMaterial(value)); 156 m = Object.create(MaterialsModel.getMaterial(value));
95 if(fm) 157 if(m)
96 { 158 {
97 el.elementModel.shapeModel.GLGeomObj.setFillMaterial(fm); 159 el.elementModel.shapeModel.GLGeomObj.setFillMaterial(m);
98 el.elementModel.shapeModel.fillMaterial = fm; 160 color = this.getMaterialColor(value);
161 if(color)
162 {
163 el.elementModel.shapeModel.GLGeomObj.setFillColor(color);
164 }
99 el.elementModel.shapeModel.GLGeomObj.buildBuffers(); 165 el.elementModel.shapeModel.GLGeomObj.buildBuffers();
100 el.elementModel.shapeModel.GLWorld.render(); 166 el.elementModel.shapeModel.GLWorld.render();
101 } 167 }
102 break; 168 break;
169 case "editStrokeMaterial":
170 NJevent("showMaterialPopup",{materialId : this.getProperty(el, "strokeMaterial")});
171 break;
172 case "editFillMaterial":
173 NJevent("showMaterialPopup",{materialId : this.getProperty(el, "fillMaterial")});
174 break;
175 case "animate":
176 if(value)
177 {
178 el.elementModel.shapeModel.animate = true;
179 el.elementModel.shapeModel.GLWorld._previewAnimation = true;
180 el.elementModel.shapeModel.GLWorld.restartRenderLoop();
181 }
182 else
183 {
184 el.elementModel.shapeModel.animate = false;
185 el.elementModel.shapeModel.GLWorld._previewAnimation = false;
186 el.elementModel.shapeModel.GLWorld._canvas.task.stop();
187 }
188 break;
189 case "strokeHardness":
190 this.setShapeProperty(el, "strokeHardness", value);
191 el.elementModel.shapeModel.GLGeomObj.setStrokeHardness(val);
192 el.elementModel.shapeModel.GLWorld.render();
193 break;
194 case "strokeSmoothing":
195 this.setShapeProperty(el, "strokeSmoothing", value);
196 el.elementModel.shapeModel.GLGeomObj.setSmoothingAmount(val);
197 el.elementModel.shapeModel.GLWorld.render();
198 break;
199 case "doSmoothing":
200 this.setShapeProperty(el, "doSmoothing", value);
201 el.elementModel.shapeModel.GLGeomObj.setDoSmoothing(value);
202 el.elementModel.shapeModel.GLWorld.render();
203 break;
204 case "isCalligraphic":
205 this.setShapeProperty(el, "isCalligraphic", value);
206 el.elementModel.shapeModel.GLGeomObj.setStrokeUseCalligraphic(value);
207 el.elementModel.shapeModel.GLWorld.render();
208 break;
209 case "strokeAngle":
210 this.setShapeProperty(el, "strokeAngle", value);
211 el.elementModel.shapeModel.GLGeomObj.setStrokeAngle(Math.PI * val/180);
212 el.elementModel.shapeModel.GLWorld.render();
213 break;
103 default: 214 default:
104 CanvasController.setProperty(el, p, value); 215