Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: Add scene loading and asset selection in Content Browser

esengine36dfaf0ecc0c
diff --git a/editor/src/Editor.ts b/editor/src/Editor.ts
index 442fee182..dc71c58af 100644
--- a/editor/src/Editor.ts
+++ b/editor/src/Editor.ts
@@ -12,7 +12,7 @@ import { SceneViewPanel } from './panels/SceneViewPanel';
import { ContentBrowserPanel, type ContentBrowserOptions } from './panels/ContentBrowserPanel';
import { registerBuiltinEditors } from './property/editors';
import { registerBuiltinSchemas } from './schemas/ComponentSchemas';
-import { saveSceneToFile, loadSceneFromFile } from './io/SceneSerializer';
+import { saveSceneToFile, loadSceneFromFile, loadSceneFromPath } from './io/SceneSerializer';
import { icons } from './utils/icons';
// =============================================================================
@@ -90,6 +90,14 @@ export class Editor {
}
}
+ async openSceneFromPath(scenePath: string): Promise<void> {
+ const scene = await loadSceneFromPath(scenePath);
+ if (scene) {
+ this.store_.loadScene(scene);
+ console.log('Scene loaded:', scenePath);
+ }
+ }
+
// =========================================================================
// Private Methods
// =========================================================================
@@ -182,6 +190,7 @@ export class Editor {
this.sceneViewPanel_ = new SceneViewPanel(sceneViewContainer, this.store_);
this.contentBrowserPanel_ = new ContentBrowserPanel(contentBrowserContainer, {
projectPath: this.projectPath_ ?? undefined,
+ onOpenScene: (scenePath) => this.openSceneFromPath(scenePath),
});
this.setupToolbarEvents();
diff --git a/editor/src/index.ts b/editor/src/index.ts
index be1fbdb5b..4a0c2f41a 100644
--- a/editor/src/index.ts
+++ b/editor/src/index.ts
@@ -106,6 +106,7 @@ export {
SceneSerializer,
saveSceneToFile,
loadSceneFromFile,
+ loadSceneFromPath,
} from './io';
// =============================================================================
diff --git a/editor/src/io/SceneSerializer.ts b/editor/src/io/SceneSerializer.ts
index 7e2805331..c9741da20 100644
--- a/editor/src/io/SceneSerializer.ts
+++ b/editor/src/io/SceneSerializer.ts
@@ -72,6 +72,7 @@ export class SceneSerializer {
interface NativeFS {
saveFile(content: string, defaultPath?: string): Promise<string | null>;
loadFile(): Promise<{ path: string; content: string } | null>;
+ readFile(path: string): Promise<string | null>;
}
function getNativeFS(): NativeFS | null {
@@ -113,6 +114,26 @@ export async function loadSceneFromFile(): Promise<SceneData | null> {
return browserLoadFile();
}
+export async function loadSceneFromPath(path: string): Promise<SceneData | null> {
+ const nativeFS = getNativeFS();
+ if (!nativeFS) {
+ console.error('Native FS not available');
+ return null;
+ }
+
+ try {
+ const content = await nativeFS.readFile(path);
+ if (content) {
+ const serializer = new SceneSerializer();
+ return serializer.deserialize(content);
+ }
+ return null;
+ } catch (err) {
+ console.error('Failed to load scene from path:', path, err);
+ return null;
+ }
+}
+
// =============================================================================
// Browser File Operations (Fallback)
// =============================================================================
diff --git a/editor/src/io/index.ts b/editor/src/io/index.ts
index 8351affd2..37120eaa8 100644
--- a/editor/src/io/index.ts
+++ b/editor/src/io/index.ts
@@ -3,4 +3,4 @@
* @brief IO module exports
*/
-export { SceneSerializer, saveSceneToFile, loadSceneFromFile } from './SceneSerializer';
+export { SceneSerializer, saveSceneToFile, loadSceneFromFile, loadSceneFromPath } from './SceneSerializer';
diff --git a/editor/src/panels/ContentBrowserPanel.ts b/editor/src/panels/ContentBrowserPanel.ts
index b4093267b..f99f55897 100644
--- a/editor/src/panels/ContentBrowserPanel.ts
+++ b/editor/src/panels/ContentBrowserPanel.ts
@@ -45,6 +45,7 @@ interface AssetItem {
export interface ContentBrowserOptions {
projectPath?: string;
+ onOpenScene?: (scenePath: string) => void;
}
// =============================================================================
@@ -135,10 +136,13 @@ export class ContentBrowserPanel {
private searchFilter_: string = '';
private unwatchFn_: (() => void) | null = null;
private currentItems_: AssetItem[] = [];
+ private onOpenScene_: ((scenePath: string) => void) | null = null;
+ private selectedAssetPath_: string | null = null;
constructor(container: HTMLElement, options?: ContentBrowserOptions) {
this.container_ = container;
this.projectPath_ = options?.projectPath ?? null;
+ this.onOpenScene_ = options?.onOpenScene ?? null;
if (this.projectPath_) {
this.currentPath_ = getParentPath(this.projectPath_);
@@ -319,6 +323,17 @@ export class ContentBrowserPanel {
}
});
+ this.gridContainer_?.addEventListener('click', (e) => {
+ const target = e.target as HTMLElement;
+ const item = target.closest('.es-asset-item') as HTMLElement;
+ if (!item) return;
+
+ const path = item.dataset.path;
+ if (path) {
+ this.selectAsset(path);
+ }
+ });
+
this.gridContainer_?.addEventListener('dblclick', async (e) => {
const target = e.target as HTMLElement;
const item = target.closest('.es-asset-item') as HTMLElement;
@@ -341,8 +356,29 @@ export class ContentBrowserPanel {
});
}
+ private selectAsset(path: string): void {
+ this.selectedAssetPath_ = path;
+ this.updateAssetSelection();
+ }
+
+ private updateAssetSelection(): void {
+ if (!this.gridContainer_) return;
+
+ const items = this.gridContainer_.querySelectorAll('.es-asset-item');
+ items.forEach((item) => {
+ const el = item as HTMLElement;
+ if (el.dataset.path === this.selectedAssetPath_) {
+ el.classList.add('es-selected');
+ } else {
+ el.classList.remove('es-selected');
+ }
+ });
+ }
+
private onAssetDoubleClick(path: string, type: AssetItem['type']): void {
- console.log('Asset double-clicked:', path, type);
+ if (type === 'scene' && this.onOpenScene_) {
+ this.onOpenScene_(path);
+ }
}
private async toggleFolder(path: string): Promise<void> {