Skip to content

Commit 84c89d7

Browse files
committed
Improve asset specific type generation via namespaces.
1 parent 2de606f commit 84c89d7

File tree

2 files changed

+84
-18
lines changed

2 files changed

+84
-18
lines changed

bin/createDeclaration.js

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ for (let file of files)
2828
continue;
2929
}
3030

31-
// start off by importing pixi-animate
32-
let output = `import * as animate from \'pixi-animate\';
33-
`;
34-
3531
const classes = [];
3632
// get set up to find any classes that are in the library, and make a list
3733
const classFinder = /data\.lib\.([a-zA-Z_$0-9]+) = class extends (MovieClip|Container)/g;
@@ -110,31 +106,101 @@ for (let file of files)
110106
});
111107
foundClass = classFinder.exec(source);
112108
}
109+
let lib = '';
113110
// for each class, set up a declaration for it
114111
for (const classData of classes)
115112
{
116-
output += `declare class ${classData.className} extends animate.${classData.baseClass} {${classData.props.length
113+
// indented twice, for two namespaces in
114+
lib += ` export class ${classData.className} extends animate.${classData.baseClass} {${classData.props.length
117115
? '\n' + classData.props
118-
.map(({ name, source, type }) => ` ${name}: ${source ? '' : 'animate.'}${type};`)
119-
.join('\n') + '\n'
116+
.map(({ name, source, type }) => ` ${name}: ${source ? '' : 'animate.'}${type};`)
117+
.join('\n') + '\n '
120118
: ''}}
121119
`;
122120
}
123-
// create an interface for our specific library
124-
output += `interface Lib {
125-
${classes.map(({ className }) => ` ${className}: typeof ${className};`).join('\n')}
126-
}
127-
`;
128121
// figure out which class is the root class
129122
const [, root] = (/data\.stage ?= ?data\.lib\.([a-zA-Z0-9$_]+)/).exec(source);
130-
// declare the data object, mix in our specific library (can't override it, I think?)
131-
output += `declare const data: Omit<animate.AnimateAsset, 'stage'|'lib'> & {lib: Lib, stage: typeof ${root}};
132-
`;
133-
output += `${source.match(/export default data/) ? 'export default' : 'export ='} data;
123+
// set up our library using a modified copy of AnimateAsset as a template
124+
let output = `import * as animate from 'pixi-animate';
125+
import { Texture, Spritesheet } from 'pixi.js';
126+
127+
declare namespace data
128+
{
129+
/**
130+
* Background color of the scene, as defined when the asset is published.
131+
*/
132+
export const background: number;
133+
/**
134+
* Width of the scene, as defined when the asset is published.
135+
*/
136+
export const width: number;
137+
/**
138+
* Height of the scene, as defined when the asset is published.
139+
*/
140+
export const height: number;
141+
/**
142+
* Framerate of the scene (frames per second), as defined when the asset is published.
143+
*/
144+
export const framerate: number;
145+
/**
146+
* Total number of frames of the root MovieClip.
147+
*/
148+
export const totalFrames: number;
149+
/**
150+
* Dictionary of paths to shape files and textures, indexed by unique id within the scene file.
151+
*/
152+
export let assets: { [id: string]: string };
153+
/**
154+
* Dictionary of loaded shape instructions for this scene. This is intially an empty object that
155+
* can be filled by animate.load(), or by a custom loading system. It must be filled before
156+
* any items are instantiated.
157+
*/
158+
export let shapes: { [id: string]: animate.DrawCommands[] };
159+
/**
160+
* Dictionary of loaded individual images for this scene.This is intially an empty object that
161+
* will be filled by animate.load(). It will be used by the animate.load() method for
162+
* getTexture(), and is not needed if getTexture() is overridden.
163+
*/
164+
export let textures: { [id: string]: Texture };
165+
/**
166+
* Dictionary of loaded spritesheet for this scene.This is intially an empty object that
167+
* will be filled by animate.load(). It will be used by the animate.load() method for
168+
* getTexture(), and is not needed if getTexture() is overridden.
169+
*/
170+
export let spritesheets: Spritesheet[];
171+
/**
172+
* Function for getting a texture by ID.
173+
* It can be set to PIXI.Texture.from for global texture atlas.
174+
*/
175+
export let getTexture: (id: string) => Texture;
176+
/**
177+
* Creates classes for each Container and MovieClip within the scene, filling data.lib and
178+
* setting data.stage.
179+
*/
180+
export function setup(library: typeof animate): void;
181+
/**
182+
* Dictionary of display object constructors used within this scene. This is an empty object
183+
* before setup() is run, but can be overwritten with a shared library dictionary (before setup() is run).
184+
*/
185+
export namespace lib
186+
{
187+
${lib}
188+
}
189+
/**
190+
* Constructor for the root MovieClip. Is null before setup() is run.
191+
*/
192+
export let stage: typeof lib.${root};
193+
}
194+
${source.match(/export default data/) ? 'export default' : 'export ='} data;
134195
`;
135196
if ((/export {[a-zA-Z0-9$_]+}/).test(source))
136197
{
137-
output += `export {${classes.map(({ className }) => className).join(', ')}};`;
198+
for (const className of classes)
199+
{
200+
output += `import ${className} = data.lib.${className};
201+
export { ${className} };
202+
`;
203+
}
138204
}
139205

140206
fs.writeFileSync(file.replace('.js', '.d.ts'), output, 'utf8');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pixi-animate",
33
"description": "PIXI plugin for the PixiAnimate Extension",
4-
"version": "2.0.0-rc4",
4+
"version": "2.0.0-rc5",
55
"main": "lib/pixi-animate.js",
66
"module": "lib/pixi-animate.es.js",
77
"bundle": "dist/pixi-animate.js",

0 commit comments

Comments
 (0)