UIManager.ts
15.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
import { ResUtils } from "simba-cc-resutils";
import { ToastManager } from "./ToastManager";
import { Presenter, createPresenter, PresenterParamType } from "../../classbase/PresenterBase";
import { View } from "../../classbase/ViewBase";
import { CCView } from "../../classbase/CCViewBase";
import { AlertManager } from "./AlertManager";
import { SpecialToastType } from "../../../game/ui/SpecialToast";
export namespace UIManager {
const MODAL_Z = 1000;
const ALERT_Z = 2000;
const TOAST_Z = 3000;
let rootNode: cc.Node;
let rootView: View;
let modalPresenter: Presenter<any> | undefined;
let toastRoot: cc.Node;
let alertRoot: cc.Node;
let modalRoot: cc.Node;
let modalRootView: View;
let loadingContainer: cc.Node;
let loadingView: cc.Node;
/**场景最上层如提示父节点 */
let toastParentNode:cc.Node;
type PresenterExt = Presenter<any> & { hideViews?: string[] };
const presenterStack: PresenterExt[] = [];
const viewMap: { [key: string]: PresenterExt } = {};
const viewCache: { [key: string]: PresenterExt } = {};
const loadingViews: string[] = [];
export async function init(uiRoot: cc.Node,toastParentNode_:cc.Node) {
let promise = ToastManager.init();
rootNode = uiRoot;
rootView = rootNode.addComponent(CCView);
toastParentNode = toastParentNode_;
if (!modalRoot) {
modalRoot = new cc.Node();
modalRoot.name = "modalRoot";
modalRoot.zIndex = MODAL_Z;
rootNode.addChild(modalRoot);
modalRootView = modalRoot.addComponent(CCView);
}
if (!alertRoot) {
alertRoot = new cc.Node();
alertRoot.name = "alertRoot";
alertRoot.zIndex = ALERT_Z;
rootNode.addChild(alertRoot);
}
if (!toastRoot) {
toastRoot = new cc.Node();
toastRoot.name = "toastRoot";
toastRoot.zIndex = TOAST_Z;
rootNode.addChild(toastRoot);
}
if (!loadingContainer) {
loadingContainer = new cc.Node();
loadingContainer.name = "loadingContainer";
loadingContainer.zIndex = 99;
loadingContainer.active = false;
rootNode.addChild(loadingContainer);
let widget = loadingContainer.addComponent(cc.Widget);
widget.top = widget.bottom = widget.left = widget.right = 0;
widget.isAlignTop = widget.isAlignBottom = widget.isAlignLeft = widget.isAlignRight = true;
loadingContainer.addComponent(cc.BlockInputEvents);
}
if (!loadingView) {
let ret = await ResUtils.loadRes("prefab/ui/ViewLoading", cc.Prefab);
loadingView = cc.instantiate(ret);
loadingContainer.addChild(loadingView);
loadingView.active = false;
}
await promise;
}
let viewInstanceId = 0;
export function pushPresenter<T extends Presenter<any, any>>(presentCls: { new(): T, uuid: string, allowMultiInstances?: boolean }, param: PresenterParamType<T>, hideOthers = false) {
return new Promise<T>(async (resolve, reject) => {
try {
if (presenterStack.length > 0) presenterStack[presenterStack.length - 1].onEnterBackground();
let nodeName = presentCls.allowMultiInstances ? presentCls.uuid + (viewInstanceId++) : presentCls.uuid;
if (viewMap[nodeName]) {
if (presenterStack[presenterStack.length - 1].view.name == nodeName) {
console.log(presentCls.uuid + "视图已经在最顶层");
presenterStack[presenterStack.length - 1].onOpen(param);
resolve(presenterStack[presenterStack.length - 1] as T);
return;
}
// move view to top
let index = presenterStack.findIndex(v => v.view.name === nodeName);
let presenter = presenterStack[index];
presenterStack.push(presenter);
presenterStack.splice(index, 1);
(presenter.view as CCView).node.setSiblingIndex(presenterStack.length);
presenter.onOpen(param);
if (presenter.hideViews) {
let len = presenterStack.length - 1;
if (hideOthers) {
for (let i = index; i < len; i++) {
if (!presenterStack[i].view.isHidden) {
presenterStack[i].view.hide();
presenter.hideViews!.push(presenterStack[i].view.name);
}
}
} else {
let hideNode: PresenterExt | undefined;
for (let i = index; i < len; i++) {
if (presenterStack[i].hideViews) {
hideNode = presenterStack[i];
break;
}
}
if (hideNode) {
for (let str of presenter.hideViews!) {
hideNode.hideViews!.push(str);
}
} else {
for (let str of presenter.hideViews!) {
viewMap[str].view.show();
}
}
delete presenter.hideViews;
}
} else if (hideOthers) {
let len = presenterStack.length - 1;
presenter.hideViews = [];
for (let i = 0; i < len; i++) {
if (!presenterStack[i].view.isHidden) {
presenterStack[i].view.hide();
presenter.hideViews.push(presenterStack[i].view.name);
}
}
}
resolve(presenter as T);
} else {
if (!loadingContainer.active) {
loadingViews.push(presentCls.uuid);
loadingContainer.active = true;
loadingView.active = false;
(<any>loadingView).timer = setTimeout(() => {
(<any>loadingView).timer = undefined;
loadingView.active = true;
}, 1000);
} else {
let index = loadingViews.findIndex(v => v === presentCls.uuid);
if (index >= 0) {
reject("View is already loading.");
return;
}
}
let error: Error | undefined;
let presenter: PresenterExt = viewCache[presentCls.uuid];
if (!presenter) {
// create a view
try {
presenter = await createPresenter(presentCls);
presenter.view.name = nodeName;
viewCache[presentCls.uuid] = presenter;
} catch (e) {
error = e;
}
}
if (presenter) {
(presenter as T).open(rootView, param);
(presenter.view as CCView).onClose.once((destroy) => {
onPresenterInStackClose(presenter, destroy);
});
if (hideOthers) {
presenter.hideViews = [];
let len = presenterStack.length;
for (let i = 0; i < len; i++) {
if (!presenterStack[i].view.isHidden) {
presenterStack[i].view.hide();
presenter.hideViews.push(presenterStack[i].view.name);
}
}
}
presenterStack.push(presenter);
viewMap[nodeName] = presenter;
}
let index = loadingViews.findIndex(v => v === presentCls.uuid);
loadingViews.splice(index, 1);
if (loadingViews.length == 0) {
if ((<any>loadingView).timer) {
clearTimeout((<any>loadingView).timer);
(<any>loadingView).timer = undefined;
}
loadingContainer.active = false;
}
if (!presenter) reject(error ? error : new Error());
else resolve(presenter as T);
}
} catch (error) {
console.log("pushPresenter error, ", error);
}
});
}
export function getTopPresenter(): Presenter {
return presenterStack[presenterStack.length - 1];
}
export function popPresenter(destroy: boolean = false) {
let presenter = presenterStack.pop();
if (presenter) {
delete viewMap[presenter.view.name];
if (destroy) delete viewCache[presenter.view.name];
presenter.view.close(destroy);
if (presenter.hideViews) {
for (let v of presenter.hideViews) {
viewMap[v].view.show();
}
}
if (presenterStack.length > 0) presenterStack[presenterStack.length - 1].onEnterForeground();
}
}
/**弹出到具体某个presenter */
export function popToPresenter(presenter: Presenter<any, any>) {
while (presenterStack && presenterStack[presenterStack.length - 1] !== presenter) {
popPresenter();
}
}
/**
* 弹出所有presenter
* @param destroy 是否销毁
*/
export function popAll(destroy?: boolean) {
while (presenterStack.length) {
let presenter = presenterStack.pop()!;
delete viewMap[presenter.view.name];
presenter.view.close(destroy);
}
}
function onPresenterInStackClose(presenter: Presenter<any>, destroy?: boolean) {
let view = presenter.view.name;
if (!viewMap[view]) return;
let presenterExt = presenter as PresenterExt;
let index = presenterStack.findIndex(v => v === presenter);
presenterStack.splice(index, 1);
if (index === presenterStack.length) {
presenterStack[presenterStack.length - 1].onEnterForeground();
}
delete viewMap[view];
if (destroy) {
delete viewCache[view];
}
if (presenterExt.hideViews && presenterExt.hideViews.length) {
let len = presenterStack.length;
let node1: PresenterExt | undefined;
for (let i = index; i < len; i++) {
if (presenterStack[i].hideViews) {
node1 = presenterStack[i];
break;
}
}
if (node1) {
presenterExt.hideViews.push(...presenterExt.hideViews);
} else {
for (let i of presenterExt.hideViews) {
if (viewMap[i] && viewMap[i].view) {
viewMap[i].view.show();
}
}
}
}
}
export function closePresenterInStack(presenter: Presenter<any>, destroy?: boolean) {
let view = ((presenter.constructor as any).uuid as string);
if (!viewMap[view]) return;
if (presenterStack[presenterStack.length - 1] === presenter) {
popPresenter();
} else {
presenter.view.close(destroy);
}
}
export function showModalPresenter<T extends Presenter<any, any>>(presentCls: { new(): T, uuid: string, allowMultiInstances?: boolean }, param: PresenterParamType<T>) {
return new Promise<T>(async (resolve, reject) => {
if (modalPresenter instanceof presentCls) {
resolve(modalPresenter);
} else {
let nodeName = presentCls.uuid;
if (!loadingContainer.active) {
loadingViews.push(presentCls.uuid);
loadingContainer.active = true;
loadingView.active = false;
(<any>loadingView).timer = setTimeout(() => {
(<any>loadingView).timer = undefined;
loadingView.active = true;
}, 1000);
} else {
let index = loadingViews.findIndex(v => v === presentCls.uuid);
if (index >= 0) {
reject("View is already loading.");
return;
}
}
let error: Error | undefined;
let presenter: PresenterExt = viewCache[presentCls.uuid];
if (!presenter) {
// create a view
try {
presenter = await createPresenter(presentCls);
presenter.view.name = nodeName;
viewCache[presentCls.uuid] = presenter;
} catch (e) {
error = e;
}
}
if (presenter) {
(presenter as T).open(modalRootView, param);
modalPresenter = presenter;
(modalPresenter.view as CCView).onClose.once(() => {
modalPresenter = undefined;
})
}
let index = loadingViews.findIndex(v => v === presentCls.uuid);
loadingViews.splice(index, 1);
if (loadingViews.length == 0) {
if ((<any>loadingView).timer) {
clearTimeout((<any>loadingView).timer);
(<any>loadingView).timer = undefined;
}
loadingContainer.active = false;
}
if (!presenter) reject(error ? error : new Error());
else resolve(presenter as T);
}
});
}
export function dismissModalPresenter(destroy?: boolean) {
if (modalPresenter) {
modalPresenter.view.close(destroy);
modalPresenter = undefined;
}
}
export function getModalPresenter() {
return modalPresenter;
}
export function showToast(content: string | cc.Node) {
ToastManager.addToast(toastRoot, content);
}
/**
* 在场景最上层弱提示
* @param parentNode toast的父节点
* @param content 显示内容
*/
export function showSceneToast(content: string | cc.Node, time: number = 2){
ToastManager.addToast(toastParentNode,content, time);
}
/**
* 展示特殊Toast
* @param type 特殊Toast的类型
*/
export function showSpecialToast(type: SpecialToastType) {
ToastManager.addSpecialToast(toastRoot, type);
}
export function getPresenter(view: string): Presenter | undefined {
return viewMap[view];
}
export function showAlert(nodeOrPrefab: cc.Node | string, animate = true) {
return AlertManager.showAlert(rootNode, nodeOrPrefab, animate);
}
}