Skip to content

Commit

Permalink
Fix deletion of descendant controls in the gui editor (#12271)
Browse files Browse the repository at this point in the history
* Fix deletion of descendant controls in the gui editor

* Fix linting.
  • Loading branch information
carolhmj authored Mar 31, 2022
1 parent 6b0d029 commit dd2eb63
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions packages/tools/guiEditor/src/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Control } from "gui/2D/controls/control";
import { LockObject } from "shared-ui-components/tabs/propertyGrids/lockObject";
import type { ISize } from "core/Maths/math";
import { CoordinateHelper } from "./diagram/coordinateHelper";
import { Container } from "gui/2D/controls/container";
import { KeyboardManager } from "./keyboardManager";

export enum DragOverLocation {
Expand Down Expand Up @@ -184,10 +185,37 @@ export class GlobalState {
this.onSelectionChangedObservable.notifyObservers();
}

private _findParentControlInTexture(texture: AdvancedDynamicTexture, searchedControl: Control) {
const searchList = [texture.rootContainer];
while (searchList.length > 0) {
const current = searchList.splice(0, 1)[0];
const children = current._children;
if (children.indexOf(searchedControl) !== -1) {
return current;
}
for (const child of children) {
if (child instanceof Container) {
searchList.push(child);
}
}
}
return null;
}

public deleteSelectedNodes() {
for (const control of this.selectedControls) {
this.guiTexture.removeControl(control);
this.liveGuiTexture?.removeControl(control);
const guiTextureParent = this._findParentControlInTexture(this.guiTexture, control);
guiTextureParent?.removeControl(control);
if (this.liveGuiTexture) {
const allDescendants = control.getDescendants();
for (const descendant of allDescendants) {
const liveGuiTextureDescendantParent = this._findParentControlInTexture(this.liveGuiTexture, descendant);
liveGuiTextureDescendantParent?.removeControl(descendant);
descendant.dispose();
}
const liveGuiTextureParent = this._findParentControlInTexture(this.liveGuiTexture, control);
liveGuiTextureParent?.removeControl(control);
}
control.dispose();
}
this.setSelection([]);
Expand Down

0 comments on commit dd2eb63

Please sign in to comment.