@image/packer
Version:
image packer
126 lines (125 loc) • 4.4 kB
JavaScript
import Queue from './Queue';
import Sprite from './Sprite';
import AtlasGroup from './AtlasGroup';
function resolvePaths(input, resolver) {
var atlases = [];
return Promise
.all(input.map(function (a) {
var atlas = {
files: [],
layoutConfig: a.layoutConfig,
exportConfig: a.exportConfig,
};
atlases.push(atlas);
return Promise
.all(a.files.map(function (file) {
return resolver(file.path)
.then(function (arr) {
arr.map(function (path) {
atlas.files.push({
path: path,
convertOption: file.convertOption
});
});
});
}));
}))
.then(function () { return atlases; });
}
function resolverExt(arg, resolver) {
var promises = [];
if (Array.isArray(arg)) {
arg.forEach(function (f) {
promises.push(resolver(f));
});
}
else {
promises.push(resolver(arg));
}
return Promise.all(promises)
.then(function (arr2) {
var items = [];
arr2.map(function (arr1) {
arr1.map(function (path) {
items.push(path);
});
});
return items;
});
}
export function spriteProcess(atlasesNR, concurrency, cache, imageProcessor, log, resolver) {
var filesToConverts = {};
var filePaths;
var sprites;
var atlasGroups = [];
var atlases;
var queue = new Queue(concurrency);
return Promise.resolve()
.then(function () {
return resolvePaths(atlasesNR, function (args) {
return resolverExt(args, resolver ? resolver : function (p) { return Promise.resolve([p]); });
});
})
.then(function (a) {
atlases = a;
atlases.forEach(function (atlas) {
atlas.files.forEach(function (file) {
if (!filesToConverts[file.path]) {
filesToConverts[file.path] = [];
}
var converts = filesToConverts[file.path];
converts.push(file.convertOption);
});
});
filePaths = Object.keys(filesToConverts);
// instantiate all Sprite
sprites = filePaths.map(function (filePath) {
return new Sprite(filePath, filesToConverts[filePath], cache, imageProcessor);
});
})
.then(function () {
// process all Sprite
return Promise.all(sprites.map(function (sprite) { return sprite.process(queue); }));
})
.then(function () {
// instantiate all AtlasGroup
atlases.forEach(function (atlas) {
var scaledSprites = [];
atlas.files.forEach(function (file) {
var sprite = sprites.find(function (s) { return s.path === file.path; });
if (sprite) {
var scaledSprite = sprite.scaledSprites.find(function (s) { return s.convertOptions === file.convertOption; });
if (scaledSprite) {
scaledSprites.push(scaledSprite);
}
}
});
var atlasGroup = new AtlasGroup(scaledSprites, atlas.layoutConfig, atlas.exportConfig, cache, imageProcessor, log);
atlasGroups.push(atlasGroup);
});
})
.then(function () {
// process all AtlasGroup
return Promise.all(atlasGroups.map(function (atlasGroup) { return atlasGroup.process(queue); }));
})
.then(function () {
// fill output model
var output = {
atlases: []
};
atlasGroups.forEach(function (atlasGroup) {
var sheets = [];
atlasGroup.spritesheets.forEach(function (spritesheet) {
sheets.push({
sprites: spritesheet.loadingInformation,
path: spritesheet.cachedImagePath,
hash: spritesheet.hash,
width: spritesheet.width,
height: spritesheet.height,
});
});
output.atlases.push({ sheets: sheets });
});
return output;
});
}