| 1 | define(["./_base", "dojo/_base/lang", "dojo/_base/declare", "dojo/_base/kernel", "dojo/_base/sniff", |
| 2 | "dojo/on", "dojo/_base/array", "dojo/dom-construct", "dojo/_base/Color", "./matrix" ], |
| 3 | function(g, lang, declare, kernel, has, on, arr, domConstruct, Color, matrixLib){ |
| 4 | |
| 5 | function removeItemAt(a, index) { |
| 6 | var len = (a.length - 1); |
| 7 | while (index < len) { |
| 8 | a[index] = a[++index]; |
| 9 | } |
| 10 | a.length = len; |
| 11 | } |
| 12 | |
| 13 | var shape = g.shape = { |
| 14 | // summary: |
| 15 | // This module contains the core graphics Shape API. |
| 16 | // Different graphics renderer implementation modules (svg, canvas, vml, silverlight, etc.) extend this |
| 17 | // basic api to provide renderer-specific implementations for each shape. |
| 18 | }; |
| 19 | |
| 20 | shape.Shape = declare("dojox.gfx.shape.Shape", null, { |
| 21 | // summary: |
| 22 | // a Shape object, which knows how to apply |
| 23 | // graphical attributes and transformations |
| 24 | |
| 25 | constructor: function(){ |
| 26 | // rawNode: Node |
| 27 | // underlying graphics-renderer-specific implementation object (if applicable) |
| 28 | this.rawNode = null; |
| 29 | |
| 30 | // shape: Object |
| 31 | // an abstract shape object |
| 32 | // (see dojox/gfx.defaultPath, |
| 33 | // dojox/gfx.defaultPolyline, |
| 34 | // dojox/gfx.defaultRect, |
| 35 | // dojox/gfx.defaultEllipse, |
| 36 | // dojox/gfx.defaultCircle, |
| 37 | // dojox/gfx.defaultLine, |
| 38 | // or dojox/gfx.defaultImage) |
| 39 | this.shape = null; |
| 40 | |
| 41 | // matrix: dojox/gfx/matrix.Matrix2D |
| 42 | // a transformation matrix |
| 43 | this.matrix = null; |
| 44 | |
| 45 | // fillStyle: dojox/gfx.Fill |
| 46 | // a fill object |
| 47 | // (see dojox/gfx.defaultLinearGradient, |
| 48 | // dojox/gfx.defaultRadialGradient, |
| 49 | // dojox/gfx.defaultPattern, |
| 50 | // or dojo/Color) |
| 51 | this.fillStyle = null; |
| 52 | |
| 53 | // strokeStyle: dojox/gfx.Stroke |
| 54 | // a stroke object |
| 55 | // (see dojox/gfx.defaultStroke) |
| 56 | this.strokeStyle = null; |
| 57 | |
| 58 | // bbox: dojox/gfx.Rectangle |
| 59 | // a bounding box of this shape |
| 60 | // (see dojox/gfx.defaultRect) |
| 61 | this.bbox = null; |
| 62 | |
| 63 | // virtual group structure |
| 64 | |
| 65 | // parent: Object |
| 66 | // a parent or null |
| 67 | // (see dojox/gfx/shape.Surface, |
| 68 | // or dojox/gfx.Group) |
| 69 | this.parent = null; |
| 70 | |
| 71 | // parentMatrix: dojox/gfx/matrix.Matrix2D |
| 72 | // a transformation matrix inherited from the parent |
| 73 | this.parentMatrix = null; |
| 74 | |
| 75 | if(has("gfxRegistry")){ |
| 76 | var uid = shape.register(this); |
| 77 | this.getUID = function(){ |
| 78 | return uid; |
| 79 | } |
| 80 | } |
| 81 | }, |
| 82 | |
| 83 | destroy: function(){ |
| 84 | // summary: |
| 85 | // Releases all internal resources owned by this shape. Once this method has been called, |
| 86 | // the instance is considered destroyed and should not be used anymore. |
| 87 | if(has("gfxRegistry")){ |
| 88 | shape.dispose(this); |
| 89 | } |
| 90 | if(this.rawNode && "__gfxObject__" in this.rawNode){ |
| 91 | this.rawNode.__gfxObject__ = null; |
| 92 | } |
| 93 | this.rawNode = null; |
| 94 | }, |
| 95 | |
| 96 | // trivial getters |
| 97 | |
| 98 | getNode: function(){ |
| 99 | // summary: |
| 100 | // Different graphics rendering subsystems implement shapes in different ways. This |
| 101 | // method provides access to the underlying graphics subsystem object. Clients calling this |
| 102 | // method and using the return value must be careful not to try sharing or using the underlying node |
| 103 | // in a general way across renderer implementation. |
| 104 | // Returns the underlying graphics Node, or null if no underlying graphics node is used by this shape. |
| 105 | return this.rawNode; // Node |
| 106 | }, |
| 107 | getShape: function(){ |
| 108 | // summary: |
| 109 | // returns the current Shape object or null |
| 110 | // (see dojox/gfx.defaultPath, |
| 111 | // dojox/gfx.defaultPolyline, |
| 112 | // dojox/gfx.defaultRect, |
| 113 | // dojox/gfx.defaultEllipse, |
| 114 | // dojox/gfx.defaultCircle, |
| 115 | // dojox/gfx.defaultLine, |
| 116 | // or dojox/gfx.defaultImage) |
| 117 | return this.shape; // Object |
| 118 | }, |
| 119 | getTransform: function(){ |
| 120 | // summary: |
| 121 | // Returns the current transformation matrix applied to this Shape or null |
| 122 | return this.matrix; // dojox/gfx/matrix.Matrix2D |
| 123 | }, |
| 124 | getFill: function(){ |
| 125 | // summary: |
| 126 | // Returns the current fill object or null |
| 127 | // (see dojox/gfx.defaultLinearGradient, |
| 128 | // dojox/gfx.defaultRadialGradient, |
| 129 | // dojox/gfx.defaultPattern, |
| 130 | // or dojo/Color) |
| 131 | return this.fillStyle; // Object |
| 132 | }, |
| 133 | getStroke: function(){ |
| 134 | // summary: |
| 135 | // Returns the current stroke object or null |
| 136 | // (see dojox/gfx.defaultStroke) |
| 137 | return this.strokeStyle; // Object |
| 138 | }, |
| 139 | getParent: function(){ |
| 140 | // summary: |
| 141 | // Returns the parent Shape, Group or null if this Shape is unparented. |
| 142 | // (see dojox/gfx/shape.Surface, |
| 143 | // or dojox/gfx.Group) |
| 144 | return this.parent; // Object |
| 145 | }, |
| 146 | getBoundingBox: function(){ |
| 147 | // summary: |
| 148 | // Returns the bounding box Rectangle for this shape or null if a BoundingBox cannot be |
| 149 | // calculated for the shape on the current renderer or for shapes with no geometric area (points). |
| 150 | // A bounding box is a rectangular geometric region |
| 151 | // defining the X and Y extent of the shape. |
| 152 | // (see dojox/gfx.defaultRect) |
| 153 | // Note that this method returns a direct reference to the attribute of this instance. Therefore you should |
| 154 | // not modify its value directly but clone it instead. |
| 155 | return this.bbox; // dojox/gfx.Rectangle |
| 156 | }, |
| 157 | getTransformedBoundingBox: function(){ |
| 158 | // summary: |
| 159 | // returns an array of four points or null |
| 160 | // four points represent four corners of the untransformed bounding box |
| 161 | var b = this.getBoundingBox(); |
| 162 | if(!b){ |
| 163 | return null; // null |
| 164 | } |
| 165 | var m = this._getRealMatrix(), |
| 166 | gm = matrixLib; |
| 167 | return [ // Array |
| 168 | gm.multiplyPoint(m, b.x, b.y), |
| 169 | gm.multiplyPoint(m, b.x + b.width, b.y), |
| 170 | gm.multiplyPoint(m, b.x + b.width, b.y + b.height), |
| 171 | gm.multiplyPoint(m, b.x, b.y + b.height) |
| 172 | ]; |
| 173 | }, |
| 174 | getEventSource: function(){ |
| 175 | // summary: |
| 176 | // returns a Node, which is used as |
| 177 | // a source of events for this shape |
| 178 | |
| 179 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 180 | return this.rawNode; // Node |
| 181 | }, |
| 182 | |
| 183 | // empty settings |
| 184 | |
| 185 | setClip: function(clip){ |
| 186 | // summary: |
| 187 | // sets the clipping area of this shape. |
| 188 | // description: |
| 189 | // The clipping area defines the shape area that will be effectively visible. Everything that |
| 190 | // would be drawn outside of the clipping area will not be rendered. |
| 191 | // The possible clipping area types are rectangle, ellipse, polyline and path, but all are not |
| 192 | // supported by all the renderers. vml only supports rectangle clipping, while the gfx silverlight renderer does not |
| 193 | // support path clipping. |
| 194 | // The clip parameter defines the clipping area geometry, and should be an object with the following properties: |
| 195 | // |
| 196 | // - {x:Number, y:Number, width:Number, height:Number} for rectangular clip |
| 197 | // - {cx:Number, cy:Number, rx:Number, ry:Number} for ellipse clip |
| 198 | // - {points:Array} for polyline clip |
| 199 | // - {d:String} for a path clip. |
| 200 | // |
| 201 | // The clip geometry coordinates are expressed in the coordinate system used to draw the shape. In other |
| 202 | // words, the clipping area is defined in the shape parent coordinate system and the shape transform is automatically applied. |
| 203 | // example: |
| 204 | // The following example shows how to clip a gfx image with all the possible clip geometry: a rectangle, |
| 205 | // an ellipse, a circle (using the ellipse geometry), a polyline and a path: |
| 206 | // |
| 207 | // | surface.createImage({src:img, width:200,height:200}).setClip({x:10,y:10,width:50,height:50}); |
| 208 | // | surface.createImage({src:img, x:100,y:50,width:200,height:200}).setClip({cx:200,cy:100,rx:20,ry:30}); |
| 209 | // | surface.createImage({src:img, x:0,y:350,width:200,height:200}).setClip({cx:100,cy:425,rx:60,ry:60}); |
| 210 | // | surface.createImage({src:img, x:300,y:0,width:200,height:200}).setClip({points:[350,0,450,50,380,130,300,110]}); |
| 211 | // | surface.createImage({src:img, x:300,y:350,width:200,height:200}).setClip({d:"M 350,350 C314,414 317,557 373,450.0000 z"}); |
| 212 | |
| 213 | // clip: Object |
| 214 | // an object that defines the clipping geometry, or null to remove clip. |
| 215 | |
| 216 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 217 | this.clip = clip; |
| 218 | }, |
| 219 | |
| 220 | getClip: function(){ |
| 221 | return this.clip; |
| 222 | }, |
| 223 | |
| 224 | setShape: function(shape){ |
| 225 | // summary: |
| 226 | // sets a shape object |
| 227 | // (the default implementation simply ignores it) |
| 228 | // shape: Object |
| 229 | // a shape object |
| 230 | // (see dojox/gfx.defaultPath, |
| 231 | // dojox/gfx.defaultPolyline, |
| 232 | // dojox/gfx.defaultRect, |
| 233 | // dojox/gfx.defaultEllipse, |
| 234 | // dojox/gfx.defaultCircle, |
| 235 | // dojox/gfx.defaultLine, |
| 236 | // or dojox/gfx.defaultImage) |
| 237 | |
| 238 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 239 | this.shape = g.makeParameters(this.shape, shape); |
| 240 | this.bbox = null; |
| 241 | return this; // self |
| 242 | }, |
| 243 | setFill: function(fill){ |
| 244 | // summary: |
| 245 | // sets a fill object |
| 246 | // (the default implementation simply ignores it) |
| 247 | // fill: Object |
| 248 | // a fill object |
| 249 | // (see dojox/gfx.defaultLinearGradient, |
| 250 | // dojox/gfx.defaultRadialGradient, |
| 251 | // dojox/gfx.defaultPattern, |
| 252 | // or dojo/_base/Color) |
| 253 | |
| 254 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 255 | if(!fill){ |
| 256 | // don't fill |
| 257 | this.fillStyle = null; |
| 258 | return this; // self |
| 259 | } |
| 260 | var f = null; |
| 261 | if(typeof(fill) == "object" && "type" in fill){ |
| 262 | // gradient or pattern |
| 263 | switch(fill.type){ |
| 264 | case "linear": |
| 265 | f = g.makeParameters(g.defaultLinearGradient, fill); |
| 266 | break; |
| 267 | case "radial": |
| 268 | f = g.makeParameters(g.defaultRadialGradient, fill); |
| 269 | break; |
| 270 | case "pattern": |
| 271 | f = g.makeParameters(g.defaultPattern, fill); |
| 272 | break; |
| 273 | } |
| 274 | }else{ |
| 275 | // color object |
| 276 | f = g.normalizeColor(fill); |
| 277 | } |
| 278 | this.fillStyle = f; |
| 279 | return this; // self |
| 280 | }, |
| 281 | setStroke: function(stroke){ |
| 282 | // summary: |
| 283 | // sets a stroke object |
| 284 | // (the default implementation simply ignores it) |
| 285 | // stroke: Object |
| 286 | // a stroke object |
| 287 | // (see dojox/gfx.defaultStroke) |
| 288 | |
| 289 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 290 | if(!stroke){ |
| 291 | // don't stroke |
| 292 | this.strokeStyle = null; |
| 293 | return this; // self |
| 294 | } |
| 295 | // normalize the stroke |
| 296 | if(typeof stroke == "string" || lang.isArray(stroke) || stroke instanceof Color){ |
| 297 | stroke = {color: stroke}; |
| 298 | } |
| 299 | var s = this.strokeStyle = g.makeParameters(g.defaultStroke, stroke); |
| 300 | s.color = g.normalizeColor(s.color); |
| 301 | return this; // self |
| 302 | }, |
| 303 | setTransform: function(matrix){ |
| 304 | // summary: |
| 305 | // sets a transformation matrix |
| 306 | // matrix: dojox/gfx/matrix.Matrix2D |
| 307 | // a matrix or a matrix-like object |
| 308 | // (see an argument of dojox/gfx/matrix.Matrix2D |
| 309 | // constructor for a list of acceptable arguments) |
| 310 | |
| 311 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 312 | this.matrix = matrixLib.clone(matrix ? matrixLib.normalize(matrix) : matrixLib.identity); |
| 313 | return this._applyTransform(); // self |
| 314 | }, |
| 315 | |
| 316 | _applyTransform: function(){ |
| 317 | // summary: |
| 318 | // physically sets a matrix |
| 319 | |
| 320 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 321 | return this; // self |
| 322 | }, |
| 323 | |
| 324 | // z-index |
| 325 | |
| 326 | moveToFront: function(){ |
| 327 | // summary: |
| 328 | // moves a shape to front of its parent's list of shapes |
| 329 | var p = this.getParent(); |
| 330 | if(p){ |
| 331 | p._moveChildToFront(this); |
| 332 | this._moveToFront(); // execute renderer-specific action |
| 333 | } |
| 334 | return this; // self |
| 335 | }, |
| 336 | moveToBack: function(){ |
| 337 | // summary: |
| 338 | // moves a shape to back of its parent's list of shapes |
| 339 | var p = this.getParent(); |
| 340 | if(p){ |
| 341 | p._moveChildToBack(this); |
| 342 | this._moveToBack(); // execute renderer-specific action |
| 343 | } |
| 344 | return this; |
| 345 | }, |
| 346 | _moveToFront: function(){ |
| 347 | // summary: |
| 348 | // renderer-specific hook, see dojox/gfx/shape.Shape.moveToFront() |
| 349 | |
| 350 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 351 | }, |
| 352 | _moveToBack: function(){ |
| 353 | // summary: |
| 354 | // renderer-specific hook, see dojox/gfx/shape.Shape.moveToFront() |
| 355 | |
| 356 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 357 | }, |
| 358 | |
| 359 | // apply left & right transformation |
| 360 | |
| 361 | applyRightTransform: function(matrix){ |
| 362 | // summary: |
| 363 | // multiplies the existing matrix with an argument on right side |
| 364 | // (this.matrix * matrix) |
| 365 | // matrix: dojox/gfx/matrix.Matrix2D |
| 366 | // a matrix or a matrix-like object |
| 367 | // (see an argument of dojox/gfx/matrix.Matrix2D |
| 368 | // constructor for a list of acceptable arguments) |
| 369 | return matrix ? this.setTransform([this.matrix, matrix]) : this; // self |
| 370 | }, |
| 371 | applyLeftTransform: function(matrix){ |
| 372 | // summary: |
| 373 | // multiplies the existing matrix with an argument on left side |
| 374 | // (matrix * this.matrix) |
| 375 | // matrix: dojox/gfx/matrix.Matrix2D |
| 376 | // a matrix or a matrix-like object |
| 377 | // (see an argument of dojox/gfx/matrix.Matrix2D |
| 378 | // constructor for a list of acceptable arguments) |
| 379 | return matrix ? this.setTransform([matrix, this.matrix]) : this; // self |
| 380 | }, |
| 381 | applyTransform: function(matrix){ |
| 382 | // summary: |
| 383 | // a shortcut for dojox/gfx/shape.Shape.applyRightTransform |
| 384 | // matrix: dojox/gfx/matrix.Matrix2D |
| 385 | // a matrix or a matrix-like object |
| 386 | // (see an argument of dojox/gfx/matrix.Matrix2D |
| 387 | // constructor for a list of acceptable arguments) |
| 388 | return matrix ? this.setTransform([this.matrix, matrix]) : this; // self |
| 389 | }, |
| 390 | |
| 391 | // virtual group methods |
| 392 | |
| 393 | removeShape: function(silently){ |
| 394 | // summary: |
| 395 | // removes the shape from its parent's list of shapes |
| 396 | // silently: Boolean |
| 397 | // if true, do not redraw a picture yet |
| 398 | if(this.parent){ |
| 399 | this.parent.remove(this, silently); |
| 400 | } |
| 401 | return this; // self |
| 402 | }, |
| 403 | _setParent: function(parent, matrix){ |
| 404 | // summary: |
| 405 | // sets a parent |
| 406 | // parent: Object |
| 407 | // a parent or null |
| 408 | // (see dojox/gfx/shape.Surface, |
| 409 | // or dojox/gfx.Group) |
| 410 | // matrix: dojox/gfx/matrix.Matrix2D |
| 411 | // a 2D matrix or a matrix-like object |
| 412 | this.parent = parent; |
| 413 | return this._updateParentMatrix(matrix); // self |
| 414 | }, |
| 415 | _updateParentMatrix: function(matrix){ |
| 416 | // summary: |
| 417 | // updates the parent matrix with new matrix |
| 418 | // matrix: dojox/gfx/Matrix2D |
| 419 | // a 2D matrix or a matrix-like object |
| 420 | this.parentMatrix = matrix ? matrixLib.clone(matrix) : null; |
| 421 | return this._applyTransform(); // self |
| 422 | }, |
| 423 | _getRealMatrix: function(){ |
| 424 | // summary: |
| 425 | // returns the cumulative ('real') transformation matrix |
| 426 | // by combining the shape's matrix with its parent's matrix |
| 427 | var m = this.matrix; |
| 428 | var p = this.parent; |
| 429 | while(p){ |
| 430 | if(p.matrix){ |
| 431 | m = matrixLib.multiply(p.matrix, m); |
| 432 | } |
| 433 | p = p.parent; |
| 434 | } |
| 435 | return m; // dojox/gfx/matrix.Matrix2D |
| 436 | } |
| 437 | }); |
| 438 | |
| 439 | shape._eventsProcessing = { |
| 440 | on: function(type, listener){ |
| 441 | // summary: |
| 442 | // Connects an event to this shape. |
| 443 | |
| 444 | return on(this.getEventSource(), type, shape.fixCallback(this, g.fixTarget, listener)); |
| 445 | }, |
| 446 | |
| 447 | connect: function(name, object, method){ |
| 448 | // summary: |
| 449 | // connects a handler to an event on this shape |
| 450 | |
| 451 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 452 | // redirect to fixCallback to normalize events and add the gfxTarget to the event. The latter |
| 453 | // is done by dojox/gfx.fixTarget which is defined by each renderer |
| 454 | if(name.substring(0, 2) == "on"){ |
| 455 | name = name.substring(2); |
| 456 | } |
| 457 | return this.on(name, method ? lang.hitch(object, method) : object); |
| 458 | }, |
| 459 | |
| 460 | disconnect: function(token){ |
| 461 | // summary: |
| 462 | // connects a handler by token from an event on this shape |
| 463 | |
| 464 | // COULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 465 | |
| 466 | return token.remove(); |
| 467 | } |
| 468 | }; |
| 469 | |
| 470 | shape.fixCallback = function(gfxElement, fixFunction, scope, method){ |
| 471 | // summary: |
| 472 | // Wraps the callback to allow for tests and event normalization |
| 473 | // before it gets invoked. This is where 'fixTarget' is invoked. |
| 474 | // tags: |
| 475 | // private |
| 476 | // gfxElement: Object |
| 477 | // The GFX object that triggers the action (ex.: |
| 478 | // dojox/gfx.Surface and dojox/gfx/shape.Shape). A new event property |
| 479 | // 'gfxTarget' is added to the event to reference this object. |
| 480 | // for easy manipulation of GFX objects by the event handlers. |
| 481 | // fixFunction: Function |
| 482 | // The function that implements the logic to set the 'gfxTarget' |
| 483 | // property to the event. It should be 'dojox/gfx.fixTarget' for |
| 484 | // most of the cases |
| 485 | // scope: Object |
| 486 | // Optional. The scope to be used when invoking 'method'. If |
| 487 | // omitted, a global scope is used. |
| 488 | // method: Function|String |
| 489 | // The original callback to be invoked. |
| 490 | if(!method){ |
| 491 | method = scope; |
| 492 | scope = null; |
| 493 | } |
| 494 | if(lang.isString(method)){ |
| 495 | scope = scope || kernel.global; |
| 496 | if(!scope[method]){ throw(['dojox.gfx.shape.fixCallback: scope["', method, '"] is null (scope="', scope, '")'].join('')); } |
| 497 | return function(e){ |
| 498 | return fixFunction(e,gfxElement) ? scope[method].apply(scope, arguments || []) : undefined; }; // Function |
| 499 | } |
| 500 | return !scope |
| 501 | ? function(e){ |
| 502 | return fixFunction(e,gfxElement) ? method.apply(scope, arguments) : undefined; } |
| 503 | : function(e){ |
| 504 | return fixFunction(e,gfxElement) ? method.apply(scope, arguments || []) : undefined; }; // Function |
| 505 | }; |
| 506 | lang.extend(shape.Shape, shape._eventsProcessing); |
| 507 | |
| 508 | shape.Container = { |
| 509 | // summary: |
| 510 | // a container of shapes, which can be used |
| 511 | // as a foundation for renderer-specific groups, or as a way |
| 512 | // to logically group shapes (e.g, to propagate matricies) |
| 513 | |
| 514 | _init: function() { |
| 515 | // children: Array |
| 516 | // a list of children |
| 517 | this.children = []; |
| 518 | this._batch = 0; |
| 519 | }, |
| 520 | |
| 521 | // group management |
| 522 | |
| 523 | openBatch: function() { |
| 524 | // summary: |
| 525 | // starts a new batch, subsequent new child shapes will be held in |
| 526 | // the batch instead of appending to the container directly. |
| 527 | // description: |
| 528 | // Because the canvas renderer has no DOM hierarchy, the canvas implementation differs |
| 529 | // such that it suspends the repaint requests for this container until the current batch is closed by a call to closeBatch(). |
| 530 | return this; |
| 531 | }, |
| 532 | closeBatch: function() { |
| 533 | // summary: |
| 534 | // submits the current batch, append all pending child shapes to DOM |
| 535 | // description: |
| 536 | // On canvas, this method flushes the pending redraws queue. |
| 537 | return this; |
| 538 | }, |
| 539 | add: function(shape){ |
| 540 | // summary: |
| 541 | // adds a shape to the list |
| 542 | // shape: dojox/gfx/shape.Shape |
| 543 | // the shape to add to the list |
| 544 | var oldParent = shape.getParent(); |
| 545 | if(oldParent){ |
| 546 | oldParent.remove(shape, true); |
| 547 | } |
| 548 | this.children.push(shape); |
| 549 | return shape._setParent(this, this._getRealMatrix()); // self |
| 550 | }, |
| 551 | remove: function(shape, silently){ |
| 552 | // summary: |
| 553 | // removes a shape from the list |
| 554 | // shape: dojox/gfx/shape.Shape |
| 555 | // the shape to remove |
| 556 | // silently: Boolean |
| 557 | // if true, do not redraw a picture yet |
| 558 | for(var i = 0; i < this.children.length; ++i){ |
| 559 | if(this.children[i] == shape){ |
| 560 | if(silently){ |
| 561 | // skip for now |
| 562 | }else{ |
| 563 | shape.parent = null; |
| 564 | shape.parentMatrix = null; |
| 565 | } |
| 566 | removeItemAt(this.children, i); |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | return this; // self |
| 571 | }, |
| 572 | clear: function(/*Boolean?*/ destroy){ |
| 573 | // summary: |
| 574 | // removes all shapes from a group/surface. |
| 575 | // destroy: Boolean |
| 576 | // Indicates whether the children should be destroyed. Optional. |
| 577 | var shape; |
| 578 | for(var i = 0; i < this.children.length;++i){ |
| 579 | shape = this.children[i]; |
| 580 | shape.parent = null; |
| 581 | shape.parentMatrix = null; |
| 582 | if(destroy){ |
| 583 | shape.destroy(); |
| 584 | } |
| 585 | } |
| 586 | this.children = []; |
| 587 | return this; // self |
| 588 | }, |
| 589 | getBoundingBox: function(){ |
| 590 | // summary: |
| 591 | // Returns the bounding box Rectangle for this shape. |
| 592 | if(this.children){ |
| 593 | // if this is a composite shape, then sum up all the children |
| 594 | var result = null; |
| 595 | arr.forEach(this.children, function(shape){ |
| 596 | var bb = shape.getBoundingBox(); |
| 597 | if(bb){ |
| 598 | var ct = shape.getTransform(); |
| 599 | if(ct){ |
| 600 | bb = matrixLib.multiplyRectangle(ct, bb); |
| 601 | } |
| 602 | if(result){ |
| 603 | // merge two bbox |
| 604 | result.x = Math.min(result.x, bb.x); |
| 605 | result.y = Math.min(result.y, bb.y); |
| 606 | result.endX = Math.max(result.endX, bb.x + bb.width); |
| 607 | result.endY = Math.max(result.endY, bb.y + bb.height); |
| 608 | }else{ |
| 609 | // first bbox |
| 610 | result = { |
| 611 | x: bb.x, |
| 612 | y: bb.y, |
| 613 | endX: bb.x + bb.width, |
| 614 | endY: bb.y + bb.height |
| 615 | }; |
| 616 | } |
| 617 | } |
| 618 | }); |
| 619 | if(result){ |
| 620 | result.width = result.endX - result.x; |
| 621 | result.height = result.endY - result.y; |
| 622 | } |
| 623 | return result; // dojox/gfx.Rectangle |
| 624 | } |
| 625 | // unknown/empty bounding box, subclass shall override this impl |
| 626 | return null; |
| 627 | }, |
| 628 | // moving child nodes |
| 629 | _moveChildToFront: function(shape){ |
| 630 | // summary: |
| 631 | // moves a shape to front of the list of shapes |
| 632 | // shape: dojox/gfx/shape.Shape |
| 633 | // one of the child shapes to move to the front |
| 634 | for(var i = 0; i < this.children.length; ++i){ |
| 635 | if(this.children[i] == shape){ |
| 636 | removeItemAt(this.children, i); |
| 637 | this.children.push(shape); |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | return this; // self |
| 642 | }, |
| 643 | _moveChildToBack: function(shape){ |
| 644 | // summary: |
| 645 | // moves a shape to back of the list of shapes |
| 646 | // shape: dojox/gfx/shape.Shape |
| 647 | // one of the child shapes to move to the front |
| 648 | for(var i = 0; i < this.children.length; ++i){ |
| 649 | if(this.children[i] == shape){ |
| 650 | removeItemAt(this.children, i); |
| 651 | this.children.unshift(shape); |
| 652 | break; |
| 653 | } |
| 654 | } |
| 655 | return this; // self |
| 656 | } |
| 657 | }; |
| 658 | |
| 659 | shape.Surface = declare("dojox.gfx.shape.Surface", null, { |
| 660 | // summary: |
| 661 | // a surface object to be used for drawings |
| 662 | constructor: function(){ |
| 663 | // underlying node |
| 664 | this.rawNode = null; |
| 665 | // the parent node |
| 666 | this._parent = null; |
| 667 | // the list of DOM nodes to be deleted in the case of destruction |
| 668 | this._nodes = []; |
| 669 | // the list of events to be detached in the case of destruction |
| 670 | this._events = []; |
| 671 | }, |
| 672 | destroy: function(){ |
| 673 | // summary: |
| 674 | // destroy all relevant external resources and release all |
| 675 | // external references to make this object garbage-collectible |
| 676 | arr.forEach(this._nodes, domConstruct.destroy); |
| 677 | this._nodes = []; |
| 678 | arr.forEach(this._events, function(h){ if(h){ h.remove(); } }); |
| 679 | this._events = []; |
| 680 | this.rawNode = null; // recycle it in _nodes, if it needs to be recycled |
| 681 | if(has("ie")){ |
| 682 | while(this._parent.lastChild){ |
| 683 | domConstruct.destroy(this._parent.lastChild); |
| 684 | } |
| 685 | }else{ |
| 686 | this._parent.innerHTML = ""; |
| 687 | } |
| 688 | this._parent = null; |
| 689 | }, |
| 690 | getEventSource: function(){ |
| 691 | // summary: |
| 692 | // returns a node, which can be used to attach event listeners |
| 693 | return this.rawNode; // Node |
| 694 | }, |
| 695 | _getRealMatrix: function(){ |
| 696 | // summary: |
| 697 | // always returns the identity matrix |
| 698 | return null; // dojox/gfx/Matrix2D |
| 699 | }, |
| 700 | /*===== |
| 701 | setDimensions: function(width, height){ |
| 702 | // summary: |
| 703 | // sets the width and height of the rawNode |
| 704 | // width: String |
| 705 | // width of surface, e.g., "100px" |
| 706 | // height: String |
| 707 | // height of surface, e.g., "100px" |
| 708 | return this; // self |
| 709 | }, |
| 710 | getDimensions: function(){ |
| 711 | // summary: |
| 712 | // gets current width and height in pixels |
| 713 | // returns: Object |
| 714 | // object with properties "width" and "height" |
| 715 | }, |
| 716 | =====*/ |
| 717 | isLoaded: true, |
| 718 | onLoad: function(/*dojox/gfx/shape.Surface*/ surface){ |
| 719 | // summary: |
| 720 | // local event, fired once when the surface is created |
| 721 | // asynchronously, used only when isLoaded is false, required |
| 722 | // only for Silverlight. |
| 723 | }, |
| 724 | whenLoaded: function(/*Object|Null*/ context, /*Function|String*/ method){ |
| 725 | var f = lang.hitch(context, method); |
| 726 | if(this.isLoaded){ |
| 727 | f(this); |
| 728 | }else{ |
| 729 | on.once(this, "load", function(surface){ |
| 730 | f(surface); |
| 731 | }); |
| 732 | } |
| 733 | } |
| 734 | }); |
| 735 | lang.extend(shape.Surface, shape._eventsProcessing); |
| 736 | |
| 737 | /*===== |
| 738 | g.Point = declare("dojox/gfx.Point", null, { |
| 739 | // summary: |
| 740 | // 2D point for drawings - {x, y} |
| 741 | // description: |
| 742 | // Do not use this object directly! |
| 743 | // Use the naked object instead: {x: 1, y: 2}. |
| 744 | }); |
| 745 | |
| 746 | g.Rectangle = declare("dojox.gfx.Rectangle", null, { |
| 747 | // summary: |
| 748 | // rectangle - {x, y, width, height} |
| 749 | // description: |
| 750 | // Do not use this object directly! |
| 751 | // Use the naked object instead: {x: 1, y: 2, width: 100, height: 200}. |
| 752 | }); |
| 753 | =====*/ |
| 754 | |
| 755 | |
| 756 | shape.Rect = declare("dojox.gfx.shape.Rect", shape.Shape, { |
| 757 | // summary: |
| 758 | // a generic rectangle |
| 759 | constructor: function(rawNode){ |
| 760 | // rawNode: Node |
| 761 | // The underlying graphics system object (typically a DOM Node) |
| 762 | this.shape = g.getDefault("Rect"); |
| 763 | this.rawNode = rawNode; |
| 764 | }, |
| 765 | getBoundingBox: function(){ |
| 766 | // summary: |
| 767 | // returns the bounding box (its shape in this case) |
| 768 | return this.shape; // dojox/gfx.Rectangle |
| 769 | } |
| 770 | }); |
| 771 | |
| 772 | shape.Ellipse = declare("dojox.gfx.shape.Ellipse", shape.Shape, { |
| 773 | // summary: |
| 774 | // a generic ellipse |
| 775 | constructor: function(rawNode){ |
| 776 | // rawNode: Node |
| 777 | // a DOM Node |
| 778 | this.shape = g.getDefault("Ellipse"); |
| 779 | this.rawNode = rawNode; |
| 780 | }, |
| 781 | getBoundingBox: function(){ |
| 782 | // summary: |
| 783 | // returns the bounding box |
| 784 | if(!this.bbox){ |
| 785 | var shape = this.shape; |
| 786 | this.bbox = {x: shape.cx - shape.rx, y: shape.cy - shape.ry, |
| 787 | width: 2 * shape.rx, height: 2 * shape.ry}; |
| 788 | } |
| 789 | return this.bbox; // dojox/gfx.Rectangle |
| 790 | } |
| 791 | }); |
| 792 | |
| 793 | shape.Circle = declare("dojox.gfx.shape.Circle", shape.Shape, { |
| 794 | // summary: |
| 795 | // a generic circle |
| 796 | constructor: function(rawNode){ |
| 797 | // rawNode: Node |
| 798 | // a DOM Node |
| 799 | this.shape = g.getDefault("Circle"); |
| 800 | this.rawNode = rawNode; |
| 801 | }, |
| 802 | getBoundingBox: function(){ |
| 803 | // summary: |
| 804 | // returns the bounding box |
| 805 | if(!this.bbox){ |
| 806 | var shape = this.shape; |
| 807 | this.bbox = {x: shape.cx - shape.r, y: shape.cy - shape.r, |
| 808 | width: 2 * shape.r, height: 2 * shape.r}; |
| 809 | } |
| 810 | return this.bbox; // dojox/gfx.Rectangle |
| 811 | } |
| 812 | }); |
| 813 | |
| 814 | shape.Line = declare("dojox.gfx.shape.Line", shape.Shape, { |
| 815 | // summary: |
| 816 | // a generic line (do not instantiate it directly) |
| 817 | constructor: function(rawNode){ |
| 818 | // rawNode: Node |
| 819 | // a DOM Node |
| 820 | this.shape = g.getDefault("Line"); |
| 821 | this.rawNode = rawNode; |
| 822 | }, |
| 823 | getBoundingBox: function(){ |
| 824 | // summary: |
| 825 | // returns the bounding box |
| 826 | if(!this.bbox){ |
| 827 | var shape = this.shape; |
| 828 | this.bbox = { |
| 829 | x: Math.min(shape.x1, shape.x2), |
| 830 | y: Math.min(shape.y1, shape.y2), |
| 831 | width: Math.abs(shape.x2 - shape.x1), |
| 832 | height: Math.abs(shape.y2 - shape.y1) |
| 833 | }; |
| 834 | } |
| 835 | return this.bbox; // dojox/gfx.Rectangle |
| 836 | } |
| 837 | }); |
| 838 | |
| 839 | shape.Polyline = declare("dojox.gfx.shape.Polyline", shape.Shape, { |
| 840 | // summary: |
| 841 | // a generic polyline/polygon (do not instantiate it directly) |
| 842 | constructor: function(rawNode){ |
| 843 | // rawNode: Node |
| 844 | // a DOM Node |
| 845 | this.shape = g.getDefault("Polyline"); |
| 846 | this.rawNode = rawNode; |
| 847 | }, |
| 848 | setShape: function(points, closed){ |
| 849 | // summary: |
| 850 | // sets a polyline/polygon shape object |
| 851 | // points: Object|Array |
| 852 | // a polyline/polygon shape object, or an array of points |
| 853 | // closed: Boolean |
| 854 | // close the polyline to make a polygon |
| 855 | if(points && points instanceof Array){ |
| 856 | this.inherited(arguments, [{points: points}]); |
| 857 | if(closed && this.shape.points.length){ |
| 858 | this.shape.points.push(this.shape.points[0]); |
| 859 | } |
| 860 | }else{ |
| 861 | this.inherited(arguments, [points]); |
| 862 | } |
| 863 | return this; // self |
| 864 | }, |
| 865 | _normalizePoints: function(){ |
| 866 | // summary: |
| 867 | // normalize points to array of {x:number, y:number} |
| 868 | var p = this.shape.points, l = p && p.length; |
| 869 | if(l && typeof p[0] == "number"){ |
| 870 | var points = []; |
| 871 | for(var i = 0; i < l; i += 2){ |
| 872 | points.push({x: p[i], y: p[i + 1]}); |
| 873 | } |
| 874 | this.shape.points = points; |
| 875 | } |
| 876 | }, |
| 877 | getBoundingBox: function(){ |
| 878 | // summary: |
| 879 | // returns the bounding box |
| 880 | if(!this.bbox && this.shape.points.length){ |
| 881 | var p = this.shape.points; |
| 882 | var l = p.length; |
| 883 | var t = p[0]; |
| 884 | var bbox = {l: t.x, t: t.y, r: t.x, b: t.y}; |
| 885 | for(var i = 1; i < l; ++i){ |
| 886 | t = p[i]; |
| 887 | if(bbox.l > t.x) bbox.l = t.x; |
| 888 | if(bbox.r < t.x) bbox.r = t.x; |
| 889 | if(bbox.t > t.y) bbox.t = t.y; |
| 890 | if(bbox.b < t.y) bbox.b = t.y; |
| 891 | } |
| 892 | this.bbox = { |
| 893 | x: bbox.l, |
| 894 | y: bbox.t, |
| 895 | width: bbox.r - bbox.l, |
| 896 | height: bbox.b - bbox.t |
| 897 | }; |
| 898 | } |
| 899 | return this.bbox; // dojox/gfx.Rectangle |
| 900 | } |
| 901 | }); |
| 902 | |
| 903 | shape.Image = declare("dojox.gfx.shape.Image", shape.Shape, { |
| 904 | // summary: |
| 905 | // a generic image (do not instantiate it directly) |
| 906 | constructor: function(rawNode){ |
| 907 | // rawNode: Node |
| 908 | // a DOM Node |
| 909 | this.shape = g.getDefault("Image"); |
| 910 | this.rawNode = rawNode; |
| 911 | }, |
| 912 | getBoundingBox: function(){ |
| 913 | // summary: |
| 914 | // returns the bounding box (its shape in this case) |
| 915 | return this.shape; // dojox/gfx.Rectangle |
| 916 | }, |
| 917 | setStroke: function(){ |
| 918 | // summary: |
| 919 | // ignore setting a stroke style |
| 920 | return this; // self |
| 921 | }, |
| 922 | setFill: function(){ |
| 923 | // summary: |
| 924 | // ignore setting a fill style |
| 925 | return this; // self |
| 926 | } |
| 927 | }); |
| 928 | |
| 929 | shape.Text = declare(shape.Shape, { |
| 930 | // summary: |
| 931 | // a generic text (do not instantiate it directly) |
| 932 | constructor: function(rawNode){ |
| 933 | // rawNode: Node |
| 934 | // a DOM Node |
| 935 | this.fontStyle = null; |
| 936 | this.shape = g.getDefault("Text"); |
| 937 | this.rawNode = rawNode; |
| 938 | }, |
| 939 | getFont: function(){ |
| 940 | // summary: |
| 941 | // returns the current font object or null |
| 942 | return this.fontStyle; // Object |
| 943 | }, |
| 944 | setFont: function(newFont){ |
| 945 | // summary: |
| 946 | // sets a font for text |
| 947 | // newFont: Object |
| 948 | // a font object (see dojox/gfx.defaultFont) or a font string |
| 949 | this.fontStyle = typeof newFont == "string" ? g.splitFontString(newFont) : |
| 950 | g.makeParameters(g.defaultFont, newFont); |
| 951 | this._setFont(); |
| 952 | return this; // self |
| 953 | }, |
| 954 | getBoundingBox: function(){ |
| 955 | var bbox = null, s = this.getShape(); |
| 956 | if(s.text){ |
| 957 | bbox = g._base._computeTextBoundingBox(this); |
| 958 | } |
| 959 | return bbox; |
| 960 | } |
| 961 | }); |
| 962 | |
| 963 | shape.Creator = { |
| 964 | // summary: |
| 965 | // shape creators |
| 966 | createShape: function(shape){ |
| 967 | // summary: |
| 968 | // creates a shape object based on its type; it is meant to be used |
| 969 | // by group-like objects |
| 970 | // shape: Object |
| 971 | // a shape descriptor object |
| 972 | // returns: dojox/gfx/shape.Shape | Null |
| 973 | // a fully instantiated surface-specific Shape object |
| 974 | switch(shape.type){ |
| 975 | case g.defaultPath.type: return this.createPath(shape); |
| 976 | case g.defaultRect.type: return this.createRect(shape); |
| 977 | case g.defaultCircle.type: return this.createCircle(shape); |
| 978 | case g.defaultEllipse.type: return this.createEllipse(shape); |
| 979 | case g.defaultLine.type: return this.createLine(shape); |
| 980 | case g.defaultPolyline.type: return this.createPolyline(shape); |
| 981 | case g.defaultImage.type: return this.createImage(shape); |
| 982 | case g.defaultText.type: return this.createText(shape); |
| 983 | case g.defaultTextPath.type: return this.createTextPath(shape); |
| 984 | } |
| 985 | return null; |
| 986 | }, |
| 987 | createGroup: function(){ |
| 988 | // summary: |
| 989 | // creates a group shape |
| 990 | return this.createObject(g.Group); // dojox/gfx/Group |
| 991 | }, |
| 992 | createRect: function(rect){ |
| 993 | // summary: |
| 994 | // creates a rectangle shape |
| 995 | // rect: Object |
| 996 | // a path object (see dojox/gfx.defaultRect) |
| 997 | return this.createObject(g.Rect, rect); // dojox/gfx/shape.Rect |
| 998 | }, |
| 999 | createEllipse: function(ellipse){ |
| 1000 | // summary: |
| 1001 | // creates an ellipse shape |
| 1002 | // ellipse: Object |
| 1003 | // an ellipse object (see dojox/gfx.defaultEllipse) |
| 1004 | return this.createObject(g.Ellipse, ellipse); // dojox/gfx/shape.Ellipse |
| 1005 | }, |
| 1006 | createCircle: function(circle){ |
| 1007 | // summary: |
| 1008 | // creates a circle shape |
| 1009 | // circle: Object |
| 1010 | // a circle object (see dojox/gfx.defaultCircle) |
| 1011 | return this.createObject(g.Circle, circle); // dojox/gfx/shape.Circle |
| 1012 | }, |
| 1013 | createLine: function(line){ |
| 1014 | // summary: |
| 1015 | // creates a line shape |
| 1016 | // line: Object |
| 1017 | // a line object (see dojox/gfx.defaultLine) |
| 1018 | return this.createObject(g.Line, line); // dojox/gfx/shape.Line |
| 1019 | }, |
| 1020 | createPolyline: function(points){ |
| 1021 | // summary: |
| 1022 | // creates a polyline/polygon shape |
| 1023 | // points: Object |
| 1024 | // a points object (see dojox/gfx.defaultPolyline) |
| 1025 | // or an Array of points |
| 1026 | return this.createObject(g.Polyline, points); // dojox/gfx/shape.Polyline |
| 1027 | }, |
| 1028 | createImage: function(image){ |
| 1029 | // summary: |
| 1030 | // creates a image shape |
| 1031 | // image: Object |
| 1032 | // an image object (see dojox/gfx.defaultImage) |
| 1033 | return this.createObject(g.Image, image); // dojox/gfx/shape.Image |
| 1034 | }, |
| 1035 | createText: function(text){ |
| 1036 | // summary: |
| 1037 | // creates a text shape |
| 1038 | // text: Object |
| 1039 | // a text object (see dojox/gfx.defaultText) |
| 1040 | return this.createObject(g.Text, text); // dojox/gfx/shape.Text |
| 1041 | }, |
| 1042 | createPath: function(path){ |
| 1043 | // summary: |
| 1044 | // creates a path shape |
| 1045 | // path: Object |
| 1046 | // a path object (see dojox/gfx.defaultPath) |
| 1047 | return this.createObject(g.Path, path); // dojox/gfx/shape.Path |
| 1048 | }, |
| 1049 | createTextPath: function(text){ |
| 1050 | // summary: |
| 1051 | // creates a text shape |
| 1052 | // text: Object |
| 1053 | // a textpath object (see dojox/gfx.defaultTextPath) |
| 1054 | return this.createObject(g.TextPath, {}).setText(text); // dojox/gfx/shape.TextPath |
| 1055 | }, |
| 1056 | createObject: function(shapeType, rawShape){ |
| 1057 | // summary: |
| 1058 | // creates an instance of the passed shapeType class |
| 1059 | // shapeType: Function |
| 1060 | // a class constructor to create an instance of |
| 1061 | // rawShape: Object |
| 1062 | // properties to be passed in to the classes 'setShape' method |
| 1063 | |
| 1064 | // SHOULD BE RE-IMPLEMENTED BY THE RENDERER! |
| 1065 | return null; // dojox/gfx/shape.Shape |
| 1066 | } |
| 1067 | }; |
| 1068 | |
| 1069 | /*===== |
| 1070 | lang.extend(shape.Surface, shape.Container); |
| 1071 | lang.extend(shape.Surface, shape.Creator); |
| 1072 | |
| 1073 | g.Group = declare(shape.Shape, { |
| 1074 | // summary: |
| 1075 | // a group shape, which can be used |
| 1076 | // to logically group shapes (e.g, to propagate matricies) |
| 1077 | }); |
| 1078 | lang.extend(g.Group, shape.Container); |
| 1079 | lang.extend(g.Group, shape.Creator); |
| 1080 | |
| 1081 | g.Rect = shape.Rect; |
| 1082 | g.Circle = shape.Circle; |
| 1083 | g.Ellipse = shape.Ellipse; |
| 1084 | g.Line = shape.Line; |
| 1085 | g.Polyline = shape.Polyline; |
| 1086 | g.Text = shape.Text; |
| 1087 | g.Surface = shape.Surface; |
| 1088 | =====*/ |
| 1089 | |
| 1090 | return shape; |
| 1091 | }); |