Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / fx / target / Sprite.js
1 /**
2  * @class Ext.fx.target.Sprite
3  * @extends Ext.fx.target.Target
4
5 This class represents a animation target for a {@link Ext.draw.Sprite}. In general this class will not be
6 created directly, the {@link Ext.draw.Sprite} will be passed to the animation and
7 and the appropriate target will be created.
8
9  * @markdown
10  */
11
12 Ext.define('Ext.fx.target.Sprite', {
13
14     /* Begin Definitions */
15
16     extend: 'Ext.fx.target.Target',
17
18     /* End Definitions */
19
20     type: 'draw',
21
22     getFromPrim: function(sprite, attr) {
23         var o;
24         if (attr == 'translate') {
25             o = {
26                 x: sprite.attr.translation.x || 0,
27                 y: sprite.attr.translation.y || 0
28             };
29         }
30         else if (attr == 'rotate') {
31             o = {
32                 degrees: sprite.attr.rotation.degrees || 0,
33                 x: sprite.attr.rotation.x,
34                 y: sprite.attr.rotation.y
35             };
36         }
37         else {
38             o = sprite.attr[attr];
39         }
40         return o;
41     },
42
43     getAttr: function(attr, val) {
44         return [[this.target, val != undefined ? val : this.getFromPrim(this.target, attr)]];
45     },
46
47     setAttr: function(targetData) {
48         var ln = targetData.length,
49             spriteArr = [],
50             attrs, attr, attrArr, attPtr, spritePtr, idx, value, i, j, x, y, ln2;
51         for (i = 0; i < ln; i++) {
52             attrs = targetData[i].attrs;
53             for (attr in attrs) {
54                 attrArr = attrs[attr];
55                 ln2 = attrArr.length;
56                 for (j = 0; j < ln2; j++) {
57                     spritePtr = attrArr[j][0];
58                     attPtr = attrArr[j][1];
59                     if (attr === 'translate') {
60                         value = {
61                             x: attPtr.x,
62                             y: attPtr.y
63                         };
64                     }
65                     else if (attr === 'rotate') {
66                         x = attPtr.x;
67                         if (isNaN(x)) {
68                             x = null;
69                         }
70                         y = attPtr.y;
71                         if (isNaN(y)) {
72                             y = null;
73                         }
74                         value = {
75                             degrees: attPtr.degrees,
76                             x: x,
77                             y: y
78                         };
79                     }
80                     else if (attr === 'width' || attr === 'height' || attr === 'x' || attr === 'y') {
81                         value = parseFloat(attPtr);
82                     }
83                     else {
84                         value = attPtr;
85                     }
86                     idx = Ext.Array.indexOf(spriteArr, spritePtr);
87                     if (idx == -1) {
88                         spriteArr.push([spritePtr, {}]);
89                         idx = spriteArr.length - 1;
90                     }
91                     spriteArr[idx][1][attr] = value;
92                 }
93             }
94         }
95         ln = spriteArr.length;
96         for (i = 0; i < ln; i++) {
97             spritePtr = spriteArr[i];
98             spritePtr[0].setAttributes(spritePtr[1]);
99         }
100         this.target.redraw();
101     }
102 });