1+ import UIComponentPrototype from "./UIComponentPrototype" ;
2+ import Phaser from 'phaser' ;
3+ import _ from 'underscore' ;
4+
5+ /**
6+ * @memberOf PhaserComps.UIComponents
7+ * @class UIContainer
8+ * @classdesc
9+ * Base container component. Allows to add dynamically created UIComponents inside other components.
10+ *
11+ * Child components clips must be root components only, not any clip's children.
12+ *
13+ * Note, that UIContainer can be only a child component of another UIComponentPrototype instance.
14+ *
15+ * @extends PhaserComps.UIComponents.UIComponentPrototype
16+ * @param {PhaserComps.UIComponents.UIComponentPrototype } parent UIComponentPrototype instance to find clip inside
17+ * @param {String } key key to find clip inside parent
18+ */
19+
20+ export default class UIContainer extends UIComponentPrototype {
21+
22+ constructor ( parent , key ) {
23+ super ( parent , key ) ;
24+
25+ /**
26+ * List of children UIComponentPrototypes added
27+ * @type {Array<PhaserComps.UIComponents.UIComponentPrototype> }
28+ * @private
29+ */
30+ this . _children = [ ] ;
31+
32+ /**
33+ * Current active Phaser container instance
34+ * @type {Phaser.GameObjects.Container }
35+ * @private
36+ */
37+ this . _containerClip = null ;
38+ }
39+
40+ /**
41+ * @method PhaserComps.UIComponents.UIContainer#addChild
42+ * @description
43+ * Adds child to children list, and adds it to Phaser container instance, if one exists
44+ *
45+ * @param {PhaserComps.UIComponents.UIComponentPrototype } child child component to add
46+ * @return {PhaserComps.UIComponents.UIComponentPrototype } child
47+ */
48+ addChild ( child ) {
49+ if ( this . _children . indexOf ( child ) !== - 1 ) {
50+ return child ; // TODO move to top?
51+ }
52+ this . _children . push ( child ) ;
53+
54+ // add to container instance, or hide
55+ if ( this . _containerClip ) {
56+ this . _addUIComponentToContainerClip ( child ) ;
57+ } else {
58+ child . _clip . groupVisible = false ;
59+ }
60+ return child ;
61+ }
62+
63+ /**
64+ * @method PhaserComps.UIComponents.UIContainer#removeChild
65+ * @description
66+ * Removes child from children list, and removes it from Phaser container instance, if one exists
67+ *
68+ * @param {PhaserComps.UIComponents.UIComponentPrototype } child child component to remove
69+ * @return {PhaserComps.UIComponents.UIComponentPrototype } returns child param
70+ */
71+ removeChild ( child ) {
72+ let index = this . _children . indexOf ( child ) ;
73+ if ( index === - 1 ) {
74+ return child ;
75+ }
76+ this . _children . splice ( index , 1 ) ;
77+
78+ if ( this . _containerClip ) {
79+ this . _removeUIComponentFromContainerClip ( child ) ;
80+ }
81+ return child ;
82+ }
83+
84+ /**
85+ *
86+ * @param {PhaserComps.UIComponents.UIComponentPrototype } child
87+ * @private
88+ */
89+ _addUIComponentToContainerClip ( child ) {
90+ _ . each ( child . _clip . childrenList , clipChild => {
91+ this . _containerClip . add ( clipChild ) ;
92+ } , this ) ;
93+ child . _clip . groupVisible = true ;
94+ }
95+
96+ /**
97+ *
98+ * @param {PhaserComps.UIComponents.UIComponentPrototype } child
99+ * @param {Boolean } [destroyChild=false]
100+ * @private
101+ */
102+ _removeUIComponentFromContainerClip ( child , destroyChild ) {
103+ _ . each ( child . _clip . childrenList , clipChild => {
104+ this . _containerClip . remove ( clipChild , destroyChild ) ;
105+ } , this ) ;
106+ child . _clip . groupVisible = false ;
107+ }
108+
109+ onClipAppend ( clip ) {
110+ super . onClipAppend ( clip ) ;
111+ this . _containerClip = clip . _container ;
112+ if ( this . _containerClip ) {
113+ _ . each ( this . _children , child => {
114+ this . _addUIComponentToContainerClip ( child ) ;
115+ } , this ) ;
116+ }
117+ }
118+
119+ onClipRemove ( clip ) {
120+ super . onClipRemove ( clip ) ;
121+ // hide and remove children from current container
122+ if ( this . _containerClip ) {
123+ _ . each ( this . _children , child => {
124+ this . _removeUIComponentFromContainerClip ( child ) ;
125+ } , this ) ;
126+ }
127+ }
128+
129+ destroy ( ) {
130+ // remove and destroy children
131+ _ . each ( this . _children , child => {
132+ if ( this . _containerClip ) { // TODO check if needed
133+ this . _removeUIComponentFromContainerClip ( child ) ;
134+ }
135+ child . destroy ( ) ;
136+ } , this ) ;
137+ this . _children . length = 0 ;
138+ super . destroy ( ) ;
139+ }
140+ }
0 commit comments