diff --git a/assets/core/gui/layer/Defines.ts b/assets/core/gui/layer/Defines.ts index ed9cb74..29272db 100644 --- a/assets/core/gui/layer/Defines.ts +++ b/assets/core/gui/layer/Defines.ts @@ -2,7 +2,7 @@ * @Author: dgflash * @Date: 2021-11-18 11:21:32 * @LastEditors: dgflash - * @LastEditTime: 2022-09-13 17:48:48 + * @LastEditTime: 2023-01-09 11:52:38 */ import { Node } from "cc"; @@ -46,15 +46,15 @@ export interface PopViewParams extends UICallbacks { /** 本类型仅供gui模块内部使用,请勿在功能逻辑中使用 */ export class ViewParams { /** 界面唯一标识 */ - uuid!: string; + uuid: string = null!; /** 预制路径 */ - prefabPath!: string; + prefabPath: string = null!; /** 传递给打开界面的参数 */ - params: any | null; + params: any = null!; /** 窗口事件 */ - callbacks!: UICallbacks | null; + callbacks: UICallbacks = null!; /** 是否在使用状态 */ valid: boolean = true; /** 界面根节点 */ - node: Node | null = null; + node: Node = null!; } \ No newline at end of file diff --git a/assets/core/gui/layer/DelegateComponent.ts b/assets/core/gui/layer/DelegateComponent.ts index 2bd4084..8299d60 100644 --- a/assets/core/gui/layer/DelegateComponent.ts +++ b/assets/core/gui/layer/DelegateComponent.ts @@ -2,7 +2,7 @@ * @Author: dgflash * @Date: 2022-09-01 18:00:28 * @LastEditors: dgflash - * @LastEditTime: 2022-09-02 13:38:43 + * @LastEditTime: 2023-01-09 11:55:03 */ import { Component, Node, _decorator } from "cc"; import { oops } from "../../Oops"; @@ -14,37 +14,33 @@ const { ccclass } = _decorator; @ccclass('DelegateComponent') export class DelegateComponent extends Component { /** 视图参数 */ - viewParams: ViewParams | null = null; + viewParams: ViewParams = null!; /** 窗口添加 */ add() { - let viewParams = this.viewParams!; - // 触发窗口组件上添加到父节点后的事件 - this.applyComponentsFunction(this.node, "onAdded", viewParams.params); - if (typeof viewParams.callbacks!.onAdded === "function") { - viewParams.callbacks!.onAdded(this.node, viewParams.params); + this.applyComponentsFunction(this.node, "onAdded", this.viewParams.params); + if (typeof this.viewParams.callbacks.onAdded === "function") { + this.viewParams.callbacks.onAdded(this.node, this.viewParams.params); } } /** 删除节点,该方法只能调用一次,将会触发onBeforeRemoved回调 */ remove(isDestroy: boolean) { - let viewParams = this.viewParams!; - - if (viewParams.valid) { + if (this.viewParams.valid) { // 触发窗口组件上移除之前的事件 - this.applyComponentsFunction(this.node, "onBeforeRemove", viewParams.params); + this.applyComponentsFunction(this.node, "onBeforeRemove", this.viewParams.params); // 通知外部对象窗口组件上移除之前的事件(关闭窗口前的关闭动画处理) - if (typeof viewParams.callbacks!.onBeforeRemove === "function") { - viewParams.callbacks!.onBeforeRemove( + if (typeof this.viewParams.callbacks.onBeforeRemove === "function") { + this.viewParams.callbacks.onBeforeRemove( this.node, () => { - this.removed(viewParams, isDestroy); + this.removed(this.viewParams, isDestroy); }); } else { - this.removed(viewParams, isDestroy); + this.removed(this.viewParams, isDestroy); } } } @@ -53,7 +49,7 @@ export class DelegateComponent extends Component { private removed(viewParams: ViewParams, isDestroy: boolean) { viewParams.valid = false; - if (typeof viewParams.callbacks!.onRemoved === "function") { + if (typeof viewParams.callbacks.onRemoved === "function") { viewParams.callbacks!.onRemoved(this.node, viewParams.params); } @@ -69,17 +65,15 @@ export class DelegateComponent extends Component { } onDestroy() { - let viewParams = this.viewParams!; - // 触发窗口组件上窗口移除之后的事件 - this.applyComponentsFunction(this.node, "onRemoved", viewParams.params); + this.applyComponentsFunction(this.node, "onRemoved", this.viewParams.params); // 通知外部对象窗口移除之后的事件 - if (typeof viewParams.callbacks!.onRemoved === "function") { - viewParams.callbacks!.onRemoved(this.node, viewParams.params); + if (typeof this.viewParams.callbacks!.onRemoved === "function") { + this.viewParams.callbacks!.onRemoved(this.node, this.viewParams.params); } - this.viewParams = null; + this.viewParams = null!; } protected applyComponentsFunction(node: Node, funName: string, params: any) { diff --git a/assets/core/gui/layer/LayerUI.ts b/assets/core/gui/layer/LayerUI.ts index a378676..d3861fb 100644 --- a/assets/core/gui/layer/LayerUI.ts +++ b/assets/core/gui/layer/LayerUI.ts @@ -10,7 +10,7 @@ * size : 当前层上显示的所有Node节点数。 * clear : 清除所有Node节点,队列当中未创建的任务也会被清除。 */ -import { error, instantiate, isValid, Node, Prefab, warn, Widget } from "cc"; +import { Node, Prefab, Widget, error, instantiate, isValid, warn } from "cc"; import { oops } from "../../Oops"; import { UICallbacks, ViewParams } from "./Defines"; import { DelegateComponent } from "./DelegateComponent"; @@ -111,12 +111,12 @@ export class LayerUI extends Node { */ protected createNode(viewParams: ViewParams) { viewParams.valid = true; - let childNode: Node | null = viewParams!.node!; - let comp: DelegateComponent | null = childNode.getComponent(DelegateComponent); - childNode.parent = this; - comp!.add(); - return childNode; + let comp: DelegateComponent = viewParams.node.getComponent(DelegateComponent)!; + viewParams.node.parent = this; + comp.add(); + + return viewParams.node; } /** @@ -131,7 +131,7 @@ export class LayerUI extends Node { // 界面移出舞台 let children = this.__nodes(); for (let i = 0; i < children.length; i++) { - let viewParams = children[i].viewParams!; + let viewParams = children[i].viewParams; if (viewParams.prefabPath === prefabPath) { if (isDestroy) { // 直接释放界面 @@ -159,7 +159,7 @@ export class LayerUI extends Node { this.ui_nodes.delete(viewParams.uuid); var childNode = viewParams.node; - var comp = childNode!.getComponent(DelegateComponent)!; + var comp = childNode.getComponent(DelegateComponent)!; comp.remove(isDestroy); } } @@ -169,9 +169,10 @@ export class LayerUI extends Node { */ private removeCache(prefabPath: string) { let viewParams = this.ui_cache.get(prefabPath); - if (viewParams && viewParams.valid == false) { + if (viewParams) { var childNode = viewParams.node; - childNode!.getComponent(DelegateComponent)!.remove(true); + var comp = childNode.getComponent(DelegateComponent)! + comp.remove(true); this.ui_nodes.delete(viewParams.uuid); this.ui_cache.delete(prefabPath); } @@ -181,14 +182,14 @@ export class LayerUI extends Node { * 根据唯一标识获取节点,如果节点不存在或者还在队列中,则返回null * @param uuid 唯一标识 */ - getByUuid(uuid: string): Node | null { + getByUuid(uuid: string): Node { let children = this.__nodes(); for (let comp of children) { if (comp.viewParams && comp.viewParams.uuid === uuid) { return comp.node; } } - return null; + return null!; } /** @@ -199,7 +200,7 @@ export class LayerUI extends Node { let arr: Array = []; let children = this.__nodes(); for (let comp of children) { - if (comp.viewParams!.prefabPath === prefabPath) { + if (comp.viewParams.prefabPath === prefabPath) { arr.push(comp.node); } } @@ -213,7 +214,7 @@ export class LayerUI extends Node { has(prefabPathOrUUID: string): boolean { let children = this.__nodes(); for (let comp of children) { - if (comp.viewParams!.uuid === prefabPathOrUUID || comp.viewParams!.prefabPath === prefabPathOrUUID) { + if (comp.viewParams.uuid === prefabPathOrUUID || comp.viewParams.prefabPath === prefabPathOrUUID) { return true; } } @@ -228,7 +229,7 @@ export class LayerUI extends Node { let arr: Node[] = []; let children = this.__nodes(); for (let comp of children) { - if (prefabPathReg.test(comp.viewParams!.prefabPath)) { + if (prefabPathReg.test(comp.viewParams.prefabPath)) { arr.push(comp.node); } }