From 45ac750ebd2a6f479fc14d5c82f7bde0923e372b Mon Sep 17 00:00:00 2001 From: dgflash Date: Sun, 4 Aug 2024 19:21:47 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=BA=9F=E5=BC=83UIMap=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=202=E3=80=81=E6=B7=BB=E5=8A=A0=E5=BC=B1=E7=BD=91?= =?UTF-8?q?=E6=83=85=E5=86=B5=E6=89=93=E5=BC=80=E7=AA=97=E5=8F=A3=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E5=BC=82=E5=B8=B8=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/core/gui/layer/DelegateComponent.ts | 43 +++++----- assets/core/gui/layer/LayerManager.ts | 32 ++++---- assets/core/gui/layer/LayerPopup.ts | 18 +++-- assets/core/gui/layer/LayerUI.ts | 17 ++-- assets/core/gui/layer/UIMap.ts | 93 ---------------------- assets/core/gui/layer/UIMap.ts.meta | 9 --- assets/core/utils/ViewUtil.ts | 13 ++- assets/module/common/ModuleUtil.ts | 26 +++--- 8 files changed, 83 insertions(+), 168 deletions(-) delete mode 100644 assets/core/gui/layer/UIMap.ts delete mode 100644 assets/core/gui/layer/UIMap.ts.meta diff --git a/assets/core/gui/layer/DelegateComponent.ts b/assets/core/gui/layer/DelegateComponent.ts index df7b1c6..ec9880c 100644 --- a/assets/core/gui/layer/DelegateComponent.ts +++ b/assets/core/gui/layer/DelegateComponent.ts @@ -10,6 +10,10 @@ import { ViewParams } from "./Defines"; const { ccclass } = _decorator; +const EventOnAdded: string = "onAdded"; +const EventOnBeforeRemove: string = "onBeforeRemove"; +const EventOnRemoved: string = "onRemoved"; + /** 窗口事件触发组件 */ @ccclass('DelegateComponent') export class DelegateComponent extends Component { @@ -22,13 +26,23 @@ export class DelegateComponent extends Component { add(): Promise { return new Promise(async (resolve, reject) => { // 触发窗口组件上添加到父节点后的事件 - const r = await this.applyComponentsFunction(this.node, "onAdded", this.vp.params); + for (let i = 0; i < this.node.components.length; i++) { + const component: any = this.node.components[i]; + const func = component[EventOnAdded]; + if (func) { + if (await func.call(component, this.vp.params) == false) { + resolve(false); + return; + } + } + } // 触发外部窗口显示前的事件(辅助实现自定义动画逻辑) if (typeof this.vp.callbacks.onAdded === "function") { this.vp.callbacks.onAdded(this.node, this.vp.params); } - resolve(r); + + resolve(true); }); } @@ -36,7 +50,7 @@ export class DelegateComponent extends Component { remove(isDestroy?: boolean) { if (this.vp.valid) { // 触发窗口移除舞台之前事件 - this.applyComponentsFunction(this.node, "onBeforeRemove", this.vp.params); + this.applyComponentsFunction(this.node, EventOnBeforeRemove, this.vp.params); // 通知外部对象窗口组件上移除之前的事件(关闭窗口前的关闭动画处理) if (typeof this.vp.callbacks.onBeforeRemove === "function") { @@ -82,24 +96,17 @@ export class DelegateComponent extends Component { onDestroy() { // 触发窗口组件上窗口移除之后的事件 - this.applyComponentsFunction(this.node, "onRemoved", this.vp.params); + this.applyComponentsFunction(this.node, EventOnRemoved, this.vp.params); this.vp = null!; } - protected applyComponentsFunction(node: Node, funName: string, params: any): Promise { - return new Promise(async (resolve, reject) => { - var r = true; - for (let i = 0; i < node.components.length; i++) { - const component: any = node.components[i]; - const func = component[funName]; - if (func) { - r = await func.call(component, params); - if (r == false) { - break; - } - } + protected applyComponentsFunction(node: Node, funName: string, params: any) { + for (let i = 0; i < node.components.length; i++) { + const component: any = node.components[i]; + const func = component[funName]; + if (func) { + func.call(component, params); } - resolve(r); - }); + } } } \ No newline at end of file diff --git a/assets/core/gui/layer/LayerManager.ts b/assets/core/gui/layer/LayerManager.ts index c6e04fc..0e21a9a 100644 --- a/assets/core/gui/layer/LayerManager.ts +++ b/assets/core/gui/layer/LayerManager.ts @@ -6,7 +6,6 @@ import { LayerDialog } from "./LayerDialog"; import { LayerNotify } from "./LayerNotify"; import { LayerPopUp } from "./LayerPopup"; import { LayerUI } from "./LayerUI"; -import { UIMap } from "./UIMap"; /** 界面层类型 */ export enum LayerType { @@ -71,8 +70,6 @@ export class LayerManager { game!: Node; /** 新手引导层 */ guide!: Node; - /** 界面地图 */ - uiMap!: UIMap; /** 界面层 */ private ui!: LayerUI; @@ -100,6 +97,14 @@ export class LayerManager { this.configs = configs; } + /** + * 设置窗口打开失败回调 + * @param callback 回调方法 + */ + setOpenFailure(callback: Function) { + this.ui.onOpenFailure = this.popup.onOpenFailure = this.dialog.onOpenFailure = this.system.onOpenFailure = callback; + } + /** * 渐隐飘过提示 * @param content 文本表示 @@ -130,17 +135,6 @@ export class LayerManager { this.configs[uiId] = config; } - /** - * 设置界面地图配置 - * @param data 界面地图数据 - */ - setUIMap(data: any) { - if (this.uiMap == null) { - this.uiMap = new UIMap(); - } - this.uiMap.init(this, data); - } - /** * 同步打开一个窗口 * @param uiId 窗口唯一编号 @@ -221,8 +215,13 @@ export class LayerManager { replaceAsync(removeUiId: number, openUiId: number, uiArgs: any = null): Promise { return new Promise(async (resolve, reject) => { var node = await this.openAsync(openUiId, uiArgs); - this.remove(removeUiId); - resolve(node); + if (node) { + this.remove(removeUiId); + resolve(node); + } + else { + resolve(null); + } }); } @@ -254,6 +253,7 @@ export class LayerManager { result = this.system.has(config.prefab); break; } + return result; } diff --git a/assets/core/gui/layer/LayerPopup.ts b/assets/core/gui/layer/LayerPopup.ts index c93eaa8..f0f1e41 100644 --- a/assets/core/gui/layer/LayerPopup.ts +++ b/assets/core/gui/layer/LayerPopup.ts @@ -30,14 +30,16 @@ export class LayerPopUp extends LayerUI { this.black.enabled = false; } - protected async showUi(vp: ViewParams) { - await super.showUi(vp); + protected async showUi(vp: ViewParams): Promise { + var r = await super.showUi(vp); + if (r) { + // 界面加载完成显示时,启动触摸非窗口区域关闭 + this.openVacancyRemove(vp.config); - // 界面加载完成显示时,启动触摸非窗口区域关闭 - this.openVacancyRemove(vp.config); - - // 界面加载完成显示时,层级事件阻挡 - this.black.enabled = true; + // 界面加载完成显示时,层级事件阻挡 + this.black.enabled = true; + } + return r; } protected onCloseWindow(vp: ViewParams) { @@ -59,6 +61,8 @@ export class LayerPopUp extends LayerUI { /** 关闭遮罩 */ protected closeMask() { + if (this.mask == null) return; + var flag = true; for (var value of this.ui_nodes.values()) { if (value.config.mask) { diff --git a/assets/core/gui/layer/LayerUI.ts b/assets/core/gui/layer/LayerUI.ts index 7c0b037..68ebde9 100644 --- a/assets/core/gui/layer/LayerUI.ts +++ b/assets/core/gui/layer/LayerUI.ts @@ -6,6 +6,8 @@ import { UIConfig } from "./LayerManager"; /** 界面层对象 */ export class LayerUI extends Node { + /** 全局窗口打开失败 */ + onOpenFailure: Function = null!; /** 显示界面节点集合 */ protected ui_nodes = new Map(); /** 被移除的界面缓存数据 */ @@ -62,6 +64,7 @@ export class LayerUI extends Node { protected async load(vp: ViewParams, bundle?: string) { // 加载界面资源超时提示 const timerId = setTimeout(this.onLoadingTimeoutGui, oops.config.game.loadingTimeoutGui); + if (vp && vp.node) { await this.showUi(vp); } @@ -106,10 +109,11 @@ export class LayerUI extends Node { * 创建界面节点 * @param vp 视图参数 */ - protected async showUi(vp: ViewParams) { + protected async showUi(vp: ViewParams): Promise { // 触发窗口添加事件 const comp = vp.node.getComponent(DelegateComponent)!; - if (await comp.add()) { + const r: boolean = await comp.add(); + if (r) { vp.node.parent = this; // 标记界面为使用状态 @@ -119,11 +123,14 @@ export class LayerUI extends Node { console.warn(`路径为【${vp.config.prefab}】的自定义预处理逻辑异常.检查预制上绑定的组件中 onAdded 方法,返回true才能正确完成窗口显示流程`); this.failure(vp); } + return r; } - private failure(vp: ViewParams) { - this.ui_nodes.delete(vp.config.prefab); + /** 打开窗口失败逻辑 */ + protected failure(vp: ViewParams) { + this.onCloseWindow(vp); vp.callbacks && vp.callbacks.onLoadFailure && vp.callbacks.onLoadFailure(); + this.onOpenFailure && this.onOpenFailure(); } /** @@ -161,7 +168,7 @@ export class LayerUI extends Node { private removeCache(prefabPath: string) { let vp = this.ui_cache.get(prefabPath); if (vp) { - this.ui_nodes.delete(vp.config.prefab); + this.onCloseWindow(vp); this.ui_cache.delete(prefabPath); var childNode = vp.node; childNode.destroy(); diff --git a/assets/core/gui/layer/UIMap.ts b/assets/core/gui/layer/UIMap.ts deleted file mode 100644 index 5de5dfc..0000000 --- a/assets/core/gui/layer/UIMap.ts +++ /dev/null @@ -1,93 +0,0 @@ -/* - * @Author: dgflash - * @Date: 2022-06-14 19:35:16 - * @LastEditors: dgflash - * @LastEditTime: 2022-09-02 13:27:20 - */ -import { LayerManager } from "./LayerManager"; - -/** 界面关系树节点 */ -class TreeNode { - id!: number; - /** 父节点编号 */ - pid!: number; - /** 父节点 */ - parent: TreeNode | null = null; - /** 子节点 */ - child: Array = []; - /** 界面名 */ - name!: string; - /** 界面代号(用于同一界面有多条路径时) */ - panel!: string; -} - -/** 用于树形结构两节点之间的寻路功能 */ -export class UIMap { - /** UI层级管理器 */ - private manager!: LayerManager; - /** 界面节点树 */ - private nodes: Map = new Map(); - - /** 创建UI关系树 */ - init(manager: LayerManager, data: any) { - this.manager = manager; - - // 解析数据 - for (var key in data) { - var d = data[key]; - var n = new TreeNode(); - n.id = parseInt(key); - n.pid = d.parent; - n.name = d.name; - n.panel = d.panel; - this.nodes.set(n.id, n); - } - - // 设置节点关系 - this.nodes.forEach((value: TreeNode, key: number) => { - value.parent = this.nodes.get(value.pid)!; - if (value.parent) - value.parent.child.push(value); - }); - } - - /** - * 树节点寻路 - * @param startId 起始节点编号 - * @param endId 结束节点编号 - * @returns - */ - pathFinding(startId: number, endId: number): any { - var start: TreeNode = this.nodes.get(startId)!; - var end: TreeNode = this.nodes.get(endId)!; - - var close: Array = this.findUp(start); - var open: Array = this.findUp(end); - - close.forEach(value => { - this.manager.remove(value.id, true); - }); - - open.forEach(value => { - this.manager.open(value.id); - }); - - return { paths_close: close, paths_open: open }; - } - - /** 向上寻找子节点直到根节点停止,并返回节点路径数组 */ - private findUp(start: TreeNode): TreeNode[] { - var paths: TreeNode[] = []; - var current: TreeNode = start; - while (current.parent != null) { // 父级为空时为根节点 - paths.push(current); - current = current.parent!; - } - return paths; - } - - /** 释放所有节点 */ - release() { - this.nodes.clear(); - } -} diff --git a/assets/core/gui/layer/UIMap.ts.meta b/assets/core/gui/layer/UIMap.ts.meta deleted file mode 100644 index 9046b81..0000000 --- a/assets/core/gui/layer/UIMap.ts.meta +++ /dev/null @@ -1,9 +0,0 @@ -{ - "ver": "4.0.24", - "importer": "typescript", - "imported": true, - "uuid": "1bd03820-b48b-4821-a8e8-a9dd08e1518b", - "files": [], - "subMetas": {}, - "userData": {} -} diff --git a/assets/core/utils/ViewUtil.ts b/assets/core/utils/ViewUtil.ts index 36ab0b8..33a796e 100644 --- a/assets/core/utils/ViewUtil.ts +++ b/assets/core/utils/ViewUtil.ts @@ -103,15 +103,14 @@ export class ViewUtil { */ static createPrefabNodeAsync(path: string): Promise { return new Promise(async (resolve, reject) => { - resLoader.load(path, Prefab, (err: Error | null, content: Prefab) => { - if (err) { - console.error(`名为【${path}】的资源加载失败`); - return; - } - + var prefab = await resLoader.loadAsync(path, Prefab) + if (prefab) { var node = this.createPrefabNode(path); resolve(node); - }); + } + else { + resolve(null!); + } }); } diff --git a/assets/module/common/ModuleUtil.ts b/assets/module/common/ModuleUtil.ts index 61ee70b..2ccb51a 100644 --- a/assets/module/common/ModuleUtil.ts +++ b/assets/module/common/ModuleUtil.ts @@ -58,19 +58,7 @@ export class ModuleUtil { } /** - * 业务实体上移除界面组件 - * @param ent 模块实体 - * @param ctor 界面逻辑组件 - * @param uiId 界面资源编号 - * @param isDestroy 是否释放界面缓存(默认为释放界面缓存) - */ - public static removeViewUi(ent: ecs.Entity, ctor: CompType, uiId: number, isDestroy: boolean = true) { - ent.remove(ctor, isDestroy); - oops.gui.remove(uiId, isDestroy); - } - - /** - * 添加界面组件 + * 通过资源内存中获取预制上的组件添加到ECS实体中 * @param ent 模块实体 * @param ctor 界面逻辑组件 * @param parent 显示对象父级 @@ -86,4 +74,16 @@ export class ModuleUtil { ent.add(comp); node.parent = parent; } + + /** + * 业务实体上移除界面组件 + * @param ent 模块实体 + * @param ctor 界面逻辑组件 + * @param uiId 界面资源编号 + * @param isDestroy 是否释放界面缓存(默认为释放界面缓存) + */ + public static removeViewUi(ent: ecs.Entity, ctor: CompType, uiId: number, isDestroy: boolean = true) { + ent.remove(ctor, isDestroy); + oops.gui.remove(uiId, isDestroy); + } } \ No newline at end of file