-
Notifications
You must be signed in to change notification settings - Fork 152
/
babylon.atlasPicture.ts
303 lines (279 loc) · 11.6 KB
/
babylon.atlasPicture.ts
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
module BABYLON {
/**
* Interface to create your own Loader of Atlas Data file.
* Call the AtlasPictureInfoFactory.addLoader to addd your loader instance
*/
export interface IAtlasLoader {
loadFile(content: any): { api: AtlasPictureInfo, errorMsg: string, errorCode: number };
}
// Proxy Class for the TexturePacker's JSON Array data format
interface IFrame {
filename: string;
frame: { x: number, y: number, w: number, h: number };
rotated: boolean;
trimmed: boolean;
srpiteSourceSize: { x: number, y: number, w: number, h: number };
sourceSize: { x: number, y: number, w: number, h: number };
}
// Proxy Class for the TexturePacker's JSON Array data format
interface IMeta {
app: string;
version: string;
image: string;
format: string;
size: { w: number, h: number }
scale: string;
smartupdate: string;
}
/**
* This class will contains information about a sub picture present in an Atlas Picture.
*/
export class AtlasSubPictureInfo {
/**
* Name of the SubPicture, generally the filename of the initial picture file.
*/
name: string;
/**
* Location of the bottom/left corner of the sub picture from the bottom/left corner the Atlas Picture
*/
location: Vector2;
/**
* Size in pixel of the sub picture
*/
size: Size;
}
/**
* This class represent an Atlas Picture, it contains the information of all the sub pictures and the Texture that stores the bitmap.
* You get an instance of this class using methods of the AtlasPictureInfoFactory
*/
export class AtlasPictureInfo {
/**
* Creates many sprite from the Atlas Picture
* @param filterCallback a predicate if true is returned then the corresponding sub picture will be used to create a sprite.
* The Predicate has many parameters:
* - index: just an index incremented at each sub picture submitted for Sprite creation
* - name: the sub picture's name
* - aspi: the AtlasSubPictureInfo corresponding to the submitted sub picture
* - settings: the Sprite2D creation settings, you can alter this JSON object but BEWARE, the alterations will be kept for subsequent Sprite2D creations!
* @param spriteSettings The Sprite2D settings to use for Sprite creation, this JSON object will be passed to the filterCallback for you to alter it, if needed.
*/
createSprites(filterCallback: (index: number, name: string, aspi: AtlasSubPictureInfo, settings: any) => boolean,
spriteSettings: {
parent?: Prim2DBase,
position?: Vector2,
x?: number,
y?: number,
rotation?: number,
size?: Size,
scale?: number,
scaleX?: number,
scaleY?: number,
dontInheritParentScale?: boolean,
opacity?: number,
zOrder?: number,
origin?: Vector2,
scale9?: Vector4,
invertY?: boolean,
alignToPixel?: boolean,
isVisible?: boolean,
isPickable?: boolean,
isContainer?: boolean,
childrenFlatZOrder?: boolean,
marginTop?: number | string,
marginLeft?: number | string,
marginRight?: number | string,
marginBottom?: number | string,
margin?: number | string,
marginHAlignment?: number,
marginVAlignment?: number,
marginAlignment?: string,
paddingTop?: number | string,
paddingLeft?: number | string,
paddingRight?: number | string,
paddingBottom?: number | string,
padding?: string,
}): Array<Sprite2D> {
let res = new Array<Sprite2D>();
let index = 0;
this.subPictures.forEach((k, v) => {
if (!filterCallback || filterCallback(index++, k, v, spriteSettings)) {
let s = this.createSprite(k, spriteSettings);
if (s) {
res.push(s);
}
}
});
return res;
}
/**
* Create one Sprite from a sub picture
* @param subPictureName the name of the sub picture to use
* @param spriteSettings the Sprite2D settings to use for the Sprite instance creation
*/
createSprite(subPictureName: string, spriteSettings: {
parent?: Prim2DBase,
position?: Vector2,
x?: number,
y?: number,
rotation?: number,
size?: Size,
scale?: number,
scaleX?: number,
scaleY?: number,
dontInheritParentScale?: boolean,
opacity?: number,
zOrder?: number,
origin?: Vector2,
scale9?: Vector4,
invertY?: boolean,
alignToPixel?: boolean,
isVisible?: boolean,
isPickable?: boolean,
isContainer?: boolean,
childrenFlatZOrder?: boolean,
marginTop?: number | string,
marginLeft?: number | string,
marginRight?: number | string,
marginBottom?: number | string,
margin?: number | string,
marginHAlignment?: number,
marginVAlignment?: number,
marginAlignment?: string,
paddingTop?: number | string,
paddingLeft?: number | string,
paddingRight?: number | string,
paddingBottom?: number | string,
padding?: string,
}): Sprite2D {
let spi = this.subPictures.get(subPictureName);
if (!spi) {
return null;
}
if (!spriteSettings) {
spriteSettings = {};
}
let s = <any>spriteSettings;
s.id = subPictureName;
s.spriteLocation = spi.location;
s.spriteSize = spi.size;
let sprite = new Sprite2D(this.texture, spriteSettings);
return sprite;
}
/**
* Size of the Atlas Picture
*/
atlasSize: Size;
/**
* String Dictionary of all the sub pictures, the key is the sub picture's name, the value is the info object
*/
subPictures: StringDictionary<AtlasSubPictureInfo>;
/**
* The Texture associated to the Atlas Picture info
*/
texture: Texture;
}
/**
* This if the Factory class containing static method to create Atlas Pictures Info objects or add new loaders
*/
export class AtlasPictureInfoFactory {
/**
* Add a custom loader
* @param fileExtension must be the file extension (without the dot) of the file that is loaded by this loader (e.g.: json)
* @param plugin the instance of the loader
*/
public static addLoader(fileExtension: string, plugin: IAtlasLoader) {
let a = AtlasPictureInfoFactory.plugins.getOrAddWithFactory(fileExtension.toLocaleLowerCase(), () => new Array<IAtlasLoader>());
a.push(plugin);
}
/**
* Load an Atlas Picture Info object from a data file at a given url and with a given texture
* @param texture the texture containing the atlas bitmap
* @param url the URL of the Atlas Info data file
* @param onLoad a callback that will be called when the AtlasPictureInfo object will be loaded and ready
* @param onError a callback that will be called in case of error
*/
public static loadFromUrl(texture: Texture, url: string, onLoad: (api: AtlasPictureInfo) => void, onError: (msg: string, code: number) => void = null) {
Tools.LoadFile(url, (data) => {
let ext = url.split('.').pop().split(/\#|\?/)[0];
let plugins = AtlasPictureInfoFactory.plugins.get(ext.toLocaleLowerCase());
if (!plugins) {
if (onError) {
onError("couldn't find a plugin for this file extension", -1);
}
return;
}
for (let p of plugins) {
let ret = p.loadFile(data);
if (ret) {
if (ret.api) {
ret.api.texture = texture;
if (onLoad) {
onLoad(ret.api);
}
} else if (onError) {
onError(ret.errorMsg, ret.errorCode);
}
return;
}
}
if (onError) {
onError("No plugin to load this Atlas Data file format", -1);
}
}, null, null, null, () => {
if (onError) {
onError("Couldn't load file", -1);
}
});
return null;
}
private static plugins: StringDictionary<Array<IAtlasLoader>> = new StringDictionary<Array<IAtlasLoader>>();
}
// Loader class for the TexturePacker's JSON Array data format
@AtlasLoaderPlugin("json", new JSONArrayLoader())
class JSONArrayLoader implements IAtlasLoader {
loadFile(content): { api: AtlasPictureInfo, errorMsg: string, errorCode: number } {
let errorMsg: string = null;
let errorCode: number = 0;
let root = null;
let api: AtlasPictureInfo = null;
try {
let frames: Array<IFrame>;
let meta: IMeta;
try {
root = JSON.parse(content);
frames = <Array<IFrame>>root.frames;
meta = <IMeta>root.meta;
if (!frames || !meta) {
throw Error("Not a JSON Array file format");
}
} catch (ex1) {
return null;
}
api = new AtlasPictureInfo();
api.atlasSize = new Size(meta.size.w, meta.size.h);
api.subPictures = new StringDictionary<AtlasSubPictureInfo>();
for (let f of frames) {
let aspi = new AtlasSubPictureInfo();
aspi.name = f.filename;
aspi.location = new Vector2(f.frame.x, api.atlasSize.height - (f.frame.y + f.frame.h));
aspi.size = new Size(f.frame.w, f.frame.h);
api.subPictures.add(aspi.name, aspi);
}
} catch (ex2) {
errorMsg = "Unknown Exception: " + ex2;
errorCode = -2;
}
return { api: api, errorMsg: errorMsg, errorCode: errorCode };
}
}
/**
* Use this decorator when you declare an Atlas Loader Class for the loader to register itself automatically.
* @param fileExtension the extension of the file that the plugin is loading (there can be many plugin for the same extension)
* @param plugin an instance of the plugin class to add to the AtlasPictureInfoFactory
*/
export function AtlasLoaderPlugin(fileExtension: string, plugin: IAtlasLoader): (target: Object) => void {
return () => {
AtlasPictureInfoFactory.addLoader(fileExtension, plugin);
}
}
}