From 21dd47c2b0e417b35155809a75d9f364a0a52d21 Mon Sep 17 00:00:00 2001 From: unsettledgames <47360416+unsettledgames@users.noreply.github.com> Date: Mon, 6 Dec 2021 20:12:57 +0100 Subject: [PATCH] Finished refactoring, moved sprite scaling functions in File --- js/Canvas.js | 0 js/EditorState.js | 1 - js/File.js | 375 +++++++++++++++++++++++++++++++- js/FileManager.js | 12 +- js/History.js | 8 +- js/Startup.js | 3 +- js/_resizeCanvas.js | 0 js/_resizeSprite.js | 396 ---------------------------------- js/pixel-editor.js | 11 +- js/tools/FillTool.js | 2 +- js/tools/MoveSelectionTool.js | 8 +- js/tools/RectangleTool.js | 2 +- refactor_dependencies | 12 ++ views/main-menu.hbs | 2 +- 14 files changed, 405 insertions(+), 427 deletions(-) delete mode 100644 js/Canvas.js delete mode 100644 js/_resizeCanvas.js delete mode 100644 js/_resizeSprite.js create mode 100644 refactor_dependencies diff --git a/js/Canvas.js b/js/Canvas.js deleted file mode 100644 index e69de29..0000000 diff --git a/js/EditorState.js b/js/EditorState.js index c8cce1d..ee1761a 100644 --- a/js/EditorState.js +++ b/js/EditorState.js @@ -52,7 +52,6 @@ const EditorState = (() => { } function chooseMode() { - console.log("Here"); let prevMode = pixelEditorMode.toLowerCase(); if (pixelEditorMode === "Basic") { diff --git a/js/File.js b/js/File.js index 8b0bf82..6d2cf2b 100644 --- a/js/File.js +++ b/js/File.js @@ -22,6 +22,18 @@ class File { // Border offsets rcBorders = {left: 0, right: 0, top: 0, bottom: 0}; + // Sprite scaling attributes + // Should I keep the sprite ratio? + keepRatio = true; + // Used to store the current ratio + currentRatio = undefined; + // The currenty selected resizing algorithm (nearest-neighbor or bilinear-interpolation) + currentAlgo = 'nearest-neighbor'; + // Current resize data + data = {width: 0, height: 0, widthPercentage: 100, heightPercentage: 100}; + // Start resize data + startData = {width: 0, height:0, widthPercentage: 100, heightPercentage: 100}; + // Sprite scaling attributes openResizeCanvasWindow() { @@ -97,7 +109,6 @@ class File { * @param {*} saveHistory Should I save the history? You shouldn't if you're undoing */ resizeCanvas(event, size, customData, saveHistory = true) { - console.log("resizing"); let imageDatas = []; let leftOffset = 0; let topOffset = 0; @@ -314,6 +325,368 @@ class File { this.currentPivotObject = event.target; this.currentPivotObject.classList.add("rc-selected-pivot"); } + + /** Opens the sprite resizing window + * + */ + openResizeSpriteWindow() { + // Inits the sprie resize inputs + this.initResizeSpriteInputs(); + + // Computing the current ratio + this.currentRatio = currFile.canvasSize[0] / currFile.canvasSize[1]; + + // Initializing the input fields + this.data.width = currFile.canvasSize[0]; + this.data.height = currFile.canvasSize[1]; + + this.startData.width = parseInt(this.data.width); + this.startData.height = parseInt(this.data.height); + this.startData.heightPercentage = 100; + this.startData.widthPercentage = 100; + + // Opening the pop up now that it's ready + Dialogue.showDialogue('resize-sprite'); + } + + /** Initalizes the input values and binds the elements to their events + * + */ + initResizeSpriteInputs() { + document.getElementById("rs-width").value = currFile.canvasSize[0]; + document.getElementById("rs-height").value = currFile.canvasSize[1]; + + document.getElementById("rs-width-percentage").value = 100; + document.getElementById("rs-height-percentage").value = 100; + + document.getElementById("rs-keep-ratio").checked = true; + + Events.on("change", "rs-width", this.changedWidth.bind(this)); + Events.on("change", "rs-height", this.changedHeight.bind(this)); + + Events.on("change", "rs-width-percentage", this.changedWidthPercentage.bind(this)); + Events.on("change", "rs-height-percentage", this.changedHeightPercentage.bind(this)); + + Events.on("click", "resize-sprite-confirm", this.resizeSprite.bind(this)); + Events.on("click", "rs-keep-ratio", this.toggleRatio.bind(this)); + Events.on("change", "resize-algorithm-combobox", this.changedAlgorithm.bind(this)); + } + + /** Resizes (scales) the sprite + * + * @param {*} event + * @param {*} ratio Keeps infos about the x ratio and y ratio + */ + resizeSprite(event, ratio) { + // Old data + let oldWidth, oldHeight; + // New data + let newWidth, newHeight; + // Current imageDatas + let rsImageDatas = []; + // Index that will be used a few lines below + let layerIndex = 0; + // Copy of the imageDatas that will be stored in the history + let imageDatasCopy = []; + + oldWidth = currFile.canvasSize[0]; + oldHeight = currFile.canvasSize[1]; + this.rcPivot = "middle"; + + // Updating values if the user didn't press enter + switch (document.activeElement.id) { + case "rs-width-percentage": + this.changedWidthPercentage(); + break; + case "rs-width": + this.changedWidth(); + break; + case "rs-height-percentage": + this.changedHeightPercentage(); + break; + case "rs-height": + this.changedHeight(); + break; + default: + // In this case everything has been updated correctly + break; + } + + // Computing newWidth and newHeight + if (ratio == null) { + newWidth = this.data.width; + newHeight = this.data.height; + } + else { + newWidth = currFile.canvasSize[0] * ratio[0]; + newHeight = currFile.canvasSize[1] * ratio[1]; + } + + // Get all the image datas + for (let i=0; i { } Util.setValue('lpe-file-name', fileName); - Events.on("click", "save-project-confirm", saveProject); - Dialogue.showDialogue('save-project', false); } @@ -38,9 +36,7 @@ const FileManager = (() => { } Util.setValue('export-file-name', fileName); - Events.on("click", "export-confirm", exportProject); - Dialogue.showDialogue('export', false); } @@ -66,11 +62,11 @@ const FileManager = (() => { //create name let fileName = Util.getValue("export-file-name"); //set download link - var linkHolder = document.getElementById('save-image-link-holder'); + let linkHolder = document.getElementById('save-image-link-holder'); // Creating a tmp canvas to flatten everything - var exportCanvas = document.createElement("canvas"); - var emptyCanvas = document.createElement("canvas"); - var layersCopy = currFile.layers.slice(); + let exportCanvas = document.createElement("canvas"); + let emptyCanvas = document.createElement("canvas"); + let layersCopy = currFile.layers.slice(); exportCanvas.width = currFile.canvasSize[0]; exportCanvas.height = currFile.canvasSize[1]; diff --git a/js/History.js b/js/History.js index 1afe5d9..89f505c 100644 --- a/js/History.js +++ b/js/History.js @@ -104,8 +104,8 @@ class HistoryState { this.undo = function() { let layerIndex = 0; - currentAlgo = algo; - resizeSprite(null, [1 / this.xRatio, 1 / this.yRatio]); + currFile.currentAlgo = algo; + currFile.resizeSprite(null, [1 / this.xRatio, 1 / this.yRatio]); // Also putting the old data for (let i=0; i { let firstPixel = true; - let editorMode = "Basic"; let splashPostfix = ''; Events.on('click', 'create-button', create, false); @@ -142,7 +141,7 @@ const Startup = (() => { else { //if this palette isnt the one specified in the url, then reset the url if (!palettes[selectedPalette].specified) - history.pushState(null, null, '/pixel-editor'); + history.pushState(null, null, '/pixel-editor'); //fill the palette with specified colours ColorModule.createColorPalette(palettes[selectedPalette].colors); diff --git a/js/_resizeCanvas.js b/js/_resizeCanvas.js deleted file mode 100644 index e69de29..0000000 diff --git a/js/_resizeSprite.js b/js/_resizeSprite.js deleted file mode 100644 index 22a2069..0000000 --- a/js/_resizeSprite.js +++ /dev/null @@ -1,396 +0,0 @@ -// REFACTOR: method of File class probably - -/* This scripts contains all the code used to handle the sprite scaling */ -// Should I keep the sprite ratio? -let keepRatio = true; -// Used to store the current ratio -let currentRatio; -// The currenty selected resizing algorithm (nearest-neighbor or bilinear-interpolation) -let currentAlgo = 'nearest-neighbor'; -// Current resize data -let data = {width: 0, height: 0, widthPercentage: 100, heightPercentage: 100}; -// Start resize data -let startData = {width: 0, height:0, widthPercentage: 100, heightPercentage: 100}; - -/** Opens the sprite resizing window - * - */ -function openResizeSpriteWindow() { - // Inits the sprie resize inputs - initResizeSpriteInputs(); - - // Computing the current ratio - currentRatio = currFile.canvasSize[0] / currFile.canvasSize[1]; - - console.log("Current ratio: " + currentRatio); - - // Initializing the input fields - data.width = currFile.canvasSize[0]; - data.height = currFile.canvasSize[1]; - - startData.width = parseInt(data.width); - startData.height = parseInt(data.height); - startData.heightPercentage = 100; - startData.widthPercentage = 100; - - // Opening the pop up now that it's ready - Dialogue.showDialogue('resize-sprite'); -} - -/** Initalizes the input values and binds the elements to their events - * - */ -function initResizeSpriteInputs() { - document.getElementById("rs-width").value = currFile.canvasSize[0]; - document.getElementById("rs-height").value = currFile.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); - document.getElementById("resize-algorithm-combobox").addEventListener("change", changedAlgorithm); -} - -/** Resizes (scales) the sprite - * - * @param {*} event - * @param {*} ratio Keeps infos about the x ratio and y ratio - */ -function resizeSprite(event, ratio) { - // Old data - let oldWidth, oldHeight; - // New data - let newWidth, newHeight; - // Current imageDatas - let rsImageDatas = []; - // Index that will be used a few lines below - let layerIndex = 0; - // Copy of the imageDatas that will be stored in the history - let imageDatasCopy = []; - - oldWidth = currFile.canvasSize[0]; - oldHeight = currFile.canvasSize[1]; - 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; - } - - // Computing newWidth and newHeight - if (ratio == null) { - newWidth = data.width; - newHeight = data.height; - } - else { - newWidth = currFile.canvasSize[0] * ratio[0]; - newHeight = currFile.canvasSize[1] * ratio[1]; - } - - // Get all the image datas - for (let i=0; i
    -
  • +