From f9358eb24ee8c53027eaa28cf94bf09c1b421d81 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Thu, 10 Oct 2024 19:47:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20appendChild=E6=97=B6=E6=B2=A1=E6=9C=89re?= =?UTF-8?q?moveChild?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/render/container.ts | 39 ++++++++++++++++++++++-------------- src/core/render/item.ts | 3 +-- src/core/render/shader.ts | 6 ++---- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/core/render/container.ts b/src/core/render/container.ts index fb8d862..6ba73da 100644 --- a/src/core/render/container.ts +++ b/src/core/render/container.ts @@ -13,7 +13,7 @@ export class Container extends RenderItem implements IRenderChildable { - children: RenderItem[] = []; + children: Set = new Set(); sortedChildren: RenderItem[] = []; private needSort: boolean = false; @@ -46,13 +46,7 @@ export class Container }); } - /** - * 添加子元素到这个容器上,然后在下一个tick执行更新 - * @param children 要添加的子元素 - */ - appendChild(...children: RenderItem[]) { - children.forEach(v => (v.parent = this)); - this.children.push(...children); + private requestSort() { if (!this.needSort) { this.needSort = true; this.requestBeforeFrame(() => { @@ -60,23 +54,38 @@ export class Container this.sortChildren(); }); } + } + + /** + * 添加子元素到这个容器上,然后在下一个tick执行更新 + * @param children 要添加的子元素 + */ + appendChild(...children: RenderItem[]) { + children.forEach(v => { + v.remove(); + this.children.add(v); + v.parent = this; + }); + children.forEach(v => (v.parent = this)); + this.requestSort(); this.update(this); } removeChild(...child: RenderItem[]): void { + let changed = false; child.forEach(v => { - const index = this.children.indexOf(v); - if (index === -1) return; - this.children.splice(index, 1); + const success = this.children.delete(v); + if (success) changed = true; + v.parent = void 0; }); - this.sortChildren(); + if (changed) this.requestSort(); this.update(this); } sortChildren() { - this.sortedChildren = this.children - .slice() - .sort((a, b) => a.zIndex - b.zIndex); + this.sortedChildren = [...this.children].sort( + (a, b) => a.zIndex - b.zIndex + ); } destroy(): void { diff --git a/src/core/render/item.ts b/src/core/render/item.ts index c828775..fa2d5f4 100644 --- a/src/core/render/item.ts +++ b/src/core/render/item.ts @@ -52,7 +52,7 @@ interface IRenderConfig { } export interface IRenderChildable { - children: RenderItem[]; + children: Set; /** * 向这个元素添加子元素 @@ -407,7 +407,6 @@ export abstract class RenderItem */ remove() { this.parent?.removeChild(this); - this.parent = void 0; } /** diff --git a/src/core/render/shader.ts b/src/core/render/shader.ts index 94121c8..15dde9c 100644 --- a/src/core/render/shader.ts +++ b/src/core/render/shader.ts @@ -813,16 +813,14 @@ class ShaderProgram { /** * 获取一个 uniform matrix,需要事先定义,否则返回null * @param matrix uniform matrix 的名称 - * @returns */ getMatrix(matrix: string): ShaderUniformMatrix | null { return this.matrix.get(matrix) ?? null; } /** - * 获取一个 uniform block,例如 UBO VAO VBO,需要事先定义,否则返回null + * 获取一个 uniform block,例如 UBO,需要事先定义,否则返回null * @param block uniform block 的名称 - * @returns */ getUniformBlock(block: string): ShaderUniformBlock | null { return this.block.get(block) ?? null; @@ -923,7 +921,7 @@ class ShaderProgram { } /** - * 定义一个 uniform block,例如 UBO VAO VBO,并存入本着色器程序的 uniform block 映射 + * 定义一个 uniform block,例如 UBO,并存入本着色器程序的 uniform block 映射 * 用于一次性向着色器传输大量数据 * @param uniform uniform block 名称 * @returns uniform block 的操作对象,可用于设置其值