3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.draw.CompositeSprite
17 * @extends Ext.util.MixedCollection
19 * A composite Sprite handles a group of sprites with common methods to a sprite
20 * such as `hide`, `show`, `setAttributes`. These methods are applied to the set of sprites
23 * CompositeSprite extends {@link Ext.util.MixedCollection} so you can use the same methods
24 * in `MixedCollection` to iterate through sprites, add and remove elements, etc.
26 * In order to create a CompositeSprite, one has to provide a handle to the surface where it is
29 * var group = Ext.create('Ext.draw.CompositeSprite', {
30 * surface: drawComponent.surface
33 * Then just by using `MixedCollection` methods it's possible to add {@link Ext.draw.Sprite}s:
39 * And then apply common Sprite methods to them:
41 * group.setAttributes({
45 Ext.define('Ext.draw.CompositeSprite', {
47 /* Begin Definitions */
49 extend: 'Ext.util.MixedCollection',
51 animate: 'Ext.util.Animate'
55 isCompositeSprite: true,
56 constructor: function(config) {
59 config = config || {};
60 Ext.apply(me, config);
69 me.id = Ext.id(null, 'ext-sprite-group-');
74 onClick: function(e) {
75 this.fireEvent('click', e);
79 onMouseUp: function(e) {
80 this.fireEvent('mouseup', e);
84 onMouseDown: function(e) {
85 this.fireEvent('mousedown', e);
89 onMouseOver: function(e) {
90 this.fireEvent('mouseover', e);
94 onMouseOut: function(e) {
95 this.fireEvent('mouseout', e);
98 attachEvents: function(o) {
103 mousedown: me.onMouseDown,
104 mouseup: me.onMouseUp,
105 mouseover: me.onMouseOver,
106 mouseout: me.onMouseOut,
111 /** Add a Sprite to the Group */
112 add: function(key, o) {
113 var result = this.callParent(arguments);
114 this.attachEvents(result);
118 insert: function(index, key, o) {
119 return this.callParent(arguments);
122 /** Remove a Sprite from the Group */
123 remove: function(o) {
128 mousedown: me.onMouseDown,
129 mouseup: me.onMouseUp,
130 mouseover: me.onMouseOver,
131 mouseout: me.onMouseOut,
134 me.callParent(arguments);
138 * Returns the group bounding box.
139 * Behaves like {@link Ext.draw.Sprite} getBBox method.
141 getBBox: function() {
149 maxHeight = -infinity,
151 maxWidth = -infinity,
152 maxWidthBBox, maxHeightBBox;
154 for (; i < len; i++) {
157 bb = sprite.getBBox();
158 minX = Math.min(minX, bb.x);
159 minY = Math.min(minY, bb.y);
160 maxHeight = Math.max(maxHeight, bb.height + bb.y);
161 maxWidth = Math.max(maxWidth, bb.width + bb.x);
168 height: maxHeight - minY,
169 width: maxWidth - minX
174 * Iterates through all sprites calling
175 * `setAttributes` on each one. For more information
176 * {@link Ext.draw.Sprite} provides a description of the
177 * attributes that can be set with this method.
179 setAttributes: function(attrs, redraw) {
184 for (; i < len; i++) {
185 items[i].setAttributes(attrs, redraw);
191 * Hides all sprites. If the first parameter of the method is true
192 * then a redraw will be forced for each sprite.
194 hide: function(redraw) {
199 for (; i < len; i++) {
200 items[i].hide(redraw);
206 * Shows all sprites. If the first parameter of the method is true
207 * then a redraw will be forced for each sprite.
209 show: function(redraw) {
214 for (; i < len; i++) {
215 items[i].show(redraw);
224 surface = me.getSurface(),
228 for (; i < len; i++) {
229 surface.renderItem(items[i]);
235 setStyle: function(obj) {
241 for (; i < len; i++) {
250 addCls: function(obj) {
253 surface = this.getSurface(),
257 for (; i < len; i++) {
258 surface.addCls(items[i], obj);
263 removeCls: function(obj) {
266 surface = this.getSurface(),
270 for (; i < len; i++) {
271 surface.removeCls(items[i], obj);
277 * Grab the surface from the items
279 * @return {Ext.draw.Surface} The surface, null if not found
281 getSurface: function(){
282 var first = this.first();
284 return first.surface;
290 * Destroys the SpriteGroup
294 surface = me.getSurface(),
298 while (me.getCount() > 0) {
301 surface.remove(item);