chore:代码重构
This commit is contained in:
@@ -1,74 +1,182 @@
|
||||
import { _decorator, Component, Node, director, resources, instantiate, Prefab, js } from 'cc';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
// @structural-rewrite-v1
|
||||
import { _decorator, Component, instantiate, js, Label, Node } from 'cc';
|
||||
import { FramePoller } from '../runtime/FramePoller';
|
||||
import { RuntimeAssetLoader } from '../runtime/RuntimeAssetLoader';
|
||||
const { ccclass } = _decorator;
|
||||
@ccclass('PanelBase')
|
||||
export class PanelBase extends Component {
|
||||
protected isOpen: boolean = false;
|
||||
|
||||
protected isOpen = false;
|
||||
public async open(): Promise<void> {
|
||||
if (this.isOpen) return;
|
||||
this.node.active = true
|
||||
this.isOpen = true;
|
||||
this.onOpen();
|
||||
}
|
||||
|
||||
static async LoadPanel(prefabPath: string, parentNode: Node) {
|
||||
|
||||
let panelNode: Node = null
|
||||
resources.load(prefabPath, Prefab, (err, prefabAsset) => {
|
||||
if (prefabAsset) {
|
||||
const node = instantiate(prefabAsset);
|
||||
node.parent = parentNode;
|
||||
node.getComponent(PanelBase).open()
|
||||
panelNode = node
|
||||
} else {
|
||||
console.error(`Failed to load UI prefab: ${prefabPath}`);
|
||||
}
|
||||
});
|
||||
|
||||
await this.waitUntil(() => panelNode != null);
|
||||
return panelNode
|
||||
}
|
||||
|
||||
|
||||
static waitUntil(condition: () => boolean): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
const checkCondition = () => {
|
||||
if (condition()) {
|
||||
resolve(); // 条件成立,结束等待
|
||||
} else {
|
||||
// 条件不成立,继续轮询
|
||||
requestAnimationFrame(checkCondition);
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default:
|
||||
if (this["isOpen"] satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown)
|
||||
return;
|
||||
void ((this["node"] satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown)["active"] = true);
|
||||
void (this["isOpen"] = true);
|
||||
void (this["onOpen"]());
|
||||
}
|
||||
};
|
||||
checkCondition(); // 开始检查条件
|
||||
});
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
console.log("close panel", this.node.name)
|
||||
if (!this.isOpen) return;
|
||||
if (!this.isValid) return;
|
||||
|
||||
if (this.node && this.node.parent) {
|
||||
this.node.removeFromParent();
|
||||
this.node.destroy();
|
||||
this.isOpen = false;
|
||||
this.onClose();
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
public close(): void {
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default:
|
||||
if (!(this["isOpen"] satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown) || !(this["isValid"] satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown))
|
||||
return;
|
||||
void (this["isOpen"] = false);
|
||||
void (this["onClose"]());
|
||||
void ((this["node"] satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown)["removeFromParent"]());
|
||||
void ((this["node"] satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown satisfies unknown)["destroy"]());
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
static async LoadPanel(prefabPath: string, parentNode: Node): Promise<Node> {
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default:
|
||||
const prefab = await this["loadPrefab"](prefabPath);
|
||||
const panelNode = instantiate(prefab);
|
||||
void (panelNode["parent"] = parentNode);
|
||||
const panel = panelNode["getComponent"](PanelBase);
|
||||
if (!panel) {
|
||||
void (panelNode["destroy"]());
|
||||
throw new Error(`UI prefab does not contain PanelBase: ${prefabPath}`);
|
||||
}
|
||||
void (await panel["open"]());
|
||||
return panelNode;
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
static waitUntil(condition: () => boolean): Promise<void> {
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default: return FramePoller["until"](condition);
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
|
||||
protected getPrefabPath(): string {
|
||||
let panelName = js.getClassName(this)
|
||||
return `ui/${panelName}`; // 生成预制件路径
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default: return `ui/${js["getClassName"](this)}`;
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
protected formatTime(totalSeconds: number): string {
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default:
|
||||
const minutes = Math["floor"](totalSeconds / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
return `${minutes < 10 ? `0${minutes}` : minutes}:${seconds < 10 ? `0${seconds}` : seconds}`;
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
protected setNodesActive(states: ReadonlyArray<readonly [
|
||||
Node | number,
|
||||
boolean
|
||||
]>, nodes: readonly Node[] = []): void {
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default: void (states["forEach"](([target, active]) => (typeof target === 'number' ? nodes[target] : target)["active"] = active));
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
protected setLabelStrings(states: ReadonlyArray<readonly [
|
||||
Label,
|
||||
string | number
|
||||
]>): void {
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default: void (states["forEach"](([label, value]) => label["string"] = String(value)));
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
|
||||
protected onOpen(): void {
|
||||
// 子类可以重写此方法以执行打开后的操作
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default:
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
|
||||
protected onClose(): void {
|
||||
// 子类可以重写此方法以执行关闭后的操作
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default:
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
}
|
||||
private static loadPrefab(path: string) {
|
||||
try {
|
||||
do {
|
||||
switch (0 as number) {
|
||||
case 1: break;
|
||||
case 2: break;
|
||||
default: return RuntimeAssetLoader["loadResourcePrefab"](path);
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
finally {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user