Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor object menu and source settings for improved functionality

konstantin-paulus3c9e0e861c00
diff --git a/apps/web/src/components/sidebar-left/project-menu/object-menu.tsx b/apps/web/src/components/sidebar-left/project-menu/object-menu.tsx
index b29eb398..e53305a7 100644
--- a/apps/web/src/components/sidebar-left/project-menu/object-menu.tsx
+++ b/apps/web/src/components/sidebar-left/project-menu/object-menu.tsx
@@ -13,7 +13,7 @@ import {
DropdownMenuGroup,
} from "@/components/ui/dropdown-menu";
import { useWorld } from "@diffusionstudio/koota-solid";
-import { isGroup, isSequence } from "@diffusionstudio/runtime";
+import { isGroupLike, isSequence } from "@diffusionstudio/runtime";
import {
groupSelection,
ungroupSelection,
@@ -27,9 +27,8 @@ export function ObjectMenu() {
const world = useWorld();
const { nodes } = useSelection();
const hasSelection = () => nodes().length > 0;
- // Sequences carry the Group trait too, so the one check enables Ungroup
- // for both — the same containers the shortcut dissolves.
- const hasContainer = () => nodes().some(isGroup);
+
+ const hasContainer = () => nodes().some((node) => isGroupLike(node) || isSequence(node));
const hasSequence = () => nodes().some(isSequence);
return (
diff --git a/apps/web/src/components/sidebar-right/inspector/source.tsx b/apps/web/src/components/sidebar-right/inspector/source.tsx
index 788fd6f3..f8678631 100644
--- a/apps/web/src/components/sidebar-right/inspector/source.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/source.tsx
@@ -16,10 +16,12 @@ import { PanelSection } from "@/components/ui/panel-section";
import { ControlRow } from "@/components/ui/control-group";
import { AssetThumbnail } from "@/components/ui/asset-thumbnail";
import { OpacitySwatch } from "@/components/ui/opacity-swatch";
+import { ColorOpacityPicker } from "@/components/ui/color-opacity-picker";
import { assetName } from "@diffusionstudio/assets";
-import { useHas, useTrait } from "@diffusionstudio/koota-solid";
+import { useHas, useTrait, useWorld } from "@diffusionstudio/koota-solid";
import { AssetId, Color, Computed, PaintType, colorToHex, getIntrinsicPaint, parseColor } from "@diffusionstudio/runtime";
import { useDerived, useEditor } from "@/engine/hooks";
+import { syncKeyframe } from "@/engine/keyframes";
import { useLibrary } from "@/engine/library";
import { FitMenu } from "./fill-picker";
import { AssetFillPicker } from "./asset-picker";
@@ -65,7 +67,8 @@ export function SourceSettings(props: SourceSettingsProps) {
let anchorRef!: HTMLDivElement;
- const [picking, setPicking] = createSignal(false);
+ // One picker at a time: they anchor to the same section and would overlap.
+ const [picking, setPicking] = createSignal<"media" | "solid">();
const intrinsic = useDerived(() => getIntrinsicPaint(entity()));
const isMedia = createMemo(
@@ -89,7 +92,7 @@ export function SourceSettings(props: SourceSettingsProps) {
// Removal is the element ceasing to be a media element: the node is
// rewritten as the <rect> it otherwise was, and this section goes with it.
const handleRemoveMedia = () => {
- setPicking(false);
+ setPicking(undefined);
editor.removeIntrinsicPaint(entity());
};
@@ -113,7 +116,7 @@ export function SourceSettings(props: SourceSettingsProps) {
<div class="flex h-7 w-full items-center overflow-hidden rounded-md border border-transparent bg-input text-foreground focus-within:border-primary">
<button
class="flex h-full min-w-0 flex-1 items-center gap-2 pl-1 text-left"
- onClick={() => setPicking(true)}
+ onClick={() => setPicking("media")}
>
<span class="size-5 min-w-5 overflow-hidden rounded-sm">
<Show
@@ -153,11 +156,16 @@ export function SourceSettings(props: SourceSettingsProps) {
</Show>
<Show when={hasFill()}>
- <SolidFillRow node={entity()} />
+ <SolidFillRow
+ node={entity()}
+ anchorRef={anchorRef}
+ picking={picking() === "solid"}
+ onPickingChange={(open) => setPicking(open ? "solid" : undefined)}
+ />
</Show>
</PanelSection>
- <Show when={picking()}>
+ <Show when={picking() === "media"}>
<FloatingInspector open anchorRef={anchorRef}>
<FloatingInspectorHeader>
<FloatingInspectorTitle>Source</FloatingInspectorTitle>
@@ -169,7 +177,7 @@ export function SourceSettings(props: SourceSettingsProps) {
size="icon"
variant="ghost"
class="text-muted-foreground"
- onClick={() => setPicking(false)}
+ onClick={() => setPicking(undefined)}
>
<Icon name="close-remove" class="size-6" />
</TooltipTrigger>
@@ -192,20 +200,37 @@ export function SourceSettings(props: SourceSettingsProps) {
type SolidFillRowProps = {
node: Entity;
+ anchorRef: HTMLElement;
+ picking: boolean;
+ onPickingChange(open: boolean): void;
};
/**
* The node's intrinsic solid: the `fill` prop, edited in place as a hex the
- * way a solid fill row edits its color. Read from `Computed.color` so it
- * animates; alpha is not offered because the prop ignores it. The X takes
- * the prop off the node.
+ * way a solid fill row edits its color, or through the picker the swatch
+ * opens — both are `fill` prop writes, the node keeps its identity. Read
+ * from `Computed.color` so it animates (the `color` keyframe track drives
+ * the same trait); alpha is not offered because the prop ignores it. The X
+ * takes the prop off the node.
*/
function SolidFillRow(props: SolidFillRowProps) {
+ const world = useWorld();
const editor = useEditor();
const color = useDerived(() => props.node.get(Computed)?.color ?? 0xE0E0E0);
const colorText = createMemo(() => colorToHex(color()).replace("#", ""));
+ const updateColor = (next: number) => {
+ const hex = colorToHex(next);
+ editor.editProperty(props.node, "fill", hex);
+ syncKeyframe(world, editor, props.node, "color", hex);
+ };
+
+ const handleRemoveFill = () => {
+ props.onPickingChange(false);
+ editor.editProperty(props.node, "fill", false);
+ };
+
const [draft, setDraft] = createSignal(colorText());
let cancelCommit = false;
@@ -252,9 +277,12 @@ function SolidFillRow(props: SolidFillRowProps) {
<ControlRow label="Solid">
<div class="flex h-7 w-full items-center overflow-hidden rounded-md border border-transparent bg-input text-foreground focus-within:border-primary">
<div class="flex h-full min-w-0 flex-1 items-center gap-2 pl-1">
- <span class="size-5 min-w-5 overflow-hidden rounded-sm">
+ <button
+ class="size-5 min-w-5 overflow-hidden rounded-sm"
+ onClick={() => props.onPickingChange(true)}
+ >
<OpacitySwatch color={color()} opacity={1} />
- </span>
+ </button>
<input
type="text"
value={draft()}
@@ -270,13 +298,44 @@ function SolidFillRow(props: SolidFillRowProps) {
size="icon"
variant="ghost"
class="text-muted-foreground"
- onClick={() => editor.editProperty(props.node, "fill", false)}
+ onClick={handleRemoveFill}
>
<Icon name="close-remove-small" />
</TooltipTrigger>
<TooltipContent>Remove fill</TooltipContent>
</Tooltip>
</div>
+
+ <Show when={props.picking}>
+ <FloatingInspector open anchorRef={props.anchorRef}>
+ <FloatingInspectorHeader>
+ <FloatingInspectorTitle>Solid</FloatingInspectorTitle>
+ <div class="ml-auto flex items-center gap-1">
+ <Tooltip>
+ <TooltipTrigger
+ as={Button}
+ size="icon"
+ variant="ghost"
+ class="text-muted-foreground"
+ onClick={() => props.onPickingChange(false)}
+ >
+ <Icon name="close-remove" class="size-6" />
+ </TooltipTrigger>
+ <TooltipContent>Close</TooltipContent>
+ </Tooltip>
+ </div>
+ </FloatingInspectorHeader>
+ <FloatingInspectorContent class="p-0">
+ <ColorOpacityPicker
+ color={color()}
+ opacity={1}
+ onColorChange={updateColor}
+ withoutOpacity
+ keyframeTarget={props.node}
+ />
+ </FloatingInspectorContent>
+ </FloatingInspector>
+ </Show>
</ControlRow>
);
}
diff --git a/apps/web/src/engine/group.tsx b/apps/web/src/engine/group.tsx
index 31977040..f01d1c6b 100644
--- a/apps/web/src/engine/group.tsx
+++ b/apps/web/src/engine/group.tsx
@@ -12,6 +12,7 @@ import {
Group,
IsMask,
Position,
+ Scene,
Selected,
Sequential,
Skew,
@@ -38,6 +39,7 @@ import { syncKeyframe } from './keyframes';
import { resolveNewSequenceOverlaps } from './overlap';
import { authoredTime } from './timing';
+import type { DocumentEditor } from './editor';
import type { TransformWrite } from './input/interactions';
import type { Mat2D } from '@diffusionstudio/runtime';
import type { Entity, World } from 'koota';
@@ -198,13 +200,14 @@ function timelineShift(world: World, group: Entity): number {
}
/**
- * Dissolves every selected group, sequences included: a sequence is a group
- * without spatial identity of its own (see the Sequential observer), so the
- * one bake covers both — for a sequence it finds the identity and writes
- * nothing.
+ * Dissolves every selected container, whatever its kind: groups, sequences
+ * (a sequence is a group without spatial identity of its own; see the
+ * Sequential observer) and scenes. The one bake covers all of them — a
+ * sequence's transform is the identity and writes nothing, a scene's is the
+ * pure translation its `x`/`y` spell.
*/
export function ungroupSelection(world: World): void {
- dissolveContainers(world, [...world.query(Selected, Group)]);
+ dissolveContainers(world, [...world.query(Selected, Or(Group, Scene))]);
}
/** Dissolves only the selected sequences, the inverse of the wrap above. */
@@ -228,8 +231,6 @@ function dissolveContainers(world: World, containers: Entity[]): void {
if (!containers.length) return;
const editor = getDocumentEditor(world);
- const computed = store(world, Computed);
- const flip = store(world, Flip);
const released: Entity[] = [];
for (const group of containers) {
@@ -245,92 +246,101 @@ function dissolveContainers(world: World, containers: Entity[]): void {
const bake = !isIdentity(groupLocal);
const shift = timelineShift(world, group);
- const children = nodeChildren(world, group);
-
- 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 };
- }
- }
-
+ for (const child of nodeChildren(world, group)) {
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);
- }
+ // A sequence among the children can hold neither the position nor
+ // the start the bake writes; what its contents mean is written to
+ // them instead, in the same space the sequence passes through.
+ for (const leaf of spatialLeaves(world, child)) {
+ bakeContainerInto(world, editor, leaf, bake ? groupLocal : null, shift);
}
+ }
- 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);
- }
+ editor.remove(group);
+ }
+
+ if (released.length) editor.select(released);
+}
- if (shift !== 0) {
- const fps = world.get(FrameRate)?.value ?? 30;
- const start = (authoredTime(world, child, 'start') ?? 0) + shift;
- editor.editProperty(child, 'start', start === 0 ? false : framesToSeconds(start, fps));
- const end = authoredTime(world, child, 'end');
- if (end !== undefined) editor.editProperty(child, 'end', framesToSeconds(end + shift, fps));
+/**
+ * Writes what a dissolved container leaves behind onto one released node:
+ * `containerLocal` (null for the identity) composed onto the node's local
+ * matrix and spelled back as props, and `shift` added to its times.
+ */
+function bakeContainerInto(
+ world: World,
+ editor: DocumentEditor,
+ child: Entity,
+ containerLocal: Mat2D | null,
+ shift: number,
+): void {
+ const computed = store(world, Computed);
+ const flip = store(world, Flip);
+ const cid = child.id();
+
+ if (containerLocal) {
+ 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(containerLocal, 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);
+ const writes: TransformWrite[] = [];
+ 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]);
+ if (writes.length) editTransform(world, editor, child, writes);
+
+ // 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)) {
+ // The scale-row's spelling: one uniform `scale`, or the two axes
+ // with whichever of the two forms it replaces unset.
+ if (Math.abs(scaleX - scaleY) < EPSILON) {
+ editor.editProperty(child, 'scaleX', false);
+ editor.editProperty(child, 'scaleY', false);
+ editor.editProperty(child, 'scale', scaleX === 1 ? false : scaleX);
+ syncKeyframe(world, editor, child, 'scale', scaleX);
+ } else {
+ editor.editProperty(child, 'scale', false);
+ editor.editProperty(child, 'scaleX', scaleX);
+ editor.editProperty(child, 'scaleY', scaleY);
+ syncKeyframe(world, editor, child, 'scaleX', scaleX);
+ syncKeyframe(world, editor, child, 'scaleY', scaleY);
}
}
- editor.remove(group);
+ const skewX = round2(decomposed.skewX);
+ const skewY = round2(decomposed.skewY);
+ if (skewX !== round2(computed.skewX[cid] ?? 0) || skewY !== round2(computed.skewY[cid] ?? 0)) {
+ // 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, { x: skewX, y: skewY });
+ }
}
- if (released.length) editor.select(released);
+ if (shift !== 0) {
+ const fps = world.get(FrameRate)?.value ?? 30;
+ const start = (authoredTime(world, child, 'start') ?? 0) + shift;
+ editor.editProperty(child, 'start', start === 0 ? false : framesToSeconds(start, fps));
+ const end = authoredTime(world, child, 'end');
+ if (end !== undefined) editor.editProperty(child, 'end', framesToSeconds(end + shift, fps));
+ }
}