@@ -4,18 +4,15 @@ import { writeFile } from 'fs';
44import { errorMsg } from '#util/dialog' ;
55import { colorMatch } from '#components/import/color-match' ;
66
7- const debug = false ;
7+ function exportSprite ( { buffer , mappings } ) {
88
9- export function exportPNG ( ) {
10- const { currentSprite : { buffer, mappings } , palettesRGB, sprites } = environment ;
11-
12- if ( ! mappings . length || ! sprites . length ) return ;
9+ const { palettesRGB } = environment ;
1310
1411 const canvas = document . createElement ( 'canvas' ) ;
15- if ( debug ) {
16- canvas . className = 'canvas-debug' ;
17- canvas . style . width = '200px' ;
18- document . body . appendChild ( canvas ) ;
12+ if ( ! mappings . length ) {
13+ canvas . width = 32 ;
14+ canvas . height = 32 ;
15+ return canvas ;
1916 }
2017 const ctx = canvas . getContext ( '2d' ) ;
2118 let tileBuffer = ctx . getImageData ( 0 , 0 , 8 , 8 ) ;
@@ -84,10 +81,64 @@ export function exportPNG() {
8481
8582 } ) ;
8683
84+ return canvas ;
85+ }
86+
87+
88+ export function exportSpritesheet ( ) {
89+ const { sprites } = environment ;
90+ if ( ! sprites . length ) return ;
91+
92+ const canvases = sprites . map ( ( { buffer, mappings } ) => exportSprite ( { buffer, mappings } ) ) ;
93+
94+ const canvas = document . createElement ( 'canvas' ) ;
95+ canvas . width = 8 ;
96+ canvas . height = 8 ;
97+ const ctx = canvas . getContext ( '2d' ) ;
98+ // canvas.className = 'canvas-debug';
99+ // document.body.appendChild(canvas);
100+
101+ let cursor = 8 ;
102+
103+ canvases . forEach ( current => {
104+ const last = ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ;
105+ const { width, height } = current ;
106+ const diff = width + 8 ;
107+ canvas . width += diff
108+ canvas . height = Math . max ( canvas . height , height + 8 ) ;
109+ ctx . putImageData ( last , 0 , 0 ) ;
110+ ctx . drawImage ( current , cursor , 8 ) ;
111+ cursor += diff ;
112+ } ) ;
113+
114+ dialog . showSaveDialog ( {
115+ title : 'Export Spritesheet' ,
116+ defaultPath : `spritesheet.png` ,
117+ filters : [ { name : 'PNG Image' , extensions : [ 'png' ] } ] ,
118+ } )
119+ . then ( ( { filePath } ) => {
120+ if ( filePath ) {
121+ const base64Data = canvas . toDataURL ( ) . replace ( / d a t a ( .* ?) , / , '' ) ;
122+ writeFile ( filePath , Buffer . from ( base64Data , 'base64' ) , ( err , success ) => {
123+ err && errorMsg ( 'Error exporting spritesheet' , String ( err ) ) ;
124+ } ) ;
125+ }
126+ } )
127+ . catch ( console . error ) ;
128+
129+ }
130+
131+ export function exportPNG ( ) {
132+ const { currentSprite : { buffer, mappings } , sprites } = environment ;
133+
134+ if ( ! mappings . length || ! sprites . length ) return ;
135+
136+ const canvas = exportSprite ( { buffer, mappings } ) ;
137+
87138 dialog . showSaveDialog ( {
88139 title : 'Export Sprite' ,
89140 defaultPath : `0x${ environment . config . currentSprite . toString ( 16 ) . toUpperCase ( ) } .png` ,
90- filters : [ { name : 'PNG Image File ' , extensions : [ 'png' ] } ] ,
141+ filters : [ { name : 'PNG Image' , extensions : [ 'png' ] } ] ,
91142 } )
92143 . then ( ( { filePath } ) => {
93144 if ( filePath ) {
@@ -98,7 +149,6 @@ export function exportPNG() {
98149 }
99150 } )
100151 . catch ( console . error ) ;
101-
102152}
103153
104154export async function importImg ( ) {
@@ -170,7 +220,6 @@ export async function importImg() {
170220 }
171221
172222 } ) ;
173-
174223}
175224
176225function flipBuffer ( buffer , hflip , vflip ) {
0 commit comments