@urso/core 0.8.21 → 0.9.1-dev

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/LICENSE +20 -20
  2. package/README.md +227 -227
  3. package/build/js/index.js +77212 -2
  4. package/package.json +36 -50
  5. package/src/js/app.js +105 -104
  6. package/src/js/components/base/controller.js +76 -78
  7. package/src/js/components/debug/controller.js +39 -39
  8. package/src/js/components/debug/coords.js +23 -23
  9. package/src/js/components/debug/fps.js +36 -36
  10. package/src/js/components/debug/template.js +55 -55
  11. package/src/js/components/debug/timescale.js +60 -60
  12. package/src/js/components/deviceRotate/controller.js +95 -95
  13. package/src/js/components/editor/api.js +127 -127
  14. package/src/js/components/editor/controller.js +13 -13
  15. package/src/js/components/fullscreen/android.js +104 -104
  16. package/src/js/components/fullscreen/controller.js +76 -76
  17. package/src/js/components/fullscreen/desktop.js +49 -49
  18. package/src/js/components/fullscreen/ios.js +115 -115
  19. package/src/js/components/layersSwitcher/config.js +26 -26
  20. package/src/js/components/layersSwitcher/controller.js +36 -36
  21. package/src/js/components/loader/controller.js +66 -66
  22. package/src/js/components/loader/template.js +71 -71
  23. package/src/js/components/soundInitialPopup/controller.js +43 -43
  24. package/src/js/components/soundInitialPopup/template.js +110 -110
  25. package/src/js/components/stateDriven/controller.js +123 -123
  26. package/src/js/config/load.js +325 -194
  27. package/src/js/config/main.js +17 -17
  28. package/src/js/extra/browserEvents.js +57 -57
  29. package/src/js/extra/main.js +23 -25
  30. package/src/js/extra/pixiPatch.js +173 -171
  31. package/src/js/index.js +7 -7
  32. package/src/js/lib/cache.js +199 -125
  33. package/src/js/lib/composition.js +85 -85
  34. package/src/js/lib/device.js +1215 -1215
  35. package/src/js/lib/helper.js +678 -678
  36. package/src/js/lib/loader.js +216 -218
  37. package/src/js/lib/localData.js +29 -29
  38. package/src/js/lib/logger.js +69 -69
  39. package/src/js/lib/math.js +161 -161
  40. package/src/js/lib/objectPool.js +208 -208
  41. package/src/js/lib/time.js +19 -19
  42. package/src/js/lib/tween.js +153 -153
  43. package/src/js/modules/assets/baseModel.js +21 -21
  44. package/src/js/modules/assets/config.js +38 -38
  45. package/src/js/modules/assets/controller.js +65 -65
  46. package/src/js/modules/assets/models/atlas.js +19 -19
  47. package/src/js/modules/assets/models/audiosprite.js +28 -28
  48. package/src/js/modules/assets/models/bitmapFont.js +11 -11
  49. package/src/js/modules/assets/models/container.js +19 -19
  50. package/src/js/modules/assets/models/font.js +11 -11
  51. package/src/js/modules/assets/models/html.js +11 -11
  52. package/src/js/modules/assets/models/image.js +17 -17
  53. package/src/js/modules/assets/models/json.js +11 -11
  54. package/src/js/modules/assets/models/jsonAtlas.js +11 -11
  55. package/src/js/modules/assets/models/sound.js +17 -17
  56. package/src/js/modules/assets/models/spine.js +18 -17
  57. package/src/js/modules/assets/models/spineAtlas.js +11 -0
  58. package/src/js/modules/assets/service.js +564 -553
  59. package/src/js/modules/i18n/config.js +17 -17
  60. package/src/js/modules/i18n/controller.js +71 -71
  61. package/src/js/modules/instances/controller.js +357 -357
  62. package/src/js/modules/logic/config/sounds.js +23 -23
  63. package/src/js/modules/logic/controller.js +52 -52
  64. package/src/js/modules/logic/main.js +8 -8
  65. package/src/js/modules/logic/sounds.js +103 -103
  66. package/src/js/modules/objects/baseModel.js +205 -207
  67. package/src/js/modules/objects/cache.js +99 -99
  68. package/src/js/modules/objects/config.js +10 -10
  69. package/src/js/modules/objects/controller.js +139 -139
  70. package/src/js/modules/objects/find.js +58 -58
  71. package/src/js/modules/objects/models/bitmapText.js +56 -49
  72. package/src/js/modules/objects/models/button.js +208 -209
  73. package/src/js/modules/objects/models/buttonComposite.js +38 -38
  74. package/src/js/modules/objects/models/checkbox.js +100 -100
  75. package/src/js/modules/objects/models/collection.js +56 -54
  76. package/src/js/modules/objects/models/component.js +48 -48
  77. package/src/js/modules/objects/models/container.js +22 -22
  78. package/src/js/modules/objects/models/emitterFx.js +103 -114
  79. package/src/js/modules/objects/models/graphics.js +40 -40
  80. package/src/js/modules/objects/models/group.js +22 -22
  81. package/src/js/modules/objects/models/hitArea.js +187 -187
  82. package/src/js/modules/objects/models/image.js +36 -36
  83. package/src/js/modules/objects/models/imagesAnimation.js +115 -115
  84. package/src/js/modules/objects/models/mask.js +40 -40
  85. package/src/js/modules/objects/models/nineSlicePlane.js +32 -32
  86. package/src/js/modules/objects/models/slider.js +357 -357
  87. package/src/js/modules/objects/models/spine.js +354 -339
  88. package/src/js/modules/objects/models/text.js +70 -70
  89. package/src/js/modules/objects/models/toggle.js +186 -186
  90. package/src/js/modules/objects/models/world.js +22 -22
  91. package/src/js/modules/objects/pool.js +68 -68
  92. package/src/js/modules/objects/propertyAdapter.js +592 -592
  93. package/src/js/modules/objects/proxy.js +299 -298
  94. package/src/js/modules/objects/selector.js +136 -136
  95. package/src/js/modules/objects/service.js +255 -254
  96. package/src/js/modules/objects/styles.js +210 -210
  97. package/src/js/modules/observer/controller.js +168 -168
  98. package/src/js/modules/observer/events.js +51 -56
  99. package/src/js/modules/scenes/controller.js +126 -127
  100. package/src/js/modules/scenes/model.js +28 -28
  101. package/src/js/modules/scenes/pixiWrapper.js +360 -351
  102. package/src/js/modules/scenes/resolutions.js +173 -173
  103. package/src/js/modules/scenes/resolutionsConfig.js +73 -73
  104. package/src/js/modules/scenes/service.js +144 -146
  105. package/src/js/modules/soundManager/controller.js +103 -103
  106. package/src/js/modules/soundManager/soundSprite.js +314 -314
  107. package/src/js/modules/statesManager/action.js +97 -97
  108. package/src/js/modules/statesManager/all.js +23 -23
  109. package/src/js/modules/statesManager/configStates.js +77 -77
  110. package/src/js/modules/statesManager/controller.js +219 -219
  111. package/src/js/modules/statesManager/functionsStorage.js +83 -83
  112. package/src/js/modules/statesManager/helper.js +27 -27
  113. package/src/js/modules/statesManager/race.js +91 -91
  114. package/src/js/modules/statesManager/sequence.js +48 -48
  115. package/src/js/modules/template/controller.js +28 -28
  116. package/src/js/modules/template/model.js +11 -11
  117. package/src/js/modules/template/service.js +137 -137
  118. package/src/js/modules/template/types.js +50 -49
  119. package/src/js/modules/transport/baseConnectionType.js +25 -25
  120. package/src/js/modules/transport/config.js +13 -13
  121. package/src/js/modules/transport/connectionTypes/websocket.js +77 -77
  122. package/src/js/modules/transport/connectionTypes/xhr.js +47 -47
  123. package/src/js/modules/transport/controller.js +48 -48
  124. package/src/js/modules/transport/decorator.js +17 -17
  125. package/src/js/modules/transport/service.js +150 -150
  126. package/vite.config.js +31 -0
  127. package/.babelrc +0 -12
  128. package/build/js/index.js.LICENSE.txt +0 -227
  129. package/src/js/modules/objects/models/atlasImage.js +0 -52
  130. package/src/js/modules/objects/models/dragContainer.js +0 -665
  131. package/src/js/modules/objects/models/emitter.js +0 -69
  132. package/src/js/modules/objects/models/scrollbox.js +0 -67
  133. package/src/js/modules/objects/models/textInput.js +0 -68
  134. package/webpack.config.js +0 -47
@@ -1,592 +1,592 @@
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) - this._getWidthAsNumber(object) : 0; //parentWidth-objectWidth
327
- case 'center':
328
- const parentWidth = object.parent ? this._getWidthAsNumber(object.parent) - this._getWidthAsNumber(object) : 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) - this._getHeightAsNumber(object) : 0; //parentHeight
351
- case 'center':
352
- const parentHeight = object.parent ? this._getHeightAsNumber(object.parent) - this._getHeightAsNumber(object) : 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
- if (this._canBeParent(object))
462
- return this._getPropertyAsNumber(object.parent, propertyName, parentPropertyName);
463
- else
464
- return object._baseObject[propertyName];
465
-
466
- default:
467
- Urso.logger.error('Property value not number or string!', object, propertyName);
468
- return;
469
- }
470
- }
471
-
472
- /**
473
- * get rounded persents of number
474
- * @param {String} percentsString
475
- * @param {Number} number
476
- * @returns {Number}
477
- */
478
- _getRoundedPercentageOfNumber(percentsString, number) {
479
- const percentsFloat = parseFloat(percentsString);
480
- return ~~(percentsFloat * number / 100);
481
- }
482
-
483
- /**
484
- * check can be object a parent (by type)
485
- * @param {Object} object
486
- * @returns {Boolean}
487
- */
488
- _canBeParent(object) {
489
- return this._parentTypes.includes(object.type);
490
- }
491
-
492
- /**
493
- * check is value a number or a percents string
494
- * @param {mixed} value
495
- * @returns {Boolean}
496
- */
497
- _isValueANumberOrPercentsString(value) {
498
- return typeof value === 'number' || (typeof value === 'string' && value.endsWith('%'))
499
- }
500
-
501
- /**
502
- * main function to handle stretchingType
503
- * @param {Object} object
504
- */
505
- _adaptStretchingType(object) {
506
- if (object.width !== '100%' || object.height !== '100%' || !object.stretchingType)
507
- return;
508
-
509
- switch (object.stretchingType) {
510
- case 'inscribed':
511
- this._inscribe(object);
512
- break;
513
-
514
- case 'circumscribed':
515
- this._circumscribe(object);
516
- break;
517
-
518
- case 'false':
519
- break;
520
-
521
- default:
522
- Urso.logger.error('StretchingType value not valid!');
523
- break;
524
- }
525
- }
526
-
527
- /**
528
- * set property and adapt it (only for stretchingType)
529
- * @param {Object} object
530
- * @param {String} propertyName
531
- * @param {Number} value
532
- */
533
- _setPropertyAndAdaptIt(object, propertyName, value) {
534
- object[propertyName] = value;
535
- this.propertyChangeHandler(object, propertyName);
536
- }
537
-
538
- /**
539
- * set streching (only for stretchingType)
540
- * @param {Object} object
541
- * @param {Object} params
542
- */
543
- _setStreching(object, { scale, objectWidth, objectHeight }) {
544
- if (object.scaleX === 1)
545
- this._setPropertyAndAdaptIt(object, 'width', objectWidth * scale);
546
- else
547
- this._setPropertyAndAdaptIt(object, 'scaleX', scale);
548
-
549
- if (object.scaleY === 1)
550
- this._setPropertyAndAdaptIt(object, 'height', objectHeight * scale);
551
- else
552
- this._setPropertyAndAdaptIt(object, 'scaleY', scale);
553
- }
554
-
555
- /**
556
- * get object values for streching (only for stretchingType)
557
- * @param {Object} object
558
- */
559
- _getObjectValuesForStreching(object) {
560
- const objectWidth = this._getWidthAsNumber(object);
561
- const objectHeight = this._getHeightAsNumber(object);
562
- const parentWidth = this._getWidthAsNumber(object.parent);
563
- const parentHeight = this._getHeightAsNumber(object.parent);
564
-
565
- const scaleX = parentWidth / objectWidth;
566
- const scaleY = parentHeight / objectHeight;
567
-
568
- return { objectWidth, objectHeight, scaleX, scaleY };
569
- }
570
-
571
- /**
572
- * inscribe handler (only for stretchingType)
573
- * @param {Object} object
574
- */
575
- _inscribe(object) {
576
- const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
577
- const scale = Math.min(scaleX, scaleY);
578
- this._setStreching(object, { scale, objectWidth, objectHeight });
579
- }
580
-
581
- /**
582
- * circumscribe handler (only for stretchingType)
583
- * @param {Object} object
584
- */
585
- _circumscribe(object) {
586
- const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
587
- const scale = Math.max(scaleX, scaleY);
588
- this._setStreching(object, { scale, objectWidth, objectHeight });
589
- }
590
- }
591
-
592
- module.exports = PropertyAdapter;
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) - this._getWidthAsNumber(object) : 0; //parentWidth-objectWidth
327
+ case 'center':
328
+ const parentWidth = object.parent ? this._getWidthAsNumber(object.parent) - this._getWidthAsNumber(object) : 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) - this._getHeightAsNumber(object) : 0; //parentHeight
351
+ case 'center':
352
+ const parentHeight = object.parent ? this._getHeightAsNumber(object.parent) - this._getHeightAsNumber(object) : 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
+ if (this._canBeParent(object))
462
+ return this._getPropertyAsNumber(object.parent, propertyName, parentPropertyName);
463
+ else
464
+ return object._baseObject[propertyName];
465
+
466
+ default:
467
+ Urso.logger.error('Property value not number or string!', object, propertyName);
468
+ return;
469
+ }
470
+ }
471
+
472
+ /**
473
+ * get rounded persents of number
474
+ * @param {String} percentsString
475
+ * @param {Number} number
476
+ * @returns {Number}
477
+ */
478
+ _getRoundedPercentageOfNumber(percentsString, number) {
479
+ const percentsFloat = parseFloat(percentsString);
480
+ return ~~(percentsFloat * number / 100);
481
+ }
482
+
483
+ /**
484
+ * check can be object a parent (by type)
485
+ * @param {Object} object
486
+ * @returns {Boolean}
487
+ */
488
+ _canBeParent(object) {
489
+ return this._parentTypes.includes(object.type);
490
+ }
491
+
492
+ /**
493
+ * check is value a number or a percents string
494
+ * @param {mixed} value
495
+ * @returns {Boolean}
496
+ */
497
+ _isValueANumberOrPercentsString(value) {
498
+ return typeof value === 'number' || (typeof value === 'string' && value.endsWith('%'))
499
+ }
500
+
501
+ /**
502
+ * main function to handle stretchingType
503
+ * @param {Object} object
504
+ */
505
+ _adaptStretchingType(object) {
506
+ if (object.width !== '100%' || object.height !== '100%' || !object.stretchingType)
507
+ return;
508
+
509
+ switch (object.stretchingType) {
510
+ case 'inscribed':
511
+ this._inscribe(object);
512
+ break;
513
+
514
+ case 'circumscribed':
515
+ this._circumscribe(object);
516
+ break;
517
+
518
+ case 'false':
519
+ break;
520
+
521
+ default:
522
+ Urso.logger.error('StretchingType value not valid!');
523
+ break;
524
+ }
525
+ }
526
+
527
+ /**
528
+ * set property and adapt it (only for stretchingType)
529
+ * @param {Object} object
530
+ * @param {String} propertyName
531
+ * @param {Number} value
532
+ */
533
+ _setPropertyAndAdaptIt(object, propertyName, value) {
534
+ object[propertyName] = value;
535
+ this.propertyChangeHandler(object, propertyName);
536
+ }
537
+
538
+ /**
539
+ * set streching (only for stretchingType)
540
+ * @param {Object} object
541
+ * @param {Object} params
542
+ */
543
+ _setStreching(object, { scale, objectWidth, objectHeight }) {
544
+ if (object.scaleX === 1)
545
+ this._setPropertyAndAdaptIt(object, 'width', objectWidth * scale);
546
+ else
547
+ this._setPropertyAndAdaptIt(object, 'scaleX', scale);
548
+
549
+ if (object.scaleY === 1)
550
+ this._setPropertyAndAdaptIt(object, 'height', objectHeight * scale);
551
+ else
552
+ this._setPropertyAndAdaptIt(object, 'scaleY', scale);
553
+ }
554
+
555
+ /**
556
+ * get object values for streching (only for stretchingType)
557
+ * @param {Object} object
558
+ */
559
+ _getObjectValuesForStreching(object) {
560
+ const objectWidth = this._getWidthAsNumber(object);
561
+ const objectHeight = this._getHeightAsNumber(object);
562
+ const parentWidth = this._getWidthAsNumber(object.parent);
563
+ const parentHeight = this._getHeightAsNumber(object.parent);
564
+
565
+ const scaleX = parentWidth / objectWidth;
566
+ const scaleY = parentHeight / objectHeight;
567
+
568
+ return { objectWidth, objectHeight, scaleX, scaleY };
569
+ }
570
+
571
+ /**
572
+ * inscribe handler (only for stretchingType)
573
+ * @param {Object} object
574
+ */
575
+ _inscribe(object) {
576
+ const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
577
+ const scale = Math.min(scaleX, scaleY);
578
+ this._setStreching(object, { scale, objectWidth, objectHeight });
579
+ }
580
+
581
+ /**
582
+ * circumscribe handler (only for stretchingType)
583
+ * @param {Object} object
584
+ */
585
+ _circumscribe(object) {
586
+ const { objectWidth, objectHeight, scaleX, scaleY } = this._getObjectValuesForStreching(object);
587
+ const scale = Math.max(scaleX, scaleY);
588
+ this._setStreching(object, { scale, objectWidth, objectHeight });
589
+ }
590
+ }
591
+
592
+ export default PropertyAdapter;