Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add grouping and ungrouping functionality to selection in the editor

konstantin-paulus374adf88ce4e
diff --git a/apps/web/src/components/sidebar-right/inspector/scene-template.tsx b/apps/web/src/components/sidebar-right/inspector/scene-template.tsx
index 21ef52f2..2aa1af17 100644
--- a/apps/web/src/components/sidebar-right/inspector/scene-template.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/scene-template.tsx
@@ -6,7 +6,7 @@ import { For, Show, createSignal } from "solid-js";
import { Icon } from "@/components/ui/icon";
import { PanelSection } from "@/components/ui/panel-section";
import { useWorld } from "@diffusionstudio/koota-solid";
-import { Scene } from "@diffusionstudio/reconciler";
+import { Scene, SolidPaint } from "@diffusionstudio/reconciler";
import { Root, Source, Tool, ToolType, getNextName, getViewport, screenToWorld } from "@diffusionstudio/runtime";
import { useEditor } from "@/engine/hooks";
@@ -29,7 +29,9 @@ export function SceneTemplatePanel() {
const y = Math.round(center.y - preset.height / 2);
const [scene] = editor.insertElement(root, () => (
- <Scene name={name} x={x} y={y} width={preset.width} height={preset.height} fill="#000000" />
+ <Scene name={name} x={x} y={y} width={preset.width} height={preset.height}>
+ <SolidPaint color="#000000" />
+ </Scene>
));
if (scene) {
editor.activate(scene);
diff --git a/apps/web/src/engine/group.tsx b/apps/web/src/engine/group.tsx
new file mode 100644
index 00000000..5eeabf3b
--- /dev/null
+++ b/apps/web/src/engine/group.tsx
@@ -0,0 +1,219 @@
+/* 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/. */
+
+/**
+ * Grouping and ungrouping a selection, both without anything moving on the
+ * canvas. Grouping needs no rewriting: a fresh `<group>` carries the identity
+ * transform, so the members' coordinates mean the same thing inside it that
+ * they meant beside it. Ungrouping does the inverse of whatever the group has
+ * accumulated since: its local matrix is composed onto each child's and
+ * decomposed back into the props the source can spell, the same bake a group
+ * resize runs through `resizeNode` — so a child of a moved, rotated or scaled
+ * group steps out standing exactly where it was drawn.
+ */
+
+import { Group as GroupElement } from '@diffusionstudio/reconciler';
+import {
+ AdjustmentLayer,
+ Computed,
+ End,
+ Flip,
+ FrameRate,
+ Geometry,
+ Group,
+ IsMask,
+ Selected,
+ Skew,
+ Start,
+ computeLocalMatrix,
+ decompose2D,
+ entityAnchor,
+ entityLocalMat,
+ entityOffset,
+ framesToSeconds,
+ getEntityChildren,
+ getNextName,
+ getParentEntity,
+ getTimelineOrigin,
+ multiply2D,
+ store,
+ translate2D,
+} from '@diffusionstudio/runtime';
+import { Not, Or } from 'koota';
+
+import { getDocumentEditor } from './editor';
+import { editTransform } from './input/interactions';
+import { syncKeyframe } from './keyframes';
+
+import type { TransformWrite } from './input/interactions';
+import type { Mat2D } from '@diffusionstudio/runtime';
+import type { Entity, World } from 'koota';
+
+/** The node kinds a group holds; a mask belongs to its target, not the group. */
+const NODES = Or(Geometry, Group, AdjustmentLayer);
+
+const EPSILON = 1e-6;
+
+const round2 = (value: number): number => Math.round(value * 100) / 100;
+const round4 = (value: number): number => Math.round(value * 10000) / 10000;
+
+/** Whether `mat` is close enough to the identity for a bake to write nothing. */
+function isIdentity(mat: Mat2D): boolean {
+ return Math.abs(mat.a - 1) < EPSILON && Math.abs(mat.b) < EPSILON
+ && Math.abs(mat.c) < EPSILON && Math.abs(mat.d - 1) < EPSILON
+ && Math.abs(mat.e) < EPSILON && Math.abs(mat.f) < EPSILON;
+}
+
+/**
+ * Puts the selection into a new group where it stands. The group is authored
+ * with no transform of its own, so the members keep the coordinates they had
+ * and nothing moves; its box is derived from theirs (see `computeGroupBounds`),
+ * so there is no size to author either. Only the members sharing the first
+ * one's parent go in — a wrap has one place to put things (see `wrap`). The
+ * selection moves to the group.
+ */
+export function groupSelection(world: World): void {
+ const editor = getDocumentEditor(world);
+ const selected = [...world.query(Selected, NODES, Not(IsMask))];
+ if (!selected.length) return;
+
+ const group = editor.wrap(selected, () => <GroupElement name={getNextName(world, 'Group')} />);
+ if (group) editor.select(group);
+}
+
+/**
+ * How far the group's own timeline sits from its parent's, in frames: what
+ * has to be added to a child's authored times for it to play at the same
+ * moment once the group is gone. Zero for any group that was never slid
+ * along the timeline.
+ */
+function timelineShift(world: World, group: Entity): number {
+ return (store(world, Computed).origin[group.id()] ?? 0) - getTimelineOrigin(group);
+}
+
+/**
+ * Dissolves every selected group: the children move out into the group's
+ * parent, in front of where the group stood and in the order they were in,
+ * and the group goes away. Whatever transform the group had is baked into
+ * each child first — composed onto the child's local matrix and decomposed
+ * back into `x`/`y`/`rotation`/`scale`, the way `resizeNode` spells a scaled
+ * child — so the canvas shows the same picture without the group. A shear the
+ * bake produces (a rotated child in a non-uniformly scaled group) has no JSX
+ * spelling; like the resize gesture, it goes to the `Skew` trait alone. A
+ * group slid along the timeline hands its offset to the children's times the
+ * same way. The selection moves to the released children.
+ */
+export function ungroupSelection(world: World): void {
+ const editor = getDocumentEditor(world);
+ const groups = [...world.query(Selected, Group)];
+ if (!groups.length) return;
+
+ const computed = store(world, Computed);
+ const flip = store(world, Flip);
+ const released: Entity[] = [];
+
+ for (const group of groups) {
+ if (!group.isAlive()) continue;
+ const parent = getParentEntity(group);
+ if (!parent) continue;
+
+ // The group's transform as drawn right now, motion included — rebuilt
+ // rather than read, since an adjustment layer composes itself into the
+ // stored matrix of the clip below it.
+ computeLocalMatrix(world, group);
+ const groupLocal = entityLocalMat(world, group);
+ const bake = !isIdentity(groupLocal);
+ const shift = timelineShift(world, group);
+
+ const children = getEntityChildren(world, group).filter(
+ (child) => (child.has(Geometry) || child.has(Group) || child.has(AdjustmentLayer)) && !child.has(IsMask),
+ );
+
+ for (const child of children) {
+ const writes: TransformWrite[] = [];
+ let scale: { x: number; y: number } | null = null;
+ let skew: { x: number; y: number } | null = null;
+ const cid = child.id();
+
+ if (bake) {
+ computeLocalMatrix(world, child);
+ const anchor = entityAnchor(world, child);
+ const pivotX = anchor.x * (computed.width[cid] ?? 0);
+ const pivotY = anchor.y * (computed.height[cid] ?? 0);
+
+ // The pivot folded in before decomposing, as `resizeNode` folds
+ // it: the translation that comes out is position + pivot, clear
+ // of the (I - L)·pivot term the local matrix wraps around it.
+ const composed = multiply2D(
+ multiply2D(groupLocal, entityLocalMat(world, child)),
+ translate2D(pivotX, pivotY),
+ );
+ const decomposed = decompose2D(composed);
+ const offset = entityOffset(world, child);
+
+ const x = Math.round(decomposed.x - pivotX - offset.x);
+ const y = Math.round(decomposed.y - pivotY - offset.y);
+ const rotation = round2(decomposed.rotation);
+ if (x !== Math.round(computed.positionX[cid] ?? 0)) writes.push(['x', x]);
+ if (y !== Math.round(computed.positionY[cid] ?? 0)) writes.push(['y', y]);
+ if (rotation !== round2(computed.rotation[cid] ?? 0)) writes.push(['rotation', rotation]);
+
+ // The decomposed scale carries the flip the local matrix folded
+ // in; divided back out, since the trait keeps holding it.
+ const scaleX = round4(decomposed.scaleX / (flip.x[cid] ?? 1));
+ const scaleY = round4(decomposed.scaleY / (flip.y[cid] ?? 1));
+ if (scaleX !== round4(computed.scaleX[cid] ?? 1) || scaleY !== round4(computed.scaleY[cid] ?? 1)) {
+ scale = { x: scaleX, y: scaleY };
+ }
+
+ const skewX = round2(decomposed.skewX);
+ const skewY = round2(decomposed.skewY);
+ if (skewX !== round2(computed.skewX[cid] ?? 0) || skewY !== round2(computed.skewY[cid] ?? 0)) {
+ skew = { x: skewX, y: skewY };
+ }
+ }
+
+ if (!editor.reparent(child, parent, group)) continue;
+ released.push(child);
+
+ if (writes.length) editTransform(world, editor, child, writes);
+
+ if (scale) {
+ // The scale-row's spelling: one uniform `scale`, or the two axes
+ // with whichever of the two forms it replaces unset.
+ if (Math.abs(scale.x - scale.y) < EPSILON) {
+ editor.editProperty(child, 'scaleX', false);
+ editor.editProperty(child, 'scaleY', false);
+ editor.editProperty(child, 'scale', scale.x === 1 ? false : scale.x);
+ syncKeyframe(world, editor, child, 'scale', scale.x);
+ } else {
+ editor.editProperty(child, 'scale', false);
+ editor.editProperty(child, 'scaleX', scale.x);
+ editor.editProperty(child, 'scaleY', scale.y);
+ syncKeyframe(world, editor, child, 'scaleX', scale.x);
+ syncKeyframe(world, editor, child, 'scaleY', scale.y);
+ }
+ }
+
+ if (skew) {
+ // Skew has no JSX spelling, so it is written to the trait alone
+ // and lasts until the project is rendered again (see resizeNode).
+ child.add(Skew);
+ child.set(Skew, skew);
+ }
+
+ if (shift !== 0) {
+ const fps = world.get(FrameRate)?.value ?? 30;
+ const start = (child.get(Start)?.value ?? 0) + shift;
+ editor.editProperty(child, 'start', start === 0 ? false : framesToSeconds(start, fps));
+ const end = child.get(End)?.value;
+ if (end !== undefined) editor.editProperty(child, 'end', framesToSeconds(end + shift, fps));
+ }
+ }
+
+ editor.remove(group);
+ }
+
+ if (released.length) editor.select(released);
+}
diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.ts
index 5bcc380c..70b6f31a 100644
--- a/apps/web/src/engine/input/shortcuts.ts
+++ b/apps/web/src/engine/input/shortcuts.ts
@@ -32,6 +32,7 @@ import { Not, Or } from 'koota';
import { zoomBy, zoomTo, zoomToFit, zoomToSelection } from '../camera';
import { getDocumentEditor } from '../editor';
+import { groupSelection, ungroupSelection } from '../group';
import { getEditHistory } from '../history';
import { splitAtPlayhead } from '../split';
import { Keys, MODIFIER_KEYS, Pointer } from '../traits';
@@ -386,6 +387,8 @@ const PRESSED_SHORTCUTS: readonly Shortcut[] = [
{ keys: ['backspace'], action: deleteSelection },
{ keys: ['delete'], action: deleteSelection },
{ keys: ['d', 'mod', '!shift'], action: duplicateSelection },
+ { keys: ['g', 'mod', '!shift'], action: groupSelection },
+ { keys: ['g', 'mod', 'shift'], action: ungroupSelection },
{ keys: ['b', 'mod'], action: splitAtPlayhead },
{ keys: ['c', 'mod'], action: copySelection },
{ keys: ['v', 'mod'], action: pasteSelection },