@urso/core 0.4.35 → 0.4.36

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