@urso/core 0.5.6 → 0.5.9

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