3333800e-410e-433b-bb40-a18733be8b29.js
13.9 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
"use strict";
cc._RF.push(module, '33338AOQQ5DO7tAoYczvosp', 'UIManager');
// script/common/gameplay/managers/UIManager.ts
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UIManager = void 0;
const ResUtils_1 = require("../../utils/ResUtils");
const ToastManager_1 = require("./ToastManager");
const PresenterBase_1 = require("../../classbase/PresenterBase");
const CCViewBase_1 = require("../../classbase/CCViewBase");
const AlertManager_1 = require("./AlertManager");
var UIManager;
(function (UIManager) {
const MODAL_Z = 1000;
const ALERT_Z = 2000;
const TOAST_Z = 3000;
let rootNode;
let rootView;
let modalPresenter;
let toastRoot;
let alertRoot;
let modalRoot;
let modalRootView;
let loadingContainer;
let loadingView;
const presenterStack = [];
const viewMap = {};
const viewCache = {};
const loadingViews = [];
async function init(uiRoot) {
let promise = ToastManager_1.ToastManager.init();
rootNode = uiRoot;
rootView = rootNode.addComponent(CCViewBase_1.CCView);
modalRoot = new cc.Node();
modalRoot.zIndex = MODAL_Z;
rootNode.addChild(modalRoot);
modalRootView = modalRoot.addComponent(CCViewBase_1.CCView);
alertRoot = new cc.Node();
alertRoot.zIndex = ALERT_Z;
rootNode.addChild(alertRoot);
toastRoot = new cc.Node();
toastRoot.zIndex = TOAST_Z;
rootNode.addChild(toastRoot);
loadingContainer = new cc.Node();
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);
let ret = await ResUtils_1.ResUtils.loadRes("prefab/ui/ViewLoading", cc.Prefab);
loadingView = cc.instantiate(ret);
loadingView.active = false;
loadingContainer.addChild(loadingView);
ResUtils_1.ResUtils.releaseAsset(ret);
await promise;
}
UIManager.init = init;
let viewInstanceId = 0;
function pushPresenter(presentCls, param, hideOthers = false) {
return new Promise(async (resolve, reject) => {
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]);
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.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;
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);
}
else {
if (!loadingContainer.active) {
loadingViews.push(presentCls.uuid);
loadingContainer.active = true;
loadingView.active = false;
loadingView.timer = setTimeout(() => {
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;
let presenter = viewCache[presentCls.uuid];
if (!presenter) {
// create a view
try {
presenter = await PresenterBase_1.createPresenter(presentCls);
presenter.view.name = nodeName;
viewCache[presentCls.uuid] = presenter;
}
catch (e) {
error = e;
}
}
if (presenter) {
presenter.open(rootView, param);
presenter.view.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 (loadingView.timer) {
clearTimeout(loadingView.timer);
loadingView.timer = undefined;
}
loadingContainer.active = false;
}
if (!presenter)
reject(error ? error : new Error());
else
resolve(presenter);
}
});
}
UIManager.pushPresenter = pushPresenter;
function getTopPresenter() {
return presenterStack[presenterStack.length - 1];
}
UIManager.getTopPresenter = getTopPresenter;
function popPresenter(destroy = 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();
}
}
UIManager.popPresenter = popPresenter;
function popToPresenter(presenter) {
while (presenterStack && presenterStack[presenterStack.length - 1] !== presenter) {
popPresenter();
}
}
UIManager.popToPresenter = popToPresenter;
function popAll(destroy) {
while (presenterStack.length) {
let presenter = presenterStack.pop();
delete viewMap[presenter.view.name];
presenter.view.close(destroy);
}
}
UIManager.popAll = popAll;
function onPresenterInStackClose(presenter, destroy) {
let view = presenter.view.name;
if (!viewMap[view])
return;
let presenterExt = presenter;
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;
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) {
viewMap[i].view.show();
}
}
}
}
function closePresenterInStack(presenter, destroy) {
let view = presenter.constructor.uuid;
if (!viewMap[view])
return;
if (presenterStack[presenterStack.length - 1] === presenter) {
popPresenter();
}
else {
presenter.view.close(destroy);
}
}
UIManager.closePresenterInStack = closePresenterInStack;
function showModalPresenter(presentCls, param) {
return new Promise(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;
loadingView.timer = setTimeout(() => {
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;
let presenter = viewCache[presentCls.uuid];
if (!presenter) {
// create a view
try {
presenter = await PresenterBase_1.createPresenter(presentCls);
presenter.view.name = nodeName;
viewCache[presentCls.uuid] = presenter;
}
catch (e) {
error = e;
}
}
if (presenter) {
presenter.open(modalRootView, param);
modalPresenter = presenter;
modalPresenter.view.onClose.once(() => {
modalPresenter = undefined;
});
}
let index = loadingViews.findIndex(v => v === presentCls.uuid);
loadingViews.splice(index, 1);
if (loadingViews.length == 0) {
if (loadingView.timer) {
clearTimeout(loadingView.timer);
loadingView.timer = undefined;
}
loadingContainer.active = false;
}
if (!presenter)
reject(error ? error : new Error());
else
resolve(presenter);
}
});
}
UIManager.showModalPresenter = showModalPresenter;
function dismissModalPresenter(destroy) {
if (modalPresenter) {
modalPresenter.view.close(destroy);
modalPresenter = undefined;
}
}
UIManager.dismissModalPresenter = dismissModalPresenter;
function getModalPresenter() {
return modalPresenter;
}
UIManager.getModalPresenter = getModalPresenter;
function showToast(content) {
ToastManager_1.ToastManager.addToast(toastRoot, content);
}
UIManager.showToast = showToast;
function getPresenter(view) {
return viewMap[view];
}
UIManager.getPresenter = getPresenter;
function showAlert(nodeOrPrefab, animate = true) {
return AlertManager_1.AlertManager.showAlert(alertRoot, nodeOrPrefab, animate);
}
UIManager.showAlert = showAlert;
})(UIManager = exports.UIManager || (exports.UIManager = {}));
cc._RF.pop();