1- import { observable , computed , action , autorun , toJS } from 'mobx' ;
1+ import { observable , computed , action , autorun , toJS , makeObservable } from 'mobx' ;
22const { dialog } = require ( '@electron/remote' ) ;
33import { errorMsg } from '#util/dialog' ;
44import { removeBackground } from './remove-background' ;
@@ -8,23 +8,22 @@ import { getMappings } from './generate-mappings';
88import { importSprite } from './import-sprite' ;
99
1010class ImportState {
11-
12- @observable config = {
11+ config = {
1312 active : false ,
1413 } ;
1514
16- @ observable bboxes = [ ] ;
17- @ observable sprites = [ ] ;
18- @ observable mappings = [ ] ;
19- @ observable type = 'mappings' ;
20- @ observable importWidth = 0 ;
21- @ observable importHeight = 0 ;
22- @ observable spriteIndex = 0 ;
23- @ observable paletteLine = 0 ;
24- @ observable fuzziness = 4 ;
25- @ observable scale = 4 ;
26-
27- @ action reset = ( ) => {
15+ bboxes = [ ] ;
16+ sprites = [ ] ;
17+ mappings = [ ] ;
18+ type = 'mappings' ;
19+ importWidth = 0 ;
20+ importHeight = 0 ;
21+ spriteIndex = 0 ;
22+ paletteLine = 0 ;
23+ fuzziness = 4 ;
24+ scale = 4 ;
25+
26+ reset = ( ) => {
2827 this . path = undefined ;
2928 this . canvas = undefined ;
3029 this . ctx = undefined ;
@@ -35,7 +34,7 @@ class ImportState {
3534 this . mappings . replace ( [ ] ) ;
3635 } ;
3736
38- @ action newImport = ( ) => {
37+ newImport = ( ) => {
3938
4039 dialog . showOpenDialog ( {
4140 title : 'Import Spritesheet' ,
@@ -51,7 +50,7 @@ class ImportState {
5150 . catch ( console . error ) ;
5251 } ;
5352
54- @ action cancel = ( ) => {
53+ cancel = ( ) => {
5554 this . reset ( ) ;
5655 this . config . active = false ;
5756 } ;
@@ -98,7 +97,7 @@ class ImportState {
9897 ctx . putImageData ( buffer , 32 , 32 ) ;
9998 } ;
10099
101- @ action getBBoxes = ( ) => {
100+ getBBoxes = ( ) => {
102101 const { ctx, canvas } = this ;
103102 const { width, height } = canvas ;
104103 const buffer = ctx . getImageData ( 0 , 0 , width , height ) ;
@@ -107,7 +106,7 @@ class ImportState {
107106 ) ;
108107 } ;
109108
110- @ action importSprites = ( ) => {
109+ importSprites = ( ) => {
111110 const { ctx, canvas } = this ;
112111 const { width, height } = canvas ;
113112 const buffer = ctx . getImageData ( 0 , 0 , width , height ) ;
@@ -123,17 +122,47 @@ class ImportState {
123122 this . sprites . replace ( sprites ) ;
124123 } ;
125124
126- @ action backToDetect = ( ) => {
125+ backToDetect = ( ) => {
127126 this . sprites . replace ( [ ] ) ;
128127 } ;
129128
129+ constructor ( ) {
130+ makeObservable ( this , {
131+ config : observable ,
132+ bboxes : observable ,
133+ sprites : observable ,
134+ mappings : observable ,
135+ type : observable ,
136+ importWidth : observable ,
137+ importHeight : observable ,
138+ spriteIndex : observable ,
139+ paletteLine : observable ,
140+ fuzziness : observable ,
141+ scale : observable ,
142+ reset : action ,
143+ newImport : action ,
144+ cancel : action ,
145+ getBBoxes : action ,
146+ importSprites : action ,
147+ backToDetect : action ,
148+ currentSprite : computed ,
149+ tileQty : computed ,
150+ changePalette : action ,
151+ changeType : action ,
152+ importOne : action ,
153+ importAll : action ,
154+ next : action ,
155+ prev : action
156+ } ) ;
157+ }
158+
130159 // import stuff
131160
132- @ computed get currentSprite ( ) {
161+ get currentSprite ( ) {
133162 return this . sprites [ this . spriteIndex ] ;
134163 }
135164
136- @ computed get tileQty ( ) {
165+ get tileQty ( ) {
137166 return this . mappings . reduce ( ( a , c ) => {
138167 return a + ( c . width * c . height ) ;
139168 } , 0 ) ;
@@ -167,30 +196,30 @@ class ImportState {
167196 }
168197 } ;
169198
170- @ action changePalette = ( ) => {
199+ changePalette = ( ) => {
171200 const { canvas, ctx } = this ;
172201 const { width, height, buffer } = this . currentSprite ;
173202 const coloredBuffer = colorMatch ( buffer , this . paletteLine ) ;
174203 ctx . putImageData ( coloredBuffer , 8 , 8 ) ;
175204 } ;
176205
177- @ action changeType = ( ) => {
206+ changeType = ( ) => {
178207 const { canvas, ctx, type } = this ;
179208 this . mappings . replace ( getMappings ( canvas , ctx , type ) ) ;
180- }
209+ } ;
181210
182- @ action importOne = ( ) => {
211+ importOne = ( ) => {
183212 const { ctx, mappings, paletteLine } = this ;
184213 importSprite ( ctx , mappings , paletteLine ) ;
185214 this . next ( ) ;
186215 } ;
187216
188- @ action importAll = ( ) => {
217+ importAll = ( ) => {
189218 this . auto = true ;
190219 this . spriteIndex = - 1 ;
191220 } ;
192221
193- @ action next = ( ) => {
222+ next = ( ) => {
194223 if ( this . spriteIndex < this . sprites . length - 1 ) {
195224 this . spriteIndex ++ ;
196225 }
@@ -199,12 +228,11 @@ class ImportState {
199228 }
200229 } ;
201230
202- @ action prev = ( ) => {
231+ prev = ( ) => {
203232 if ( this . spriteIndex > 0 ) {
204233 this . spriteIndex -- ;
205234 }
206235 } ;
207-
208236}
209237
210238const importState = new ImportState ( ) ;
0 commit comments