-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
114 lines (95 loc) · 4.15 KB
/
index.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
"use strict";
var Resource = require("resource-loader").Resource;
function pathWithoutFile(path) {
var elements = path.split("/");
elements.pop(); // remove filename
return elements.join("/");
}
// Simple "wait for all" helper class
function WaitForAll(count, allDone) {
var remaining = count;
this.done = function() {
remaining--;
if (remaining === 0) {
allDone();
}
};
}
module.exports = function (PIXI)
{
var PIXI_VERSION = Number(PIXI.VERSION.charAt(0));
if(PIXI_VERSION < 4) {
throw new Error("PIXI version needs to be >= v4.0.0");
}
return function (resource, next)
{
// skip if no data, its not json, or it isn't a pixi-packer manifest
if (!resource.data || resource.type !== Resource.TYPE.JSON || !resource.data.meta || resource.data.meta.type !== "pixi-packer") {
return next();
}
if (resource.data.meta.version > 1) {
throw new Error("pixi-packer manifest version " + resource.data.meta.version + " not supported");
}
var loader = this;
var loadOptions = {
crossOrigin: resource.crossOrigin,
loadType: Resource.LOAD_TYPE.IMAGE,
parentResource: resource
};
var urlForManifest = resource.url.replace(loader.baseUrl, "");
var route = pathWithoutFile(urlForManifest);
var resolution = resource.data.resolution;
var versionResolutionFactor = PIXI_VERSION >= 5 ? 1 : resolution;
if (resource.data.spritesheets.length && loader.progress === 100) {
// This is a temporary workaround until a solution for https://github.com/englercj/resource-loader/pull/32 is found
loader.progress = 0;
}
// Load all spritesheets
var waiter = new WaitForAll(resource.data.spritesheets.length, next);
resource.data.spritesheets.forEach(function(spritesheet) {
var name = spritesheet.image;
var imageUrl = route + "/" + spritesheet.image;
if (PIXI.Texture.removeFromCache) {
PIXI.Texture.removeFromCache(name);
PIXI.Texture.removeFromCache(imageUrl);
PIXI.BaseTexture.removeFromCache(name);
PIXI.BaseTexture.removeFromCache(imageUrl);
}
loader.add(name, imageUrl, loadOptions, function (res) {
res.texture.baseTexture.resolution = resolution;
res.texture.baseTexture.update();
res.textures = {};
spritesheet.sprites.forEach(function(sprite) {
var frame = new PIXI.Rectangle(
sprite.position.x / versionResolutionFactor,
sprite.position.y / versionResolutionFactor,
sprite.dimension.w / versionResolutionFactor,
sprite.dimension.h / versionResolutionFactor
);
var crop = new PIXI.Rectangle(
0,
0,
sprite.dimension.w / resolution,
sprite.dimension.h / resolution
);
var trim = crop;
// Check to see if the sprite is trimmed
if (sprite.trim) {
trim = new PIXI.Rectangle(
sprite.trim.x / resolution,
sprite.trim.y / resolution,
sprite.trim.w / resolution,
sprite.trim.h / resolution
);
frame.width = sprite.trim.w / versionResolutionFactor;
frame.height = sprite.trim.h / versionResolutionFactor;
}
res.textures[sprite.name] = new PIXI.Texture(res.texture.baseTexture, frame, crop, trim, 0);
// lets also add the frame to pixi's global cache for fromFrame and fromImage functions
PIXI.utils.TextureCache[sprite.name] = res.textures[sprite.name];
});
waiter.done();
});
});
};
};