Skip to content

Commit 48b9f69

Browse files
author
Matt Colman
committed
- fix a bug where loader would not complete unless there were webfonts to load
- add flow and generated api documentation - add more documentation to the README
1 parent 2db00bb commit 48b9f69

File tree

7 files changed

+211
-37
lines changed

7 files changed

+211
-37
lines changed

Diff for: .babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["es2015"]
2+
"presets": ["es2015", "flow"]
33
}

Diff for: .flowconfig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[ignore]
2+
3+
[include]
4+
5+
[libs]
6+
7+
[options]

Diff for: README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,35 @@ resolve: {
112112
}
113113
```
114114

115-
## Usage
115+
## Simple Usage
116116
```
117117
import ManifestLoader from 'phaser-manifest-loader'
118118
import manifest from './manifest' // see manifest example below
119-
preload() {
119+
120+
// no webfonts in this manifest so we can simply use the preload method
121+
preload() {
120122
this.game.plugins.add(ManifestLoader).loadManifest(manifest)
121123
}
122124
```
123125

126+
## Advanced Usage (Webfonts)
127+
```
128+
import 'assets/fonts/bebas/stylesheet.css' // IMPORTANT remember to load your webfont stylesheet
129+
import ManifestLoader from 'phaser-manifest-loader'
130+
import manifest from './manifest' // see manifest example below
131+
132+
// Load in a manifest of assets including web fonts
133+
// because webfonts don't use the Phaser loader we can't take advantage of Phaser's
134+
// preload method. So we performing loading in the create method and use the Promise
135+
// returned from `loadManifest`.
136+
create() {
137+
const loader = this.game.plugins.add(ManifestLoader) // returns a Promise
138+
loader.loadManifest(manifest).then(() => {
139+
this.state.start('Main');
140+
})
141+
}
142+
```
143+
124144
## Manifest Example
125145
```
126146
const manifest = {
@@ -154,6 +174,18 @@ const manifest = {
154174
export default manifest
155175
```
156176

177+
## API
178+
### loadManifest
179+
180+
[loadManifest loads a manifest of assets]
181+
182+
**Parameters**
183+
184+
- `manifest` **Manifest**
185+
- `assetPostfix` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** (optional, default `.g`)
186+
187+
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
188+
157189
## Run demo
158190
`npm start`
159191

Diff for: lib/ManifestLoader.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ var ManifestLoader = function (_Phaser$Plugin) {
4141

4242
/**
4343
* [loadManifest loads a manifest of assets]
44-
* @param {Object} manifest
45-
* @param {String} assetPostfix (optional) default = ''
46-
* @return {Promise}
4744
*/
4845

4946
}, {
@@ -72,11 +69,15 @@ var ManifestLoader = function (_Phaser$Plugin) {
7269
key: '_loadWebFonts',
7370
value: function _loadWebFonts(fonts) {
7471
return new Promise(function (resolve) {
75-
_webfontloader2.default.load(Object.assign({}, fonts, {
76-
active: function active() {
77-
resolve();
78-
}
79-
}));
72+
if (!fonts) {
73+
resolve();
74+
} else {
75+
_webfontloader2.default.load(Object.assign({}, fonts, {
76+
active: function active() {
77+
resolve();
78+
}
79+
}));
80+
}
8081
});
8182
}
8283
}]);

Diff for: package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
},
1414
"license": "MIT",
1515
"devDependencies": {
16-
"babel-cli": "^6.22.2",
16+
"babel-cli": "^6.24.1",
1717
"babel-core": "^6.21.0",
1818
"babel-loader": "^6.2.10",
1919
"babel-polyfill": "^6.20.0",
2020
"babel-preset-es2015": "^6.18.0",
21+
"babel-preset-flow": "^6.23.0",
2122
"browser-sync": "^2.18.6",
2223
"browser-sync-webpack-plugin": "^1.1.3",
2324
"css-loader": "^0.26.1",
@@ -29,14 +30,15 @@
2930
"exports-loader": "^0.6.3",
3031
"expose-loader": "^0.7.1",
3132
"file-loader": "^0.9.0",
33+
"flow-bin": "^0.43.1",
3234
"image-webpack-loader": "^3.1.0",
3335
"imports-loader": "^0.7.0",
3436
"style-loader": "^0.13.1",
3537
"url-loader": "^0.5.7",
36-
"webpack": "^2.2.0",
37-
"phaser-ce": "^2.7.3"
38+
"webpack": "^2.2.0"
3839
},
3940
"dependencies": {
41+
"phaser-ce": "^2.7.3",
4042
"webfontloader": "^1.6.27"
4143
}
4244
}

Diff for: src/ManifestLoader.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@ import Phaser from 'phaser'
22
import WebFont from 'webfontloader'
33
import AssetLoader from './AssetLoader'
44

5+
type Manifest = {
6+
spritesheets: Array<string>,
7+
images: Array<string>,
8+
audio: Array<string>,
9+
bitmap_fonts: Array<string>,
10+
fonts: {
11+
custom: {
12+
families: Array<string>,
13+
},
14+
google: {
15+
families: Array<string>,
16+
},
17+
},
18+
}
19+
520
class ManifestLoader extends Phaser.Plugin {
621

722
init () {}
823

924
/**
1025
* [loadManifest loads a manifest of assets]
11-
* @param {Object} manifest
12-
* @param {String} assetPostfix (optional) default = ''
13-
* @return {Promise}
1426
*/
15-
loadManifest (manifest, assetPostfix = '') {
27+
loadManifest (manifest: Manifest, assetPostfix: string = ''): Promise {
1628
return Promise.all([
1729
this._loadAssets(manifest, assetPostfix),
1830
this._loadWebFonts(manifest.fonts)
@@ -33,11 +45,15 @@ class ManifestLoader extends Phaser.Plugin {
3345

3446
_loadWebFonts (fonts) {
3547
return new Promise((resolve) => {
36-
WebFont.load(Object.assign({}, fonts, {
37-
active: () => {
38-
resolve()
39-
}
40-
}))
48+
if (!fonts) {
49+
resolve()
50+
} else {
51+
WebFont.load(Object.assign({}, fonts, {
52+
active: () => {
53+
resolve()
54+
}
55+
}))
56+
}
4157
})
4258
}
4359
}

0 commit comments

Comments
 (0)