2020-09-18 16:22:10 +03:00
|
|
|
let resizeSpriteInitialized = false;
|
2020-09-19 18:04:46 +03:00
|
|
|
let keepRatio = true;
|
|
|
|
let currentRatio;
|
|
|
|
let data = {width: 0, height: 0, widthPercentage: 100, heightPercentage: 100};
|
|
|
|
let startData = {width: 0, height:0, widthPercentage: 100, heightPercentage: 100};
|
2020-09-18 16:22:10 +03:00
|
|
|
|
2020-09-19 18:04:46 +03:00
|
|
|
function openResizeSpriteWindow() {
|
|
|
|
if (!resizeSpriteInitialized) {
|
|
|
|
resizeSpriteInitialized = true;
|
|
|
|
initResizeSpriteInputs();
|
2020-09-18 16:22:10 +03:00
|
|
|
}
|
2020-09-19 18:04:46 +03:00
|
|
|
|
|
|
|
currentRatio = layers[0].canvasSize[0] / layers[0].canvasSize[1];
|
|
|
|
|
|
|
|
data.width = layers[0].canvasSize[0];
|
|
|
|
data.height = layers[1].canvasSize[1];
|
|
|
|
|
|
|
|
startData.width = data.width;
|
|
|
|
startData.height = data.height;
|
|
|
|
|
|
|
|
showDialogue('resize-sprite');
|
|
|
|
}
|
|
|
|
|
|
|
|
function initResizeSpriteInputs() {
|
|
|
|
document.getElementById("rs-width").value = layers[0].canvasSize[0];
|
|
|
|
document.getElementById("rs-height").value = layers[0].canvasSize[1];
|
|
|
|
|
|
|
|
document.getElementById("rs-width-percentage").value = 100;
|
|
|
|
document.getElementById("rs-height-percentage").value = 100;
|
|
|
|
|
|
|
|
document.getElementById("rs-keep-ratio").checked = true;
|
|
|
|
|
|
|
|
document.getElementById("rs-width").addEventListener("change", changedWidth);
|
|
|
|
document.getElementById("rs-height").addEventListener("change", changedHeight);
|
|
|
|
document.getElementById("rs-width-percentage").addEventListener("change", changedWidthPercentage);
|
|
|
|
document.getElementById("rs-height-percentage").addEventListener("change", changedHeightPercentage);
|
|
|
|
|
|
|
|
document.getElementById("resize-sprite-confirm").addEventListener("click", resizeSprite);
|
|
|
|
document.getElementById("rs-keep-ratio").addEventListener("click", toggleRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
function resizeSprite() {
|
2020-09-21 11:39:37 +03:00
|
|
|
let newWidth, newHeight;
|
2020-09-21 13:33:45 +03:00
|
|
|
let imageDatas = [];
|
|
|
|
let layerIndex = 0;
|
2020-09-21 11:39:37 +03:00
|
|
|
|
|
|
|
rcPivot = "middle";
|
|
|
|
// Updating values if the user didn't press enter
|
|
|
|
switch (document.activeElement.id) {
|
|
|
|
case "rs-width-percentage":
|
|
|
|
changedWidthPercentage();
|
|
|
|
break;
|
|
|
|
case "rs-width":
|
|
|
|
changedWidth();
|
|
|
|
break;
|
|
|
|
case "rs-height-percentage":
|
|
|
|
changedHeightPercentage();
|
|
|
|
break;
|
|
|
|
case "rs-height":
|
|
|
|
changedHeight();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// In this case everything has been updated correctly
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
newWidth = data.width;
|
|
|
|
newHeight = data.height;
|
|
|
|
|
|
|
|
console.log(newWidth + ", " + newHeight);
|
2020-09-19 18:04:46 +03:00
|
|
|
|
2020-09-21 13:33:45 +03:00
|
|
|
// Get all the image datas
|
|
|
|
for (let i=0; i<layers.length; i++) {
|
|
|
|
if (layers[i].menuEntry != null) {
|
|
|
|
imageDatas.push(layers[i].context.getImageData(
|
|
|
|
0, 0, layers[0].canvasSize[0], layers[0].canvasSize[1])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
// Resizing the canvas
|
|
|
|
resizeCanvas(null, {x: newWidth, y: newHeight});
|
2020-09-21 13:33:45 +03:00
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
// Put the image datas on the new canvases
|
2020-09-21 13:33:45 +03:00
|
|
|
for (let i=0; i<layers.length; i++) {
|
|
|
|
if (layers[i].menuEntry != null) {
|
|
|
|
layers[i].context.putImageData(
|
|
|
|
resizeImageData(imageDatas[layerIndex], newWidth, newHeight, 'nearest-neighbor'), 0, 0
|
|
|
|
);
|
|
|
|
layerIndex++;
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 11:39:37 +03:00
|
|
|
closeDialogue();
|
2020-09-19 18:04:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function changedWidth(event) {
|
|
|
|
let oldValue = data.width;
|
|
|
|
let ratio;
|
|
|
|
let percentageRatio;
|
2020-09-21 11:39:37 +03:00
|
|
|
let newHeight, newHeightPerc, newWidthPerc;
|
|
|
|
|
2020-09-19 18:04:46 +03:00
|
|
|
data.width = event.target.value;
|
|
|
|
delta = data.width - oldValue;
|
|
|
|
|
|
|
|
ratio = data.width / oldValue;
|
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
newHeight = data.width / currentRatio;
|
|
|
|
newHeightPerc = (newHeight * 100) / startData.height;
|
|
|
|
newWidthPerc = (data.width * 100) / startData.width;
|
|
|
|
|
2020-09-19 18:04:46 +03:00
|
|
|
if (keepRatio) {
|
2020-09-21 11:39:37 +03:00
|
|
|
document.getElementById("rs-height").value = newHeight;
|
|
|
|
data.height = newHeight;
|
|
|
|
|
|
|
|
document.getElementById("rs-height-percentage").value = newHeightPerc;
|
|
|
|
data.heightPercentage = newHeightPerc;
|
2020-09-19 18:04:46 +03:00
|
|
|
}
|
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
document.getElementById("rs-width-percentage").value = newWidthPerc;
|
2020-09-19 18:04:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function changedHeight(event) {
|
|
|
|
let oldValue = data.height;
|
|
|
|
let ratio;
|
2020-09-21 11:39:37 +03:00
|
|
|
let newWidth, newWidthPerc, newHeightPerc;
|
2020-09-19 18:04:46 +03:00
|
|
|
|
|
|
|
data.height = event.target.value;
|
|
|
|
delta = data.height - oldValue;
|
|
|
|
|
|
|
|
ratio = data.height / oldValue;
|
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
newWidth = data.height * currentRatio;
|
|
|
|
newWidthPerc = (newWidth * 100) / startData.width;
|
|
|
|
newHeightPerc = (data.height * 100) / startData.height;
|
|
|
|
|
2020-09-19 18:04:46 +03:00
|
|
|
if (keepRatio) {
|
2020-09-21 11:39:37 +03:00
|
|
|
document.getElementById("rs-width").value = newWidth;
|
|
|
|
data.width = newWidth;
|
|
|
|
|
|
|
|
document.getElementById("rs-width-percentage").value = newWidthPerc;
|
|
|
|
data.widthPercentage = newWidthPerc;
|
2020-09-19 18:04:46 +03:00
|
|
|
}
|
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
document.getElementById("rs-height-percentage").value = newHeightPerc;
|
|
|
|
data.heightPercentage = newHeightPerc;
|
2020-09-19 18:04:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function changedWidthPercentage(event) {
|
|
|
|
let oldValue = data.widthPercentage;
|
|
|
|
let ratio;
|
2020-09-21 11:39:37 +03:00
|
|
|
let newWidth, newHeight, newHeightPerc;
|
2020-09-19 18:04:46 +03:00
|
|
|
|
|
|
|
data.widthPercentage = event.target.value;
|
|
|
|
delta = data.widthPercentage - oldValue;
|
|
|
|
|
|
|
|
ratio = data.widthPercentage / oldValue;
|
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
newHeight = document.getElementById("rs-height").value * ratio;
|
|
|
|
newHeightPerc = data.widthPercentage / currentRatio;
|
|
|
|
newWidth = document.getElementById("rs-width").value * ratio;
|
|
|
|
|
2020-09-19 18:04:46 +03:00
|
|
|
if (keepRatio) {
|
2020-09-21 11:39:37 +03:00
|
|
|
document.getElementById("rs-height-percentage").value = newHeightPerc;
|
|
|
|
data.heightPercentage = newHeightPerc;
|
|
|
|
|
|
|
|
document.getElementById("rs-height").value = newHeight
|
|
|
|
data.height = newHeight;
|
2020-09-19 18:04:46 +03:00
|
|
|
}
|
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
document.getElementById("rs-width").value = newWidth;
|
|
|
|
data.width = newWidth;
|
2020-09-19 18:04:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function changedHeightPercentage(event) {
|
|
|
|
let oldValue = data.heightPercentage;
|
|
|
|
let ratio;
|
2020-09-21 11:39:37 +03:00
|
|
|
let newHeight, newWidth, newWidthPerc;
|
2020-09-19 18:04:46 +03:00
|
|
|
|
|
|
|
data.heightPercentage = event.target.value;
|
|
|
|
delta = data.heightPercentage - oldValue;
|
|
|
|
|
|
|
|
ratio = data.heightPercentage / oldValue;
|
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
newWidth = document.getElementById("rs-width").value * ratio;
|
|
|
|
newWidthPerc = data.heightPercentage * currentRatio;
|
|
|
|
newHeight = document.getElementById("rs-height").value * ratio;
|
|
|
|
|
2020-09-19 18:04:46 +03:00
|
|
|
if (keepRatio) {
|
|
|
|
document.getElementById("rs-width-percentage").value = data.heightPercentage * currentRatio;
|
2020-09-21 11:39:37 +03:00
|
|
|
data.widthPercentage = newWidthPerc;
|
|
|
|
|
|
|
|
document.getElementById("rs-width").value = newWidth;
|
|
|
|
data.width = newWidth;
|
2020-09-19 18:04:46 +03:00
|
|
|
}
|
|
|
|
|
2020-09-21 11:39:37 +03:00
|
|
|
document.getElementById("rs-height").value = newHeight;
|
|
|
|
data.height = newHeight;
|
2020-09-19 18:04:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function toggleRatio(event) {
|
|
|
|
keepRatio = !keepRatio;
|
|
|
|
console.log(keepRatio);
|
|
|
|
}
|