The VS Code of video editing, built for agents.
TypeScript
Enhance pointer and keyboard interaction handling in the engine
konstantin-paulus7d5614b61fd6
diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.tsindex 531443f5..3c21dde6 100644--- a/apps/web/src/engine/create-engine.ts+++ b/apps/web/src/engine/create-engine.ts@@ -83,6 +83,18 @@ class Engine {this.addEvent('pointerup', event)}+ /**+ * Whether the pointer is on the stage, which a shortcut reads to tell a+ * press that could start a canvas gesture from one that could not.+ */+ private readonly onPointerEnter = () => {+ this.world.set(Pointer, { over: true });+ }++ private readonly onPointerLeave = () => {+ this.world.set(Pointer, { over: false });+ }+private readonly onBlur = () => {// Key-up never arrives when focus leaves mid-hold (⌘-tab, devtools).this.world.get(Keys)!.held.clear();@@ -107,7 +119,10 @@ class Engine {keys.held.add(key);if (isMod) keys.held.add('mod');- if (!event.repeat) this.world.set(Keys, { justPressed: true });+ if (!event.repeat) {+ keys.pressed.add(key);+ if (isMod) keys.pressed.add('mod');+ }event.preventDefault();}@@ -118,8 +133,11 @@ class Engine {const isMod = event.key === 'Meta' || event.key === 'Control';keys.held.delete(key);- if (isMod) keys.held.delete('mod');- this.world.set(Keys, { justLifted: true });+ keys.lifted.add(key);+ if (isMod) {+ keys.held.delete('mod');+ keys.lifted.add('mod');+ }};private readonly loop = (timestamp: number): void => {@@ -136,9 +154,8 @@ class Engine {this.setFrame((count) => count + 1);const keys = this.world.get(Keys);- if (keys?.justPressed || keys?.justLifted) {- this.world.set(Keys, { justPressed: false, justLifted: false });- }+ keys?.pressed.clear();+ keys?.lifted.clear();this.rafId = requestAnimationFrame(this.loop);}@@ -214,6 +231,8 @@ class Engine {canvas.addEventListener('dblclick', this.onDoubleClick);canvas.addEventListener('pointerdown', this.onPointerDown);canvas.addEventListener('pointermove', this.onPointerMove);+ canvas.addEventListener('pointerenter', this.onPointerEnter);+ canvas.addEventListener('pointerleave', this.onPointerLeave);window.addEventListener('pointerup', this.onPointerUp);window.addEventListener('pointercancel', this.onPointerUp);window.addEventListener('keydown', this.onKeyDown);@@ -258,6 +277,8 @@ class Engine {this.canvas?.removeEventListener('dblclick', this.onDoubleClick);this.canvas?.removeEventListener('pointerdown', this.onPointerDown);this.canvas?.removeEventListener('pointermove', this.onPointerMove);+ this.canvas?.removeEventListener('pointerenter', this.onPointerEnter);+ this.canvas?.removeEventListener('pointerleave', this.onPointerLeave);window.removeEventListener('pointerup', this.onPointerUp);window.removeEventListener('pointercancel', this.onPointerUp);window.removeEventListener('keydown', this.onKeyDown);diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.tsindex 7bfa2742..9a744c1e 100644--- a/apps/web/src/engine/input/shortcuts.ts+++ b/apps/web/src/engine/input/shortcuts.ts@@ -7,26 +7,24 @@* 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, getEntityChildren, getParentEntity, getSelection, isGroupLike, Position, Selected, store } from '@diffusionstudio/runtime';+import { Computed, getActiveEntity, getCameraMatrix, getEntityChildren, getParentEntity, getSelection, isGroupLike, Position, Selected, store, togglePlayback } from '@diffusionstudio/runtime';import { getDocumentEditor } from '../editor';import { splitAtPlayhead } from '../split';-import { Keys } from '../traits';+import { Keys, Pointer } from '../traits';import { editTransform } from './interactions';import type { TransformWrite } from './interactions';+import type { CameraMatrix } from '@diffusionstudio/runtime';import type { World } from 'koota';-interface Shortcut {- /**- * Keys to hold down for the shortcut to trigger. Joined with '+' to form a set of keys.- */- key: string;- mod?: boolean;- shift?: boolean;- alt?: boolean;+type Shortcut = {+ keys: string[];action: (world: World) => void;}@@ -109,39 +107,89 @@ export function nudgeSelection(world: World, dx: number, dy: number): void {const nudge = (dx: number, dy: number) => (world: World): void => nudgeSelection(world, dx, dy);-const SHORTCUTS: readonly Shortcut[] = [- { key: 'backspace', action: deleteSelection },- { key: 'delete', action: deleteSelection },- { key: 'd', mod: true, action: duplicateSelection },- { key: 'b', mod: true, action: splitAtPlayhead },- { key: 'c', mod: true, action: copySelection },- { key: 'v', mod: true, action: pasteSelection },- { key: 'arrowleft', action: nudge(-NUDGE, 0) },- { key: 'arrowright', action: nudge(NUDGE, 0) },- { key: 'arrowup', action: nudge(0, -NUDGE) },- { key: 'arrowdown', action: nudge(0, NUDGE) },- { key: 'arrowleft', shift: true, action: nudge(-NUDGE_FAST, 0) },- { key: 'arrowright', shift: true, action: nudge(NUDGE_FAST, 0) },- { key: 'arrowup', shift: true, action: nudge(0, -NUDGE_FAST) },- { key: 'arrowdown', shift: true, action: nudge(0, NUDGE_FAST) },+/**+ * 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;++function toggleActivePlayback(world: World): void {+ const scene = getActiveEntity(world);+ if (scene) togglePlayback(world, scene);+}++function onSpacePressed(world: World): void {+ if (world.get(Pointer)?.over) {+ spaceCamera = getCameraMatrix(world);+ } else {+ spaceCamera = null;+ toggleActivePlayback(world);+ }+}++function onSpaceLifted(world: World): void {+ const camera = getCameraMatrix(world);+ if (spaceCamera?.every((value, index) => value === camera[index])) {+ toggleActivePlayback(world);+ }+ spaceCamera = null;+}++const PRESSED_SHORTCUTS: readonly Shortcut[] = [+ { keys: ['backspace'], action: deleteSelection },+ { keys: ['delete'], action: deleteSelection },+ { keys: ['d', 'mod'], action: duplicateSelection },+ { keys: ['b', 'mod'], action: splitAtPlayhead },+ { keys: ['c', 'mod'], action: copySelection },+ { keys: ['v', 'mod'], action: pasteSelection },+ { keys: ['arrowleft', '!shift'], action: nudge(-NUDGE, 0) },+ { keys: ['arrowright', '!shift'], action: nudge(NUDGE, 0) },+ { keys: ['arrowup', '!shift'], action: nudge(0, -NUDGE) },+ { keys: ['arrowdown', '!shift'], action: nudge(0, NUDGE) },+ { keys: ['arrowleft', 'shift'], action: nudge(-NUDGE_FAST, 0) },+ { keys: ['arrowright', 'shift'], action: nudge(NUDGE_FAST, 0) },+ { keys: ['arrowup', 'shift'], action: nudge(0, -NUDGE_FAST) },+ { keys: ['arrowdown', 'shift'], action: nudge(0, NUDGE_FAST) },+ { keys: [' '], action: onSpacePressed },+];++const LIFTED_SHORTCUTS: readonly Shortcut[] = [+ { keys: [' '], action: onSpaceLifted },];/**- * On a frame with a fresh press, runs the shortcut the held keys spell, then- * resets the frame's press and lift flags: it is the last reader of them.+ * Whether `moved` — the keys that went down, or up, this frame — spells the+ * shortcut: one of its keys has to be the one that moved, the rest have to+ * be held, and a '!' key has to be up. The key that moved is matched+ * against `moved` rather than `held` because a lift takes it out of `held`,+ * and a tap shorter than a frame is over before the frame runs.*/+function matches(shortcut: Shortcut, moved: Set<string>, held: Set<string>): boolean {+ let triggered = false;++ for (const key of shortcut.keys) {+ if (key.startsWith('!')) {+ if (held.has(key.slice(1))) return false;+ } else if (moved.has(key)) {+ triggered = true;+ } else if (!held.has(key)) {+ return false;+ }+ }++ return triggered;+}++/** On a frame with a fresh press or release, runs the shortcut it spells. */export function shortcutSystem(world: World): void {const keys = world.get(Keys);- if (!keys?.justPressed) return;-+ if (!keys) return;- const shortcut = SHORTCUTS.find(- (shortcut) =>- shortcut.key.split('+').every(key => keys.held.has(key)) &&- keys.held.has('mod') === !!shortcut.mod &&- keys.held.has('shift') === !!shortcut.shift &&- keys.held.has('alt') === !!shortcut.alt,- );+ if (keys.pressed.size) {+ PRESSED_SHORTCUTS.find(shortcut => matches(shortcut, keys.pressed, keys.held))?.action(world);+ }- shortcut?.action(world);+ if (keys.lifted.size) {+ LIFTED_SHORTCUTS.find(shortcut => matches(shortcut, keys.lifted, keys.held))?.action(world);+ }}diff --git a/apps/web/src/engine/traits.ts b/apps/web/src/engine/traits.tsindex 45ae13c1..92a341c9 100644--- a/apps/web/src/engine/traits.ts+++ b/apps/web/src/engine/traits.ts@@ -19,7 +19,9 @@ export type PointerPhase = 'pressed' | 'lifted';/*** The pointer in canvas device pixels, as of the last event the input system* drained. `dragStart*` is where the current press began, which is what a- * gesture measures its delta from.+ * gesture measures its delta from. `over` is whether the pointer is on the+ * stage at all, which is what tells a key press whether it could be the+ * start of a canvas gesture (see the space shortcut).*/export const Pointer = trait({phase: 'lifted' as PointerPhase,@@ -28,21 +30,23 @@ export const Pointer = trait({clientY: 0,dragStartX: 0,dragStartY: 0,+ over: false,});/*** The keyboard, lowercased, with 'mod' standing in for meta/control so a* shortcut does not have to know which platform it is on. `held` is what is- * down right now (what a gesture reads its modifiers from, mutated in- * place); `justPressed` and `justLifted` say whether a key went down or up- * since the last frame, for whatever acts on a press rather than a hold —- * which key it was is what `held` holds by then. Both flags are reset by the- * shortcut system, so anything reading them runs before it.+ * down right now (what a gesture reads its modifiers from); `pressed` and+ * `lifted` are the keys that went down and up since the last frame, for+ * whatever acts on a press or a release rather than on a hold. All three+ * are mutated in place, and the frame loop empties `pressed` and `lifted`+ * once the systems have run, so a key movement is there for exactly the one+ * frame that follows it.*/export const Keys = trait({held: () => new Set<string>(),- justPressed: false,- justLifted: false,+ pressed: () => new Set<string>(),+ lifted: () => new Set<string>(),});export type SnapLine = { from: Point; to: Point };