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> extends RenderItem<E | EContainerEvent>
implements IRenderChildable implements IRenderChildable
{ {
children: RenderItem[] = []; children: Set<RenderItem> = new Set();
sortedChildren: RenderItem[] = []; sortedChildren: RenderItem[] = [];
private needSort: boolean = false; private needSort: boolean = false;
@ -46,13 +46,7 @@ export class Container<E extends EContainerEvent = EContainerEvent>
}); });
} }
/** private requestSort() {
* tick执行更新
* @param children
*/
appendChild(...children: RenderItem<any>[]) {
children.forEach(v => (v.parent = this));
this.children.push(...children);
if (!this.needSort) { if (!this.needSort) {
this.needSort = true; this.needSort = true;
this.requestBeforeFrame(() => { this.requestBeforeFrame(() => {
@ -60,23 +54,38 @@ export class Container<E extends EContainerEvent = EContainerEvent>
this.sortChildren(); 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); this.update(this);
} }
removeChild(...child: RenderItem<any>[]): void { removeChild(...child: RenderItem<any>[]): void {
let changed = false;
child.forEach(v => { child.forEach(v => {
const index = this.children.indexOf(v); const success = this.children.delete(v);
if (index === -1) return; if (success) changed = true;
this.children.splice(index, 1); v.parent = void 0;
}); });
this.sortChildren(); if (changed) this.requestSort();
this.update(this); this.update(this);
} }
sortChildren() { sortChildren() {
this.sortedChildren = this.children this.sortedChildren = [...this.children].sort(
.slice() (a, b) => a.zIndex - b.zIndex
.sort((a, b) => a.zIndex - b.zIndex); );
} }
destroy(): void { destroy(): void {

View File

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

View File

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