Skip to content

Commit 52b9d89

Browse files
Julien Moreau-Mathisjulien-moreau
authored andcommitted
feat: add support of nodes in command palette to quickly select and focus a node in the scene
1 parent 2f7f693 commit 52b9d89

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

editor/src/editor/dialogs/command-palette/command-palette.tsx

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { dirname, join, extname, basename } from "path/posix";
22

33
import { Component, ReactNode } from "react";
44

5+
import { IoMdCube } from "react-icons/io";
56
import { FaFileAlt } from "react-icons/fa";
67
import { FaCirclePlus } from "react-icons/fa6";
78
import { IoSparklesSharp } from "react-icons/io5";
89
import { HiMiniCommandLine } from "react-icons/hi2";
910

11+
import { Node, IParticleSystem, Sound } from "babylonjs";
12+
1013
import { normalizedGlob } from "../../../tools/fs";
14+
import { isNode } from "../../../tools/guards/nodes";
1115
import { onSelectedAssetChanged } from "../../../tools/observables";
1216

1317
import { Editor } from "../../main";
@@ -29,6 +33,7 @@ export interface ICommandPaletteState {
2933
query: string;
3034

3135
files: ICommandPaletteType[];
36+
entities: ICommandPaletteType[];
3237
}
3338

3439
export interface ICommandPaletteType {
@@ -47,6 +52,7 @@ export class CommandPalette extends Component<ICommandPaletteProps, ICommandPale
4752
query: "",
4853
open: false,
4954
files: [],
55+
entities: [],
5056
};
5157
}
5258

@@ -91,6 +97,14 @@ export class CommandPalette extends Component<ICommandPaletteProps, ICommandPale
9197
))}
9298
</CommandGroup>
9399

100+
<CommandGroup heading="Hierarchy">
101+
{this.state.entities.map((entity) => (
102+
<CommandItem key={entity.key} onSelect={() => this._executeCommand(entity)} className="flex items-center gap-2">
103+
<IoMdCube className="w-10 h-10" /> {entity.text}
104+
</CommandItem>
105+
))}
106+
</CommandGroup>
107+
94108
<CommandGroup heading="Files">
95109
{this.state.files.map((file) => (
96110
<CommandItem key={file.key} onSelect={() => this._executeCommand(file)} className="flex items-center gap-2">
@@ -107,6 +121,7 @@ export class CommandPalette extends Component<ICommandPaletteProps, ICommandPale
107121
this.setState({ open });
108122

109123
if (open) {
124+
this._refreshEntities();
110125
this._refreshAssetFiles();
111126
}
112127
}
@@ -116,6 +131,32 @@ export class CommandPalette extends Component<ICommandPaletteProps, ICommandPale
116131
this.setOpen(false);
117132
}
118133

134+
private _refreshEntities(): void {
135+
const scene = this.props.editor.layout.preview.scene;
136+
137+
const objects = [...scene.meshes, ...scene.lights, ...scene.cameras, ...scene.particleSystems, ...(scene.sounds ?? [])] as (Node | IParticleSystem | Sound)[];
138+
const entities = objects.map(
139+
(entity) =>
140+
({
141+
key: entity.id,
142+
text: entity.name,
143+
label: entity.name,
144+
action: () => {
145+
this.props.editor.layout.graph.setSelectedNode(entity);
146+
this.props.editor.layout.inspector.setEditedObject(entity);
147+
this.props.editor.layout.animations.setEditedObject(entity);
148+
if (isNode(entity)) {
149+
this.props.editor.layout.preview.gizmo.setAttachedNode(entity);
150+
}
151+
152+
this.props.editor.layout.preview.focusObject(entity);
153+
},
154+
}) as ICommandPaletteType
155+
);
156+
157+
this.setState({ entities });
158+
}
159+
119160
private async _refreshAssetFiles(): Promise<void> {
120161
if (!this.props.editor.state.projectPath) {
121162
return;
@@ -129,12 +170,15 @@ export class CommandPalette extends Component<ICommandPaletteProps, ICommandPale
129170
},
130171
});
131172

132-
const files = glob.map((file) => ({
133-
key: basename(file),
134-
text: basename(file),
135-
label: file.path,
136-
action: () => onSelectedAssetChanged.notifyObservers(file),
137-
}));
173+
const files = glob.map(
174+
(file) =>
175+
({
176+
key: basename(file),
177+
text: basename(file),
178+
label: file.path,
179+
action: () => onSelectedAssetChanged.notifyObservers(file),
180+
}) as ICommandPaletteType
181+
);
138182

139183
this.setState({ files });
140184
}

0 commit comments

Comments
 (0)