From d972f9c530a5dff534086b42788b793fea3b66f4 Mon Sep 17 00:00:00 2001 From: unsettledgames <47360416+unsettledgames@users.noreply.github.com> Date: Mon, 6 Dec 2021 11:26:42 +0100 Subject: [PATCH] Turned the PaleteBlock into an IIFE --- README.md | 7 +- js/Color.js | 12 + js/ColorModule.js | 2 +- js/{_colorPicker.js => ColorPicker.js} | 6 +- js/Dialogue.js | 2 +- js/{_featureToggles.js => FeatureToggles.js} | 0 js/PaletteBlock.js | 300 ++++++++++++++++++ js/ToolManager.js | 9 +- js/_paletteBlock.js | 311 ------------------- js/{_jscolor.js => lib/jscolor.js} | 0 js/pixel-editor.js | 8 +- views/popups/palette.hbs | 12 +- 12 files changed, 339 insertions(+), 330 deletions(-) rename js/{_colorPicker.js => ColorPicker.js} (99%) rename js/{_featureToggles.js => FeatureToggles.js} (100%) create mode 100644 js/PaletteBlock.js delete mode 100644 js/_paletteBlock.js rename js/{_jscolor.js => lib/jscolor.js} (100%) diff --git a/README.md b/README.md index 66ec0d6..a75d3d7 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ The next version is mostly focused on adding missing essential features and port Suggestions / Planned features: - Documentation - - Possibility to hide and resize menus (layers, palette) - Tiled mode - Load palette from LPE file @@ -26,8 +25,8 @@ Suggestions / Planned features: - Possibly add collaborate function - Code refactoring - - Find inefficient sections (nested loops, useless / inefficient parts) - - Create classes ResizableTool and SelectionTool. Make them inherit from Tool, avoid creating brush resizing functions each time for each tool that can be resized + - Find and fix inefficient sections (nested loops, useless / inefficient parts) + - Refactor the ColorPicker IIFE - Mobile - Touch equivalent for mouse clicks @@ -36,7 +35,7 @@ Suggestions / Planned features: - Fix popups - Polish: - - ctrl a to select everything / selection -> all, same for deselection + - CTRL+A to select everything / selection -> all, same for deselection - Warning windows for wrong inputs - Palette option remove unused colors - Move selection with arrows diff --git a/js/Color.js b/js/Color.js index e2ee851..71827f7 100644 --- a/js/Color.js +++ b/js/Color.js @@ -165,4 +165,16 @@ class Color { return {h: myH * 360, s: myS * 100, v: myV * 100}; } + + /** Converts a CSS colour eg rgb(x,y,z) to a hex string + * + * @param {*} rgb + */ + static cssToHex(rgb) { + rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); + function hex(x) { + return ("0" + parseInt(x).toString(16)).slice(-2); + } + return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); + } } \ No newline at end of file diff --git a/js/ColorModule.js b/js/ColorModule.js index e541c9c..80019a2 100644 --- a/js/ColorModule.js +++ b/js/ColorModule.js @@ -160,7 +160,7 @@ const ColorModule = (() => { const col = coloursList.children[i].style.backgroundColor; if (col.includes("rgb")) { - addColor(cssToHex(col)); + addColor(Color.cssToHex(col)); } else { addColor(col); diff --git a/js/_colorPicker.js b/js/ColorPicker.js similarity index 99% rename from js/_colorPicker.js rename to js/ColorPicker.js index 79e8eab..eae3659 100644 --- a/js/_colorPicker.js +++ b/js/ColorPicker.js @@ -773,7 +773,11 @@ const ColorPicker = (() => { } return { - init + init, + getSelectedColours, + updatePickerByHex, + updateSlidersByHex, + updateMiniPickerColour } })(); diff --git a/js/Dialogue.js b/js/Dialogue.js index c737261..ab687bf 100644 --- a/js/Dialogue.js +++ b/js/Dialogue.js @@ -44,7 +44,7 @@ const Dialogue = (() => { // If I'm opening the palette window, I initialize the colour picker if (dialogueName == 'palette-block' && Startup.documentCreated()) { ColorPicker.init(); - pbInit(); + PaletteBlock.init(); } //track google event diff --git a/js/_featureToggles.js b/js/FeatureToggles.js similarity index 100% rename from js/_featureToggles.js rename to js/FeatureToggles.js diff --git a/js/PaletteBlock.js b/js/PaletteBlock.js new file mode 100644 index 0000000..edcb09d --- /dev/null +++ b/js/PaletteBlock.js @@ -0,0 +1,300 @@ +const PaletteBlock = (() => { + // HTML elements + let coloursList = document.getElementById("palette-list"); + let rampMenu = document.getElementById("pb-ramp-options"); + let pbRampDialogue = document.getElementById("pb-ramp-dialogue"); + + // PaletteBlock-specific data + let currentSquareSize = coloursList.children[0].clientWidth; + let blockData = {blockWidth: 300, blockHeight: 320, squareSize: 40}; + let isRampSelecting = false; + let ramps = []; + let currentSelection = {startIndex:0, endIndex:0, startCoords:[], endCoords: [], name: "", colour: "", label: null}; + + + // Making the palette list sortable + new Sortable(document.getElementById("palette-list"), { + animation: 100, + onEnd: updateRampSelection + }); + // Listening for the palette block resize + new ResizeObserver(updateSizeData).observe(coloursList.parentElement); + + Events.on("click", "pb-addcolours", addColours); + Events.on("click", "pb-removecolours", removeColours); + + /** Listens for the mouse wheel, used to change the size of the squares in the palette list + * + */ + coloursList.parentElement.addEventListener("wheel", function (mouseEvent) { + // Only resize when pressing alt, used to distinguish between scrolling through the palette and + // resizing it + if (mouseEvent.altKey) { + resizeSquares(mouseEvent); + } + }); + + + // Initializes the palette block + function init() { + let simplePalette = document.getElementById("colors-menu"); + let childCount = coloursList.childElementCount; + + currentSquareSize = coloursList.children[0].clientWidth; + coloursList = document.getElementById("palette-list"); + + // Remove all the colours + for (let i=0; i endIndex) { + let tmp = startIndex; + startIndex = endIndex; + endIndex = tmp; + } + + for (let i=startIndex; i<=endIndex; i++) { + coloursList.removeChild(coloursList.children[startIndex]); + } + clearBorders(); + } + + /** Starts selecting a ramp. Saves the data needed to draw the outline. + * + * @param {*} mouseEvent + */ + function startRampSelection(mouseEvent) { + if (mouseEvent.which == 3) { + let index = getElementIndex(mouseEvent.target); + + isRampSelecting = true; + + currentSelection.startIndex = index; + currentSelection.endIndex = index; + + currentSelection.startCoords = getColourCoordinates(index); + currentSelection.endCoords = getColourCoordinates(index); + } + } + + /** Updates the outline for the current selection. + * + * @param {*} mouseEvent + */ + function updateRampSelection(mouseEvent) { + if (mouseEvent != null && mouseEvent.which == 3) { + currentSelection.endIndex = getElementIndex(mouseEvent.target); + } + + if (mouseEvent == null || mouseEvent.which == 3) { + let startCoords = getColourCoordinates(currentSelection.startIndex); + let endCoords = getColourCoordinates(currentSelection.endIndex); + + let startIndex = currentSelection.startIndex; + let endIndex = currentSelection.endIndex; + + if (currentSelection.startIndex > endIndex) { + let tmp = startIndex; + startIndex = endIndex; + endIndex = tmp; + + tmp = startCoords; + startCoords = endCoords; + endCoords = tmp; + } + + clearBorders(); + + for (let i=startIndex; i<=endIndex; i++) { + let currentSquare = coloursList.children[i]; + let currentCoords = getColourCoordinates(i); + let borderStyle = "3px solid white"; + let bordersToSet = []; + + // Deciding which borders to use to make the outline + if (i == 0 || i == startIndex) { + bordersToSet.push("border-left"); + } + if (currentCoords[1] == startCoords[1] || ((currentCoords[1] == startCoords[1] + 1)) && currentCoords[0] < startCoords[0]) { + bordersToSet.push("border-top"); + } + if (currentCoords[1] == endCoords[1] || ((currentCoords[1] == endCoords[1] - 1)) && currentCoords[0] > endCoords[0]) { + bordersToSet.push("border-bottom"); + } + if ((i == coloursList.childElementCount - 1) || (currentCoords[0] == Math.floor(blockData.blockWidth / blockData.squareSize) - 1) + || i == endIndex) { + bordersToSet.push("border-right"); + } + if (bordersToSet != []) { + currentSquare.style["box-sizing"] = "border-box"; + + for (let i=0; i 0 ? -5 : 5; + currentSquareSize += amount; + + for (let i=0; i { Events.onCustom("tool-shortcut", onShortcut); function onShortcut(tool) { + if (!Startup.documentCreated || Dialogue.isOpen()) + return; switchTool(tools[tool]); } function onMouseWheel(mouseEvent) { + if (!Startup.documentCreated || Dialogue.isOpen()) + return; + let mousePos = Input.getCursorPosition(mouseEvent); tools["zoom"].onMouseWheel(mousePos, mouseEvent.deltaY < 0 ? 'in' : 'out'); } function onMouseDown(mouseEvent) { - if (!Startup.documentCreated()) + if (!Startup.documentCreated() || Dialogue.isOpen()) return; let mousePos = Input.getCursorPosition(mouseEvent); @@ -70,7 +75,7 @@ const ToolManager = (() => { } function onMouseMove(mouseEvent) { - if (!Startup.documentCreated()) + if (!Startup.documentCreated() || Dialogue.isOpen()) return; let mousePos = Input.getCursorPosition(mouseEvent); // Call the hover event diff --git a/js/_paletteBlock.js b/js/_paletteBlock.js deleted file mode 100644 index ac480ab..0000000 --- a/js/_paletteBlock.js +++ /dev/null @@ -1,311 +0,0 @@ -/** INIT is called when it shouldn't **/ - -let coloursList = document.getElementById("palette-list"); - -let rampMenu = document.getElementById("pb-ramp-options"); -let pbRampDialogue = document.getElementById("pb-ramp-dialogue"); - -let currentSquareSize = coloursList.children[0].clientWidth; -let blockData = {blockWidth: 300, blockHeight: 320, squareSize: 40}; -let isRampSelecting = false; -let ramps = []; -let currentSelection = {startIndex:0, endIndex:0, startCoords:[], endCoords: [], name: "", colour: "", label: null}; - -// Making the palette list sortable -new Sortable(document.getElementById("palette-list"), { - animation: 100, - onEnd: updateRampSelection -}); - -// Listening for the palette block resize -new ResizeObserver(updateSizeData).observe(coloursList.parentElement); - -// Initializes the palette block -function pbInit() { - let simplePalette = document.getElementById("colors-menu"); - let childCount = coloursList.childElementCount; - - currentSquareSize = coloursList.children[0].clientWidth; - coloursList = document.getElementById("palette-list"); - - // Remove all the colours - for (let i=0; i endIndex) { - let tmp = startIndex; - startIndex = endIndex; - endIndex = tmp; - } - - for (let i=startIndex; i<=endIndex; i++) { - coloursList.removeChild(coloursList.children[startIndex]); - } - clearBorders(); -} - -/** Starts selecting a ramp. Saves the data needed to draw the outline. - * - * @param {*} mouseEvent - */ -function startRampSelection(mouseEvent) { - if (mouseEvent.which == 3) { - let index = getElementIndex(mouseEvent.target); - - isRampSelecting = true; - - currentSelection.startIndex = index; - currentSelection.endIndex = index; - - currentSelection.startCoords = getColourCoordinates(index); - currentSelection.endCoords = getColourCoordinates(index); - } -} - -/** Updates the outline for the current selection. - * - * @param {*} mouseEvent - */ -function updateRampSelection(mouseEvent) { - if (mouseEvent != null && mouseEvent.which == 3) { - currentSelection.endIndex = getElementIndex(mouseEvent.target); - } - - if (mouseEvent == null || mouseEvent.which == 3) { - let startCoords = getColourCoordinates(currentSelection.startIndex); - let endCoords = getColourCoordinates(currentSelection.endIndex); - - let startIndex = currentSelection.startIndex; - let endIndex = currentSelection.endIndex; - - if (currentSelection.startIndex > endIndex) { - let tmp = startIndex; - startIndex = endIndex; - endIndex = tmp; - - tmp = startCoords; - startCoords = endCoords; - endCoords = tmp; - } - - clearBorders(); - - for (let i=startIndex; i<=endIndex; i++) { - let currentSquare = coloursList.children[i]; - let currentCoords = getColourCoordinates(i); - let borderStyle = "3px solid white"; - let bordersToSet = []; - - // Deciding which borders to use to make the outline - if (i == 0 || i == startIndex) { - bordersToSet.push("border-left"); - } - if (currentCoords[1] == startCoords[1] || ((currentCoords[1] == startCoords[1] + 1)) && currentCoords[0] < startCoords[0]) { - bordersToSet.push("border-top"); - } - if (currentCoords[1] == endCoords[1] || ((currentCoords[1] == endCoords[1] - 1)) && currentCoords[0] > endCoords[0]) { - bordersToSet.push("border-bottom"); - } - if ((i == coloursList.childElementCount - 1) || (currentCoords[0] == Math.floor(blockData.blockWidth / blockData.squareSize) - 1) - || i == endIndex) { - bordersToSet.push("border-right"); - } - if (bordersToSet != []) { - currentSquare.style["box-sizing"] = "border-box"; - - for (let i=0; i 0 ? -5 : 5; - currentSquareSize += amount; - - for (let i=0; i
    -
  • -
  • +
  • +
- - + +
\ No newline at end of file