Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / fx / target / Sprite.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.fx.target.Sprite
17  * @extends Ext.fx.target.Target
18
19 This class represents a animation target for a {@link Ext.draw.Sprite}. In general this class will not be
20 created directly, the {@link Ext.draw.Sprite} will be passed to the animation and
21 and the appropriate target will be created.
22
23  * @markdown
24  */
25
26 Ext.define('Ext.fx.target.Sprite', {
27
28     /* Begin Definitions */
29
30     extend: 'Ext.fx.target.Target',
31
32     /* End Definitions */
33
34     type: 'draw',
35
36     getFromPrim: function(sprite, attr) {
37         var o;
38         if (attr == 'translate') {
39             o = {
40                 x: sprite.attr.translation.x || 0,
41                 y: sprite.attr.translation.y || 0
42             };
43         }
44         else if (attr == 'rotate') {
45             o = {
46                 degrees: sprite.attr.rotation.degrees || 0,
47                 x: sprite.attr.rotation.x,
48                 y: sprite.attr.rotation.y
49             };
50         }
51         else {
52             o = sprite.attr[attr];
53         }
54         return o;
55     },
56
57     getAttr: function(attr, val) {
58         return [[this.target, val != undefined ? val : this.getFromPrim(this.target, attr)]];
59     },
60
61     setAttr: function(targetData) {
62         var ln = targetData.length,
63             spriteArr = [],
64             attrs, attr, attrArr, attPtr, spritePtr, idx, value, i, j, x, y, ln2;
65         for (i = 0; i < ln; i++) {
66             attrs = targetData[i].attrs;
67             for (attr in attrs) {
68                 attrArr = attrs[attr];
69                 ln2 = attrArr.length;
70                 for (j = 0; j < ln2; j++) {
71                     spritePtr = attrArr[j][0];
72                     attPtr = attrArr[j][1];
73                     if (attr === 'translate') {
74                         value = {
75                             x: attPtr.x,
76                             y: attPtr.y
77                         };
78                     }
79                     else if (attr === 'rotate') {
80                         x = attPtr.x;
81                         if (isNaN(x)) {
82                             x = null;
83                         }
84                         y = attPtr.y;
85                         if (isNaN(y)) {
86                             y = null;
87                         }
88                         value = {
89                             degrees: attPtr.degrees,
90                             x: x,
91                             y: y
92                         };
93                     }
94                     else if (attr === 'width' || attr === 'height' || attr === 'x' || attr === 'y') {
95                         value = parseFloat(attPtr);
96                     }
97                     else {
98                         value = attPtr;
99                     }
100                     idx = Ext.Array.indexOf(spriteArr, spritePtr);
101                     if (idx == -1) {
102                         spriteArr.push([spritePtr, {}]);
103                         idx = spriteArr.length - 1;
104                     }
105                     spriteArr[idx][1][attr] = value;
106                 }
107             }
108         }
109         ln = spriteArr.length;
110         for (i = 0; i < ln; i++) {
111             spritePtr = spriteArr[i];
112             spritePtr[0].setAttributes(spritePtr[1]);
113         }
114         this.target.redraw();
115     }
116 });
117