habitica/gulp/gulp-sprites.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-09-12 00:59:06 +00:00
import gulp from 'gulp';
import spritesmith from 'gulp.spritesmith';
2015-11-08 01:09:51 +00:00
import clean from 'rimraf';
2015-09-13 01:43:04 +00:00
import mergeStream from 'merge-stream';
2019-10-08 14:57:10 +00:00
import { sync } from 'glob';
2015-09-12 00:59:06 +00:00
const IMG_DIST_PATH = 'website/client/src/assets/images/sprites/';
const CSS_DIST_PATH = 'website/client/src/assets/css/sprites/';
function checkForSpecialTreatment (name) {
2019-10-08 14:57:10 +00:00
const regex = /^hair|skin|beard|mustach|shirt|flower|^headAccessory_special_\w+Ears|^eyewear_special_\w+TopFrame|^eyewear_special_\w+HalfMoon/;
return name.match(regex) || name === 'head_0';
}
2015-09-12 00:59:06 +00:00
function cssVarMap (sprite) {
2019-10-08 14:57:10 +00:00
// For hair, skins, beards, etc. we want to output a '.customize-options.WHATEVER' class,
// which works as a 60x60 image pointing at the proper part of the 90x90 sprite.
// We set up the custom info here, and the template makes use of it.
2019-10-08 14:57:10 +00:00
const requiresSpecialTreatment = checkForSpecialTreatment(sprite.name);
if (requiresSpecialTreatment) {
sprite.custom = {
px: {
offsetX: '-25px',
offsetY: '-15px',
width: '60px',
height: '60px',
},
};
2015-09-12 00:59:06 +00:00
}
// even more for shirts
if (sprite.name.indexOf('shirt') !== -1) {
sprite.custom.px.offsetX = '-29px';
sprite.custom.px.offsetY = '-42px';
}
if (sprite.name.indexOf('hair_base') !== -1) {
2019-10-08 14:57:10 +00:00
const styleArray = sprite.name.split('_').slice(2, 3);
if (Number(styleArray[0]) > 14) {
sprite.custom.px.offsetY = '0'; // don't crop updos
}
}
}
2015-09-12 00:59:06 +00:00
function createSpritesStream (name, src) {
2019-10-08 14:57:10 +00:00
const stream = mergeStream();
2015-09-12 16:09:29 +00:00
const spriteData = gulp.src(src)
.pipe(spritesmith({
imgName: `spritesmith-${name}.png`,
cssName: `spritesmith-${name}.css`,
algorithm: 'binary-tree',
padding: 1,
cssTemplate: 'website/raw_sprites/css/css.template.handlebars',
cssVarMap,
}));
2015-09-13 01:43:04 +00:00
const cssStream = spriteData.css
.pipe(gulp.dest(CSS_DIST_PATH));
2015-09-13 01:43:04 +00:00
stream.add(cssStream);
2015-09-13 01:43:04 +00:00
return stream;
2015-09-12 16:09:29 +00:00
}
gulp.task('sprites:main', () => {
const mainSrc = sync('habitica-images/**/*.png');
return createSpritesStream('main', mainSrc);
});
2015-09-12 00:59:06 +00:00
2019-10-08 14:57:10 +00:00
gulp.task('sprites:clean', done => {
clean(`${IMG_DIST_PATH}spritesmith*,${CSS_DIST_PATH}spritesmith*}`, done);
});
gulp.task('sprites:compile', gulp.series('sprites:clean', 'sprites:main', done => done()));