@urso/core 0.3.9 → 0.4.0

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