@urso/core 0.5.5 → 0.5.8

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