fix: appendChild时没有removeChild

This commit is contained in:
unanmed 2024-10-10 19:47:34 +08:00
parent d98bae7144
commit f9358eb24e
3 changed files with 27 additions and 21 deletions

View File

@ -13,7 +13,7 @@ export class Container<E extends EContainerEvent = EContainerEvent>
extends RenderItem<E | EContainerEvent>
implements IRenderChildable
{
children: RenderItem[] = [];
children: Set<RenderItem> = new Set();
sortedChildren: RenderItem[] = [];
private needSort: boolean = false;
@ -46,13 +46,7 @@ export class Container<E extends EContainerEvent = EContainerEvent>
});
}
/**
* tick执行更新
* @param children
*/
appendChild(...children: RenderItem<any>[]) {
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<E extends EContainerEvent = EContainerEvent>
this.sortChildren();
});
}
}
/**
* tick执行更新
* @param children
*/
appendChild(...children: RenderItem<any>[]) {
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<any>[]): 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 {

View File

@ -52,7 +52,7 @@ interface IRenderConfig {
}
export interface IRenderChildable {
children: RenderItem[];
children: Set<RenderItem>;
/**
*
@ -407,7 +407,6 @@ export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
*/
remove() {
this.parent?.removeChild(this);
this.parent = void 0;
}
/**

View File

@ -813,16 +813,14 @@ class ShaderProgram {
/**
* uniform matrixnull
* @param matrix uniform matrix
* @returns
*/
getMatrix(matrix: string): ShaderUniformMatrix | null {
return this.matrix.get(matrix) ?? null;
}
/**
* uniform block UBO VAO VBOnull
* uniform block UBOnull
* @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