Debian Codegraph @

dojox/drawing/tools/custom/Vector.js

raw symbols SCIP
1 define(["dojo", "../../util/oo", "../../manager/_registry", "../Arrow", "../../defaults"],
2 function(dojo, oo, registry, Arrow, defaults){
3
4 var Vector = oo.declare(
5 Arrow,
6 function(options){
7 this.minimumSize = this.style.arrows.length;
8 this.addShadow({size:3, mult:2});
9 },
10 {
11 // summary:
12 // Creates a Vector Stencil.
13 // description:
14 // Generally the same as an arrow, except that the arrow
15 // head is only at the end. There is additionaly functionality
16 // to allow for a 'zero vector' - one with no length.
17
18 draws:true,
19 type:"dojox.drawing.tools.custom.Vector",
20 minimumSize:30,
21 showAngle:true,
22
23
24
25 changeAxis: function(cosphi){
26 // summary:
27 // Converts a vector to and from the z axis.
28 // If passed a cosphi value that is used to set
29 // the axis, otherwise it is the opp of what it is.
30 cosphi = cosphi!==undefined?cosphi:this.style.zAxis? 0 : 1;
31 if(cosphi == 0){
32 this.style.zAxis = false;
33 this.data.cosphi = 0;
34 }else{
35 this.style.zAxis = true;
36 var p = this.points;
37 var pt = this.zPoint();
38 this.setPoints([
39 {x:p[0].x, y:p[0].y},
40 {x:pt.x, y:pt.y}
41 ]);
42 }
43 this.render();
44 },
45
46 _createZeroVector: function(shp, d, sty){
47 // summary:
48 // Special creation function for the zero-vector shape
49
50 var s = shp=="hit" ? this.minimumSize : this.minimumSize/6;
51 var f = shp=="hit" ? sty.fill : null;
52 d = {
53 cx:this.data.x1,
54 cy:this.data.y1,
55 rx:s,
56 ry:s
57 };
58
59 this.remove(this[shp]);
60 this[shp] = this.container.createEllipse(d)
61 .setStroke(sty)
62 .setFill(f);
63 this.util.attr(this[shp], "drawingType", "stencil");
64 },
65
66 _create: function(/*String*/shp, /*StencilData*/d, /*Object*/sty){
67 // summary:
68 // Creates a dojox.gfx.shape based on passed arguments.
69 // Can be called many times by implementation to create
70 // multiple shapes in one stencil.
71
72 this.remove(this[shp]);
73 this[shp] = this.container.createLine(d)
74 .setStroke(sty);
75 this._setNodeAtts(this[shp]);
76 },
77
78 onDrag: function(/*EventObject*/obj){
79 // summary:
80 // See stencil._Base.onDrag
81
82 if(this.created){ return; }
83
84 var x1 = obj.start.x,
85 y1 = obj.start.y,
86 x2 = obj.x,
87 y2 = obj.y;
88
89 if(this.keys.shift && !this.style.zAxis){
90 var pt = this.util.snapAngle(obj, 45/180);
91 x2 = pt.x;
92 y2 = pt.y;
93 }
94
95 if(this.keys.alt){
96 // FIXME:
97 // should double the length of the line
98 // FIXME:
99 // if alt dragging past ZERO it seems to work
100 // but select/deselect shows bugs
101 var dx = x2>x1 ? ((x2-x1)/2) : ((x1-x2)/-2);
102 var dy = y2>y1 ? ((y2-y1)/2) : ((y1-y2)/-2);
103 x1 -= dx;
104 x2 -= dx;
105 y1 -= dy;
106 y2 -= dy;
107 }
108
109 if(this.style.zAxis){
110 var pts = this.zPoint(obj);
111 x2 = pts.x;
112 y2 = pts.y;
113 }
114
115 this.setPoints([
116 {x:x1, y:y1},
117 {x:x2, y:y2}
118 ]);
119 this.render();
120 },
121
122 onTransform: function(/* ? manager.Anchor */anchor){
123 // summary:
124 // Called from anchor point mouse drag
125 // also called from plugins.Pan.checkBounds
126 if(!this._isBeingModified){
127 this.onTransformBegin();
128 }
129 // this is not needed for anchor moves, but it
130 // is for stencil move:
131
132 this.setPoints(this.points);
133 this.render();
134 },
135
136 anchorConstrain: function(x, y){
137 // summary:
138 // Called from anchor point mouse drag
139 if(!this.style.zAxis){ return null; }
140 var radians = this.style.zAngle*Math.PI/180;
141 //Constrain to angle
142 var test = x<0 ? x>-y : x<-y;
143 var dx = test ? x : -y/Math.tan(radians);
144 var dy = !test ? y : -Math.tan(radians)*x;
145 return {x:dx, y:dy};
146 },
147
148 zPoint: function(obj){
149 // summary:
150 // Takes any point and converts it to
151 // be on the z-axis.
152 if(obj===undefined){
153 if(!this.points[0]){ return null; };
154 var d = this.pointsToData();
155 obj = {
156 start:{
157 x:d.x1,
158 y:d.y1
159 },
160 x:d.x2,
161 y:d.y2
162 };
163 }
164 var radius = this.util.length(obj);
165 var angle = this.util.angle(obj);
166 angle = angle<0 ? 360 + angle : angle;
167
168 angle = angle > 135 && angle < 315 ? this.style.zAngle : this.util.oppAngle(this.style.zAngle);
169
170 return this.util.pointOnCircle(obj.start.x, obj.start.y, radius, angle);
171 },
172
173 pointsToData: function(p){
174 // summary:
175 // Converts points to data
176 p = p || this.points;
177 var cosphi = 0;
178 var obj = {start:{x:p[0].x, y:p[0].y}, x:p[1].x, y:p[1].y};
179 if(this.style.zAxis && (this.util.length(obj)>this.minimumSize)){
180
181 var angle = this.util.angle(obj);
182 angle = angle<0 ? 360 + angle : angle;
183 cosphi = angle > 135 && angle < 315 ? 1 : -1;
184 }
185 this.data = {
186 x1: p[0].x,
187 y1: p[0].y,
188 x2: p[1].x,
189 y2: p[1].y,
190 cosphi: cosphi
191 };
192 return this.data;
193 },
194
195 dataToPoints: function(o){
196 // summary:
197 // Converts data to points.
198 o = o || this.data;
199 if(o.radius || o.angle){
200 // instead of using x1,x2,y1,y1,
201 // it's been set as x,y,angle,radius
202 var cosphi = 0;
203 var pt = this.util.pointOnCircle(o.x,o.y,o.radius,o.angle);
204 if(this.style.zAxis || (o.cosphi && o.cosphi!=0)){
205 this.style.zAxis = true;
206 cosphi = o.angle > 135 && o.angle < 315 ? 1 : -1;
207 }
208 //console.log(" ---- pts:", pt.x, pt.y);
209 this.data = o = {
210 x1:o.x,
211 y1:o.y,
212 x2:pt.x,
213 y2:pt.y,
214 cosphi:cosphi
215 };
216
217 }
218 this.points = [
219 {x:o.x1, y:o.y1},
220 {x:o.x2, y:o.y2}
221 ];
222 return this.points;
223 },
224
225 render: function(){
226 // summary:
227 // Renders the 'hit' object (the shape used for an expanded
228 // hit area and for highlighting) and the'shape' (the actual
229 // display object). Additionally checks if Vector should be
230 // drawn as an arrow or a circle (zero-length)
231
232 this.onBeforeRender(this);
233 if(this.getRadius() >= this.minimumSize){
234 this._create("hit", this.data, this.style.currentHit);
235 this._create("shape", this.data, this.style.current);
236
237 }else{
238 this.data.cosphi = 0;
239 this._createZeroVector("hit", this.data, this.style.currentHit);
240 this._createZeroVector("shape", this.data, this.style.current);
241 }
242 },
243 onUp: function(/*EventObject*/obj){
244 // summary:
245 // See stencil._Base.onUp
246
247 if(this.created || !this._downOnCanvas){ return; }
248 this._downOnCanvas = false;
249 //Default vector for single click
250 if(!this.shape){
251 var d = 100;
252 obj.start.x = this.style.zAxis ? obj.start.x + d : obj.start.x;
253 obj.y = obj.y+d;
254 this.setPoints([
255 {x:obj.start.x, y:obj.start.y},
256 {x:obj.x, y:obj.y}
257 ]);
258 this.render();
259 }
260
261 // When within minimum size this sets zero vector length to zero
262 if(this.getRadius()<this.minimumSize){
263 var p = this.points;
264 this.setPoints([
265 {x:p[0].x, y:p[0].y},
266 {x:p[0].x, y:p[0].y}
267 ]);
268 }else{
269 //SnapAngle fails for the zero length vector
270 var p = this.points;
271 var pt = this.style.zAxis ? this.zPoint(obj) : this.util.snapAngle(obj, this.angleSnap/180);
272 this.setPoints([
273 {x:p[0].x, y:p[0].y},
274 {x:pt.x, y:pt.y}
275 ]);
276 }
277 this.renderedOnce = true;
278 this.onRender(this);
279 }
280 }
281
282 );
283
284 dojo.setObject("dojox.drawing.tools.custom.Vector", Vector);
285 Vector.setup = {
286 // summary:
287 // See stencil._Base ToolsSetup
288
289 name:"dojox.drawing.tools.custom.Vector",
290 tooltip:"Vector Tool",
291 iconClass:"iconVector"
292 };
293
294 if(defaults.zAxisEnabled){
295 Vector.setup.secondary = {
296 // summary:
297 // Creates a secondary tool for the Vector Stencil.
298 // description:
299 // See Toolbar.js makeButtons function. The toolbar
300 // checks Vector.setup for a secondary tool and requires
301 // name, label, and funct. Currently it doesn't accept icon
302 // and only uses text from label for the button. Funct is the
303 // function that fires when the button is clicked.
304 //
305 // Setup and postSetup are optional
306 // and allow tool specific functions to be added to the
307 // Toolbar object as if they were written there.
308 name: "vectorSecondary",
309 label: "z-axis",
310 funct: function(button){
311 button.selected ? this.zDeselect(button) : this.zSelect(button);
312
313 var stencils = this.drawing.stencils.selectedStencils;
314 for(var nm in stencils){
315 if(stencils[nm].shortType == "vector" && (stencils[nm].style.zAxis != defaults.zAxis)){
316 var s = stencils[nm];
317 s.changeAxis();
318 //Reset anchors
319 if(s.style.zAxis){ s.deselect(); s.select(); }
320 }
321 }
322
323 },
324 setup: function(){
325 // summary:
326 // All functions, variables and connections defined here
327 // are treated as if they were added directly to toolbar.
328 // They are included with the tool because secondary buttons
329 // are tool specific.
330 var zAxis = defaults.zAxis;
331 this.zSelect = function(button){
332 if(!button.enabled){ return; }
333 zAxis = true;
334 defaults.zAxis = true;
335 button.select();
336 this.vectorTest();
337 this.zSelected = button;
338 };
339 this.zDeselect = function(button){
340 if(!button.enabled){ return; }
341 zAxis = false;
342 defaults.zAxis = false;
343 button.deselect();
344 this.vectorTest();
345 this.zSelected = null;
346 };
347 this.vectorTest = function(){
348 dojo.forEach(this.buttons, function(b){
349 if(b.toolType=="vector" && b.selected){
350 this.drawing.currentStencil.style.zAxis = zAxis;
351 }
352 },this);
353 };
354 dojo.connect(this, "onRenderStencil", this, function(){
355 if(this.zSelected){
356 this.zDeselect(this.zSelected);
357 }
358 });
359 var c = dojo.connect(this.drawing, "onSurfaceReady", this, function(){
360 dojo.disconnect(c);
361 dojo.connect(this.drawing.stencils, "onSelect", this, function(stencil){
362 if(stencil.shortType == "vector"){
363 if(stencil.style.zAxis){
364 //If stencil is on the z-axis, update button to reflect that
365 dojo.forEach(this.buttons, function(b){
366 if(b.toolType=="vectorSecondary"){
367 this.zSelect(b);
368 }
369 },this);
370
371 }else{
372 //Update button to not be z-axis
373 dojo.forEach(this.buttons, function(b){
374 if(b.toolType=="vectorSecondary"){
375 this.zDeselect(b);
376 }
377 },this);
378 }
379 };
380 });
381 });
382 },
383 postSetup: function(btn){
384 // summary:
385 // Depending on the secondary tool, it may need
386 // extra functionality for some of the basic functions.
387 // Post is passed the button so those connections can
388 // be made.
389 dojo.connect(btn, "enable", function(){ defaults.zAxisEnabled = true; });
390 dojo.connect(btn, "disable", function(){ defaults.zAxisEnabled = false; });
391 }
392 };
393 }
394 registry.register(Vector.setup, "tool");
395 return Vector;
396 });