@urso/core 0.1.95 → 0.2.3-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.
Files changed (144) hide show
  1. package/.babelrc +11 -11
  2. package/README.md +10 -10
  3. package/build/js/index.js +1 -1
  4. package/build/js/index.js.LICENSE.txt +33 -33
  5. package/package.json +49 -49
  6. package/src/js/app.js +76 -76
  7. package/src/js/components/_info.js +8 -8
  8. package/src/js/components/base/_info.js +3 -3
  9. package/src/js/components/base/controller.js +78 -78
  10. package/src/js/components/debug/_info.js +6 -6
  11. package/src/js/components/debug/controller.js +38 -38
  12. package/src/js/components/debug/coords.js +23 -23
  13. package/src/js/components/debug/fps.js +34 -34
  14. package/src/js/components/debug/template.js +33 -33
  15. package/src/js/components/deviceRotate/_info.js +3 -3
  16. package/src/js/components/deviceRotate/controller.js +86 -86
  17. package/src/js/components/fullscreen/_info.js +6 -6
  18. package/src/js/components/fullscreen/android.js +104 -104
  19. package/src/js/components/fullscreen/controller.js +64 -64
  20. package/src/js/components/fullscreen/desktop.js +49 -49
  21. package/src/js/components/fullscreen/ios.js +111 -111
  22. package/src/js/components/loader/_info.js +4 -4
  23. package/src/js/components/loader/controller.js +66 -66
  24. package/src/js/components/loader/template.js +70 -70
  25. package/src/js/components/soundInitialPopup/_info.js +3 -3
  26. package/src/js/components/soundInitialPopup/controller.js +42 -42
  27. package/src/js/components/soundInitialPopup/template.js +109 -109
  28. package/src/js/components/stateDriven/_info.js +4 -3
  29. package/src/js/components/stateDriven/actionConfig.js +7 -0
  30. package/src/js/components/stateDriven/controller.js +96 -52
  31. package/src/js/config/load.js +5 -5
  32. package/src/js/config/main.js +9 -9
  33. package/src/js/extra/_info.js +22 -22
  34. package/src/js/extra/browserEvents.js +32 -32
  35. package/src/js/index.js +8 -8
  36. package/src/js/lib/_info.js +13 -13
  37. package/src/js/lib/cache.js +98 -98
  38. package/src/js/lib/composition.js +89 -89
  39. package/src/js/lib/device.js +1276 -1276
  40. package/src/js/lib/helper.js +513 -513
  41. package/src/js/lib/loader.js +121 -121
  42. package/src/js/lib/localData.js +15 -15
  43. package/src/js/lib/logger.js +22 -22
  44. package/src/js/lib/math.js +20 -20
  45. package/src/js/lib/objectPool.js +54 -54
  46. package/src/js/lib/time.js +18 -18
  47. package/src/js/lib/tween.js +147 -147
  48. package/src/js/modules/_info.js +11 -11
  49. package/src/js/modules/assets/_info.js +7 -7
  50. package/src/js/modules/assets/baseModel.js +18 -18
  51. package/src/js/modules/assets/config.js +35 -35
  52. package/src/js/modules/assets/controller.js +37 -37
  53. package/src/js/modules/assets/models/_info.js +12 -12
  54. package/src/js/modules/assets/models/atlas.js +8 -8
  55. package/src/js/modules/assets/models/audiosprite.js +27 -27
  56. package/src/js/modules/assets/models/bitmapFont.js +8 -8
  57. package/src/js/modules/assets/models/container.js +16 -16
  58. package/src/js/modules/assets/models/dragonBones.js +26 -26
  59. package/src/js/modules/assets/models/font.js +8 -8
  60. package/src/js/modules/assets/models/image.js +14 -14
  61. package/src/js/modules/assets/models/json.js +8 -8
  62. package/src/js/modules/assets/models/sound.js +14 -14
  63. package/src/js/modules/assets/models/spine.js +8 -8
  64. package/src/js/modules/assets/service.js +330 -330
  65. package/src/js/modules/instances/_info.js +3 -3
  66. package/src/js/modules/instances/controller.js +218 -218
  67. package/src/js/modules/logic/_info.js +5 -5
  68. package/src/js/modules/logic/buttons.js +120 -120
  69. package/src/js/modules/logic/config/_info.js +3 -3
  70. package/src/js/modules/logic/config/buttons.js +63 -63
  71. package/src/js/modules/logic/config/sounds.js +23 -23
  72. package/src/js/modules/logic/controller.js +48 -48
  73. package/src/js/modules/logic/sounds.js +96 -96
  74. package/src/js/modules/objects/_info.js +11 -11
  75. package/src/js/modules/objects/baseModel.js +161 -161
  76. package/src/js/modules/objects/cache.js +99 -99
  77. package/src/js/modules/objects/controller.js +115 -115
  78. package/src/js/modules/objects/create.js +231 -231
  79. package/src/js/modules/objects/find.js +140 -140
  80. package/src/js/modules/objects/models/_info.js +20 -20
  81. package/src/js/modules/objects/models/atlasImage.js +50 -50
  82. package/src/js/modules/objects/models/bitmapText.js +23 -23
  83. package/src/js/modules/objects/models/button.js +184 -184
  84. package/src/js/modules/objects/models/buttonComposite.js +35 -35
  85. package/src/js/modules/objects/models/collection.js +54 -54
  86. package/src/js/modules/objects/models/component.js +44 -44
  87. package/src/js/modules/objects/models/container.js +19 -19
  88. package/src/js/modules/objects/models/dragonBones.js +250 -250
  89. package/src/js/modules/objects/models/emitter.js +54 -54
  90. package/src/js/modules/objects/models/graphics.js +38 -38
  91. package/src/js/modules/objects/models/group.js +19 -19
  92. package/src/js/modules/objects/models/hitArea.js +104 -104
  93. package/src/js/modules/objects/models/image.js +34 -34
  94. package/src/js/modules/objects/models/imagesAnimation.js +113 -113
  95. package/src/js/modules/objects/models/mask.js +38 -38
  96. package/src/js/modules/objects/models/spine.js +83 -83
  97. package/src/js/modules/objects/models/text.js +38 -38
  98. package/src/js/modules/objects/models/world.js +19 -19
  99. package/src/js/modules/objects/propertyAdapter.js +416 -416
  100. package/src/js/modules/objects/proxy.js +179 -179
  101. package/src/js/modules/objects/styles.js +116 -116
  102. package/src/js/modules/observer/_info.js +4 -4
  103. package/src/js/modules/observer/controller.js +99 -99
  104. package/src/js/modules/observer/events.js +28 -28
  105. package/src/js/modules/scenes/_info.js +8 -8
  106. package/src/js/modules/scenes/controller.js +34 -34
  107. package/src/js/modules/scenes/model.js +35 -35
  108. package/src/js/modules/scenes/pixiWrapper.js +194 -194
  109. package/src/js/modules/scenes/resolutions.js +161 -161
  110. package/src/js/modules/scenes/resolutionsConfig.js +73 -73
  111. package/src/js/modules/scenes/service.js +84 -84
  112. package/src/js/modules/soundManager/_info.js +3 -3
  113. package/src/js/modules/soundManager/controller.js +99 -99
  114. package/src/js/modules/soundManager/soundSprite.js +202 -202
  115. package/src/js/modules/statesManager/_info.js +12 -12
  116. package/src/js/modules/statesManager/action.js +55 -113
  117. package/src/js/modules/statesManager/actions/_info.js +3 -3
  118. package/src/js/modules/statesManager/all.js +23 -23
  119. package/src/js/modules/statesManager/configStates.js +71 -86
  120. package/src/js/modules/statesManager/controller.js +132 -91
  121. package/src/js/modules/statesManager/functionsStorage.js +83 -0
  122. package/src/js/modules/statesManager/helper.js +27 -37
  123. package/src/js/modules/statesManager/race.js +75 -80
  124. package/src/js/modules/statesManager/sequence.js +47 -47
  125. package/src/js/modules/template/_info.js +6 -6
  126. package/src/js/modules/template/controller.js +28 -28
  127. package/src/js/modules/template/model.js +11 -11
  128. package/src/js/modules/template/service.js +125 -125
  129. package/src/js/modules/template/types.js +41 -41
  130. package/src/js/modules/transport/_info.js +8 -8
  131. package/src/js/modules/transport/baseConnectionType.js +24 -24
  132. package/src/js/modules/transport/config.js +22 -22
  133. package/src/js/modules/transport/connectionTypes/_info.js +3 -3
  134. package/src/js/modules/transport/connectionTypes/websocket.js +74 -74
  135. package/src/js/modules/transport/connectionTypes/xhr.js +41 -41
  136. package/src/js/modules/transport/controller.js +48 -48
  137. package/src/js/modules/transport/decorator.js +16 -16
  138. package/src/js/modules/transport/service.js +138 -138
  139. package/src/js/templates/_info.js +4 -4
  140. package/src/js/templates/groups/_info.js +1 -1
  141. package/src/js/templates/scenes/_info.js +1 -1
  142. package/webpack.config.js +47 -47
  143. package/src/js/modules/statesManager/actionModel.js +0 -48
  144. package/src/js/modules/statesManager/configActions.js +0 -54
@@ -1,417 +1,417 @@
1
- class PropertyAdapter {
2
-
3
- constructor() {
4
- this.singleton = true;
5
-
6
- this._dependencies = {
7
- 'x': this._updateHorizontal.bind(this),
8
- 'y': this._updateVertical.bind(this),
9
- 'anchorX': this._updateHorizontal.bind(this),
10
- 'anchorY': this._updateVertical.bind(this),
11
- 'scaleX': this._adaptScaleX.bind(this),
12
- 'scaleY': this._adaptScaleY.bind(this),
13
- 'alignX': this._updateHorizontal.bind(this),
14
- 'alignY': this._updateVertical.bind(this),
15
- 'width': this._updateHorizontal.bind(this),
16
- 'height': this._updateVertical.bind(this),
17
- 'angle': this._updateAngle.bind(this),
18
- 'stretchingType': this.adaptStretchingType.bind(this), //todo check on parent change
19
- 'parent': this.parentChangeHandler.bind(this)
20
- };
21
-
22
- this._parentToChildDependencies = {
23
- 'width': { children: ['width'] },
24
- 'height': { children: ['height'] },
25
- 'anchorX': { children: ['x'] },
26
- 'anchorY': { children: ['y'] },
27
- 'stretchingType': { children: ['width', 'height'] }
28
- };
29
-
30
- this._parentTypes = [
31
- Urso.types.objects.COMPONENT,
32
- Urso.types.objects.CONTAINER,
33
- Urso.types.objects.GROUP,
34
- Urso.types.objects.WORLD
35
- ];
36
-
37
- this._typesWithoutAnchor = [
38
- Urso.types.objects.EMITTER,
39
- Urso.types.objects.GRAPHICS,
40
- Urso.types.objects.HITAREA,
41
- Urso.types.objects.MASK,
42
- Urso.types.objects.SPINE,
43
- Urso.types.objects.WORLD
44
- ];
45
- }
46
-
47
- isAdaptiveProperty(property) {
48
- return Object.keys(this._dependencies).includes(property);
49
- };
50
-
51
- propertyChangeHandler(object, propertyName) {
52
- this._adaptProperty(object, propertyName);
53
- this._adaptChildProperties(object, propertyName);
54
- }
55
-
56
- _adaptProperty(object, propertyName) {
57
- if (this._dependencies[propertyName])
58
- this._dependencies[propertyName](object);
59
- }
60
-
61
- _updateHorizontal(object) {
62
- let x = this._getXAsNumber(object); //adaptX
63
- x += this.adaptAnchorX(object);
64
- x += this.adaptAlignX(object);
65
- object._baseObject.x = x;
66
-
67
- this.adaptWidth(object);
68
- }
69
-
70
- _updateAngle(object) {
71
- object._baseObject.angle = object.angle;
72
-
73
- if (!this._canBeParent(object)) {
74
- return;
75
- }
76
-
77
- this._updateHorizontal(object);
78
- this._updateVertical(object);
79
- }
80
-
81
- _updateVertical(object) {
82
- let y = this._getYAsNumber(object); //adaptX
83
- y += this.adaptAnchorY(object);
84
- y += this.adaptAlignY(object);
85
- object._baseObject.y = y;
86
-
87
- this.adaptHeight(object);
88
- }
89
-
90
- _setPropertyWithoutAdaption(object, propertyName, value) { //in error case
91
- object[propertyName] = value;
92
- }
93
-
94
- _adaptChildProperties(object, propertyName) {
95
- const { children } = this._parentToChildDependencies[propertyName] || {};
96
- const objectHasChildren = this._canBeParent(object) && object.hasOwnProperty('contents');
97
-
98
- if (!children || !objectHasChildren)
99
- return;
100
-
101
- for (let dependency of children)
102
- for (let child of object.contents)
103
- this.propertyChangeHandler(child, dependency);
104
- }
105
-
106
- parentChangeHandler(child) {
107
- if (child.parent == null)
108
- return;
109
-
110
- for (let propertyName of this._propertiesDependentOnParent())
111
- this.propertyChangeHandler(child, propertyName);
112
- }
113
-
114
- _propertiesDependentOnParent() {
115
- const properties = [];
116
- for (let propertyName of Object.keys(this._parentToChildDependencies)) {
117
- const { children } = this._parentToChildDependencies[propertyName];
118
-
119
- if (children)
120
- for (let dependency of children)
121
- properties.push(dependency);
122
- }
123
-
124
- return properties;
125
- }
126
-
127
- adaptAnchorX(object) {
128
- const pixiObject = object._baseObject;
129
-
130
- if (typeof object.anchorX !== 'number' || object.anchorX < 0 || object.anchorX > 1)
131
- Urso.logger.error('AnchorX value is not valid!', object);
132
-
133
- if (this._canBeParent(object)) {
134
- if (object.anchorY === 0)
135
- return 0;
136
-
137
- if (object.angle) {
138
- return this._getAnchorOffsetByAngle(object, 'x');
139
- }
140
-
141
- const objectWidth = this._getWidthAsNumber(object);
142
- return - objectWidth * object.anchorX;
143
- } else if (!this._typesWithoutAnchor.includes(object.type)) {
144
- pixiObject.anchor.x = object.anchorX;
145
- } else {
146
- Urso.logger.warn(); ('AnchorX value cannot be used with this object type !', object);
147
- }
148
-
149
- return 0;
150
- }
151
-
152
- adaptAnchorY(object) {
153
- const pixiObject = object._baseObject;
154
-
155
- if (typeof object.anchorY !== 'number' || object.anchorY < 0 || object.anchorY > 1)
156
- Urso.logger.error('AnchorY value is not valid!', object);
157
-
158
- if (this._canBeParent(object)) {
159
- if (object.anchorY === 0)
160
- return 0;
161
-
162
- if (object.angle) {
163
- return this._getAnchorOffsetByAngle(object, 'y');
164
- }
165
-
166
- const objectHeight = this._getHeightAsNumber(object);
167
- return - objectHeight * object.anchorY;
168
- } else if (!this._typesWithoutAnchor.includes(object.type)) {
169
- pixiObject.anchor.y = object.anchorY;
170
- } else {
171
- Urso.logger.warn(); ('AnchorY value cannot be used with this object type !', object);
172
- }
173
-
174
- return 0;
175
- }
176
-
177
- _getAnchorOffsetByAngle(object, side) { //side can be x or y
178
- const objectWidth = this._getWidthAsNumber(object);
179
- const objectHeight = this._getHeightAsNumber(object);
180
- const xCatet = (objectWidth * object.anchorX);
181
- const yCatet = (objectHeight * object.anchorY);
182
- const offsetRadius = Math.sqrt(Math.pow(xCatet, 2) + Math.pow(yCatet, 2));
183
- const angleRadian = Math.atan(xCatet / yCatet); //todo or yCatet/xCatet ?
184
- const angle = Urso.helper.getAngle(angleRadian);
185
- const offsetAngle = object.angle + angle;
186
- const offsetFunction = side === 'x' ? 'cos' : 'sin';
187
- const angleOffset = - offsetRadius * Math[offsetFunction](Urso.helper.getRadian(offsetAngle));
188
-
189
- return angleOffset;
190
- }
191
-
192
- _adaptScaleX(object) {
193
- const pixiObject = object._baseObject;
194
-
195
- if (object.scaleX !== 1 && typeof object.width !== 'boolean') {
196
- Urso.logger.error('ScaleX value cannot be set. Width already used!!');
197
- this._setPropertyWithoutAdaption(object, 'scaleX', 1);
198
- return;
199
- }
200
-
201
- if (typeof object.scaleX === 'number')
202
- pixiObject.scale.x = object.scaleX;
203
- else
204
- Urso.logger.error('ScaleX value is not valid!');
205
- }
206
-
207
- _adaptScaleY(object) {
208
- const pixiObject = object._baseObject;
209
-
210
- if (object.scaleY !== 1 && typeof object.height !== 'boolean') {
211
- Urso.logger.error('ScaleY value cannot be set. Height already used!!');
212
- this._setPropertyWithoutAdaption(object, 'scaleY', 1);
213
- return;
214
- }
215
-
216
- if (typeof object.scaleY === 'number' && object.scaleY >= 0) // TODO: CHECK SCALE CAN BE NEGATIVE
217
- pixiObject.scale.y = object.scaleY;
218
- else
219
- Urso.logger.error('ScaleY value is not valid!');
220
- }
221
-
222
- adaptAlignX(object) {
223
- if (typeof object.alignX !== 'string') {
224
- Urso.logger.error('AlignX value is not string!');
225
- return 0;
226
- }
227
-
228
- const parentWidth = object.parent ? this._getWidthAsNumber(object.parent) : 0;
229
-
230
- switch (object.alignX) {
231
- case 'left':
232
- return 0;
233
- case 'right':
234
- return parentWidth;
235
- case 'center':
236
- return parentWidth / 2;
237
- default:
238
- Urso.logger.error('AlignX string is not valid!');
239
- return 0;
240
- }
241
- }
242
-
243
- adaptAlignY(object) {
244
- if (typeof object.alignY !== 'string') {
245
- Urso.logger.error('AlignY value is not string!');
246
- return 0;
247
- }
248
-
249
- const parentHeight = object.parent ? this._getHeightAsNumber(object.parent) : 0;
250
-
251
- switch (object.alignY) {
252
- case 'top':
253
- return 0;
254
- case 'bottom':
255
- return parentHeight;
256
- case 'center':
257
- return parentHeight / 2;
258
- default:
259
- Urso.logger.error('AlignY string is not valid!');
260
- return 0;
261
- }
262
- }
263
-
264
- adaptWidth(object) {
265
- const pixiObject = object._baseObject;
266
-
267
- if (typeof object.width !== 'boolean' && object.scaleX !== 1) {
268
- Urso.logger.error('Width value cannot be set. ScaleX already used!!', object);
269
- this._setPropertyWithoutAdaption(object, 'width', false);
270
- return;
271
- }
272
-
273
- if (!object.width)
274
- return;
275
-
276
- if (!this._isValueANumberOrPercentsString(object.width))
277
- return Urso.logger.error('Width value is not valid!!');
278
-
279
- if (!this._canBeParent(object))
280
- pixiObject.width = this._getWidthAsNumber(object);
281
- }
282
-
283
- adaptHeight(object) {
284
- const pixiObject = object._baseObject;
285
-
286
- if (typeof object.height !== 'boolean' && object.scaleY !== 1) {
287
- Urso.logger.error('Height value cannot be set. ScaleY already used!!', object);
288
- this._setPropertyWithoutAdaption(object, 'height', false);
289
- return;
290
- }
291
-
292
- if (!object.height)
293
- return;
294
-
295
- if (!this._isValueANumberOrPercentsString(object.height))
296
- return Urso.logger.error('Height value not valid!');
297
-
298
- if (!this._canBeParent(object))
299
- pixiObject.height = this._getHeightAsNumber(object);
300
- }
301
-
302
- _getXAsNumber(object) {
303
- return this._getPropertyAsNumber(object, 'x', 'width');
304
- }
305
-
306
- _getYAsNumber(object) {
307
- return this._getPropertyAsNumber(object, 'y', 'height');
308
- }
309
-
310
- _getWidthAsNumber(object) {
311
- return this._getPropertyAsNumber(object, 'width', 'width');
312
- }
313
-
314
- _getHeightAsNumber(object) {
315
- return this._getPropertyAsNumber(object, 'height', 'height');
316
- }
317
-
318
- //x, y, width or height
319
- _getPropertyAsNumber(object, propertyName, parentPropertyName) {
320
- const propType = typeof object[propertyName];
321
-
322
- switch (propType) {
323
- case 'number':
324
- return object[propertyName];
325
-
326
- case 'string':
327
- const parentValue = this._getPropertyAsNumber(object.parent, parentPropertyName, parentPropertyName);
328
- return this._getRoundedPercentageOfNumber(object[propertyName], parentValue);
329
-
330
- case 'boolean':
331
- return this._getPropertyAsNumber(object.parent, propertyName, parentPropertyName)
332
-
333
- default:
334
- Urso.logger.error('Property value not number or string!', object, propertyName);
335
- return;
336
- }
337
- }
338
-
339
- _getRoundedPercentageOfNumber(percentsString, number) {
340
- const percentsFloat = parseFloat(percentsString);
341
- return ~~(percentsFloat * number / 100);
342
- }
343
-
344
- _canBeParent(object) {
345
- return this._parentTypes.includes(object.type);
346
- }
347
-
348
- _isValueANumberOrPercentsString(value) {
349
- return typeof value === 'number' || (typeof value === 'string' && value.endsWith('%'))
350
- }
351
-
352
- //stretchingType
353
- adaptStretchingType(object) {
354
- if (object.width !== '100%' || object.height !== '100%' || !object.stretchingType)
355
- return;
356
-
357
- switch (object.stretchingType) {
358
- case 'inscribed':
359
- this._inscribe(object);
360
- break;
361
-
362
- case 'circumscribed':
363
- this._circumscribe(object);
364
- break;
365
-
366
- case 'false':
367
- break;
368
-
369
- default:
370
- Urso.logger.error('StretchingType value not valid!');
371
- break;
372
- }
373
- }
374
-
375
- _setPropertyAndAdaptIt(object, propertyName, value) {
376
- object[propertyName] = value;
377
- this.propertyChangeHandler(object, propertyName);
378
- }
379
-
380
- _setStreching(object, { scale, objectWidth, objectHeight }) {
381
- if (object.scaleX === 1)
382
- this._setPropertyAndAdaptIt(object, 'width', objectWidth * scale);
383
- else
384
- this._setPropertyAndAdaptIt(object, 'scaleX', scale);
385
-
386
- if (object.scaleY === 1)
387
- this._setPropertyAndAdaptIt(object, 'height', objectHeight * scale);
388
- else
389
- this._setPropertyAndAdaptIt(object, 'scaleY', scale);
390
- }
391
-
392
- _getObjectValuesForStreching(object) {
393
- const objectWidth = this._getWidthAsNumber(object);
394
- const objectHeight = this._getHeightAsNumber(object);
395
- const parentWidth = this._getWidthAsNumber(object.parent);
396
- const parentHeight = this._getHeightAsNumber(object.parent);
397
-
398
- const scaleX = parentWidth / objectWidth;
399
- const scaleY = parentHeight / objectHeight;
400
-
401
- return { objectWidth, objectHeight, scaleX, scaleY };
402
- }
403
-
404
- _inscribe(object) {
405
- const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
406
- const scale = Math.min(scaleX, scaleY);
407
- this._setStreching(object, { scale, objectWidth, objectHeight });
408
- }
409
-
410
- _circumscribe(object) {
411
- const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
412
- const scale = Math.max(scaleX, scaleY);
413
- this._setStreching(object, { scale, objectWidth, objectHeight });
414
- }
415
- }
416
-
1
+ class PropertyAdapter {
2
+
3
+ constructor() {
4
+ this.singleton = true;
5
+
6
+ this._dependencies = {
7
+ 'x': this._updateHorizontal.bind(this),
8
+ 'y': this._updateVertical.bind(this),
9
+ 'anchorX': this._updateHorizontal.bind(this),
10
+ 'anchorY': this._updateVertical.bind(this),
11
+ 'scaleX': this._adaptScaleX.bind(this),
12
+ 'scaleY': this._adaptScaleY.bind(this),
13
+ 'alignX': this._updateHorizontal.bind(this),
14
+ 'alignY': this._updateVertical.bind(this),
15
+ 'width': this._updateHorizontal.bind(this),
16
+ 'height': this._updateVertical.bind(this),
17
+ 'angle': this._updateAngle.bind(this),
18
+ 'stretchingType': this.adaptStretchingType.bind(this), //todo check on parent change
19
+ 'parent': this.parentChangeHandler.bind(this)
20
+ };
21
+
22
+ this._parentToChildDependencies = {
23
+ 'width': { children: ['width'] },
24
+ 'height': { children: ['height'] },
25
+ 'anchorX': { children: ['x'] },
26
+ 'anchorY': { children: ['y'] },
27
+ 'stretchingType': { children: ['width', 'height'] }
28
+ };
29
+
30
+ this._parentTypes = [
31
+ Urso.types.objects.COMPONENT,
32
+ Urso.types.objects.CONTAINER,
33
+ Urso.types.objects.GROUP,
34
+ Urso.types.objects.WORLD
35
+ ];
36
+
37
+ this._typesWithoutAnchor = [
38
+ Urso.types.objects.EMITTER,
39
+ Urso.types.objects.GRAPHICS,
40
+ Urso.types.objects.HITAREA,
41
+ Urso.types.objects.MASK,
42
+ Urso.types.objects.SPINE,
43
+ Urso.types.objects.WORLD
44
+ ];
45
+ }
46
+
47
+ isAdaptiveProperty(property) {
48
+ return Object.keys(this._dependencies).includes(property);
49
+ };
50
+
51
+ propertyChangeHandler(object, propertyName) {
52
+ this._adaptProperty(object, propertyName);
53
+ this._adaptChildProperties(object, propertyName);
54
+ }
55
+
56
+ _adaptProperty(object, propertyName) {
57
+ if (this._dependencies[propertyName])
58
+ this._dependencies[propertyName](object);
59
+ }
60
+
61
+ _updateHorizontal(object) {
62
+ let x = this._getXAsNumber(object); //adaptX
63
+ x += this.adaptAnchorX(object);
64
+ x += this.adaptAlignX(object);
65
+ object._baseObject.x = x;
66
+
67
+ this.adaptWidth(object);
68
+ }
69
+
70
+ _updateAngle(object) {
71
+ object._baseObject.angle = object.angle;
72
+
73
+ if (!this._canBeParent(object)) {
74
+ return;
75
+ }
76
+
77
+ this._updateHorizontal(object);
78
+ this._updateVertical(object);
79
+ }
80
+
81
+ _updateVertical(object) {
82
+ let y = this._getYAsNumber(object); //adaptX
83
+ y += this.adaptAnchorY(object);
84
+ y += this.adaptAlignY(object);
85
+ object._baseObject.y = y;
86
+
87
+ this.adaptHeight(object);
88
+ }
89
+
90
+ _setPropertyWithoutAdaption(object, propertyName, value) { //in error case
91
+ object[propertyName] = value;
92
+ }
93
+
94
+ _adaptChildProperties(object, propertyName) {
95
+ const { children } = this._parentToChildDependencies[propertyName] || {};
96
+ const objectHasChildren = this._canBeParent(object) && object.hasOwnProperty('contents');
97
+
98
+ if (!children || !objectHasChildren)
99
+ return;
100
+
101
+ for (let dependency of children)
102
+ for (let child of object.contents)
103
+ this.propertyChangeHandler(child, dependency);
104
+ }
105
+
106
+ parentChangeHandler(child) {
107
+ if (child.parent == null)
108
+ return;
109
+
110
+ for (let propertyName of this._propertiesDependentOnParent())
111
+ this.propertyChangeHandler(child, propertyName);
112
+ }
113
+
114
+ _propertiesDependentOnParent() {
115
+ const properties = [];
116
+ for (let propertyName of Object.keys(this._parentToChildDependencies)) {
117
+ const { children } = this._parentToChildDependencies[propertyName];
118
+
119
+ if (children)
120
+ for (let dependency of children)
121
+ properties.push(dependency);
122
+ }
123
+
124
+ return properties;
125
+ }
126
+
127
+ adaptAnchorX(object) {
128
+ const pixiObject = object._baseObject;
129
+
130
+ if (typeof object.anchorX !== 'number' || object.anchorX < 0 || object.anchorX > 1)
131
+ Urso.logger.error('AnchorX value is not valid!', object);
132
+
133
+ if (this._canBeParent(object)) {
134
+ if (object.anchorY === 0)
135
+ return 0;
136
+
137
+ if (object.angle) {
138
+ return this._getAnchorOffsetByAngle(object, 'x');
139
+ }
140
+
141
+ const objectWidth = this._getWidthAsNumber(object);
142
+ return - objectWidth * object.anchorX;
143
+ } else if (!this._typesWithoutAnchor.includes(object.type)) {
144
+ pixiObject.anchor.x = object.anchorX;
145
+ } else {
146
+ Urso.logger.warn(); ('AnchorX value cannot be used with this object type !', object);
147
+ }
148
+
149
+ return 0;
150
+ }
151
+
152
+ adaptAnchorY(object) {
153
+ const pixiObject = object._baseObject;
154
+
155
+ if (typeof object.anchorY !== 'number' || object.anchorY < 0 || object.anchorY > 1)
156
+ Urso.logger.error('AnchorY value is not valid!', object);
157
+
158
+ if (this._canBeParent(object)) {
159
+ if (object.anchorY === 0)
160
+ return 0;
161
+
162
+ if (object.angle) {
163
+ return this._getAnchorOffsetByAngle(object, 'y');
164
+ }
165
+
166
+ const objectHeight = this._getHeightAsNumber(object);
167
+ return - objectHeight * object.anchorY;
168
+ } else if (!this._typesWithoutAnchor.includes(object.type)) {
169
+ pixiObject.anchor.y = object.anchorY;
170
+ } else {
171
+ Urso.logger.warn(); ('AnchorY value cannot be used with this object type !', object);
172
+ }
173
+
174
+ return 0;
175
+ }
176
+
177
+ _getAnchorOffsetByAngle(object, side) { //side can be x or y
178
+ const objectWidth = this._getWidthAsNumber(object);
179
+ const objectHeight = this._getHeightAsNumber(object);
180
+ const xCatet = (objectWidth * object.anchorX);
181
+ const yCatet = (objectHeight * object.anchorY);
182
+ const offsetRadius = Math.sqrt(Math.pow(xCatet, 2) + Math.pow(yCatet, 2));
183
+ const angleRadian = Math.atan(xCatet / yCatet); //todo or yCatet/xCatet ?
184
+ const angle = Urso.helper.getAngle(angleRadian);
185
+ const offsetAngle = object.angle + angle;
186
+ const offsetFunction = side === 'x' ? 'cos' : 'sin';
187
+ const angleOffset = - offsetRadius * Math[offsetFunction](Urso.helper.getRadian(offsetAngle));
188
+
189
+ return angleOffset;
190
+ }
191
+
192
+ _adaptScaleX(object) {
193
+ const pixiObject = object._baseObject;
194
+
195
+ if (object.scaleX !== 1 && typeof object.width !== 'boolean') {
196
+ Urso.logger.error('ScaleX value cannot be set. Width already used!!');
197
+ this._setPropertyWithoutAdaption(object, 'scaleX', 1);
198
+ return;
199
+ }
200
+
201
+ if (typeof object.scaleX === 'number')
202
+ pixiObject.scale.x = object.scaleX;
203
+ else
204
+ Urso.logger.error('ScaleX value is not valid!');
205
+ }
206
+
207
+ _adaptScaleY(object) {
208
+ const pixiObject = object._baseObject;
209
+
210
+ if (object.scaleY !== 1 && typeof object.height !== 'boolean') {
211
+ Urso.logger.error('ScaleY value cannot be set. Height already used!!');
212
+ this._setPropertyWithoutAdaption(object, 'scaleY', 1);
213
+ return;
214
+ }
215
+
216
+ if (typeof object.scaleY === 'number' && object.scaleY >= 0) // TODO: CHECK SCALE CAN BE NEGATIVE
217
+ pixiObject.scale.y = object.scaleY;
218
+ else
219
+ Urso.logger.error('ScaleY value is not valid!');
220
+ }
221
+
222
+ adaptAlignX(object) {
223
+ if (typeof object.alignX !== 'string') {
224
+ Urso.logger.error('AlignX value is not string!');
225
+ return 0;
226
+ }
227
+
228
+ const parentWidth = object.parent ? this._getWidthAsNumber(object.parent) : 0;
229
+
230
+ switch (object.alignX) {
231
+ case 'left':
232
+ return 0;
233
+ case 'right':
234
+ return parentWidth;
235
+ case 'center':
236
+ return parentWidth / 2;
237
+ default:
238
+ Urso.logger.error('AlignX string is not valid!');
239
+ return 0;
240
+ }
241
+ }
242
+
243
+ adaptAlignY(object) {
244
+ if (typeof object.alignY !== 'string') {
245
+ Urso.logger.error('AlignY value is not string!');
246
+ return 0;
247
+ }
248
+
249
+ const parentHeight = object.parent ? this._getHeightAsNumber(object.parent) : 0;
250
+
251
+ switch (object.alignY) {
252
+ case 'top':
253
+ return 0;
254
+ case 'bottom':
255
+ return parentHeight;
256
+ case 'center':
257
+ return parentHeight / 2;
258
+ default:
259
+ Urso.logger.error('AlignY string is not valid!');
260
+ return 0;
261
+ }
262
+ }
263
+
264
+ adaptWidth(object) {
265
+ const pixiObject = object._baseObject;
266
+
267
+ if (typeof object.width !== 'boolean' && object.scaleX !== 1) {
268
+ Urso.logger.error('Width value cannot be set. ScaleX already used!!', object);
269
+ this._setPropertyWithoutAdaption(object, 'width', false);
270
+ return;
271
+ }
272
+
273
+ if (!object.width)
274
+ return;
275
+
276
+ if (!this._isValueANumberOrPercentsString(object.width))
277
+ return Urso.logger.error('Width value is not valid!!');
278
+
279
+ if (!this._canBeParent(object))
280
+ pixiObject.width = this._getWidthAsNumber(object);
281
+ }
282
+
283
+ adaptHeight(object) {
284
+ const pixiObject = object._baseObject;
285
+
286
+ if (typeof object.height !== 'boolean' && object.scaleY !== 1) {
287
+ Urso.logger.error('Height value cannot be set. ScaleY already used!!', object);
288
+ this._setPropertyWithoutAdaption(object, 'height', false);
289
+ return;
290
+ }
291
+
292
+ if (!object.height)
293
+ return;
294
+
295
+ if (!this._isValueANumberOrPercentsString(object.height))
296
+ return Urso.logger.error('Height value not valid!');
297
+
298
+ if (!this._canBeParent(object))
299
+ pixiObject.height = this._getHeightAsNumber(object);
300
+ }
301
+
302
+ _getXAsNumber(object) {
303
+ return this._getPropertyAsNumber(object, 'x', 'width');
304
+ }
305
+
306
+ _getYAsNumber(object) {
307
+ return this._getPropertyAsNumber(object, 'y', 'height');
308
+ }
309
+
310
+ _getWidthAsNumber(object) {
311
+ return this._getPropertyAsNumber(object, 'width', 'width');
312
+ }
313
+
314
+ _getHeightAsNumber(object) {
315
+ return this._getPropertyAsNumber(object, 'height', 'height');
316
+ }
317
+
318
+ //x, y, width or height
319
+ _getPropertyAsNumber(object, propertyName, parentPropertyName) {
320
+ const propType = typeof object[propertyName];
321
+
322
+ switch (propType) {
323
+ case 'number':
324
+ return object[propertyName];
325
+
326
+ case 'string':
327
+ const parentValue = this._getPropertyAsNumber(object.parent, parentPropertyName, parentPropertyName);
328
+ return this._getRoundedPercentageOfNumber(object[propertyName], parentValue);
329
+
330
+ case 'boolean':
331
+ return this._getPropertyAsNumber(object.parent, propertyName, parentPropertyName)
332
+
333
+ default:
334
+ Urso.logger.error('Property value not number or string!', object, propertyName);
335
+ return;
336
+ }
337
+ }
338
+
339
+ _getRoundedPercentageOfNumber(percentsString, number) {
340
+ const percentsFloat = parseFloat(percentsString);
341
+ return ~~(percentsFloat * number / 100);
342
+ }
343
+
344
+ _canBeParent(object) {
345
+ return this._parentTypes.includes(object.type);
346
+ }
347
+
348
+ _isValueANumberOrPercentsString(value) {
349
+ return typeof value === 'number' || (typeof value === 'string' && value.endsWith('%'))
350
+ }
351
+
352
+ //stretchingType
353
+ adaptStretchingType(object) {
354
+ if (object.width !== '100%' || object.height !== '100%' || !object.stretchingType)
355
+ return;
356
+
357
+ switch (object.stretchingType) {
358
+ case 'inscribed':
359
+ this._inscribe(object);
360
+ break;
361
+
362
+ case 'circumscribed':
363
+ this._circumscribe(object);
364
+ break;
365
+
366
+ case 'false':
367
+ break;
368
+
369
+ default:
370
+ Urso.logger.error('StretchingType value not valid!');
371
+ break;
372
+ }
373
+ }
374
+
375
+ _setPropertyAndAdaptIt(object, propertyName, value) {
376
+ object[propertyName] = value;
377
+ this.propertyChangeHandler(object, propertyName);
378
+ }
379
+
380
+ _setStreching(object, { scale, objectWidth, objectHeight }) {
381
+ if (object.scaleX === 1)
382
+ this._setPropertyAndAdaptIt(object, 'width', objectWidth * scale);
383
+ else
384
+ this._setPropertyAndAdaptIt(object, 'scaleX', scale);
385
+
386
+ if (object.scaleY === 1)
387
+ this._setPropertyAndAdaptIt(object, 'height', objectHeight * scale);
388
+ else
389
+ this._setPropertyAndAdaptIt(object, 'scaleY', scale);
390
+ }
391
+
392
+ _getObjectValuesForStreching(object) {
393
+ const objectWidth = this._getWidthAsNumber(object);
394
+ const objectHeight = this._getHeightAsNumber(object);
395
+ const parentWidth = this._getWidthAsNumber(object.parent);
396
+ const parentHeight = this._getHeightAsNumber(object.parent);
397
+
398
+ const scaleX = parentWidth / objectWidth;
399
+ const scaleY = parentHeight / objectHeight;
400
+
401
+ return { objectWidth, objectHeight, scaleX, scaleY };
402
+ }
403
+
404
+ _inscribe(object) {
405
+ const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
406
+ const scale = Math.min(scaleX, scaleY);
407
+ this._setStreching(object, { scale, objectWidth, objectHeight });
408
+ }
409
+
410
+ _circumscribe(object) {
411
+ const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
412
+ const scale = Math.max(scaleX, scaleY);
413
+ this._setStreching(object, { scale, objectWidth, objectHeight });
414
+ }
415
+ }
416
+
417
417
  module.exports = PropertyAdapter;