| 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 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 48 |
|
| 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(shp, d, sty){ |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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(obj){ |
| 79 |
|
| 80 |
|
| 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 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 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(anchor){ |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if(!this._isBeingModified){ |
| 127 |
this.onTransformBegin(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
this.setPoints(this.points); |
| 133 |
this.render(); |
| 134 |
}, |
| 135 |
|
| 136 |
anchorConstrain: function(x, y){ |
| 137 |
|
| 138 |
|
| 139 |
if(!this.style.zAxis){ return null; } |
| 140 |
var radians = this.style.zAngle*Math.PI/180; |
| 141 |
|
| 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 |
|
| 150 |
|
| 151 |
|
| 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 |
|
| 175 |
|
| 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 |
|
| 197 |
|
| 198 |
o = o || this.data; |
| 199 |
if(o.radius || o.angle){ |
| 200 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 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(obj){ |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if(this.created || !this._downOnCanvas){ return; } |
| 248 |
this._downOnCanvas = false; |
| 249 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 287 |
|
| 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 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 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 |
|
| 319 |
if(s.style.zAxis){ s.deselect(); s.select(); } |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
}, |
| 324 |
setup: function(){ |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 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 |
|
| 365 |
dojo.forEach(this.buttons, function(b){ |
| 366 |
if(b.toolType=="vectorSecondary"){ |
| 367 |
this.zSelect(b); |
| 368 |
} |
| 369 |
},this); |
| 370 |
|
| 371 |
}else{ |
| 372 |
|
| 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 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 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 |
}); |