From 5f4547cdf41ce09f93ce6c9cf5cbe9514d00bf8e Mon Sep 17 00:00:00 2001 From: DMKAL <83312683+tirzah2@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:16:58 +0100 Subject: [PATCH 1/3] Update style2.css --- styles/style2.css | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/styles/style2.css b/styles/style2.css index ca92779..e14a275 100644 --- a/styles/style2.css +++ b/styles/style2.css @@ -1,7 +1,8 @@ .minpaint { z-index: 100; - min-width: 500px; + min-width: 750px; height: unset !important; +width: unset !important; min-height: 500px !important; max-height: fit-content; resize: both; @@ -24,7 +25,7 @@ height: unset !important; min-height: 500px !important; border: 0px; resize: both; - min-width: 500px; + min-width: 750px; } .minpaint .window-content { From a92df18367aace33994649649b7c2a0258a16f04 Mon Sep 17 00:00:00 2001 From: DMKAL <83312683+tirzah2@users.noreply.github.com> Date: Mon, 5 Aug 2024 06:39:55 -0800 Subject: [PATCH 2/3] Add files via upload --- js/minipaint.js | 84 +++++++++++++++++++++++++++++ styles/style2.css | 2 +- templates/minipaint.html | 113 ++++++++++++++++++++++----------------- 3 files changed, 150 insertions(+), 49 deletions(-) diff --git a/js/minipaint.js b/js/minipaint.js index ad28681..a340dcc 100644 --- a/js/minipaint.js +++ b/js/minipaint.js @@ -176,6 +176,90 @@ async function replaceTile() { }, "image/png"); } +async function loadToken() { + const selectedTokens = canvas.tokens.controlled; + + if (selectedTokens.length !== 1) { + ui.notifications.warn("Please select exactly one token."); + return; + } + + const token = selectedTokens[0]; + const tokenSrc = token.document.texture.src; + + if (!tokenSrc) { + ui.notifications.error("Token image source not found."); + return; + } + + const image = new Image(); + image.src = tokenSrc; + + image.onload = () => { + const Layers = document.getElementById('myFrame').contentWindow.Layers; + const name = image.src.replace(/^.*[\\\/]/, ''); + const new_layer = { + name: name, + type: 'image', + data: image, + width: image.naturalWidth || image.width, + height: image.naturalHeight || image.height, + width_original: image.naturalWidth || image.width, + height_original: image.naturalHeight || image.height, + }; + Layers.insert(new_layer); + }; + + image.onerror = () => { + ui.notifications.error("Failed to load the token image."); + }; +} +async function replaceToken() { + const selectedTokens = canvas.tokens.controlled; + + if (selectedTokens.length !== 1) { + ui.notifications.warn("Please select exactly one token."); + return; + } + + const token = selectedTokens[0]; + const originalPath = token.document.texture.src; + const directoryPath = originalPath.substring(0, originalPath.lastIndexOf("/")); + + const iframe = document.getElementById('myFrame'); + const minipaintCanvas = iframe.contentWindow.document.querySelector('#canvas_minipaint'); + + if (!minipaintCanvas) { + ui.notifications.error("Could not find the miniPaint canvas."); + return; + } + + // Convert the miniPaint canvas content to a Blob + minipaintCanvas.toBlob(async (blob) => { + if (!blob) { + ui.notifications.error("Failed to create a Blob from the canvas."); + return; + } + + // Generate a random filename + const randomFilename = generateRandomFilename() + ".png"; + + // Create a File object from the Blob with the random filename + const file = new File([blob], randomFilename, { type: blob.type }); + + // Upload the image file to the same directory as the original token image + const response = await FilePicker.upload("data", directoryPath, file, {}); + + if (response.path) { + // Update the token with the new image source + await token.document.update({ "texture.src": response.path }); + + ui.notifications.info("Token image replaced successfully."); + } else { + ui.notifications.error("Failed to upload the modified image."); + } + }, "image/png"); +} async function showCanvasInImagePopout() { diff --git a/styles/style2.css b/styles/style2.css index e14a275..8144d2f 100644 --- a/styles/style2.css +++ b/styles/style2.css @@ -45,4 +45,4 @@ width: unset !important; width: 100%; height: 100%; border: 0; -} +} \ No newline at end of file diff --git a/templates/minipaint.html b/templates/minipaint.html index 29baa6a..81ffb5a 100644 --- a/templates/minipaint.html +++ b/templates/minipaint.html @@ -38,57 +38,74 @@ // Check if iframe and its content are accessible if (iframe && iframe.contentWindow) { - const iframeDocument = iframe.contentWindow.document; - - // Find the menu bar element - const menuBar = iframeDocument.querySelector('.menu_bar'); - - if (menuBar) { - // Create the Load Tile menu item - const loadTileMenuItem = iframeDocument.createElement('li'); - loadTileMenuItem.innerHTML = ` - -`; - -// Browse Foundry Menu Item -const loadBrowseMenuItem = iframeDocument.createElement('li'); -loadBrowseMenuItem.innerHTML = ` - -`; - -// Replace Tile Menu Item -const replaceTileMenuItem = iframeDocument.createElement('li'); -replaceTileMenuItem.innerHTML = ` - -`; -const sendTochat = iframeDocument.createElement('li'); -sendTochat.innerHTML = ` - -`; - -const adddot = iframeDocument.createElement('li'); -adddot.innerHTML = ` -
•
-`; - - // Insert the new menu items at the beginning of the menu bar - menuBar.insertBefore(adddot, menuBar.firstChild); - menuBar.insertBefore(sendTochat, menuBar.firstChild); - menuBar.insertBefore(replaceTileMenuItem, menuBar.firstChild); - menuBar.insertBefore(loadBrowseMenuItem, menuBar.firstChild); - menuBar.insertBefore(loadTileMenuItem, menuBar.firstChild); - } + const iframeDocument = iframe.contentWindow.document; + + // Find the menu bar element + const menuBar = iframeDocument.querySelector('.menu_bar'); + + if (menuBar) { + // Create the Load Tile and Load Token menu items + const loadTileMenuItem = iframeDocument.createElement('li'); + loadTileMenuItem.innerHTML = ` + + `; + const loadTokenMenuItem = iframeDocument.createElement('li'); + loadTokenMenuItem.innerHTML = ` + + `; + + // Browse Foundry Menu Item + const loadBrowseMenuItem = iframeDocument.createElement('li'); + loadBrowseMenuItem.innerHTML = ` + + `; + + // Create the Replace Tile and Replace Token menu items + const replaceTileMenuItem = iframeDocument.createElement('li'); + replaceTileMenuItem.innerHTML = ` + + `; + const replaceTokenMenuItem = iframeDocument.createElement('li'); + replaceTokenMenuItem.innerHTML = ` + + `; + + const sendTochat = iframeDocument.createElement('li'); + sendTochat.innerHTML = ` + + `; + + const adddot = iframeDocument.createElement('li'); + adddot.innerHTML = ` +
•
+ `; + + // Insert the new menu items at the beginning of the menu bar + menuBar.insertBefore(adddot, menuBar.firstChild); + menuBar.insertBefore(sendTochat, menuBar.firstChild); + menuBar.insertBefore(replaceTokenMenuItem, menuBar.firstChild); + menuBar.insertBefore(replaceTileMenuItem, menuBar.firstChild); + menuBar.insertBefore(loadBrowseMenuItem, menuBar.firstChild); + menuBar.insertBefore(loadTokenMenuItem, menuBar.firstChild); + menuBar.insertBefore(loadTileMenuItem, menuBar.firstChild); } } + +} + // Attach an event listener to the iframe to run the function once it's loaded document.getElementById('myFrame').addEventListener('load', injectMenuItems); From b76eabaa82b3f0ae040cf563f1676a29e3df9334 Mon Sep 17 00:00:00 2001 From: DMKAL <83312683+tirzah2@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:40:31 +0100 Subject: [PATCH 3/3] Update module.json --- module.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/module.json b/module.json index 733bf0e..e1817b0 100644 --- a/module.json +++ b/module.json @@ -2,7 +2,7 @@ "name": "minipaint", "title": "miniPaint", "description": "Adds a button in the Tile Controls to open miniPaint for editing Tiles, Paint with a drawing tablet and more. ", - "version": "1.1", + "version": "1.2", "author": "DMKal", "minimumCoreVersion": "11", "compatibleCoreVersion": "11", @@ -29,5 +29,5 @@ "url": "https://github.com/tirzah2/minipaint", "readme": "https://github.com/tirzah2/minipaint/README.md", "manifest": "https://github.com/tirzah2/minipaint/releases/latest/download/module.json", - "download": "https://github.com/tirzah2/minipaint/releases/download/1.1/module.zip" + "download": "https://github.com/tirzah2/minipaint/releases/download/1.2/module.zip" }