PresenterCCViewFactory.ts
3.27 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
import { Presenter, setPresenterViewFactory, Subpresenter, setSubpresenterwFactory } from "../../common/classbase/PresenterBase";
import { ResUtils } from "../../common/utils/ResUtils";
import { CCView, CCSubview } from "../../common/classbase/CCViewBase";
import { Subview } from "../../common/classbase/ViewBase";
const PresenterMap: { [key: string]: string } = {};
const ViewMap: { [key: string]: { cls: { new(): CCView }, prefab: string } } = {};
export function RegPresenter(viewType: string) {
return function (target: { new(): Presenter<any, any>, uuid: string }) {
PresenterMap[target.uuid] = viewType;
}
}
export function RegView(viewType: string, prefabPath: string) {
return function (target: { new(): CCView }) {
ViewMap[viewType] = { cls: target, prefab: prefabPath };
}
}
const ViewTypeSubviewMap: { [key: string]: { cls: { new(): CCSubview } } } = {};
const ViewTypeSubpresenterMap: { [key: string]: { cls: { new(): Subpresenter } } } = {};
export function RegSubpresenter(viewType: string) {
return function (target: { new(): Subpresenter }) {
ViewTypeSubpresenterMap[viewType] = { cls: target };
Object.defineProperty(target, "_viewType", { configurable: false, enumerable: false, value: viewType, writable: false });
}
}
export function RegSubview(viewType: string) {
return function (target: { new(): CCSubview }) {
ViewTypeSubviewMap[viewType] = { cls: target };
Object.defineProperty(target, "_viewType", { configurable: false, enumerable: false, value: viewType, writable: false });
}
}
let factory: any = async (presenterCls) => {
let ret = ViewMap[PresenterMap[presenterCls.uuid]];
if (ret) {
let node = await 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: { new(): Presenter, uuid: string }) => {
let ret = ViewMap[PresenterMap[presenterCls.uuid]];
if (ret) {
await ResUtils.loadRes(ret.prefab, cc.Prefab);
}
}
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;
// });
setSubpresenterwFactory((v: Subview) => {
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;
});