]> git.proxmox.com Git - sencha-touch.git/blob - src/src/draw/sprite/Instancing.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / src / draw / sprite / Instancing.js
1 /**
2 * @class Ext.draw.sprite.Instancing
3 * @extends Ext.draw.sprite.Sprite
4 *
5 * Sprite that represents multiple instances based on the given template.
6 */
7 Ext.define("Ext.draw.sprite.Instancing", {
8 extend: "Ext.draw.sprite.Sprite",
9 alias: 'sprite.instancing',
10 type: 'instancing',
11 config: {
12
13 /**
14 * @cfg {Object} [template=null] The sprite template used by all instances.
15 */
16 template: null
17 },
18 instances: null,
19 constructor: function (config) {
20 this.instances = [];
21 this.callSuper([config]);
22 if (config && config.template) {
23 this.setTemplate(config.template);
24 }
25 },
26
27 applyTemplate: function (template) {
28 if (!(template instanceof Ext.draw.sprite.Sprite)) {
29 template = Ext.create(template.xclass || "sprite." + template.type, template);
30 }
31 template.setParent(this);
32 template.attr.children = [];
33 this.instances = [];
34 this.position = 0;
35 return template;
36 },
37
38 /**
39 * Creates a new sprite instance.
40 *
41 * @param {Object} config The configuration of the instance.
42 * @param {Object} [data]
43 * @param {Boolean} [bypassNormalization] 'true' to bypass attribute normalization.
44 * @param {Boolean} [avoidCopy] 'true' to avoid copying.
45 * @return {Object} The attributes of the instance.
46 */
47 createInstance: function (config, data, bypassNormalization, avoidCopy) {
48 var template = this.getTemplate(),
49 originalAttr = template.attr,
50 attr = Ext.Object.chain(originalAttr);
51 template.topModifier.prepareAttributes(attr);
52 template.attr = attr;
53 template.setAttributes(config, bypassNormalization, avoidCopy);
54 attr.data = data;
55 this.instances.push(attr);
56 template.attr = originalAttr;
57 this.position++;
58 originalAttr.children.push(attr);
59 return attr;
60 },
61
62 /**
63 * Not supported.
64 *
65 * @return {null}
66 */
67 getBBox: function () { return null; },
68
69 /**
70 * Returns the bounding box for the instance at the given index.
71 *
72 * @param {Number} index The index of the instance.
73 * @param {Boolean} [isWithoutTransform] 'true' to not apply sprite transforms to the bounding box.
74 * @return {Object} The bounding box for the instance.
75 */
76 getBBoxFor: function (index, isWithoutTransform) {
77 var template = this.getTemplate(),
78 originalAttr = template.attr,
79 bbox;
80 template.attr = this.instances[index];
81 bbox = template.getBBox(isWithoutTransform);
82 template.attr = originalAttr;
83 return bbox;
84 },
85
86 render: function (surface, ctx, clipRegion, region) {
87 var me = this,
88 mat = me.attr.matrix,
89 template = me.getTemplate(),
90 originalAttr = template.attr,
91 instances = me.instances,
92 i, ln = me.position;
93
94 mat.toContext(ctx);
95 template.preRender(surface, ctx, clipRegion, region);
96 template.useAttributes(ctx, region);
97 for (i = 0; i < ln; i++) {
98 if (instances[i].dirtyZIndex) {
99 break;
100 }
101 }
102 for (i = 0; i < ln; i++) {
103 if (instances[i].hidden) {
104 continue;
105 }
106 ctx.save();
107 template.attr = instances[i];
108 template.applyTransformations();
109 template.useAttributes(ctx, region);
110 template.render(surface, ctx, clipRegion, region);
111 ctx.restore();
112 }
113 template.attr = originalAttr;
114 },
115
116 /**
117 * Sets the attributes for the instance at the given index.
118 *
119 * @param {Number} index the index of the instance
120 * @param {Object} changes the attributes to change
121 * @param {Boolean} [bypassNormalization] 'true' to avoid attribute normalization
122 */
123 setAttributesFor: function (index, changes, bypassNormalization) {
124 var template = this.getTemplate(),
125 originalAttr = template.attr,
126 attr = this.instances[index];
127 template.attr = attr;
128 try {
129 if (bypassNormalization) {
130 changes = Ext.apply({}, changes);
131 } else {
132 changes = template.self.def.normalize(changes);
133 }
134 template.topModifier.pushDown(attr, changes);
135 template.updateDirtyFlags(attr);
136 } finally {
137 template.attr = originalAttr;
138 }
139 },
140
141 destroy: function () {
142 this.callSuper();
143 this.instances.length = 0;
144 this.instances = null;
145 if (this.getTemplate()) {
146 this.getTemplate().destroy();
147 }
148 }
149 });