Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance entity management with new clipHeight and expanded properties

konstantin-pauluse6597e394a84
diff --git a/apps/web/src/engine/index.ts b/apps/web/src/engine/index.ts
index 16889a2a..1dc8a819 100644
--- a/apps/web/src/engine/index.ts
+++ b/apps/web/src/engine/index.ts
@@ -10,6 +10,7 @@ export * from './hooks';
export * from './editor';
export * from './timing';
export * from './split';
+export * from './overlap';
export * from './traits';
export * from './input';
export * from './hud';
diff --git a/apps/web/src/engine/overlap.ts b/apps/web/src/engine/overlap.ts
new file mode 100644
index 00000000..114bd3eb
--- /dev/null
+++ b/apps/web/src/engine/overlap.ts
@@ -0,0 +1,162 @@
+/* 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/. */
+
+/**
+ * The no-overlap rule of a `<sequence>`, kept as edits. The runtime has this
+ * too (`resolveSequentialOverlaps` in actions/overlap.ts), where it trims,
+ * removes and splits the traits themselves: that settles the canvas and
+ * nothing else. A drop is an edit of the project, so the version the editor
+ * uses is this one — same rules, spelled through the document, so the file
+ * ends up saying what the timeline shows.
+ *
+ * The model is overwrite: what the user just dropped wins, and the siblings
+ * it landed on give way — trimmed at whichever edge is covered, removed when
+ * they are covered entirely, and split in two when the drop lands inside one.
+ */
+
+import { ChildOf, Computed, Geometry, Group, Sequential, getParentNode, isGroup, store } from '@diffusionstudio/runtime';
+import { Or } from 'koota';
+
+import { getDocumentEditor } from './editor';
+import { trimIn, trimOut } from './timing';
+
+import type { DocumentEditor } from './editor';
+import type { Entity, World } from 'koota';
+
+/**
+ * Settles every sequence the just-dropped clips landed in. `dragged` is what
+ * the user was holding, and it is authoritative: those clips keep what they
+ * are, and never trim or remove each other.
+ */
+export function resolveSequentialOverlaps(world: World, dragged: Entity[]): void {
+ if (dragged.length === 0) return;
+
+ const editor = getDocumentEditor(world);
+ const computed = store(world, Computed);
+ const ignore = new Set(dragged);
+
+ for (const entity of dragged) {
+ const parent = getParentNode(entity);
+ if (parent === null || !parent.has(Sequential)) continue;
+
+ const occStart = computed.start[entity.id()];
+ const occEnd = computed.end[entity.id()];
+ if (occStart === undefined || occEnd === undefined || occEnd <= occStart) continue;
+
+ // Snapshot before mutating: resolving removes, trims and copies, all of
+ // which change the live query — and a copy a split makes is the other
+ // half of a sibling already dealt with, not another one to deal with.
+ const siblings = [...world.query(Or(Geometry, Group), ChildOf(parent))]
+ .filter((sibling) => !ignore.has(sibling));
+
+ for (const sibling of siblings) {
+ resolveEntityOverlap(world, editor, sibling, occStart, occEnd, ignore);
+ }
+ }
+}
+
+/**
+ * Settles a sequence that has just been made, where there is no dropped clip
+ * to be authoritative — every child merely happened to overlap on the canvas.
+ * Precedence goes by start frame instead: the earlier clip keeps what it has
+ * and the later one gives way.
+ *
+ * Each child is authoritative over all the ones after it, and every pass
+ * trims all of them the same way, so their starts stay in order and the
+ * "dropped inside a sibling" split cannot come up here — only edge trims and
+ * removals.
+ */
+export function resolveNewSequenceOverlaps(world: World, sequence: Entity): void {
+ if (!sequence.has(Sequential)) return;
+
+ const editor = getDocumentEditor(world);
+ const computed = store(world, Computed);
+
+ const children = [...world.query(Or(Geometry, Group), ChildOf(sequence))]
+ .sort((a, b) => (computed.start[a.id()] ?? 0) - (computed.start[b.id()] ?? 0));
+
+ // Nothing to protect: there is no clip the user is holding, and the
+ // recursion into a group has no leaves to skip.
+ const ignore = new Set<Entity>();
+
+ for (let i = 0; i < children.length; i++) {
+ const entity = children[i]!;
+ // An earlier authoritative clip may have trimmed or removed this one.
+ if (!entity.isAlive()) continue;
+
+ const occStart = computed.start[entity.id()];
+ const occEnd = computed.end[entity.id()];
+ if (occStart === undefined || occEnd === undefined || occEnd <= occStart) continue;
+
+ for (let j = i + 1; j < children.length; j++) {
+ const sibling = children[j]!;
+ if (!sibling.isAlive()) continue;
+ resolveEntityOverlap(world, editor, sibling, occStart, occEnd, ignore);
+ }
+ }
+}
+
+/**
+ * Settles one entity against the span `[occStart, occEnd)` something else has
+ * taken. A group has no time of its own to give up, so its leaves are settled
+ * and its bounds follow from what is left of them.
+ */
+function resolveEntityOverlap(
+ world: World,
+ editor: DocumentEditor,
+ entity: Entity,
+ occStart: number,
+ occEnd: number,
+ ignore: Set<Entity>,
+): void {
+ const computed = store(world, Computed);
+
+ const start = computed.start[entity.id()];
+ const end = computed.end[entity.id()];
+ if (start === undefined || end === undefined) return;
+
+ // Touching edges abut, they do not overlap.
+ if (end <= occStart || start >= occEnd) return;
+
+ if (isGroup(entity)) {
+ if (start >= occStart && end <= occEnd) {
+ editor.remove(entity);
+ return;
+ }
+
+ const children = [...world.query(Or(Geometry, Group), ChildOf(entity))]
+ .filter((child) => !ignore.has(child));
+ for (const child of children) {
+ resolveEntityOverlap(world, editor, child, occStart, occEnd, ignore);
+ }
+
+ // Emptied of everything it held, the group is nothing on its own.
+ if (world.query(Or(Geometry, Group), ChildOf(entity)).length === 0) {
+ editor.remove(entity);
+ }
+ return;
+ }
+
+ const startCovered = start >= occStart;
+ const endCovered = end <= occEnd;
+
+ if (startCovered && endCovered) {
+ editor.remove(entity);
+ } else if (!startCovered && endCovered) {
+ // Covered from its out point back: keep the head, ending at the drop.
+ trimOut(world, entity, occStart);
+ } else if (startCovered && !endCovered) {
+ // Covered from its in point on: keep the tail, starting at the drop.
+ trimIn(world, entity, occEnd);
+ } else {
+ // The drop landed inside it, so what is left is a head and a tail —
+ // the same cut `splitAtPlayhead` makes, around a span rather than a
+ // frame. Copied before either half is trimmed, so the copy is spelled
+ // from the whole clip and still runs to the end it ran to.
+ const [pair] = editor.duplicateInPlace([entity]);
+
+ trimOut(world, entity, occStart);
+ if (pair) trimIn(world, pair.copy, occEnd);
+ }
+}
diff --git a/packages/jsx/src/types.ts b/packages/jsx/src/types.ts
index e9772a31..6104d367 100644
--- a/packages/jsx/src/types.ts
+++ b/packages/jsx/src/types.ts
@@ -175,6 +175,19 @@ type IdentityProps = {
* selected; the editor writes the bare attribute and removes it again.
*/
selected?: boolean;
+ /**
+ * Height of the element's row in the timeline, px. Editor state, here for
+ * the same reason `selected` is: the timeline is where a row is resized and
+ * the document is the only place that can remember it. Absent means the
+ * common row height.
+ */
+ clipHeight?: number;
+ /**
+ * Whether the timeline shows this element's keyframe rows below its clip.
+ * Editor state, as `clipHeight` is. Absent means collapsed; the editor
+ * writes the bare attribute and removes it again.
+ */
+ expanded?: boolean;
};
type PositionProps = {
diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.ts
index e5558412..47d0a17b 100644
--- a/packages/reconciler/src/document.ts
+++ b/packages/reconciler/src/document.ts
@@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { Active, Animation, AnimationPhase, AnimationType, appendChild, AssetId, Audio, Background, BlendMode, BlendModeType, Blur, Caption, CaptionAlign, CaptionType, Chars, ClipsContent, CornerRadius, createEntity, DEFAULT_BACKGROUND, Color, ColorStop, Effect, EffectType, End, FontStyle, FrameRate, Loop, Geometry, GeometryType, Library, getEntityTree, getParentEntity, getParentNode, Hidden, IsMask, isText, ItemIndex, Keyframe, KeyframeTrack, MixedCornerRadius, Muted, Name, Offset, Opacity, Paint, PaintType, parseColor, Playback, PlaybackRate, Position, removeChild, resizeEntity, Scale, ScaleMode, ScaleModeType, secondsToFrames, getAsset, getAssetFile, getEntityChildren, getLibrary, Group, Sequential, Shader, Size, Stage, Root, Rotation, Scene, Selected, Shadow, Source, SourceIn, SourceOut, setCameraMatrix, Start, Stroke, StrokeCap, StrokeJoin, StrokeStyle, SurfaceHost, SurfaceHostHandle, TextAlign, TextBaseline, TextCase, TextRange, TextStyle, Transition, TransitionType, UniformScale, Volume } from '@diffusionstudio/runtime';
+import { Active, Animation, AnimationPhase, AnimationType, appendChild, AssetId, Audio, Background, BlendMode, BlendModeType, Blur, Caption, CaptionAlign, CaptionType, Chars, ClipHeight, ClipsContent, CornerRadius, createEntity, DEFAULT_BACKGROUND, Color, ColorStop, Effect, EffectType, End, Expanded, FontStyle, FrameRate, Loop, Geometry, GeometryType, Library, getEntityTree, getParentEntity, getParentNode, Hidden, IsMask, isText, ItemIndex, Keyframe, KeyframeTrack, MixedCornerRadius, Muted, Name, Offset, Opacity, Paint, PaintType, parseColor, Playback, PlaybackRate, Position, removeChild, resizeEntity, Scale, ScaleMode, ScaleModeType, secondsToFrames, getAsset, getAssetFile, getEntityChildren, getLibrary, Group, Sequential, Shader, Size, Stage, Root, Rotation, Scene, Selected, Shadow, Source, SourceIn, SourceOut, setCameraMatrix, Start, Stroke, StrokeCap, StrokeJoin, StrokeStyle, SurfaceHost, SurfaceHostHandle, TextAlign, TextBaseline, TextCase, TextRange, TextStyle, Transition, TransitionType, UniformScale, Volume } from '@diffusionstudio/runtime';
import { isPropValue, LOOP_ATTR, parseTime, SOURCE_ATTR } from '@diffusionstudio/jsx';
import type { CameraMatrix, PropertyPath } from '@diffusionstudio/runtime';
@@ -704,6 +704,25 @@ export class RuntimeDocument implements ProjectDocument<HostNode> {
}
return;
}
+ case 'clipHeight': {
+ const height = toNumber(value);
+ if (height === undefined) {
+ entity.remove(ClipHeight);
+ return;
+ }
+
+ entity.add(ClipHeight);
+ entity.set(ClipHeight, { value: height });
+ return;
+ }
+ case 'expanded': {
+ if (value === true) {
+ entity.add(Expanded);
+ } else {
+ entity.remove(Expanded);
+ }
+ return;
+ }
case 'x':
case 'y': {
if (entity.has(Sequential)) return;
diff --git a/packages/runtime/src/actions/index.ts b/packages/runtime/src/actions/index.ts
index d3df237b..6e4f6ede 100644
--- a/packages/runtime/src/actions/index.ts
+++ b/packages/runtime/src/actions/index.ts
@@ -19,6 +19,7 @@ export * from './keyframe';
export * from './overlap';
export * from './group';
export * from './frame';
+export * from './playback';
export * from './clipboard';
export * from './assets';
export * from './interactive';
diff --git a/packages/runtime/src/actions/playback.ts b/packages/runtime/src/actions/playback.ts
new file mode 100644
index 00000000..191a2622
--- /dev/null
+++ b/packages/runtime/src/actions/playback.ts
@@ -0,0 +1,49 @@
+/* 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/. */
+
+// Playhead control. Where the playhead is and whether it is moving are not
+// part of the composition — nothing rendered or exported depends on them —
+// so these write the traits themselves and nothing reaches the file, the
+// same reason the canvas toggles Playback rather than going through an
+// editor. `playbackSystem` advances the playhead from here while playing.
+
+import { store } from '../world/store';
+import { AudioEngine, Computed, FrameRate, Playback } from '../traits';
+
+import type { Entity, World } from 'koota';
+
+/**
+ * Moves `scene`'s playhead to `frame` — a seek, which is what a scrub of the
+ * ruler or a click in the timeline comes to. Both mirrors of the playhead are
+ * written: the seconds are what audio is scheduled against and what the next
+ * advance carries on from, the frames what everything comparing nodes works
+ * in. Frames before the start of the scene do not exist; past its end they
+ * do, since a scene is as long as what is in it and dropping a clip out there
+ * is how it gets longer.
+ */
+export function setPlayhead(world: World, scene: Entity, frame: number): void {
+ const computed = store(world, Computed);
+ const fps = world.get(FrameRate)?.value ?? 30;
+ const eid = scene.id();
+
+ const clamped = Math.max(0, Math.round(frame));
+
+ computed.localTime[eid] = clamped;
+ computed.localTimeInSeconds[eid] = clamped / fps;
+}
+
+/**
+ * Starts or stops `scene` at 1x. The audio context is gated until it has seen
+ * a gesture and a play is one, so it is resumed here rather than at every
+ * call site that can start playback.
+ */
+export function togglePlayback(world: World, scene: Entity): void {
+ const playback = scene.get(Playback);
+ if (!playback) return;
+
+ scene.set(Playback, { playing: !playback.playing, speed: 1 });
+
+ const context = world.get(AudioEngine)?.context;
+ if (context instanceof AudioContext) void context.resume();
+}