UNPKG

52 kBJavaScriptView Raw
1/**
2 * @file Groups of graphic sprites management (2D or 3D). Internally, it manages {@link CB_GraphicSprites} objects. Contains the {@link CB_GraphicSpritesScene} class.
3 * @author Joan Alba Maldonado <workindalian@gmail.com>
4 * @license Creative Commons Attribution 4.0 International. See more at {@link https://crossbrowdy.com/about#what_is_the_crossbrowdy_copyright_and_license}.
5 */
6
7
8/**
9 * An object with the information that belongs to a group of graphic sprites.
10 * @example
11 {
12 //'my_sprites_scene_1':
13 id: "my_sprites_scene_1",
14 srcType: CB_GraphicSprites.SRC_TYPES.IMAGE,
15 srcLeft: 10,
16 srcTop: 20,
17 srcWidth: 64,
18 srcHeight: 32,
19 left: 10,
20 top: 20,
21 width: 64,
22 height: 32,
23 data: { datum_1: "value_1", datum_2: 2, datum_3: [ "a", "b", "c" ] },
24 //Sprites groups:
25 spritesGroups:
26 [
27 //'my_sprites_1':
28 {
29 id: "my_sprites_1",
30 src: "path/to/image.gif",
31 sprites:
32 [
33 //'my_sprite_1':
34 {
35 id: "my_sprite_1",
36 subSprites:
37 [
38 //'my_subsprite_1':
39 { id: "my_subsprite_1", srcLeft: 10, srcTop: 20, zIndex: 1 },
40 //'my_subsprite_2':
41 { id: "my_subsprite_2", srcLeft: 20, srcTop: 40, zIndex: 2 }
42 ]
43 },
44 //'my_sprite_2':
45 {
46 id: "my_sprite_2",
47 subSprites:
48 [
49 //'my_subsprite_3':
50 { id: "my_subsprite_3", srcLeft: 30, srcTop: 60, zIndex: 1 },
51 //'my_subsprite_4':
52 { id: "my_subsprite_4", srcLeft: 40, srcTop: 80, zIndex: 2 }
53 ]
54 }
55 ]
56 },
57 //'my_sprites_2':
58 {
59 id: "my_sprites_2",
60 src: "path/to/image2.gif",
61 sprites:
62 [
63 //'my_sprite_3':
64 {
65 id: "my_sprite_3",
66 subSprites:
67 [
68 //'my_subsprite_1':
69 { id: "my_subsprite_5", srcLeft: 10, srcTop: 20, zIndex: 1 },
70 //'my_subsprite_2':
71 { id: "my_subsprite_6", srcLeft: 20, srcTop: 40, zIndex: 2 }
72 ]
73 },
74 //'my_sprite_4':
75 {
76 id: "my_sprite_4",
77 subSprites:
78 [
79 //'my_subsprite_3':
80 { id: "my_subsprite_7", srcLeft: 30, srcTop: 60, zIndex: 1 },
81 //'my_subsprite_4':
82 { id: "my_subsprite_8", srcLeft: 40, srcTop: 80, zIndex: 2 }
83 ]
84 }
85 ]
86 }
87 ]
88 }
89 * @memberof CB_GraphicSpritesScene
90 * @typedef {Object} CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT
91 * @property {string|*} [id='CB_GraphicSpritesScene_' + CB_GraphicSpritesScene._idUnique++] - Identifier of the sprites groups object and also for the graphic sprites scene (also used as the {@link CB_GraphicSprites.id} property for the {@link CB_GraphicSpritesScene} object). It should be unique. Recommended. It must be a value which evaluates to true. By default, it is generated automatically (with an internal counter).
92 * @property {*} [src=""] - The value for the "src" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
93 * @property {string} [srcType={@link CB_GraphicSprites.SRC_TYPES_DEFAULT}] - The value for the "srcType" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects. It should point to a property of the {@link CB_GraphicSprites.SRC_TYPES} object. You can use other values of the {@link CB_GraphicSprites.SRC_TYPES} object or create new ones.
94 * @property {number} [srcLeft={@link CB_GraphicSprites.LEFT_SOURCE_DEFAULT}] - The value for the "srcLeft" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
95 * @property {number} [srcTop={@link CB_GraphicSprites.TOP_SOURCE_DEFAULT}] - The value for the "srcTop" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
96 * @property {number} [srcWidth={@link CB_GraphicSprites.WIDTH_SOURCE_DEFAULT}] - The value for the "srcWidth" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
97 * @property {number} [srcHeight={@link CB_GraphicSprites.HEIGHT_SOURCE_DEFAULT}] - The value for the "srcHeight" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
98 * @property {number} [left={@link CB_GraphicSprites.LEFT_DEFAULT}] - The value for the "left" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
99 * @property {number} [top={@link CB_GraphicSprites.TOP_DEFAULT}] - The value for the "top" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
100 * @property {number} [width={@link CB_GraphicSprites.WIDTH_DEFAULT}] - The value for the "width" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
101 * @property {number} [height={@link CB_GraphicSprites.HEIGHT_DEFAULT}] - The value for the "height" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
102 * @property {number} [zIndex={@link CB_GraphicSprites.ZINDEX_DEFAULT}] - The value for the "zIndex" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
103 * @property {boolean} [disabled=false] - The value for the "disabled" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
104 * @property {object} [data={ 'that' : CB_GraphicSprites.SPRITES_GROUPS_OBJECT, 'getThis' = function() { return this.that; } }] - Object with any additional data desired which can be any kind. It will always have a "that" property pointing to the {@link CB_GraphicSprites.SPRITES_GROUPS_OBJECT} object where it belongs to and a function in its "getThis" property returning the same value (added automatically). These properties ("that" and "getThis") cannot be overridden. The value given will also be used for the "data" property which will be used as default if not provided (or the provided one was wrong) in the given {@link CB_GraphicSprites.SPRITES_OBJECT} objects (in the "spritesGroups" property), when creating the internal {@link CB_GraphicSprites} objects.
105 * @property {array} [spritesGroups=[]] - Numeric array containing {@link CB_GraphicSprites.SPRITES_OBJECT} objects with all the sprites groups that are useful for creating the internal {@link CB_GraphicSprites} objects. Recommended at least to provide one {@link CB_GraphicSprites.SPRITES_OBJECT} object in the first index.
106 * @property {*} [parent=undefined] - Property pointing to or containing its parent (also used as the {@link CB_GraphicSpritesScene.parent} property for the {@link CB_GraphicSpritesScene} object).
107 * @property {array} items - Read-only numeric array containing {@link CB_GraphicSprites} objects created internally. Their "parent" property will be set to point the current {@link CB_GraphicSpritesScene} object which contains them.
108 * @property {array} itemsByZIndex - Read-only property containing a numeric array of all the {@link CB_GraphicSprites} objects ordered by their z-index ("zIndex" property). It is updated automatically when the z-index of a sprite is set with its {@link CB_GraphicSprites#setZIndex} method or when inserting/removing {@link CB_GraphicSprites} objects through the {@link CB_GraphicSpritesScene#insertSpritesGroups}, {@link CB_GraphicSpritesScene#insertSpritesGroup}, {@link CB_GraphicSpritesScene#removeSpritesGroup} or {@link CB_GraphicSpritesScene#removeSpritesGroupById} methods.
109 * @property {boolean} [byReference_DEFAULT=false] - Default value to use as the "byReference" parameter for the constructor and for the {@link CB_GraphicSpritesScene#insertSpritesGroups} method. If a boolean value is not provided, it will be parsed to boolean (resulting undefined to be false).
110 * @property {CB_GraphicSpritesScene} container - Read-only property pointing to the {@link CB_GraphicSpritesScene} object which contains it.
111 * @property {boolean} isSpritesGroups - Read-only property which is always set to true to help identify this type of object.
112 * @property {'spritesGroups'} type - Read-only property indicating the type of object (always "spritesGroups").
113 */
114
115
116/**
117 * Class to manage different groups of graphic sprites (2D or 3D). Internally, it manages {@link CB_GraphicSprites} objects.
118 * @class
119 * @classdesc Class to manage different groups of graphic sprites (2D or 3D). Internally, it manages {@link CB_GraphicSprites} objects.
120 * @param {CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} [spritesGroups] - Object with the desired groups of sprites. The information will be used for the {@link CB_GraphicSpritesScene#spritesGroups} property. Used as the "spritesGroups" parameter when calling the {@link CB_GraphicSpritesScene#insertSpritesGroups} method internally.
121 * @param {boolean} [byReference=false] - This value will be used as the "byReference" parameter of the constructor when creating the new internal {@link CB_GraphicSprites} objects. If a boolean value is not provided, it will use the value of the {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT.byReference_DEFAULT} property of the given {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} object (parsed to boolean).
122 * @returns {CB_GraphicSpritesScene} Returns a new {@link CB_GraphicSpritesScene} object.
123 * @todo Think about a "createCopy" parameter on different the insert methods (to insert sprites groups/graphic sprites objects, etc.) so it will make a copy of the object to avoid using/modifying the original one. If the "createCopy" is set to false, it should always use the object as reference (using/modifying it).
124 * @todo Think about a method to remove a sprite group object when the same sprite group is received by parameter. The same to remove a {@link CB_GraphicSprites} object, receiving a {@link CB_GraphicSprites} object by parameter. The same to remove the sprites groups object, receiving a sprites groups object by parameter. Only remove them if they match exactly.
125 * @todo Think about a method to insert {@link CB_GraphicSprites} object directly. The same with a method that inserts many {@link CB_GraphicSprites} objects (receiving an array with them).
126 */
127var CB_GraphicSpritesScene = function(spritesGroups, byReference)
128{
129 //Creates an instance of this object and returns it in the case that it is being called from an unexpected context:
130 if (this === window || !(this instanceof CB_GraphicSpritesScene)) { return new CB_GraphicSpritesScene(spritesGroups, byReference); }
131
132 //Properties and variables:
133 /**
134 * Identifier of the sprites groups object (the "id" property of the {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} stored in the {@link CB_GraphicSpritesScene#spritesGroups} property) and the {@link CB_GraphicSpritesScene} object itself (same one). It should be unique. It must be a value which evaluates to true. By default, it is generated automatically (with an internal counter).
135 * @var
136 * @readonly
137 * @type {string|*}
138 * @default 'CB_GraphicSpritesScene_' + CB_GraphicSpritesScene._idUnique++
139 */
140 this.id = "";
141
142 /**
143 * Property pointing to or containing its parent. It is the same as the "parent" property of the {@link CB_GraphicSprites.SPRITES_GROUPS_OBJECT} stored in the {@link CB_GraphicSprites#spritesGroups} property.
144 * @var
145 * @readonly
146 * @type {*}
147 * @default
148 */
149 this.parent = undefined;
150
151
152 /**
153 * Object containing all the internally-created {@link CB_GraphicSprites} objects and their information.
154 * @var
155 * @readonly
156 * @type {SPRITES_GROUPS_OBJECT}
157 * @default
158 */
159 this.spritesGroups = {};
160
161 //Calls the constructor of the object when creates an instance:
162 return this._init(spritesGroups, byReference);
163}
164
165
166//Constants:
167/**
168 * Property which is always set to true to help identify this type of object.
169 * @constant
170 * @type {boolean}
171 * @default
172 */
173CB_GraphicSpritesScene.prototype.isSpritesScene = true;
174
175
176/**
177 * Indicates the type of object (always "spritesScene").
178 * @constant
179 * @type {string}
180 * @default
181 */
182CB_GraphicSpritesScene.prototype.type = "spritesScene";
183
184
185//Variables:
186CB_GraphicSpritesScene._idUnique = 0; //Counter to make the sprites group id unique.
187
188
189//Constructor:
190CB_GraphicSpritesScene.prototype._init = function(spritesGroups, byReference)
191{
192 this.insertSpritesGroups(spritesGroups, byReference);
193 return this;
194}
195
196
197/**
198 * Destroys the graphic sprites scene object (removing all the sprites groups and the internal {@link CB_GraphicSprites} objects, etc.) and frees memory.
199 * @function
200 */
201CB_GraphicSpritesScene.prototype.destructor = function()
202{
203 //Destroys all the internal CB_GraphicSprites objects:
204 this.executeFunctionAll(function() { this.destructor(); });
205
206 //Resets properties to their default value:
207 this.removeSpritesGroups();
208}
209
210
211/**
212 * Alias for {@link CB_GraphicSpritesScene#removeSpritesGroups}.
213 * @function CB_GraphicSpritesScene#removeAll
214 * @see {@link CB_GraphicSpritesScene#removeSpritesGroups}
215 */
216/**
217 * Alias for {@link CB_GraphicSpritesScene#removeSpritesGroups}.
218 * @function CB_GraphicSpritesScene#removeSpritesGroupsAll
219 * @see {@link CB_GraphicSpritesScene#removeSpritesGroups}
220 */
221/**
222 * Removes all the sprites groups (and all the {@link CB_GraphicSprites} objects) by clearing the {@link CB_GraphicSprites#spritesGroups} property.
223 * @function
224 */
225CB_GraphicSpritesScene.prototype.removeSpritesGroups = CB_GraphicSpritesScene.prototype.removeSpritesGroupsAll = CB_GraphicSpritesScene.prototype.removeAll = function()
226{
227 this.spritesGroups = {};
228}
229
230
231/**
232 * Adds the desired groups of graphic sprites. Calls the {@link CB_GraphicSpritesScene#insertSpritesGroup} and {@link CB_GraphicSpritesScene#updateGraphicSpritesByZIndex} methods internally.
233 * @function
234 * @param {CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} [spritesGroups] - Object with the desired sprites groups. They will be stored in the "{@link CB_GraphicSpritesScene#spritesGroups}.spritesGroups" property and the {@link CB_GraphicSprites} objects created internally will be stored in the "{@link CB_GraphicSpritesScene#spritesGroups}.items" property.
235 * @param {boolean} [byReference=!!{@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT.byReference_DEFAULT}] - This value will be used as the "byReference" parameter of the constructor when creating the new internal {@link CB_GraphicSprites} objects. If a boolean value is not provided, it will use the value of the {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT.byReference_DEFAULT} property of the given {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} object (parsed to boolean).
236 * @returns {CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} Returns the {@link CB_GraphicSpritesScene#spritesGroups} property after updating it.
237 */
238CB_GraphicSpritesScene.prototype.insertSpritesGroups = function(spritesGroups, byReference)
239{
240 //Sets the properties (sanitizing them):
241 this.spritesGroups = this.spritesGroups || {};
242 spritesGroups = spritesGroups || {};
243 this.spritesGroups.isSpritesGroups = true;
244 this.spritesGroups.type = "spritesGroups";
245 this.spritesGroups.container = this;
246 this.parent = this.spritesGroups.parent = spritesGroups.parent;
247 this.id = this.spritesGroups.id = spritesGroups.id = spritesGroups.id || "CB_GraphicSpritesScene_" + CB_GraphicSpritesScene._idUnique++;
248 this.spritesGroups.src = spritesGroups.src = spritesGroups.src || !isNaN(spritesGroups.src) && spritesGroups.src !== null ? spritesGroups.src : "";
249 this.spritesGroups.srcType = spritesGroups.srcType = spritesGroups.srcType || CB_GraphicSprites.SRC_TYPES_DEFAULT;
250 spritesGroups.srcLeft = parseFloat(spritesGroups.srcLeft);
251 this.spritesGroups.srcLeft = spritesGroups.srcLeft = !isNaN(spritesGroups.srcLeft) ? spritesGroups.srcLeft : parseFloat(CB_GraphicSprites.LEFT_SOURCE_DEFAULT) || 0;
252 spritesGroups.left = parseFloat(spritesGroups.left);
253 this.spritesGroups.left = spritesGroups.left = !isNaN(spritesGroups.left) ? spritesGroups.left : parseFloat(CB_GraphicSprites.LEFT_DEFAULT) || 0;
254 spritesGroups.srcTop = parseFloat(spritesGroups.srcTop);
255 this.spritesGroups.srcTop = spritesGroups.srcTop = !isNaN(spritesGroups.srcTop) ? spritesGroups.srcTop : parseFloat(CB_GraphicSprites.TOP_SOURCE_DEFAULT) || 0;
256 spritesGroups.top = parseFloat(spritesGroups.top);
257 this.spritesGroups.top = spritesGroups.top = !isNaN(spritesGroups.top) ? spritesGroups.top : parseFloat(CB_GraphicSprites.TOP_DEFAULT) || 0;
258 spritesGroups.srcWidth = parseFloat(spritesGroups.srcWidth);
259 this.spritesGroups.srcWidth = spritesGroups.srcWidth = !isNaN(spritesGroups.srcWidth) ? spritesGroups.srcWidth : parseFloat(CB_GraphicSprites.WIDTH_SOURCE_DEFAULT) || 0;
260 spritesGroups.width = parseFloat(spritesGroups.width);
261 this.spritesGroups.width = spritesGroups.width = !isNaN(spritesGroups.width) ? spritesGroups.width : parseFloat(CB_GraphicSprites.WIDTH_DEFAULT) || 0;
262 spritesGroups.srcHeight = parseFloat(spritesGroups.srcHeight);
263 this.spritesGroups.srcHeight = spritesGroups.srcHeight = !isNaN(spritesGroups.srcHeight) ? spritesGroups.srcHeight : parseFloat(CB_GraphicSprites.HEIGHT_SOURCE_DEFAULT) || 0;
264 spritesGroups.height = parseFloat(spritesGroups.height);
265 this.spritesGroups.height = spritesGroups.height = !isNaN(spritesGroups.height) ? spritesGroups.height : parseFloat(CB_GraphicSprites.HEIGHT_DEFAULT) || 0;
266 spritesGroups.zIndex = parseFloat(spritesGroups.zIndex);
267 this.spritesGroups.zIndex = spritesGroups.zIndex = !isNaN(spritesGroups.zIndex) ? spritesGroups.zIndex : parseFloat(CB_GraphicSprites.ZINDEX_DEFAULT) || 0;
268 this.spritesGroups.data = typeof(spritesGroups.data) === "object" && spritesGroups.data !== null ? CB_copyObject(spritesGroups.data, false) : {}; //Accepts any object but not other values.
269 this.spritesGroups.data.that = this.spritesGroups;
270 this.spritesGroups.data.getThis = function() { return this.that; };
271
272 spritesGroups.spritesGroups = CB_isArray(spritesGroups.spritesGroups) ? spritesGroups.spritesGroups : [];
273
274 //Inserts the given sprites groups, one by one:
275 byReference = (byReference === true || byReference === false) ? byReference : !!spritesGroups.byReference_DEFAULT;
276 var spritesGroupsLength = spritesGroups.spritesGroups.length;
277 for (var x = 0; x < spritesGroupsLength; x++)
278 {
279 this.insertSpritesGroup(spritesGroups.spritesGroups[x], true, byReference);
280 }
281
282 //Updates the array with the CB_GraphicSprites ordered by z-index:
283 this.updateGraphicSpritesByZIndex();
284
285 //Returns the sprites:
286 return this.spritesGroups;
287}
288
289
290/**
291 * Alias for {@link CB_GraphicSpritesScene#updateGraphicSpritesByZIndex}.
292 * @function CB_GraphicSpritesScene#updateItemsByZIndex
293 * @see {@link CB_GraphicSpritesScene#updateGraphicSpritesByZIndex}
294 */
295/**
296 * Updates (sorts again) the "itemsByZIndex" property (which is an array with the {@link CB_GraphicSprites} objects ordered by z-index, whose data comes from the array in the "items" property of the {@link CB_GraphicSpritesScene#spritesGroups} object) of the {@link CB_GraphicSpritesScene#spritesGroups} object.
297 * @function
298 * @returns {array} Returns the "itemsByZIndex" array of the {@link CB_GraphicSpritesScene#spritesGroups} object after updating it. Returns null if the property could not be obtained or updated.
299 */
300CB_GraphicSpritesScene.prototype.updateGraphicSpritesByZIndex = CB_GraphicSpritesScene.prototype.updateItemsByZIndex = function()
301{
302 this.spritesGroups.items = this.spritesGroups.items || null;
303 if (this.spritesGroups.items)
304 {
305 var spritesGroupLength = this.spritesGroups.items.length;
306 if (spritesGroupLength)
307 {
308 var elementIndex = null;
309 this.spritesGroups.itemsByZIndex = [];
310 for (var x = 0; x < spritesGroupLength; x++)
311 {
312 elementIndex = CB_GraphicSpritesScene._choosePositionByZIndex(this.spritesGroups.itemsByZIndex, this.spritesGroups.items[x]);
313 this.spritesGroups.itemsByZIndex = CB_Arrays.insertElement(this.spritesGroups.itemsByZIndex, elementIndex, this.spritesGroups.items[x]);
314 this.spritesGroups.items[x].positionByZIndex = elementIndex;
315 }
316 return this.spritesGroups.itemsByZIndex;
317 }
318 }
319 return null;
320}
321
322
323/**
324 * Alias for {@link CB_GraphicSpritesScene#removeSpritesGroup}.
325 * @function CB_GraphicSpritesScene#remove
326 * @see {@link CB_GraphicSpritesScene#removeSpritesGroup}
327 */
328/**
329 * Alias for {@link CB_GraphicSpritesScene#removeSpritesGroup}.
330 * @function CB_GraphicSpritesScene#removeGraphicSprites
331 * @see {@link CB_GraphicSpritesScene#removeSpritesGroup}
332 */
333/**
334 * Removes a sprites group and its {@link CB_GraphicSprites} object by its index (its position in the {@link CB_GraphicSpritesScene#spritesGroups.items} array). Calls the {@link CB_GraphicSpritesScene#updateGraphicSpritesByZIndex} method internally.
335 * @function
336 * @param {integer} [index=0] - The index where the {@link CB_GraphicSprites} object is located (its position in the {@link CB_GraphicSpritesScene#spritesGroups.items} array).
337 * @returns {boolean} Returns true if the {@link CB_GraphicSprites} object has been deleted from the graphic sprites scene object or false otherwise.
338 */
339CB_GraphicSpritesScene.prototype.removeSpritesGroup = CB_GraphicSpritesScene.prototype.removeGraphicSprites = CB_GraphicSpritesScene.prototype.remove = function(index)
340{
341 var removed = false;
342 var spritesGroupsLeft = CB_Arrays.removeElementByPosition(this.spritesGroups.items, index, function() { removed = true; });
343 if (removed)
344 {
345 this.spritesGroups.spritesGroups = CB_Arrays.removeElementByPosition(this.spritesGroups.spritesGroups, index);
346 this.spritesGroups.items = spritesGroupsLeft;
347
348 //Updates the array with the CB_GraphicSprites ordered by z-index:
349 this.updateGraphicSpritesByZIndex();
350 }
351 return removed;
352}
353
354
355/**
356 * Alias for {@link CB_GraphicSpritesScene#removeSpritesGroupById}.
357 * @function CB_GraphicSpritesScene#removeById
358 * @see {@link CB_GraphicSpritesScene#removeSpritesGroupById}
359 */
360/**
361 * Alias for {@link CB_GraphicSpritesScene#removeSpritesGroupById}.
362 * @function CB_GraphicSpritesScene#removeGraphicSpritesById
363 * @see {@link CB_GraphicSpritesScene#removeSpritesGroupById}
364 */
365/**
366 * Removes a sprites group and its {@link CB_GraphicSprites} object by its identifier. Calls the {@link CB_GraphicSpritesScene#updateGraphicSpritesByZIndex} method internally.
367 * @function
368 * @param {string|*} [id=undefined] - The identifier of the {@link CB_GraphicSprites} object.
369 * @returns {boolean} Returns true if the {@link CB_GraphicSprites} object has been deleted from the graphic sprites scene object or false otherwise.
370 * @todo Optimize it (perhaps using a cache matching the IDs with their position, maybe using the "position" or "positionByZIndex" properties).
371 */
372CB_GraphicSpritesScene.prototype.removeSpritesGroupById = CB_GraphicSpritesScene.prototype.removeGraphicSpritesById = CB_GraphicSpritesScene.prototype.removeById = function(id)
373{
374 var removed = false;
375 var index = null;
376 var spritesGroupsLeft = CB_Arrays.removeDuplicated(this.spritesGroups.items, function(value, position, array) { if (value && value.id === id) { removed = true; index = position; return true; }; return false; }, true);
377 if (removed)
378 {
379 if (index !== null) { this.spritesGroups.spritesGroups = CB_Arrays.removeElementByPosition(this.spritesGroups.spritesGroups, index); }
380 this.spritesGroups.items = spritesGroupsLeft;
381
382 //Updates the array with the CB_GraphicSprites ordered by z-index:
383 this.updateGraphicSpritesByZIndex();
384 }
385 return removed;
386}
387
388
389/**
390 * Object used as the returning value of the {@link CB_GraphicSpritesScene#insertSpritesGroup} method.
391 * @memberof CB_GraphicSpritesScene
392 * @typedef {Object} CB_GraphicSpritesScene.insertSpritesGroup_OBJECT
393 * @property {CB_GraphicSprites.SPRITES_OBJECT} spritesGroup - The {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} which has been inserted (it could have been modified/sanitized from the given one and some missing properties or those which were wrong could have been inherited) and was used to create the internal {@link CB_GraphicSprites} object when calling its constructor.
394 * @property {CB_GraphicSprites} item - The {@link CB_GraphicSprites} object created and inserted from the {@link CB_GraphicSprites.SPRITES_OBJECT}.
395 */
396
397/**
398 * Alias for {@link CB_GraphicSpritesScene#insertSpritesGroup}.
399 * @function CB_GraphicSpritesScene#insert
400 * @see {@link CB_GraphicSpritesScene#insertSpritesGroup}
401 */
402/**
403 * Adds the desired group of graphic sprites. Creates internal {@link CB_GraphicSprites} objects.
404 * @function
405 * @param {CB_GraphicSprites.SPRITES_OBJECT} [spritesGroup] - Object with the desired sprites group. It will be stored in the "{@link CB_GraphicSpritesScene#spritesGroups}.spritesGroups" property and the {@link CB_GraphicSprites} object created internally will be stored in the "{@link CB_GraphicSpritesScene#spritesGroups}.items" property.
406 * @param {boolean} [avoidUpdatingGraphicSpritesByZIndex=false] - If set to true, it will not call the {CB_GraphicSpritesScene#updateGraphicSpritesByZIndex} method internally. Internal usage recommended only.
407 * @param {boolean} [byReference=false] - This value will be used as the "byReference" parameter of the constructor when creating the new internal {@link CB_GraphicSprites} objects.
408 * @returns {CB_GraphicSpritesScene.insertSpritesGroup_OBJECT} Returns an object whose "spritesGroup" property contains the {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} which has been inserted (it could have been modified/sanitized from the given one and some missing properties or those which were wrong could have been inherited) and was used to create the internal {@link CB_GraphicSprites} object when calling its constructor and the "item" property contains the {@link CB_GraphicSprites} object created and inserted.
409 */
410CB_GraphicSpritesScene.prototype.insertSpritesGroup = CB_GraphicSpritesScene.prototype.insert = function(spritesGroup, avoidUpdatingGraphicSpritesByZIndex, byReference)
411{
412 //Sets the properties (sanitizing them):
413 this.spritesGroups = this.spritesGroups || {};
414 spritesGroup = spritesGroup || {};
415 spritesGroup.parent = this; //Overwrites the parent to point to the CB_GraphicSprites itself which will contain it.
416 spritesGroup.src = spritesGroup.src || !isNaN(spritesGroup.src) && spritesGroup.src !== null ? spritesGroup.src : this.spritesGroups.src || !isNaN(this.spritesGroups.src) && this.spritesGroups.src !== null ? this.spritesGroups.src : "";
417 spritesGroup.srcType = spritesGroup.srcType || this.spritesGroups.srcType || CB_GraphicSprites.SRC_TYPES_DEFAULT;
418 spritesGroup.srcLeft = parseFloat(spritesGroup.srcLeft);
419 spritesGroup.srcLeft = !isNaN(spritesGroup.srcLeft) ? spritesGroup.srcLeft : parseFloat(this.spritesGroups.srcLeft);
420 spritesGroup.srcLeft = !isNaN(spritesGroup.srcLeft) ? spritesGroup.srcLeft : parseFloat(CB_GraphicSprites.LEFT_SOURCE_DEFAULT) || 0;
421 spritesGroup.left = parseFloat(spritesGroup.left);
422 spritesGroup.left = !isNaN(spritesGroup.left) ? spritesGroup.left : parseFloat(this.spritesGroups.left);
423 spritesGroup.left = !isNaN(spritesGroup.left) ? spritesGroup.left : parseFloat(CB_GraphicSprites.LEFT_DEFAULT) || 0;
424 spritesGroup.srcTop = parseFloat(spritesGroup.srcTop);
425 spritesGroup.srcTop = !isNaN(spritesGroup.srcTop) ? spritesGroup.srcTop : parseFloat(this.spritesGroups.srcTop);
426 spritesGroup.srcTop = !isNaN(spritesGroup.srcTop) ? spritesGroup.srcTop : parseFloat(CB_GraphicSprites.TOP_SOURCE_DEFAULT) || 0;
427 spritesGroup.top = parseFloat(spritesGroup.top);
428 spritesGroup.top = !isNaN(spritesGroup.top) ? spritesGroup.top : parseFloat(this.spritesGroups.top);
429 spritesGroup.top = !isNaN(spritesGroup.top) ? spritesGroup.top : parseFloat(CB_GraphicSprites.TOP_DEFAULT) || 0;
430 spritesGroup.srcWidth = parseFloat(spritesGroup.srcWidth);
431 spritesGroup.srcWidth = !isNaN(spritesGroup.srcWidth) ? spritesGroup.srcWidth : parseFloat(this.spritesGroups.srcWidth);
432 spritesGroup.srcWidth = !isNaN(spritesGroup.srcWidth) ? spritesGroup.srcWidth : parseFloat(CB_GraphicSprites.WIDTH_SOURCE_DEFAULT) || 0;
433 spritesGroup.width = parseFloat(spritesGroup.width);
434 spritesGroup.width = !isNaN(spritesGroup.width) ? spritesGroup.width : parseFloat(this.spritesGroups.width);
435 spritesGroup.width = !isNaN(spritesGroup.width) ? spritesGroup.width : parseFloat(CB_GraphicSprites.WIDTH_DEFAULT) || 0;
436 spritesGroup.srcHeight = parseFloat(spritesGroup.srcHeight);
437 spritesGroup.srcHeight = !isNaN(spritesGroup.srcHeight) ? spritesGroup.srcHeight : parseFloat(this.spritesGroups.srcHeight);
438 spritesGroup.srcHeight = !isNaN(spritesGroup.srcHeight) ? spritesGroup.srcHeight : parseFloat(CB_GraphicSprites.HEIGHT_SOURCE_DEFAULT);
439 spritesGroup.height = parseFloat(spritesGroup.height);
440 spritesGroup.height = !isNaN(spritesGroup.height) ? spritesGroup.height : parseFloat(this.spritesGroups.height);
441 spritesGroup.height = !isNaN(spritesGroup.height) ? spritesGroup.height : parseFloat(CB_GraphicSprites.HEIGHT_DEFAULT) || 0;
442 spritesGroup.zIndex = parseFloat(spritesGroup.zIndex);
443 spritesGroup.zIndex = !isNaN(spritesGroup.zIndex) ? spritesGroup.zIndex : parseFloat(this.spritesGroups.zIndex);
444 spritesGroup.zIndex = !isNaN(spritesGroup.zIndex) ? spritesGroup.zIndex : parseFloat(CB_GraphicSprites.ZINDEX_DEFAULT) || 0;
445 spritesGroup.disabled = !!spritesGroup.disabled || false;
446 spritesGroup.data = typeof(spritesGroup.data) === "object" && spritesGroup.data !== null ? spritesGroup.data : {}; //this.spritesGroups.data;
447 if (typeof(this.spritesGroups.data) === "object" && this.spritesGroups.data !== null) { spritesGroup.data = CB_combineJSON(this.spritesGroups.data, spritesGroup.data); } //Combine objects.
448 spritesGroup.data = typeof(spritesGroup.data) === "object" && spritesGroup.data !== null ? CB_copyObject(spritesGroup.data, false) : {}; //Note: the CB_GraphicSprites class will add the "that" and "getThis" properties to the "data".
449
450 //Creates and inserts the sprites group:
451 this.spritesGroups.items = this.spritesGroups.items || [];
452 this.spritesGroups.spritesGroups = this.spritesGroups.spritesGroups || [];
453 var item = new CB_GraphicSprites(spritesGroup, byReference);
454 var position = this.getGraphicSpritesIndexById(item.id); //If there is a sprites group with the same ID, it will be replaced by the new one (in the same position).
455 position = position !== -1 ? position : this.spritesGroups.items.length;
456 item.position = position;
457 this.spritesGroups.items[position] = item;
458 spritesGroup.isSpritesGroup = true; //Adds the "isSpritesGroup" property to the sprites groups object.
459 spritesGroup.type = "spritesGroup"; //Adds the "type" property to the sprites groups object.
460 this.spritesGroups.spritesGroups[position] = spritesGroup; //Overrides the sprites groups object, sanitized.
461
462 if (!avoidUpdatingGraphicSpritesByZIndex) { this.updateGraphicSpritesByZIndex(); }
463
464 //Returns the sprites:
465 return { "spritesGroup" : spritesGroup, "item" : item };
466}
467
468
469//Returns the position that an element (subsprites group, sprite or sub-sprite) should have in a given array, having in mind that they are sorted by by z-index in that array (ascending order):
470CB_GraphicSpritesScene._choosePositionByZIndex = function(array, element)
471{
472 if (array && element)
473 {
474 var arrayLength = array.length;
475 if (arrayLength)
476 {
477 for (var x = 0; x < arrayLength; x++)
478 {
479 if (array[x] && array[x].zIndex > element.zIndex) { return x; }
480 }
481 return array.length; //By default, best position is at the end of the array (as a new element).
482 }
483 }
484 return 0;
485}
486
487
488/**
489 * Alias for {@link CB_GraphicSpritesScene#getSpritesGroups}.
490 * @function CB_GraphicSpritesScene#getSpritesGroupsAll
491 * @see {@link CB_GraphicSpritesScene#getSpritesGroups}
492 */
493/**
494 * Gets the sprites groups object (the {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} object which is in the {@link CB_GraphicSpritesScene#spritesGroups} property), if any.
495 * @function
496 * @param {*} [returnValueOnFail=undefined] - The value we want it to return in the case that no value is found. If not provided, undefined will be returned.
497 * @returns {CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT|*} Returns the sprites groups object (the {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} object which is in the {@link CB_GraphicSpritesScene#spritesGroups} property), if any, or the value of "returnValueOnFail" otherwise.
498 */
499CB_GraphicSpritesScene.prototype.getSpritesGroups = CB_GraphicSpritesScene.prototype.getSpritesGroupsAll = function(returnValueOnFail)
500{
501 return this.spritesGroups ? this.spritesGroups : returnValueOnFail;
502}
503
504
505/**
506 * Alias for {@link CB_GraphicSpritesScene#getGraphicSpritesAll}.
507 * @function CB_GraphicSpritesScene#getAll
508 * @see {@link CB_GraphicSpritesScene#getGraphicSpritesAll}
509 */
510/**
511 * Gets all the sprites graphic objects (the "items" property of the internal {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} object, if any).
512 * @function
513 * @param {boolean} [orderedByZIndex=false] - If set to true, it will return the {@link CB_GraphicSprites} objects sorted by their z-index (ascending order).
514 * @param {*} [returnValueOnFail=undefined] - The value we want it to return in the case that no value is found. If not provided, undefined will be returned.
515 * @returns {array|*} Returns an array with all the {@link CB_GraphicSprites} objects or the value of "returnValueOnFail" otherwise.
516 */
517CB_GraphicSpritesScene.prototype.getGraphicSpritesAll = CB_GraphicSpritesScene.prototype.getAll = function(orderedByZIndex, returnValueOnFail)
518{
519 if (this.spritesGroups)
520 {
521 if (!orderedByZIndex)
522 {
523 return (this.spritesGroups.items) ? this.spritesGroups.items : returnValueOnFail;
524 }
525 else
526 {
527 return (this.spritesGroups.itemsByZIndex) ? this.spritesGroups.itemsByZIndex : returnValueOnFail;
528 }
529 }
530 return returnValueOnFail;
531}
532
533
534/**
535 * Alias for {@link CB_GraphicSpritesScene#getGraphicSprites}.
536 * @function CB_GraphicSpritesScene#get
537 * @see {@link CB_GraphicSpritesScene#getGraphicSprites}
538 */
539/**
540 * Gets a desired {@link CB_GraphicSprites} object through its index (its position in the {@link CB_GraphicSpritesScene#spritesGroups.items} array). Faster than getting it through its identifier with the {@link CB_GraphicSpritesScene#getGraphicSpritesById} method.
541 * @function
542 * @param {integer} [index=0] - The index where the desired {@link CB_GraphicSprites} object must be located (its position in the {@link CB_GraphicSpritesScene#spritesGroups.items} array).
543 * @param {*} [returnValueOnFail=undefined] - The value we want it to return in the case that no value is found. If not provided, undefined will be returned.
544 * @returns {CB_GraphicSprites|*} Returns a {@link CB_GraphicSprites} object if found or the value of "returnValueOnFail" otherwise.
545 */
546CB_GraphicSpritesScene.prototype.getGraphicSprites = CB_GraphicSpritesScene.prototype.get = function(index, returnValueOnFail)
547{
548 index = parseInt(index);
549 index = isNaN(index) ? 0 : index;
550 if (index < 0) { index *= -1; } //It must be a positive integer.
551 return this.spritesGroups && this.spritesGroups.items && this.spritesGroups.items[index] ? this.spritesGroups.items[index] : returnValueOnFail;
552}
553
554
555/**
556 * Alias for {@link CB_GraphicSpritesScene#getGraphicSpritesById}.
557 * @function CB_GraphicSpritesScene#getById
558 * @see {@link CB_GraphicSpritesScene#getGraphicSpritesById}
559 */
560/**
561 * Gets a desired {@link CB_GraphicSprites} object through its identifier. Slower than getting it through its index with the {@link CB_GraphicSpritesScene#getGraphicSprites} method.
562 * @function
563 * @param {string|*} [id=undefined] - The identifier of the {@link CB_GraphicSprites} object that we want to get.
564 * @param {*} [returnValueOnFail=undefined] - The value we want it to return in the case that no value is found. If not provided, undefined will be returned.
565 * @returns {CB_GraphicSprites|*} Returns a {@link CB_GraphicSprites} object if found or the value of "returnValueOnFail" otherwise.
566 */
567CB_GraphicSpritesScene.prototype.getGraphicSpritesById = CB_GraphicSpritesScene.prototype.getById = function(id, returnValueOnFail)
568{
569 var index = this.getGraphicSpritesIndexById(id);
570 return index !== -1 ? this.spritesGroups.items[index] : returnValueOnFail;
571}
572
573
574/**
575 * Alias for {@link CB_GraphicSpritesScene#getGraphicSpritesIndexById}.
576 * @function CB_GraphicSpritesScene#getIndexById
577 * @see {@link CB_GraphicSpritesScene#getGraphicSpritesIndexById}
578 */
579/**
580 * Gets the index (the position in the {@link CB_GraphicSpritesScene#spritesGroups.items} array) of a desired {@link CB_GraphicSprites} object by its identifier.
581 * @function
582 * @param {string|*} [id=undefined] - The identifier of the {@link CB_GraphicSprites} object whose index we want to get.
583 * @returns {integer} Returns the index (the position in the {@link CB_GraphicSpritesScene#spritesGroups.items} array) of the desired {@link CB_GraphicSprites} object or -1 if not found.
584 * @todo Optimize it (perhaps using a cache matching the IDs with their position, maybe using the "position" or "positionByZIndex" properties).
585 */
586CB_GraphicSpritesScene.prototype.getGraphicSpritesIndexById = CB_GraphicSpritesScene.prototype.getIndexById = function(id)
587{
588 if (this.spritesGroups && this.spritesGroups.items)
589 {
590 var spritesGroupsLength = this.spritesGroups.items.length;
591 for (var x = 0; x < spritesGroupsLength; x++)
592 {
593 if (this.spritesGroups.items[x].id === id) { return x; }
594 }
595 }
596 return -1;
597}
598
599
600/**
601 * Alias for {@link CB_GraphicSpritesScene#executeFunctionAll}.
602 * @function CB_GraphicSpritesScene#executeAll
603 * @see {@link CB_GraphicSpritesScene#executeFunctionAll}
604 */
605 /**
606 * Alias for {@link CB_GraphicSpritesScene#executeFunctionAll}.
607 * @function CB_GraphicSpritesScene#forEach
608 * @see {@link CB_GraphicSpritesScene#executeFunctionAll}
609 */
610 /**
611 * Alias for {@link CB_GraphicSpritesScene#executeFunctionAll}.
612 * @function CB_GraphicSpritesScene#forEachGraphicSprites
613 * @see {@link CB_GraphicSpritesScene#executeFunctionAll}
614 */
615 /**
616 * Performs a desired action, using the provided function, on all the existing {@link CB_GraphicSprites} objects or on the desired ones (if provided). Calls the {@link CB_Arrays.executeFunctionAll} function internally and returns its returning value.
617 * @function
618 * @param {CB_Arrays.executeFunctionAll_ON_LOOP_CALLBACK} functionEach - Function that will be called for each {@link CB_GraphicSprites} object. As the first parameter it receives the {@link CB_GraphicSprites} object of the "graphicSpritesObjects" being looped, as the second parameter the position of this {@link CB_GraphicSprites} object in the "graphicSpritesObjects" array provided (or, if not provided, in the array returned by the {@link CB_GraphicSpritesScene#getGraphicSpritesAll} method), the third parameter is the array being looped and the fourth parameter will be the "delayBetweenEach" being used, being "this" the {@link CB_GraphicSprites} object itself.
619 * @param {boolean} [orderedByZIndex=false] - If set to true, it will loop the {@link CB_GraphicSprites} sorted by their z-index (ascending order).
620 * @param {number|CB_Arrays.executeFunctionAll_ON_LOOP_CALLBACK} [delayBetweenEach=0] - If a value greater than zero is used, it will be used as the delay desired between each call to the "functionEach" function (calling them using the [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} function internally). If not provided or the value is 0 (zero) or lower, each call to the "functionEach" function will be performed immediately one after the other. If a function is provided, it will be called with the same parameters as the "functionEach" function and its returning value will be used as the delay (executed every loop for each {@link CB_GraphicSprites} object).
621 * @param {array} [graphicSpritesObjects={@link CB_GraphicSpritesScene#getGraphicSpritesAll}()] - A numeric array containing the {@link CB_GraphicSprites} objects that we want to loop. It should contain only {@link CB_GraphicSprites} objects which are already in the current {@link CB_GraphicSpritesScene} object. If not provided, it will use all the {@link CB_GraphicSprites} objects contained in the {@link CB_GraphicSpritesScene} object.
622 * @param {boolean} [returnSetTimeoutsArray=false] - Defines whether we want the method to return an integer or a numeric array with information of each [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} call. Returning an array with information of each [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} call is only useful when the [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} function is called internally, which happens when the "delayBetweenEach" parameter is greater than 0 (zero).
623 * @param {boolean} [delayBetweenEachAffectsFirst=false] - If set to true, the desired delay (if any) will also affect the first call to the "functionEach" function.
624 * @param {CB_Arrays.executeFunctionAll_ON_FINISH_CALLBACK} [functionFinish] - Function that will be called for when it has finished looping all the items. The first parameter will be the array which was looped, the second parameter will be the number of times that the "functionEach" callback was called (the most likely, matches the number of elements unless they are undefined or null), and the third parameter will be the maximum "delay" used, being "this" the array itself.
625 * @returns {integer|array} If the "returnSetTimeoutsArray" parameter is set to false, it will return the number of calls to the "functionEach" function that were performed (which should be the same number as the {@link CB_GraphicSprites} objects given in the "graphicSpritesObjects" parameter). Otherwise, if the "returnSetTimeoutsArray" is set to true, it will return a numeric array with a {@link CB_Arrays.executeFunctionAll_OBJECT} object for each {@link CB_GraphicSprites} given. The length of this array will also be the number of calls to the "functionEach" function that were performed. Note that if a value greater than 0 (zero) for the "delayBetweenEach" parameter has been provided, perhaps not all calls of the "functionEach" function will have been performed yet when exiting this method because of the asynchronous nature of the [setTimeout]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout} function.
626 * @todo Think about only allowing {@link CB_GraphicSprites} objects (in the "graphicSpritesObjects" parameter) which are already in the {@link CB_GraphicSpritesScene} (identify them by their ID), to avoid problems.
627 */
628CB_GraphicSpritesScene.prototype.executeFunctionAll = CB_GraphicSpritesScene.prototype.executeAll = CB_GraphicSpritesScene.prototype.forEachGraphicSprites = CB_GraphicSpritesScene.prototype.forEach = function(functionEach, orderedByZIndex, delayBetweenEach, graphicSpritesObjects, returnSetTimeoutsArray, delayBetweenEachAffectsFirst, functionFinish)
629{
630 return CB_Arrays.executeFunctionAll(CB_isArray(graphicSpritesObjects) ? graphicSpritesObjects : this.getGraphicSpritesAll(orderedByZIndex, []), functionEach, delayBetweenEach, returnSetTimeoutsArray, delayBetweenEachAffectsFirst, functionFinish);
631}
632
633
634/**
635 * Sets the desired value of a given property name to the {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} object as well to its {@link CB_GraphicSprites} objects and their children (their {@link CB_GraphicSprites.SPRITES_OBJECT} object, including their {@link CB_GraphicSprites.SPRITE_OBJECT} and their {@link CB_GraphicSprites.SUBSPRITE_OBJECT} objects). Calls the {@link CB_GraphicSprites#setPropertyCascade} method internally.
636 * @function
637 * @param {number} propertyName - The name of the property we want to affect. Also used as the "propertyName" parameter when calling the {@link CB_GraphicSprites#setPropertyCascade} method internally.
638 * @param {*} [value=undefined] - The value desired for the given property. Also used as the "value" parameter when calling the {@link CB_GraphicSprites#setPropertyCascade} method internally.
639 * @param {boolean} [onlyCurrent=false] - If set to true, it will only affect the current sprite and its sub-sprites of each {@link CB_GraphicSprites} object (and also the {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} object and the {@link CB_GraphicSprites.SPRITES_OBJECT} object of each {@link CB_GraphicSprites} object). Used as the "propertyName" parameter when calling the {@link CB_GraphicSprites#setPropertyCascade} method internally.
640 * @returns {integer} Returns the number of elements affected (counting the {@link CB_GraphicSpritesScene.SPRITES_GROUPS_OBJECT} object and the {@link CB_GraphicSprites.SPRITES_OBJECT} objects).
641 */
642CB_GraphicSpritesScene.prototype.setPropertyCascade = function(propertyName, value, onlyCurrent)
643{
644 if (!propertyName) { return; }
645
646 this.spritesGroups[propertyName] = value;
647 var affected = 1;
648
649 this.forEachGraphicSprites(function() { affected += this.setPropertyCascade(propertyName, value, onlyCurrent); });
650
651 return affected;
652}
653
654
655/**
656 * Gets a new copy of this object with the same attributes (all sub-objects will be a copy, they will not the same reference).
657 * @function
658 * @param {boolean} [avoidCopyingPointers=false] - If set to true, it will not copy the {@link CB_GraphicSprites#pointer} property of each {@link CB_GraphicSprites} object.
659 * @param {boolean} [avoidCopyingTimes=false] - If set to true, it will not copy neither the {@link CB_GraphicSprites#time} property of each {@link CB_GraphicSprites} object nor the "time" property of each of their sprites ({@link CB_GraphicSprites.SPRITE_OBJECT} objects).
660 * @param {boolean} [clearReferences=false] - If set to true, it will not copy neither the "container" nor the "parent" nor the "data.that" nor the "data.getThis" properties of any element. Useful to be able to stringify the object preventing the "TypeError: cyclic object value" error. When set to true, calls the {@link CB_GraphicSprites.clearReferences} function internally. If set to true and the "filterProperties" parameter is also set to true, the {@link CB_GraphicSprites.filterProperties} will always be called before calling the {@link CB_GraphicSprites.clearReferences} function.
661 * @param {boolean} [filterProperties=false] - If set to true, it will call the {@link CB_GraphicSprites.filterProperties} function internally to filter the properties that we do not want to keep (using the given "propertiesToKeepObject" as the parameter to call it). When set to true, calls the {@link CB_GraphicSprites.filterProperties} function internally. If set to true and the "clearReferences" parameter is also set to true, the {@link CB_GraphicSprites.filterProperties} will always be called before calling the {@link CB_GraphicSprites.clearReferences} function.
662 * @param {CB_GraphicSprites.filterProperties_propertiesToKeepObject_TYPE} [propertiesToKeepObject=CB_GraphicSprites.filterProperties_DEFAULT_PROPERTIES] - The object with the properties that we want to keep. Only used when the "filterProperties" parameter is set to true, as the "propertiesToKeepObject" when calling the {@link CB_GraphicSprites.filterProperties} function internally.
663 * @returns {CB_GraphicSpritesScene} Returns a copy of this object with the same attributes (all sub-objects will be a copy, not the same reference).
664 */
665CB_GraphicSpritesScene.prototype.getCopy = function(avoidCopyingPointers, avoidCopyingTimes, clearReferences, filterProperties, propertiesToKeepObject)
666{
667 var spritesGroupsCopy = CB_copyObject(this.spritesGroups);
668
669 var newCopy = new CB_GraphicSpritesScene(spritesGroupsCopy, false);
670
671 CB_GraphicSprites._copyNeededProperties(newCopy, this) //Copies the needed properties from the original element.
672 CB_GraphicSprites._copyNeededProperties(newCopy.spritesGroups, this.spritesGroups) //Copies the needed properties from the original element.
673
674 this.forEach
675 (
676 function(CB_GraphicSpritesObject, index)
677 {
678 CB_GraphicSprites._copyNeededProperties(newCopy.get(index), CB_GraphicSpritesObject) //Copies the needed properties from the original element.
679 CB_GraphicSprites._copyNeededProperties(newCopy.get(index).spritesGroup, CB_GraphicSpritesObject.spritesGroup) //Copies the needed properties from the original element.
680
681 //If desired, sets the same pointers of each CB_GraphicSprites object:
682 if (!avoidCopyingPointers) { newCopy.get(index).pointer = this.pointer; newCopy.get(index).pointerPrevious = this.pointerPrevious; }
683 if (!avoidCopyingTimes) { newCopy.get(index).time = this.time; }
684 newCopy.get(index).time = this.time;
685 this.forEach
686 (
687 function(sprite, spriteIndex)
688 {
689 if (!avoidCopyingTimes) { newCopy.get(index).get(spriteIndex).time = this.time; }
690
691 CB_GraphicSprites._copyNeededProperties(newCopy.get(index).get(spriteIndex), sprite) //Copies the needed properties from the original element.
692
693 sprite.forEach
694 (
695 function(subSprite, subSpriteIndex)
696 {
697 CB_GraphicSprites._copyNeededProperties(newCopy.get(index).get(spriteIndex).get(subSpriteIndex), subSprite) //Copies the needed properties from the original element.
698
699 //If desired, sets the same times:
700 if (!avoidCopyingTimes) { sprite.get(subSpriteIndex).time = this.time; }
701 }
702 );
703 }
704 );
705 }
706 );
707
708 //Sets the same parent:
709 newCopy.parent = this.parent;
710
711 //If we want to, filters the properties keeping just the desired ones:
712 if (filterProperties) { newCopy = CB_GraphicSprites.filterProperties(newCopy, propertiesToKeepObject); }
713
714 //If we want to, clears the references:
715 if (clearReferences) { CB_GraphicSprites.clearReferences(newCopy); }
716
717 return newCopy;
718}
\No newline at end of file