@urso/core 0.7.91 → 0.7.93-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +11 -11
- package/LICENSE +20 -20
- package/README.md +227 -227
- package/build/162.js/index.js +1 -0
- package/build/24.js/index.js +1 -0
- package/build/js/index.js +1 -1
- package/package.json +50 -50
- package/src/js/app.js +104 -104
- package/src/js/components/base/controller.js +78 -78
- package/src/js/components/debug/controller.js +38 -38
- package/src/js/components/debug/coords.js +23 -23
- package/src/js/components/debug/fps.js +36 -36
- package/src/js/components/debug/template.js +55 -55
- package/src/js/components/debug/timescale.js +60 -60
- package/src/js/components/deviceRotate/controller.js +95 -95
- package/src/js/components/editor/api.js +127 -127
- package/src/js/components/editor/controller.js +12 -12
- package/src/js/components/fullscreen/android.js +104 -104
- package/src/js/components/fullscreen/controller.js +76 -76
- package/src/js/components/fullscreen/desktop.js +49 -49
- package/src/js/components/fullscreen/ios.js +115 -115
- package/src/js/components/layersSwitcher/config.js +26 -26
- package/src/js/components/layersSwitcher/controller.js +36 -36
- package/src/js/components/loader/controller.js +66 -66
- package/src/js/components/loader/template.js +70 -70
- package/src/js/components/soundInitialPopup/controller.js +42 -42
- package/src/js/components/soundInitialPopup/template.js +109 -109
- package/src/js/components/stateDriven/controller.js +123 -123
- package/src/js/config/load.js +194 -194
- package/src/js/config/main.js +17 -17
- package/src/js/extra/browserEvents.js +57 -57
- package/src/js/extra/main.js +19 -19
- package/src/js/extra/pixiPatch.js +171 -171
- package/src/js/index.js +7 -7
- package/src/js/lib/cache.js +125 -125
- package/src/js/lib/composition.js +85 -85
- package/src/js/lib/device.js +1215 -1215
- package/src/js/lib/helper.js +678 -678
- package/src/js/lib/loader.js +211 -211
- package/src/js/lib/localData.js +28 -28
- package/src/js/lib/logger.js +69 -69
- package/src/js/lib/math.js +161 -161
- package/src/js/lib/objectPool.js +207 -207
- package/src/js/lib/time.js +18 -18
- package/src/js/lib/tween.js +152 -152
- package/src/js/modules/assets/baseModel.js +20 -20
- package/src/js/modules/assets/config.js +38 -38
- package/src/js/modules/assets/controller.js +65 -65
- package/src/js/modules/assets/models/atlas.js +18 -18
- package/src/js/modules/assets/models/audiosprite.js +28 -28
- package/src/js/modules/assets/models/bitmapFont.js +10 -10
- package/src/js/modules/assets/models/container.js +18 -18
- package/src/js/modules/assets/models/font.js +10 -10
- package/src/js/modules/assets/models/html.js +10 -10
- package/src/js/modules/assets/models/image.js +17 -17
- package/src/js/modules/assets/models/json.js +10 -10
- package/src/js/modules/assets/models/jsonAtlas.js +10 -10
- package/src/js/modules/assets/models/sound.js +16 -16
- package/src/js/modules/assets/models/spine.js +16 -16
- package/src/js/modules/assets/service.js +541 -541
- package/src/js/modules/i18n/config.js +17 -17
- package/src/js/modules/i18n/controller.js +71 -71
- package/src/js/modules/instances/controller.js +357 -357
- package/src/js/modules/logic/config/sounds.js +23 -23
- package/src/js/modules/logic/controller.js +52 -52
- package/src/js/modules/logic/main.js +8 -8
- package/src/js/modules/logic/sounds.js +103 -103
- package/src/js/modules/objects/baseModel.js +207 -207
- package/src/js/modules/objects/cache.js +99 -99
- package/src/js/modules/objects/config.js +9 -9
- package/src/js/modules/objects/controller.js +139 -139
- package/src/js/modules/objects/find.js +58 -58
- package/src/js/modules/objects/models/atlasImage.js +52 -52
- package/src/js/modules/objects/models/bitmapText.js +48 -48
- package/src/js/modules/objects/models/button.js +196 -196
- package/src/js/modules/objects/models/buttonComposite.js +37 -37
- package/src/js/modules/objects/models/checkbox.js +96 -96
- package/src/js/modules/objects/models/collection.js +54 -54
- package/src/js/modules/objects/models/component.js +48 -48
- package/src/js/modules/objects/models/container.js +21 -21
- package/src/js/modules/objects/models/dragContainer.js +664 -664
- package/src/js/modules/objects/models/emitter.js +69 -69
- package/src/js/modules/objects/models/emitterFx.js +114 -114
- package/src/js/modules/objects/models/graphics.js +40 -40
- package/src/js/modules/objects/models/group.js +21 -21
- package/src/js/modules/objects/models/hitArea.js +119 -119
- package/src/js/modules/objects/models/image.js +36 -36
- package/src/js/modules/objects/models/imagesAnimation.js +115 -115
- package/src/js/modules/objects/models/mask.js +40 -40
- package/src/js/modules/objects/models/nineSlicePlane.js +32 -32
- package/src/js/modules/objects/models/scrollbox.js +66 -66
- package/src/js/modules/objects/models/slider.js +352 -352
- package/src/js/modules/objects/models/spine.js +338 -338
- package/src/js/modules/objects/models/text.js +70 -70
- package/src/js/modules/objects/models/textInput.js +68 -68
- package/src/js/modules/objects/models/toggle.js +179 -179
- package/src/js/modules/objects/models/world.js +21 -21
- package/src/js/modules/objects/pool.js +68 -68
- package/src/js/modules/objects/propertyAdapter.js +588 -588
- package/src/js/modules/objects/proxy.js +298 -298
- package/src/js/modules/objects/selector.js +136 -136
- package/src/js/modules/objects/service.js +254 -254
- package/src/js/modules/objects/styles.js +210 -210
- package/src/js/modules/observer/controller.js +167 -167
- package/src/js/modules/observer/events.js +56 -56
- package/src/js/modules/scenes/controller.js +127 -127
- package/src/js/modules/scenes/model.js +28 -28
- package/src/js/modules/scenes/pixiWrapper.js +351 -351
- package/src/js/modules/scenes/resolutions.js +173 -173
- package/src/js/modules/scenes/resolutionsConfig.js +73 -73
- package/src/js/modules/scenes/service.js +146 -146
- package/src/js/modules/soundManager/controller.js +103 -103
- package/src/js/modules/soundManager/soundSprite.js +285 -285
- package/src/js/modules/statesManager/action.js +97 -97
- package/src/js/modules/statesManager/all.js +23 -23
- package/src/js/modules/statesManager/configStates.js +77 -77
- package/src/js/modules/statesManager/controller.js +218 -218
- package/src/js/modules/statesManager/functionsStorage.js +82 -82
- package/src/js/modules/statesManager/helper.js +27 -27
- package/src/js/modules/statesManager/race.js +91 -91
- package/src/js/modules/statesManager/sequence.js +48 -48
- package/src/js/modules/template/controller.js +28 -28
- package/src/js/modules/template/model.js +11 -11
- package/src/js/modules/template/service.js +137 -137
- package/src/js/modules/template/types.js +48 -48
- package/src/js/modules/transport/baseConnectionType.js +25 -25
- package/src/js/modules/transport/config.js +13 -13
- package/src/js/modules/transport/connectionTypes/websocket.js +76 -76
- package/src/js/modules/transport/connectionTypes/xhr.js +46 -46
- package/src/js/modules/transport/controller.js +48 -48
- package/src/js/modules/transport/decorator.js +17 -17
- package/src/js/modules/transport/service.js +150 -150
- package/webpack.config.js +47 -47
|
@@ -1,541 +1,541 @@
|
|
|
1
|
-
class ModulesAssetsService {
|
|
2
|
-
|
|
3
|
-
constructor() {
|
|
4
|
-
this.singleton = true;
|
|
5
|
-
|
|
6
|
-
this.assets = {};
|
|
7
|
-
this._loadCounter = 0;
|
|
8
|
-
|
|
9
|
-
this._currentQuality = 'auto';
|
|
10
|
-
this._addedAssetsCache = [];
|
|
11
|
-
this.lazyLoadProcessStarted = false;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* system; check webP support
|
|
16
|
-
*/
|
|
17
|
-
checkWebPSupport() {
|
|
18
|
-
if (Urso.device.webP)
|
|
19
|
-
Urso.addInstancesMode('webP');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* get current quality
|
|
24
|
-
* @returns {String}
|
|
25
|
-
*/
|
|
26
|
-
getQuality() {
|
|
27
|
-
return this._currentQuality;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Update quality
|
|
32
|
-
*/
|
|
33
|
-
updateQuality() {
|
|
34
|
-
this._currentQuality = this._detectQuality();
|
|
35
|
-
Urso.addInstancesMode(this._currentQuality + 'Quality');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* sort assets and store to assets space
|
|
40
|
-
* @param {Mixed} assets - asset or array of assets
|
|
41
|
-
* @returns {Object}
|
|
42
|
-
*/
|
|
43
|
-
sortAssets(assets) {
|
|
44
|
-
const assetsSpace = this._createNewAssetsSpace();
|
|
45
|
-
assetsSpace[this.getInstance('Config').loadingGroups.initial] = [];
|
|
46
|
-
|
|
47
|
-
if (Array.isArray(assets))
|
|
48
|
-
for (let asset of assets)
|
|
49
|
-
this._addAsset(assetsSpace, asset);
|
|
50
|
-
else
|
|
51
|
-
this._addAsset(assetsSpace, assets);
|
|
52
|
-
|
|
53
|
-
return assetsSpace;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* start load assetsSpace
|
|
58
|
-
* @param {Object} assetsSpace
|
|
59
|
-
* @param {Function} callback
|
|
60
|
-
*/
|
|
61
|
-
startLoad(assetsSpace, callback, updateCallback) {
|
|
62
|
-
this.loadGroup(
|
|
63
|
-
assetsSpace,
|
|
64
|
-
this.getInstance('Config').loadingGroups.initial,
|
|
65
|
-
(() => { callback(); this._startLazyLoad(); }).bind(this),
|
|
66
|
-
updateCallback
|
|
67
|
-
)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* load assets group by name
|
|
72
|
-
* @param {Object} assetsSpace
|
|
73
|
-
* @param {String} group
|
|
74
|
-
* @param {Function} callback
|
|
75
|
-
* @returns
|
|
76
|
-
*/
|
|
77
|
-
loadGroup(assetsSpace, group, callback = () => { }, updateCallback = () => { }) {
|
|
78
|
-
if (!assetsSpace) //global space
|
|
79
|
-
assetsSpace = this.assets;
|
|
80
|
-
|
|
81
|
-
if (!assetsSpace[group])
|
|
82
|
-
return Urso.logger.error('ModulesAssetsService group error, no assets:' + group + ' Check ModulesAssetsConfig please');
|
|
83
|
-
|
|
84
|
-
//we need load and parse atlases at first (!)
|
|
85
|
-
const loadRestAssetsCallback = () => { this._loadGroupRestAssets(assetsSpace, group, callback, updateCallback) };
|
|
86
|
-
|
|
87
|
-
const loadAtlasesCallback = () => {
|
|
88
|
-
this._loadGroupAtlases(assetsSpace, group, loadRestAssetsCallback, Urso.types.assets.ATLAS);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
this._loadGroupAtlases(assetsSpace, group, loadAtlasesCallback, Urso.types.assets.JSONATLAS);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
preloadAllImagesInGPU() {
|
|
95
|
-
const world = Urso.findOne('^WORLD')._baseObject;
|
|
96
|
-
|
|
97
|
-
Urso.cache.globalAtlas.pages.forEach(({ baseTexture }) => {
|
|
98
|
-
const texture = new PIXI.Texture(baseTexture);
|
|
99
|
-
const sprite = new PIXI.Sprite(texture);
|
|
100
|
-
|
|
101
|
-
sprite.x = -10000;
|
|
102
|
-
sprite.y = -10000;
|
|
103
|
-
|
|
104
|
-
world.addChild(sprite);
|
|
105
|
-
|
|
106
|
-
setTimeout(() => {
|
|
107
|
-
world.removeChild(sprite);
|
|
108
|
-
sprite.destroy()
|
|
109
|
-
}, 1);
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* create new assets space for loading
|
|
115
|
-
* @returns {Object}
|
|
116
|
-
*/
|
|
117
|
-
_createNewAssetsSpace() {
|
|
118
|
-
this._loadCounter++;
|
|
119
|
-
this.assets[this._loadCounter] = {};
|
|
120
|
-
return this.assets[this._loadCounter];
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* load atlases from assets group
|
|
125
|
-
* @param {Object} assetsSpace
|
|
126
|
-
* @param {String} group
|
|
127
|
-
* @param {Function} callback
|
|
128
|
-
*/
|
|
129
|
-
_loadGroupAtlases(assetsSpace, group, callback, atlasType) {
|
|
130
|
-
const atlases = assetsSpace[group].filter(assetModel => assetModel.type === atlasType);
|
|
131
|
-
|
|
132
|
-
if (!atlases.length)
|
|
133
|
-
return callback();
|
|
134
|
-
|
|
135
|
-
let loader = Urso.getInstance('Lib.Loader');
|
|
136
|
-
|
|
137
|
-
for (let assetModel of atlases)
|
|
138
|
-
this._addAssetToLoader(assetModel, loader);
|
|
139
|
-
|
|
140
|
-
loader.start(() => {
|
|
141
|
-
if (atlasType === Urso.types.assets.ATLAS)
|
|
142
|
-
this._processLoadedAtlases(assetsSpace, group);
|
|
143
|
-
|
|
144
|
-
callback();
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* load rest assets (atkases is loaded)
|
|
150
|
-
* @param {Object} assetsSpace
|
|
151
|
-
* @param {String} group
|
|
152
|
-
* @param {Function} callback
|
|
153
|
-
*/
|
|
154
|
-
_loadGroupRestAssets(assetsSpace, group, callback, updateCallback) {
|
|
155
|
-
let loader = Urso.getInstance('Lib.Loader');
|
|
156
|
-
//load update callback
|
|
157
|
-
loader.setOnLoadUpdate((params) => { updateCallback(Math.floor(params.progress)); });
|
|
158
|
-
const noAtlasSpines = [];
|
|
159
|
-
|
|
160
|
-
for (let assetModel of assetsSpace[group])
|
|
161
|
-
if (assetModel.type !== Urso.types.assets.ATLAS && assetModel.type !== Urso.types.assets.JSONATLAS)
|
|
162
|
-
if (!Urso.cache.getFile(assetModel.path)) {
|
|
163
|
-
//filter noAtlas Spine files
|
|
164
|
-
if (assetModel.type === Urso.types.assets.SPINE && assetModel.noAtlas) {
|
|
165
|
-
noAtlasSpines.push(assetModel);
|
|
166
|
-
} else
|
|
167
|
-
this._addAssetToLoader(assetModel, loader);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
loader.start(
|
|
171
|
-
() => {
|
|
172
|
-
this._processLoadedAssets(assetsSpace, group);
|
|
173
|
-
this._loadNoAtlasSpines(noAtlasSpines, () => {
|
|
174
|
-
this.emit(Urso.events.MODULES_ASSETS_GROUP_LOADED, group);
|
|
175
|
-
callback();
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* load spines without .atlas files
|
|
183
|
-
* @param {Array} noAtlasSpines
|
|
184
|
-
* @param {Function} callback
|
|
185
|
-
* @returns
|
|
186
|
-
*/
|
|
187
|
-
_loadNoAtlasSpines(noAtlasSpines, callback) {
|
|
188
|
-
if (!noAtlasSpines.length)
|
|
189
|
-
return callback();
|
|
190
|
-
|
|
191
|
-
let loader = Urso.getInstance('Lib.Loader');
|
|
192
|
-
|
|
193
|
-
for (let assetModel of noAtlasSpines)
|
|
194
|
-
this._addAssetToLoader(assetModel, loader);
|
|
195
|
-
|
|
196
|
-
loader.start(callback);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* process loaded atlases
|
|
201
|
-
* @param {Object} assetsSpace
|
|
202
|
-
* @param {String} group
|
|
203
|
-
*/
|
|
204
|
-
_processLoadedAtlases(assetsSpace, group) {
|
|
205
|
-
const atlases = assetsSpace[group].filter(assetModel => assetModel.type === Urso.types.assets.ATLAS);
|
|
206
|
-
const { addFolderPathInAtlasTextureKey } = this.getInstance('Config');
|
|
207
|
-
|
|
208
|
-
for (let assetModel of atlases) {
|
|
209
|
-
const assetKey = assetModel.key;
|
|
210
|
-
let imageData = Urso.cache.getAtlas(assetKey);
|
|
211
|
-
const folderPath = imageData.url.split('/').slice(0, -1).join('/');
|
|
212
|
-
|
|
213
|
-
for (const name of Object.keys(imageData.spritesheet._frames)) {
|
|
214
|
-
let texture = imageData.textures[name];
|
|
215
|
-
let newFilename = name;
|
|
216
|
-
|
|
217
|
-
if (addFolderPathInAtlasTextureKey && !name.includes('/'))
|
|
218
|
-
newFilename = folderPath + '/' + name;
|
|
219
|
-
|
|
220
|
-
Urso.cache.addFile(newFilename, texture);
|
|
221
|
-
|
|
222
|
-
if (assetModel.cacheTextures) {
|
|
223
|
-
const textureKey = newFilename.split('.')[0];
|
|
224
|
-
Urso.cache.addTexture(textureKey, texture);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* process loaded assets
|
|
232
|
-
* @param {Object} assetsSpace
|
|
233
|
-
* @param {String} group
|
|
234
|
-
*/
|
|
235
|
-
_processLoadedAssets(assetsSpace, group) {
|
|
236
|
-
for (let assetModel of assetsSpace[group]) {
|
|
237
|
-
if (assetModel.type === Urso.types.assets.IMAGE)
|
|
238
|
-
this._processLoadedImage(assetModel);
|
|
239
|
-
|
|
240
|
-
if (assetModel.type === Urso.types.assets.BITMAPFONT)
|
|
241
|
-
this._processLoadedBitmapFont(assetModel);
|
|
242
|
-
|
|
243
|
-
if (assetModel.type === Urso.types.assets.FONT)
|
|
244
|
-
this._processLoadedFont(assetModel);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
delete assetsSpace[group];
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* process loaded font
|
|
252
|
-
* @param {Object} source
|
|
253
|
-
*/
|
|
254
|
-
_processLoadedFont(source) {
|
|
255
|
-
const data = Urso.cache.getFile(source.key);
|
|
256
|
-
const font = new FontFace(source.key, data.data);
|
|
257
|
-
font.load().then(() => {
|
|
258
|
-
document.fonts.add(font);
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* process loaded bitmap font
|
|
264
|
-
* @param {Object} source
|
|
265
|
-
*/
|
|
266
|
-
_processLoadedBitmapFont(assetModel) {
|
|
267
|
-
this._updateFontKey(assetModel.key)
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Update font key if cannot use original
|
|
272
|
-
* @param {String} fontName
|
|
273
|
-
* @returns
|
|
274
|
-
*/
|
|
275
|
-
_updateFontKey(fontName) {
|
|
276
|
-
const fontData = Urso.cache.getBitmapFont(fontName);
|
|
277
|
-
|
|
278
|
-
if (PIXI.BitmapFont.available[fontName] || !fontData)
|
|
279
|
-
return;
|
|
280
|
-
|
|
281
|
-
const savedFont = PIXI.BitmapFont.available[fontData.bitmapFont.font];
|
|
282
|
-
|
|
283
|
-
if (savedFont) {
|
|
284
|
-
Urso.logger.warn(`bitmapFont ${fontData.bitmapFont.font} was rewritten with key ${fontName}`);
|
|
285
|
-
PIXI.BitmapFont.available[fontName] = savedFont;
|
|
286
|
-
delete PIXI.BitmapFont.available[fontData.bitmapFont.font];
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* get current assets resolution
|
|
292
|
-
* @returns {Number}
|
|
293
|
-
*/
|
|
294
|
-
getCurrentResolution() {
|
|
295
|
-
const { qualityFactors, defaultQualityFactor } = this.getInstance('Config');
|
|
296
|
-
return qualityFactors[this._currentQuality] || defaultQualityFactor || 1;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* process loaded image
|
|
301
|
-
* @param {Object} assetModel
|
|
302
|
-
*/
|
|
303
|
-
_processLoadedImage(assetModel) {
|
|
304
|
-
const resolution = this.getCurrentResolution();
|
|
305
|
-
|
|
306
|
-
const assetKey = assetModel.key;
|
|
307
|
-
//textures cache
|
|
308
|
-
let imageData = Urso.cache.getImage(assetKey);
|
|
309
|
-
|
|
310
|
-
if (!imageData) {
|
|
311
|
-
//from atlas ?!
|
|
312
|
-
let texture = Urso.cache.getFile(assetModel.path);
|
|
313
|
-
|
|
314
|
-
if (!texture)
|
|
315
|
-
return Urso.logger.error('ModulesAssetsService process Loaded Image error: no image ', assetModel);
|
|
316
|
-
|
|
317
|
-
Urso.cache.addTexture(assetKey, texture); //TODO change resolution of base texture
|
|
318
|
-
} else {
|
|
319
|
-
//regular image
|
|
320
|
-
const baseTexture = new PIXI.BaseTexture(imageData.data, { resolution });
|
|
321
|
-
const texture = new PIXI.Texture(baseTexture);
|
|
322
|
-
Urso.cache.addTexture(assetKey, texture);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
if (assetModel.preloadGPU) {
|
|
326
|
-
let tempOblect = Urso.objects.create({
|
|
327
|
-
type: Urso.types.objects.IMAGE,
|
|
328
|
-
assetKey: assetKey,
|
|
329
|
-
x: -10000, y: -10000
|
|
330
|
-
}, false, true, true);
|
|
331
|
-
|
|
332
|
-
setTimeout(() => { tempOblect.destroy() }, 1)
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* add asset
|
|
338
|
-
* @param {Object} assetsSpace
|
|
339
|
-
* @param {Object} asset
|
|
340
|
-
* @param {String} loadingGroup
|
|
341
|
-
*/
|
|
342
|
-
_addAsset(assetsSpace, asset, loadingGroup) {
|
|
343
|
-
//cache for all assets. We do not need to load same assets twice or more
|
|
344
|
-
if (asset.type !== Urso.types.assets.CONTAINER) {
|
|
345
|
-
let addedAssetKey = `${asset.type}_${asset.key}`;
|
|
346
|
-
|
|
347
|
-
if (this._addedAssetsCache.includes(addedAssetKey))
|
|
348
|
-
return;
|
|
349
|
-
|
|
350
|
-
this._addedAssetsCache.push(addedAssetKey);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
let model;
|
|
354
|
-
|
|
355
|
-
switch (asset.type) {
|
|
356
|
-
case Urso.types.assets.ATLAS:
|
|
357
|
-
model = this.getInstance('Models.Atlas', asset)
|
|
358
|
-
break;
|
|
359
|
-
case Urso.types.assets.AUDIOSPRITE:
|
|
360
|
-
model = this.getInstance('Models.Audiosprite', asset)
|
|
361
|
-
break;
|
|
362
|
-
case Urso.types.assets.BITMAPFONT:
|
|
363
|
-
model = this.getInstance('Models.BitmapFont', asset)
|
|
364
|
-
break;
|
|
365
|
-
case Urso.types.assets.CONTAINER:
|
|
366
|
-
model = this.getInstance('Models.Container', asset)
|
|
367
|
-
break;
|
|
368
|
-
case Urso.types.assets.FONT:
|
|
369
|
-
model = this.getInstance('Models.Font', asset)
|
|
370
|
-
break;
|
|
371
|
-
case Urso.types.assets.HTML:
|
|
372
|
-
model = this.getInstance('Models.Html', asset)
|
|
373
|
-
break;
|
|
374
|
-
case Urso.types.assets.IMAGE:
|
|
375
|
-
model = this.getInstance('Models.Image', asset)
|
|
376
|
-
break;
|
|
377
|
-
case Urso.types.assets.JSON:
|
|
378
|
-
model = this.getInstance('Models.Json', asset)
|
|
379
|
-
break;
|
|
380
|
-
case Urso.types.assets.JSONATLAS:
|
|
381
|
-
model = this.getInstance('Models.JsonAtlas', asset)
|
|
382
|
-
break;
|
|
383
|
-
case Urso.types.assets.SOUND:
|
|
384
|
-
model = this.getInstance('Models.Sound', asset)
|
|
385
|
-
break;
|
|
386
|
-
case Urso.types.assets.SPINE:
|
|
387
|
-
model = this.getInstance('Models.Spine', asset)
|
|
388
|
-
break;
|
|
389
|
-
default:
|
|
390
|
-
Urso.logger.error('ModulesAssetsService asset type error', asset);
|
|
391
|
-
break;
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
//set loadingGroup
|
|
395
|
-
model.loadingGroup = loadingGroup || model.loadingGroup || this.getInstance('Config').loadingGroups.initial;
|
|
396
|
-
|
|
397
|
-
//check if container
|
|
398
|
-
if (model.contents) {
|
|
399
|
-
for (let content of model.contents) {
|
|
400
|
-
this._addAsset(assetsSpace, content, model.loadingGroup);
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
return;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
if (model.loadingGroup === this.getInstance('Config').loadingGroups.initial) {
|
|
407
|
-
assetsSpace[model.loadingGroup].push(model);
|
|
408
|
-
} else {
|
|
409
|
-
//add single asset to loading group
|
|
410
|
-
if (!this.assets[model.loadingGroup])
|
|
411
|
-
this.assets[model.loadingGroup] = [];
|
|
412
|
-
|
|
413
|
-
this.assets[model.loadingGroup].push(model);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
/**
|
|
418
|
-
* start lazy load process
|
|
419
|
-
*/
|
|
420
|
-
_startLazyLoad() {
|
|
421
|
-
if (this.lazyLoadProcessStarted)
|
|
422
|
-
return;
|
|
423
|
-
|
|
424
|
-
this.lazyLoadProcessStarted = true;
|
|
425
|
-
this._continueLazyLoad();
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* continue lazy load process (with current step)
|
|
430
|
-
* @param {Number} step
|
|
431
|
-
*/
|
|
432
|
-
_continueLazyLoad(step) {
|
|
433
|
-
if (!step)
|
|
434
|
-
step = 0;
|
|
435
|
-
|
|
436
|
-
const lazyLoadGroups = this.getInstance('Config').lazyLoadGroups;
|
|
437
|
-
|
|
438
|
-
if (step >= lazyLoadGroups.length) {
|
|
439
|
-
this.emit(Urso.events.MODULES_ASSETS_LAZYLOAD_FINISHED);
|
|
440
|
-
return;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
let groupName = lazyLoadGroups[step];
|
|
444
|
-
|
|
445
|
-
if (!groupName)
|
|
446
|
-
Urso.logger.error('ModulesAssetsService lazy loading groupName error');
|
|
447
|
-
|
|
448
|
-
this.loadGroup(null, groupName, () => { this._continueLazyLoad(step + 1); })
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* quality reducer
|
|
453
|
-
* @param {Object} qualityFactors
|
|
454
|
-
* @param {Number} widthFactor
|
|
455
|
-
* @returns
|
|
456
|
-
*/
|
|
457
|
-
_qualityReducer(qualityFactors, widthFactor) {
|
|
458
|
-
return [(acc, val) => {
|
|
459
|
-
if (acc === null) {
|
|
460
|
-
return val;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
const currentQuality = qualityFactors[acc];
|
|
464
|
-
const qualityFactor = qualityFactors[val];
|
|
465
|
-
|
|
466
|
-
const nextQuality = (currentQuality > qualityFactor && qualityFactor >= widthFactor)
|
|
467
|
-
|| (qualityFactor >= widthFactor && widthFactor > currentQuality)
|
|
468
|
-
|| (widthFactor >= qualityFactor && qualityFactor > currentQuality);
|
|
469
|
-
|
|
470
|
-
return nextQuality ? val : acc;
|
|
471
|
-
|
|
472
|
-
}, null]
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
/**
|
|
476
|
-
* detect assets quality
|
|
477
|
-
* @returns {Number}
|
|
478
|
-
*/
|
|
479
|
-
_detectQuality() {
|
|
480
|
-
const { qualityFactors } = this.getInstance('Config');
|
|
481
|
-
const userQuality = Urso.helper.parseGetParams()['quality'];
|
|
482
|
-
|
|
483
|
-
if (userQuality && qualityFactors[userQuality]) {
|
|
484
|
-
return userQuality;
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
return this._calculateQuality(qualityFactors);
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
/**
|
|
491
|
-
* calculate quality
|
|
492
|
-
* @param {Object} qualityFactors
|
|
493
|
-
*/
|
|
494
|
-
_calculateQuality(qualityFactors) {
|
|
495
|
-
const { android, iOS, iPad, macOS } = Urso.device;
|
|
496
|
-
const isMobile = android || iOS || iPad;
|
|
497
|
-
|
|
498
|
-
if (macOS && !isMobile) {
|
|
499
|
-
return 'high';
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
if (macOS && iPad) {
|
|
503
|
-
return 'medium';
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
const resCfg = Urso.getInstance('Modules.Scenes.ResolutionsConfig').contents[0];
|
|
507
|
-
|
|
508
|
-
const { devicePixelRatio } = window;
|
|
509
|
-
let { width, height } = screen;
|
|
510
|
-
|
|
511
|
-
if (isMobile) {
|
|
512
|
-
width = (width > height) ? width : height;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
if (iOS) {
|
|
516
|
-
width *= devicePixelRatio;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
const widthFactor = width / resCfg.width;
|
|
520
|
-
|
|
521
|
-
return Object
|
|
522
|
-
.keys(qualityFactors)
|
|
523
|
-
.reduce(...this._qualityReducer(qualityFactors, widthFactor));
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
/**
|
|
527
|
-
* add asset to loader instance
|
|
528
|
-
* @param {Object} assetModel
|
|
529
|
-
* @param {Object} loader
|
|
530
|
-
*/
|
|
531
|
-
_addAssetToLoader(assetModel, loader) {
|
|
532
|
-
if (assetModel.path)
|
|
533
|
-
loader.addAsset(assetModel);
|
|
534
|
-
else if (assetModel.contents) {
|
|
535
|
-
//do nothing, its a container
|
|
536
|
-
} else
|
|
537
|
-
Urso.logger.error('ModulesAssetsService model error', assetModel);
|
|
538
|
-
};
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
module.exports = ModulesAssetsService;
|
|
1
|
+
class ModulesAssetsService {
|
|
2
|
+
|
|
3
|
+
constructor() {
|
|
4
|
+
this.singleton = true;
|
|
5
|
+
|
|
6
|
+
this.assets = {};
|
|
7
|
+
this._loadCounter = 0;
|
|
8
|
+
|
|
9
|
+
this._currentQuality = 'auto';
|
|
10
|
+
this._addedAssetsCache = [];
|
|
11
|
+
this.lazyLoadProcessStarted = false;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* system; check webP support
|
|
16
|
+
*/
|
|
17
|
+
checkWebPSupport() {
|
|
18
|
+
if (Urso.device.webP)
|
|
19
|
+
Urso.addInstancesMode('webP');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* get current quality
|
|
24
|
+
* @returns {String}
|
|
25
|
+
*/
|
|
26
|
+
getQuality() {
|
|
27
|
+
return this._currentQuality;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Update quality
|
|
32
|
+
*/
|
|
33
|
+
updateQuality() {
|
|
34
|
+
this._currentQuality = this._detectQuality();
|
|
35
|
+
Urso.addInstancesMode(this._currentQuality + 'Quality');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* sort assets and store to assets space
|
|
40
|
+
* @param {Mixed} assets - asset or array of assets
|
|
41
|
+
* @returns {Object}
|
|
42
|
+
*/
|
|
43
|
+
sortAssets(assets) {
|
|
44
|
+
const assetsSpace = this._createNewAssetsSpace();
|
|
45
|
+
assetsSpace[this.getInstance('Config').loadingGroups.initial] = [];
|
|
46
|
+
|
|
47
|
+
if (Array.isArray(assets))
|
|
48
|
+
for (let asset of assets)
|
|
49
|
+
this._addAsset(assetsSpace, asset);
|
|
50
|
+
else
|
|
51
|
+
this._addAsset(assetsSpace, assets);
|
|
52
|
+
|
|
53
|
+
return assetsSpace;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* start load assetsSpace
|
|
58
|
+
* @param {Object} assetsSpace
|
|
59
|
+
* @param {Function} callback
|
|
60
|
+
*/
|
|
61
|
+
startLoad(assetsSpace, callback, updateCallback) {
|
|
62
|
+
this.loadGroup(
|
|
63
|
+
assetsSpace,
|
|
64
|
+
this.getInstance('Config').loadingGroups.initial,
|
|
65
|
+
(() => { callback(); this._startLazyLoad(); }).bind(this),
|
|
66
|
+
updateCallback
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* load assets group by name
|
|
72
|
+
* @param {Object} assetsSpace
|
|
73
|
+
* @param {String} group
|
|
74
|
+
* @param {Function} callback
|
|
75
|
+
* @returns
|
|
76
|
+
*/
|
|
77
|
+
loadGroup(assetsSpace, group, callback = () => { }, updateCallback = () => { }) {
|
|
78
|
+
if (!assetsSpace) //global space
|
|
79
|
+
assetsSpace = this.assets;
|
|
80
|
+
|
|
81
|
+
if (!assetsSpace[group])
|
|
82
|
+
return Urso.logger.error('ModulesAssetsService group error, no assets:' + group + ' Check ModulesAssetsConfig please');
|
|
83
|
+
|
|
84
|
+
//we need load and parse atlases at first (!)
|
|
85
|
+
const loadRestAssetsCallback = () => { this._loadGroupRestAssets(assetsSpace, group, callback, updateCallback) };
|
|
86
|
+
|
|
87
|
+
const loadAtlasesCallback = () => {
|
|
88
|
+
this._loadGroupAtlases(assetsSpace, group, loadRestAssetsCallback, Urso.types.assets.ATLAS);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
this._loadGroupAtlases(assetsSpace, group, loadAtlasesCallback, Urso.types.assets.JSONATLAS);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
preloadAllImagesInGPU() {
|
|
95
|
+
const world = Urso.findOne('^WORLD')._baseObject;
|
|
96
|
+
|
|
97
|
+
Urso.cache.globalAtlas.pages.forEach(({ baseTexture }) => {
|
|
98
|
+
const texture = new PIXI.Texture(baseTexture);
|
|
99
|
+
const sprite = new PIXI.Sprite(texture);
|
|
100
|
+
|
|
101
|
+
sprite.x = -10000;
|
|
102
|
+
sprite.y = -10000;
|
|
103
|
+
|
|
104
|
+
world.addChild(sprite);
|
|
105
|
+
|
|
106
|
+
setTimeout(() => {
|
|
107
|
+
world.removeChild(sprite);
|
|
108
|
+
sprite.destroy()
|
|
109
|
+
}, 1);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* create new assets space for loading
|
|
115
|
+
* @returns {Object}
|
|
116
|
+
*/
|
|
117
|
+
_createNewAssetsSpace() {
|
|
118
|
+
this._loadCounter++;
|
|
119
|
+
this.assets[this._loadCounter] = {};
|
|
120
|
+
return this.assets[this._loadCounter];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* load atlases from assets group
|
|
125
|
+
* @param {Object} assetsSpace
|
|
126
|
+
* @param {String} group
|
|
127
|
+
* @param {Function} callback
|
|
128
|
+
*/
|
|
129
|
+
_loadGroupAtlases(assetsSpace, group, callback, atlasType) {
|
|
130
|
+
const atlases = assetsSpace[group].filter(assetModel => assetModel.type === atlasType);
|
|
131
|
+
|
|
132
|
+
if (!atlases.length)
|
|
133
|
+
return callback();
|
|
134
|
+
|
|
135
|
+
let loader = Urso.getInstance('Lib.Loader');
|
|
136
|
+
|
|
137
|
+
for (let assetModel of atlases)
|
|
138
|
+
this._addAssetToLoader(assetModel, loader);
|
|
139
|
+
|
|
140
|
+
loader.start(() => {
|
|
141
|
+
if (atlasType === Urso.types.assets.ATLAS)
|
|
142
|
+
this._processLoadedAtlases(assetsSpace, group);
|
|
143
|
+
|
|
144
|
+
callback();
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* load rest assets (atkases is loaded)
|
|
150
|
+
* @param {Object} assetsSpace
|
|
151
|
+
* @param {String} group
|
|
152
|
+
* @param {Function} callback
|
|
153
|
+
*/
|
|
154
|
+
_loadGroupRestAssets(assetsSpace, group, callback, updateCallback) {
|
|
155
|
+
let loader = Urso.getInstance('Lib.Loader');
|
|
156
|
+
//load update callback
|
|
157
|
+
loader.setOnLoadUpdate((params) => { updateCallback(Math.floor(params.progress)); });
|
|
158
|
+
const noAtlasSpines = [];
|
|
159
|
+
|
|
160
|
+
for (let assetModel of assetsSpace[group])
|
|
161
|
+
if (assetModel.type !== Urso.types.assets.ATLAS && assetModel.type !== Urso.types.assets.JSONATLAS)
|
|
162
|
+
if (!Urso.cache.getFile(assetModel.path)) {
|
|
163
|
+
//filter noAtlas Spine files
|
|
164
|
+
if (assetModel.type === Urso.types.assets.SPINE && assetModel.noAtlas) {
|
|
165
|
+
noAtlasSpines.push(assetModel);
|
|
166
|
+
} else
|
|
167
|
+
this._addAssetToLoader(assetModel, loader);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
loader.start(
|
|
171
|
+
() => {
|
|
172
|
+
this._processLoadedAssets(assetsSpace, group);
|
|
173
|
+
this._loadNoAtlasSpines(noAtlasSpines, () => {
|
|
174
|
+
this.emit(Urso.events.MODULES_ASSETS_GROUP_LOADED, group);
|
|
175
|
+
callback();
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* load spines without .atlas files
|
|
183
|
+
* @param {Array} noAtlasSpines
|
|
184
|
+
* @param {Function} callback
|
|
185
|
+
* @returns
|
|
186
|
+
*/
|
|
187
|
+
_loadNoAtlasSpines(noAtlasSpines, callback) {
|
|
188
|
+
if (!noAtlasSpines.length)
|
|
189
|
+
return callback();
|
|
190
|
+
|
|
191
|
+
let loader = Urso.getInstance('Lib.Loader');
|
|
192
|
+
|
|
193
|
+
for (let assetModel of noAtlasSpines)
|
|
194
|
+
this._addAssetToLoader(assetModel, loader);
|
|
195
|
+
|
|
196
|
+
loader.start(callback);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* process loaded atlases
|
|
201
|
+
* @param {Object} assetsSpace
|
|
202
|
+
* @param {String} group
|
|
203
|
+
*/
|
|
204
|
+
_processLoadedAtlases(assetsSpace, group) {
|
|
205
|
+
const atlases = assetsSpace[group].filter(assetModel => assetModel.type === Urso.types.assets.ATLAS);
|
|
206
|
+
const { addFolderPathInAtlasTextureKey } = this.getInstance('Config');
|
|
207
|
+
|
|
208
|
+
for (let assetModel of atlases) {
|
|
209
|
+
const assetKey = assetModel.key;
|
|
210
|
+
let imageData = Urso.cache.getAtlas(assetKey);
|
|
211
|
+
const folderPath = imageData.url.split('/').slice(0, -1).join('/');
|
|
212
|
+
|
|
213
|
+
for (const name of Object.keys(imageData.spritesheet._frames)) {
|
|
214
|
+
let texture = imageData.textures[name];
|
|
215
|
+
let newFilename = name;
|
|
216
|
+
|
|
217
|
+
if (addFolderPathInAtlasTextureKey && !name.includes('/'))
|
|
218
|
+
newFilename = folderPath + '/' + name;
|
|
219
|
+
|
|
220
|
+
Urso.cache.addFile(newFilename, texture);
|
|
221
|
+
|
|
222
|
+
if (assetModel.cacheTextures) {
|
|
223
|
+
const textureKey = newFilename.split('.')[0];
|
|
224
|
+
Urso.cache.addTexture(textureKey, texture);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* process loaded assets
|
|
232
|
+
* @param {Object} assetsSpace
|
|
233
|
+
* @param {String} group
|
|
234
|
+
*/
|
|
235
|
+
_processLoadedAssets(assetsSpace, group) {
|
|
236
|
+
for (let assetModel of assetsSpace[group]) {
|
|
237
|
+
if (assetModel.type === Urso.types.assets.IMAGE)
|
|
238
|
+
this._processLoadedImage(assetModel);
|
|
239
|
+
|
|
240
|
+
if (assetModel.type === Urso.types.assets.BITMAPFONT)
|
|
241
|
+
this._processLoadedBitmapFont(assetModel);
|
|
242
|
+
|
|
243
|
+
if (assetModel.type === Urso.types.assets.FONT)
|
|
244
|
+
this._processLoadedFont(assetModel);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
delete assetsSpace[group];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* process loaded font
|
|
252
|
+
* @param {Object} source
|
|
253
|
+
*/
|
|
254
|
+
_processLoadedFont(source) {
|
|
255
|
+
const data = Urso.cache.getFile(source.key);
|
|
256
|
+
const font = new FontFace(source.key, data.data);
|
|
257
|
+
font.load().then(() => {
|
|
258
|
+
document.fonts.add(font);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* process loaded bitmap font
|
|
264
|
+
* @param {Object} source
|
|
265
|
+
*/
|
|
266
|
+
_processLoadedBitmapFont(assetModel) {
|
|
267
|
+
this._updateFontKey(assetModel.key)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Update font key if cannot use original
|
|
272
|
+
* @param {String} fontName
|
|
273
|
+
* @returns
|
|
274
|
+
*/
|
|
275
|
+
_updateFontKey(fontName) {
|
|
276
|
+
const fontData = Urso.cache.getBitmapFont(fontName);
|
|
277
|
+
|
|
278
|
+
if (PIXI.BitmapFont.available[fontName] || !fontData)
|
|
279
|
+
return;
|
|
280
|
+
|
|
281
|
+
const savedFont = PIXI.BitmapFont.available[fontData.bitmapFont.font];
|
|
282
|
+
|
|
283
|
+
if (savedFont) {
|
|
284
|
+
Urso.logger.warn(`bitmapFont ${fontData.bitmapFont.font} was rewritten with key ${fontName}`);
|
|
285
|
+
PIXI.BitmapFont.available[fontName] = savedFont;
|
|
286
|
+
delete PIXI.BitmapFont.available[fontData.bitmapFont.font];
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* get current assets resolution
|
|
292
|
+
* @returns {Number}
|
|
293
|
+
*/
|
|
294
|
+
getCurrentResolution() {
|
|
295
|
+
const { qualityFactors, defaultQualityFactor } = this.getInstance('Config');
|
|
296
|
+
return qualityFactors[this._currentQuality] || defaultQualityFactor || 1;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* process loaded image
|
|
301
|
+
* @param {Object} assetModel
|
|
302
|
+
*/
|
|
303
|
+
_processLoadedImage(assetModel) {
|
|
304
|
+
const resolution = this.getCurrentResolution();
|
|
305
|
+
|
|
306
|
+
const assetKey = assetModel.key;
|
|
307
|
+
//textures cache
|
|
308
|
+
let imageData = Urso.cache.getImage(assetKey);
|
|
309
|
+
|
|
310
|
+
if (!imageData) {
|
|
311
|
+
//from atlas ?!
|
|
312
|
+
let texture = Urso.cache.getFile(assetModel.path);
|
|
313
|
+
|
|
314
|
+
if (!texture)
|
|
315
|
+
return Urso.logger.error('ModulesAssetsService process Loaded Image error: no image ', assetModel);
|
|
316
|
+
|
|
317
|
+
Urso.cache.addTexture(assetKey, texture); //TODO change resolution of base texture
|
|
318
|
+
} else {
|
|
319
|
+
//regular image
|
|
320
|
+
const baseTexture = new PIXI.BaseTexture(imageData.data, { resolution });
|
|
321
|
+
const texture = new PIXI.Texture(baseTexture);
|
|
322
|
+
Urso.cache.addTexture(assetKey, texture);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (assetModel.preloadGPU) {
|
|
326
|
+
let tempOblect = Urso.objects.create({
|
|
327
|
+
type: Urso.types.objects.IMAGE,
|
|
328
|
+
assetKey: assetKey,
|
|
329
|
+
x: -10000, y: -10000
|
|
330
|
+
}, false, true, true);
|
|
331
|
+
|
|
332
|
+
setTimeout(() => { tempOblect.destroy() }, 1)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* add asset
|
|
338
|
+
* @param {Object} assetsSpace
|
|
339
|
+
* @param {Object} asset
|
|
340
|
+
* @param {String} loadingGroup
|
|
341
|
+
*/
|
|
342
|
+
_addAsset(assetsSpace, asset, loadingGroup) {
|
|
343
|
+
//cache for all assets. We do not need to load same assets twice or more
|
|
344
|
+
if (asset.type !== Urso.types.assets.CONTAINER) {
|
|
345
|
+
let addedAssetKey = `${asset.type}_${asset.key}`;
|
|
346
|
+
|
|
347
|
+
if (this._addedAssetsCache.includes(addedAssetKey))
|
|
348
|
+
return;
|
|
349
|
+
|
|
350
|
+
this._addedAssetsCache.push(addedAssetKey);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
let model;
|
|
354
|
+
|
|
355
|
+
switch (asset.type) {
|
|
356
|
+
case Urso.types.assets.ATLAS:
|
|
357
|
+
model = this.getInstance('Models.Atlas', asset)
|
|
358
|
+
break;
|
|
359
|
+
case Urso.types.assets.AUDIOSPRITE:
|
|
360
|
+
model = this.getInstance('Models.Audiosprite', asset)
|
|
361
|
+
break;
|
|
362
|
+
case Urso.types.assets.BITMAPFONT:
|
|
363
|
+
model = this.getInstance('Models.BitmapFont', asset)
|
|
364
|
+
break;
|
|
365
|
+
case Urso.types.assets.CONTAINER:
|
|
366
|
+
model = this.getInstance('Models.Container', asset)
|
|
367
|
+
break;
|
|
368
|
+
case Urso.types.assets.FONT:
|
|
369
|
+
model = this.getInstance('Models.Font', asset)
|
|
370
|
+
break;
|
|
371
|
+
case Urso.types.assets.HTML:
|
|
372
|
+
model = this.getInstance('Models.Html', asset)
|
|
373
|
+
break;
|
|
374
|
+
case Urso.types.assets.IMAGE:
|
|
375
|
+
model = this.getInstance('Models.Image', asset)
|
|
376
|
+
break;
|
|
377
|
+
case Urso.types.assets.JSON:
|
|
378
|
+
model = this.getInstance('Models.Json', asset)
|
|
379
|
+
break;
|
|
380
|
+
case Urso.types.assets.JSONATLAS:
|
|
381
|
+
model = this.getInstance('Models.JsonAtlas', asset)
|
|
382
|
+
break;
|
|
383
|
+
case Urso.types.assets.SOUND:
|
|
384
|
+
model = this.getInstance('Models.Sound', asset)
|
|
385
|
+
break;
|
|
386
|
+
case Urso.types.assets.SPINE:
|
|
387
|
+
model = this.getInstance('Models.Spine', asset)
|
|
388
|
+
break;
|
|
389
|
+
default:
|
|
390
|
+
Urso.logger.error('ModulesAssetsService asset type error', asset);
|
|
391
|
+
break;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
//set loadingGroup
|
|
395
|
+
model.loadingGroup = loadingGroup || model.loadingGroup || this.getInstance('Config').loadingGroups.initial;
|
|
396
|
+
|
|
397
|
+
//check if container
|
|
398
|
+
if (model.contents) {
|
|
399
|
+
for (let content of model.contents) {
|
|
400
|
+
this._addAsset(assetsSpace, content, model.loadingGroup);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (model.loadingGroup === this.getInstance('Config').loadingGroups.initial) {
|
|
407
|
+
assetsSpace[model.loadingGroup].push(model);
|
|
408
|
+
} else {
|
|
409
|
+
//add single asset to loading group
|
|
410
|
+
if (!this.assets[model.loadingGroup])
|
|
411
|
+
this.assets[model.loadingGroup] = [];
|
|
412
|
+
|
|
413
|
+
this.assets[model.loadingGroup].push(model);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* start lazy load process
|
|
419
|
+
*/
|
|
420
|
+
_startLazyLoad() {
|
|
421
|
+
if (this.lazyLoadProcessStarted)
|
|
422
|
+
return;
|
|
423
|
+
|
|
424
|
+
this.lazyLoadProcessStarted = true;
|
|
425
|
+
this._continueLazyLoad();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* continue lazy load process (with current step)
|
|
430
|
+
* @param {Number} step
|
|
431
|
+
*/
|
|
432
|
+
_continueLazyLoad(step) {
|
|
433
|
+
if (!step)
|
|
434
|
+
step = 0;
|
|
435
|
+
|
|
436
|
+
const lazyLoadGroups = this.getInstance('Config').lazyLoadGroups;
|
|
437
|
+
|
|
438
|
+
if (step >= lazyLoadGroups.length) {
|
|
439
|
+
this.emit(Urso.events.MODULES_ASSETS_LAZYLOAD_FINISHED);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
let groupName = lazyLoadGroups[step];
|
|
444
|
+
|
|
445
|
+
if (!groupName)
|
|
446
|
+
Urso.logger.error('ModulesAssetsService lazy loading groupName error');
|
|
447
|
+
|
|
448
|
+
this.loadGroup(null, groupName, () => { this._continueLazyLoad(step + 1); })
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* quality reducer
|
|
453
|
+
* @param {Object} qualityFactors
|
|
454
|
+
* @param {Number} widthFactor
|
|
455
|
+
* @returns
|
|
456
|
+
*/
|
|
457
|
+
_qualityReducer(qualityFactors, widthFactor) {
|
|
458
|
+
return [(acc, val) => {
|
|
459
|
+
if (acc === null) {
|
|
460
|
+
return val;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const currentQuality = qualityFactors[acc];
|
|
464
|
+
const qualityFactor = qualityFactors[val];
|
|
465
|
+
|
|
466
|
+
const nextQuality = (currentQuality > qualityFactor && qualityFactor >= widthFactor)
|
|
467
|
+
|| (qualityFactor >= widthFactor && widthFactor > currentQuality)
|
|
468
|
+
|| (widthFactor >= qualityFactor && qualityFactor > currentQuality);
|
|
469
|
+
|
|
470
|
+
return nextQuality ? val : acc;
|
|
471
|
+
|
|
472
|
+
}, null]
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* detect assets quality
|
|
477
|
+
* @returns {Number}
|
|
478
|
+
*/
|
|
479
|
+
_detectQuality() {
|
|
480
|
+
const { qualityFactors } = this.getInstance('Config');
|
|
481
|
+
const userQuality = Urso.helper.parseGetParams()['quality'];
|
|
482
|
+
|
|
483
|
+
if (userQuality && qualityFactors[userQuality]) {
|
|
484
|
+
return userQuality;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
return this._calculateQuality(qualityFactors);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* calculate quality
|
|
492
|
+
* @param {Object} qualityFactors
|
|
493
|
+
*/
|
|
494
|
+
_calculateQuality(qualityFactors) {
|
|
495
|
+
const { android, iOS, iPad, macOS } = Urso.device;
|
|
496
|
+
const isMobile = android || iOS || iPad;
|
|
497
|
+
|
|
498
|
+
if (macOS && !isMobile) {
|
|
499
|
+
return 'high';
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (macOS && iPad) {
|
|
503
|
+
return 'medium';
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const resCfg = Urso.getInstance('Modules.Scenes.ResolutionsConfig').contents[0];
|
|
507
|
+
|
|
508
|
+
const { devicePixelRatio } = window;
|
|
509
|
+
let { width, height } = screen;
|
|
510
|
+
|
|
511
|
+
if (isMobile) {
|
|
512
|
+
width = (width > height) ? width : height;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (iOS) {
|
|
516
|
+
width *= devicePixelRatio;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const widthFactor = width / resCfg.width;
|
|
520
|
+
|
|
521
|
+
return Object
|
|
522
|
+
.keys(qualityFactors)
|
|
523
|
+
.reduce(...this._qualityReducer(qualityFactors, widthFactor));
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* add asset to loader instance
|
|
528
|
+
* @param {Object} assetModel
|
|
529
|
+
* @param {Object} loader
|
|
530
|
+
*/
|
|
531
|
+
_addAssetToLoader(assetModel, loader) {
|
|
532
|
+
if (assetModel.path)
|
|
533
|
+
loader.addAsset(assetModel);
|
|
534
|
+
else if (assetModel.contents) {
|
|
535
|
+
//do nothing, its a container
|
|
536
|
+
} else
|
|
537
|
+
Urso.logger.error('ModulesAssetsService model error', assetModel);
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
module.exports = ModulesAssetsService;
|