Skip to content

Commit aba2361

Browse files
committed
UIContainer added
1 parent 1289f78 commit aba2361

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed

src/components/UIComponentPrototype.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,17 @@ export default class UIComponentPrototype extends Phaser.Events.EventEmitter {
167167
}
168168

169169
/**
170+
* Destroy ComponentPrototype and clip, if exists
170171
* @method PhaserComps.UIComponents.UIComponentPrototype#destroy
171172
* @protected
172173
*/
173174
destroy() {
174175
if (this._parent){
175176
this._parent.removeListener(_EVENT_STATE, this._onEventState);
176177
}
178+
if (this._clip) {
179+
this._clip.destroy();
180+
}
177181
}
178182

179183
/**

src/components/UIComponents.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import UIButtonDraggable from "./UIButtonDraggable";
66
import UIScrollBar from "./UIScrollBar";
77
import UIScrollPanel from "./UIScrollPanel";
88
import UIProgressBar from "./UIProgressBar";
9+
import UIContainer from "./UIContainer";
910

1011
/**
1112
* @namespace PhaserComps.UIComponents
@@ -19,7 +20,8 @@ const UIComponents = {
1920
UIButtonDraggable: UIButtonDraggable,
2021
UIScrollBar: UIScrollBar,
2122
UIScrollPanel: UIScrollPanel,
22-
UIProgressBar: UIProgressBar
23+
UIProgressBar: UIProgressBar,
24+
UIContainer: UIContainer
2325
};
2426

2527
export default UIComponents;

src/components/UIContainer.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)