/
CCSpriteFrameCache.js
421 lines (380 loc) · 17.3 KB
/
CCSpriteFrameCache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
* Singleton that handles the loading of the sprite frames. It saves in a cache the sprite frames.
* @class
* @extends cc.Class
* @example
* // add SpriteFrames to SpriteFrameCache With File
* cc.SpriteFrameCache.getInstance().addSpriteFrames(s_grossiniPlist);
*/
cc.SpriteFrameCache = cc.Class.extend(/** @lends cc.SpriteFrameCache# */{
_spriteFrames:null,
_spriteFramesAliases:null,
_loadedFileNames:null,
/**
* Constructor
*/
ctor:function () {
this._spriteFrames = {};
this._spriteFramesAliases = {};
this._loadedFileNames = [];
},
/**
* Adds multiple Sprite Frames with a dictionary. The texture will be associated with the created sprite frames.
* @param {object} dictionary
* @param {HTMLImageElement|cc.Texture2D} texture
*/
_addSpriteFramesWithDictionary:function (dictionary, texture) {
var metadataDict = dictionary["metadata"];
var framesDict = dictionary["frames"];
var format = 0;
// get the format
if (metadataDict != null) {
format = parseInt(this._valueForKey("format", metadataDict));
}
// check the format
cc.Assert(format >= 0 && format <= 3, "format is not supported for cc.SpriteFrameCache addSpriteFramesWithDictionary:textureFilename:");
for (var key in framesDict) {
var frameDict = framesDict[key];
if (frameDict) {
var spriteFrame = this._spriteFrames[key];
if (spriteFrame) {
continue;
}
if (format == 0) {
var x = parseFloat(this._valueForKey("x", frameDict));
var y = parseFloat(this._valueForKey("y", frameDict));
var w = parseFloat(this._valueForKey("width", frameDict));
var h = parseFloat(this._valueForKey("height", frameDict));
var ox = parseFloat(this._valueForKey("offsetX", frameDict));
var oy = parseFloat(this._valueForKey("offsetY", frameDict));
var ow = parseInt(this._valueForKey("originalWidth", frameDict));
var oh = parseInt(this._valueForKey("originalHeight", frameDict));
// check ow/oh
if (!ow || !oh) {
cc.log("cocos2d: WARNING: originalWidth/Height not found on the cc.SpriteFrame. AnchorPoint won't work as expected. Regenrate the .plist");
}
// Math.abs ow/oh
ow = Math.abs(ow);
oh = Math.abs(oh);
// create frame
spriteFrame = new cc.SpriteFrame();
spriteFrame.initWithTexture(texture, cc.rect(x, y, w, h), false, cc.p(ox, oy), cc.size(ow, oh));
} else if (format == 1 || format == 2) {
var frame = cc.RectFromString(this._valueForKey("frame", frameDict));
var rotated = false;
// rotation
if (format == 2) {
rotated = this._valueForKey("rotated", frameDict) == "true";
}
var offset = cc.PointFromString(this._valueForKey("offset", frameDict));
var sourceSize = cc.SizeFromString(this._valueForKey("sourceSize", frameDict));
// create frame
spriteFrame = new cc.SpriteFrame();
spriteFrame.initWithTexture(texture, frame, rotated, offset, sourceSize);
} else if (format == 3) {
// get values
var spriteSize, spriteOffset, spriteSourceSize, textureRect, textureRotated;
if (frameDict.hasOwnProperty("spriteSize")) {
spriteSize = cc.SizeFromString(this._valueForKey("spriteSize", frameDict));
spriteOffset = cc.PointFromString(this._valueForKey("spriteOffset", frameDict));
spriteSourceSize = cc.SizeFromString(this._valueForKey("spriteSourceSize", frameDict));
textureRect = cc.RectFromString(this._valueForKey("textureRect", frameDict));
textureRotated = this._valueForKey("textureRotated", frameDict) == "true";
} else {
spriteSize = cc.RectFromString(this._valueForKey("frame", frameDict));
spriteOffset = cc.PointFromString(this._valueForKey("offset", frameDict));
spriteSourceSize = cc.SizeFromString(this._valueForKey("sourceSize", frameDict));
textureRect = cc.SizeFromString(this._valueForKey("sourceSize", frameDict));
textureRotated = this._valueForKey("rotated", frameDict) == "true";
}
// get aliases
var aliases = frameDict["aliases"];
var frameKey = key.toString();
for (var aliasKey in aliases) {
if (this._spriteFramesAliases.hasOwnProperty(aliases[aliasKey])) {
cc.log("cocos2d: WARNING: an alias with name " + aliasKey + " already exists");
}
this._spriteFramesAliases[aliases[aliasKey]] = frameKey;
}
// create frame
spriteFrame = new cc.SpriteFrame();
if (frameDict.hasOwnProperty("spriteSize")) {
spriteFrame.initWithTexture(texture,
cc.rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height),
textureRotated,
spriteOffset,
spriteSourceSize);
} else {
spriteFrame.initWithTexture(texture, spriteSize, textureRotated, spriteOffset, spriteSourceSize);
}
}
if (spriteFrame.isRotated()) {
//clip to canvas
var tempTexture = cc.cutRotateImageToCanvas(spriteFrame.getTexture(), spriteFrame.getRect());
var rect = spriteFrame.getRect();
spriteFrame.setRect(cc.rect(0, 0, rect.size.width, rect.size.height));
spriteFrame.setTexture(tempTexture);
}
// add sprite frame
this._spriteFrames[key] = spriteFrame;
}
}
},
/**
* Adds multiple Sprite Frames from a json file. A texture will be loaded automatically.
* @param {object} jsonData
*/
addSpriteFramesWithJson:function (jsonData) {
var dict = jsonData;
var texturePath = "";
var metadataDict = dict["metadata"];
if (metadataDict) {
// try to read texture file name from meta data
texturePath = this._valueForKey("textureFileName", metadataDict);
texturePath = texturePath.toString();
}
var texture = cc.TextureCache.getInstance().addImage(texturePath);
if (texture) {
this._addSpriteFramesWithDictionary(dict, texture);
}
else {
cc.log("cocos2d: cc.SpriteFrameCache: Couldn't load texture");
}
},
/**
* <p>
* Adds multiple Sprite Frames from a plist file.<br/>
* A texture will be loaded automatically. The texture name will composed by replacing the .plist suffix with .png<br/>
* If you want to use another texture, you should use the addSpriteFrames:texture method.<br/>
* </p>
* @param {String} plist plist filename
* @param {HTMLImageElement|cc.Texture2D} texture
* @example
* // add SpriteFrames to SpriteFrameCache With File
* cc.SpriteFrameCache.getInstance().addSpriteFrames(s_grossiniPlist);
*/
addSpriteFrames:function (plist, texture) {
var dict = cc.FileUtils.getInstance().dictionaryWithContentsOfFileThreadSafe(plist);
switch (arguments.length) {
case 1:
cc.Assert(plist, "plist filename should not be NULL");
if (!cc.ArrayContainsObject(this._loadedFileNames, plist)) {
var texturePath = "";
var metadataDict = dict["metadata"];
if (metadataDict) {
// try to read texture file name from meta data
texturePath = this._valueForKey("textureFileName", metadataDict).toString();
}
if (texturePath != "") {
// build texture path relative to plist file
var getIndex = plist.lastIndexOf('/'), pszPath;
pszPath = getIndex ? plist.substring(0, getIndex + 1) : "";
texturePath = pszPath + texturePath;
} else {
// build texture path by replacing file extension
texturePath = plist;
// remove .xxx
var startPos = texturePath.lastIndexOf(".", texturePath.length);
texturePath = texturePath.substr(0, startPos);
// append .png
texturePath = texturePath + ".png";
}
var getTexture = cc.TextureCache.getInstance().addImage(texturePath);
if (getTexture) {
this._addSpriteFramesWithDictionary(dict, getTexture);
} else {
cc.log("cocos2d: cc.SpriteFrameCache: Couldn't load texture");
}
}
break;
case 2:
if ((texture instanceof cc.Texture2D) || (texture instanceof HTMLImageElement) || (texture instanceof HTMLCanvasElement)) {
/** Adds multiple Sprite Frames from a plist file. The texture will be associated with the created sprite frames. */
this._addSpriteFramesWithDictionary(dict, texture);
} else {
/** Adds multiple Sprite Frames from a plist file. The texture will be associated with the created sprite frames.
@since v0.99.5
*/
var textureFileName = texture;
cc.Assert(textureFileName, "texture name should not be null");
var gTexture = cc.TextureCache.getInstance().addImage(textureFileName);
if (gTexture) {
this._addSpriteFramesWithDictionary(dict, gTexture);
} else {
cc.log("cocos2d: cc.SpriteFrameCache: couldn't load texture file. File not found " + textureFileName);
}
}
break;
default:
throw "Argument must be non-nil ";
}
},
/**
* <p>
* Adds an sprite frame with a given name.<br/>
* If the name already exists, then the contents of the old name will be replaced with the new one.
* </p>
* @param {cc.SpriteFrame} frame
* @param {String} frameName
*/
addSpriteFrame:function (frame, frameName) {
this._spriteFrames[frameName] = frame;
},
/**
* <p>
* Purges the dictionary of loaded sprite frames.<br/>
* Call this method if you receive the "Memory Warning".<br/>
* In the short term: it will free some resources preventing your app from being killed.<br/>
* In the medium term: it will allocate more resources.<br/>
* In the long term: it will be the same.<br/>
* </p>
*/
removeSpriteFrames:function () {
this._spriteFrames = [];
this._spriteFramesAliases = [];
this._loadedFileNames = {};
},
/**
* Deletes an sprite frame from the sprite frame cache.
* @param {String} name
*/
removeSpriteFrameByName:function (name) {
// explicit nil handling
if (!name) {
return;
}
// Is this an alias ?
if (this._spriteFramesAliases.hasOwnProperty(name)) {
delete(this._spriteFramesAliases[name]);
}
if (this._spriteFrames.hasOwnProperty(name)) {
delete(this._spriteFrames[name]);
}
// XXX. Since we don't know the .plist file that originated the frame, we must remove all .plist from the cache
this._loadedFileNames = {};
},
/**
* <p>
* Removes multiple Sprite Frames from a plist file.<br/>
* Sprite Frames stored in this file will be removed.<br/>
* It is convinient to call this method when a specific texture needs to be removed.<br/>
* </p>
* @param {String} plist plist filename
*/
removeSpriteFramesFromFile:function (plist) {
var path = cc.FileUtils.getInstance().fullPathFromRelativePath(plist);
var dict = cc.FileUtils.getInstance().dictionaryWithContentsOfFileThreadSafe(path);
this._removeSpriteFramesFromDictionary(dict);
//remove it from the cache
if (cc.ArrayContainsObject(this._loadedFileNames, plist)) {
cc.ArrayRemoveObject(plist);
}
},
/**
* Removes multiple Sprite Frames from Dictionary.
* @param {object} dictionary SpriteFrame of Dictionary
*/
_removeSpriteFramesFromDictionary:function (dictionary) {
var framesDict = dictionary["frames"];
for (var key in framesDict) {
if (this._spriteFrames.hasOwnProperty(key)) {
delete(this._spriteFrames[key]);
}
}
},
/**
* <p>
* Removes all Sprite Frames associated with the specified textures.<br/>
* It is convinient to call this method when a specific texture needs to be removed.
* </p>
* @param {HTMLImageElement|HTMLCanvasElement|cc.Texture2D} texture
*/
removeSpriteFramesFromTexture:function (texture) {
for (var key in this._spriteFrames) {
var frame = this._spriteFrames[key];
if (frame && (frame.getTexture() == texture)) {
delete(this._spriteFrames[key]);
}
}
},
/**
* <p>
* Returns an Sprite Frame that was previously added.<br/>
* If the name is not found it will return nil.<br/>
* You should retain the returned copy if you are going to use it.<br/>
* </p>
* @param {String} name name of SpriteFrame
* @return {cc.SpriteFrame}
* @example
* //get a SpriteFrame by name
* var frame = cc.SpriteFrameCache.getInstance().getSpriteFrame("grossini_dance_01.png");
*/
getSpriteFrame:function (name) {
var frame;
if (this._spriteFrames.hasOwnProperty(name)) {
frame = this._spriteFrames[name];
}
if (!frame) {
// try alias dictionary
var key;
if (this._spriteFramesAliases.hasOwnProperty(name)) {
key = this._spriteFramesAliases[name];
}
if (key) {
if (this._spriteFrames.hasOwnProperty(key.toString())) {
frame = this._spriteFrames[key.toString()];
}
if (!frame) {
cc.log("cocos2d: cc.SpriteFrameCahce: Frame " + name + " not found");
}
}
}
return frame;
},
_valueForKey:function (key, dict) {
if (dict) {
if (dict.hasOwnProperty(key)) {
return dict[key].toString();
}
}
return "";
}
});
cc.s_sharedSpriteFrameCache = null;
/**
* Returns the shared instance of the Sprite Frame cache
* @return {cc.SpriteFrameCache}
*/
cc.SpriteFrameCache.getInstance = function () {
if (!cc.s_sharedSpriteFrameCache) {
cc.s_sharedSpriteFrameCache = new cc.SpriteFrameCache();
}
return cc.s_sharedSpriteFrameCache;
};
/**
* Purges the cache. It releases all the Sprite Frames and the retained instance.
*/
cc.SpriteFrameCache.purgeSharedSpriteFrameCache = function () {
cc.s_sharedSpriteFrameCache = null;
};