Added history management for sprite scaling

This commit is contained in:
unsettledgames 2020-09-22 14:17:31 +02:00
parent 29f8baf627
commit 9c68e541d9
2 changed files with 57 additions and 18 deletions

View File

@ -3,17 +3,34 @@ var redoStates = [];
const undoLogStyle = 'background: #87ff1c; color: black; padding: 5px;'; const undoLogStyle = 'background: #87ff1c; color: black; padding: 5px;';
function HistoryStateResizeSprite(newPercs, oldPercs, algo) { function HistoryStateResizeSprite(xRatio, yRatio, algo, oldData) {
this.newPercs = newPercs; this.xRatio = xRatio;
this.oldPercs = oldPercs; this.yRatio = yRatio;
this.algo = algo; this.algo = algo;
this.oldData = oldData;
this.undo = function() { this.undo = function() {
let layerIndex = 0;
currentAlgo = algo;
resizeSprite(null, [1 / this.xRatio, 1 / this.yRatio]);
// Also putting the old data
for (let i=0; i<layers.length; i++) {
if (layers[i].menuEntry != null) {
layers[i].context.putImageData(this.oldData[layerIndex], 0, 0);
layerIndex++;
layers[i].updateLayerPreview();
}
}
redoStates.push(this);
}; };
this.redo = function() { this.redo = function() {
currentAlgo = algo;
resizeSprite(null, [this.xRatio, this.yRatio]);
undoStates.push(this);
}; };
saveHistoryState(this); saveHistoryState(this);

View File

@ -40,12 +40,17 @@ function initResizeSpriteInputs() {
document.getElementById("resize-algorithm-combobox").addEventListener("change", changedAlgorithm); document.getElementById("resize-algorithm-combobox").addEventListener("change", changedAlgorithm);
} }
function resizeSprite() { function resizeSprite(event, ratio) {
let oldWidth, oldHeight;
let newWidth, newHeight; let newWidth, newHeight;
let imageDatas = []; let rsImageDatas = [];
let layerIndex = 0; let layerIndex = 0;
let imageDatasCopy = [];
oldWidth = layers[0].canvasSize[0];
oldHeight = layers[1].canvasSize[1];
rcPivot = "middle"; rcPivot = "middle";
// Updating values if the user didn't press enter // Updating values if the user didn't press enter
switch (document.activeElement.id) { switch (document.activeElement.id) {
case "rs-width-percentage": case "rs-width-percentage":
@ -65,20 +70,29 @@ function resizeSprite() {
break; break;
} }
if (ratio == null) {
newWidth = data.width; newWidth = data.width;
newHeight = data.height; newHeight = data.height;
}
console.log(newWidth + ", " + newHeight); else {
newWidth = layers[0].canvasSize[0] * ratio[0];
newHeight = layers[1].canvasSize[1] * ratio[1];
}
// Get all the image datas // Get all the image datas
for (let i=0; i<layers.length; i++) { for (let i=0; i<layers.length; i++) {
if (layers[i].menuEntry != null) { if (layers[i].menuEntry != null) {
imageDatas.push(layers[i].context.getImageData( rsImageDatas.push(layers[i].context.getImageData(
0, 0, layers[0].canvasSize[0], layers[0].canvasSize[1]) 0, 0, layers[0].canvasSize[0], layers[0].canvasSize[1])
); );
} }
} }
if (ratio == null) {
imageDatasCopy = rsImageDatas.slice();
new HistoryStateResizeSprite(newWidth / oldWidth, newHeight / oldHeight, currentAlgo, imageDatasCopy);
}
// Resizing the canvas // Resizing the canvas
resizeCanvas(null, {x: newWidth, y: newHeight}); resizeCanvas(null, {x: newWidth, y: newHeight});
@ -86,18 +100,26 @@ function resizeSprite() {
for (let i=0; i<layers.length; i++) { for (let i=0; i<layers.length; i++) {
if (layers[i].menuEntry != null) { if (layers[i].menuEntry != null) {
layers[i].context.putImageData( layers[i].context.putImageData(
resizeImageData(imageDatas[layerIndex], newWidth, newHeight, currentAlgo), 0, 0 resizeImageData(rsImageDatas[layerIndex], newWidth, newHeight, currentAlgo), 0, 0
); );
layers[i].updateLayerPreview();
layerIndex++; layerIndex++;
} }
} }
// Updating start values when I finish scaling the sprite // Updating start values when I finish scaling the sprite
// OPTIMIZABLE? Can't I just assign data to startData? Is js smart enough to understand? // OPTIMIZABLE? Can't I just assign data to startData? Is js smart enough to understand?
if (ratio == null) {
startData.width = data.width; startData.width = data.width;
startData.height = data.height; startData.height = data.height;
startData.widthPercentage = data.widthPercentage; }
startData.heightPercentage = data.heightPercentage; else {
startData.width = layers[0].canvasSize[0];
startData.height = layers[0].canvasSize[1];
}
startData.widthPercentage = 100;
startData.heightPercentage = 100;
closeDialogue(); closeDialogue();
} }