Repository navigation
Expand file tree
/
Copy pathminipaint.html
More file actions
644 lines (550 loc) · 26 KB
/
Copy pathminipaint.html
File metadata and controls
644 lines (550 loc) · 26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
<div class="minipaint-app">
<iframe id="myFrame" style="width:100%; border:0;" src="modules/minipaint/index.html" allow="camera"></iframe>
</div>
</div>
<script>
// Function to hide specific elements in the iframe
function hideElementsInIframe() {
const iframe = document.getElementById('myFrame');
// Check if iframe and its content are accessible
if (iframe && iframe.contentWindow) {
const iframeDocument = iframe.contentWindow.document;
// Use querySelector to find the specific elements by their selectors
const helpMenuItem = iframeDocument.querySelector('#main_menu_0_7');
const logoElement = iframeDocument.querySelector('a.logo');
// Hide the help menu item if found
if (helpMenuItem) {
helpMenuItem.style.display = 'none';
}
// Hide the logo element if found
if (logoElement) {
logoElement.style.display = 'none';
}
}
}
// Attach an event listener to the iframe to run the function once it's loaded
document.getElementById('myFrame').addEventListener('load', hideElementsInIframe);
// Function to inject the Load Tile and Replace Tile menu items into the menu bar
function injectMenuItems() {
const iframe = document.getElementById('myFrame');
// Check if iframe and its content are accessible
if (iframe && iframe.contentWindow) {
const iframeDocument = iframe.contentWindow.document;
// test
const fileMenu = iframeDocument.querySelector('#main_menu_0_0');
const imageMenu = iframeDocument.querySelector('#main_menu_0_3'); // Adjust ID if necessary
if (imageMenu) {
// Wait until the Image dropdown is clicked to expand the items
imageMenu.addEventListener('click', () => {
const infoMenuItem = iframeDocument.querySelector('#main_menu_1_0'); // Adjust the ID if necessary to target "Information ..."
if (infoMenuItem) {
// Create the Pop! menu item
const popMenuItem = iframeDocument.createElement('a');
popMenuItem.id = "main_menu_pop";
popMenuItem.role = "menuitem";
popMenuItem.tabindex = "-1";
popMenuItem.ariaHaspopup = "false";
popMenuItem.href = "javascript:void(0)";
popMenuItem.target = "_self";
popMenuItem.dataset.level = "70";
popMenuItem.dataset.index = "pop";
popMenuItem.innerHTML = `
<span class="name">Show Players</span>
`;
popMenuItem. => window.parent.showCanvasInImagePopout();
// Insert the Pop! menu item above the "Information ..." item
infoMenuItem.parentNode.insertBefore(popMenuItem, infoMenuItem);
}
});
}
if (fileMenu) {
// Wait until the File dropdown is clicked to expand the items
fileMenu.addEventListener('click', () => {
const saveAsMenuItem = iframeDocument.querySelector('#main_menu_1_6');
const newMenuItem = iframeDocument.querySelector('#main_menu_1_0');
const mainMenuLeft = parseFloat(window.getComputedStyle(fileMenu).left); // Get the left style as a number
const adjustedLeft = mainMenuLeft - 151.854;
if (saveAsMenuItem && newMenuItem) {
// Create New Token menu item
const newTokenMenuItem = iframeDocument.createElement('a');
newTokenMenuItem.id = "main_menu_new_token";
newTokenMenuItem.role = "menuitem";
newTokenMenuItem.tabindex = "-1";
newTokenMenuItem.ariaHaspopup = "false";
newTokenMenuItem.href = "javascript:void(0)";
newTokenMenuItem.target = "_self";
newTokenMenuItem.dataset.level = "70";
newTokenMenuItem.dataset.index = "5";
newTokenMenuItem.innerHTML = `
<span class="name">New Simple Token</span>
`;
newTokenMenuItem. => window.parent.open_tokenframe();
const saveAsRingMenuItem = iframeDocument.createElement('a');
saveAsRingMenuItem.id = "main_menu_save_as_ring";
saveAsRingMenuItem.role = "menuitem";
saveAsRingMenuItem.tabindex = "-1";
saveAsRingMenuItem.ariaHaspopup = "false";
saveAsRingMenuItem.href = "javascript:void(0)";
saveAsRingMenuItem.target = "_self";
saveAsRingMenuItem.dataset.level = "72";
saveAsRingMenuItem.dataset.index = "7";
saveAsRingMenuItem.innerHTML = `
<span class="name">Save as Frame</span>
`;
saveAsRingMenuItem. => saveCanvasToFrames();
const ringsBrowserMenuItem = iframeDocument.createElement('a');
ringsBrowserMenuItem.id = "main_menu_rings_browser";
ringsBrowserMenuItem.role = "menuitem";
ringsBrowserMenuItem.tabindex = "-1";
ringsBrowserMenuItem.ariaHaspopup = "false";
ringsBrowserMenuItem.href = "javascript:void(0)";
ringsBrowserMenuItem.target = "_self";
ringsBrowserMenuItem.dataset.level = "71";
ringsBrowserMenuItem.dataset.index = "10";
ringsBrowserMenuItem.innerHTML = `
<span class="name">Frame Browser</span>
`;
ringsBrowserMenuItem. => galleryMP.render(true);
// Create Save Token menu item
const saveTokenMenuItem = iframeDocument.createElement('a');
saveTokenMenuItem.id = "main_menu_save_proto";
saveTokenMenuItem.role = "menuitem";
saveTokenMenuItem.tabindex = "-1";
saveTokenMenuItem.ariaHaspopup = "false";
saveTokenMenuItem.href = "javascript:void(0)";
saveTokenMenuItem.target = "_self";
saveTokenMenuItem.dataset.level = "1";
saveTokenMenuItem.dataset.index = "10";
saveTokenMenuItem.innerHTML = `
<span class="name">Save Token</span>
`;
saveTokenMenuItem. => window.parent.replaceActorTexture('prototypeToken');
// Create Save Portrait menu item
const savePortraitMenuItem = iframeDocument.createElement('a');
savePortraitMenuItem.id = "main_menu_save_actor_image";
savePortraitMenuItem.role = "menuitem";
savePortraitMenuItem.tabindex = "-1";
savePortraitMenuItem.ariaHaspopup = "false";
savePortraitMenuItem.href = "javascript:void(0)";
savePortraitMenuItem.target = "_self";
savePortraitMenuItem.dataset.level = "1";
savePortraitMenuItem.dataset.index = "10";
savePortraitMenuItem.innerHTML = `
<span class="name">Save Portrait</span>
`;
savePortraitMenuItem. => window.parent.replaceActorTexture('img');
// Create Save Item menu item
const saveItemMenuItem = iframeDocument.createElement('a');
saveItemMenuItem.id = "main_menu_save_item_image";
saveItemMenuItem.role = "menuitem";
saveItemMenuItem.tabindex = "-1";
saveItemMenuItem.ariaHaspopup = "false";
saveItemMenuItem.href = "javascript:void(0)";
saveItemMenuItem.target = "_self";
saveItemMenuItem.dataset.level = "1";
saveItemMenuItem.dataset.index = "70";
saveItemMenuItem.innerHTML = `
<span class="name">Save Item</span>
`;
saveItemMenuItem. => window.parent.replaceItemTexture();
//test
const newFrameMenuItem = iframeDocument.createElement('a');
newFrameMenuItem.id = "main_menu_new_frame";
newFrameMenuItem.role = "menuitem";
newFrameMenuItem.tabindex = "-1";
newFrameMenuItem.ariaHaspopup = "true";
newFrameMenuItem.ariaExpanded = "false";
newFrameMenuItem.href = "javascript:void(0)";
newFrameMenuItem.target = "_self";
newFrameMenuItem.dataset.level = "71";
newFrameMenuItem.dataset.index = "";
newFrameMenuItem.innerHTML = `
<span class="name"><- Templates</span>
`;
// Create the submenu (dropdown) for the New Frame options
const subMenu = iframeDocument.createElement('ul');
subMenu.className = 'menu_dropdown';
subMenu.role = 'menu';
subMenu.style.display = 'none'; // Hidden by default
subMenu.style.position = 'absolute';
subMenu.style.zIndex = '1000';
subMenu.style.left = `221px`; // Adjusted left position
subMenu.style.top = '30px'; // Adjusted top position
// Create Frame 1, Frame 2, and Frame 3 menu items
['Frame 1', 'Frame 2', 'Frame 3','Token 1'].forEach((frame, index) => {
const frameItem = iframeDocument.createElement('li');
frameItem.innerHTML = `
<a href="javascript:void(0)" role="menuitem" tabindex="-1" data-index="frame_${index + 1}">
${frame}
</a>
`;
// Attach the click event listener to each frame item
frameItem. => {
switch (index) {
case 0:
window.parent.openFrame1();
break;
case 1:
window.parent.openFrame2();
break;
case 2:
window.parent.openFrame3();
break;
case 3:
window.parent.openFrame4();
break;
}
subMenu.style.display = 'none';
newFrameMenuItem.ariaExpanded = 'false';
};
// Append frame item to the submenu
subMenu.appendChild(frameItem);
});
// Toggle submenu visibility on click
newFrameMenuItem. => {
e.stopPropagation(); // Prevent the click event from bubbling up
const isVisible = subMenu.style.display === 'block';
subMenu.style.display = isVisible ? 'none' : 'block';
newFrameMenuItem.ariaExpanded = !isVisible;
};
// Hide the submenu if clicking outside
iframeDocument.addEventListener('click', (e) => {
if (subMenu.style.display === 'block' && !subMenu.contains(e.target)) {
subMenu.style.display = 'none';
newFrameMenuItem.ariaExpanded = 'false';
}
});
// Insert the New Frame menu item above the "New" item
// Insert the New Frame menu item above the "New" item
newMenuItem.parentNode.insertBefore(newFrameMenuItem, newMenuItem);
// Insert the submenu inside the "Main Menu" structure
const mainMenu = iframeDocument.querySelector('.main_menu');
if (mainMenu) {
mainMenu.appendChild(subMenu);
}
const templateMenuItem = iframeDocument.querySelector('#main_menu_new_frame');
templateMenuItem.parentNode.insertBefore(saveAsRingMenuItem, templateMenuItem.nextSibling);
// Insert the New Simple Token menu item above the "New" item
newMenuItem.parentNode.insertBefore(newTokenMenuItem, newFrameMenuItem);
newMenuItem.parentNode.insertBefore(ringsBrowserMenuItem, newTokenMenuItem.nextSibling);
// Insert the new menu items above the "Save As" item
saveAsMenuItem.parentNode.insertBefore(savePortraitMenuItem, saveAsMenuItem);
if (!window.parent.minipaintItem) {
saveItemMenuItem.style.display = 'none';
} else {
saveItemMenuItem.style.display = 'block';
saveItemMenuItem.style.backgroundColor = 'darkgreen';
saveItemMenuItem.style.border = '3px solid lightgreen';
saveItemMenuItem.style.color = 'lightgreen';
}
if (window.parent.tidyType === 'img') {
savePortraitMenuItem.style.backgroundColor = 'darkgreen';
savePortraitMenuItem.style.border = '3px solid lightgreen';
savePortraitMenuItem.style.color = 'lightgreen';
//saveTokenMenuItem.style.display = 'none'; // Hide Save Token button
} else if (window.parent.tidyType === 'prototypeToken') {
saveTokenMenuItem.style.backgroundColor = 'darkgreen';
saveTokenMenuItem.style.border = '3px solid lightgreen';
saveTokenMenuItem.style.color = 'lightgreen';
//savePortraitMenuItem.style.display = 'none'; // Hide Save Portrait button
} else {
// Hide both buttons if tidyType is null, undefined, or any other value
saveTokenMenuItem.style.display = 'none';
savePortraitMenuItem.style.display = 'none';
}
const OpenGifMenuItem = iframeDocument.createElement('a');
OpenGifMenuItem.id = "main_menu_open_gif";
OpenGifMenuItem.role = "menuitem";
OpenGifMenuItem.tabindex = "-1";
OpenGifMenuItem.ariaHaspopup = "false";
OpenGifMenuItem.href = "javascript:void(0)";
OpenGifMenuItem.target = "_self";
OpenGifMenuItem.dataset.level = "79";
OpenGifMenuItem.dataset.index = "10";
OpenGifMenuItem.innerHTML = `
<span class="name">Open Animation</span>
`;
OpenGifMenuItem. => openGif();
// Insert "Save as GIF" menu item after "Save as Frame"
const saveAsGifMenuItem = iframeDocument.createElement('a');
saveAsGifMenuItem.id = "main_menu_save_as_gif";
saveAsGifMenuItem.role = "menuitem";
saveAsGifMenuItem.tabindex = "-1";
saveAsGifMenuItem.ariaHaspopup = "false";
saveAsGifMenuItem.href = "javascript:void(0)";
saveAsGifMenuItem.target = "_self";
saveAsGifMenuItem.dataset.level = "73";
saveAsGifMenuItem.dataset.index = "8";
saveAsGifMenuItem.innerHTML = `
<span class="name">Save Animation</span>
`;
saveAsGifMenuItem. => window.parent.saveAnimation();
saveAsRingMenuItem.parentNode.insertBefore(saveAsGifMenuItem, saveAsRingMenuItem.nextSibling);
saveAsRingMenuItem.parentNode.insertBefore(OpenGifMenuItem, saveAsGifMenuItem.nextSibling);
// Insert other menu items
saveAsMenuItem.parentNode.insertBefore(saveTokenMenuItem, savePortraitMenuItem);
saveAsMenuItem.parentNode.insertBefore(saveItemMenuItem, saveTokenMenuItem);
}
});
}
iframeDocument.getElementById('showall').addEventListener('click', function() {
// Call the function defined in minipaint.js
window.parent.showAllLayers();
});
iframeDocument.getElementById('hideall').addEventListener('click', function() {
// Call the function defined in minipaint.js
window.parent.hideAllLayers();
});
iframeDocument.getElementById('hexa').addEventListener('click', function() {
// Call the function defined in minipaint.js
window.parent.showHexagonalPixelateDialog();
});
iframeDocument.getElementById('ink').addEventListener('click', function() {
// Call the function defined in minipaint.js
window.parent.showInkEffectDialog();
});
iframeDocument.getElementById('mazify').addEventListener('click', function() {
// Call the function defined in minipaint.js
window.parent.showEdgeWorkEffectDialog();
});
iframeDocument.getElementById('dotscreen').addEventListener('click', function() {
// Call the function defined in minipaint.js
window.parent.showDotScreenEffectDialog();
});
iframeDocument.getElementById('colorHalftone').addEventListener('click', function() {
// Call the function defined in minipaint.js
window.parent.showColorHalftoneEffectDialog();
});
// Attach event listener to the Lens Blur button
iframeDocument.getElementById('lensBlurButton').addEventListener('click', function() {
// Call the function defined in minipaint.js to show the Lens Blur effect dialog
window.parent.showLensBlurEffectDialog();
});
const fxButton = iframeDocument.getElementById('multieffect');
if (fxButton) {
// Modify the button's onclick event to trigger the applyFiltersToAllLayersWithPreview function
fxButton. => {
// Assuming applyFiltersToAllLayersWithPreview is defined in the parent window
window.parent.applyFiltersToAllLayersWithPreview();
};
// Optionally, you can modify the button's appearance or add additional attributes if needed
fxButton.title = "Apply filters to all layers with preview";
}
const menuBar = iframeDocument.querySelector('.menu_bar');
if (menuBar) {
// Create Load Selected menu item
const loadSelectedMenuItem = iframeDocument.createElement('li');
loadSelectedMenuItem.innerHTML = `
<a id="main_menu_load_selected" role="menuitem" tabindex="-1" aria-haspopup="false" aria-expanded="false" href="javascript:void(0)" data-level="0" title="Load the selected token or tile into miniPaint">
Load Selected
</a>
`;
// Create Replace Selected menu item
const replaceSelectedMenuItem = iframeDocument.createElement('li');
replaceSelectedMenuItem.innerHTML = `
<a id="main_menu_replace_selected" role="menuitem" tabindex="-1" aria-haspopup="false" aria-expanded="false" href="javascript:void(0)" data-level="0" title="Replace the selected token or tile with the edited image">
Replace Selected
</a>
`;
// Browse Foundry Menu Item
const loadBrowseMenuItem = iframeDocument.createElement('li');
loadBrowseMenuItem.innerHTML = `
<a id="main_menu_browse_foundry" role="menuitem" tabindex="-1" aria-haspopup="false" aria-expanded="false" href="javascript:void(0)" data-level="0" title="Open the Foundry file picker to import an image">
<i class="fas fa-folder-open"></i> Foundry
</a>
`;
// Save Foundry Menu Item
const saveFoundryMenuItem = iframeDocument.createElement('li');
saveFoundryMenuItem.innerHTML = `
<a id="main_menu_save_foundry" role="menuitem" tabindex="-1" aria-haspopup="false" aria-expanded="false" href="javascript:void(0)" data-level="0" title="Save the edited image back to Foundry">
<i class="fas fa-save"></i> Foundry
</a>
`;
// Insert menu items into the menu bar in the correct order
menuBar.insertBefore(loadSelectedMenuItem, menuBar.firstChild);
menuBar.insertBefore(replaceSelectedMenuItem, loadSelectedMenuItem.nextSibling);
menuBar.insertBefore(loadBrowseMenuItem, replaceSelectedMenuItem.nextSibling);
menuBar.insertBefore(saveFoundryMenuItem, loadBrowseMenuItem.nextSibling);
}
}
}
// Attach an event listener to the iframe to run the function once it's loaded
document.getElementById('myFrame').addEventListener('load', injectMenuItems);
function createStyledButton(iframeDocument, text, clickHandler) {
const buttonDiv = iframeDocument.createElement('div');
buttonDiv.style.width = '65px';
buttonDiv.style.height = '25px';
buttonDiv.style.backgroundColor = 'transparent';
buttonDiv.style.position = 'relative';
buttonDiv.style.display = 'flex';
buttonDiv.style.justifyContent = 'center';
buttonDiv.style.alignItems = 'center';
buttonDiv.style.marginTop = '5px';
buttonDiv.style.marginLeft = '5px';
buttonDiv.style.cursor = 'pointer';
buttonDiv.style.border = '1px solid #ccc';
const buttonText = iframeDocument.createElement('span');
buttonText.textContent = text;
buttonText.style.color = '#FFFFFF';
buttonText.style.fontSize = '12px';
buttonText.style.fontWeight = 'bold';
buttonText.style.cursor = 'pointer';
buttonText.style.pointerEvents = 'none';
buttonDiv.appendChild(buttonText);
buttonDiv.>
return buttonDiv;
}
// Function to create a new button with shared styles
function createStyledButton(iframeDocument, text, action) {
const buttonDiv = iframeDocument.createElement('div');
buttonDiv.style.width = '65px';
buttonDiv.style.height = '25px';
buttonDiv.style.backgroundColor = '#464d4f';
buttonDiv.style.position = 'relative';
buttonDiv.style.display = 'flex';
buttonDiv.style.justifyContent = 'center';
buttonDiv.style.alignItems = 'center';
buttonDiv.style.marginTop = '5px';
buttonDiv.style.marginLeft = '5px';
buttonDiv.style.cursor = 'pointer';
buttonDiv.style.border = '1px solid #ccc'; // Optional border for visibility
const buttonText = iframeDocument.createElement('span');
buttonText.textContent = text;
buttonText.style.color = '#FFFFFF';
buttonText.style.fontSize = '12px';
buttonText.style.fontWeight = 'bold';
buttonText.style.cursor = 'pointer';
buttonText.style.pointerEvents = 'none'; // Prevent text from blocking clicks
buttonDiv.appendChild(buttonText);
buttonDiv.>
return buttonDiv;
}
// Function to replace the element in the iframe
function replaceElementInIframe() {
const iframe = document.getElementById('myFrame');
if (iframe && iframe.contentWindow) {
const iframeDocument = iframe.contentWindow.document;
const animationElement = iframeDocument.querySelector('#animation');
if (animationElement) {
// Create label divs
const bgLabel = iframeDocument.createElement('div');
bgLabel.style.width = '65px';
bgLabel.style.height = '11px';
bgLabel.style.backgroundColor = 'transparent';
bgLabel.style.position = 'relative';
bgLabel.style.display = 'flex';
bgLabel.style.justifyContent = 'center';
bgLabel.style.alignItems = 'center';
bgLabel.style.marginTop = '5px';
bgLabel.style.marginLeft = '5px';
bgLabel.textContent = 'BgRemove';
const sketchLabel = iframeDocument.createElement('div');
sketchLabel.style.width = '65px';
sketchLabel.style.height = '11px';
sketchLabel.style.backgroundColor = 'transparent';
sketchLabel.style.position = 'relative';
sketchLabel.style.display = 'flex';
sketchLabel.style.justifyContent = 'center';
sketchLabel.style.alignItems = 'center';
sketchLabel.style.marginTop = '5px';
sketchLabel.style.marginLeft = '5px';
sketchLabel.textContent = 'StabilityAI';
// Create the "BG" button using the reusable function
const bgButton = createStyledButton(iframeDocument, 'BG', () => {
window.parent.removeBackgroundAndAddLayer();
});
bgButton.title = "Remove the background of the image using removeBG.";
const sketchButton = createStyledButton(iframeDocument, 'Sketch', () => {
window.parent.applySketchEffect();
});
sketchButton.title = "Transform your sketch into a real image using Stability AI.";
const bgRemoveButton = createStyledButton(iframeDocument, 'BG', () => {
window.parent.removeBackgroundUsingStabilityAI();
});
bgRemoveButton.title = "Remove the background using Stability AI's API.";
const srButton = createStyledButton(iframeDocument, 'S&R', () => {
window.parent.searchAndReplace();
});
srButton.title = "Search and replace specific elements in the image using Stability AI.";
const genRing = createStyledButton(iframeDocument, 'GenRing', () => {
window.parent.applyInpaintRing();
});
genRing.title = "Generate a token ring replacing the transparent area.";
const genToken = createStyledButton(iframeDocument, 'GenToken', () => {
window.parent.applyInpaintToken();
});
genToken.title = "Generate and apply a token inside of the transparent area.";
// Append the labels and buttons after the "Animate" button
animationElement.parentNode.insertBefore(bgLabel, animationElement.nextSibling);
bgLabel.parentNode.insertBefore(bgButton, bgLabel.nextSibling);
bgButton.parentNode.insertBefore(sketchLabel, bgButton.nextSibling);
sketchLabel.parentNode.insertBefore(sketchButton, sketchLabel.nextSibling);
sketchButton.parentNode.insertBefore(bgRemoveButton, sketchButton.nextSibling);
bgRemoveButton.parentNode.insertBefore(srButton, bgRemoveButton.nextSibling);
srButton.parentNode.insertBefore(genRing, srButton.nextSibling);
genRing.parentNode.insertBefore(genToken, genRing.nextSibling);
}
}
}
// Attach an event listener to the iframe to run the function once it's loaded
document.getElementById('myFrame').addEventListener('load', replaceElementInIframe);
const iframe = document.getElementById('myFrame');
if (iframe && iframe.contentWindow) {
iframe.contentWindow.document.addEventListener('drop', async function(event) {
event.preventDefault();
const dataTransfer = event.dataTransfer;
const data = dataTransfer.getData("text/plain");
try {
const parsedData = JSON.parse(data);
if (parsedData && parsedData.type) {
console.log("Dropped FoundryVTT item:", parsedData);
// Use the UUID to retrieve the item from Foundry VTT
const uuid = parsedData.uuid;
let item;
// Use the UUID to get the item from the FoundryVTT database
item = await fromUuid(uuid);
if (item) {
console.log(`Detailed ${parsedData.type} information:`, item);
// Extract the image path
const imgPath = item.img;
// Load the image into miniPaint as a new layer
if (imgPath) {
const iframeDoc = iframe.contentWindow.document;
const miniPaint = iframe.contentWindow;
const Layers = miniPaint.Layers;
// Create a new Image object and load it
const image = new Image();
image.src = imgPath;
image. => {
const new_layer = {
name: item.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);
console.log("Image added to canvas as a new layer.");
};
image. => {
console.error("Failed to load the image.");
};
} else {
console.error("No image path found in the item.");
}
} else {
console.log(`Could not retrieve the detailed information for ${parsedData.type}.`);
}
}
} catch (err) {
console.log("Non-JSON data dropped, likely not a FoundryVTT item.");
}
});
}
}
// Attach an event listener to the iframe to run the function once it's loaded
document.getElementById('myFrame').addEventListener('load', attachDropEventListener);
</script>