UIManager.ts
7.32 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
import { ResType, WindowName } from "../Global";
import { UIBase } from "./UIBase";
export class UIManager {
private static _ins: UIManager = null;
static get ins(): UIManager {
if (this._ins == null) {
this._ins = new UIManager();
}
return this._ins;
}
private _uiRoot: cc.Node;
private _uiLayer: cc.Node;
get uiLayer(): cc.Node { return this._uiLayer; }
private _windowLockLayer: cc.Node;
private _windowLayer: cc.Node;
private _widgetLayer: cc.Node;
get widgetLayer(): cc.Node { return this._widgetLayer; }
private _loadingLayer: cc.Node;
get loadingLayer(): cc.Node { return this._loadingLayer; }
// private _loadingNode: Node;
private _tipsLayer: cc.Node;
private _adLayer: cc.Node;
get adLayer(): cc.Node { return this._adLayer; }
private _cache: Map<string, cc.Node> = new Map<string, cc.Node>();
get cache(): Map<string, cc.Node> { return this._cache; }
private _adRoot: cc.Node;
// setLoadingNode(node: Node): void {
// this._loadingNode = node;
// }
// setAdNode(node: cc.Node) {
// this._adRoot = node;
// }
init(uiRoot: cc.Node): void {
//已经进入了GameMain场景
this._uiRoot = uiRoot;
this._uiLayer = this._uiRoot.getChildByName("uiLayer");
this._windowLockLayer = this._uiRoot.getChildByName("windowBlcok");
this._windowLockLayer.active = false;
// this._windowLockLayer.getComponent(Widget).updateAlignment();
this._windowLayer = this._uiRoot.getChildByName("windowLayer");
this._widgetLayer = this._uiRoot.getChildByName("widgetLayer");
this._loadingLayer = this._uiRoot.getChildByName("loadingLayer");
this._loadingLayer.active = false;
this._tipsLayer = this._uiRoot.getChildByName("tipsLayer");
this._adLayer = this._uiRoot.getChildByName("adLayer");
// if (this._loadingNode) {
// // this._loadingNode.removeFromParent();
// this._loadingLayer.addChild(this._loadingNode);
// this._loadingNode.getChildByName("type1").active = false;
// this._loadingNode.getChildByName("type2").active = true;
// this._loadingNode.active = false;
// }
// uiRoot.addChild(this._adRoot);//添加广告节点
}
// static get adRoot() {
// let _scene: any = cc.director.getScene();
// let _adRoot = _scene.getChildByName('Canvas').getChildByName('adLayer') as cc.Node;
// if (_adRoot == null) {
// _adRoot = new cc.Node();
// _adRoot.name = 'adLayer';
// _adRoot.parent = _scene.getChildByName('Canvas');
// _adRoot.x = 0;
// _adRoot.y = 0;
// _adRoot.width = cc.winSize.width;
// _adRoot.height = cc.winSize.height;
// _adRoot.setSiblingIndex(999);
// }
// return _adRoot;
// }
openScene(name: string) {
this._loadingLayer.active = true;
this._loadingLayer.getChildByName('sceneNode').active = true;
this._loadingLayer.getChildByName('windowNode').active = false;
cc.resources.loadScene('scenes/' + name,
(finish: number, total: number) => {
let prg = finish / total;
this._loadingLayer.getChildByName('sceneNode').getComponentInChildren(cc.ProgressBar).progress = prg;
},
(error, scene: cc.SceneAsset) => {
this._loadingLayer.active = false;
cc.director.runScene(scene);
});
// cc.director.scene
}
/**
*
* @param type
* @param callBack 参数 view:cc.Node
*/
loadWindow(type: WindowName, callBack: Function) {
if (this._cache.has(type)) {
callBack(this._cache.get(type));
}
else {
this._loadingLayer.active = true;
this._loadingLayer.getChildByName('sceneNode').active = false;
this._loadingLayer.getChildByName('windowNode').active = true;
cc.resources.load(type, cc.Prefab, (error, prefab: cc.Prefab) => {
this._loadingLayer.active = false;
if (!error) {
if (!this._cache.has(type)) {
let view = cc.instantiate(prefab);
this._cache.set(type, view);
callBack(view);
}
}
});
}
}
openWindow(type: WindowName, openParam: any = null): void {
if (this._cache.has(type)) {
this.doOpenWindow(this._cache.get(type), openParam);
}
else {
// this._loadingLayer.active = true;
// this._loadingLayer.getChildByName('sceneNode').active = false;
// this._loadingLayer.getChildByName('windowNode').active = true;
// cc.resources.load(type, cc.Prefab,
// (finished, total) => {
// let prg = finished / total;
// },
// (error, prefab: cc.Prefab) => {
// //finish
// if (!this._cache.has(type)) {
// let view = cc.instantiate(prefab);
// this._cache.set(type, view);
// this._loadingLayer.active = false;
// this.doOpenWindow(view, openParam);
// }
// });
this.loadWindow(type, (view: cc.Node) => {
this.doOpenWindow(view, openParam);
})
}
}
private doOpenWindow(view: cc.Node, param: any): void {
if (!view.parent) {
!this._windowLockLayer.active && (this._windowLockLayer.active = true);
let base = view.getComponent(UIBase);
base && (base.enableParam = param);
this._windowLayer.addChild(view);
view.active = true;
base && base.onPopUpEffect();
}
}
closeWindow(type: WindowName, callback: Function = null): void {
if (this._cache.has(type)) {
let view = this._cache.get(type);
// view.active = false;
let base = view.getComponent(UIBase);
base && base.onBeforeClose();
base.onCloseEffect(() => {
let isOnWindowLayer = (view.isValid && view.parent === this._windowLayer)
if (isOnWindowLayer) {
view.removeFromParent();
view.active = false;
}
if (isOnWindowLayer && this._windowLayer.children.length == 0) {
this._windowLockLayer.active = false;
}
callback && callback();
}, this)
}
}
getWidgetNode(type: ResType): cc.Node {
return this._widgetLayer.getChildByName('widget' + type.toString());
}
showTips(str: string): void {
// let node = this._tipsLayer.getChildByName('TipsNode');
// node.active = true;
// node.getComponentInChildren(Label).string = str;
// Tween.stopAllByTarget(node);
// tween(node).delay(3).call(this.closeTips.bind(this)).start()
}
private closeTips(): void {
let node = this._tipsLayer.getChildByName('TipsNode');
node.active = false;
}
// playResFlyEffect
}