优化GUI模块代码

This commit is contained in:
dgflash
2025-08-20 09:26:11 +08:00
parent 876e58096a
commit 6936e09f89
8 changed files with 62 additions and 54 deletions

View File

@@ -4,11 +4,12 @@
* @LastEditors: dgflash
* @LastEditTime: 2025-08-15 10:06:47
*/
import { Layers, Node, NodePool, Prefab, Vec3, warn, Widget } from "cc";
import { Node, NodePool, Prefab, Vec3, warn } from "cc";
import { resLoader } from "../../common/loader/ResLoader";
import { ViewUtil } from "../../utils/ViewUtil";
import { LayerCustomType } from "./LayerEnum";
import { GameElementParams, LayerGameElement } from "./LayerGameElement";
import { LayerHelper } from "./LayerHelper";
import { GameElementConfig } from "./UIConfig";
/* 二维游戏层 */
@@ -18,14 +19,7 @@ export class LayerGame extends Node {
constructor() {
super(LayerCustomType.Game);
const widget: Widget = this.addComponent(Widget);
widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true;
widget.left = widget.right = widget.top = widget.bottom = 0;
widget.alignMode = 2;
widget.enabled = true;
this.layer = Layers.Enum.UI_2D;
LayerHelper.setFullScreen(this);
}
/**

View File

@@ -0,0 +1,20 @@
import { Layers } from "cc";
import { Node, Widget } from "cc";
/** 界面层辅助工具 */
export class LayerHelper {
/**
* 界面层全屏布局
* @param node 全屏布局的节点
*/
static setFullScreen(node: Node) {
const widget: Widget = node.addComponent(Widget);
widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true;
widget.left = widget.right = widget.top = widget.bottom = 0;
widget.alignMode = 2;
widget.enabled = true;
node.layer = Layers.Enum.UI_2D;
return widget;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "2e65136e-fc22-466b-a076-16dfbb12b505",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -1,14 +1,14 @@
import { Camera, Layers, Node, ResolutionPolicy, SafeArea, Widget, screen, view, warn } from "cc";
import { resLoader } from "../../common/loader/ResLoader";
import { oops } from "../../Oops";
import { LayerUIElement, UICallbacks } from "./LayerUIElement";
import { LayerDialog } from "./LayerDialog";
import { LayerCustomType, LayerTypeCls, UIConfigMap, Uiid } from "./LayerEnum";
import { LayerGame } from "./LayerGame";
import { LayerNotify } from "./LayerNotify";
import { LayerPopUp } from "./LayerPopup";
import { LayerUI } from "./LayerUI";
import { LayerUIElement, UICallbacks } from "./LayerUIElement";
import { UIConfig } from "./UIConfig";
import { LayerGame } from "./LayerGame";
/** 界面层级管理器 */
export class LayerManager {
@@ -372,7 +372,7 @@ export class LayerManager {
* @example
* oops.gui.clear();
*/
clear(isDestroy: boolean = false) {
clear(isDestroy: boolean = true) {
this.uiLayers.forEach((layer: LayerUI) => {
layer.clear(isDestroy);
})

View File

@@ -4,15 +4,14 @@
* @LastEditors: dgflash
* @LastEditTime: 2022-09-02 13:44:12
*/
import { BlockInputEvents, Layers, Node, Widget, instantiate } from "cc";
import { BlockInputEvents, Node, instantiate } from "cc";
import { EDITOR } from "cc/env";
import { ViewUtil } from "../../utils/ViewUtil";
import { PromptResType } from "../GuiEnum";
import { Notify } from "../prompt/Notify";
import { LayerHelper } from "./LayerHelper";
/*
* 滚动消息提示层
*/
/* 滚动消息提示层 */
export class LayerNotify extends Node {
private black!: BlockInputEvents;
/** 等待提示资源 */
@@ -24,14 +23,8 @@ export class LayerNotify extends Node {
constructor(name: string) {
super(name);
LayerHelper.setFullScreen(this);
const widget: Widget = this.addComponent(Widget);
widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true;
widget.left = widget.right = widget.top = widget.bottom = 0;
widget.alignMode = 2;
widget.enabled = true;
this.layer = Layers.Enum.UI_2D;
this.black = this.addComponent(BlockInputEvents);
this.black.enabled = false;
}

View File

@@ -3,13 +3,12 @@
* @LastEditors: dgflash
* @LastEditTime: 2022-09-02 13:44:28
*/
import { BlockInputEvents, EventTouch, Layers, Node } from "cc";
import { BlockInputEvents, EventTouch, Node } from "cc";
import { ViewUtil } from "../../utils/ViewUtil";
import { PromptResType } from "../GuiEnum";
import { LayerUI } from "./LayerUI";
import { UIConfig } from "./UIConfig";
import { UIParams } from "./LayerUIElement";
import { UIConfig } from "./UIConfig";
/* 弹窗层,允许同时弹出多个窗口 */
export class LayerPopUp extends LayerUI {
@@ -21,7 +20,6 @@ export class LayerPopUp extends LayerUI {
constructor(name: string) {
super(name);
this.layer = Layers.Enum.UI_2D;
this.on(Node.EventType.CHILD_ADDED, this.onChildAdded, this);
this.on(Node.EventType.CHILD_REMOVED, this.onChildRemoved, this);
}

View File

@@ -1,7 +1,8 @@
import { instantiate, Node, Prefab, SafeArea, Widget } from "cc";
import { instantiate, Node, Prefab, SafeArea } from "cc";
import { Collection } from "db://oops-framework/libs/collection/Collection";
import { oops } from "../../Oops";
import { Uiid } from "./LayerEnum";
import { LayerHelper } from "./LayerHelper";
import { LayerUIElement, UICallbacks, UIParams } from "./LayerUIElement";
import { UIConfig } from "./UIConfig";
@@ -20,12 +21,7 @@ export class LayerUI extends Node {
*/
constructor(name: string) {
super(name);
const widget: Widget = this.addComponent(Widget);
widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true;
widget.left = widget.right = widget.top = widget.bottom = 0;
widget.alignMode = 2;
widget.enabled = true;
LayerHelper.setFullScreen(this);
}
/**
@@ -147,20 +143,20 @@ export class LayerUI extends Node {
if (isDestroy !== undefined) release = isDestroy;
// 界面移出舞台
const vp = this.ui_nodes.get(prefabPath);
if (vp) {
const uip = this.ui_nodes.get(prefabPath);
if (uip) {
// 优先使用参数中控制的释放条件,如果未传递参数则用配置中的释放条件,默认不缓存关闭的界面
if (release === undefined) {
release = vp.config.destroy !== undefined ? vp.config.destroy : true;
release = uip.config.destroy !== undefined ? uip.config.destroy : true;
}
// 不释放界面,缓存起来待下次使用
if (release === false) {
this.ui_cache.set(vp.config.prefab, vp);
this.ui_cache.set(uip.config.prefab, uip);
}
const childNode = vp.node;
const comp = childNode.getComponent(LayerUIElement)!;
const node = uip.node;
const comp = node.getComponent(LayerUIElement)!;
comp.remove(release);
}
@@ -174,12 +170,10 @@ export class LayerUI extends Node {
if (vp) {
this.onCloseWindow(vp);
this.ui_cache.delete(prefabPath);
const childNode = vp.node;
const comp = childNode.getComponent(LayerUIElement)!;
if (comp) {
comp.remove(true);
}
childNode.destroy();
const node = vp.node;
const comp = node.getComponent(LayerUIElement)!;
comp.remove(true);
node.destroy();
}
}
@@ -189,8 +183,7 @@ export class LayerUI extends Node {
*/
get(prefabPath: string): Node {
const vp = this.ui_nodes.get(prefabPath);
if (vp)
return vp.node;
if (vp) return vp.node;
return null!;
}
@@ -208,10 +201,11 @@ export class LayerUI extends Node {
*/
clear(isDestroy: boolean): void {
// 清除所有显示的界面
this.ui_nodes.forEach((value: UIParams, key: string) => {
this.remove(value.config.prefab, isDestroy);
value.valid = false;
});
const length = this.ui_nodes.array.length - 1;
for (let i = length; i >= 0; i--) {
const uip = this.ui_nodes.array[i];
this.remove(uip.config.prefab, isDestroy);
}
this.ui_nodes.clear();
// 清除缓存中的界面

View File

@@ -8,12 +8,12 @@ import { Component, Node, _decorator } from "cc";
import { oops } from "../../Oops";
import { UIConfig } from "./UIConfig";
const { ccclass } = _decorator;
const EventOnAdded: string = "onAdded";
const EventOnBeforeRemove: string = "onBeforeRemove";
const EventOnRemoved: string = "onRemoved";
const { ccclass } = _decorator;
/** 窗口元素组件 */
@ccclass('LayerUIElement')
export class LayerUIElement extends Component {