forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.js
More file actions
1295 lines (1053 loc) · 49.6 KB
/
component.js
File metadata and controls
1295 lines (1053 loc) · 49.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pc.extend(pc, function () {
var topMasks = [];
var _debugLogging = false;
/**
* @enum pc.ELEMENTTYPE
* @name pc.ELEMENTTYPE_GROUP
* @description A {@link pc.ElementComponent} that contains child {@link pc.ElementComponent}s.
*/
pc.ELEMENTTYPE_GROUP = 'group';
/**
* @enum pc.ELEMENTTYPE
* @name pc.ELEMENTTYPE_IMAGE
* @description A {@link pc.ElementComponent} that displays an image.
*/
pc.ELEMENTTYPE_IMAGE = 'image';
/**
* @enum pc.ELEMENTTYPE
* @name pc.ELEMENTTYPE_TEXT
* @description A {@link pc.ElementComponent} that displays text.
*/
pc.ELEMENTTYPE_TEXT = 'text';
var vecA = new pc.Vec3();
var vecB = new pc.Vec3();
var matA = new pc.Mat4();
var matB = new pc.Mat4();
var matC = new pc.Mat4();
var matD = new pc.Mat4();
/**
* @component
* @name pc.ElementComponent
* @extends pc.Component
* @class Enables an Entity to be positioned using anchors and screen coordinates under a {@link pc.ScreenComponent} or under other ElementComponents.
* Depending on its type it can be used to render images, text or just as a layout mechanism to build 2D and 3D user interfaces.
* If the component is a descendant of a {@link pc.ScreenComponent}, then the Entity's {@link pc.Entity.setLocalPosition} is in the {@link pc.ScreenComponent}'s coordinate system.
* @param {pc.ElementComponentSystem} system The ComponentSystem that created this Component
* @param {pc.Entity} entity The Entity that this Component is attached to.
* @property {String} type The type of the ElementComponent. Can be one of the following:
* <ul>
* <li>pc.ELEMENTTYPE_GROUP: The component can be used as a layout mechanism to create groups of ElementComponents e.g. panels.</li>
* <li>pc.ELEMENTTYPE_IMAGE: The component will render an image</li>
* <li>pc.ELEMENTTYPE_TEXT: The component will render text</li>
* </ul>
* @property {pc.Entity} screen The Entity with a {@link pc.ScreenComponent} that this component belongs to. This is automatically set when the component is a child of a ScreenComponent.
* @property {Number} drawOrder The draw order of the component. A higher value means that the component will be rendered on top of other components.
* @property {pc.Vec4} anchor Specifies where the left, bottom, right and top edges of the component are anchored relative to its parent. Each value
* ranges from 0 to 1. E.g. a value of [0,0,0,0] means that the element will be anchored to the bottom left of its parent. A value of [1, 1, 1, 1] means
* it will be anchored to the top right. A split anchor is when the left-right or top-bottom pairs of the anchor are not equal. In that case the component will be resized to cover that entire area. E.g. a value of [0,0,1,1] will make the component resize exactly as its parent.
* @property {pc.Vec2} pivot The position of the pivot of the component relative to its anchor. Each value ranges from 0 to 1 where [0,0] is the bottom left and [1,1] is the top right.
* @property {pc.Vec4} margin The distance from the left, bottom, right and top edges of the anchor. For example if we are using a split anchor like [0,0,1,1] and the margin is [0,0,0,0] then the component will be the same width and height as its parent.
* @property {Number} left The distance from the left edge of the anchor. Can be used in combination with a split anchor to make the component's left edge always be 'left' units away from the left.
* @property {Number} right The distance from the right edge of the anchor. Can be used in combination with a split anchor to make the component's right edge always be 'right' units away from the right.
* @property {Number} bottom The distance from the bottom edge of the anchor. Can be used in combination with a split anchor to make the component's top edge always be 'top' units away from the top.
* @property {Number} top The distance from the top edge of the anchor. Can be used in combination with a split anchor to make the component's bottom edge always be 'bottom' units away from the bottom.
* @property {Number} width The width of the element.
* @property {Number} height The height of the element.
* @property {pc.Vec3[]} screenCorners An array of 4 {@link pc.Vec3}s that represent the bottom left, bottom right, top right and top left corners of the component relative to its parent {@link pc.ScreenComponent}.
* @property {pc.Vec3[]} worldCorners An array of 4 {@link pc.Vec3}s that represent the bottom left, bottom right, top right and top left corners of the component in world space. Only works for 3D ElementComponents.
* @property {pc.Vec2[]} canvasCorners An array of 4 {@link pc.Vec2}s that represent the bottom left, bottom right, top right and top left corners of the component in canvas pixels. Only works for screen space ElementComponents.
* @property {Boolean} useInput If true then the component will receive Mouse or Touch input events.
* @property {pc.Color} color The color of the image for {@link pc.ELEMENTTYPE_IMAGE} types or the color of the text for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} opacity The opacity of the image for {@link pc.ELEMENTTYPE_IMAGE} types or the text for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} textWidth The width of the text rendered by the component. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} textHeight The height of the text rendered by the component. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} autoWidth Automatically set the width of the component to be the same as the textWidth. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} autoHeight Automatically set the height of the component to be the same as the textHeight. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} fontAsset The id of the font asset used for rendering the text. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {pc.Font} font The font used for rendering the text. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} fontSize The size of the font. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} spacing The spacing between the letters of the text. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} lineHeight The height of each line of text. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Boolean} wrapLines Whether to automatically wrap lines based on the element width. Only works for {@link pc.ELEMENTTYPE_TEXT} types, and when autoWidth is set to false.
* @property {pc.Vec2} alignment The horizontal and vertical alignment of the text. Values range from 0 to 1 where [0,0] is the bottom left and [1,1] is the top right. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {String} text The text to render. Only works for {@link pc.ELEMENTTYPE_TEXT} types.
* @property {Number} textureAsset The id of the texture asset to render. Only works for {@link pc.ELEMENTTYPE_IMAGE} types.
* @property {pc.Texture} texture The texture to render. Only works for {@link pc.ELEMENTTYPE_IMAGE} types.
* @property {Number} spriteAsset The id of the sprite asset to render. Only works for {@link pc.ELEMENTTYPE_IMAGE} types which can render either a texture or a sprite.
* @property {pc.Sprite} sprite The sprite to render. Only works for {@link pc.ELEMENTTYPE_IMAGE} types which can render either a texture or a sprite.
* @property {Number} frame The frame of the sprite to render. Only works for {@link pc.ELEMENTTYPE_IMAGE} types who have a sprite assigned.
* @property {Number} materialAsset The id of the material asset to use when rendering an image. Only works for {@link pc.ELEMENTTYPE_IMAGE} types.
* @property {pc.Material} material The material to use when rendering an image. Only works for {@link pc.ELEMENTTYPE_IMAGE} types.
* @property {pc.Vec4} rect Specifies which region of the texture to use in order to render an image. Values range from 0 to 1 and indicate u, v, width, height. Only works for {@link pc.ELEMENTTYPE_IMAGE} types.
* @property {Number} batchGroupId Assign element to a specific batch group (see {@link pc.BatchGroup}). Default value is -1 (no group).
*/
var ElementComponent = function ElementComponent (system, entity) {
this._anchor = new pc.Vec4();
this._localAnchor = new pc.Vec4();
this._pivot = new pc.Vec2();
this._width = 32;
this._height = 32;
this._margin = new pc.Vec4(0,0,-32,-32);
// the model transform used to render
this._modelTransform = new pc.Mat4();
this._screenToWorld = new pc.Mat4();
// transform that updates local position according to anchor values
this._anchorTransform = new pc.Mat4();
this._anchorDirty = true;
// transforms to calculate screen coordinates
this._parentWorldTransform = new pc.Mat4();
this._screenTransform = new pc.Mat4();
// the corners of the element relative to its screen component.
// Order is bottom left, bottom right, top right, top left
this._screenCorners = [new pc.Vec3(), new pc.Vec3(), new pc.Vec3(), new pc.Vec3()];
// canvas-space corners of the element.
// Order is bottom left, bottom right, top right, top left
this._canvasCorners = [new pc.Vec2(), new pc.Vec2(), new pc.Vec2(), new pc.Vec2()];
// the world-space corners of the element
// Order is bottom left, bottom right, top right, top left
this._worldCorners = [new pc.Vec3(), new pc.Vec3(), new pc.Vec3(), new pc.Vec3()];
this._cornersDirty = true;
this._canvasCornersDirty = true;
this._worldCornersDirty = true;
this.entity.on('insert', this._onInsert, this);
this._patch();
this.screen = null;
// if present a parent element that masks this element
this._maskEntity = null;
this._maskDepth = 0;
this._type = pc.ELEMENTTYPE_GROUP;
// element types
this._image = null;
this._text = null;
this._group = null;
// input related
this._useInput = false;
this._batchGroupId = -1;
// #ifdef DEBUG
this._batchGroup = null;
// #endif
};
ElementComponent = pc.inherits(ElementComponent, pc.Component);
pc.extend(ElementComponent.prototype, {
_patch: function () {
this.entity._sync = this._sync;
this.entity.setPosition = this._setPosition;
this.entity.setLocalPosition = this._setLocalPosition;
},
_unpatch: function () {
this.entity._sync = pc.Entity.prototype._sync;
this.entity.setPosition = pc.Entity.prototype.setPosition;
this.entity.setLocalPosition = pc.Entity.prototype.setLocalPosition;
},
_setPosition: function () {
var position = new pc.Vec3();
var invParentWtm = new pc.Mat4();
return function (x, y, z) {
if (! this.element.screen)
return pc.Entity.prototype.setPosition.call(this, x, y, z);
if (x instanceof pc.Vec3) {
position.copy(x);
} else {
position.set(x, y, z);
}
this.getWorldTransform(); // ensure hierarchy is up to date
invParentWtm.copy(this.element._screenToWorld).invert();
invParentWtm.transformPoint(position, this.localPosition);
if (! this._dirtyLocal)
this._dirtify(true);
};
}(),
_setLocalPosition: function (x, y, z) {
if (x instanceof pc.Vec3) {
this.localPosition.copy(x);
} else {
this.localPosition.set(x, y, z);
}
// update margin
var element = this.element;
var p = this.localPosition.data;
var pvt = element._pivot.data;
element._margin.data[0] = p[0] - element._width * pvt[0];
element._margin.data[2] = (element._localAnchor.data[2] - element._localAnchor.data[0]) - element._width - element._margin.data[0];
element._margin.data[1] = p[1] - element._height * pvt[1];
element._margin.data[3] = (element._localAnchor.data[3]-element._localAnchor.data[1]) - element._height - element._margin.data[1];
if (! this._dirtyLocal)
this._dirtify(true);
},
// this method overwrites GraphNode#sync and so operates in scope of the Entity.
_sync: function () {
var element = this.element;
var screen = element.screen;
if (screen) {
if (element._anchorDirty) {
var resx = 0;
var resy = 0;
var px = 0;
var py = 1;
if (this._parent && this._parent.element) {
// use parent rect
resx = this._parent.element.width;
resy = this._parent.element.height;
px = this._parent.element.pivot.x;
py = this._parent.element.pivot.y;
} else if (screen) {
// use screen rect
var resolution = screen.screen.resolution;
resx = resolution.x / screen.screen.scale;
resy = resolution.y / screen.screen.scale;
}
element._anchorTransform.setTranslate((resx*(element.anchor.x - px)), -(resy * (py-element.anchor.y)), 0);
element._anchorDirty = false;
element._calculateLocalAnchors();
}
// if element size is dirty
// recalculate its size
// WARNING: Order is important as calculateSize resets dirtyLocal
// so this needs to run before resetting dirtyLocal to false below
if (element._sizeDirty) {
element._calculateSize();
}
}
if (this._dirtyLocal) {
this.localTransform.setTRS(this.localPosition, this.localRotation, this.localScale);
// update margin
var p = this.localPosition.data;
var pvt = element._pivot.data;
element._margin.data[0] = p[0] - element._width * pvt[0];
element._margin.data[2] = (element._localAnchor.data[2] - element._localAnchor.data[0]) - element._width - element._margin.data[0];
element._margin.data[1] = p[1] - element._height * pvt[1];
element._margin.data[3] = (element._localAnchor.data[3]-element._localAnchor.data[1]) - element._height - element._margin.data[1];
this._dirtyLocal = false;
}
if (! screen) {
if (this._dirtyWorld) {
element._cornersDirty = true;
element._canvasCornersDirty = true;
element._worldCornersDirty = true;
}
return pc.Entity.prototype._sync.call(this);
}
if (this._dirtyWorld) {
if (this._parent === null) {
this.worldTransform.copy(this.localTransform);
} else {
// transform element hierarchy
if (this._parent.element) {
element._screenToWorld.mul2(this._parent.element._modelTransform, element._anchorTransform);
} else {
element._screenToWorld.copy(element._anchorTransform);
}
element._modelTransform.mul2(element._screenToWorld, this.localTransform);
if (screen) {
element._screenToWorld.mul2(screen.screen._screenMatrix, element._screenToWorld);
if (!screen.screen.screenSpace) {
element._screenToWorld.mul2(screen.worldTransform, element._screenToWorld);
}
this.worldTransform.mul2(element._screenToWorld, this.localTransform);
// update parent world transform
var parentWorldTransform = element._parentWorldTransform;
parentWorldTransform.setIdentity();
var parent = this._parent;
if (parent && parent.element && parent !== screen) {
matA.setTRS(pc.Vec3.ZERO, parent.getLocalRotation(), parent.getLocalScale());
parentWorldTransform.mul2(parent.element._parentWorldTransform, matA);
}
// update element transform
// rotate and scale around pivot
var depthOffset = vecA;
depthOffset.set(0, 0, this.localPosition.z);
var pivotOffset = vecB;
pivotOffset.set(element._absLeft + element._pivot.x * element.width, element._absBottom + element._pivot.y * element.height, 0);
matA.setTranslate(-pivotOffset.x, -pivotOffset.y, -pivotOffset.z);
matB.setTRS(depthOffset, this.getLocalRotation(), this.getLocalScale());
matC.setTranslate(pivotOffset.x, pivotOffset.y, pivotOffset.z);
element._screenTransform.mul2(element._parentWorldTransform, matC).mul(matB).mul(matA);
element._cornersDirty = true;
element._canvasCornersDirty = true;
element._worldCornersDirty = true;
} else {
this.worldTransform.copy(element._modelTransform);
}
}
this._dirtyWorld = false;
}
},
_onInsert: function (parent) {
// when the entity is reparented find a possible new screen and mask
var result = this._parseUpToScreen();
this.entity._dirtify();
this._updateScreen(result.screen);
this._dirtifyMask();
},
_dirtifyMask: function () {
var parent = this.entity;
while (parent) {
var next = parent.getParent();
if ((next === null || next.screen) && parent.element) {
if (!this.system._prerender || !this.system._prerender.length) {
this.system._prerender = [];
this.system.app.once('prerender', this._onPrerender, this);
if (_debugLogging) console.log('register prerender');
}
var i = this.system._prerender.indexOf(this.entity);
if (i >= 0) {
this.system._prerender.splice(i, 1);
}
var j = this.system._prerender.indexOf(parent);
if (j < 0) {
this.system._prerender.push(parent);
}
if (_debugLogging) console.log('set prerender root to: ' + parent.name);
}
parent = next;
}
},
_onPrerender: function () {
var ref = 0;
for (var i = 0; i < this.system._prerender.length; i++) {
var mask = this.system._prerender[i];
if (_debugLogging) console.log('prerender from: ' + mask.name);
ref = mask.element.syncMask(ref)+1;
}
this.system._prerender.length = 0;
},
_updateScreen: function (screen) {
if (this.screen && this.screen !== screen) {
this.screen.screen.off('set:resolution', this._onScreenResize, this);
this.screen.screen.off('set:referenceresolution', this._onScreenResize, this);
this.screen.screen.off('set:scaleblend', this._onScreenResize, this);
this.screen.screen.off('set:screenspace', this._onScreenSpaceChange, this);
}
this.screen = screen;
if (this.screen) {
this.screen.screen.on('set:resolution', this._onScreenResize, this);
this.screen.screen.on('set:referenceresolution', this._onScreenResize, this);
this.screen.screen.on('set:scaleblend', this._onScreenResize, this);
this.screen.screen.on('set:screenspace', this._onScreenSpaceChange, this);
}
this._calculateSize();
this.fire('set:screen', this.screen);
this._anchorDirty = true;
// update all child screens
var children = this.entity.getChildren();
for (var i = 0, l = children.length; i < l; i++) {
if (children[i].element) children[i].element._updateScreen(screen);
}
// calculate draw order
if (this.screen) this.screen.screen.syncDrawOrder();
},
syncMask: function (ref) {
var result = this._parseUpToScreen();
return this._updateMask(result.mask, ref);
},
_setMaskedBy: function (mask) {
var i, mi, len;
var elem = this._image || this._text;
if (!elem) return;
if (mask) {
// if (elem._maskedBy && elem._maskedBy !== mask) {
// // already masked by something else
// }
var ref = mask.element._image._maskRef;
if (_debugLogging) console.log("masking: " + this.entity.name + " with " + ref);
var sp = new pc.StencilParameters({
ref: ref,
func: pc.FUNC_EQUAL,
});
for (i = 0, len = elem._model.meshInstances.length; i<len; i++) {
mi = elem._model.meshInstances[i];
mi.stencilFront = mi.stencilBack = sp;
}
elem._maskedBy = mask;
} else {
if (_debugLogging) console.log("no masking on: " + this.entity.name);
// remove mask
// restore default material
for (i = 0, len = elem._model.meshInstances.length; i<len; i++) {
mi = elem._model.meshInstances[i];
mi.stencilFront = mi.stencilBack = null;
}
elem._maskedBy = null;
}
},
_getMaskDepth: function () {
var depth = 1;
var parent = this.entity;
while(parent) {
parent = parent.getParent();
if (parent && parent.element && parent.element.mask) {
depth++;
}
}
return depth;
},
// set the mask ancestor on this entity
_updateMask: function (mask, ref) {
var i, l, sp, children;
if (!ref) ref = 1;
if (mask) {
var material;
this._maskEntity = mask;
this._setMaskedBy(mask);
if (this.mask) {
if (_debugLogging) console.log("masking: " + this.entity.name + " with " + ref);
sp = new pc.StencilParameters({
ref: ref++,
func: pc.FUNC_EQUAL,
zpass: pc.STENCILOP_INCREMENT
});
this._image._meshInstance.stencilFront = sp;
this._image._meshInstance.stencilBack = sp;
this._image._maskRef = ref;
if (_debugLogging) console.log("masking from: " + this.entity.name + " with " + ref);
mask = this.entity;
}
// recurse through all children
children = this.entity.getChildren();
for (i = 0, l = children.length; i < l; i++) {
if (children[i].element) {
children[i].element._updateMask(mask, ref);
}
}
} else {
// clearing mask
this._setMaskedBy(null);
// if this is mask we still need to mask children
if (this.mask) {
sp = new pc.StencilParameters({
func: pc.FUNC_ALWAYS,
zpass: pc.STENCILOP_REPLACE,
ref: ref,
});
this._image._meshInstance.stencilFront = sp;
this._image._meshInstance.stencilBack = sp;
this._image._maskRef = ref;
if (_debugLogging) console.log("masking from: " + this.entity.name + " with " + ref);
mask = this.entity;
}
this._maskEntity = null;
// recurse through all children
children = this.entity.getChildren();
for (i = 0, l = children.length; i < l; i++) {
if (children[i].element) {
children[i].element._updateMask(mask, ref);
}
}
}
return ref;
},
// search up the parent hierarchy until we reach a screen
// this screen is the parent screen
// also searches for masked elements to get the relevant mask
_parseUpToScreen: function () {
var result = {
screen: null,
mask: null
};
var parent = this.entity._parent;
while(parent && !parent.screen) {
if (parent.element && parent.element.mask) {
// mask entity
if (!result.mask) result.mask = parent;
}
parent = parent.parent;
}
if (parent && parent.screen) result.screen = parent;
return result;
},
_onScreenResize: function (res) {
this._anchorDirty = true;
this._cornersDirty = true;
this._worldCornersDirty = true;
this._calculateSize();
this.fire('screen:set:resolution', res);
},
_onScreenSpaceChange: function () {
this.fire('screen:set:screenspace', this.screen.screen.screenSpace);
},
// store pixel positions of anchor relative to current parent resolution
_calculateLocalAnchors: function () {
var resx = 1000;
var resy = 1000;
var parent = this.entity._parent;
if (parent && parent.element) {
resx = parent.element.width;
resy = parent.element.height;
} else if (this.screen) {
var res = this.screen.screen.resolution;
var scale = this.screen.screen.scale;
resx = res.x / scale;
resy = res.y / scale;
}
this._localAnchor.set(
this._anchor.x*resx,
this._anchor.y*resy,
this._anchor.z*resx,
this._anchor.w*resy
);
},
// internal - apply offset x,y to local position and find point in world space
getOffsetPosition: function (x, y) {
var p = this.entity.getLocalPosition().clone();
p.x += x;
p.y += y;
this._screenToWorld.transformPoint(p, p);
return p;
},
onEnable: function () {
ElementComponent._super.onEnable.call(this);
if (this._image) this._image.onEnable();
if (this._text) this._text.onEnable();
if (this._group) this._group.onEnable();
if (this.useInput && this.system.app.elementInput) {
this.system.app.elementInput.addElement(this);
}
if (this.mask) {
var maskDepth = this._getMaskDepth();
if (maskDepth === 1) {
this._topMask = true;
if (topMasks.indexOf(this) < 0) topMasks.push(this);
}
}
},
onDisable: function () {
ElementComponent._super.onDisable.call(this);
if (this._image) this._image.onDisable();
if (this._text) this._text.onDisable();
if (this._group) this._group.onDisable();
if (this.system.app.elementInput && this.useInput) {
this.system.app.elementInput.removeElement(this);
}
if (this._topMask) {
var index = topMasks.indexOf(this);
if (index >= 0) topMasks.splice(index, 1);
this._topMask = false;
}
},
onRemove: function () {
this.entity.off('insert', this._onInsert, this);
this._unpatch();
if (this._image) this._image.destroy();
if (this._text) this._text.destroy();
if (this.system.app.elementInput && this.useInput) {
this.system.app.elementInput.removeElement(this);
}
if (this._topMask) {
var index = topMasks.indexOf(this);
if (index >= 0) topMasks.splice(index, 1);
this._topMask = false;
}
},
// recalculates
// localAnchor, width, height, (local position is updated if anchors are split)
// assumes these properties are up to date
// _margin
_calculateSize: function () {
// can't calculate if local anchors are wrong
if (!this.entity._parent && !this.screen) return;
this._calculateLocalAnchors();
var p = this.entity.getLocalPosition();
this._setWidth(this._absRight - this._absLeft);
this._setHeight(this._absTop - this._absBottom);
p.x = this._margin.data[0] + this._width * this._pivot.data[0];
p.y = this._margin.data[1] + this._height * this._pivot.data[1];
this.entity.setLocalPosition(p);
this._sizeDirty = false;
},
// internal set width without updating margin
_setWidth: function (w) {
this._width = w;
var i,l;
var c = this.entity._children;
for (i = 0, l = c.length; i < l; i++) {
if (c[i].element) {
c[i].element._anchorDirty = true;
c[i].element._sizeDirty = true;
}
}
this.fire('set:width', this._width);
this.fire('resize', this._width, this._height);
},
// internal set height without updating margin
_setHeight: function (h) {
this._height = h;
var i,l;
var c = this.entity._children;
for (i = 0, l = c.length; i < l; i++) {
if (c[i].element) {
c[i].element._anchorDirty = true;
c[i].element._sizeDirty = true;
}
}
this.fire('set:height', this._height);
this.fire('resize', this._width, this._height);
}
});
Object.defineProperty(ElementComponent.prototype, "type", {
get: function () {
return this._type;
},
set: function (value) {
if (value !== this._type) {
this._type = value;
if (this._image) {
this._image.destroy();
this._image = null;
}
if (this._text) {
this._text.destroy();
this._text = null;
}
if (value === pc.ELEMENTTYPE_IMAGE) {
this._image = new pc.ImageElement(this);
} else if (value === pc.ELEMENTTYPE_TEXT) {
this._text = new pc.TextElement(this);
}
}
}
});
Object.defineProperty(ElementComponent.prototype, "drawOrder", {
get: function () {
return this._drawOrder;
},
set: function (value) {
this._drawOrder = value;
this.fire('set:draworder', this._drawOrder);
}
});
Object.defineProperty(ElementComponent.prototype, "_absLeft", {
get: function () {
return this._localAnchor.data[0] + this._margin.data[0];
}
});
Object.defineProperty(ElementComponent.prototype, "_absRight", {
get: function () {
return this._localAnchor.data[2] - this._margin.data[2];
}
});
Object.defineProperty(ElementComponent.prototype, "_absTop", {
get: function () {
return this._localAnchor.data[3] - this._margin.data[3];
}
});
Object.defineProperty(ElementComponent.prototype, "_absBottom", {
get: function () {
return this._localAnchor.data[1] + this._margin.data[1];
}
});
Object.defineProperty(ElementComponent.prototype, "margin", {
get: function () {
return this._margin;
},
set: function (value) {
this._margin.copy(value);
this._calculateSize();
}
});
Object.defineProperty(ElementComponent.prototype, "left", {
get: function () {
return this._margin.data[0];
},
set: function (value) {
this._margin.data[0] = value;
var p = this.entity.getLocalPosition();
var wr = this._absRight;
var wl = this._localAnchor.data[0] + value;
this._setWidth(wr - wl);
p.x = value + this._width * this._pivot.data[0];
this.entity.setLocalPosition(p);
}
});
Object.defineProperty(ElementComponent.prototype, "right", {
get: function () {
return this._margin.data[2];
},
set: function (value) {
this._margin.data[2] = value;
// update width
var p = this.entity.getLocalPosition();
var wl = this._absLeft;
var wr = this._localAnchor.data[2] - value;
this._setWidth(wr - wl);
// update position
p.x = (this._localAnchor.data[2]-this._localAnchor.data[0]) - value - (this._width*(1-this._pivot.data[0]));
this.entity.setLocalPosition(p);
}
});
Object.defineProperty(ElementComponent.prototype, "top", {
get: function () {
return this._margin.data[3];
},
set: function (value) {
this._margin.data[3] = value;
var p = this.entity.getLocalPosition();
var wb = this._absBottom;
var wt = this._localAnchor.data[3] - value;
this._setHeight(wt-wb);
p.y = (this._localAnchor.data[3] - this._localAnchor.data[1]) - value - this._height*(1-this._pivot.data[1]);
this.entity.setLocalPosition(p);
}
});
Object.defineProperty(ElementComponent.prototype, "bottom", {
get: function () {
return this._margin.data[1];
},
set: function (value) {
this._margin.data[1] = value;
var p = this.entity.getLocalPosition();
var wt = this._absTop;
var wb = this._localAnchor.data[1] + value;
this._setHeight(wt-wb);
p.y = value + this._height*this._pivot.data[1];
this.entity.setLocalPosition(p);
}
});
Object.defineProperty(ElementComponent.prototype, "width", {
get: function () {
return this._width;
},
set: function (value) {
this._width = value;
// reset margin data
var p = this.entity.getLocalPosition().data;
var pvt = this._pivot.data;
this._margin.data[0] = p[0] - this._width * pvt[0];
this._margin.data[2] = (this._localAnchor.data[2] - this._localAnchor.data[0]) - this._width - this._margin.data[0];
var i,l;
var c = this.entity._children;
for (i = 0, l = c.length; i < l; i++) {
if (c[i].element) {
c[i].element._anchorDirty = true;
c[i].element._sizeDirty = true;
}
}
this.fire('set:width', this._width);
this.fire('resize', this._width, this._height);
}
});
Object.defineProperty(ElementComponent.prototype, "height", {
get: function () {
return this._height;
},
set: function (value) {
this._height = value;
// reset margin data
var p = this.entity.getLocalPosition().data;
var pvt = this._pivot.data;
this._margin.data[1] = p[1] - this._height * pvt[1];
this._margin.data[3] = (this._localAnchor.data[3]-this._localAnchor.data[1]) - this._height - this._margin.data[1];
var i,l;
var c = this.entity._children;
for (i = 0, l = c.length; i < l; i++) {
if (c[i].element) {
c[i].element._anchorDirty = true;
c[i].element._sizeDirty = true;
}
}
this.fire('set:height', this._height);
this.fire('resize', this._width, this._height);
}
});
Object.defineProperty(ElementComponent.prototype, "pivot", {
get: function () {
return this._pivot;
},
set: function (value) {
var prevX = this._pivot.x;
var prevY = this._pivot.y;
if (value instanceof pc.Vec2) {
this._pivot.set(value.x, value.y);
} else {
this._pivot.set(value[0], value[1]);
}
var mx = this._margin.data[0] + this._margin.data[2];
var dx = this._pivot.x - prevX;
this._margin.data[0] += mx * dx;
this._margin.data[2] -= mx * dx;
var my = this._margin.data[1] + this._margin.data[3];
var dy = this._pivot.y - prevY;
this._margin.data[1] += my * dy;
this._margin.data[3] -= my * dy;
this._onScreenResize();
this.fire('set:pivot', this._pivot);
}
});
Object.defineProperty(ElementComponent.prototype, "anchor", {
get: function () {
return this._anchor;
},
set: function (value) {
if (value instanceof pc.Vec4) {
this._anchor.set(value.x, value.y, value.z, value.w);
} else {
this._anchor.set(value[0], value[1], value[2], value[3]);
}
if (!this.entity._parent && !this.screen) {
this._calculateLocalAnchors();
} else {
this._calculateSize();
}
this._anchorDirty = true;
if (! this.entity._dirtyLocal)
this.entity._dirtify(true);
this.fire('set:anchor', this._anchor);
}
});
// Returns the 4 corners of the element relative to its screen component.
// Only works for elements that have a screen.
// Order is bottom left, bottom right, top right, top left.
Object.defineProperty(ElementComponent.prototype, 'screenCorners', {
get: function () {
if (! this._cornersDirty || ! this.screen)
return this._screenCorners;
var parentBottomLeft = this.entity.parent && this.entity.parent.element && this.entity.parent.element.screenCorners[0];
// init corners
this._screenCorners[0].set(this._absLeft, this._absBottom, 0);
this._screenCorners[1].set(this._absRight, this._absBottom, 0);
this._screenCorners[2].set(this._absRight, this._absTop, 0);
this._screenCorners[3].set(this._absLeft, this._absTop, 0);