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 gizmo tools for transform manipulation

esengine7a51ebd89742
diff --git a/editor/src/panels/InspectorPanel.ts b/editor/src/panels/InspectorPanel.ts
index 723a5ec23..07da6a8f4 100644
--- a/editor/src/panels/InspectorPanel.ts
+++ b/editor/src/panels/InspectorPanel.ts
@@ -13,6 +13,12 @@ import {
import { getComponentSchema, getAllComponentSchemas } from '../schemas/ComponentSchemas';
import { icons } from '../utils/icons';
+interface EditorInfo {
+ editor: PropertyEditorInstance;
+ componentType: string;
+ propertyName: string;
+}
+
// =============================================================================
// InspectorPanel
// =============================================================================
@@ -22,7 +28,7 @@ export class InspectorPanel {
private store_: EditorStore;
private contentContainer_: HTMLElement;
private unsubscribe_: (() => void) | null = null;
- private editors_: PropertyEditorInstance[] = [];
+ private editors_: EditorInfo[] = [];
private currentEntity_: Entity | null = null;
private currentComponentCount_: number = 0;
@@ -241,7 +247,11 @@ export class InspectorPanel {
});
if (editor) {
- this.editors_.push(editor);
+ this.editors_.push({
+ editor,
+ componentType: component.type,
+ propertyName: propMeta.name,
+ });
}
row.appendChild(label);
@@ -359,12 +369,21 @@ export class InspectorPanel {
}
private updateEditors(): void {
- // TODO: Update existing editors with new values
+ const entityData = this.store_.getSelectedEntityData();
+ if (!entityData) return;
+
+ for (const info of this.editors_) {
+ const component = entityData.components.find(c => c.type === info.componentType);
+ if (component) {
+ const value = component.data[info.propertyName];
+ info.editor.update(value);
+ }
+ }
}
private disposeEditors(): void {
- for (const editor of this.editors_) {
- editor.dispose();
+ for (const info of this.editors_) {
+ info.editor.dispose();
}
this.editors_ = [];
}
diff --git a/editor/src/panels/SceneViewPanel.ts b/editor/src/panels/SceneViewPanel.ts
index 51141ef56..cae12a897 100644
--- a/editor/src/panels/SceneViewPanel.ts
+++ b/editor/src/panels/SceneViewPanel.ts
@@ -1,11 +1,36 @@
/**
* @file SceneViewPanel.ts
- * @brief Scene viewport panel with canvas rendering
+ * @brief Scene viewport panel with canvas rendering and gizmo tools
*/
import type { Entity } from 'esengine';
import type { EditorStore } from '../store/EditorStore';
import type { EditorBridge } from '../bridge/EditorBridge';
+import { icons } from '../utils/icons';
+
+// =============================================================================
+// Types
+// =============================================================================
+
+type GizmoMode = 'select' | 'move' | 'rotate' | 'scale';
+type DragAxis = 'none' | 'x' | 'y' | 'xy';
+
+interface GizmoColors {
+ x: string;
+ y: string;
+ xy: string;
+ hover: string;
+}
+
+const GIZMO_COLORS: GizmoColors = {
+ x: '#e74c3c',
+ y: '#2ecc71',
+ xy: '#f1c40f',
+ hover: '#ffffff',
+};
+
+const GIZMO_SIZE = 80;
+const GIZMO_HANDLE_SIZE = 10;
// =============================================================================
// SceneViewPanel
@@ -27,6 +52,18 @@ export class SceneViewPanel {
private lastMouseX_ = 0;
private lastMouseY_ = 0;
+ private gizmoMode_: GizmoMode = 'move';
+ private dragAxis_: DragAxis = 'none';
+ private hoveredAxis_: DragAxis = 'none';
+ private isGizmoDragging_ = false;
+ private gizmoDragStartX_ = 0;
+ private gizmoDragStartY_ = 0;
+ private gizmoDragStartValue_: { x: number; y: number; z: number } = { x: 0, y: 0, z: 0 };
+ private keydownHandler_: ((e: KeyboardEvent) => void) | null = null;
+ private skipNextClick_ = false;
+ private boundOnDocumentMouseMove_: ((e: MouseEvent) => void) | null = null;
+ private boundOnDocumentMouseUp_: ((e: MouseEvent) => void) | null = null;
+
constructor(container: HTMLElement, store: EditorStore) {
this.container_ = container;
this.store_ = store;
@@ -36,7 +73,14 @@ export class SceneViewPanel {
<div class="es-panel-header">
<span class="es-panel-title">Scene</span>
<div class="es-sceneview-tools">
- <button class="es-btn es-btn-icon" data-action="reset-view" title="Reset View">⟲</button>
+ <div class="es-gizmo-toolbar">
+ <button class="es-btn es-btn-icon es-gizmo-btn" data-mode="select" title="Select (Q)">${icons.pointer(14)}</button>
+ <button class="es-btn es-btn-icon es-gizmo-btn es-active" data-mode="move" title="Move (W)">${icons.move(14)}</button>
+ <button class="es-btn es-btn-icon es-gizmo-btn" data-mode="rotate" title="Rotate (E)">${icons.rotateCw(14)}</button>
+ <button class="es-btn es-btn-icon es-gizmo-btn" data-mode="scale" title="Scale (R)">${icons.maximize(14)}</button>
+ </div>
+ <div class="es-toolbar-divider"></div>
+ <button class="es-btn es-btn-icon" data-action="reset-view" title="Reset View">${icons.refresh(14)}</button>
<span class="es-zoom-display">100%</span>
</div>
</div>
@@ -65,6 +109,11 @@ export class SceneViewPanel {
this.resizeObserver_.disconnect();
this.resizeObserver_ = null;
}
+ if (this.keydownHandler_) {
+ document.removeEventListener('keydown', this.keydownHandler_);
+ this.keydownHandler_ = null;
+ }
+ this.stopDocumentDrag();
}
setBridge(bridge: EditorBridge): void {
@@ -101,12 +150,41 @@ export class SceneViewPanel {
this.requestRender();
});
+ const gizmoButtons = this.container_.querySelectorAll('.es-gizmo-btn');
+ gizmoButtons.forEach(btn => {
+ btn.addEventListener('click', () => {
+ const mode = (btn as HTMLElement).dataset.mode as GizmoMode;
+ if (mode) this.setGizmoMode(mode);
+ });
+ });
+
+ this.keydownHandler_ = (e: KeyboardEvent) => {
+ if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
+ return;
+ }
+ switch (e.key.toLowerCase()) {
+ case 'q':
+ this.setGizmoMode('select');
+ break;
+ case 'w':
+ this.setGizmoMode('move');
+ break;
+ case 'e':
+ this.setGizmoMode('rotate');
+ break;
+ case 'r':
+ this.setGizmoMode('scale');
+ break;
+ }
+ };
+ document.addEventListener('keydown', this.keydownHandler_);
+
this.canvas_.addEventListener('mousedown', this.onMouseDown.bind(this));
this.canvas_.addEventListener('mousemove', this.onMouseMove.bind(this));
this.canvas_.addEventListener('mouseup', this.onMouseUp.bind(this));
- this.canvas_.addEventListener('mouseleave', this.onMouseUp.bind(this));
+ this.canvas_.addEventListener('mouseleave', this.onMouseLeave.bind(this));
this.canvas_.addEventListener('wheel', this.onWheel.bind(this));
- this.canvas_.addEventListener('click', this.onClick.bind(this));
+ this.canvas_.addEventListener('click', this.onCanvasClick.bind(this));
const viewport = this.container_.querySelector('.es-sceneview-viewport');
if (viewport) {
@@ -117,33 +195,303 @@ export class SceneViewPanel {
}
}
+ private setGizmoMode(mode: GizmoMode): void {
+ this.gizmoMode_ = mode;
+ const buttons = this.container_.querySelectorAll('.es-gizmo-btn');
+ buttons.forEach(btn => {
+ const btnMode = (btn as HTMLElement).dataset.mode;
+ btn.classList.toggle('es-active', btnMode === mode);
+ });
+ this.requestRender();
+ }
+
private onMouseDown(e: MouseEvent): void {
if (e.button === 1 || (e.button === 0 && e.altKey)) {
this.isDragging_ = true;
this.lastMouseX_ = e.clientX;
this.lastMouseY_ = e.clientY;
this.canvas_.style.cursor = 'grabbing';
+ this.startDocumentDrag();
+ return;
+ }
+
+ if (e.button !== 0) return;
+
+ const { worldX, worldY } = this.screenToWorld(e.clientX, e.clientY);
+
+ if (this.gizmoMode_ !== 'select' && this.store_.selectedEntity !== null) {
+ const axis = this.hitTestGizmo(worldX, worldY);
+ if (axis !== 'none') {
+ this.isGizmoDragging_ = true;
+ this.dragAxis_ = axis;
+ this.gizmoDragStartX_ = worldX;
+ this.gizmoDragStartY_ = worldY;
+
+ const entityData = this.store_.getSelectedEntityData();
+ const transform = entityData?.components.find(c => c.type === 'LocalTransform');
+ if (transform) {
+ if (this.gizmoMode_ === 'rotate') {
+ const quat = transform.data.rotation as { x: number; y: number; z: number; w: number };
+ this.gizmoDragStartValue_ = this.quatToEuler(quat ?? { x: 0, y: 0, z: 0, w: 1 });
+ } else {
+ const prop = this.gizmoMode_ === 'scale' ? 'scale' : 'position';
+ const value = transform.data[prop] as { x: number; y: number; z: number };
+ this.gizmoDragStartValue_ = { ...value };
+ }
+ }
+ this.startDocumentDrag();
+ return;
+ }
+ }
+
+ this.lastMouseX_ = e.clientX;
+ this.lastMouseY_ = e.clientY;
+ }
+
+ private startDocumentDrag(): void {
+ this.boundOnDocumentMouseMove_ = this.onDocumentMouseMove.bind(this);
+ this.boundOnDocumentMouseUp_ = this.onDocumentMouseUp.bind(this);
+ document.addEventListener('mousemove', this.boundOnDocumentMouseMove_);
+ document.addEventListener('mouseup', this.boundOnDocumentMouseUp_);
+ }
+
+ private stopDocumentDrag(): void {
+ if (this.boundOnDocumentMouseMove_) {
+ document.removeEventListener('mousemove', this.boundOnDocumentMouseMove_);
+ this.boundOnDocumentMouseMove_ = null;
+ }
+ if (this.boundOnDocumentMouseUp_) {
+ document.removeEventListener('mouseup', this.boundOnDocumentMouseUp_);
+ this.boundOnDocumentMouseUp_ = null;
}
}
+ private onDocumentMouseMove(e: MouseEvent): void {
+ this.onMouseMove(e);
+ }
+
+ private onDocumentMouseUp(e: MouseEvent): void {
+ this.onMouseUp(e);
+ this.stopDocumentDrag();
+ }
+
private onMouseMove(e: MouseEvent): void {
- if (!this.isDragging_) return;
+ const { worldX, worldY } = this.screenToWorld(e.clientX, e.clientY);
- const dx = e.clientX - this.lastMouseX_;
- const dy = e.clientY - this.lastMouseY_;
+ if (this.isDragging_) {
+ const dx = e.clientX - this.lastMouseX_;
+ const dy = e.clientY - this.lastMouseY_;
- this.panX_ += dx / this.zoom_;
- this.panY_ += dy / this.zoom_;
+ this.panX_ += dx / this.zoom_;
+ this.panY_ += dy / this.zoom_;
- this.lastMouseX_ = e.clientX;
- this.lastMouseY_ = e.clientY;
+ this.lastMouseX_ = e.clientX;
+ this.lastMouseY_ = e.clientY;
+ this.requestRender();
+ return;
+ }
- this.requestRender();
+ if (this.isGizmoDragging_) {
+ this.handleGizmoDrag(worldX, worldY);
+ return;
+ }
+
+ if (this.gizmoMode_ !== 'select' && this.store_.selectedEntity !== null) {
+ const axis = this.hitTestGizmo(worldX, worldY);
+ if (axis !== this.hoveredAxis_) {
+ this.hoveredAxis_ = axis;
+ this.canvas_.style.cursor = axis !== 'none' ? 'pointer' : 'default';
+ this.requestRender();
+ }
+ }
}
private onMouseUp(_e: MouseEvent): void {
- this.isDragging_ = false;
- this.canvas_.style.cursor = 'default';
+ if (this.isDragging_) {
+ this.isDragging_ = false;
+ this.canvas_.style.cursor = 'default';
+ this.skipNextClick_ = true;
+ }
+
+ if (this.isGizmoDragging_) {
+ this.isGizmoDragging_ = false;
+ this.dragAxis_ = 'none';
+ this.skipNextClick_ = true;
+ }
+ }
+
+ private onMouseLeave(_e: MouseEvent): void {
+ if (this.isDragging_ || this.isGizmoDragging_) {
+ return;
+ }
+ this.hoveredAxis_ = 'none';
+ this.requestRender();
+ }
+
+ private onCanvasClick(e: MouseEvent): void {
+ if (this.skipNextClick_) {
+ this.skipNextClick_ = false;
+ return;
+ }
+
+ if (this.isDragging_ || this.isGizmoDragging_) return;
+
+ const { worldX, worldY } = this.screenToWorld(e.clientX, e.clientY);
+
+ if (this.gizmoMode_ !== 'select' && this.store_.selectedEntity !== null) {
+ const axis = this.hitTestGizmo(worldX, worldY);
+ if (axis !== 'none') return;
+ }
+
+ const entity = this.findEntityAtPosition(worldX, worldY);
+ this.store_.selectEntity(entity);
+ }
+
+ private screenToWorld(clientX: number, clientY: number): { worldX: number; worldY: number } {
+ const rect = this.canvas_.getBoundingClientRect();
+ const dpr = window.devicePixelRatio || 1;
+ const x = (clientX - rect.left) * dpr;
+ const y = (clientY - rect.top) * dpr;
+ const w = this.canvas_.width;
+ const h = this.canvas_.height;
+
+ return {
+ worldX: (x - w / 2) / this.zoom_ - this.panX_,
+ worldY: -(y - h / 2) / this.zoom_ + this.panY_,
+ };
+ }
+
+ private getSelectedEntityPosition(): { x: number; y: number; z: number } | null {
+ const entityData = this.store_.getSelectedEntityData();
+ if (!entityData) return null;
+
+ const transform = entityData.components.find(c => c.type === 'LocalTransform');
+ if (!transform) return null;
+
+ return transform.data.position as { x: number; y: number; z: number } ?? { x: 0, y: 0, z: 0 };
+ }
+
+ private hitTestGizmo(worldX: number, worldY: number): DragAxis {
+ const pos = this.getSelectedEntityPosition();
+ if (!pos) return 'none';
+
+ const gizmoScale = GIZMO_SIZE / this.zoom_;
+ const handleSize = GIZMO_HANDLE_SIZE / this.zoom_;
+
+ const dx = worldX - pos.x;
+ const dy = worldY - pos.y;
+
+ if (this.gizmoMode_ === 'move' || this.gizmoMode_ === 'scale') {
+ if (Math.abs(dx) < handleSize * 2 && Math.abs(dy) < handleSize * 2) {
+ return 'xy';
+ }
+ if (dx > 0 && dx < gizmoScale && Math.abs(dy) < handleSize) {
+ return 'x';
+ }
+ if (dy > 0 && dy < gizmoScale && Math.abs(dx) < handleSize) {
+ return 'y';
+ }
+ } else if (this.gizmoMode_ === 'rotate') {
+ const dist = Math.sqrt(dx * dx + dy * dy);
+ if (Math.abs(dist - gizmoScale * 0.8) < handleSize * 2) {
+ return 'xy';
+ }
+ }
+
+ return 'none';
+ }
+
+ private handleGizmoDrag(worldX: number, worldY: number): void {
+ const entity = this.store_.selectedEntity;
+ if (entity === null) return;
+
+ const dx = worldX - this.gizmoDragStartX_;
+ const dy = worldY - this.gizmoDragStartY_;
+
+ if (this.gizmoMode_ === 'move') {
+ let newX = this.gizmoDragStartValue_.x;
+ let newY = this.gizmoDragStartValue_.y;
+
+ if (this.dragAxis_ === 'x' || this.dragAxis_ === 'xy') {
+ newX += dx;
+ }
+ if (this.dragAxis_ === 'y' || this.dragAxis_ === 'xy') {
+ newY += dy;
+ }
+
+ const newPos = { x: newX, y: newY, z: this.gizmoDragStartValue_.z };
+ this.store_.updatePropertyDirect(entity, 'LocalTransform', 'position', newPos);
+ } else if (this.gizmoMode_ === 'scale') {
+ const scaleFactor = 0.01;
+ let newX = this.gizmoDragStartValue_.x;
+ let newY = this.gizmoDragStartValue_.y;
+
+ if (this.dragAxis_ === 'x' || this.dragAxis_ === 'xy') {
+ newX += dx * scaleFactor;
+ }
+ if (this.dragAxis_ === 'y' || this.dragAxis_ === 'xy') {
+ newY += dy * scaleFactor;
+ }
+
+ const newScale = { x: Math.max(0.01, newX), y: Math.max(0.01, newY), z: this.gizmoDragStartValue_.z };
+ this.store_.updatePropertyDirect(entity, 'LocalTransform', 'scale', newScale);
+ } else if (this.gizmoMode_ === 'rotate') {
+ const pos = this.getSelectedEntityPosition();
+ if (!pos) return;
+
+ const startAngle = Math.atan2(
+ this.gizmoDragStartY_ - pos.y,
+ this.gizmoDragStartX_ - pos.x
+ );
+ const currentAngle = Math.atan2(worldY - pos.y, worldX - pos.x);
+ const deltaAngle = (currentAngle - startAngle) * (180 / Math.PI);
+
+ const newRotZ = this.gizmoDragStartValue_.z + deltaAngle;
+ const euler = { x: 0, y: 0, z: newRotZ };
+ const quat = this.eulerToQuat(euler);
+ this.store_.updatePropertyDirect(entity, 'LocalTransform', 'rotation', quat);
+ }
+
+ this.requestRender();
+ }
+
+ private quatToEuler(q: { x: number; y: number; z: number; w: number }): { x: number; y: number; z: number } {
+ const { x, y, z, w } = q;
+
+ const sinrCosp = 2 * (w * x + y * z);
+ const cosrCosp = 1 - 2 * (x * x + y * y);
+ const roll = Math.atan2(sinrCosp, cosrCosp);
+
+ const sinp = 2 * (w * y - z * x);
+ const pitch = Math.abs(sinp) >= 1 ? Math.sign(sinp) * Math.PI / 2 : Math.asin(sinp);
+
+ const sinyCosp = 2 * (w * z + x * y);
+ const cosyCosp = 1 - 2 * (y * y + z * z);
+ const yaw = Math.atan2(sinyCosp, cosyCosp);
+
+ const toDeg = 180 / Math.PI;
+ return { x: roll * toDeg, y: pitch * toDeg, z: yaw * toDeg };
+ }
+
+ private eulerToQuat(euler: { x: number; y: number; z: number }): { x: number; y: number; z: number; w: number } {
+ const toRad = Math.PI / 180;
+ const roll = euler.x * toRad;
+ const pitch = euler.y * toRad;
+ const yaw = euler.z * toRad;
+
+ const cr = Math.cos(roll * 0.5);
+ const sr = Math.sin(roll * 0.5);
+ const cp = Math.cos(pitch * 0.5);
+ const sp = Math.sin(pitch * 0.5);
+ const cy = Math.cos(yaw * 0.5);
+ const sy = Math.sin(yaw * 0.5);
+
+ return {
+ w: cr * cp * cy + sr * sp * sy,
+ x: sr * cp * cy - cr * sp * sy,
+ y: cr * sp * cy + sr * cp * sy,
+ z: cr * cp * sy - sr * sp * cy,
+ };
}
private onWheel(e: WheelEvent): void {
@@ -168,35 +516,31 @@ export class SceneViewPanel {
this.requestRender();
}
- private onClick(e: MouseEvent): void {
- if (this.isDragging_) return;
-
- const rect = this.canvas_.getBoundingClientRect();
- const x = e.clientX - rect.left;
- const y = e.clientY - rect.top;
-
- const worldX = (x - rect.width / 2) / this.zoom_ - this.panX_;
- const worldY = -(y - rect.height / 2) / this.zoom_ - this.panY_;
-
- const entity = this.findEntityAtPosition(worldX, worldY);
- this.store_.selectEntity(entity);
- }
-
private findEntityAtPosition(worldX: number, worldY: number): Entity | null {
const scene = this.store_.scene;
+ const defaultSize = 50;
for (let i = scene.entities.length - 1; i >= 0; i--) {
const entity = scene.entities[i];
const transform = entity.components.find(c => c.type === 'LocalTransform');
- const sprite = entity.components.find(c => c.type === 'Sprite');
- if (!transform || !sprite) continue;
+ if (!transform) continue;
const pos = transform.data.position as { x: number; y: number; z: number };
- const size = sprite.data.size as { x: number; y: number };
+ const scale = transform.data.scale as { x: number; y: number; z: number } ?? { x: 1, y: 1, z: 1 };
+ const sprite = entity.components.find(c => c.type === 'Sprite');
+
+ let w = defaultSize;
+ let h = defaultSize;
- const halfW = (size?.x ?? 100) / 2;
- const halfH = (size?.y ?? 100) / 2;
+ if (sprite) {
+ const size = sprite.data.size as { x: number; y: number };
+ w = (size?.x ?? defaultSize) * Math.abs(scale.x);
+ h = (size?.y ?? defaultSize) * Math.abs(scale.y);
+ }
+
+ const halfW = w / 2;
+ const halfH = h / 2;
if (
worldX >= (pos?.x ?? 0) - halfW &&
@@ -258,6 +602,114 @@ export class SceneViewPanel {
this.drawEntity(ctx, entity, entity.id === selectedEntity);
}
+ if (selectedEntity !== null && this.gizmoMode_ !== 'select') {
+ this.drawGizmo(ctx);
+ }
+
+ ctx.restore();
+ }
+
+ private drawGizmo(ctx: CanvasRenderingContext2D): void {
+ const pos = this.getSelectedEntityPosition();
+ if (!pos) return;
+
+ ctx.save();
+ ctx.translate(pos.x, -pos.y);
+
+ const size = GIZMO_SIZE / this.zoom_;
+ const lineWidth = 2 / this.zoom_;
+ const handleSize = GIZMO_HANDLE_SIZE / this.zoom_;
+ const arrowSize = 8 / this.zoom_;
+
+ if (this.gizmoMode_ === 'move') {
+ ctx.fillStyle = this.hoveredAxis_ === 'xy' ? GIZMO_COLORS.hover : GIZMO_COLORS.xy;
+ ctx.fillRect(-handleSize, -handleSize, handleSize * 2, handleSize * 2);
+
+ ctx.strokeStyle = this.hoveredAxis_ === 'x' ? GIZMO_COLORS.hover : GIZMO_COLORS.x;
+ ctx.lineWidth = lineWidth;
+ ctx.beginPath();
+ ctx.moveTo(0, 0);
+ ctx.lineTo(size, 0);
+ ctx.stroke();
+
+ ctx.fillStyle = this.hoveredAxis_ === 'x' ? GIZMO_COLORS.hover : GIZMO_COLORS.x;
+ ctx.beginPath();
+ ctx.moveTo(size, 0);
+ ctx.lineTo(size - arrowSize, -arrowSize / 2);
+ ctx.lineTo(size - arrowSize, arrowSize / 2);
+ ctx.closePath();
+ ctx.fill();
+
+ ctx.strokeStyle = this.hoveredAxis_ === 'y' ? GIZMO_COLORS.hover : GIZMO_COLORS.y;
+ ctx.beginPath();
+ ctx.moveTo(0, 0);
+ ctx.lineTo(0, -size);
+ ctx.stroke();
+
+ ctx.fillStyle = this.hoveredAxis_ === 'y' ? GIZMO_COLORS.hover : GIZMO_COLORS.y;
+ ctx.beginPath();
+ ctx.moveTo(0, -size);
+ ctx.lineTo(-arrowSize / 2, -size + arrowSize);
+ ctx.lineTo(arrowSize / 2, -size + arrowSize);
+ ctx.closePath();
+ ctx.fill();
+ } else if (this.gizmoMode_ === 'rotate') {
+ const radius = size * 0.8;
+
+ const entityData = this.store_.getSelectedEntityData();
+ const transform = entityData?.components.find(c => c.type === 'LocalTransform');
+ const quat = transform?.data.rotation as { x: number; y: number; z: number; w: number } ?? { x: 0, y: 0, z: 0, w: 1 };
+ const euler = this.quatToEuler(quat);
+ const angleRad = -euler.z * Math.PI / 180;
+
+ ctx.strokeStyle = 'rgba(100, 100, 100, 0.5)';
+ ctx.lineWidth = lineWidth;
+ ctx.beginPath();
+ ctx.moveTo(0, 0);
+ ctx.lineTo(radius, 0);
+ ctx.stroke();
+
+ ctx.strokeStyle = this.hoveredAxis_ === 'xy' ? GIZMO_COLORS.hover : GIZMO_COLORS.xy;
+ ctx.lineWidth = lineWidth * 1.5;
+ ctx.beginPath();
+ ctx.arc(0, 0, radius, 0, Math.PI * 2);
+ ctx.stroke();
+
+ ctx.strokeStyle = '#00aaff';
+ ctx.lineWidth = lineWidth * 2;
+ ctx.beginPath();
+ ctx.moveTo(0, 0);
+ ctx.lineTo(Math.cos(angleRad) * radius, Math.sin(angleRad) * radius);
+ ctx.stroke();
+
+ ctx.fillStyle = '#00aaff';
+ ctx.beginPath();
+ ctx.arc(Math.cos(angleRad) * radius, Math.sin(angleRad) * radius, handleSize, 0, Math.PI * 2);
+ ctx.fill();
+ } else if (this.gizmoMode_ === 'scale') {
+ ctx.fillStyle = this.hoveredAxis_ === 'xy' ? GIZMO_COLORS.hover : GIZMO_COLORS.xy;
+ ctx.fillRect(-handleSize, -handleSize, handleSize * 2, handleSize * 2);
+
+ ctx.strokeStyle = this.hoveredAxis_ === 'x' ? GIZMO_COLORS.hover : GIZMO_COLORS.x;
+ ctx.lineWidth = lineWidth;
+ ctx.beginPath();
+ ctx.moveTo(0, 0);
+ ctx.lineTo(size, 0);
+ ctx.stroke();
+
+ ctx.fillStyle = this.hoveredAxis_ === 'x' ? GIZMO_COLORS.hover : GIZMO_COLORS.x;
+ ctx.fillRect(size - handleSize, -handleSize, handleSize * 2, handleSize * 2);
+
+ ctx.strokeStyle = this.hoveredAxis_ === 'y' ? GIZMO_COLORS.hover : GIZMO_COLORS.y;
+ ctx.beginPath();
+ ctx.moveTo(0, 0);
+ ctx.lineTo(0, -size);
+ ctx.stroke();
+
+ ctx.fillStyle = this.hoveredAxis_ === 'y' ? GIZMO_COLORS.hover : GIZMO_COLORS.y;
+ ctx.fillRect(-handleSize, -size - handleSize, handleSize * 2, handleSize * 2);
+ }
+
ctx.restore();
}
@@ -318,14 +770,15 @@ export class SceneViewPanel {
let w = 50;
let h = 50;
let color = 'rgba(100, 100, 200, 0.5)';
+ const hasSprite = !!sprite;
if (sprite) {
const size = sprite.data.size as { x: number; y: number };
const col = sprite.data.color as { x: number; y: number; z: number; w: number };
if (size) {
- w = size.x * scale.x;
- h = size.y * scale.y;
+ w = size.x * Math.abs(scale.x);
+ h = size.y * Math.abs(scale.y);
}
if (col) {
const r = Math.round(col.x * 255);
@@ -339,12 +792,15 @@ export class SceneViewPanel {
ctx.save();
ctx.translate(pos.x, -pos.y);
- ctx.fillStyle = color;
- ctx.fillRect(-w / 2, -h / 2, w, h);
+ if (hasSprite) {
+ ctx.fillStyle = color;
+ ctx.fillRect(-w / 2, -h / 2, w, h);
+ }
- if (isSelected) {
+ if (isSelected && hasSprite) {
ctx.strokeStyle = '#00aaff';
ctx.lineWidth = 2 / this.zoom_;
+ ctx.setLineDash([]);
ctx.strokeRect(-w / 2, -h / 2, w, h);
ctx.fillStyle = '#00aaff';
diff --git a/editor/src/property/editors.ts b/editor/src/property/editors.ts
index c247ba694..f3599d7d7 100644
--- a/editor/src/property/editors.ts
+++ b/editor/src/property/editors.ts
@@ -420,6 +420,133 @@ function hexToVec4(hex: string): Vec4 {
return { x: r, y: g, z: b, w: 1 };
}
+// =============================================================================
+// Euler Angle Editor (displays degrees, stores as quaternion)
+// =============================================================================
+
+interface Quat {
+ x: number;
+ y: number;
+ z: number;
+ w: number;
+}
+
+interface EulerAngles {
+ x: number;
+ y: number;
+ z: number;
+}
+
+function quatToEuler(q: Quat): EulerAngles {
+ const { x, y, z, w } = q;
+
+ const sinrCosp = 2 * (w * x + y * z);
+ const cosrCosp = 1 - 2 * (x * x + y * y);
+ const roll = Math.atan2(sinrCosp, cosrCosp);
+
+ const sinp = 2 * (w * y - z * x);
+ let pitch: number;
+ if (Math.abs(sinp) >= 1) {
+ pitch = Math.sign(sinp) * Math.PI / 2;
+ } else {
+ pitch = Math.asin(sinp);
+ }
+
+ const sinyCosp = 2 * (w * z + x * y);
+ const cosyCosp = 1 - 2 * (y * y + z * z);
+ const yaw = Math.atan2(sinyCosp, cosyCosp);
+
+ const toDeg = 180 / Math.PI;
+ return {
+ x: roll * toDeg,
+ y: pitch * toDeg,
+ z: yaw * toDeg,
+ };
+}
+
+function eulerToQuat(euler: EulerAngles): Quat {
+ const toRad = Math.PI / 180;
+ const roll = euler.x * toRad;
+ const pitch = euler.y * toRad;
+ const yaw = euler.z * toRad;
+
+ const cr = Math.cos(roll * 0.5);
+ const sr = Math.sin(roll * 0.5);
+ const cp = Math.cos(pitch * 0.5);
+ const sp = Math.sin(pitch * 0.5);
+ const cy = Math.cos(yaw * 0.5);
+ const sy = Math.sin(yaw * 0.5);
+
+ return {
+ w: cr * cp * cy + sr * sp * sy,
+ x: sr * cp * cy - cr * sp * sy,
+ y: cr * sp * cy + sr * cp * sy,
+ z: cr * cp * sy - sr * sp * cy,
+ };
+}
+
+function createEulerEditor(
+ container: HTMLElement,
+ ctx: PropertyEditorContext
+): PropertyEditorInstance {
+ const { value, onChange } = ctx;
+ const quat = (value as Quat) ?? { x: 0, y: 0, z: 0, w: 1 };
+ let currentEuler = quatToEuler(quat);
+
+ const wrapper = document.createElement('div');
+ wrapper.className = 'es-vec-editor es-vec3-editor';
+
+ const inputs: HTMLInputElement[] = [];
+ const labelTexts = ['X', 'Y', 'Z'];
+ const keys: (keyof EulerAngles)[] = ['x', 'y', 'z'];
+ const colorClasses = ['es-vec-x', 'es-vec-y', 'es-vec-z'];
+
+ labelTexts.forEach((labelText, i) => {
+ const group = document.createElement('div');
+ group.className = `es-vec-field ${colorClasses[i]}`;
+
+ const label = document.createElement('span');
+ label.className = 'es-vec-label';
+ label.textContent = labelText;
+
+ const input = document.createElement('input');
+ input.type = 'number';
+ input.className = 'es-input es-input-number';
+ input.step = '0.1';
+ input.value = currentEuler[keys[i]].toFixed(1);
+
+ input.addEventListener('change', () => {
+ currentEuler = { ...currentEuler, [keys[i]]: parseFloat(input.value) || 0 };
+ onChange(eulerToQuat(currentEuler));
+ });
+
+ setupDragLabel(label, input, (newValue) => {
+ currentEuler = { ...currentEuler, [keys[i]]: newValue };
+ onChange(eulerToQuat(currentEuler));
+ }, 1);
+
+ group.appendChild(label);
+ group.appendChild(input);
+ wrapper.appendChild(group);
+ inputs.push(input);
+ });
+
+ container.appendChild(wrapper);
+
+ return {
+ update(v: unknown) {
+ const q = (v as Quat) ?? { x: 0, y: 0, z: 0, w: 1 };
+ currentEuler = quatToEuler(q);
+ inputs[0].value = currentEuler.x.toFixed(1);
+ inputs[1].value = currentEuler.y.toFixed(1);
+ inputs[2].value = currentEuler.z.toFixed(1);
+ },
+ dispose() {
+ wrapper.remove();
+ },
+ };
+}
+
// =============================================================================
// Enum Editor
// =============================================================================
@@ -476,4 +603,5 @@ export function registerBuiltinEditors(): void {
registerPropertyEditor('vec4', createVec4Editor);
registerPropertyEditor('color', createColorEditor);
registerPropertyEditor('enum', createEnumEditor);
+ registerPropertyEditor('euler', createEulerEditor);
}
diff --git a/editor/src/schemas/ComponentSchemas.ts b/editor/src/schemas/ComponentSchemas.ts
index 2134693cf..c759a6b6e 100644
--- a/editor/src/schemas/ComponentSchemas.ts
+++ b/editor/src/schemas/ComponentSchemas.ts
@@ -22,7 +22,7 @@ export const LocalTransformSchema: ComponentSchema = {
name: 'LocalTransform',
properties: [
{ name: 'position', type: 'vec3' },
- { name: 'rotation', type: 'vec4' },
+ { name: 'rotation', type: 'euler' },
{ name: 'scale', type: 'vec3' },
],
};
diff --git a/editor/src/store/EditorStore.ts b/editor/src/store/EditorStore.ts
index 740af9f12..f4249d8ff 100644
--- a/editor/src/store/EditorStore.ts
+++ b/editor/src/store/EditorStore.ts
@@ -210,6 +210,23 @@ export class EditorStore {
return entityData.components.find(c => c.type === type) ?? null;
}
+ updatePropertyDirect(
+ entity: Entity,
+ componentType: string,
+ propertyName: string,
+ newValue: unknown
+ ): void {
+ const entityData = this.state_.scene.entities.find(e => e.id === entity);
+ if (!entityData) return;
+
+ const component = entityData.components.find(c => c.type === componentType);
+ if (!component) return;
+
+ component.data[propertyName] = newValue;
+ this.state_.isDirty = true;
+ this.notify();
+ }
+
// =========================================================================
// Undo/Redo
// =========================================================================
diff --git a/editor/src/styles/editor.css b/editor/src/styles/editor.css
index 44ccedb38..5cc65d98e 100644
--- a/editor/src/styles/editor.css
+++ b/editor/src/styles/editor.css
@@ -564,6 +564,43 @@ svg {
display: block;
}
+.es-gizmo-toolbar {
+ display: flex;
+ gap: 2px;
+ background: var(--es-bg-tertiary);
+ border-radius: 4px;
+ padding: 2px;
+}
+
+.es-gizmo-btn {
+ background: transparent;
+ border: none;
+ border-radius: 3px;
+ padding: 4px;
+ opacity: 0.6;
+}
+
+.es-gizmo-btn:hover {
+ background: var(--es-bg-hover);
+ opacity: 1;
+}
+
+.es-gizmo-btn.es-active {
+ background: var(--es-accent);
+ opacity: 1;
+}
+
+.es-gizmo-btn.es-active svg {
+ stroke: white;
+}
+
+.es-toolbar-divider {
+ width: 1px;
+ height: 20px;
+ background: var(--es-border);
+ margin: 0 4px;
+}
+
/* =============================================================================
* Common Elements
* ============================================================================= */
diff --git a/editor/src/utils/icons.ts b/editor/src/utils/icons.ts
index 4395d351d..a9ae38620 100644
--- a/editor/src/utils/icons.ts
+++ b/editor/src/utils/icons.ts
@@ -38,6 +38,7 @@ import {
Code,
Volume2,
Braces,
+ MousePointer2,
} from 'lucide';
// =============================================================================
@@ -104,6 +105,9 @@ export const icons = {
code: (size?: number) => renderIcon(Code as IconNode, size),
volume: (size?: number) => renderIcon(Volume2 as IconNode, size),
braces: (size?: number) => renderIcon(Braces as IconNode, size),
+ pointer: (size?: number) => renderIcon(MousePointer2 as IconNode, size),
+ rotateCw: (size?: number) => renderIcon(RotateCw as IconNode, size),
+ maximize: (size?: number) => renderIcon(Maximize as IconNode, size),
};
export type IconName = keyof typeof icons;