Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add Tracking Settings to Inspector Component

konstantin-paulusf40cadc07f43
diff --git a/apps/web/src/components/sidebar-right/inspector/inspector.tsx b/apps/web/src/components/sidebar-right/inspector/inspector.tsx
index e5b3f094..2084c7b3 100644
--- a/apps/web/src/components/sidebar-right/inspector/inspector.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/inspector.tsx
@@ -40,6 +40,7 @@ import { EffectsSettings } from "./effects";
import { AnimationsSettings } from "./animations";
import { TransitionSettings } from "./transition";
import { ClipPathsSettings } from "./clip-paths";
+import { TrackingSettings } from "./tracking";
import { AudioSettings } from "./audio";
import { InterpolationSettings } from "./interpolation";
@@ -167,6 +168,10 @@ export function Inspector() {
<SourceSettings selection={nodes()} />
</Show>
+ <Show when={includesTarget("shape")}>
+ <TrackingSettings selection={nodes()} />
+ </Show>
+
<Show when={includesTarget("shape", "text", "scene", "caption")}>
<StrokesSettings selection={nodes()} />
</Show>
diff --git a/apps/web/src/components/sidebar-right/inspector/tracking.tsx b/apps/web/src/components/sidebar-right/inspector/tracking.tsx
new file mode 100644
index 00000000..a3b132a8
--- /dev/null
+++ b/apps/web/src/components/sidebar-right/inspector/tracking.tsx
@@ -0,0 +1,98 @@
+/* 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/. */
+
+import { For, Show } from "solid-js";
+import { Button } from "@/components/ui/button";
+import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
+import { Icon } from "@/components/ui/icon";
+import { ItemRow } from "@/components/ui/item-row";
+import { PanelSection } from "@/components/ui/panel-section";
+import { toast } from "somoto";
+import { useWorld } from "@diffusionstudio/koota-solid";
+import { Tool, ToolType } from "@diffusionstudio/runtime";
+import { useDerived } from "@/engine/hooks";
+import { getVideoRect, listObjectMasks, removeObjectMask } from "@/engine/object-mask";
+
+import type { Entity } from "koota";
+import type { ObjectMaskSource } from "@/engine/object-mask";
+
+type TrackingSettingsProps = {
+ selection: Entity[];
+};
+
+/**
+ * The tracks of the selected clip's footage: the objects the object mask
+ * tool has followed through it, which effects then take as masks. A track
+ * belongs to the video, not the clip — every clip of the same footage lists
+ * it, and removing it takes it out of every effect that uses it. The plus
+ * picks the object mask tool to track another. Shown for video clips only.
+ */
+export function TrackingSettings(props: TrackingSettingsProps) {
+ const world = useWorld();
+ const entity = () => props.selection[0]!;
+
+ const isVideo = useDerived(() => getVideoRect(world, entity()) !== null);
+ const tracks = useDerived(
+ () => listObjectMasks(world, entity()),
+ (a, b) => a.length === b.length && a.every((track, i) => track.asset.id === b[i]!.asset.id && track.name === b[i]!.name),
+ );
+
+ const handleRemove = async (track: ObjectMaskSource) => {
+ try {
+ await removeObjectMask(world, track);
+ } catch (e) {
+ toast.error("Failed to remove track", { description: (e as Error).message });
+ }
+ };
+
+ return (
+ <Show when={isVideo()}>
+ <PanelSection
+ title="Tracking"
+ actions={
+ <Tooltip>
+ <TooltipTrigger
+ as={Button}
+ size="icon"
+ variant="ghost"
+ class="text-muted-foreground"
+ onClick={() => world.set(Tool, { value: ToolType.OBJECT_MASK })}
+ >
+ <Icon name="plus-add" />
+ </TooltipTrigger>
+ <TooltipContent>Track object</TooltipContent>
+ </Tooltip>
+ }
+ >
+ <For each={tracks()}>
+ {(track) => <TrackRow track={track} onRemove={() => handleRemove(track)} />}
+ </For>
+ </PanelSection>
+ </Show>
+ );
+}
+
+type TrackRowProps = {
+ track: ObjectMaskSource;
+ onRemove(): void;
+};
+
+function TrackRow(props: TrackRowProps) {
+ return (
+ <ItemRow label="Object" value={props.track.name} icon={<Icon name="object-mask" />} class="text-foreground">
+ <Tooltip>
+ <TooltipTrigger
+ as={Button}
+ size="icon"
+ variant="ghost"
+ class="text-muted-foreground"
+ onClick={props.onRemove}
+ >
+ <Icon name="close-remove-small" />
+ </TooltipTrigger>
+ <TooltipContent>Remove track</TooltipContent>
+ </Tooltip>
+ </ItemRow>
+ );
+}
diff --git a/apps/web/src/engine/object-mask/copy.tsx b/apps/web/src/engine/object-mask/copy.tsx
index 380d4b43..5be8ed57 100644
--- a/apps/web/src/engine/object-mask/copy.tsx
+++ b/apps/web/src/engine/object-mask/copy.tsx
@@ -4,10 +4,11 @@
import { assetName } from '@diffusionstudio/assets';
import { Mask } from '@diffusionstudio/reconciler';
-import { AssetId, Blur, Cache, Library, getParentNode } from '@diffusionstudio/runtime';
+import { AssetId, Blur, Cache, Library, Mask as MaskTrait, getParentNode } from '@diffusionstudio/runtime';
import { getDocumentEditor } from '../editor';
import { getVideoRect } from './media';
+import { getMaskRestoreOf } from './tracking';
import type { Entity, World } from 'koota';
import type { MaskAsset } from '@diffusionstudio/assets';
@@ -68,6 +69,25 @@ export function copyObjectMask(world: World, effect: Entity, source: ObjectMaskS
return mask ?? null;
}
+/**
+ * Deletes the tracked frames of `source`: every `<mask>` naming them leaves
+ * the document, wherever it is, and the file leaves the library — a mask
+ * without its frames would only fail to load. A restore of the file is
+ * stopped, since there is nothing left for it to write back to.
+ */
+export async function removeObjectMask(world: World, source: ObjectMaskSource): Promise<void> {
+ const id = source.asset.id;
+ const users = world.query(MaskTrait, AssetId).filter((mask) => mask.get(AssetId)?.value === id);
+
+ if (users.length > 0) {
+ getDocumentEditor(world).remove(users);
+ }
+
+ getMaskRestoreOf(source.asset)?.controller.abort();
+
+ await world.get(Library)?.remove([source.asset]);
+}
+
/** The feather another mask of the same frames has, anywhere on the effect's clip. */
function featherOf(effect: Entity, source: ObjectMaskSource): number | undefined {
const node = getParentNode(effect);