1
- import { IResourceMetadata , Loader , LoaderResource } from " @pixi/loaders" ;
2
- import { BaseTexture , Texture } from " @pixi/core" ;
3
- import { ISkeletonParser , TextureAtlas } from " @pixi-spine/base" ;
4
- import { ALPHA_MODES } from " @pixi/constants" ;
1
+ import { IResourceMetadata , Loader , LoaderResource } from ' @pixi/loaders' ;
2
+ import { BaseTexture , IAutoDetectOptions , Resource , Texture } from ' @pixi/core' ;
3
+ import { ISkeletonParser , TextureAtlas } from ' @pixi-spine/base' ;
4
+ import { ALPHA_MODES } from ' @pixi/constants' ;
5
5
6
6
function isJson ( resource : LoaderResource ) {
7
7
return resource . type === LoaderResource . TYPE . JSON ;
@@ -24,6 +24,7 @@ export abstract class AbstractSpineParser {
24
24
abstract parseData ( resource : LoaderResource , parser : ISkeletonParser , atlas : TextureAtlas , dataToParse : any ) : void ;
25
25
26
26
genMiddleware ( ) {
27
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
27
28
const self = this ;
28
29
29
30
return {
@@ -34,8 +35,7 @@ export abstract class AbstractSpineParser {
34
35
}
35
36
36
37
const isJsonSpineModel = isJson ( resource ) && resource . data . bones ;
37
- const isBinarySpineModel = isBuffer ( resource ) && ( resource . extension === 'skel' || resource . metadata
38
- && ( resource . metadata as any ) . spineMetadata ) ;
38
+ const isBinarySpineModel = isBuffer ( resource ) && ( resource . extension === 'skel' || ( resource . metadata && ( resource . metadata as any ) . spineMetadata ) ) ;
39
39
40
40
if ( ! isJsonSpineModel && ! isBinarySpineModel ) {
41
41
return next ( ) ;
@@ -61,11 +61,13 @@ export abstract class AbstractSpineParser {
61
61
}
62
62
63
63
const metadataAtlas = metadata . spineAtlas ;
64
+
64
65
if ( metadataAtlas === false ) {
65
66
return next ( ) ;
66
67
}
67
68
if ( metadataAtlas && metadataAtlas . pages ) {
68
69
self . parseData ( resource , parser , metadataAtlas , dataToParse ) ;
70
+
69
71
return next ( ) ;
70
72
}
71
73
@@ -77,83 +79,91 @@ export abstract class AbstractSpineParser {
77
79
* have the same name
78
80
*/
79
81
let atlasPath = resource . url ;
80
- let queryStringPos = atlasPath . indexOf ( '?' ) ;
82
+ const queryStringPos = atlasPath . indexOf ( '?' ) ;
83
+
81
84
if ( queryStringPos > 0 ) {
82
- //remove querystring
83
- atlasPath = atlasPath . substr ( 0 , queryStringPos )
85
+ // remove querystring
86
+ atlasPath = atlasPath . substr ( 0 , queryStringPos ) ;
84
87
}
85
88
atlasPath = atlasPath . substr ( 0 , atlasPath . lastIndexOf ( '.' ) ) + metadataAtlasSuffix ;
86
- // use atlas path as a params. (no need to use same atlas file name with json file name)
89
+ // use atlas path as a params. (no need to use same atlas file name with json file name)
87
90
if ( metadata . spineAtlasFile ) {
88
91
atlasPath = metadata . spineAtlasFile ;
89
92
}
90
93
91
- // remove the baseUrl
94
+ // remove the baseUrl
92
95
atlasPath = atlasPath . replace ( this . baseUrl , '' ) ;
93
96
94
97
const atlasOptions = {
95
98
crossOrigin : resource . crossOrigin ,
96
99
xhrType : LoaderResource . XHR_RESPONSE_TYPE . TEXT ,
97
100
metadata : metadata . spineMetadata || null ,
98
- parentResource : resource
101
+ parentResource : resource ,
99
102
} ;
100
103
const imageOptions = {
101
104
crossOrigin : resource . crossOrigin ,
102
105
metadata : metadata . imageMetadata || null ,
103
- parentResource : resource
106
+ parentResource : resource ,
104
107
} ;
105
108
let baseUrl = resource . url . substr ( 0 , resource . url . lastIndexOf ( '/' ) + 1 ) ;
106
- //remove the baseUrl
109
+ // remove the baseUrl
110
+
107
111
baseUrl = baseUrl . replace ( this . baseUrl , '' ) ;
108
112
109
- const namePrefix = metadata . imageNamePrefix || ( resource . name + '_atlas_page_' ) ;
113
+ const namePrefix = metadata . imageNamePrefix || `${ resource . name } _atlas_page_` ;
114
+
115
+ let adapter : ( line : string , callback : ( baseTexture : BaseTexture < Resource , IAutoDetectOptions > ) => any ) => void ;
110
116
111
- const adapter = metadata . images ? staticImageLoader ( metadata . images )
112
- : metadata . image ? staticImageLoader ( { ' default' : metadata . image } )
113
- : metadata . imageLoader ? metadata . imageLoader ( this , namePrefix , baseUrl , imageOptions )
114
- : imageLoaderAdapter ( this , namePrefix , baseUrl , imageOptions ) ;
117
+ if ( metadata . images ) adapter = staticImageLoader ( metadata . images ) ;
118
+ else if ( metadata . image ) adapter = staticImageLoader ( { default : metadata . image } ) ;
119
+ else if ( metadata . imageLoader ) adapter = metadata . imageLoader ( this , namePrefix , baseUrl , imageOptions ) ;
120
+ else adapter = imageLoaderAdapter ( this , namePrefix , baseUrl , imageOptions ) ;
115
121
116
- function createSkeletonWithRawAtlas ( rawData : string ) {
117
- new TextureAtlas ( rawData , adapter , function ( spineAtlas ) {
122
+ const createSkeletonWithRawAtlas = ( rawData : string ) => {
123
+ // eslint-disable-next-line no-new
124
+ new TextureAtlas ( rawData , adapter , ( spineAtlas ) => {
118
125
if ( spineAtlas ) {
119
126
self . parseData ( resource , parser , spineAtlas , dataToParse ) ;
120
127
}
121
128
next ( ) ;
122
129
} ) ;
123
- }
130
+ } ;
124
131
125
132
if ( metadata . atlasRawData ) {
126
- createSkeletonWithRawAtlas ( metadata . atlasRawData )
133
+ createSkeletonWithRawAtlas ( metadata . atlasRawData ) ;
127
134
} else {
128
- this . add ( resource . name + ' _atlas' , atlasPath , atlasOptions , function ( atlasResource : any ) {
135
+ this . add ( ` ${ resource . name } _atlas` , atlasPath , atlasOptions , ( atlasResource : any ) => {
129
136
if ( ! atlasResource . error ) {
130
137
createSkeletonWithRawAtlas ( atlasResource . data ) ;
131
138
} else {
132
139
next ( ) ;
133
140
}
134
141
} ) ;
135
142
}
136
- }
137
- }
143
+ } ,
144
+ } ;
138
145
}
139
146
}
140
147
141
148
/**
142
149
* @public
143
150
*/
144
151
export function imageLoaderAdapter ( loader : any , namePrefix : any , baseUrl : any , imageOptions : any ) {
145
- if ( baseUrl && baseUrl . lastIndexOf ( '/' ) !== ( baseUrl . length - 1 ) ) {
152
+ if ( baseUrl && baseUrl . lastIndexOf ( '/' ) !== baseUrl . length - 1 ) {
146
153
baseUrl += '/' ;
147
154
}
148
- return function ( line : string , callback : ( baseTexture : BaseTexture ) => any ) {
155
+
156
+ return ( line : string , callback : ( baseTexture : BaseTexture ) => any ) => {
149
157
const name = namePrefix + line ;
150
158
const url = baseUrl + line ;
151
159
152
160
const cachedResource = loader . resources [ name ] ;
161
+
153
162
if ( cachedResource ) {
154
163
const done = ( ) => {
155
- callback ( cachedResource . texture . baseTexture )
156
- }
164
+ callback ( cachedResource . texture . baseTexture ) ;
165
+ } ;
166
+
157
167
if ( cachedResource . texture ) {
158
168
done ( ) ;
159
169
} else {
@@ -172,30 +182,30 @@ export function imageLoaderAdapter(loader: any, namePrefix: any, baseUrl: any, i
172
182
}
173
183
} ) ;
174
184
}
175
- }
185
+ } ;
176
186
}
177
187
178
188
/**
179
189
* @public
180
190
*/
181
191
export function syncImageLoaderAdapter ( baseUrl : any , crossOrigin : any ) {
182
- if ( baseUrl && baseUrl . lastIndexOf ( '/' ) !== ( baseUrl . length - 1 ) ) {
192
+ if ( baseUrl && baseUrl . lastIndexOf ( '/' ) !== baseUrl . length - 1 ) {
183
193
baseUrl += '/' ;
184
194
}
185
- return function ( line : any , callback : any ) {
195
+
196
+ return ( line : any , callback : any ) => {
186
197
callback ( BaseTexture . from ( line , crossOrigin ) ) ;
187
- }
198
+ } ;
188
199
}
189
200
190
201
/**
191
202
* @public
192
203
*/
193
- export function staticImageLoader ( pages : { [ key : string ] : ( BaseTexture | Texture ) } ) {
194
- return function ( line : any , callback : any ) {
195
- let page = pages [ line ] || pages [ 'default' ] as any ;
196
- if ( page && page . baseTexture )
197
- callback ( page . baseTexture ) ;
198
- else
199
- callback ( page ) ;
200
- }
204
+ export function staticImageLoader ( pages : { [ key : string ] : BaseTexture | Texture } ) {
205
+ return ( line : any , callback : any ) => {
206
+ const page = pages [ line ] || ( pages . default as any ) ;
207
+
208
+ if ( page && page . baseTexture ) callback ( page . baseTexture ) ;
209
+ else callback ( page ) ;
210
+ } ;
201
211
}
0 commit comments