3333800e-410e-433b-bb40-a18733be8b29.js 13.9 KB
"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();