@@ -4,9 +4,12 @@ const FileManager = (() => {
4
4
// Binding the browse holder change event to file loading
5
5
const browseHolder = document . getElementById ( 'open-image-browse-holder' ) ;
6
6
const browsePaletteHolder = document . getElementById ( 'load-palette-browse-holder' ) ;
7
+ const importImageHolder = document . getElementById ( 'import-image-browse-holder' ) ;
7
8
8
9
Events . on ( 'change' , browseHolder , loadFile ) ;
9
10
Events . on ( 'change' , browsePaletteHolder , loadPalette ) ;
11
+ Events . on ( 'change' , importImageHolder , loadImage ) ;
12
+ Events . on ( 'click' , 'export-confirm' , exportProject ) ;
10
13
Events . on ( "click" , "save-project-confirm" , saveProject ) ;
11
14
12
15
function openSaveProjectWindow ( ) {
@@ -27,17 +30,17 @@ const FileManager = (() => {
27
30
28
31
function openPixelExportWindow ( ) {
29
32
let selectedPalette = Util . getText ( 'palette-button' ) ;
33
+
30
34
31
35
if ( selectedPalette != 'Choose a palette...' ) {
32
36
var paletteAbbreviation = palettes [ selectedPalette ] . name ;
33
- var fileName = 'pixel-' + paletteAbbreviation + '-' + canvasSize [ 0 ] + 'x' + canvasSize [ 1 ] + '.png' ;
37
+ var fileName = 'pixel-' + paletteAbbreviation + '-' + currFile . canvasSize [ 0 ] + 'x' + currFile . canvasSize [ 1 ] + '.png' ;
34
38
} else {
35
39
var fileName = 'pixel-' + currFile . canvasSize [ 0 ] + 'x' + currFile . canvasSize [ 1 ] + '.png' ;
36
40
selectedPalette = 'none' ;
37
41
}
38
42
39
43
Util . setValue ( 'export-file-name' , fileName ) ;
40
- Events . on ( "click" , "export-confirm" , exportProject ) ;
41
44
Dialogue . showDialogue ( 'export' , false ) ;
42
45
}
43
46
@@ -339,6 +342,116 @@ const FileManager = (() => {
339
342
340
343
browsePaletteHolder . value = null ;
341
344
}
345
+
346
+ currentImportPivotElement = undefined ;
347
+ currentImportPivotPosition = 'middle' ;
348
+ isImportWindowInitialized = false ;
349
+
350
+ /**
351
+ * Displays the import image window to allow for configurations
352
+ * to be made be the image is imported.
353
+ */
354
+ function openImportImageWindow ( ) {
355
+ // Reset window values.
356
+ importImageHolder . value = null ;
357
+
358
+ document . getElementById ( 'import-image-match-size' ) . checked = false ;
359
+ document . getElementById ( 'import-image-update-palette' ) . checked = false ;
360
+ document . getElementById ( 'import-image-name' ) . innerText = "" ;
361
+
362
+ // Workaround to prevent events from firing twice for the import window.
363
+ if ( ! this . isImportWindowInitialized ) {
364
+ // Getting the pivot buttons and setting the default pivot selection.
365
+ let pivotButtons = document . getElementsByClassName ( "pivot-button" ) ;
366
+ this . currentImportPivotElement = document . querySelector ( '.import-image-location-pivot .rc-selected-pivot' ) ;
367
+
368
+ // Add event handlers for each pivot.
369
+ for ( let i = 0 ; i < pivotButtons . length ; i ++ ) {
370
+ Events . on ( "click" , pivotButtons [ i ] , onImportPivotChanged . bind ( this ) ) ;
371
+ }
372
+
373
+ Events . on ( "click" , "select-image" , ( ) => document . getElementById ( 'import-image-browse-holder' ) ?. click ( ) ) ;
374
+ Events . on ( "click" , "import-image-confirm" , importImage ) ;
375
+
376
+ this . isImportWindowInitialized = true ;
377
+ }
378
+
379
+ Dialogue . showDialogue ( 'import-image' , false ) ;
380
+ }
381
+
382
+ /**
383
+ * Loads the image and draws it to the current canvas layer. Called when
384
+ * the import image window is finalized.
385
+ */
386
+ function importImage ( ) {
387
+ if ( ! importImageHolder . files || importImageHolder . files . length === 0 ) {
388
+ alert ( 'Please select a file before attempting to import.' )
389
+ return ;
390
+ }
391
+
392
+ var fileReader = new FileReader ( ) ;
393
+
394
+ // Once the image has been loaded draw the image to the current layer at the top right.
395
+ fileReader . onload = ( e ) => {
396
+ var img = new Image ( ) ;
397
+
398
+ img . onload = ( ) => {
399
+ let shouldResizeCanvas = document . getElementById ( 'import-image-match-size' ) . checked ;
400
+ let shouldImportColors = document . getElementById ( 'import-image-update-palette' ) . checked ;
401
+
402
+ // Resize the canvas to the image size if the flag was set to true.
403
+ if ( shouldResizeCanvas ) {
404
+ currFile . resizeCanvas ( null , { x : img . width , y : img . height } , null , false ) ;
405
+ }
406
+
407
+ // Calculate pivot offset and draw the imported image. Ensure the pivot position accounts for the imported images dimensions.
408
+ let offset = Util . getPivotPosition ( this . currentImportPivotPosition , currFile . canvasSize [ 0 ] , currFile . canvasSize [ 1 ] , img . width , img . height ) ;
409
+ currFile . currentLayer . context . drawImage ( img , offset . x , offset . y ) ;
410
+
411
+ if ( shouldImportColors ) {
412
+ ColorModule . updatePaletteFromLayers ( ) ;
413
+ }
414
+
415
+ Dialogue . closeDialogue ( ) ;
416
+ } ;
417
+ img . src = e . target . result ;
418
+ } ;
419
+
420
+ fileReader . readAsDataURL ( importImageHolder . files [ 0 ] ) ;
421
+ }
422
+
423
+ /**
424
+ * Called when the import image holder file input fires an onchange event.
425
+ */
426
+ function loadImage ( ) {
427
+ if ( importImageHolder . files && importImageHolder . files [ 0 ] ) {
428
+ let fileName = document . getElementById ( "import-image-browse-holder" ) . value ;
429
+ let extension = Util . getFileExtension ( fileName ) ;
430
+
431
+ // Display the file name in the window.
432
+ document . getElementById ( 'import-image-name' ) . innerText = importImageHolder . files [ 0 ] . name ;
433
+
434
+ // Checking if the extension is supported
435
+ if ( extension !== 'png' ) {
436
+ alert ( 'Only PNG files are currently allowed to be imported at this time.' )
437
+ importImageHolder . value = null ;
438
+ }
439
+ }
440
+ }
441
+
442
+ /**
443
+ * Called when the selected pivot for the import image is changed.
444
+ * @param {* } event The event for the selected pivot.
445
+ */
446
+ function onImportPivotChanged ( event ) {
447
+ this . currentImportPivotPosition = event . target . getAttribute ( "value" ) ;
448
+
449
+ // Setting the selected class
450
+ this . currentImportPivotElement . classList . remove ( "rc-selected-pivot" ) ;
451
+ this . currentImportPivotElement = event . target ;
452
+ this . currentImportPivotElement . classList . add ( "rc-selected-pivot" ) ;
453
+ }
454
+
342
455
function upgradeLPE ( dictionary ) {
343
456
console . log ( 'dictionary === ' , dictionary ) ;
344
457
if ( dictionary . color0 && ! dictionary . colors ) {
@@ -399,8 +512,10 @@ const FileManager = (() => {
399
512
exportProject,
400
513
openPixelExportWindow,
401
514
openSaveProjectWindow,
515
+ openImportImageWindow,
402
516
open
403
- } ;
517
+ }
518
+
404
519
Object . keys ( ret ) . forEach ( k => {
405
520
if ( typeof ret [ k ] === "function" ) {
406
521
const orig = ret [ k ] ;
0 commit comments