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.

refactor: centralize component defaults and use entityMap lookups

esengine62291cacf8f4
diff --git a/editor/src/Editor.ts b/editor/src/Editor.ts
index 8dcde626f..dd4c72dfd 100644
--- a/editor/src/Editor.ts
+++ b/editor/src/Editor.ts
@@ -18,6 +18,7 @@ import { icons } from './utils/icons';
import { ScriptLoader } from './scripting';
import { PreviewService } from './preview';
import { showBuildSettingsDialog, BuildService } from './builder';
+import { getGlobalPathResolver } from './asset';
// =============================================================================
// Types
@@ -377,6 +378,12 @@ export class Editor {
});
(window as any).__esengine_editor = this;
+ (window as any).__esengine_projectPath = this.projectPath_;
+
+ if (this.projectPath_) {
+ const projectDir = this.projectPath_.replace(/[/\\][^/\\]+$/, '');
+ getGlobalPathResolver().setProjectDir(projectDir);
+ }
this.setupToolbarEvents();
this.setupStatusbarEvents();
@@ -515,7 +522,7 @@ export class Editor {
const entity = this.store_.selectedEntity;
if (entity === null) return;
- const entityData = this.store_.scene.entities.find(e => e.id === entity);
+ const entityData = this.store_.getEntityData(entity as number);
if (!entityData) return;
const newEntity = this.store_.createEntity(
diff --git a/editor/src/bridge/EditorBridge.ts b/editor/src/bridge/EditorBridge.ts
index a4b965191..964de90a8 100644
--- a/editor/src/bridge/EditorBridge.ts
+++ b/editor/src/bridge/EditorBridge.ts
@@ -52,7 +52,7 @@ export class EditorBridge {
updateEntityInRuntime(entity: Entity): void {
if (!this.registry_) return;
- const entityData = this.store_.scene.entities.find(e => e.id === entity);
+ const entityData = this.store_.getEntityData(entity as number);
if (!entityData) return;
for (const comp of entityData.components) {
@@ -139,6 +139,23 @@ export class EditorBridge {
farPlane: comp.data.farPlane ?? 1000,
});
break;
+
+ case 'SpineAnimation':
+ this.registry_.addSpineAnimation(entity, {
+ skeletonPath: comp.data.skeletonPath ?? '',
+ atlasPath: comp.data.atlasPath ?? '',
+ skin: comp.data.skin ?? 'default',
+ animation: comp.data.animation ?? '',
+ timeScale: comp.data.timeScale ?? 1,
+ loop: comp.data.loop ?? true,
+ playing: comp.data.playing ?? true,
+ flipX: comp.data.flipX ?? false,
+ flipY: comp.data.flipY ?? false,
+ color: comp.data.color ?? { x: 1, y: 1, z: 1, w: 1 },
+ layer: comp.data.layer ?? 0,
+ skeletonScale: comp.data.skeletonScale ?? 1,
+ });
+ break;
}
}
@@ -166,6 +183,13 @@ export class EditorBridge {
}
this.addComponentToRuntime(entity, comp);
break;
+
+ case 'SpineAnimation':
+ if (this.registry_.hasSpineAnimation(entity)) {
+ this.registry_.removeSpineAnimation(entity);
+ }
+ this.addComponentToRuntime(entity, comp);
+ break;
}
}
}
diff --git a/editor/src/panels/HierarchyPanel.ts b/editor/src/panels/HierarchyPanel.ts
index 5477b8ad9..710991c61 100644
--- a/editor/src/panels/HierarchyPanel.ts
+++ b/editor/src/panels/HierarchyPanel.ts
@@ -8,6 +8,7 @@ import type { EntityData, SceneData } from '../types/SceneTypes';
import type { EditorStore } from '../store/EditorStore';
import { icons } from '../utils/icons';
import { getGlobalPathResolver } from '../asset';
+import { getDefaultComponentData } from '../schemas/ComponentSchemas';
// =============================================================================
// HierarchyPanel
@@ -210,50 +211,21 @@ export class HierarchyPanel {
const newEntity = this.store_.createEntity(baseName, parent);
- this.store_.addComponent(newEntity, 'LocalTransform', {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
+ this.store_.addComponent(newEntity, 'LocalTransform', getDefaultComponentData('LocalTransform'));
this.store_.addComponent(newEntity, 'SpineAnimation', {
+ ...getDefaultComponentData('SpineAnimation'),
skeletonPath,
atlasPath,
- skin: 'default',
- animation: '',
- timeScale: 1,
- loop: true,
- playing: true,
- flipX: false,
- flipY: false,
- color: { x: 1, y: 1, z: 1, w: 1 },
- layer: 0,
- skeletonScale: 1,
});
} else if (asset.type === 'image') {
const newEntity = this.store_.createEntity(baseName, parent);
- this.store_.addComponent(newEntity, 'LocalTransform', {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
-
- this.store_.addComponent(newEntity, 'UIRect', {
- size: { x: 100, y: 100 },
- anchor: { x: 0.5, y: 0.5 },
- pivot: { x: 0.5, y: 0.5 },
- });
+ this.store_.addComponent(newEntity, 'LocalTransform', getDefaultComponentData('LocalTransform'));
this.store_.addComponent(newEntity, 'Sprite', {
+ ...getDefaultComponentData('Sprite'),
texture: this.toRelativePath(asset.path),
- color: { x: 1, y: 1, z: 1, w: 1 },
- size: { x: 100, y: 100 },
- uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 },
- layer: 0,
- flipX: false,
- flipY: false,
});
}
}
@@ -459,82 +431,17 @@ export class HierarchyPanel {
private createEntityWithComponent(componentType: string, parent: Entity | null): void {
const newEntity = this.store_.createEntity(componentType, parent);
- this.store_.addComponent(newEntity, 'LocalTransform', {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
+ this.store_.addComponent(newEntity, 'LocalTransform', getDefaultComponentData('LocalTransform'));
- if (componentType === 'Text' || componentType === 'Sprite') {
- this.store_.addComponent(newEntity, 'UIRect', this.getDefaultComponentData('UIRect'));
+ if (componentType === 'Text') {
+ this.store_.addComponent(newEntity, 'UIRect', getDefaultComponentData('UIRect'));
}
- const defaultData = this.getDefaultComponentData(componentType);
- this.store_.addComponent(newEntity, componentType, defaultData);
- }
-
- private getDefaultComponentData(componentType: string): Record<string, unknown> {
- switch (componentType) {
- case 'Sprite':
- return {
- texture: '',
- color: { x: 1, y: 1, z: 1, w: 1 },
- size: { x: 100, y: 100 },
- uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 },
- layer: 0,
- flipX: false,
- flipY: false,
- };
- case 'Text':
- return {
- content: 'Text',
- fontFamily: 'Arial',
- fontSize: 24,
- color: { x: 1, y: 1, z: 1, w: 1 },
- align: 0,
- verticalAlign: 0,
- wordWrap: true,
- overflow: 0,
- lineHeight: 1.2,
- };
- case 'Camera':
- return {
- isActive: true,
- projectionType: 0,
- fov: 60,
- orthoSize: 5,
- nearPlane: 0.1,
- farPlane: 1000,
- };
- case 'UIRect':
- return {
- size: { x: 100, y: 100 },
- anchor: { x: 0.5, y: 0.5 },
- pivot: { x: 0.5, y: 0.5 },
- };
- case 'SpineAnimation':
- return {
- skeletonPath: '',
- atlasPath: '',
- skin: 'default',
- animation: '',
- timeScale: 1,
- loop: true,
- playing: true,
- flipX: false,
- flipY: false,
- color: { x: 1, y: 1, z: 1, w: 1 },
- layer: 0,
- skeletonScale: 1,
- };
- default:
- return {};
- }
+ this.store_.addComponent(newEntity, componentType, getDefaultComponentData(componentType));
}
private duplicateEntity(entity: Entity): void {
- const entityData = this.store_.scene.entities.find(e => e.id === entity);
+ const entityData = this.store_.getEntityData(entity as number);
if (!entityData) return;
const newEntity = this.store_.createEntity(
diff --git a/editor/src/panels/InspectorPanel.ts b/editor/src/panels/InspectorPanel.ts
index f99f13b8c..2cbcb3ecb 100644
--- a/editor/src/panels/InspectorPanel.ts
+++ b/editor/src/panels/InspectorPanel.ts
@@ -10,7 +10,7 @@ import {
createPropertyEditor,
type PropertyEditorInstance,
} from '../property/PropertyEditor';
-import { getComponentSchema } from '../schemas/ComponentSchemas';
+import { getComponentSchema, getDefaultComponentData } from '../schemas/ComponentSchemas';
import { icons } from '../utils/icons';
import { showAddComponentPopup } from './AddComponentPopup';
import { getPlatformAdapter } from '../platform/PlatformAdapter';
@@ -427,6 +427,8 @@ export class InspectorPanel {
newValue
);
},
+ componentData: component.data,
+ getComponentValue: (name: string) => component.data[name],
});
if (editor) {
@@ -483,39 +485,7 @@ export class InspectorPanel {
}
private createDefaultComponentData(type: string): Record<string, unknown> {
- const schema = getComponentSchema(type);
- if (!schema) return {};
-
- const data: Record<string, unknown> = {};
- for (const prop of schema.properties) {
- switch (prop.type) {
- case 'number':
- data[prop.name] = 0;
- break;
- case 'string':
- data[prop.name] = '';
- break;
- case 'boolean':
- data[prop.name] = false;
- break;
- case 'vec2':
- data[prop.name] = { x: 0, y: 0 };
- break;
- case 'vec3':
- data[prop.name] = { x: 0, y: 0, z: 0 };
- break;
- case 'vec4':
- data[prop.name] = { x: 0, y: 0, z: 0, w: 1 };
- break;
- case 'color':
- data[prop.name] = { x: 1, y: 1, z: 1, w: 1 };
- break;
- case 'enum':
- data[prop.name] = prop.options?.[0]?.value ?? 0;
- break;
- }
- }
- return data;
+ return getDefaultComponentData(type);
}
private updateEditors(): void {
diff --git a/editor/src/schemas/ComponentSchemas.ts b/editor/src/schemas/ComponentSchemas.ts
index cf3f67be6..d9194ce22 100644
--- a/editor/src/schemas/ComponentSchemas.ts
+++ b/editor/src/schemas/ComponentSchemas.ts
@@ -4,6 +4,7 @@
*/
import type { PropertyMeta } from '../property/PropertyEditor';
+import { getComponentDefaults } from 'esengine';
// =============================================================================
// Types
@@ -145,6 +146,25 @@ export const UIRectSchema: ComponentSchema = {
],
};
+export const SpineAnimationSchema: ComponentSchema = {
+ name: 'SpineAnimation',
+ category: 'builtin',
+ properties: [
+ { name: 'skeletonPath', type: 'spine-file', fileFilter: ['.json', '.skel'] },
+ { name: 'atlasPath', type: 'spine-file', fileFilter: ['.atlas'] },
+ { name: 'skin', type: 'spine-skin', dependsOn: 'skeletonPath' },
+ { name: 'animation', type: 'spine-animation', dependsOn: 'skeletonPath' },
+ { name: 'timeScale', type: 'number', min: 0, max: 10, step: 0.1 },
+ { name: 'loop', type: 'boolean' },
+ { name: 'playing', type: 'boolean' },
+ { name: 'flipX', type: 'boolean' },
+ { name: 'flipY', type: 'boolean' },
+ { name: 'color', type: 'color' },
+ { name: 'layer', type: 'number', min: -1000, max: 1000 },
+ { name: 'skeletonScale', type: 'number', min: 0.01, max: 100, step: 0.01 },
+ ],
+};
+
// =============================================================================
// Registry
// =============================================================================
@@ -229,5 +249,43 @@ export function registerBuiltinSchemas(): void {
registerComponentSchema(CameraSchema);
registerComponentSchema(TextSchema);
registerComponentSchema(UIRectSchema);
+ registerComponentSchema(SpineAnimationSchema);
exposeRegistrationAPI();
}
+
+// =============================================================================
+// Default Component Data
+// =============================================================================
+
+const editorOnlyDefaults: Record<string, Record<string, unknown>> = {
+ UIRect: {
+ size: { x: 100, y: 100 },
+ anchor: { x: 0.5, y: 0.5 },
+ pivot: { x: 0.5, y: 0.5 },
+ },
+ Text: {
+ content: 'Text',
+ fontFamily: 'Arial',
+ fontSize: 24,
+ color: { x: 1, y: 1, z: 1, w: 1 },
+ align: 0,
+ verticalAlign: 0,
+ wordWrap: true,
+ overflow: 0,
+ lineHeight: 1.2,
+ },
+};
+
+export function getDefaultComponentData(typeName: string): Record<string, unknown> {
+ const sdkDefaults = getComponentDefaults(typeName);
+ if (sdkDefaults) {
+ return sdkDefaults;
+ }
+
+ const editorDefaults = editorOnlyDefaults[typeName];
+ if (editorDefaults) {
+ return { ...editorDefaults };
+ }
+
+ return {};
+}