Skip to content

Commit

Permalink
Don't trim tilesheet edges, auto reload tiled sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Jbudone committed May 5, 2019
1 parent ef06433 commit c664e13
Showing 1 changed file with 47 additions and 28 deletions.
75 changes: 47 additions & 28 deletions resourceBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,6 @@ const processGeneratedTilesheet = (package) => {
let spritesToExtract = [],
imagesToExtract = [],
yOffset = 0,
genMaxX = 0,
genMaxY = 0,
spriteGroups = [];

const extractionDeps = [],
Expand Down Expand Up @@ -1177,6 +1175,16 @@ const processGeneratedTilesheet = (package) => {
dstX = (x - minX),
dstY = (y - minY + yOffset);

if
(
Math.ceil(dstX) !== Math.floor(dstX) ||
Math.ceil(dstY) !== Math.floor(dstY)
)
{
debugger;
console.error(`Bad dstX/dstY: ${dstX}x${dstY}`);
}

const existingSprite = modifiedSprites.find((s) => s.source === source && s.sprite === sprite);

if (existingSprite) {
Expand Down Expand Up @@ -1248,8 +1256,6 @@ const processGeneratedTilesheet = (package) => {
});

yOffset += (maxY - minY + 1);
genMaxY += (maxY - minY + 1);
genMaxX = Math.max(genMaxX, maxX - minX + 1);

package.dependencies.push({
assetId: dependency.asset.id,
Expand Down Expand Up @@ -1293,8 +1299,6 @@ const processGeneratedTilesheet = (package) => {
});


genMaxY += (maxY - minY + 1);
genMaxX = Math.max(genMaxX, maxX - minX + 1);


spriteGroups.push({
Expand All @@ -1316,8 +1320,6 @@ const processGeneratedTilesheet = (package) => {

if (Settings.verbose) {
console.log("Generated tilesheet:");
console.log(` Width: ${package.tilesize * genMaxX}`);
console.log(` Height: ${package.tilesize * genMaxY}`);
console.log(` Sprites:`);
console.log(spritesToExtract);
console.log(` Images:`);
Expand Down Expand Up @@ -1368,8 +1370,9 @@ const processGeneratedTilesheet = (package) => {



const newColumns = maxDstX - minDstX,
newRows = maxDstY - minDstY;
// NOTE: We could trim the top/left, if we want to do this then need to fix translations
const newColumns = maxDstX,// - minDstX,
newRows = maxDstY;// - minDstY;


const spriteTranslations = {},
Expand All @@ -1382,24 +1385,25 @@ const processGeneratedTilesheet = (package) => {
const boundsHaveChanged = (oldColumns !== newColumns || oldRows !== newRows);
if (boundsHaveChanged) {

for (let i = 0; i < package.sprites.length; ++i) {
package.sprites[i].dstX -= minDstX * tilesize;
package.sprites[i].dstY -= minDstY * tilesize;
}
// NOTE: Uncomment below for trimming top/left
//for (let i = 0; i < package.sprites.length; ++i) {
// package.sprites[i].dstX -= minDstX * tilesize;
// package.sprites[i].dstY -= minDstY * tilesize;
//}

// Translate sprite in spriteIsland
spriteGroups.forEach((sg) => {

if (sg.spriteIsland) {
sg.spriteIsland.forEach((s) => {
s.dstX -= minDstX;
s.dstY -= minDstY;
});
} else {
sg.dstX -= minDstX;
sg.dstY -= minDstY;
}
});
//spriteGroups.forEach((sg) => {

// if (sg.spriteIsland) {
// sg.spriteIsland.forEach((s) => {
// s.dstX -= minDstX * tilesize;
// s.dstY -= minDstY * tilesize;
// });
// } else {
// sg.dstX -= minDstX * tilesize;
// sg.dstY -= minDstY * tilesize;
// }
//});
}

spriteGroupsToTranslate.forEach((spriteGroup) => {
Expand Down Expand Up @@ -1594,8 +1598,10 @@ const processGeneratedTilesheet = (package) => {

imagesToExtract.forEach((img) => {
const dstX = img.dstX,
dstY = img.dstY;
convertCmd += `\\( "resources/${img.source}" -filter box -repage ${dstX >= 0 ? '+' : '-'}${dstX}${dstY >= 0 ? '+' : '-'}${dstY} -background none -gravity northwest -extent ${newColumns * package.tilesize}x${newRows * package.tilesize} \\) `;
dstY = img.dstY,
right = newColumns * package.tilesize - dstX,
bot = newRows * package.tilesize - dstY;
convertCmd += `\\( "resources/${img.source}" -filter box -repage ${dstX >= 0 ? '+' : '-'}${dstX}${dstY >= 0 ? '+' : '-'}${dstY} -background none -gravity northwest -extent ${right}x${bot} -gravity southeast -extent ${right + dstX}x${bot + dstY} \\) `;
});

convertCmd += `-background none -layers merge "${package.output}"`;
Expand Down Expand Up @@ -2088,6 +2094,19 @@ const processGeneratedTilesheet = (package) => {
});

Promise.all(waitingOnMaps).then(() => {

// If we have tiled open we can send keystrokes to the process to reload the tilesheet/map

// ctrl+r reload map
// ctrl+t reload tilesheet
// NOTE: If tiled isn't open this will silently fail without side effects
// FIXME: If the sheet bounds haven't changed then we won't receive a popup
// FIXME: xdotool also allows waiting for behaviors, will this recognize the popup?
const waitForPopupTime = 0.4, // Tiled will popup asking to reload (it hasn't recognized that we've changed the bounds yet)
waitToReload = 0.1, // After denying reload: takes a moment for the popup press to respond
waitToShow = 1.0;
execSync('export DISPLAY=:0.0 ; export XAUTHORITY=/home/jbud/.Xauthority ; xdotool search --class tiled windowactivate --sync %1 key ctrl+t sleep ' + waitForPopupTime + ' key N sleep ' + waitToReload + ' key ctrl+r key ctrl+r sleep ' + waitToShow + ' windowactivate $( xdotool getactivewindow )');

success();
}).catch((err) => {
fail(err);
Expand Down

0 comments on commit c664e13

Please sign in to comment.