-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Container.ts
1382 lines (1190 loc) · 44.3 KB
/
Container.ts
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
import EventEmitter from 'eventemitter3';
import { Color, type ColorSource } from '../../color/Color';
import { cullingMixin } from '../../culling/cullingMixin';
import { Matrix } from '../../maths/matrix/Matrix';
import { DEG_TO_RAD, RAD_TO_DEG } from '../../maths/misc/const';
import { ObservablePoint } from '../../maths/point/ObservablePoint';
import { uid } from '../../utils/data/uid';
import { deprecation, v8_0_0 } from '../../utils/logging/deprecation';
import { childrenHelperMixin } from './container-mixins/childrenHelperMixin';
import { effectsMixin } from './container-mixins/effectsMixin';
import { findMixin } from './container-mixins/findMixin';
import { measureMixin } from './container-mixins/measureMixin';
import { onRenderMixin } from './container-mixins/onRenderMixin';
import { sortMixin } from './container-mixins/sortMixin';
import { toLocalGlobalMixin } from './container-mixins/toLocalGlobalMixin';
import { RenderGroup } from './RenderGroup';
import { assignWithIgnore } from './utils/assignWithIgnore';
import type { Size } from '../../maths/misc/Size';
import type { PointData } from '../../maths/point/PointData';
import type { Rectangle } from '../../maths/shapes/Rectangle';
import type { BLEND_MODES } from '../../rendering/renderers/shared/state/const';
import type { Dict } from '../../utils/types';
import type { Optional } from './container-mixins/measureMixin';
import type { DestroyOptions } from './destroyTypes';
/**
* This is where you'll find all the display objects available in Pixi.
*
* All display objects inherit from the {@link scene.Container} class. You can use a `Container` for simple grouping of
* other display objects. Here's all the available display object classes.
*
* - {@link scene.Container} is the base class for all display objects that act as a container for other objects.
* - {@link scene.Sprite} is a display object that uses a texture
* - {@link scene.AnimatedSprite} is a sprite that can play animations
* - {@link scene.TilingSprite} a fast way of rendering a tiling image
* - {@link scene.NineSliceSprite} allows you to stretch a texture using 9-slice scaling
* - {@link scene.Graphics} is a graphic object that can be drawn to the screen.
* - {@link scene.Mesh} empowers you to have maximum flexibility to render any kind of visuals you can think of
* - {@link scene.MeshSimple} mimics Mesh, providing easy-to-use constructor arguments
* - {@link scene.MeshPlane} allows you to draw a texture across several points and then manipulate these points
* - {@link scene.MeshRope} allows you to draw a texture across several points and then manipulate these points
* - {@link scene.Text} render text using custom fonts
* - {@link scene.BitmapText} render text using a bitmap font
* - {@link scene.HTMLText} render text using HTML and CSS
* @namespace scene
*/
// as pivot and skew are the least used properties of a container, we can use this optimisation
// to avoid allocating lots of unnecessary objects for them.
const defaultSkew = new ObservablePoint(null);
const defaultPivot = new ObservablePoint(null);
const defaultScale = new ObservablePoint(null, 1, 1);
export interface ContainerEvents extends PixiMixins.ContainerEvents
{
added: [container: Container];
childAdded: [child: Container, container: Container, index: number];
removed: [container: Container];
childRemoved: [child: Container, container: Container, index: number];
destroyed: [container: Container];
}
type AnyEvent = {
// The following is a hack to allow any custom event while maintaining type safety.
// For some reason, the tsc compiler gets angry about error TS1023
// "An index signature parameter type must be either 'string' or 'number'."
// This is really odd since ({}&string) should interpret as string, but then again
// there is some black magic behind why this works in the first place.
// Closest thing to an explanation:
// https://stackoverflow.com/questions/70144348/why-does-a-union-of-type-literals-and-string-cause-ide-code-completion-wh
//
// Side note, we disable @typescript-eslint/ban-types since {}&string is the only syntax that works.
// Nor of the Record/unknown/never alternatives work.
// eslint-disable-next-line @typescript-eslint/ban-types
[K: ({} & string) | ({} & symbol)]: any;
};
export const UPDATE_COLOR = 0b0001;
export const UPDATE_BLEND = 0b0010;
export const UPDATE_VISIBLE = 0b0100;
export const UPDATE_TRANSFORM = 0b1000;
export interface UpdateTransformOptions
{
x: number;
y: number;
scaleX: number;
scaleY: number;
rotation: number;
skewX: number;
skewY: number;
pivotX: number;
pivotY: number;
}
/**
* Constructor options used for `Container` instances.
* ```js
* const container = new Container({
* position: new Point(100, 200),
* scale: new Point(2, 2),
* rotation: Math.PI / 2,
* });
* ```
* @memberof scene
* @see scene.Container
*/
export interface ContainerOptions extends PixiMixins.ContainerOptions
{
/** @see scene.Container#isRenderGroup */
isRenderGroup?: boolean;
/** @see scene.Container#blendMode */
blendMode?: BLEND_MODES;
/** @see scene.Container#tint */
tint?: ColorSource;
/** @see scene.Container#alpha */
alpha?: number;
/** @see scene.Container#angle */
angle?: number;
/** @see scene.Container#children */
children?: Container[];
/** @see scene.Container#parent */
parent?: Container;
/** @see scene.Container#renderable */
renderable?: boolean;
/** @see scene.Container#rotation */
rotation?: number;
/** @see scene.Container#scale */
scale?: PointData | number;
/** @see scene.Container#pivot */
pivot?: PointData | number;
/** @see scene.Container#position */
position?: PointData;
/** @see scene.Container#skew */
skew?: PointData;
/** @see scene.Container#visible */
visible?: boolean;
/** @see scene.Container#culled */
culled?: boolean;
/** @see scene.Container#x */
x?: number;
/** @see scene.Container#y */
y?: number;
/** @see scene.Container#boundArea */
boundsArea?: Rectangle;
}
export interface Container
extends Omit<PixiMixins.Container, keyof EventEmitter<ContainerEvents & AnyEvent>>,
EventEmitter<ContainerEvents & AnyEvent> { }
/**
* Container is a general-purpose display object that holds children. It also adds built-in support for advanced
* rendering features like masking and filtering.
*
* It is the base class of all display objects that act as a container for other objects, including Graphics
* and Sprite.
*
* <details id="transforms">
*
* <summary>Transforms</summary>
*
* The [transform]{@link scene.Container#transform} of a display object describes the projection from its
* local coordinate space to its parent's local coordinate space. The following properties are derived
* from the transform:
*
* <table>
* <thead>
* <tr>
* <th>Property</th>
* <th>Description</th>
* </tr>
* </thead>
* <tbody>
* <tr>
* <td>[pivot]{@link scene.Container#pivot}</td>
* <td>
* Invariant under rotation, scaling, and skewing. The projection of into the parent's space of the pivot
* is equal to position, regardless of the other three transformations. In other words, It is the center of
* rotation, scaling, and skewing.
* </td>
* </tr>
* <tr>
* <td>[position]{@link scene.Container#position}</td>
* <td>
* Translation. This is the position of the [pivot]{@link scene.Container#pivot} in the parent's local
* space. The default value of the pivot is the origin (0,0). If the top-left corner of your display object
* is (0,0) in its local space, then the position will be its top-left corner in the parent's local space.
* </td>
* </tr>
* <tr>
* <td>[scale]{@link scene.Container#scale}</td>
* <td>
* Scaling. This will stretch (or compress) the display object's projection. The scale factors are along the
* local coordinate axes. In other words, the display object is scaled before rotated or skewed. The center
* of scaling is the [pivot]{@link scene.Container#pivot}.
* </td>
* </tr>
* <tr>
* <td>[rotation]{@link scene.Container#rotation}</td>
* <td>
* Rotation. This will rotate the display object's projection by this angle (in radians).
* </td>
* </tr>
* <tr>
* <td>[skew]{@link scene.Container#skew}</td>
* <td>
* <p>Skewing. This can be used to deform a rectangular display object into a parallelogram.</p>
* <p>
* In PixiJS, skew has a slightly different behaviour than the conventional meaning. It can be
* thought of the net rotation applied to the coordinate axes (separately). For example, if "skew.x" is
* ⍺ and "skew.y" is β, then the line x = 0 will be rotated by ⍺ (y = -x*cot⍺) and the line y = 0 will be
* rotated by β (y = x*tanβ). A line y = x*tanϴ (i.e. a line at angle ϴ to the x-axis in local-space) will
* be rotated by an angle between ⍺ and β.
* </p>
* <p>
* It can be observed that if skew is applied equally to both axes, then it will be equivalent to applying
* a rotation. Indeed, if "skew.x" = -ϴ and "skew.y" = ϴ, it will produce an equivalent of "rotation" = ϴ.
* </p>
* <p>
* Another quite interesting observation is that "skew.x", "skew.y", rotation are commutative operations. Indeed,
* because rotation is essentially a careful combination of the two.
* </p>
* </td>
* </tr>
* <tr>
* <td>[angle]{@link scene.Container#angle}</td>
* <td>Rotation. This is an alias for [rotation]{@link scene.Container#rotation}, but in degrees.</td>
* </tr>
* <tr>
* <td>[x]{@link scene.Container#x}</td>
* <td>Translation. This is an alias for position.x!</td>
* </tr>
* <tr>
* <td>[y]{@link scene.Container#y}</td>
* <td>Translation. This is an alias for position.y!</td>
* </tr>
* <tr>
* <td>[width]{@link scene.Container#width}</td>
* <td>
* Implemented in [Container]{@link scene.Container}. Scaling. The width property calculates scale.x by dividing
* the "requested" width by the local bounding box width. It is indirectly an abstraction over scale.x, and there
* is no concept of user-defined width.
* </td>
* </tr>
* <tr>
* <td>[height]{@link scene.Container#height}</td>
* <td>
* Implemented in [Container]{@link scene.Container}. Scaling. The height property calculates scale.y by dividing
* the "requested" height by the local bounding box height. It is indirectly an abstraction over scale.y, and there
* is no concept of user-defined height.
* </td>
* </tr>
* </tbody>
* </table>
* </details>
*
* <details id="alpha">
* <summary>Alpha</summary>
*
* This alpha sets a display object's **relative opacity** w.r.t its parent. For example, if the alpha of a display
* object is 0.5 and its parent's alpha is 0.5, then it will be rendered with 25% opacity (assuming alpha is not
* applied on any ancestor further up the chain).
* </details>
*
* <details id="visible">
* <summary>Renderable vs Visible</summary>
*
* The `renderable` and `visible` properties can be used to prevent a display object from being rendered to the
* screen. However, there is a subtle difference between the two. When using `renderable`, the transforms of the display
* object (and its children subtree) will continue to be calculated. When using `visible`, the transforms will not
* be calculated.
* ```ts
* import { BlurFilter, Container, Graphics, Sprite } from 'pixi.js';
*
* const container = new Container();
* const sprite = Sprite.from('https://s3-us-west-2.amazonaws.com/s.cdpn.io/693612/IaUrttj.png');
*
* sprite.width = 512;
* sprite.height = 512;
*
* // Adds a sprite as a child to this container. As a result, the sprite will be rendered whenever the container
* // is rendered.
* container.addChild(sprite);
*
* // Blurs whatever is rendered by the container
* container.filters = [new BlurFilter()];
*
* // Only the contents within a circle at the center should be rendered onto the screen.
* container.mask = new Graphics()
* .beginFill(0xffffff)
* .drawCircle(sprite.width / 2, sprite.height / 2, Math.min(sprite.width, sprite.height) / 2)
* .endFill();
* ```
*
* </details>
*
* <details id="renderGroup">
* <summary>RenderGroup</summary>
*
* In PixiJS v8, containers can be set to operate in 'render group mode',
* transforming them into entities akin to a stage in traditional rendering paradigms.
* A render group is a root renderable entity, similar to a container,
* but it's rendered in a separate pass with its own unique set of rendering instructions.
* This approach enhances rendering efficiency and organization, particularly in complex scenes.
*
* You can enable render group mode on any container using container.enableRenderGroup()
* or by initializing a new container with the render group property set to true (new Container({isRenderGroup: true})).
* The method you choose depends on your specific use case and setup requirements.
*
* An important aspect of PixiJS’s rendering process is the automatic treatment of rendered scenes as render groups.
* This conversion streamlines the rendering process, but understanding when and how this happens is crucial
* to fully leverage its benefits.
*
* One of the key advantages of using render groups is the performance efficiency in moving them. Since transformations
* are applied at the GPU level, moving a render group, even one with complex and numerous children,
* doesn't require recalculating the rendering instructions or performing transformations on each child.
* This makes operations like panning a large game world incredibly efficient.
*
* However, it's crucial to note that render groups do not batch together.
* This means that turning every container into a render group could actually slow things down,
* as each render group is processed separately. It's best to use render groups judiciously, at a broader level,
* rather than on a per-child basis.
* This approach ensures you get the performance benefits without overburdening the rendering process.
*
* RenderGroups maintain their own set of rendering instructions,
* ensuring that changes or updates within a render group don't affect the rendering
* instructions of its parent or other render groups.
* This isolation ensures more stable and predictable rendering behavior.
*
* Additionally, renderGroups can be nested, allowing for powerful options in organizing different aspects of your scene.
* This feature is particularly beneficial for separating complex game graphics from UI elements,
* enabling intricate and efficient scene management in complex applications.
*
* This means that Containers have 3 levels of matrix to be mindful of:
*
* 1. localTransform, this is the transform of the container based on its own properties
* 2. groupTransform, this it the transform of the container relative to the renderGroup it belongs too
* 3. worldTransform, this is the transform of the container relative to the Scene being rendered
* </details>
* @memberof scene
*/
export class Container extends EventEmitter<ContainerEvents & AnyEvent>
{
/**
* Mixes all enumerable properties and methods from a source object to Container.
* @param source - The source of properties and methods to mix in.
*/
public static mixin(source: Dict<any>): void
{
Object.defineProperties(Container.prototype, Object.getOwnPropertyDescriptors(source));
}
/** @private */
public uid: number = uid('renderable');
/** @private */
public _updateFlags = 0b1111;
// the render group this container owns
/** @private */
public renderGroup: RenderGroup = null;
// the render group this container belongs to
/** @private */
public parentRenderGroup: RenderGroup = null;
// the index of the container in the render group
/** @private */
public parentRenderGroupIndex: number = 0;
// set to true if the container has changed. It is reset once the changes have been applied
// by the transform system
// its here to stop ensure that when things change, only one update gets registers with the transform system
/** @private */
public didChange = false;
// same as above, but for the renderable
/** @private */
public didViewUpdate = false;
// how deep is the container relative to its render group..
// unless the element is the root render group - it will be relative to its parent
/** @private */
public relativeRenderGroupDepth = 0;
/**
* The array of children of this container.
* @readonly
*/
public children: Container[] = [];
/** The display object container that contains this display object. */
public parent: Container = null;
// used internally for changing up the render order.. mainly for masks and filters
// TODO setting this should cause a rebuild??
/** @private */
public includeInBuild = true;
/** @private */
public measurable = true;
/** @private */
public isSimple = true;
// / /////////////Transform related props//////////////
// used by the transform system to check if a container needs to be updated that frame
// if the tick matches the current transform system tick, it is not updated again
/**
* @internal
* @ignore
*/
public updateTick = -1;
/**
* Current transform of the object based on local factors: position, scale, other stuff.
* @readonly
*/
public localTransform: Matrix = new Matrix();
/**
* The relative group transform is a transform relative to the render group it belongs too. It will include all parent
* transforms and up to the render group (think of it as kind of like a stage - but the stage can be nested).
* If this container is is self a render group matrix will be relative to its parent render group
* @readonly
*/
public relativeGroupTransform: Matrix = new Matrix();
/**
* The group transform is a transform relative to the render group it belongs too.
* If this container is render group then this will be an identity matrix. other wise it
* will be the same as the relativeGroupTransform.
* Use this value when actually rendering things to the screen
* @readonly
*/
public groupTransform: Matrix = this.relativeGroupTransform;
// the global transform taking into account the render group and all parents
private _worldTransform: Matrix;
/** If the object has been destroyed via destroy(). If true, it should not be used. */
public destroyed = false;
// transform data..
/**
* The coordinate of the object relative to the local coordinates of the parent.
* @internal
* @ignore
*/
public _position: ObservablePoint = new ObservablePoint(this, 0, 0);
/**
* The scale factor of the object.
* @internal
* @ignore
*/
public _scale: ObservablePoint = defaultScale;
/**
* The pivot point of the container that it rotates around.
* @internal
* @ignore
*/
public _pivot: ObservablePoint = defaultPivot;
/**
* The skew amount, on the x and y axis.
* @internal
* @ignore
*/
public _skew: ObservablePoint = defaultSkew;
/**
* The X-coordinate value of the normalized local X axis,
* the first column of the local transformation matrix without a scale.
* @internal
* @ignore
*/
public _cx = 1;
/**
* The Y-coordinate value of the normalized local X axis,
* the first column of the local transformation matrix without a scale.
* @internal
* @ignore
*/
public _sx = 0;
/**
* The X-coordinate value of the normalized local Y axis,
* the second column of the local transformation matrix without a scale.
* @internal
* @ignore
*/
public _cy = 0;
/**
* The Y-coordinate value of the normalized local Y axis,
* the second column of the local transformation matrix without a scale.
* @internal
* @ignore
*/
public _sy = 1;
/**
* The rotation amount.
* @internal
* @ignore
*/
private _rotation = 0;
// / COLOR related props //////////////
// color stored as ABGR
public localColor = 0xFFFFFF;
public localAlpha = 1;
public groupAlpha = 1; // A
public groupColor = 0xFFFFFF; // BGR
public groupColorAlpha = 0xFFFFFFFF; // ABGR
// / BLEND related props //////////////
/**
* @internal
* @ignore
*/
public localBlendMode: BLEND_MODES = 'inherit';
/**
* @internal
* @ignore
*/
public groupBlendMode: BLEND_MODES = 'normal';
// / VISIBILITY related props //////////////
// visibility
// 0b11
// first bit is visible, second bit is renderable
/**
* This property holds three bits: culled, visible, renderable
* the third bit represents culling (0 = culled, 1 = not culled) 0b100
* the second bit represents visibility (0 = not visible, 1 = visible) 0b010
* the first bit represents renderable (0 = renderable, 1 = not renderable) 0b001
* @internal
* @ignore
*/
public localDisplayStatus = 0b111; // 0b11 | 0b10 | 0b01 | 0b00
/**
* @internal
* @ignore
*/
public globalDisplayStatus = 0b111; // 0b11 | 0b10 | 0b01 | 0b00
public renderPipeId: string;
/**
* An optional bounds area for this container. Setting this rectangle will stop the renderer
* from recursively measuring the bounds of each children and instead use this single boundArea.
* This is great for optimisation! If for example you have a 1000 spinning particles and you know they all sit
* within a specific bounds, then setting it will mean the renderer will not need to measure the
* 1000 children to find the bounds. Instead it will just use the bounds you set.
*/
public boundsArea: Rectangle;
/**
* A value that increments each time the container is modified
* the first 12 bits represent the container changes (eg transform, alpha, visible etc)
* the second 12 bits represent the view changes (eg texture swap, geometry change etc)
*
* view container
* [000000000000][00000000000]
* @ignore
*/
public _didChangeId = 0;
/**
* property that tracks if the container transform has changed
* @ignore
*/
private _didLocalTransformChangeId = -1;
constructor(options: ContainerOptions = {})
{
super();
assignWithIgnore(this, options, {
children: true,
parent: true,
effects: true,
});
options.children?.forEach((child) => this.addChild(child));
this.effects = [];
options.parent?.addChild(this);
}
/**
* Adds one or more children to the container.
*
* Multiple items can be added like so: `myContainer.addChild(thingOne, thingTwo, thingThree)`
* @param {...Container} children - The Container(s) to add to the container
* @returns {Container} - The first child that was added.
*/
public addChild<U extends Container[]>(...children: U): U[0]
{
// #if _DEBUG
if (!this.allowChildren)
{
deprecation(v8_0_0, 'addChild: Only Containers will be allowed to add children in v8.0.0');
}
// #endif
if (children.length > 1)
{
// loop through the array and add all children
for (let i = 0; i < children.length; i++)
{
this.addChild(children[i]);
}
return children[0];
}
const child = children[0];
if (child.parent === this)
{
this.children.splice(this.children.indexOf(child), 1);
this.children.push(child);
if (this.parentRenderGroup)// && !this.isRenderGroupRoot)
{
this.parentRenderGroup.structureDidChange = true;
}
return child;
}
if (child.parent)
{
// TODO Optimisation...if the parent has the same render group, this does not need to change!
child.parent.removeChild(child);
}
this.children.push(child);
if (this.sortableChildren) this.sortDirty = true;
child.parent = this;
child.didChange = true;
child.didViewUpdate = false;
// TODO - OPtimise this? could check what the parent has set?
child._updateFlags = 0b1111;
const renderGroup = this.renderGroup || this.parentRenderGroup;
if (renderGroup)
{
renderGroup.addChild(child);
}
this.emit('childAdded', child, this, this.children.length - 1);
child.emit('added', this);
if (child._zIndex !== 0)
{
child.depthOfChildModified();
}
return child;
}
/**
* Removes one or more children from the container.
* @param {...Container} children - The Container(s) to remove
* @returns {Container} The first child that was removed.
*/
public removeChild<U extends Container[]>(...children: U): U[0]
{
// if there is only one argument we can bypass looping through the them
if (children.length > 1)
{
// loop through the arguments property and remove all children
for (let i = 0; i < children.length; i++)
{
this.removeChild(children[i]);
}
return children[0];
}
const child = children[0];
const index = this.children.indexOf(child);
if (index > -1)
{
this.children.splice(index, 1);
if (this.renderGroup)
{
this.renderGroup.removeChild(child);
}
else if (this.parentRenderGroup)
{
this.parentRenderGroup.removeChild(child);
}
child.parent = null;
this.emit('childRemoved', child, this, index);
child.emit('removed', this);
}
return child;
}
/** @ignore */
public _onUpdate(point?: ObservablePoint)
{
if (point)
{
// this.updateFlags |= UPDATE_TRANSFORM;
if (point === this._skew)
{
this._updateSkew();
}
}
this._didChangeId++;
if (this.didChange) return;
this.didChange = true;
if (this.renderGroup)
{
const renderGroupParent = this.renderGroup.renderGroupParent;
// lets update its parent..
if (renderGroupParent)
{
renderGroupParent.onChildUpdate(this);
}
}
else if (this.parentRenderGroup)
{
this.parentRenderGroup.onChildUpdate(this);
}
}
set isRenderGroup(value: boolean)
{
if (this.renderGroup && value === false)
{
throw new Error('[Pixi] cannot undo a render group just yet');
}
if (value)
{
this.enableRenderGroup();
}
}
/**
* Returns true if this container is a render group.
* This means that it will be rendered as a separate pass, with its own set of instructions
*/
get isRenderGroup(): boolean
{
return !!this.renderGroup;
}
/** This enables the container to be rendered as a render group. */
public enableRenderGroup()
{
// does it OWN the render group..
if (this.renderGroup) return;
const parentRenderGroup = this.parentRenderGroup;
if (parentRenderGroup)
{
parentRenderGroup.removeChild(this);
}
this.renderGroup = new RenderGroup(this);
if (parentRenderGroup)
{
parentRenderGroup.addChild(this);
}
// find children render groups and move them out..
// if (parentRenderGroup)
// {
// for (let i = 0; i < parentRenderGroup.renderGroupChildren.length; i++)
// {
// const childRenderGroup = parentRenderGroup.renderGroupChildren[i];
// let parent = childRenderGroup.root;
// while (parent)
// {
// if (parent === this)
// {
// this.renderGroup.addRenderGroupChild(childRenderGroup);
// break;
// }
// parent = parent.parent;
// }
// }
// parentRenderGroup.addRenderGroupChild(this.renderGroup);
// }
// parentRenderGroup.removeChild(this);
this._updateIsSimple();
// this group matrix will now forever be an identity matrix,
// as its own transform will be passed to the GPU
this.groupTransform = Matrix.IDENTITY;
}
/** @ignore */
public _updateIsSimple()
{
this.isSimple = !(this.renderGroup) && (this.effects.length === 0);
}
/**
* Current transform of the object based on world (parent) factors.
* @readonly
*/
get worldTransform()
{
this._worldTransform ||= new Matrix();
if (this.renderGroup)
{
this._worldTransform.copyFrom(this.renderGroup.worldTransform);
}
else if (this.parentRenderGroup)
{
this._worldTransform.appendFrom(this.relativeGroupTransform, this.parentRenderGroup.worldTransform);
}
return this._worldTransform;
}
// / ////// transform related stuff
/**
* The position of the container on the x axis relative to the local coordinates of the parent.
* An alias to position.x
*/
get x(): number
{
return this._position.x;
}
set x(value: number)
{
this._position.x = value;
}
/**
* The position of the container on the y axis relative to the local coordinates of the parent.
* An alias to position.y
*/
get y(): number
{
return this._position.y;
}
set y(value: number)
{
this._position.y = value;
}
/**
* The coordinate of the object relative to the local coordinates of the parent.
* @since 4.0.0
*/
get position(): ObservablePoint
{
return this._position;
}
set position(value: PointData)
{
this._position.copyFrom(value);
}
/**
* The rotation of the object in radians.
* 'rotation' and 'angle' have the same effect on a display object; rotation is in radians, angle is in degrees.
*/
get rotation(): number
{
return this._rotation;
}
set rotation(value: number)
{
if (this._rotation !== value)
{
this._rotation = value;
this._onUpdate(this._skew);
}
}
/**
* The angle of the object in degrees.
* 'rotation' and 'angle' have the same effect on a display object; rotation is in radians, angle is in degrees.
*/
get angle(): number
{
return this.rotation * RAD_TO_DEG;
}
set angle(value: number)
{
this.rotation = value * DEG_TO_RAD;
}
/**
* The center of rotation, scaling, and skewing for this display object in its local space. The `position`
* is the projection of `pivot` in the parent's local space.
*
* By default, the pivot is the origin (0, 0).
* @since 4.0.0
*/
get pivot(): ObservablePoint
{
if (this._pivot === defaultPivot)
{
this._pivot = new ObservablePoint(this, 0, 0);
}
return this._pivot;
}
set pivot(value: PointData | number)
{
if (this._pivot === defaultPivot)
{
this._pivot = new ObservablePoint(this, 0, 0);
}
typeof value === 'number' ? this._pivot.set(value) : this._pivot.copyFrom(value);
}
/**
* The skew factor for the object in radians.
* @since 4.0.0
*/
get skew(): ObservablePoint
{
if (this._skew === defaultSkew)
{
this._skew = new ObservablePoint(this, 0, 0);
}
return this._skew;
}
set skew(value: PointData)
{
if (this._skew === defaultSkew)
{
this._skew = new ObservablePoint(this, 0, 0);
}
this._skew.copyFrom(value);
}
/**
* The scale factors of this object along the local coordinate axes.
*
* The default scale is (1, 1).
* @since 4.0.0
*/
get scale(): ObservablePoint
{
if (this._scale === defaultScale)
{
this._scale = new ObservablePoint(this, 1, 1);
}
return this._scale;
}
set scale(value: PointData | number)
{
if (this._scale === defaultScale)
{
this._scale = new ObservablePoint(this, 0, 0);
}
typeof value === 'number' ? this._scale.set(value) : this._scale.copyFrom(value);
}