@urso/core 0.7.89 → 0.7.91

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 (134) hide show
  1. package/.babelrc +11 -11
  2. package/LICENSE +20 -20
  3. package/README.md +227 -227
  4. package/build/js/index.js +1 -1
  5. package/build/js/index.js.LICENSE.txt +70 -0
  6. package/package.json +50 -52
  7. package/src/js/app.js +104 -104
  8. package/src/js/components/base/controller.js +78 -78
  9. package/src/js/components/debug/controller.js +38 -38
  10. package/src/js/components/debug/coords.js +23 -23
  11. package/src/js/components/debug/fps.js +36 -36
  12. package/src/js/components/debug/template.js +55 -55
  13. package/src/js/components/debug/timescale.js +60 -60
  14. package/src/js/components/deviceRotate/controller.js +95 -95
  15. package/src/js/components/editor/api.js +127 -127
  16. package/src/js/components/editor/controller.js +12 -12
  17. package/src/js/components/fullscreen/android.js +104 -104
  18. package/src/js/components/fullscreen/controller.js +76 -76
  19. package/src/js/components/fullscreen/desktop.js +49 -49
  20. package/src/js/components/fullscreen/ios.js +115 -115
  21. package/src/js/components/layersSwitcher/config.js +26 -26
  22. package/src/js/components/layersSwitcher/controller.js +36 -36
  23. package/src/js/components/loader/controller.js +66 -66
  24. package/src/js/components/loader/template.js +70 -70
  25. package/src/js/components/soundInitialPopup/controller.js +42 -42
  26. package/src/js/components/soundInitialPopup/template.js +109 -109
  27. package/src/js/components/stateDriven/controller.js +123 -123
  28. package/src/js/config/load.js +194 -194
  29. package/src/js/config/main.js +17 -17
  30. package/src/js/extra/browserEvents.js +57 -57
  31. package/src/js/extra/main.js +19 -24
  32. package/src/js/extra/pixiPatch.js +171 -171
  33. package/src/js/index.js +7 -7
  34. package/src/js/lib/cache.js +125 -125
  35. package/src/js/lib/composition.js +85 -85
  36. package/src/js/lib/device.js +1215 -1215
  37. package/src/js/lib/helper.js +678 -678
  38. package/src/js/lib/loader.js +211 -211
  39. package/src/js/lib/localData.js +28 -28
  40. package/src/js/lib/logger.js +69 -69
  41. package/src/js/lib/math.js +161 -161
  42. package/src/js/lib/objectPool.js +207 -207
  43. package/src/js/lib/time.js +18 -18
  44. package/src/js/lib/tween.js +152 -152
  45. package/src/js/modules/assets/baseModel.js +20 -20
  46. package/src/js/modules/assets/config.js +38 -38
  47. package/src/js/modules/assets/controller.js +65 -65
  48. package/src/js/modules/assets/models/atlas.js +18 -18
  49. package/src/js/modules/assets/models/audiosprite.js +28 -28
  50. package/src/js/modules/assets/models/bitmapFont.js +10 -10
  51. package/src/js/modules/assets/models/container.js +18 -18
  52. package/src/js/modules/assets/models/font.js +10 -10
  53. package/src/js/modules/assets/models/html.js +10 -10
  54. package/src/js/modules/assets/models/image.js +17 -17
  55. package/src/js/modules/assets/models/json.js +10 -10
  56. package/src/js/modules/assets/models/jsonAtlas.js +10 -10
  57. package/src/js/modules/assets/models/sound.js +16 -16
  58. package/src/js/modules/assets/models/spine.js +16 -16
  59. package/src/js/modules/assets/service.js +541 -541
  60. package/src/js/modules/i18n/config.js +17 -17
  61. package/src/js/modules/i18n/controller.js +71 -71
  62. package/src/js/modules/instances/controller.js +357 -357
  63. package/src/js/modules/logic/config/sounds.js +23 -23
  64. package/src/js/modules/logic/controller.js +52 -52
  65. package/src/js/modules/logic/main.js +8 -8
  66. package/src/js/modules/logic/sounds.js +103 -103
  67. package/src/js/modules/objects/baseModel.js +207 -207
  68. package/src/js/modules/objects/cache.js +99 -99
  69. package/src/js/modules/objects/config.js +9 -9
  70. package/src/js/modules/objects/controller.js +139 -139
  71. package/src/js/modules/objects/find.js +58 -58
  72. package/src/js/modules/objects/models/atlasImage.js +52 -52
  73. package/src/js/modules/objects/models/bitmapText.js +48 -48
  74. package/src/js/modules/objects/models/button.js +196 -196
  75. package/src/js/modules/objects/models/buttonComposite.js +37 -37
  76. package/src/js/modules/objects/models/checkbox.js +96 -96
  77. package/src/js/modules/objects/models/collection.js +54 -54
  78. package/src/js/modules/objects/models/component.js +48 -48
  79. package/src/js/modules/objects/models/container.js +21 -21
  80. package/src/js/modules/objects/models/dragContainer.js +664 -664
  81. package/src/js/modules/objects/models/emitter.js +69 -69
  82. package/src/js/modules/objects/models/emitterFx.js +114 -114
  83. package/src/js/modules/objects/models/graphics.js +40 -40
  84. package/src/js/modules/objects/models/group.js +21 -21
  85. package/src/js/modules/objects/models/hitArea.js +119 -119
  86. package/src/js/modules/objects/models/image.js +36 -36
  87. package/src/js/modules/objects/models/imagesAnimation.js +115 -115
  88. package/src/js/modules/objects/models/mask.js +40 -40
  89. package/src/js/modules/objects/models/nineSlicePlane.js +32 -32
  90. package/src/js/modules/objects/models/scrollbox.js +66 -66
  91. package/src/js/modules/objects/models/slider.js +352 -352
  92. package/src/js/modules/objects/models/spine.js +338 -338
  93. package/src/js/modules/objects/models/text.js +70 -70
  94. package/src/js/modules/objects/models/textInput.js +68 -68
  95. package/src/js/modules/objects/models/toggle.js +179 -179
  96. package/src/js/modules/objects/models/world.js +21 -21
  97. package/src/js/modules/objects/pool.js +68 -68
  98. package/src/js/modules/objects/propertyAdapter.js +588 -588
  99. package/src/js/modules/objects/proxy.js +298 -298
  100. package/src/js/modules/objects/selector.js +136 -136
  101. package/src/js/modules/objects/service.js +254 -254
  102. package/src/js/modules/objects/styles.js +210 -210
  103. package/src/js/modules/observer/controller.js +167 -167
  104. package/src/js/modules/observer/events.js +56 -56
  105. package/src/js/modules/scenes/controller.js +127 -127
  106. package/src/js/modules/scenes/model.js +28 -28
  107. package/src/js/modules/scenes/pixiWrapper.js +351 -351
  108. package/src/js/modules/scenes/resolutions.js +173 -173
  109. package/src/js/modules/scenes/resolutionsConfig.js +73 -73
  110. package/src/js/modules/scenes/service.js +146 -146
  111. package/src/js/modules/soundManager/controller.js +103 -103
  112. package/src/js/modules/soundManager/soundSprite.js +285 -285
  113. package/src/js/modules/statesManager/action.js +97 -97
  114. package/src/js/modules/statesManager/all.js +23 -23
  115. package/src/js/modules/statesManager/configStates.js +77 -77
  116. package/src/js/modules/statesManager/controller.js +218 -218
  117. package/src/js/modules/statesManager/functionsStorage.js +82 -82
  118. package/src/js/modules/statesManager/helper.js +27 -27
  119. package/src/js/modules/statesManager/race.js +91 -91
  120. package/src/js/modules/statesManager/sequence.js +48 -48
  121. package/src/js/modules/template/controller.js +28 -28
  122. package/src/js/modules/template/model.js +11 -11
  123. package/src/js/modules/template/service.js +137 -137
  124. package/src/js/modules/template/types.js +48 -48
  125. package/src/js/modules/transport/baseConnectionType.js +25 -25
  126. package/src/js/modules/transport/config.js +13 -13
  127. package/src/js/modules/transport/connectionTypes/websocket.js +76 -76
  128. package/src/js/modules/transport/connectionTypes/xhr.js +46 -46
  129. package/src/js/modules/transport/controller.js +48 -48
  130. package/src/js/modules/transport/decorator.js +17 -17
  131. package/src/js/modules/transport/service.js +150 -150
  132. package/webpack.config.js +47 -47
  133. package/build/162.js/index.js +0 -1
  134. package/build/24.js/index.js +0 -1
@@ -1,589 +1,589 @@
1
- /**
2
- * adapter Urso to Pixi objects
3
- */
4
- class PropertyAdapter {
5
-
6
- constructor() {
7
- this.singleton = true;
8
-
9
- /**
10
- * dependencies, functions to handle changing property
11
- */
12
- this._dependencies = {
13
- 'x': this._updateHorizontal.bind(this),
14
- 'y': this._updateVertical.bind(this),
15
- 'anchorX': this._updateHorizontal.bind(this),
16
- 'anchorY': this._updateVertical.bind(this),
17
- 'scaleX': this._adaptScaleX.bind(this),
18
- 'scaleY': this._adaptScaleY.bind(this),
19
- 'alignX': this._updateHorizontal.bind(this),
20
- 'alignY': this._updateVertical.bind(this),
21
- 'width': this._updateHorizontal.bind(this),
22
- 'height': this._updateVertical.bind(this),
23
- 'angle': this._updateAngle.bind(this),
24
- 'stretchingType': this._adaptStretchingType.bind(this), //todo check on parent change
25
- 'parent': this._parentChangeHandler.bind(this)
26
- };
27
-
28
- /**
29
- * functions to handle changing parent property
30
- */
31
- this._parentToChildDependencies = {
32
- 'width': { children: ['width'] },
33
- 'height': { children: ['height'] },
34
- 'anchorX': { children: ['x'] },
35
- 'anchorY': { children: ['y'] },
36
- 'stretchingType': { children: ['width', 'height'] }
37
- };
38
-
39
- /**
40
- * parent types list
41
- */
42
- this._parentTypes = [
43
- Urso.types.objects.COMPONENT,
44
- Urso.types.objects.CONTAINER,
45
- Urso.types.objects.DRAGCONTAINER,
46
- Urso.types.objects.GROUP,
47
- Urso.types.objects.SCROLLBOX,
48
- Urso.types.objects.SLIDER,
49
- Urso.types.objects.SPINE,
50
- Urso.types.objects.WORLD
51
- ];
52
-
53
- /**
54
- * types without ahchor list
55
- */
56
- this._typesWithoutAnchor = [
57
- Urso.types.objects.CHECKBOX,
58
- Urso.types.objects.EMITTER,
59
- Urso.types.objects.EMITTERFX,
60
- Urso.types.objects.GRAPHICS,
61
- Urso.types.objects.HITAREA,
62
- Urso.types.objects.MASK,
63
- Urso.types.objects.NINESLICEPLANE,
64
- Urso.types.objects.SLIDER,
65
- Urso.types.objects.SPINE,
66
- Urso.types.objects.TEXTINPUT,
67
- Urso.types.objects.WORLD
68
- ];
69
- }
70
-
71
- /**
72
- * check is adaptive property
73
- * @param {String} property
74
- * @returns {Boolean}
75
- */
76
- isAdaptiveProperty(property) {
77
- return Object.keys(this._dependencies).includes(property);
78
- };
79
-
80
- /**
81
- * property change handler (main function)
82
- * @param {Object} object
83
- * @param {String} propertyName
84
- */
85
- propertyChangeHandler(object, propertyName) {
86
- this._adaptProperty(object, propertyName);
87
- this._adaptChildProperties(object, propertyName);
88
- }
89
-
90
- /**
91
- * adapt property (launch handler changing property)
92
- * @param {Object} object
93
- * @param {String} propertyName
94
- */
95
- _adaptProperty(object, propertyName) {
96
- if (this._dependencies[propertyName])
97
- this._dependencies[propertyName](object);
98
- }
99
-
100
- /**
101
- * main function to calculate x and width
102
- * @param {Object} object
103
- */
104
- _updateHorizontal(object) {
105
- let x = this._getXAsNumber(object);
106
- x += this._adaptAnchorX(object);
107
- x += this._adaptAlignX(object);
108
- object._baseObject.x = x;
109
-
110
- this._adaptWidth(object);
111
- }
112
-
113
- /**
114
- * main function to calculate angle
115
- * @param {Object} object
116
- */
117
- _updateAngle(object) {
118
- object._baseObject.angle = object.angle;
119
-
120
- if (!this._canBeParent(object)) {
121
- return;
122
- }
123
-
124
- this._updateHorizontal(object);
125
- this._updateVertical(object);
126
- }
127
-
128
- /**
129
- * main function to calculate y and height
130
- * @param {Object} object
131
- */
132
- _updateVertical(object) {
133
- let y = this._getYAsNumber(object);
134
- y += this._adaptAnchorY(object);
135
- y += this._adaptAlignY(object);
136
- object._baseObject.y = y;
137
-
138
- this._adaptHeight(object);
139
- }
140
-
141
- /**
142
- * set property value without adaptation
143
- * @param {Object} object
144
- * @param {String} propertyName
145
- * @param {mixed} value
146
- */
147
- _setPropertyWithoutAdaption(object, propertyName, value) { //in error case
148
- object[propertyName] = value;
149
- }
150
-
151
- /**
152
- * adapt child properties if parent properties was changed
153
- * @param {Object} object
154
- * @param {String} propertyName
155
- */
156
- _adaptChildProperties(object, propertyName) {
157
- const { children } = this._parentToChildDependencies[propertyName] || {};
158
- const objectHasChildren = this._canBeParent(object) && object.hasOwnProperty('contents');
159
-
160
- if (!children || !objectHasChildren)
161
- return;
162
-
163
- for (let dependency of children)
164
- for (let child of object.contents)
165
- this.propertyChangeHandler(child, dependency);
166
- }
167
-
168
- /**
169
- * main handler when new parent was setted
170
- * @param {Object} child
171
- */
172
- _parentChangeHandler(child) {
173
- if (child.parent == null)
174
- return;
175
-
176
- for (let propertyName of this._propertiesDependentOnParent())
177
- this.propertyChangeHandler(child, propertyName);
178
- }
179
-
180
- /**
181
- * get array of properties dependent on parent
182
- * //todo cache them and get just one time
183
- * @returns {Array}
184
- */
185
- _propertiesDependentOnParent() {
186
- const properties = [];
187
-
188
- for (let propertyName of Object.keys(this._parentToChildDependencies)) {
189
- const { children } = this._parentToChildDependencies[propertyName];
190
-
191
- if (children)
192
- for (let dependency of children)
193
- properties.push(dependency);
194
- }
195
-
196
- return properties;
197
- }
198
-
199
- /**
200
- * adapt anchorX
201
- * @param {Object} object
202
- */
203
- _adaptAnchorX(object) {
204
- if (typeof object.anchorX !== 'number' || object.anchorX < 0 || object.anchorX > 1)
205
- Urso.logger.error('AnchorX value is not valid!', object);
206
-
207
- if (this._canBeParent(object) && !this._typesWithoutAnchor.includes(object.type)) { //parent types
208
- if (object.anchorX === 0)
209
- return 0;
210
-
211
- if (object.angle) {
212
- return this._getAnchorOffsetByAngle(object, 'x');
213
- }
214
-
215
- const objectWidth = this._getWidthAsNumber(object);
216
- return - objectWidth * object.anchorX;
217
- } else if (!this._typesWithoutAnchor.includes(object.type)) { //regular type and not a type without anchor
218
- const pixiObject = object._baseObject;
219
- pixiObject.anchor.x = object.anchorX;
220
- } else {
221
- Urso.logger.warn(); ('AnchorX value cannot be used with this object type !', object);
222
- }
223
-
224
- return 0;
225
- }
226
-
227
- /**
228
- * adapt anchorY
229
- * @param {Object} object
230
- */
231
- _adaptAnchorY(object) {
232
- if (typeof object.anchorY !== 'number' || object.anchorY < 0 || object.anchorY > 1)
233
- Urso.logger.error('AnchorY value is not valid!', object);
234
-
235
- if (this._canBeParent(object) && !this._typesWithoutAnchor.includes(object.type)) { //parent types
236
- if (object.anchorY === 0)
237
- return 0;
238
-
239
- if (object.angle) {
240
- return this._getAnchorOffsetByAngle(object, 'y');
241
- }
242
-
243
- const objectHeight = this._getHeightAsNumber(object);
244
- return - objectHeight * object.anchorY;
245
- } else if (!this._typesWithoutAnchor.includes(object.type)) { //regular type and not a type without anchor
246
- const pixiObject = object._baseObject;
247
- pixiObject.anchor.y = object.anchorY;
248
- } else {
249
- Urso.logger.warn(); ('AnchorY value cannot be used with this object type !', object);
250
- }
251
-
252
- return 0;
253
- }
254
-
255
- /**
256
- * get anchor offset by angle for parent types
257
- * @param {Object} object
258
- * @param {String} side - x or y
259
- * @returns {Number}
260
- */
261
- _getAnchorOffsetByAngle(object, side) { //side can be x or y
262
- const objectWidth = this._getWidthAsNumber(object);
263
- const objectHeight = this._getHeightAsNumber(object);
264
- const xCatet = (objectWidth * object.anchorX);
265
- const yCatet = (objectHeight * object.anchorY);
266
- const offsetRadius = Math.sqrt(Math.pow(xCatet, 2) + Math.pow(yCatet, 2));
267
- const angleRadian = Math.atan(xCatet / yCatet); //todo or yCatet/xCatet ?
268
- const angle = Urso.helper.getAngle(angleRadian);
269
- const offsetAngle = object.angle + angle;
270
- const offsetFunction = side === 'x' ? 'cos' : 'sin';
271
- const angleOffset = - offsetRadius * Math[offsetFunction](Urso.helper.getRadian(offsetAngle));
272
-
273
- return angleOffset;
274
- }
275
-
276
- /**
277
- * adapt scaleX
278
- * @param {Object} object
279
- */
280
- _adaptScaleX(object) {
281
- if (object.scaleX !== 1 && typeof object.width !== 'boolean') {
282
- Urso.logger.error('ScaleX value cannot be set. Width already used!!');
283
- this._setPropertyWithoutAdaption(object, 'scaleX', 1);
284
- return;
285
- }
286
-
287
- if (typeof object.scaleX === 'number') {
288
- const pixiObject = object._baseObject;
289
- pixiObject.scale.x = object.scaleX;
290
- } else
291
- Urso.logger.error('ScaleX value is not valid!');
292
- }
293
-
294
- /**
295
- * adapt scaleY
296
- * @param {Object} object
297
- */
298
- _adaptScaleY(object) {
299
- if (object.scaleY !== 1 && typeof object.height !== 'boolean') {
300
- Urso.logger.error('ScaleY value cannot be set. Height already used!!');
301
- this._setPropertyWithoutAdaption(object, 'scaleY', 1);
302
- return;
303
- }
304
-
305
- if (typeof object.scaleY === 'number' && object.scaleY >= 0) { // TODO: CHECK SCALE CAN BE NEGATIVE
306
- const pixiObject = object._baseObject;
307
- pixiObject.scale.y = object.scaleY;
308
- } else
309
- Urso.logger.error('ScaleY value is not valid!');
310
- }
311
-
312
- /**
313
- * adapt alignX
314
- * @param {Object} object
315
- */
316
- _adaptAlignX(object) {
317
- if (typeof object.alignX !== 'string') {
318
- Urso.logger.error('AlignX value is not string!');
319
- return 0;
320
- }
321
-
322
- switch (object.alignX) {
323
- case 'left':
324
- return 0;
325
- case 'right':
326
- return object.parent ? this._getWidthAsNumber(object.parent) : 0; //parentWidth
327
- case 'center':
328
- const parentWidth = object.parent ? this._getWidthAsNumber(object.parent) : 0;
329
- return parentWidth / 2;
330
- default:
331
- Urso.logger.error('AlignX string is not valid!');
332
- return 0;
333
- }
334
- }
335
-
336
- /**
337
- * adapt alignY
338
- * @param {Object} object
339
- */
340
- _adaptAlignY(object) {
341
- if (typeof object.alignY !== 'string') {
342
- Urso.logger.error('AlignY value is not string!');
343
- return 0;
344
- }
345
-
346
- switch (object.alignY) {
347
- case 'top':
348
- return 0;
349
- case 'bottom':
350
- return object.parent ? this._getHeightAsNumber(object.parent) : 0; //parentHeight
351
- case 'center':
352
- const parentHeight = object.parent ? this._getHeightAsNumber(object.parent) : 0;
353
- return parentHeight / 2;
354
- default:
355
- Urso.logger.error('AlignY string is not valid!');
356
- return 0;
357
- }
358
- }
359
-
360
- /**
361
- * adapt width
362
- * @param {Object} object
363
- */
364
- _adaptWidth(object) {
365
- if (typeof object.width !== 'boolean' && object.scaleX !== 1) {
366
- Urso.logger.error('Width value cannot be set. ScaleX already used!!', object);
367
- this._setPropertyWithoutAdaption(object, 'width', false);
368
- return;
369
- }
370
-
371
- if (!object.width)
372
- return;
373
-
374
- if (!this._isValueANumberOrPercentsString(object.width))
375
- return Urso.logger.error('Width value is not valid!!');
376
-
377
- if (!this._canBeParent(object)) {
378
- const pixiObject = object._baseObject;
379
- pixiObject.width = this._getWidthAsNumber(object);
380
- }
381
- }
382
-
383
- /**
384
- * adapt height
385
- * @param {Object} object
386
- */
387
- _adaptHeight(object) {
388
- if (typeof object.height !== 'boolean' && object.scaleY !== 1) {
389
- Urso.logger.error('Height value cannot be set. ScaleY already used!!', object);
390
- this._setPropertyWithoutAdaption(object, 'height', false);
391
- return;
392
- }
393
-
394
- if (!object.height)
395
- return;
396
-
397
- if (!this._isValueANumberOrPercentsString(object.height))
398
- return Urso.logger.error('Height value not valid!');
399
-
400
- if (!this._canBeParent(object)) {
401
- const pixiObject = object._baseObject;
402
- pixiObject.height = this._getHeightAsNumber(object);
403
- }
404
- }
405
-
406
- /**
407
- * get x number value
408
- * @param {Object} object
409
- * @returns {Number}
410
- */
411
- _getXAsNumber(object) {
412
- return this._getPropertyAsNumber(object, 'x', 'width');
413
- }
414
-
415
- /**
416
- * get y number value
417
- * @param {Object} object
418
- * @returns {Number}
419
- */
420
- _getYAsNumber(object) {
421
- return this._getPropertyAsNumber(object, 'y', 'height');
422
- }
423
-
424
- /**
425
- * get width number value
426
- * @param {Object} object
427
- * @returns {Number}
428
- */
429
- _getWidthAsNumber(object) {
430
- return this._getPropertyAsNumber(object, 'width', 'width');
431
- }
432
-
433
- /**
434
- * get height number value
435
- * @param {Object} object
436
- * @returns {Number}
437
- */
438
- _getHeightAsNumber(object) {
439
- return this._getPropertyAsNumber(object, 'height', 'height');
440
- }
441
-
442
- /**
443
- * get x, y, width or height number value
444
- * @param {Object} object
445
- * @param {String} propertyName
446
- * @param {String} parentPropertyName
447
- * @returns {Number}
448
- */
449
- _getPropertyAsNumber(object, propertyName, parentPropertyName) {
450
- const propType = typeof object[propertyName];
451
-
452
- switch (propType) {
453
- case 'number':
454
- return object[propertyName];
455
-
456
- case 'string':
457
- const parentValue = this._getPropertyAsNumber(object.parent, parentPropertyName, parentPropertyName);
458
- return this._getRoundedPercentageOfNumber(object[propertyName], parentValue);
459
-
460
- case 'boolean':
461
- return this._getPropertyAsNumber(object.parent, propertyName, parentPropertyName)
462
-
463
- default:
464
- Urso.logger.error('Property value not number or string!', object, propertyName);
465
- return;
466
- }
467
- }
468
-
469
- /**
470
- * get rounded persents of number
471
- * @param {String} percentsString
472
- * @param {Number} number
473
- * @returns {Number}
474
- */
475
- _getRoundedPercentageOfNumber(percentsString, number) {
476
- const percentsFloat = parseFloat(percentsString);
477
- return ~~(percentsFloat * number / 100);
478
- }
479
-
480
- /**
481
- * check can be object a parent (by type)
482
- * @param {Object} object
483
- * @returns {Boolean}
484
- */
485
- _canBeParent(object) {
486
- return this._parentTypes.includes(object.type);
487
- }
488
-
489
- /**
490
- * check is value a number or a percents string
491
- * @param {mixed} value
492
- * @returns {Boolean}
493
- */
494
- _isValueANumberOrPercentsString(value) {
495
- return typeof value === 'number' || (typeof value === 'string' && value.endsWith('%'))
496
- }
497
-
498
- /**
499
- * main function to handle stretchingType
500
- * @param {Object} object
501
- */
502
- _adaptStretchingType(object) {
503
- if (object.width !== '100%' || object.height !== '100%' || !object.stretchingType)
504
- return;
505
-
506
- switch (object.stretchingType) {
507
- case 'inscribed':
508
- this._inscribe(object);
509
- break;
510
-
511
- case 'circumscribed':
512
- this._circumscribe(object);
513
- break;
514
-
515
- case 'false':
516
- break;
517
-
518
- default:
519
- Urso.logger.error('StretchingType value not valid!');
520
- break;
521
- }
522
- }
523
-
524
- /**
525
- * set property and adapt it (only for stretchingType)
526
- * @param {Object} object
527
- * @param {String} propertyName
528
- * @param {Number} value
529
- */
530
- _setPropertyAndAdaptIt(object, propertyName, value) {
531
- object[propertyName] = value;
532
- this.propertyChangeHandler(object, propertyName);
533
- }
534
-
535
- /**
536
- * set streching (only for stretchingType)
537
- * @param {Object} object
538
- * @param {Object} params
539
- */
540
- _setStreching(object, { scale, objectWidth, objectHeight }) {
541
- if (object.scaleX === 1)
542
- this._setPropertyAndAdaptIt(object, 'width', objectWidth * scale);
543
- else
544
- this._setPropertyAndAdaptIt(object, 'scaleX', scale);
545
-
546
- if (object.scaleY === 1)
547
- this._setPropertyAndAdaptIt(object, 'height', objectHeight * scale);
548
- else
549
- this._setPropertyAndAdaptIt(object, 'scaleY', scale);
550
- }
551
-
552
- /**
553
- * get object values for streching (only for stretchingType)
554
- * @param {Object} object
555
- */
556
- _getObjectValuesForStreching(object) {
557
- const objectWidth = this._getWidthAsNumber(object);
558
- const objectHeight = this._getHeightAsNumber(object);
559
- const parentWidth = this._getWidthAsNumber(object.parent);
560
- const parentHeight = this._getHeightAsNumber(object.parent);
561
-
562
- const scaleX = parentWidth / objectWidth;
563
- const scaleY = parentHeight / objectHeight;
564
-
565
- return { objectWidth, objectHeight, scaleX, scaleY };
566
- }
567
-
568
- /**
569
- * inscribe handler (only for stretchingType)
570
- * @param {Object} object
571
- */
572
- _inscribe(object) {
573
- const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
574
- const scale = Math.min(scaleX, scaleY);
575
- this._setStreching(object, { scale, objectWidth, objectHeight });
576
- }
577
-
578
- /**
579
- * circumscribe handler (only for stretchingType)
580
- * @param {Object} object
581
- */
582
- _circumscribe(object) {
583
- const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
584
- const scale = Math.max(scaleX, scaleY);
585
- this._setStreching(object, { scale, objectWidth, objectHeight });
586
- }
587
- }
588
-
1
+ /**
2
+ * adapter Urso to Pixi objects
3
+ */
4
+ class PropertyAdapter {
5
+
6
+ constructor() {
7
+ this.singleton = true;
8
+
9
+ /**
10
+ * dependencies, functions to handle changing property
11
+ */
12
+ this._dependencies = {
13
+ 'x': this._updateHorizontal.bind(this),
14
+ 'y': this._updateVertical.bind(this),
15
+ 'anchorX': this._updateHorizontal.bind(this),
16
+ 'anchorY': this._updateVertical.bind(this),
17
+ 'scaleX': this._adaptScaleX.bind(this),
18
+ 'scaleY': this._adaptScaleY.bind(this),
19
+ 'alignX': this._updateHorizontal.bind(this),
20
+ 'alignY': this._updateVertical.bind(this),
21
+ 'width': this._updateHorizontal.bind(this),
22
+ 'height': this._updateVertical.bind(this),
23
+ 'angle': this._updateAngle.bind(this),
24
+ 'stretchingType': this._adaptStretchingType.bind(this), //todo check on parent change
25
+ 'parent': this._parentChangeHandler.bind(this)
26
+ };
27
+
28
+ /**
29
+ * functions to handle changing parent property
30
+ */
31
+ this._parentToChildDependencies = {
32
+ 'width': { children: ['width'] },
33
+ 'height': { children: ['height'] },
34
+ 'anchorX': { children: ['x'] },
35
+ 'anchorY': { children: ['y'] },
36
+ 'stretchingType': { children: ['width', 'height'] }
37
+ };
38
+
39
+ /**
40
+ * parent types list
41
+ */
42
+ this._parentTypes = [
43
+ Urso.types.objects.COMPONENT,
44
+ Urso.types.objects.CONTAINER,
45
+ Urso.types.objects.DRAGCONTAINER,
46
+ Urso.types.objects.GROUP,
47
+ Urso.types.objects.SCROLLBOX,
48
+ Urso.types.objects.SLIDER,
49
+ Urso.types.objects.SPINE,
50
+ Urso.types.objects.WORLD
51
+ ];
52
+
53
+ /**
54
+ * types without ahchor list
55
+ */
56
+ this._typesWithoutAnchor = [
57
+ Urso.types.objects.CHECKBOX,
58
+ Urso.types.objects.EMITTER,
59
+ Urso.types.objects.EMITTERFX,
60
+ Urso.types.objects.GRAPHICS,
61
+ Urso.types.objects.HITAREA,
62
+ Urso.types.objects.MASK,
63
+ Urso.types.objects.NINESLICEPLANE,
64
+ Urso.types.objects.SLIDER,
65
+ Urso.types.objects.SPINE,
66
+ Urso.types.objects.TEXTINPUT,
67
+ Urso.types.objects.WORLD
68
+ ];
69
+ }
70
+
71
+ /**
72
+ * check is adaptive property
73
+ * @param {String} property
74
+ * @returns {Boolean}
75
+ */
76
+ isAdaptiveProperty(property) {
77
+ return Object.keys(this._dependencies).includes(property);
78
+ };
79
+
80
+ /**
81
+ * property change handler (main function)
82
+ * @param {Object} object
83
+ * @param {String} propertyName
84
+ */
85
+ propertyChangeHandler(object, propertyName) {
86
+ this._adaptProperty(object, propertyName);
87
+ this._adaptChildProperties(object, propertyName);
88
+ }
89
+
90
+ /**
91
+ * adapt property (launch handler changing property)
92
+ * @param {Object} object
93
+ * @param {String} propertyName
94
+ */
95
+ _adaptProperty(object, propertyName) {
96
+ if (this._dependencies[propertyName])
97
+ this._dependencies[propertyName](object);
98
+ }
99
+
100
+ /**
101
+ * main function to calculate x and width
102
+ * @param {Object} object
103
+ */
104
+ _updateHorizontal(object) {
105
+ let x = this._getXAsNumber(object);
106
+ x += this._adaptAnchorX(object);
107
+ x += this._adaptAlignX(object);
108
+ object._baseObject.x = x;
109
+
110
+ this._adaptWidth(object);
111
+ }
112
+
113
+ /**
114
+ * main function to calculate angle
115
+ * @param {Object} object
116
+ */
117
+ _updateAngle(object) {
118
+ object._baseObject.angle = object.angle;
119
+
120
+ if (!this._canBeParent(object)) {
121
+ return;
122
+ }
123
+
124
+ this._updateHorizontal(object);
125
+ this._updateVertical(object);
126
+ }
127
+
128
+ /**
129
+ * main function to calculate y and height
130
+ * @param {Object} object
131
+ */
132
+ _updateVertical(object) {
133
+ let y = this._getYAsNumber(object);
134
+ y += this._adaptAnchorY(object);
135
+ y += this._adaptAlignY(object);
136
+ object._baseObject.y = y;
137
+
138
+ this._adaptHeight(object);
139
+ }
140
+
141
+ /**
142
+ * set property value without adaptation
143
+ * @param {Object} object
144
+ * @param {String} propertyName
145
+ * @param {mixed} value
146
+ */
147
+ _setPropertyWithoutAdaption(object, propertyName, value) { //in error case
148
+ object[propertyName] = value;
149
+ }
150
+
151
+ /**
152
+ * adapt child properties if parent properties was changed
153
+ * @param {Object} object
154
+ * @param {String} propertyName
155
+ */
156
+ _adaptChildProperties(object, propertyName) {
157
+ const { children } = this._parentToChildDependencies[propertyName] || {};
158
+ const objectHasChildren = this._canBeParent(object) && object.hasOwnProperty('contents');
159
+
160
+ if (!children || !objectHasChildren)
161
+ return;
162
+
163
+ for (let dependency of children)
164
+ for (let child of object.contents)
165
+ this.propertyChangeHandler(child, dependency);
166
+ }
167
+
168
+ /**
169
+ * main handler when new parent was setted
170
+ * @param {Object} child
171
+ */
172
+ _parentChangeHandler(child) {
173
+ if (child.parent == null)
174
+ return;
175
+
176
+ for (let propertyName of this._propertiesDependentOnParent())
177
+ this.propertyChangeHandler(child, propertyName);
178
+ }
179
+
180
+ /**
181
+ * get array of properties dependent on parent
182
+ * //todo cache them and get just one time
183
+ * @returns {Array}
184
+ */
185
+ _propertiesDependentOnParent() {
186
+ const properties = [];
187
+
188
+ for (let propertyName of Object.keys(this._parentToChildDependencies)) {
189
+ const { children } = this._parentToChildDependencies[propertyName];
190
+
191
+ if (children)
192
+ for (let dependency of children)
193
+ properties.push(dependency);
194
+ }
195
+
196
+ return properties;
197
+ }
198
+
199
+ /**
200
+ * adapt anchorX
201
+ * @param {Object} object
202
+ */
203
+ _adaptAnchorX(object) {
204
+ if (typeof object.anchorX !== 'number' || object.anchorX < 0 || object.anchorX > 1)
205
+ Urso.logger.error('AnchorX value is not valid!', object);
206
+
207
+ if (this._canBeParent(object) && !this._typesWithoutAnchor.includes(object.type)) { //parent types
208
+ if (object.anchorX === 0)
209
+ return 0;
210
+
211
+ if (object.angle) {
212
+ return this._getAnchorOffsetByAngle(object, 'x');
213
+ }
214
+
215
+ const objectWidth = this._getWidthAsNumber(object);
216
+ return - objectWidth * object.anchorX;
217
+ } else if (!this._typesWithoutAnchor.includes(object.type)) { //regular type and not a type without anchor
218
+ const pixiObject = object._baseObject;
219
+ pixiObject.anchor.x = object.anchorX;
220
+ } else {
221
+ Urso.logger.warn(); ('AnchorX value cannot be used with this object type !', object);
222
+ }
223
+
224
+ return 0;
225
+ }
226
+
227
+ /**
228
+ * adapt anchorY
229
+ * @param {Object} object
230
+ */
231
+ _adaptAnchorY(object) {
232
+ if (typeof object.anchorY !== 'number' || object.anchorY < 0 || object.anchorY > 1)
233
+ Urso.logger.error('AnchorY value is not valid!', object);
234
+
235
+ if (this._canBeParent(object) && !this._typesWithoutAnchor.includes(object.type)) { //parent types
236
+ if (object.anchorY === 0)
237
+ return 0;
238
+
239
+ if (object.angle) {
240
+ return this._getAnchorOffsetByAngle(object, 'y');
241
+ }
242
+
243
+ const objectHeight = this._getHeightAsNumber(object);
244
+ return - objectHeight * object.anchorY;
245
+ } else if (!this._typesWithoutAnchor.includes(object.type)) { //regular type and not a type without anchor
246
+ const pixiObject = object._baseObject;
247
+ pixiObject.anchor.y = object.anchorY;
248
+ } else {
249
+ Urso.logger.warn(); ('AnchorY value cannot be used with this object type !', object);
250
+ }
251
+
252
+ return 0;
253
+ }
254
+
255
+ /**
256
+ * get anchor offset by angle for parent types
257
+ * @param {Object} object
258
+ * @param {String} side - x or y
259
+ * @returns {Number}
260
+ */
261
+ _getAnchorOffsetByAngle(object, side) { //side can be x or y
262
+ const objectWidth = this._getWidthAsNumber(object);
263
+ const objectHeight = this._getHeightAsNumber(object);
264
+ const xCatet = (objectWidth * object.anchorX);
265
+ const yCatet = (objectHeight * object.anchorY);
266
+ const offsetRadius = Math.sqrt(Math.pow(xCatet, 2) + Math.pow(yCatet, 2));
267
+ const angleRadian = Math.atan(xCatet / yCatet); //todo or yCatet/xCatet ?
268
+ const angle = Urso.helper.getAngle(angleRadian);
269
+ const offsetAngle = object.angle + angle;
270
+ const offsetFunction = side === 'x' ? 'cos' : 'sin';
271
+ const angleOffset = - offsetRadius * Math[offsetFunction](Urso.helper.getRadian(offsetAngle));
272
+
273
+ return angleOffset;
274
+ }
275
+
276
+ /**
277
+ * adapt scaleX
278
+ * @param {Object} object
279
+ */
280
+ _adaptScaleX(object) {
281
+ if (object.scaleX !== 1 && typeof object.width !== 'boolean') {
282
+ Urso.logger.error('ScaleX value cannot be set. Width already used!!');
283
+ this._setPropertyWithoutAdaption(object, 'scaleX', 1);
284
+ return;
285
+ }
286
+
287
+ if (typeof object.scaleX === 'number') {
288
+ const pixiObject = object._baseObject;
289
+ pixiObject.scale.x = object.scaleX;
290
+ } else
291
+ Urso.logger.error('ScaleX value is not valid!');
292
+ }
293
+
294
+ /**
295
+ * adapt scaleY
296
+ * @param {Object} object
297
+ */
298
+ _adaptScaleY(object) {
299
+ if (object.scaleY !== 1 && typeof object.height !== 'boolean') {
300
+ Urso.logger.error('ScaleY value cannot be set. Height already used!!');
301
+ this._setPropertyWithoutAdaption(object, 'scaleY', 1);
302
+ return;
303
+ }
304
+
305
+ if (typeof object.scaleY === 'number' && object.scaleY >= 0) { // TODO: CHECK SCALE CAN BE NEGATIVE
306
+ const pixiObject = object._baseObject;
307
+ pixiObject.scale.y = object.scaleY;
308
+ } else
309
+ Urso.logger.error('ScaleY value is not valid!');
310
+ }
311
+
312
+ /**
313
+ * adapt alignX
314
+ * @param {Object} object
315
+ */
316
+ _adaptAlignX(object) {
317
+ if (typeof object.alignX !== 'string') {
318
+ Urso.logger.error('AlignX value is not string!');
319
+ return 0;
320
+ }
321
+
322
+ switch (object.alignX) {
323
+ case 'left':
324
+ return 0;
325
+ case 'right':
326
+ return object.parent ? this._getWidthAsNumber(object.parent) : 0; //parentWidth
327
+ case 'center':
328
+ const parentWidth = object.parent ? this._getWidthAsNumber(object.parent) : 0;
329
+ return parentWidth / 2;
330
+ default:
331
+ Urso.logger.error('AlignX string is not valid!');
332
+ return 0;
333
+ }
334
+ }
335
+
336
+ /**
337
+ * adapt alignY
338
+ * @param {Object} object
339
+ */
340
+ _adaptAlignY(object) {
341
+ if (typeof object.alignY !== 'string') {
342
+ Urso.logger.error('AlignY value is not string!');
343
+ return 0;
344
+ }
345
+
346
+ switch (object.alignY) {
347
+ case 'top':
348
+ return 0;
349
+ case 'bottom':
350
+ return object.parent ? this._getHeightAsNumber(object.parent) : 0; //parentHeight
351
+ case 'center':
352
+ const parentHeight = object.parent ? this._getHeightAsNumber(object.parent) : 0;
353
+ return parentHeight / 2;
354
+ default:
355
+ Urso.logger.error('AlignY string is not valid!');
356
+ return 0;
357
+ }
358
+ }
359
+
360
+ /**
361
+ * adapt width
362
+ * @param {Object} object
363
+ */
364
+ _adaptWidth(object) {
365
+ if (typeof object.width !== 'boolean' && object.scaleX !== 1) {
366
+ Urso.logger.error('Width value cannot be set. ScaleX already used!!', object);
367
+ this._setPropertyWithoutAdaption(object, 'width', false);
368
+ return;
369
+ }
370
+
371
+ if (!object.width)
372
+ return;
373
+
374
+ if (!this._isValueANumberOrPercentsString(object.width))
375
+ return Urso.logger.error('Width value is not valid!!');
376
+
377
+ if (!this._canBeParent(object)) {
378
+ const pixiObject = object._baseObject;
379
+ pixiObject.width = this._getWidthAsNumber(object);
380
+ }
381
+ }
382
+
383
+ /**
384
+ * adapt height
385
+ * @param {Object} object
386
+ */
387
+ _adaptHeight(object) {
388
+ if (typeof object.height !== 'boolean' && object.scaleY !== 1) {
389
+ Urso.logger.error('Height value cannot be set. ScaleY already used!!', object);
390
+ this._setPropertyWithoutAdaption(object, 'height', false);
391
+ return;
392
+ }
393
+
394
+ if (!object.height)
395
+ return;
396
+
397
+ if (!this._isValueANumberOrPercentsString(object.height))
398
+ return Urso.logger.error('Height value not valid!');
399
+
400
+ if (!this._canBeParent(object)) {
401
+ const pixiObject = object._baseObject;
402
+ pixiObject.height = this._getHeightAsNumber(object);
403
+ }
404
+ }
405
+
406
+ /**
407
+ * get x number value
408
+ * @param {Object} object
409
+ * @returns {Number}
410
+ */
411
+ _getXAsNumber(object) {
412
+ return this._getPropertyAsNumber(object, 'x', 'width');
413
+ }
414
+
415
+ /**
416
+ * get y number value
417
+ * @param {Object} object
418
+ * @returns {Number}
419
+ */
420
+ _getYAsNumber(object) {
421
+ return this._getPropertyAsNumber(object, 'y', 'height');
422
+ }
423
+
424
+ /**
425
+ * get width number value
426
+ * @param {Object} object
427
+ * @returns {Number}
428
+ */
429
+ _getWidthAsNumber(object) {
430
+ return this._getPropertyAsNumber(object, 'width', 'width');
431
+ }
432
+
433
+ /**
434
+ * get height number value
435
+ * @param {Object} object
436
+ * @returns {Number}
437
+ */
438
+ _getHeightAsNumber(object) {
439
+ return this._getPropertyAsNumber(object, 'height', 'height');
440
+ }
441
+
442
+ /**
443
+ * get x, y, width or height number value
444
+ * @param {Object} object
445
+ * @param {String} propertyName
446
+ * @param {String} parentPropertyName
447
+ * @returns {Number}
448
+ */
449
+ _getPropertyAsNumber(object, propertyName, parentPropertyName) {
450
+ const propType = typeof object[propertyName];
451
+
452
+ switch (propType) {
453
+ case 'number':
454
+ return object[propertyName];
455
+
456
+ case 'string':
457
+ const parentValue = this._getPropertyAsNumber(object.parent, parentPropertyName, parentPropertyName);
458
+ return this._getRoundedPercentageOfNumber(object[propertyName], parentValue);
459
+
460
+ case 'boolean':
461
+ return this._getPropertyAsNumber(object.parent, propertyName, parentPropertyName)
462
+
463
+ default:
464
+ Urso.logger.error('Property value not number or string!', object, propertyName);
465
+ return;
466
+ }
467
+ }
468
+
469
+ /**
470
+ * get rounded persents of number
471
+ * @param {String} percentsString
472
+ * @param {Number} number
473
+ * @returns {Number}
474
+ */
475
+ _getRoundedPercentageOfNumber(percentsString, number) {
476
+ const percentsFloat = parseFloat(percentsString);
477
+ return ~~(percentsFloat * number / 100);
478
+ }
479
+
480
+ /**
481
+ * check can be object a parent (by type)
482
+ * @param {Object} object
483
+ * @returns {Boolean}
484
+ */
485
+ _canBeParent(object) {
486
+ return this._parentTypes.includes(object.type);
487
+ }
488
+
489
+ /**
490
+ * check is value a number or a percents string
491
+ * @param {mixed} value
492
+ * @returns {Boolean}
493
+ */
494
+ _isValueANumberOrPercentsString(value) {
495
+ return typeof value === 'number' || (typeof value === 'string' && value.endsWith('%'))
496
+ }
497
+
498
+ /**
499
+ * main function to handle stretchingType
500
+ * @param {Object} object
501
+ */
502
+ _adaptStretchingType(object) {
503
+ if (object.width !== '100%' || object.height !== '100%' || !object.stretchingType)
504
+ return;
505
+
506
+ switch (object.stretchingType) {
507
+ case 'inscribed':
508
+ this._inscribe(object);
509
+ break;
510
+
511
+ case 'circumscribed':
512
+ this._circumscribe(object);
513
+ break;
514
+
515
+ case 'false':
516
+ break;
517
+
518
+ default:
519
+ Urso.logger.error('StretchingType value not valid!');
520
+ break;
521
+ }
522
+ }
523
+
524
+ /**
525
+ * set property and adapt it (only for stretchingType)
526
+ * @param {Object} object
527
+ * @param {String} propertyName
528
+ * @param {Number} value
529
+ */
530
+ _setPropertyAndAdaptIt(object, propertyName, value) {
531
+ object[propertyName] = value;
532
+ this.propertyChangeHandler(object, propertyName);
533
+ }
534
+
535
+ /**
536
+ * set streching (only for stretchingType)
537
+ * @param {Object} object
538
+ * @param {Object} params
539
+ */
540
+ _setStreching(object, { scale, objectWidth, objectHeight }) {
541
+ if (object.scaleX === 1)
542
+ this._setPropertyAndAdaptIt(object, 'width', objectWidth * scale);
543
+ else
544
+ this._setPropertyAndAdaptIt(object, 'scaleX', scale);
545
+
546
+ if (object.scaleY === 1)
547
+ this._setPropertyAndAdaptIt(object, 'height', objectHeight * scale);
548
+ else
549
+ this._setPropertyAndAdaptIt(object, 'scaleY', scale);
550
+ }
551
+
552
+ /**
553
+ * get object values for streching (only for stretchingType)
554
+ * @param {Object} object
555
+ */
556
+ _getObjectValuesForStreching(object) {
557
+ const objectWidth = this._getWidthAsNumber(object);
558
+ const objectHeight = this._getHeightAsNumber(object);
559
+ const parentWidth = this._getWidthAsNumber(object.parent);
560
+ const parentHeight = this._getHeightAsNumber(object.parent);
561
+
562
+ const scaleX = parentWidth / objectWidth;
563
+ const scaleY = parentHeight / objectHeight;
564
+
565
+ return { objectWidth, objectHeight, scaleX, scaleY };
566
+ }
567
+
568
+ /**
569
+ * inscribe handler (only for stretchingType)
570
+ * @param {Object} object
571
+ */
572
+ _inscribe(object) {
573
+ const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
574
+ const scale = Math.min(scaleX, scaleY);
575
+ this._setStreching(object, { scale, objectWidth, objectHeight });
576
+ }
577
+
578
+ /**
579
+ * circumscribe handler (only for stretchingType)
580
+ * @param {Object} object
581
+ */
582
+ _circumscribe(object) {
583
+ const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
584
+ const scale = Math.max(scaleX, scaleY);
585
+ this._setStreching(object, { scale, objectWidth, objectHeight });
586
+ }
587
+ }
588
+
589
589
  module.exports = PropertyAdapter;