ca0f1935-b748-48c8-99d1-b14ac848657e.js 3.21 KB
"use strict";
cc._RF.push(module, 'ca0f1k1t0hIyJnRsUrISGV+', 'PresenterCCViewFactory');
// script/game/ui/PresenterCCViewFactory.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RegSubview = exports.RegSubpresenter = exports.RegView = exports.RegPresenter = void 0;
const PresenterBase_1 = require("../../common/classbase/PresenterBase");
const simba_cc_resutils_1 = require("simba-cc-resutils");
const PresenterMap = {};
const ViewMap = {};
function RegPresenter(viewType) {
    return function (target) {
        PresenterMap[target.uuid] = viewType;
    };
}
exports.RegPresenter = RegPresenter;
function RegView(viewType, prefabPath) {
    return function (target) {
        ViewMap[viewType] = { cls: target, prefab: prefabPath };
    };
}
exports.RegView = RegView;
const ViewTypeSubviewMap = {};
const ViewTypeSubpresenterMap = {};
function RegSubpresenter(viewType) {
    return function (target) {
        ViewTypeSubpresenterMap[viewType] = { cls: target };
        Object.defineProperty(target, "_viewType", { configurable: false, enumerable: false, value: viewType, writable: false });
    };
}
exports.RegSubpresenter = RegSubpresenter;
function RegSubview(viewType) {
    return function (target) {
        ViewTypeSubviewMap[viewType] = { cls: target };
        Object.defineProperty(target, "_viewType", { configurable: false, enumerable: false, value: viewType, writable: false });
    };
}
exports.RegSubview = RegSubview;
let factory = async (presenterCls) => {
    let ret = ViewMap[PresenterMap[presenterCls.uuid]];
    if (ret) {
        let node = await simba_cc_resutils_1.ResUtils.createWithPrefab(ret.prefab);
        let comp = node.getComponent(ret.cls);
        if (!comp) {
            comp = node.addComponent(ret.cls);
        }
        return comp;
    }
    throw new Error("Create view failed. " + presenterCls.uuid);
};
factory.preloadView = async (presenterCls) => {
    let ret = ViewMap[PresenterMap[presenterCls.uuid]];
    if (ret) {
        await simba_cc_resutils_1.ResUtils.loadRes(ret.prefab, cc.Prefab);
    }
};
PresenterBase_1.setPresenterViewFactory(factory);
// setSubviewFactory(async (p: Subpresenter) => {
//     if (p.view) throw new Error("Subpresenter already has view.");
//     const viewType = p.constructor["_viewType"];
//     if (!viewType) throw new Error("Subpresenter not registered.");
//     if (!ViewTypeSubviewMap[viewType]) throw new Error("Subview not registered. " + viewType);
//     const data = ViewTypeSubviewMap[viewType];
//     if (!data.prefab) throw new Error("Subview prefab not registered. " + viewType);
//     let node = await ResUtils.createWithPrefab(data.prefab);
//     let ret = node.getComponent(data.cls);
//     if (!ret) {
//         ret = node.addComponent(data.cls);
//     }
//     p.attachView(ret);
//     return ret;
// });
PresenterBase_1.setSubpresenterwFactory((v) => {
    const viewType = v.constructor["_viewType"];
    if (!viewType)
        throw new Error("Subview not registered.");
    let data = ViewTypeSubpresenterMap[viewType];
    if (!data)
        throw new Error("Subpresenter not registered for view type. " + viewType);
    let presenter = new data.cls;
    presenter.attachView(v);
    return presenter;
});

cc._RF.pop();