Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor camera control and cursor management for improved tool interaction

konstantin-paulus71b18dbf6c93
diff --git a/apps/web/src/engine/camera-controller.tsx b/apps/web/src/engine/camera-controller.tsx
index 5be7ba4a..89a3dc74 100644
--- a/apps/web/src/engine/camera-controller.tsx
+++ b/apps/web/src/engine/camera-controller.tsx
@@ -4,9 +4,8 @@
import { createMemo, onCleanup, onMount } from 'solid-js';
import { useTrait, useWorld } from '@diffusionstudio/koota-solid';
-import { panCamera, setCamera, zoomCameraAt, getCamera, RenderSurface, getCameraMatrix, Root } from '@diffusionstudio/runtime';
+import { panCamera, setCamera, zoomCameraAt, getCamera, RenderSurface, getCameraMatrix, Root, Tool, ToolType } from '@diffusionstudio/runtime';
import { useEditor } from './hooks/use-editor';
-import { updateCursor } from './input/cursor';
import type { JSX } from 'solid-js';
@@ -19,6 +18,7 @@ const MAX_ZOOM_DELTA = 50;
/** Wheel pixels → zoom exponent. Higher = faster ctrl/pinch zoom. */
const ZOOM_SENSITIVITY = 0.01;
+const LEFT_BUTTON = 0;
const MIDDLE_BUTTON = 1;
function isEditable(target: EventTarget | null): boolean {
@@ -63,7 +63,6 @@ export function CameraController(): JSX.Element {
}
panPointerId = null;
- updateCursor(world, spaceHeld ? 'grab' : 'default');
};
const handleWheel = (event: WheelEvent): void => {
@@ -85,8 +84,8 @@ export function CameraController(): JSX.Element {
};
const handlePointerDown = (event: PointerEvent): void => {
- if (panning) return;
- if (!spaceHeld && event.button !== MIDDLE_BUTTON) return;
+ const armed = spaceHeld || world.get(Tool)?.value === ToolType.HAND || panning;
+ if (event.button !== MIDDLE_BUTTON && !(armed && event.button === LEFT_BUTTON)) return;
// Middle-click would otherwise start autoscroll on some platforms.
event.preventDefault();
@@ -99,7 +98,6 @@ export function CameraController(): JSX.Element {
const camera = getCamera(world);
startE = camera.e;
startF = camera.f;
- updateCursor(world, 'grabbing');
};
const handlePointerMove = (event: PointerEvent): void => {
@@ -120,21 +118,18 @@ export function CameraController(): JSX.Element {
const handleKeyDown = (event: KeyboardEvent): void => {
if (event.code !== 'Space' || event.repeat || isEditable(event.target)) return;
spaceHeld = true;
- updateCursor(world, 'grab');
};
const handleKeyUp = (event: KeyboardEvent): void => {
if (event.code !== 'Space') return;
spaceHeld = false;
endPan();
- updateCursor(world, 'default');
};
const handleBlur = (): void => {
// Key-up never arrives when focus leaves mid-hold (⌘-tab, devtools).
spaceHeld = false;
endPan();
- updateCursor(world, 'default');
};
onMount(() => {
diff --git a/apps/web/src/engine/input/cursor.ts b/apps/web/src/engine/input/cursor.ts
index 8b3ecebe..c7aae79e 100644
--- a/apps/web/src/engine/input/cursor.ts
+++ b/apps/web/src/engine/input/cursor.ts
@@ -13,7 +13,7 @@
* through `@/hooks/use-cursor`, which is where the trim cursors live.
*/
-import { RenderSurface } from '@diffusionstudio/runtime';
+import { RenderSurface, Tool, ToolType } from '@diffusionstudio/runtime';
import type { World } from 'koota';
@@ -67,3 +67,32 @@ export function updateCursor(world: World, type: CursorType, rotation = 0, color
? type
: CURSORS[type]?.(rotation * 180 / Math.PI, color) ?? 'default';
}
+
+/**
+ * What each tool points the cursor at while it is armed: `idle`, and
+ * `pressed` where the answer changes while the button is down. There is
+ * always a
+ * tool armed, so there is always an answer — adding a `ToolType` without a
+ * row here does not compile.
+ *
+ * This is the resting cursor, not the last word: what the pointer is over
+ * may say something more specific for as long as it is over it (the move
+ * tool's resize and rotate handles do, see `updateResizeCursor`). The tool
+ * takes the cursor back whenever its own answer changes, which is what
+ * `inputSystem` watches.
+ */
+const TOOL_CURSORS: Record<ToolType, { idle: CursorType; pressed?: CursorType }> = {
+ [ToolType.MOVE]: { idle: 'default' },
+ [ToolType.HAND]: { idle: 'grab', pressed: 'grabbing' },
+ [ToolType.BLADE]: { idle: 'cross' },
+ [ToolType.SCENE]: { idle: 'cross' },
+ [ToolType.RECT]: { idle: 'cross' },
+ [ToolType.TEXT]: { idle: 'cross' },
+ [ToolType.TEXT_EDIT]: { idle: 'text' },
+};
+
+/** The cursor the armed tool asks for, pressed or at rest. */
+export function getToolCursor(world: World, pressed = false): CursorType {
+ const cursor = TOOL_CURSORS[world.get(Tool)?.value ?? ToolType.MOVE];
+ return pressed ? cursor.pressed ?? cursor.idle : cursor.idle;
+}
diff --git a/apps/web/src/engine/input/input-system.ts b/apps/web/src/engine/input/input-system.ts
index 1f52c3f5..7ab19750 100644
--- a/apps/web/src/engine/input/input-system.ts
+++ b/apps/web/src/engine/input/input-system.ts
@@ -5,10 +5,11 @@
import { HitRegions, Tool, ToolType, Dragging, Hovering, isPointerInEntity, pointInQuad } from '@diffusionstudio/runtime';
import { Keys, Pointer, PointerEvents } from '../traits';
-import { updateCursor } from './cursor';
+import { getToolCursor, updateCursor } from './cursor';
import { handleCanvasInteraction, handleGeometryInteraction } from './interactions';
import type { World } from 'koota';
+import type { CursorType } from './cursor';
import type { CanvasPointerEvent, DispatchedPointerEvent, HitRegion, PointerEventType } from '@diffusionstudio/runtime';
/**
@@ -61,6 +62,7 @@ function dispatch(
let hovered: HitRegion | null = null;
let dragged: HitRegion | null = null;
+let toolCursor: CursorType | null = null;
export function inputSystem(world: World) {
const panning = world.get(Tool)?.value === ToolType.HAND || (world.get(Keys)?.held.has(' ') ?? false);
@@ -137,7 +139,6 @@ export function inputSystem(world: World) {
}
if (panning) {
- updateCursor(world, world.get(Pointer)!.phase === 'pressed' ? 'grabbing' : 'grab');
for (const entity of world.query(Hovering)) {
entity.remove(Hovering);
}
@@ -148,6 +149,13 @@ export function inputSystem(world: World) {
dragged = null;
}
+ const cursor = getToolCursor(world, world.get(Pointer)?.phase === 'pressed');
+
+ if (cursor !== toolCursor) {
+ updateCursor(world, cursor);
+ toolCursor = cursor;
+ }
+
queue.length = 0;
regions.length = 0;
}
diff --git a/apps/web/src/engine/input/interactions.ts b/apps/web/src/engine/input/interactions.ts
index dbd1f459..ce50f05c 100644
--- a/apps/web/src/engine/input/interactions.ts
+++ b/apps/web/src/engine/input/interactions.ts
@@ -32,7 +32,7 @@ import { Not, Or } from 'koota';
import { getDocumentEditor } from '../editor';
import { syncKeyframe } from '../keyframes';
import { AssetSelection, Hud, Keys, Pointer, SnapLines } from '../traits';
-import { updateCursor, type CursorType } from './cursor';
+import { getToolCursor, updateCursor, type CursorType } from './cursor';
import { mountNameInput } from '../hud/name-input';
import {
buildSnapCandidatesFromCorners, buildSnapCandidatesFromQuad, findSnapTarget,
@@ -181,7 +181,7 @@ export function handleGeometryInteraction(world: World, event: DispatchedPointer
}
if (world.get(Pointer)!.phase === 'lifted') {
- updateCursor(world, 'default');
+ updateCursor(world, getToolCursor(world));
}
}
@@ -204,7 +204,7 @@ export function handleCanvasInteraction(world: World, event: DispatchedPointerEv
.filter((entity) => quadsIntersect(marquee, entityQuad(world, entity))));
}
- updateCursor(world, 'default');
+ updateCursor(world, getToolCursor(world));
}
if (event.type === 'dragend') {
@@ -225,7 +225,7 @@ export function handleCanvasInteraction(world: World, event: DispatchedPointerEv
}
if (world.get(Pointer)!.phase === 'lifted') {
- updateCursor(world, 'default');
+ updateCursor(world, getToolCursor(world));
}
}
@@ -721,7 +721,7 @@ export function handleMaskInteraction(world: World, event: DispatchedPointerEven
}
if (world.get(Pointer)!.phase === 'lifted') {
- updateCursor(world, 'default');
+ updateCursor(world, getToolCursor(world));
}
}
diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.ts
index 9a744c1e..452ce76a 100644
--- a/apps/web/src/engine/input/shortcuts.ts
+++ b/apps/web/src/engine/input/shortcuts.ts
@@ -1,18 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-/**
- * Keyboard commands on the canvas: what a key press does, as opposed to the
- * held keys that modify a gesture. The DOM listener only records (see
- * `Keys`); the shortcut system runs once a frame and, when a key was just
- * pressed, matches what is held against the table below and runs the action.
- * Space is the exception the table cannot hold — it is also the pan
- * modifier, so what it does depends on where the pointer is and on whether
- * the camera moved while it was down; see `playbackShortcut`.
- */
-
-import { Computed, getActiveEntity, getCameraMatrix, getEntityChildren, getParentEntity, getSelection, isGroupLike, Position, Selected, store, togglePlayback } from '@diffusionstudio/runtime';
+import { Computed, getActiveEntity, getCameraMatrix, getEntityChildren, getParentEntity, getSelection, isGroupLike, Position, Selected, store, Time, togglePlayback, Tool, ToolType } from '@diffusionstudio/runtime';
import { getDocumentEditor } from '../editor';
import { splitAtPlayhead } from '../split';
@@ -107,18 +96,55 @@ export function nudgeSelection(world: World, dx: number, dy: number): void {
const nudge = (dx: number, dy: number) => (world: World): void => nudgeSelection(world, dx, dy);
+/** How long space has to be held to read as a pan and not as a tap. */
+const SPACE_HAND_DELAY = 200;
+
/**
* The camera as it stood when space went down over the stage; null when
* space is up, or when the press was one that cannot turn into a pan.
*/
let spaceCamera: CameraMatrix | null = null;
+/**
+ * The tool space borrowed the hand from, put back on release; null while
+ * space does not hold the hand.
+ */
+let spaceTool: ToolType | null = null;
+
+/**
+ * When the space press went down, so a hold long enough to be a pan can be
+ * told from a tap; null once the hand has taken over, or once space is up.
+ */
+let spacePressedAt: number | null = null;
+
+/** Puts the hand in the toolbar for as long as space holds it. */
+function takeHandTool(world: World): void {
+ spacePressedAt = null;
+ if (spaceTool !== null) return;
+ spaceTool = world.get(Tool)?.value ?? ToolType.MOVE;
+ world.set(Tool, { value: ToolType.HAND });
+}
+
+/** Gives back whatever tool space borrowed the hand from. */
+function releaseHandTool(world: World): void {
+ spacePressedAt = null;
+ if (spaceTool === null) return;
+ world.set(Tool, { value: spaceTool });
+ spaceTool = null;
+}
+
function toggleActivePlayback(world: World): void {
const scene = getActiveEntity(world);
if (scene) togglePlayback(world, scene);
}
function onSpacePressed(world: World): void {
+ // The hand waits out the delay wherever the pointer is; what the pointer
+ // decides is only when playback gets its toggle. Over the stage the press
+ // could still become a pan, so playback waits for a release that left the
+ // camera where it was; anywhere else it toggles here and now.
+ spacePressedAt = world.get(Time)?.now ?? 0;
+
if (world.get(Pointer)?.over) {
spaceCamera = getCameraMatrix(world);
} else {
@@ -133,8 +159,41 @@ function onSpaceLifted(world: World): void {
toggleActivePlayback(world);
}
spaceCamera = null;
+ releaseHandTool(world);
}
+/**
+ * What space does by being held rather than by moving, which the tables
+ * cannot express: the hand takes over once the press outlasts
+ * `SPACE_HAND_DELAY`, and a hold the window loses focus mid-way through
+ * never sees a release (`held` is cleared without a lift), so the borrowed
+ * tool goes back on the first frame the key is no longer down.
+ */
+function updateSpaceHold(world: World, held: Set<string>): void {
+ if (!held.has(' ')) {
+ spaceCamera = null;
+ releaseHandTool(world);
+ return;
+ }
+
+ if (spacePressedAt === null) return;
+ if ((world.get(Time)?.now ?? 0) - spacePressedAt >= SPACE_HAND_DELAY) {
+ takeHandTool(world);
+ }
+}
+
+/**
+ * Picks a tool. While space holds the hand the pick is what the release
+ * goes back to, so the hand keeps the stage until the key is up.
+ */
+const selectTool = (value: ToolType) => (world: World): void => {
+ if (spaceTool !== null) {
+ spaceTool = value;
+ return;
+ }
+ world.set(Tool, { value });
+};
+
const PRESSED_SHORTCUTS: readonly Shortcut[] = [
{ keys: ['backspace'], action: deleteSelection },
{ keys: ['delete'], action: deleteSelection },
@@ -142,6 +201,8 @@ const PRESSED_SHORTCUTS: readonly Shortcut[] = [
{ keys: ['b', 'mod'], action: splitAtPlayhead },
{ keys: ['c', 'mod'], action: copySelection },
{ keys: ['v', 'mod'], action: pasteSelection },
+ { keys: ['v', '!mod'], action: selectTool(ToolType.MOVE) },
+ { keys: ['h', '!mod'], action: selectTool(ToolType.HAND) },
{ keys: ['arrowleft', '!shift'], action: nudge(-NUDGE, 0) },
{ keys: ['arrowright', '!shift'], action: nudge(NUDGE, 0) },
{ keys: ['arrowup', '!shift'], action: nudge(0, -NUDGE) },
@@ -192,4 +253,6 @@ export function shortcutSystem(world: World): void {
if (keys.lifted.size) {
LIFTED_SHORTCUTS.find(shortcut => matches(shortcut, keys.lifted, keys.held))?.action(world);
}
+
+ updateSpaceHold(world, keys.held);
}