The VS Code of video editing, built for agents.
TypeScript
Refactor fill management in Inspector for improved functionality and performance
konstantin-paulus1caf194ada26
diff --git a/apps/web/src/components/sidebar-right/inspector/asset-picker.tsx b/apps/web/src/components/sidebar-right/inspector/asset-picker.tsxindex 23f7445d..01e75e8a 100644--- a/apps/web/src/components/sidebar-right/inspector/asset-picker.tsx+++ b/apps/web/src/components/sidebar-right/inspector/asset-picker.tsx@@ -2,22 +2,11 @@* 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, createMemo, createSignal, type JSX } from "solid-js";+import { For, Show, createMemo, createSignal } from "solid-js";import { toast } from "somoto";-import { saveAssetAs } from "@/engine/asset-actions";-import { AssetThumbnail } from "@/components/ui/asset-thumbnail";import { Button } from "@/components/ui/button";import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";-import {- ContextMenu,- ContextMenuContent,- ContextMenuItem,- ContextMenuPortal,- ContextMenuSeparator,- ContextMenuShortcut,- ContextMenuTrigger,-} from "@/components/ui/context-menu";import {DropdownMenu,DropdownMenuContent,@@ -26,17 +15,18 @@ import {DropdownMenuTrigger,} from "@/components/ui/dropdown-menu";import { Icon } from "@/components/ui/icon";-import { useEngine } from "@/context/engine";+import { Separator } from "@/components/ui/separator";+import { LazyAssetItem } from "@/components/sidebar-left/asset-item";import { usePromptInput } from "@/context/prompt-input";import { createDefaultConfig } from "@/components/genai/prompt-input";-import { showFileDialog } from "@/utils";-import { formatAssetDuration } from "@/utils/common";-import { Separator } from "@/components/ui/separator";-import { ChildOf, PaintType, isScene, useEntityState, type EngineWorld, removeComponent, setComponent } from "@/components/engine";-import { loadAsset, saveAsset, removeAsset, useAssets } from "@/components/engine";-import { query } from 'bitecs';+import { assetName } from "@diffusionstudio/assets";+import { useTrait } from "@diffusionstudio/koota-solid";+import { AssetId, isScene } from "@diffusionstudio/runtime";+import { useLibrary } from "@/engine/library";+import { pickAndImport } from "@/engine/asset-actions";-import type { Asset } from "@/components/engine/db";+import type { Asset } from "@diffusionstudio/assets";+import type { Entity } from "koota";type AssetFilter = "ALL" | "IMAGE" | "VIDEO";@@ -51,25 +41,36 @@ const ASSET_FILTER_OPTIONS: ReadonlyArray<{];export type AssetFillPickerProps = {- nodeEid: number;- fillEid: number;+ node: Entity;+ fill: Entity;+ /** Paints `asset` into the fill; see `FillPicker` for what that authors. */+ onSelectAsset(asset: Asset): void;};+/**+ * The library, as a fill picker: the image and video assets, filtered and+ * searched, one of which becomes the paint. The cards are the asset+ * browser's, so an asset can be renamed, replaced or deleted from here the+ * same way it can from the sidebar.+ */export function AssetFillPicker(props: AssetFillPickerProps) {const { openPromptInput } = usePromptInput();- const { world } = useEngine();- const assets = useAssets(world);- const c = world.components;+ const library = useLibrary();const [query, setQuery] = createSignal("");const [assetFilter, setAssetFilter] = createSignal<AssetFilter>("ALL");- const sceneSelected = createMemo(() => isScene(world, props.nodeEid));- const filterOptions = createMemo(() => ASSET_FILTER_OPTIONS.filter((option) => option.value === "IMAGE" || !sceneSelected()));+ // A scene's fill is its backdrop, which nothing plays: stills only.+ const filterOptions = createMemo(() =>+ ASSET_FILTER_OPTIONS.filter((option) => option.value === "IMAGE" || !isScene(props.node)),+ );const availableAssets = createMemo(() =>- assets.all()- .filter((asset) => (asset.type === "IMAGE" || asset.type === "VIDEO" || asset.type === "SEQUENCE")));+ (library()?.list() ?? []).filter((asset) => {+ if (asset.type === "IMAGE") return true;+ return !isScene(props.node) && (asset.type === "VIDEO" || asset.type === "SEQUENCE");+ }),+ );const filteredAssets = createMemo(() => {const normalizedQuery = query().trim().toLowerCase();@@ -77,32 +78,21 @@ export function AssetFillPicker(props: AssetFillPickerProps) {return availableAssets().filter((asset) => {if (selectedFilter !== "ALL" && asset.type !== selectedFilter) return false;if (!normalizedQuery) return true;- return asset.name.toLowerCase().includes(normalizedQuery);+ return assetName(asset).toLowerCase().includes(normalizedQuery);});});const handleImportAssets = async () => {- const files = await showFileDialog();- if (files.length === 0) return;+ const lib = library();+ if (!lib) return;try {- await Promise.all(files.map((file) => loadAsset(world, file)));+ await pickAndImport(lib, "");} catch (e) {toast("Failed to import assets", { description: (e as Error).message });}};- const selectedAssetId = useEntityState(c.AssetId, props.fillEid);-- const selectAsset = (asset: Asset) => {- if (asset.type !== "IMAGE" && asset.type !== "VIDEO") return;-- // Clear components from other fill types (solid/gradient) before assigning asset- const paintType = PaintType[asset.type];- clearColorStops(world, props.fillEid);- removeComponent(world, props.fillEid, c.Color);- setComponent(world, props.fillEid, c.AssetId, asset.id);- setComponent(world, props.fillEid, c.Paint, paintType);- };+ const selectedAssetId = useTrait(() => props.fill, AssetId);return (<div class="flex flex-col">@@ -173,10 +163,10 @@ export function AssetFillPicker(props: AssetFillPickerProps) {<div class="grid grid-cols-2 gap-2 px-4 pt-4 overflow-y-auto max-h-96"><For each={filteredAssets()}>{(asset) => (- <AssetCard+ <LazyAssetItemasset={asset}- selected={selectedAssetId() === asset.id}- onSelect={() => selectAsset(asset)}+ selected={selectedAssetId()?.value === asset.id}+ onSelect={() => props.onSelectAsset(asset)}/>)}</For>@@ -198,136 +188,3 @@ export function AssetFillPicker(props: AssetFillPickerProps) {</div>);}--function clearColorStops(world: EngineWorld, fillEid: number) {- const c = world.components;- for (const eid of query(world, [ChildOf(fillEid)])) {- removeComponent(world, eid, c.ColorStop);- }-}--function MenuItemLabel(props: { children: JSX.Element }) {- return (- <span class="flex-1 min-w-0 overflow-hidden text-ellipsis whitespace-nowrap">- {props.children}- </span>- );-}--type AssetCardProps = {- asset: Asset;- selected: boolean;- onSelect(): void;-};--function AssetCard(props: AssetCardProps) {- const { world } = useEngine();-- const duration = createMemo(() => formatAssetDuration(props.asset));-- const handlePreview = () => {- toast("Preview", { description: props.asset.name });- };-- const handleRename = async () => {- if (typeof window === "undefined") return;-- const nextName = window.prompt("Rename asset", props.asset.name);- if (nextName == null) return;- const trimmed = nextName.trim();- if (!trimmed || trimmed === props.asset.name) return;-- try {- await saveAsset(world, { ...props.asset, name: trimmed });- } catch (e) {- toast("Failed to rename", { description: (e as Error).message });- }- };-- const handleReplace = () => {- toast("Replace media", { description: props.asset.name });- };-- const handleSaveAs = () => saveAssetAs({- handle: props.asset.handle,- mimeType: props.asset.mimeType,- path: props.asset.name- });-- const handleDelete = async () => {- try {- await removeAsset(world, props.asset.id);- toast("Deleted", { description: props.asset.name });- } catch (e) {- toast("Failed to delete", { description: (e as Error).message });- }- };-- return (- <ContextMenu modal={false}>- <ContextMenuTrigger as="div" class="contents">- <button- type="button"- class="group flex flex-col gap-1 text-left outline-none"- aria-pressed={props.selected}- data-asset-id={props.asset.id}- onClick={props.onSelect}- >- <div- class="relative w-full rounded-sm aspect-video"- classList={{ "ring-2 ring-primary": props.selected }}- >- <AssetThumbnail- asset={props.asset}- class="absolute inset-0 rounded group-hover:brightness-70 overflow-hidden"- />- <Show when={duration()}>- <div class="absolute left-1 top-1 z-20 flex h-4 items-center justify-center rounded-sm bg-overlay px-1">- <span class="text-xxs text-primary-foreground">- {duration()}- </span>- </div>- </Show>- </div>- <span- class="text-xs truncate"- classList={{- "text-foreground": props.selected,- "text-muted-foreground": !props.selected,- }}- >- {props.asset.name}- </span>- </button>- </ContextMenuTrigger>- <ContextMenuPortal>- <ContextMenuContent class="w-[180px]">- <ContextMenuItem onSelect={handlePreview}>- <MenuItemLabel>Preview</MenuItemLabel>- <ContextMenuShortcut>Space</ContextMenuShortcut>- </ContextMenuItem>-- <ContextMenuSeparator />-- <ContextMenuItem onSelect={handleRename}>- <MenuItemLabel>Rename</MenuItemLabel>- <ContextMenuShortcut>⌘R</ContextMenuShortcut>- </ContextMenuItem>- <ContextMenuItem onSelect={handleReplace}>- <MenuItemLabel>Replace</MenuItemLabel>- </ContextMenuItem>- <ContextMenuItem onSelect={handleSaveAs}>- <MenuItemLabel>Save as…</MenuItemLabel>- </ContextMenuItem>-- <ContextMenuSeparator />-- <ContextMenuItem onSelect={handleDelete}>- <MenuItemLabel>Delete</MenuItemLabel>- <ContextMenuShortcut>⌫</ContextMenuShortcut>- </ContextMenuItem>- </ContextMenuContent>- </ContextMenuPortal>- </ContextMenu>- );-}diff --git a/apps/web/src/components/sidebar-right/inspector/blend-mode-menu.tsx b/apps/web/src/components/sidebar-right/inspector/blend-mode-menu.tsxdeleted file mode 100644index fbeb2015..00000000--- a/apps/web/src/components/sidebar-right/inspector/blend-mode-menu.tsx+++ /dev/null@@ -1,142 +0,0 @@-/* 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 { createSignal, For, Show } from 'solid-js';--import { Icon } from '@/components/ui/icon';-import {- DropdownMenu,- DropdownMenuContent,- DropdownMenuItem,- DropdownMenuPortal,- DropdownMenuSeparator,- DropdownMenuTrigger,-} from '@/components/ui/dropdown-menu';-import { Button } from '@/components/ui/button';-import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';-import { useEngine } from '@/context/engine';-import { BlendMode, useEntityState, setComponent } from '@/components/engine';---function capitalize(str: string) {- return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();-}--function displayBlendMode(mode: string) {- if (mode === String(BlendMode.SOURCE_OVER)) return "Normal";-- return capitalize(mode.replace("_", " "));-};--// Ordered to match Figma's blend mode menu groupings.-export const BLEND_MODE_ORDER: BlendMode[] = [- BlendMode.SOURCE_OVER,- BlendMode.DARKEN,- BlendMode.MULTIPLY,- BlendMode.COLOR_BURN,- BlendMode.LIGHTEN,- BlendMode.SCREEN,- BlendMode.COLOR_DODGE,- BlendMode.OVERLAY,- BlendMode.SOFT_LIGHT,- BlendMode.HARD_LIGHT,- BlendMode.DIFFERENCE,- BlendMode.EXCLUSION,- BlendMode.HUE,- BlendMode.SATURATION,- BlendMode.COLOR,- BlendMode.LUMINOSITY,-];--// Modes that begin a new group (separator goes before them).-export const BLEND_MODE_SEPARATORS = new Set<BlendMode>([- BlendMode.DARKEN,- BlendMode.LIGHTEN,- BlendMode.OVERLAY,- BlendMode.DIFFERENCE,- BlendMode.HUE,-]);--type BlendModeMenuProps = {- fillEid: number;-};--export function BlendModeMenu(props: BlendModeMenuProps) {- const { world } = useEngine();- const c = world.components;-- const blendMode = useEntityState(c.Appearance.blendMode, props.fillEid, 0);-- const [selected, setSelected] = createSignal(blendMode());-- const iconName = () =>- selected() === BlendMode.SOURCE_OVER- ? 'blending-mode-default'- : 'blending-mode-set';-- const handlePointerEnter = (mode: number) => {- setComponent(world, props.fillEid, c.Appearance, { blendMode: mode });- }-- const handleSelect = (mode: number) => {- setSelected(mode);- setComponent(world, props.fillEid, c.Appearance, { blendMode: mode });- }-- const handleOpenChange = (open: boolean) => {- // Reset blend mode if it has not been changed- if (!open && selected() !== blendMode()) {- handlePointerEnter(selected());- }- }-- return (- <DropdownMenu placement="bottom-end" onOpenChange={handleOpenChange}>- <Tooltip>- <TooltipTrigger<typeof DropdownMenuTrigger>- as={(triggerProps: object) => (- <DropdownMenuTrigger<typeof Button>- {...triggerProps}- as={(buttonProps) => (- <Button- {...buttonProps}- size="icon"- variant="ghost"- class="text-muted-foreground"- >- <Icon name={iconName()} class="size-6" />- </Button>- )}- />- )}- />- <TooltipContent>Blend mode</TooltipContent>- </Tooltip>- <DropdownMenuPortal>- <DropdownMenuContent class="w-44 overscroll-contain">- <For each={BLEND_MODE_ORDER}>- {(mode) => (- <>- <Show when={BLEND_MODE_SEPARATORS.has(mode)}>- <DropdownMenuSeparator />- </Show>- <DropdownMenuItem- onSelect={() => handleSelect(mode)}- onPointerEnter={() => handlePointerEnter(mode)}- >- <Show when={selected() === mode} fallback={<div class="size-6" />}>- <Icon name="confirm-check" class="size-6 text-primary-foreground" />- </Show>- <span class="flex-1 min-w-0 overflow-hidden text-ellipsis whitespace-nowrap">- {displayBlendMode(BlendMode[mode])}- </span>- </DropdownMenuItem>- </>- )}- </For>- </DropdownMenuContent>- </DropdownMenuPortal>- </DropdownMenu>- );-}diff --git a/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx b/apps/web/src/components/sidebar-right/inspector/fill-picker.tsxindex a2d66792..be622557 100644--- a/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx+++ b/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx@@ -2,21 +2,16 @@* 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, createEffect, createMemo, createSignal } from "solid-js";import { Button } from "@/components/ui/button";import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";-import {- createSignal,- createEffect,- Show,- For,- createMemo,-} from "solid-js";import { Icon } from "@/components/ui/icon";import {DropdownMenu,DropdownMenuContent,DropdownMenuItem,DropdownMenuPortal,+ DropdownMenuSeparator,DropdownMenuTrigger,} from "@/components/ui/dropdown-menu";import { SegmentedIconTabs } from "@/components/ui/segmented-icon-tabs";@@ -26,13 +21,26 @@ import {FloatingInspectorHeader,FloatingInspectorTitle,} from "@/components/ui/floating-inspector";-import { BlendModeMenu } from "./blend-mode-menu";+import { useTrait, useWorld } from "@diffusionstudio/koota-solid";+import {+ ColorStop as ColorStopElement,+ ImagePaint,+ LinearGradientPaint,+ RadialGradientPaint,+ SolidPaint,+ VideoPaint,+} from "@diffusionstudio/reconciler";+import { BlendMode, BlendModeType, Cache, Paint, PaintType, Rotation, ScaleMode, ScaleModeType } from "@diffusionstudio/runtime";+import { useEditor } from "@/engine/hooks";+import { BLEND_MODE_ORDER, BLEND_MODE_SEPARATORS, blendModeName, displayBlendMode } from "./blend-modes";+import { readStopProps } from "./gradient-stops";import { SolidFillPicker } from "./solid-picker";import { GradientFillPicker } from "./gradient-picker";import { AssetFillPicker } from "./asset-picker";-import { ScaleMode, PaintType, useEntityState, ChildOf, createEntity, deleteEntity, appendChild, removeComponent, setComponent } from "@/components/engine";-import { useEngine } from "@/context/engine";-import { query } from 'bitecs';++import type { Fit } from "@diffusionstudio/jsx";+import type { Asset } from "@diffusionstudio/assets";+import type { Entity } from "koota";export type FillTab = "solid" | "gradient" | "asset";@@ -40,151 +48,159 @@ const TABS = [{ value: "solid", icon: "fill.solid-color", label: "Color" },{ value: "gradient", icon: "fill.gradient", label: "Gradient" },{ value: "asset", icon: "fill.media", label: "Asset" },-]+];-const SCALE_MODE_OPTIONS: ReadonlyArray<{ value: ScaleMode; label: string; }> = [- { value: ScaleMode.FILL, label: "Fill", },- { value: ScaleMode.FIT, label: "Fit", },- { value: ScaleMode.COVER, label: "Cover", },- { value: ScaleMode.NONE, label: "None", },+/** What a fill authors when the picker seeds it. */+const DEFAULT_SOLID_COLOR = "#E0E0E0";+const DEFAULT_GRADIENT_STOPS = ["#E0E0E0", "#000000"];++/**+ * The fit modes `objectFit` can say. `cover` is what an unset prop renders+ * as, and `ScaleModeType.NONE` has no spelling, so it is not offered.+ */+const FIT_OPTIONS: ReadonlyArray<{ value: Fit; label: string }> = [+ { value: "cover", label: "Cover" },+ { value: "contain", label: "Contain" },+ { value: "fill", label: "Fill" },];+const FIT_NAMES: Partial<Record<ScaleModeType, Fit>> = {+ [ScaleModeType.COVER]: "cover",+ [ScaleModeType.FIT]: "contain",+ [ScaleModeType.FILL]: "fill",+};+export type FillPickerProps = {anchorRef: HTMLElement;- nodeEid: number;- fillEid: number;- open: boolean;+ node: Entity;+ fill: Entity;onClose(): void;+ /** The picker replaced the fill element; this one is the paint now. */+ onReplace(next: Entity): void;tabs?: FillTab[];};-export function getFillTab(paintType: PaintType): FillTab {- if (paintType === PaintType.SOLID) return "solid";- if (paintType === PaintType.LINEAR_GRADIENT) return "gradient";- if (paintType === PaintType.RADIAL_GRADIENT) return "gradient";- if (paintType === PaintType.VIDEO) return "asset";- return 'asset';-};+export function getFillTab(paint: PaintType): FillTab {+ if (paint === PaintType.SOLID) return "solid";+ if (paint === PaintType.LINEAR_GRADIENT) return "gradient";+ if (paint === PaintType.RADIAL_GRADIENT) return "gradient";+ return "asset";+}+/**+ * What a fill is, is which paint element it is, so changing its kind is not a+ * property write but a swap: the new element is inserted where the old one+ * stood and the old one removed. The picker therefore hands its caller the+ * entity it ends up with, and what a swap carries over is what the new kind's+ * defaults say — the same values the bitecs panel seeded, since a color is+ * not a gradient and neither is a picture.+ */export function FillPicker(props: FillPickerProps) {- const { world } = useEngine();- const c = world.components;+ const world = useWorld();+ const editor = useEditor();- const scaleMode = useEntityState(c.ScaleMode, props.fillEid, ScaleMode.FILL);- const type = useEntityState(c.Paint, props.fillEid, PaintType.SOLID);+ const paint = useTrait(() => props.fill, Paint);+ const paintType = () => paint()?.value ?? PaintType.SOLID;- const [currentTab, setCurrentTab] = createSignal<FillTab>(getFillTab(type() ?? PaintType.SOLID));+ const [currentTab, setCurrentTab] = createSignal<FillTab>(getFillTab(paintType()));- createEffect(() => {- setCurrentTab(getFillTab(type() ?? PaintType.SOLID));- });+ // The element's kind is the truth; a tab the user opened without choosing+ // anything yet (Asset, before an asset is picked) stands until it does.+ createEffect(() => setCurrentTab(getFillTab(paintType())));- const seedStop = (position: number, color: number) => {- const stopEid = createEntity(world);- setComponent(world, stopEid, c.ColorStop, {- offset: position,- opacity: 1,- color,- });- appendChild(world, stopEid, props.fillEid);- };+ /**+ * Puts `element` where the current fill stands and removes the old one.+ * The anchor is the next fill, so the paint keeps its place in the stack;+ * without one it is appended, which is where it was anyway.+ */+ const replaceFill = (element: () => unknown) => {+ const fills = props.node.get(Cache)?.fills ?? [];+ const index = fills.indexOf(props.fill);+ const anchor = index === -1 ? undefined : fills[index + 1];- const clearPaint = () => {- removeComponent(world, props.fillEid, c.Color);- removeComponent(world, props.fillEid, c.AssetId);- removeComponent(world, props.fillEid, c.ScaleMode);- for (const eid of query(world, [ChildOf(props.fillEid)])) {- deleteEntity(world, eid);- }+ const [next] = editor.insertElement(props.node, element, anchor);+ if (!next) return;++ editor.remove(props.fill);+ props.onReplace(next);};const handleTabChange = (tab: FillTab) => {setCurrentTab(tab);- world.history.transaction('Change fill type', () => {- if (tab == "solid") {- // Remove gradient/asset components, add solid- clearPaint();- setComponent(world, props.fillEid, c.Paint, PaintType.SOLID);- setComponent(world, props.fillEid, c.Color, 0xE0E0E0);- } else if (tab == "gradient") {- // Remove solid/asset components, seed two-stop gradient- clearPaint();- setComponent(world, props.fillEid, c.Paint, PaintType.LINEAR_GRADIENT);- seedStop(0, 0xE0E0E0);- seedStop(1, 0x000000);- }- });+ if (tab === "solid" && paintType() !== PaintType.SOLID) {+ replaceFill(() => <SolidPaint color={DEFAULT_SOLID_COLOR} />);+ return;+ }++ if (tab === "gradient" && getFillTab(paintType()) !== "gradient") {+ replaceFill(() => (+ <LinearGradientPaint>+ <ColorStopElement offset={0} color={DEFAULT_GRADIENT_STOPS[0]!} />+ <ColorStopElement offset={1} color={DEFAULT_GRADIENT_STOPS[1]!} />+ </LinearGradientPaint>+ ));+ }++ // "asset" waits: which asset it is, is the choice, and until one is+ // picked there is nothing to author.};- const handleScaleModeChange = (mode: ScaleMode) => {- setComponent(world, props.fillEid, c.ScaleMode, mode);+ /**+ * Swaps a linear gradient for a radial one (or back). The two are separate+ * elements, so the stops and the rotation are written out onto the new one+ * rather than carried by it.+ */+ const handleGradientKindChange = (radial: boolean) => {+ const isRadial = paintType() === PaintType.RADIAL_GRADIENT;+ if (radial === isRadial) return;++ const stops = readStopProps(world, props.fill);+ const rotation = props.fill.get(Rotation)?.value ?? 0;+ const Element = radial ? RadialGradientPaint : LinearGradientPaint;++ replaceFill(() => (+ <Element {...(rotation === 0 ? {} : { rotation })}>+ {stops.map((stop) => (+ <ColorStopElement offset={stop.offset} color={stop.color} opacity={stop.opacity} />+ ))}+ </Element>+ ));};- const availableTabs = createMemo(() => {- if (props.tabs) {- return TABS.filter((tab) => props.tabs?.includes(tab.value as FillTab));+ const handleSelectAsset = (asset: Asset) => {+ const plays = asset.type === "VIDEO" || asset.type === "SEQUENCE";+ const current = paintType();+ const sameKind = plays+ ? current === PaintType.VIDEO || current === PaintType.SEQUENCE+ : current === PaintType.IMAGE;++ // Same element, another source: a `src` write, so the fit and any tracks+ // under it survive the swap of picture.+ if (sameKind) {+ editor.editProperty(props.fill, "src", asset.path);+ return;}+ replaceFill(() => (plays ? <VideoPaint src={asset.path} /> : <ImagePaint src={asset.path} />));+ };++ const availableTabs = createMemo(() => {+ if (props.tabs) return TABS.filter((tab) => props.tabs?.includes(tab.value as FillTab));return TABS;});return (- <FloatingInspector- open={props.open}- anchorRef={props.anchorRef}- >+ <FloatingInspector open anchorRef={props.anchorRef}><FloatingInspectorHeader><FloatingInspectorTitle>{TABS.find((mode) => mode.value === currentTab())?.label}</FloatingInspectorTitle><div class="ml-auto flex items-center gap-1"><Show when={currentTab() === "asset"}>- <DropdownMenu placement="bottom-end">- <Tooltip>- <TooltipTrigger<typeof DropdownMenuTrigger>- as={(triggerProps: object) => (- <DropdownMenuTrigger<typeof Button>- {...triggerProps}- as={(buttonProps) => (- <Button- {...buttonProps}- size="icon"- variant="ghost"- class="text-muted-foreground hover:bg-accent data-expanded:bg-accent"- >- <Icon name="media-fit-type" class="size-6" />- </Button>- )}- />- )}- />- <TooltipContent>Fit mode</TooltipContent>- </Tooltip>- <DropdownMenuPortal>- <DropdownMenuContent class="w-[180px]">- <For each={SCALE_MODE_OPTIONS}>- {(option) => (- <DropdownMenuItem- class="px-0 pr-2"- onSelect={() => handleScaleModeChange(option.value)}- >- <span class="w-6 h-7 shrink-0 flex items-center justify-center overflow-clip">- <Show when={scaleMode() === option.value}>- <Icon name="confirm-check" class="text-popover-foreground" />- </Show>- </span>- <span class="flex-1 min-w-0 overflow-hidden text-ellipsis whitespace-nowrap">- {option.label}- </span>- </DropdownMenuItem>- )}- </For>- </DropdownMenuContent>- </DropdownMenuPortal>- </DropdownMenu>+ <FitMenu fill={props.fill} /></Show>- <BlendModeMenu fillEid={props.fillEid} />+ <BlendModeMenu fill={props.fill} /><Tooltip><TooltipTriggeras={Button}@@ -210,16 +226,166 @@ export function FillPicker(props: FillPickerProps) {</div><Show when={currentTab() === "solid"}>- <SolidFillPicker nodeEid={props.nodeEid} fillEid={props.fillEid} />+ <SolidFillPicker fill={props.fill} /></Show><Show when={currentTab() === "gradient"}>- <GradientFillPicker nodeEid={props.nodeEid} fillEid={props.fillEid} />+ <GradientFillPicker fill={props.fill} onChangeKind={handleGradientKindChange} /></Show><Show when={currentTab() === "asset"}>- <AssetFillPicker nodeEid={props.nodeEid} fillEid={props.fillEid} />+ <AssetFillPicker node={props.node} fill={props.fill} onSelectAsset={handleSelectAsset} /></Show></div></FloatingInspectorContent></FloatingInspector>);}++type FitMenuProps = {+ fill: Entity;+};++/** How an asset fill maps into the box: the paint's `objectFit`. */+function FitMenu(props: FitMenuProps) {+ const editor = useEditor();+ const scaleMode = useTrait(() => props.fill, ScaleMode);++ const current = () => FIT_NAMES[scaleMode()?.value ?? ScaleModeType.COVER] ?? "cover";++ const handleChange = (fit: Fit) => {+ editor.editProperty(props.fill, "objectFit", fit === "cover" ? false : fit);+ };++ return (+ <DropdownMenu placement="bottom-end">+ <Tooltip>+ <TooltipTrigger<typeof DropdownMenuTrigger>+ as={(triggerProps: object) => (+ <DropdownMenuTrigger<typeof Button>+ {...triggerProps}+ as={(buttonProps) => (+ <Button+ {...buttonProps}+ size="icon"+ variant="ghost"+ class="text-muted-foreground hover:bg-accent data-expanded:bg-accent"+ >+ <Icon name="media-fit-type" class="size-6" />+ </Button>+ )}+ />+ )}+ />+ <TooltipContent>Fit mode</TooltipContent>+ </Tooltip>+ <DropdownMenuPortal>+ <DropdownMenuContent class="w-[180px]">+ <For each={FIT_OPTIONS}>+ {(option) => (+ <DropdownMenuItem class="px-0 pr-2" onSelect={() => handleChange(option.value)}>+ <span class="w-6 h-7 shrink-0 flex items-center justify-center overflow-clip">+ <Show when={current() === option.value}>+ <Icon name="confirm-check" class="text-popover-foreground" />+ </Show>+ </span>+ <span class="flex-1 min-w-0 overflow-hidden text-ellipsis whitespace-nowrap">+ {option.label}+ </span>+ </DropdownMenuItem>+ )}+ </For>+ </DropdownMenuContent>+ </DropdownMenuPortal>+ </DropdownMenu>+ );+}++type BlendModeMenuProps = {+ fill: Entity;+};++/**+ * The paint's `blendMode`. Hovering a mode previews it by writing the trait+ * alone, so the authored value has to be kept here: while the menu is open+ * the trait holds the hover, and closing without choosing puts it back.+ */+function BlendModeMenu(props: BlendModeMenuProps) {+ const editor = useEditor();++ const blendMode = useTrait(() => props.fill, BlendMode);+ const authored = () => blendMode()?.value ?? BlendModeType.SOURCE_OVER;+ const [selected, setSelected] = createSignal(authored());++ const preview = (mode: BlendModeType) => {+ if (mode === BlendModeType.SOURCE_OVER) {+ props.fill.remove(BlendMode);+ return;+ }+ props.fill.add(BlendMode);+ props.fill.set(BlendMode, { value: mode });+ };++ const handleSelect = (mode: BlendModeType) => {+ setSelected(mode);+ editor.editProperty(+ props.fill,+ "blendMode",+ mode === BlendModeType.SOURCE_OVER ? false : blendModeName(mode),+ );+ };++ const handleOpenChange = (open: boolean) => {+ if (!open && selected() !== authored()) preview(selected());+ };++ return (+ <DropdownMenu placement="bottom-end" onOpenChange={handleOpenChange}>+ <Tooltip>+ <TooltipTrigger<typeof DropdownMenuTrigger>+ as={(triggerProps: object) => (+ <DropdownMenuTrigger<typeof Button>+ {...triggerProps}+ as={(buttonProps) => (+ <Button+ {...buttonProps}+ size="icon"+ variant="ghost"+ class="text-muted-foreground"+ >+ <Icon+ name={selected() === BlendModeType.SOURCE_OVER ? "blending-mode-default" : "blending-mode-set"}+ class="size-6"+ />+ </Button>+ )}+ />+ )}+ />+ <TooltipContent>Blend mode</TooltipContent>+ </Tooltip>+ <DropdownMenuPortal>+ <DropdownMenuContent class="w-44 overscroll-contain">+ <For each={BLEND_MODE_ORDER}>+ {(mode) => (+ <>+ <Show when={BLEND_MODE_SEPARATORS.has(mode)}>+ <DropdownMenuSeparator />+ </Show>+ <DropdownMenuItem+ onSelect={() => handleSelect(mode)}+ onPointerEnter={() => preview(mode)}+ >+ <Show when={selected() === mode} fallback={<div class="size-6" />}>+ <Icon name="confirm-check" class="size-6 text-primary-foreground" />+ </Show>+ <span class="flex-1 min-w-0 overflow-hidden text-ellipsis whitespace-nowrap">+ {displayBlendMode(mode)}+ </span>+ </DropdownMenuItem>+ </>+ )}+ </For>+ </DropdownMenuContent>+ </DropdownMenuPortal>+ </DropdownMenu>+ );+}diff --git a/apps/web/src/components/sidebar-right/inspector/fill-row.tsx b/apps/web/src/components/sidebar-right/inspector/fill-row.tsxindex 5c740f56..be5d61ea 100644--- a/apps/web/src/components/sidebar-right/inspector/fill-row.tsx+++ b/apps/web/src/components/sidebar-right/inspector/fill-row.tsx@@ -12,46 +12,43 @@ import {ContextMenuSeparator,ContextMenuTrigger,} from "@/components/ui/context-menu";-import { PaintType, useEntityState, useEntityTag, addComponent, removeComponent } from "@/components/engine";-import { useEngine } from "@/context/engine";-import { hasComponent } from 'bitecs';+import { useHas, useTrait } from "@diffusionstudio/koota-solid";+import { Hidden, Paint, PaintType } from "@diffusionstudio/runtime";+import { useEditor } from "@/engine/hooks";++import type { Entity } from "koota";type FillRowProps = {- nodeEid: number;- fillEid: number;+ fill: Entity;onSelect(): void;onRemove(): void;- onMoveUp?(): void;- onMoveDown?(): void;+ onMoveUp(): void;+ onMoveDown(): void;};const FILL_TYPE_LABELS: Record<PaintType, string> = {- [PaintType.SOLID]: 'Solid',- [PaintType.IMAGE]: 'Image',- [PaintType.VIDEO]: 'Video',- [PaintType.SEQUENCE]: 'Sequence',- [PaintType.LINEAR_GRADIENT]: 'Gradient',- [PaintType.RADIAL_GRADIENT]: 'Gradient',- [PaintType.WAVEFORM]: 'Waveform',- [PaintType.HTML]: 'Html',- [PaintType.SURFACE]: 'Surface',- [PaintType.SHADER]: 'Shader',+ [PaintType.SOLID]: "Solid",+ [PaintType.IMAGE]: "Image",+ [PaintType.VIDEO]: "Video",+ [PaintType.SEQUENCE]: "Sequence",+ [PaintType.LINEAR_GRADIENT]: "Gradient",+ [PaintType.RADIAL_GRADIENT]: "Gradient",+ [PaintType.WAVEFORM]: "Waveform",+ [PaintType.HTML]: "Html",+ [PaintType.SURFACE]: "Surface",+ [PaintType.SHADER]: "Shader",};export function FillRow(props: FillRowProps) {- const { world } = useEngine();- const c = world.components;+ const editor = useEditor();++ const paint = useTrait(() => props.fill, Paint);+ const hidden = useHas(() => props.fill, Hidden);- const fillType = useEntityState(c.Paint, props.fillEid, PaintType.SOLID);- const hidden = useEntityTag(c.Hidden, () => props.fillEid);+ const label = createMemo(() => FILL_TYPE_LABELS[paint()?.value ?? PaintType.SOLID] ?? "Solid");- const label = createMemo(() => (FILL_TYPE_LABELS[fillType() as PaintType] ?? 'Solid'));const toggleHidden = () => {- if (hasComponent(world, props.fillEid, c.Hidden)) {- removeComponent(world, props.fillEid, c.Hidden);- } else {- addComponent(world, props.fillEid, c.Hidden);- }+ editor.editProperty(props.fill, "hidden", !hidden());};return (@@ -61,28 +58,18 @@ export function FillRow(props: FillRowProps) {label={label()}class={hidden() ? "opacity-50" : undefined}>- <FillItem- nodeEid={props.nodeEid}- fillEid={props.fillEid}- onClick={props.onSelect}- />+ <FillItem fill={props.fill} onClick={props.onSelect} /></ContextMenuTrigger><ContextMenuContent>- <ContextMenuItem onSelect={props.onMoveUp}>- Move Up- </ContextMenuItem>- <ContextMenuItem onSelect={props.onMoveDown}>- Move Down- </ContextMenuItem>+ <ContextMenuItem onSelect={props.onMoveUp}>Move Up</ContextMenuItem>+ <ContextMenuItem onSelect={props.onMoveDown}>Move Down</ContextMenuItem><ContextMenuSeparator /><ContextMenuItem onSelect={toggleHidden}>{hidden() ? "Unhide" : "Hide"}</ContextMenuItem><ContextMenuSeparator />- <ContextMenuItem onSelect={props.onRemove}>- Remove- </ContextMenuItem>+ <ContextMenuItem onSelect={props.onRemove}>Remove</ContextMenuItem></ContextMenuContent></ContextMenu>- )+ );}diff --git a/apps/web/src/components/sidebar-right/inspector/fills.tsx b/apps/web/src/components/sidebar-right/inspector/fills.tsxindex b724c841..1d7a4eca 100644--- a/apps/web/src/components/sidebar-right/inspector/fills.tsx+++ b/apps/web/src/components/sidebar-right/inspector/fills.tsx@@ -2,64 +2,80 @@* 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,- createMemo,- createSignal,-} from "solid-js";+import { For, Show, createMemo, createSignal } from "solid-js";import { Button } from "@/components/ui/button";import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";import { Icon } from "@/components/ui/icon";import { PanelSection } from "@/components/ui/panel-section";-+import { SolidPaint } from "@diffusionstudio/reconciler";+import { Cache, isText } from "@diffusionstudio/runtime";+import { useDerived, useEditor } from "@/engine/hooks";import { FillPicker, type FillTab } from "./fill-picker";-import { useEntityState, createEntity, deleteEntity, getSiblingEntities, PaintType, isText, appendChild, setComponent } from "@/components/engine";-import { useEngine } from "@/context/engine";import { FillRow } from "./fill-row";+import type { Entity } from "koota";++/** What "Add fill" authors. */+const DEFAULT_FILL_COLOR = "#E0E0E0";++/** A text is painted by its glyphs, which no picture and no video fills. */+const TEXT_TABS: FillTab[] = ["solid", "gradient"];++// Stable identity, so a node without fills does not resample every tick.+const NO_FILLS: Entity[] = [];+type FillsSettingsProps = {- selection: Set<number>;+ selection: Entity[];};+/**+ * The paint children of the selected node, in paint order (the list is shown+ * topmost first, so the last element in the file is the first row). A row+ * opens the picker, where the fill's kind can be changed — which replaces the+ * element, since each kind is a tag of its own, so the picker hands back the+ * entity it ended up with.+ */export function FillsSettings(props: FillsSettingsProps) {- const { world } = useEngine();+ const editor = useEditor();+ const entity = () => props.selection[0]!;- const c = world.components;let anchorRef!: HTMLDivElement;- const [selectedFill, setSelectedFill] = createSignal<number>();- const eid = () => props.selection.values().next().value!;+ const [picked, setPicked] = createSignal<Entity>();- const fillEids = useEntityState(c.Cache.fills, eid, []);- const tabs = createMemo<FillTab[] | undefined>(() => {- return isText(world, eid())- ? ['solid', 'gradient']- : undefined;- });+ // Cache is derived state, written without change events.+ const fills = useDerived(() => entity().get(Cache)?.fills ?? NO_FILLS);++ const tabs = createMemo<FillTab[] | undefined>(() =>+ isText(entity()) ? TEXT_TABS : undefined,+ );const handleAppendFill = () => {- world.history.transaction('Append fill', () => {- const solidEid = createEntity(world);- setComponent(world, solidEid, c.Paint, PaintType.SOLID);- setComponent(world, solidEid, c.Color, 0xE0E0E0);- appendChild(world, solidEid, eid());- })+ editor.insertElement(entity(), () => <SolidPaint color={DEFAULT_FILL_COLOR} />);};- const handleClosePicker = () => setSelectedFill(undefined);- const handleSelectFill = (fillEid: number) => setSelectedFill(fillEid);- const handleRemoveFill = (fillEid: number) => deleteEntity(world, fillEid);-- const handleReorderFill = (fillEid: number, direction: number) => {- const fills = getSiblingEntities(world, fillEid, c.Paint);- const index = fills.indexOf(fillEid);- const newIndex = index + direction;- if (newIndex >= 0 && newIndex < fills.length) {- fills.splice(newIndex, 0, fills.splice(index, 1)[0]);- for (const [idx, eid] of fills.entries()) {- setComponent(world, eid, c.ItemIndex, idx);- }+ // Read back off the list, so removing a fill closes the picker on it.+ const editing = createMemo(() => {+ const fill = picked();+ return fill !== undefined && fills().includes(fill) ? fill : undefined;+ });++ /**+ * Swaps `fill` with its neighbour, later in the file (`direction` 1, on+ * top) or earlier. Written as a swap because a move needs an anchor:+ * `reparent` appends without one, and refuses an append into the parent the+ * element already has.+ */+ const handleReorderFill = (fill: Entity, direction: number) => {+ const siblings = fills();+ const index = siblings.indexOf(fill);+ const target = index + direction;+ if (index === -1 || target < 0 || target >= siblings.length) return;++ if (direction > 0) {+ editor.reparent(siblings[target]!, entity(), fill);+ } else {+ editor.reparent(fill, entity(), siblings[target]!);}};@@ -83,26 +99,26 @@ export function FillsSettings(props: FillsSettingsProps) {</Tooltip>}>- <For each={fillEids().toReversed()}>- {(fillEid) => (+ <For each={fills().toReversed()}>+ {(fill) => (<FillRow- nodeEid={eid()}- fillEid={fillEid}- onSelect={() => handleSelectFill(fillEid)}- onRemove={() => handleRemoveFill(fillEid)}- onMoveUp={() => handleReorderFill(fillEid, 1)}- onMoveDown={() => handleReorderFill(fillEid, -1)}+ fill={fill}+ onSelect={() => setPicked(fill)}+ onRemove={() => editor.remove(fill)}+ onMoveUp={() => handleReorderFill(fill, 1)}+ onMoveDown={() => handleReorderFill(fill, -1)}/>)}</For></PanelSection>- <Show when={selectedFill()}>++ <Show when={editing() !== undefined}><FillPicker- nodeEid={eid()}- fillEid={selectedFill()!}+ node={entity()}+ fill={editing()!}anchorRef={anchorRef}- open={!!selectedFill()}- onClose={handleClosePicker}+ onClose={() => setPicked(undefined)}+ onReplace={(next) => setPicked(next)}tabs={tabs()}/></Show>diff --git a/apps/web/src/components/sidebar-right/inspector/gradient-picker.tsx b/apps/web/src/components/sidebar-right/inspector/gradient-picker.tsxindex e0fec2f4..c4e3f35b 100644--- a/apps/web/src/components/sidebar-right/inspector/gradient-picker.tsx+++ b/apps/web/src/components/sidebar-right/inspector/gradient-picker.tsx@@ -24,7 +24,7 @@ import {SelectValue,} from "@/components/ui/select";import { ControlledTextField } from "@/components/ui/text-field";-import { Keyframe } from "@/components/ui/keyframe-bitecs";+import { Keyframe } from "@/components/ui/keyframe";import { OpacitySwatch } from "@/components/ui/opacity-swatch";import {FloatingInspector,@@ -32,28 +32,37 @@ import {FloatingInspectorHeader,FloatingInspectorTitle,} from "@/components/ui/floating-inspector";+import { ColorOpacityPicker } from "@/components/ui/color-opacity-picker";import { useDrag } from "@/hooks/use-drag";import { clamp, mergeColorWithOpacity } from "@/utils";-import { colorToHex, parseColor } from "@/utils/color";-import { ColorOpacityPicker } from "@/components/ui/color-opacity-picker";-import { ChildOf, PaintType, useEntityState, useEntityStates, useQuery, createEntity, deleteEntity, appendChild, setComponent } from "@/components/engine";-import { useEngine } from "@/context/engine";-import { Not } from 'bitecs';+import { useTrait, useWorld } from "@diffusionstudio/koota-solid";+import { ColorStop as ColorStopElement } from "@diffusionstudio/reconciler";+import { Computed, Paint, PaintType, colorToHex, parseColor } from "@diffusionstudio/runtime";+import { useDerived, useEditor } from "@/engine/hooks";+import { syncKeyframe } from "@/engine/keyframes";+import { readGradientStops, sameGradientStops, type GradientStop } from "./gradient-stops";++import type { Entity } from "koota";export type GradientPickerProps = {- nodeEid: number;- fillEid: number;+ fill: Entity;+ /** Swaps the gradient for one of the other kind, which is another element. */+ onChangeKind(radial: boolean): void;};const GRADIENT_STYLE_OPTIONS = ["Linear", "Radial"] as const;type GradientStyleOption = (typeof GRADIENT_STYLE_OPTIONS)[number];+/** `<colorStop>`'s and `<linearGradientPaint>`'s defaults. */+const DEFAULT_OPACITY = 1;+const DEFAULT_ROTATION = 0;+function handleInitialInputPointerDown(e: PointerEvent & { currentTarget: HTMLInputElement }) {if (document.activeElement === e.currentTarget) return;e.preventDefault();e.currentTarget.focus();e.currentTarget.select();-};+}function getSliderRatio(element: HTMLDivElement | undefined,@@ -63,7 +72,7 @@ function getSliderRatio(const rect = element.getBoundingClientRect();if (!rect.width) return null;return Math.round(clamp((pos.x - rect.left) / rect.width, 0, 1) * 100) / 100;-};+}// Visual position of a stop on the track in [0, 1]. Offsets in [0, 1] map// directly so the right edge stays at 100%; offsets > 1 wrap so cycling@@ -77,90 +86,96 @@ function isPrimaryPointerButton(e: PointerEvent) {return e.button === 0 && !e.ctrlKey;}+/**+ * A gradient paint: its stops, its rotation, and which of the two gradient+ * elements it is. Every stop is a `<colorStop offset color opacity>` of its+ * own, so adding, moving and deleting one are element edits; the values shown+ * are `Computed`, which the motion system writes.+ */export function GradientFillPicker(props: GradientPickerProps) {- const { world } = useEngine();- const c = world.components;+ const world = useWorld();+ const editor = useEditor();let rootRef: HTMLDivElement | undefined;let stopTrackRef: HTMLDivElement | undefined;- let draggedStopEid: number | null = null;-- const [selectedStopEid, setSelectedStopEid] = createSignal<number | null>(null);- const [colorPickerStopEid, setColorPickerStopEid] = createSignal<number | null>(null);-- const stopEids = useQuery([c.ColorStop, ChildOf(props.fillEid), Not(c.Deleted)]);- const offsets = useEntityStates(c.Computed.stopOffset, stopEids, 0);- const colors = useEntityStates(c.Computed.stopColor, stopEids, 0xFFFFFF);- const opacities = useEntityStates(c.Computed.stopOpacity, stopEids, 1);-- const stops = createMemo(() => stopEids()- .map((eid, idx) => ({- eid,- offset: offsets()[idx],- color: colors()[idx],- opacity: opacities()[idx],- }))- .toSorted((a, b) => a.offset - b.offset)- );+ let draggedStop: Entity | null = null;++ const [selectedStop, setSelectedStop] = createSignal<Entity | null>(null);+ const [colorPickerStop, setColorPickerStop] = createSignal<Entity | null>(null);++ const stops = useDerived(() => readGradientStops(world, props.fill), sameGradientStops);++ const paint = useTrait(() => props.fill, Paint);+ const rotation = useDerived(() => props.fill.get(Computed)?.rotation ?? DEFAULT_ROTATION);- const rotation = useEntityState(c.Computed.rotation, props.fillEid, 0);- const fillType = useEntityState(c.Paint, props.fillEid, PaintType.LINEAR_GRADIENT);+ const editOffset = (stop: Entity, offset: number) => {+ editor.editProperty(stop, "offset", offset);+ syncKeyframe(world, editor, stop, "offset", offset);+ };++ const editColor = (stop: Entity, color: number) => {+ const hex = colorToHex(color);+ editor.editProperty(stop, "color", hex);+ syncKeyframe(world, editor, stop, "color", hex);+ };++ const editOpacity = (stop: Entity, opacity: number) => {+ const value = Math.round(opacity * 100) / 100;+ editor.editProperty(stop, "opacity", value === DEFAULT_OPACITY ? false : value);+ syncKeyframe(world, editor, stop, "opacity", value);+ };++ /** Adds a `<colorStop>` and returns it, or null if the fill has no source yet. */+ const insertStop = (offset: number, color: number, opacity: number): Entity | null => {+ const [stop] = editor.insertElement(props.fill, () => (+ <ColorStopElement+ offset={offset}+ color={colorToHex(color)}+ {...(opacity === DEFAULT_OPACITY ? {} : { opacity })}+ />+ ));+ return stop ?? null;+ };const addStop = () => {const current = stops();const first = current[0];const second = current[1];- const newPosition = 0;const adjustedFirstPosition = second? clamp((second.offset ?? 0) / 2, 0, 1): clamp((first?.offset ?? 0) / 2, 0, 1);- const stopEid = world.history.transaction('Add gradient stop', () => {- if (first) {- setComponent(world, first.eid, c.ColorStop, { offset: adjustedFirstPosition });- }-- const newStopEid = createEntity(world);- setComponent(world, newStopEid, c.ColorStop, {- offset: newPosition,- color: first?.color ?? 0xFFFFFF,- opacity: first?.opacity ?? 1,- });- appendChild(world, newStopEid, props.fillEid);- return newStopEid;- });-- setSelectedStopEid(stopEid);+ if (first) editOffset(first.entity, adjustedFirstPosition);+ const stop = insertStop(0, first?.color ?? 0xFFFFFF, first?.opacity ?? DEFAULT_OPACITY);+ if (stop) setSelectedStop(stop);};- const removeSelectedStop = () => {- const eid = selectedStopEid();- if (eid === null) return;- if (stops().length <= 2) return;- removeStopEid(eid);+ const removeStop = (stop: Entity) => {+ editor.remove(stop);+ if (selectedStop() === stop) setSelectedStop(null);+ if (colorPickerStop() === stop) setColorPickerStop(null);};- const removeStopEid = (eid: number) => {- deleteEntity(world, eid);- if (selectedStopEid() === eid) setSelectedStopEid(null);- if (colorPickerStopEid() === eid) setColorPickerStopEid(null);+ const removeSelectedStop = () => {+ const stop = selectedStop();+ if (stop === null || stops().length <= 2) return;+ removeStop(stop);};const redistributeStopsEvenly = () => {- if (stops().length <= 1) return;+ const current = stops();+ if (current.length <= 1) return;- const denom = Math.max(stops().length - 1, 1);- world.history.transaction('Redistribute gradient stops', () => {- for (const [index, stop] of stops().entries()) {- setComponent(world, stop.eid, c.ColorStop, { offset: index / denom });- }- });+ const denom = Math.max(current.length - 1, 1);+ for (const [index, stop] of current.entries()) {+ editOffset(stop.entity, index / denom);+ }};- const duplicateStop = (eid: number) => {+ const duplicateStop = (stop: Entity) => {const sorted = stops();- const sourceIndex = sorted.findIndex(s => s.eid === eid);+ const sourceIndex = sorted.findIndex((s) => s.entity === stop);const source = sorted[sourceIndex];if (!source) return;@@ -173,30 +188,19 @@ export function GradientFillPicker(props: GradientPickerProps) {offset = sourceOffset + (clamp(next.offset, 0, 1) - sourceOffset) / 2;} else if (prev) {offset = clamp(prev.offset, 0, 1) + (sourceOffset - clamp(prev.offset, 0, 1)) / 2;- } else {- offset = clamp(sourceOffset + 0.01, 0, 1);}if (offset === sourceOffset) {offset = clamp(sourceOffset + 0.01, 0, 1);}- const stopEid = world.history.transaction('Duplicate gradient stop', () => {- const newStopEid = createEntity(world);- setComponent(world, newStopEid, c.ColorStop, { ...source, offset });- appendChild(world, newStopEid, props.fillEid);- return newStopEid;- });- setSelectedStopEid(stopEid);+ const copy = insertStop(offset, source.color, source.opacity);+ if (copy) setSelectedStop(copy);};const removeAllStops = () => {- world.history.transaction('Remove all gradient stops', () => {- for (const stop of stops()) {- deleteEntity(world, stop.eid);- }- });- setSelectedStopEid(null);- setColorPickerStopEid(null);+ editor.remove(stops().map((stop) => stop.entity));+ setSelectedStop(null);+ setColorPickerStop(null);};const handleKeyDown = (e: KeyboardEvent) => {@@ -209,7 +213,7 @@ export function GradientFillPicker(props: GradientPickerProps) {) {return;}- if (selectedStopEid() === null) return;+ if (selectedStop() === null) return;// Prevent the event from bubbling up to the document.// otherwise the selection would get deleted instead@@ -224,58 +228,41 @@ export function GradientFillPicker(props: GradientPickerProps) {// Clear stale selection if the selected stop was deleted.createEffect(() => {- const eid = selectedStopEid();- if (eid === null) return;- if (!stopEids().includes(eid)) setSelectedStopEid(null);+ const stop = selectedStop();+ if (stop === null) return;+ if (!stops().some((s) => s.entity === stop)) setSelectedStop(null);});createEffect(() => {- const eid = colorPickerStopEid();- if (eid === null) return;- if (!stopEids().includes(eid)) setColorPickerStopEid(null);+ const stop = colorPickerStop();+ if (stop === null) return;+ if (!stops().some((s) => s.entity === stop)) setColorPickerStop(null);});- const spawnColorPicker = (_anchor: HTMLElement, eid: number) => {- setSelectedStopEid(eid);- setColorPickerStopEid(eid);+ const spawnColorPicker = (stop: Entity) => {+ setSelectedStop(stop);+ setColorPickerStop(stop);};- const closeStopColorPicker = () => {- setColorPickerStopEid(null);- };-- const colorPickerStop = createMemo(() => {- return stops().find(s => s.eid === colorPickerStopEid()) ?? null;- });+ const pickedStop = createMemo(() => stops().find((s) => s.entity === colorPickerStop()) ?? null);- const insertStopAtRatio = (ratio: number): number => {+ const insertStopAtRatio = (ratio: number): Entity | null => {const nearest = stops().slice().sort((a, b) => Math.abs(a.offset - ratio) - Math.abs(b.offset - ratio),)[0];- const stopEid = createEntity(world);- setComponent(world, stopEid, c.ColorStop, {- offset: ratio,- color: nearest?.color ?? 0xFFFFFF,- opacity: nearest?.opacity ?? 1,- });- appendChild(world, stopEid, props.fillEid);-- return stopEid;+ return insertStop(ratio, nearest?.color ?? 0xFFFFFF, nearest?.opacity ?? DEFAULT_OPACITY);};const updateStopDragPosition = (pos: { x: number; y: number }) => {const ratio = getSliderRatio(stopTrackRef, pos);-- if (draggedStopEid && ratio !== null) {- setComponent(world, draggedStopEid, c.ColorStop, { offset: ratio });- }+ if (draggedStop && ratio !== null) editOffset(draggedStop, ratio);};- const beginStopDrag = (eid: number, e: PointerEvent) => {+ const beginStopDrag = (stop: Entity, e: PointerEvent) => {if (!isPrimaryPointerButton(e)) return;- draggedStopEid = eid;- setSelectedStopEid(eid);+ draggedStop = stop;+ setSelectedStop(stop);stopDrag.onPointerDown(e);};@@ -284,13 +271,8 @@ export function GradientFillPicker(props: GradientPickerProps) {const ratio = getSliderRatio(stopTrackRef, { x: e.clientX, y: e.clientY });if (ratio === null) return;- let insertedEid: number | null = null;- world.history.transaction("Insert gradient stop", () => {- insertedEid = insertStopAtRatio(ratio);- });- if (insertedEid !== null) {- beginStopDrag(insertedEid, e);- }+ const inserted = insertStopAtRatio(ratio);+ if (inserted) beginStopDrag(inserted, e);};const stopDrag = useDrag({@@ -298,38 +280,39 @@ export function GradientFillPicker(props: GradientPickerProps) {onDragMove: updateStopDragPosition,onDragEnd: (pos) => {updateStopDragPosition(pos);- draggedStopEid = null;+ draggedStop = null;},});- const rotateGradient = () => {- setComponent(world, props.fillEid, c.Rotation, (rotation() + 90) % 360);+ const editRotation = (value: number) => {+ const next = ((value % 360) + 360) % 360;+ editor.editProperty(props.fill, "rotation", next === DEFAULT_ROTATION ? false : next);+ syncKeyframe(world, editor, props.fill, "rotation", next);};+ const rotateGradient = () => editRotation(rotation() + 90);+const flipGradient = () => {- world.history.transaction('Flip gradient', () => {- for (const stop of stops()) {- setComponent(world, stop.eid, c.ColorStop, { offset: 1 - stop.offset });- }- });+ for (const stop of stops()) {+ editOffset(stop.entity, 1 - stop.offset);+ }};const handleGradientStyleChange = (style: GradientStyleOption | null) => {- const value = style === "Radial" ? PaintType.RADIAL_GRADIENT : PaintType.LINEAR_GRADIENT;- setComponent(world, props.fillEid, c.Paint, value);+ if (style === null) return;+ props.onChangeKind(style === "Radial");};const handleRotationChange = (value: number) => {- value = Math.round(value * 100) / 100;- setComponent(world, props.fillEid, c.Rotation, ((value % 360) + 360) % 360);+ editRotation(Math.round(value * 100) / 100);};- const gradientLabel = createMemo(() => {- return fillType() === PaintType.RADIAL_GRADIENT ? "Radial" : "Linear";- });+ const gradientLabel = createMemo(() =>+ paint()?.value === PaintType.RADIAL_GRADIENT ? "Radial" : "Linear",+ );const gradientBackground = createMemo(() => {- const parts = stops().map(s => {+ const parts = stops().map((s) => {const offset = clamp(s.offset * 100, 0, 100);return `${mergeColorWithOpacity(s.color, s.opacity)} ${offset}%`;});@@ -337,7 +320,7 @@ export function GradientFillPicker(props: GradientPickerProps) {});const handleGradientBackgroundClick = () => {- setSelectedStopEid(null);+ setSelectedStop(null);};return (@@ -346,13 +329,11 @@ export function GradientFillPicker(props: GradientPickerProps) {<div class="flex items-center gap-2"><Select<GradientStyleOption>class="flex-1"- value={fillType() === PaintType.RADIAL_GRADIENT ? "Radial" : "Linear"}+ value={gradientLabel()}options={[...GRADIENT_STYLE_OPTIONS]}onChange={handleGradientStyleChange}itemComponent={(itemProps) => (- <SelectItem item={itemProps.item}>- {itemProps.item.rawValue}- </SelectItem>+ <SelectItem item={itemProps.item}>{itemProps.item.rawValue}</SelectItem>)}><SelectTrigger>@@ -433,13 +414,13 @@ export function GradientFillPicker(props: GradientPickerProps) {<ContextMenuTriggeras="div"class="absolute top-0 touch-none"- classList={{ "opacity-80": selectedStopEid() !== stop.eid }}+ classList={{ "opacity-80": selectedStop() !== stop.entity }}style={{filter: "drop-shadow(0px 3px 3px rgba(0,0,0,0.48)) drop-shadow(0px 2px 1px rgba(0,0,0,0.48))",left: `${visualOffset(stop.offset) * 100}%`,transform: "translateX(-50%)",}}- onPointerDown={(e) => beginStopDrag(stop.eid, e)}+ onPointerDown={(e) => beginStopDrag(stop.entity, e)}><svgwidth="18"@@ -452,11 +433,11 @@ export function GradientFillPicker(props: GradientPickerProps) {d="M7.48137 1.69753C8.27955 0.766245 9.72025 0.766211 10.5185 1.69746L13.5184 5.19738C13.8291 5.55987 13.9999 6.02154 13.9999 6.49896L13.9999 11.9258C13.9999 13.0304 13.1044 13.9258 11.9999 13.9258L5.99999 13.9258C4.89538 13.9258 3.99993 13.0303 3.99999 11.9257L4.0003 6.49885C4.00033 6.02149 4.17109 5.55989 4.48173 5.19745L7.48137 1.69753Z"fill={colorToHex(stop.color)}stroke={- selectedStopEid() === stop.eid+ selectedStop() === stop.entity? "var(--ring)": "var(--border-input)"}- stroke-width={selectedStopEid() === stop.eid ? 2 : 1}+ stroke-width={selectedStop() === stop.entity ? 2 : 1}stroke-linejoin="round"/></svg>@@ -470,12 +451,12 @@ export function GradientFillPicker(props: GradientPickerProps) {Redistribute stops evenly</ContextMenuItem><ContextMenuSeparator />- <ContextMenuItem onSelect={() => duplicateStop(stop.eid)}>+ <ContextMenuItem onSelect={() => duplicateStop(stop.entity)}>Duplicate stop</ContextMenuItem><ContextMenuItemdisabled={stops().length <= 2}- onSelect={() => removeStopEid(stop.eid)}+ onSelect={() => removeStop(stop.entity)}>Delete stop</ContextMenuItem>@@ -517,12 +498,12 @@ export function GradientFillPicker(props: GradientPickerProps) {<For each={stops()}>{(stop) => (<GradientStopRow- eid={stop.eid}- offset={stop.offset}- color={stop.color}- opacity={stop.opacity}- selected={selectedStopEid() === stop.eid}- spawnColorPicker={spawnColorPicker}+ stop={stop}+ selected={selectedStop() === stop.entity}+ onEditOffset={(value) => editOffset(stop.entity, value)}+ onEditColor={(value) => editColor(stop.entity, value)}+ onEditOpacity={(value) => editOpacity(stop.entity, value)}+ onPickColor={() => spawnColorPicker(stop.entity)}/>)}</For>@@ -530,7 +511,7 @@ export function GradientFillPicker(props: GradientPickerProps) {</div><FloatingInspector- open={colorPickerStopEid() !== null}+ open={colorPickerStop() !== null}anchorRef={rootRef}><FloatingInspectorHeader>@@ -541,7 +522,7 @@ export function GradientFillPicker(props: GradientPickerProps) {size="icon"variant="ghost"class="text-muted-foreground"- onClick={closeStopColorPicker}+ onClick={() => setColorPickerStop(null)}><Icon name="close-remove" /></TooltipTrigger>@@ -549,19 +530,14 @@ export function GradientFillPicker(props: GradientPickerProps) {</Tooltip></FloatingInspectorHeader><FloatingInspectorContent class="p-0">- <Show when={colorPickerStop()}>+ <Show when={pickedStop()}>{(stop) => (<ColorOpacityPickercolor={stop().color}opacity={stop().opacity}- onColorChange={(next) => {- setComponent(world, stop().eid, c.ColorStop, { color: next });- }}- onOpacityChange={(next) => {- setComponent(world, stop().eid, c.ColorStop, { opacity: next });- }}- onBeginChange={(label) => world.history.startTransaction(label)}- onEndChange={() => world.history.commitTransaction()}+ onColorChange={(next) => editColor(stop().entity, next)}+ onOpacityChange={(next) => editOpacity(stop().entity, next)}+ keyframeTarget={stop().entity}/>)}</Show>@@ -572,19 +548,16 @@ export function GradientFillPicker(props: GradientPickerProps) {}type GradientStopRowProps = {- eid: number;- offset: number;- color: number;- opacity: number;+ stop: GradientStop;selected: boolean;- spawnColorPicker(anchor: HTMLElement, eid: number): void;-}+ onEditOffset(value: number): void;+ onEditColor(value: number): void;+ onEditOpacity(value: number): void;+ onPickColor(): void;+};function GradientStopRow(props: GradientStopRowProps) {- const { world } = useEngine();- const c = world.components;-- const colorText = createMemo(() => colorToHex(props.color).replace("#", ""));+ const colorText = createMemo(() => colorToHex(props.stop.color).replace("#", ""));const [colorDraft, setColorDraft] = createSignal(colorText());createEffect(() => {@@ -598,7 +571,7 @@ function GradientStopRow(props: GradientStopRowProps) {return;}- setComponent(world, props.eid, c.ColorStop, { color: nextColor });+ props.onEditColor(nextColor);};const handleColorInput = (event: InputEvent & { currentTarget: HTMLInputElement }) => {@@ -626,23 +599,23 @@ function GradientStopRow(props: GradientStopRowProps) {};const handleOffsetChange = (value: number) => {- setComponent(world, props.eid, c.ColorStop, { offset: Math.max(0, Math.round(value)) / 100 });+ props.onEditOffset(Math.max(0, Math.round(value)) / 100);};const handleOpacityChange = (value: number) => {- setComponent(world, props.eid, c.ColorStop, { opacity: clamp(Math.round(value), 0, 100) / 100 });+ props.onEditOpacity(clamp(Math.round(value), 0, 100) / 100);};const handleClickColorPicker = (e: MouseEvent & { currentTarget: HTMLButtonElement }) => {e.stopPropagation();- props.spawnColorPicker(e.currentTarget, props.eid);+ props.onPickColor();};return (<div class="flex min-w-0 items-center gap-2"><ControlledTextFieldclass="w-15 shrink-0"- value={Math.round(props.offset * 100)}+ value={Math.round(props.stop.offset * 100)}min={0}step={1}unit="%"@@ -651,7 +624,7 @@ function GradientStopRow(props: GradientStopRowProps) {autoSelectonNumber={handleOffsetChange}inputClassName="transition-none"- keyframe={<Keyframe target={props.eid} property="stop.offset" />}+ keyframe={<Keyframe target={props.stop.entity} property="offset" />}/><div@@ -667,7 +640,7 @@ function GradientStopRow(props: GradientStopRowProps) {onFocusIn={(e) => e.stopPropagation()}onClick={handleClickColorPicker}>- <OpacitySwatch color={props.color} opacity={props.opacity} />+ <OpacitySwatch color={props.stop.color} opacity={props.stop.opacity} /></button><input@@ -686,13 +659,13 @@ function GradientStopRow(props: GradientStopRowProps) {onPointerDown={(e) => e.stopPropagation()}onFocusIn={(e) => e.stopPropagation()}>- <Keyframe target={props.eid} property="stop.color" />+ <Keyframe target={props.stop.entity} property="color" /></div></div><ControlledTextFieldclass="w-15 shrink-0"- value={Math.round(props.opacity * 100)}+ value={Math.round(props.stop.opacity * 100)}min={0}max={100}step={1}@@ -702,7 +675,7 @@ function GradientStopRow(props: GradientStopRowProps) {autoSelectonNumber={handleOpacityChange}inputClassName="rounded-l-none"- keyframe={<Keyframe target={props.eid} property="stop.opacity" />}+ keyframe={<Keyframe target={props.stop.entity} property="opacity" />}/></div></div>diff --git a/apps/web/src/components/sidebar-right/inspector/gradient-stops.ts b/apps/web/src/components/sidebar-right/inspector/gradient-stops.tsnew file mode 100644index 00000000..d5449973--- /dev/null+++ b/apps/web/src/components/sidebar-right/inspector/gradient-stops.ts@@ -0,0 +1,59 @@+/* 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 { ChildOf, ColorStop, Computed, colorToHex } from "@diffusionstudio/runtime";++import type { Entity, World } from "koota";++export type GradientStop = {+ entity: Entity;+ /** Position along the gradient; the animated value, so 0-1 is only the usual case. */+ offset: number;+ color: number;+ opacity: number;+};++/**+ * The `<colorStop>` children of a gradient paint, in the order they paint in+ * (by offset, not by document order: where a stop sits is its `offset`, and+ * the file's order says nothing). Values come off `Computed`, which the+ * motion system writes, so callers sample this through `useDerived`.+ */+export function readGradientStops(world: World, fill: Entity): GradientStop[] {+ return [...world.query(ColorStop, ChildOf(fill))]+ .map((entity) => {+ const computed = entity.get(Computed);+ return {+ entity,+ offset: computed?.stopOffset ?? 0,+ color: computed?.color ?? 0xFFFFFF,+ opacity: computed?.opacity ?? 1,+ };+ })+ .sort((a, b) => a.offset - b.offset);+}++/** Compares two samples by value, so a resample per tick does not notify. */+export function sameGradientStops(a: GradientStop[], b: GradientStop[]): boolean {+ return a.length === b.length && a.every((stop, index) => {+ const other = b[index]!;+ return stop.entity === other.entity+ && stop.offset === other.offset+ && stop.color === other.color+ && stop.opacity === other.opacity;+ });+}++/**+ * The stops as `<colorStop>` props, for spelling them onto another gradient+ * element (the two gradient kinds are two tags, so switching between them+ * writes the stops out again). Opacity is left off at its default.+ */+export function readStopProps(world: World, fill: Entity): { offset: number; color: string; opacity?: number }[] {+ return readGradientStops(world, fill).map((stop) => ({+ offset: stop.offset,+ color: colorToHex(stop.color),+ ...(stop.opacity === 1 ? {} : { opacity: stop.opacity }),+ }));+}diff --git a/apps/web/src/components/sidebar-right/inspector/inspector.tsx b/apps/web/src/components/sidebar-right/inspector/inspector.tsxindex c0157261..610b8829 100644--- a/apps/web/src/components/sidebar-right/inspector/inspector.tsx+++ b/apps/web/src/components/sidebar-right/inspector/inspector.tsx@@ -30,7 +30,7 @@ import { LayoutPanel } from "./layout";// import { TransformSettings } from "./transform";// import { CaptionSettings } from "./caption-settings";import { TextPanel } from "./text";-// import { FillsSettings } from "./fills";+import { FillsSettings } from "./fills";import { StrokesSettings } from "./strokes";import { ShadowsSettings } from "./shadows";import { EffectsSettings } from "./effects";@@ -149,9 +149,9 @@ export function Inspector() {<TextPanel selection={nodes()} /></Show>- {/* <Show when={includesTarget("shape", "text", "scene", "caption")}>+ <Show when={includesTarget("shape", "text", "scene", "caption")}><FillsSettings selection={nodes()} />- </Show> */}+ </Show><Show when={includesTarget("shape", "text", "scene", "caption")}><StrokesSettings selection={nodes()} />diff --git a/apps/web/src/components/sidebar-right/inspector/solid-picker.tsx b/apps/web/src/components/sidebar-right/inspector/solid-picker.tsxindex 680ffccb..38efc714 100644--- a/apps/web/src/components/sidebar-right/inspector/solid-picker.tsx+++ b/apps/web/src/components/sidebar-right/inspector/solid-picker.tsx@@ -3,28 +3,35 @@* file, You can obtain one at http://mozilla.org/MPL/2.0/. */import { ColorOpacityPicker } from "@/components/ui/color-opacity-picker";-import { useEntityState, setComponent } from "@/components/engine";-import { useEngine } from "@/context/engine";+import { useWorld } from "@diffusionstudio/koota-solid";+import { Computed, colorToHex } from "@diffusionstudio/runtime";+import { useDerived, useEditor } from "@/engine/hooks";+import { syncKeyframe } from "@/engine/keyframes";+import type { Entity } from "koota";-export type SolidColorPickerProps = {- nodeEid: number;- fillEid: number;+export type SolidFillPickerProps = {+ fill: Entity;};-export function SolidFillPicker(props: SolidColorPickerProps) {- const { world } = useEngine();- const c = world.components;+/** A `<solidPaint>`'s color and opacity. `color` is required, never unset. */+export function SolidFillPicker(props: SolidFillPickerProps) {+ const world = useWorld();+ const editor = useEditor();- const color = useEntityState(c.Computed.color, props.fillEid, 0xE0E0E0);- const opacity = useEntityState(c.Computed.opacity, props.fillEid, 1);+ const color = useDerived(() => props.fill.get(Computed)?.color ?? 0xE0E0E0);+ const opacity = useDerived(() => props.fill.get(Computed)?.opacity ?? 1);- const updateColor = (color: number) => {- setComponent(world, props.fillEid, c.Color, color);+ const updateColor = (next: number) => {+ const hex = colorToHex(next);+ editor.editProperty(props.fill, "color", hex);+ syncKeyframe(world, editor, props.fill, "color", hex);};- const updateOpacity = (opacity: number) => {- setComponent(world, props.fillEid, c.Appearance, { opacity });+ const updateOpacity = (next: number) => {+ const value = Math.round(next * 100) / 100;+ editor.editProperty(props.fill, "opacity", value === 1 ? false : value);+ syncKeyframe(world, editor, props.fill, "opacity", value);};return (@@ -33,8 +40,7 @@ export function SolidFillPicker(props: SolidColorPickerProps) {opacity={opacity()}onColorChange={updateColor}onOpacityChange={updateOpacity}- onBeginChange={(label) => world.history.startTransaction(label)}- onEndChange={() => world.history.commitTransaction()}+ keyframeTarget={props.fill}/>);}diff --git a/apps/web/src/components/ui/fill-item.tsx b/apps/web/src/components/ui/fill-item.tsxindex 47136e7b..5925c20e 100644--- a/apps/web/src/components/ui/fill-item.tsx+++ b/apps/web/src/components/ui/fill-item.tsx@@ -5,25 +5,35 @@import { createEffect, createMemo, createSignal, onCleanup, Show } from "solid-js";import { AssetThumbnail } from "@/components/ui/asset-thumbnail";import { Icon } from "@/components/ui/icon";-import { Keyframe } from "@/components/ui/keyframe-bitecs";+import { Keyframe } from "@/components/ui/keyframe";import { OpacitySwatch } from "@/components/ui/opacity-swatch";-import { useEngine } from "@/context/engine";+import { useTrait, useWorld } from "@diffusionstudio/koota-solid";+import { AssetId, Computed, Paint, PaintType, colorToHex, parseColor } from "@diffusionstudio/runtime";+import { useDerived, useEditor } from "@/engine/hooks";+import { useLibrary } from "@/engine/library";+import { syncKeyframe } from "@/engine/keyframes";+import { readGradientStops, sameGradientStops, type GradientStop } from "@/components/sidebar-right/inspector/gradient-stops";+import { assetName } from "@diffusionstudio/assets";import { mergeColorWithOpacity } from "@/utils";-import { colorToHex, parseColor } from "@/utils/color";-import { ChildOf, PaintType, useEntityState, useEntityStates, useQuery, setComponent } from "../engine";-import { Not } from 'bitecs';++import type { Entity } from "koota";const DRAG_THRESHOLD_PX = 3;type FillItemProps = {- nodeEid: number;- fillEid: number;+ fill: Entity;onClick?(event: MouseEvent): void;};+/**+ * One paint, as the fills panel shows it: a swatch of what it paints, its+ * color (a solid) or its name (everything else), and an opacity that doubles+ * as a slider across the row. Both values are props of the paint element, so+ * they are written through the editor and keyframed after.+ */export function FillItem(props: FillItemProps) {- const { world } = useEngine();- const c = world.components;+ const world = useWorld();+ const editor = useEditor();const [isDraggingSlider, setIsDraggingSlider] = createSignal(false);@@ -40,21 +50,23 @@ export function FillItem(props: FillItemProps) {let cancelColorCommit = false;let cancelOpacityCommit = false;- const color = useEntityState(c.Computed.color, props.fillEid, 0xE0E0E0);- const opacity = useEntityState(c.Computed.opacity, props.fillEid, 1);+ const color = useDerived(() => props.fill.get(Computed)?.color ?? 0xE0E0E0);+ const opacity = useDerived(() => props.fill.get(Computed)?.opacity ?? 1);- const updateColor = (color: number) => {- setComponent(world, props.fillEid, c.Color, color);+ const updateColor = (next: number) => {+ const hex = colorToHex(next);+ editor.editProperty(props.fill, "color", hex);+ syncKeyframe(world, editor, props.fill, "color", hex);};- const updateOpacity = (opacity: number) => {- setComponent(world, props.fillEid, c.Appearance, {- opacity: Math.round(opacity * 100) / 100,- });+ const updateOpacity = (next: number) => {+ const value = Math.round(next * 100) / 100;+ editor.editProperty(props.fill, "opacity", value === 1 ? false : value);+ syncKeyframe(world, editor, props.fill, "opacity", value);};- const fillType = useEntityState(c.Paint, props.fillEid, PaintType.SOLID);- const isSolidFill = createMemo(() => fillType() === PaintType.SOLID);+ const paint = useTrait(() => props.fill, Paint);+ const isSolidFill = createMemo(() => (paint()?.value ?? PaintType.SOLID) === PaintType.SOLID);const colorText = createMemo(() => colorToHex(color()).replace("#", ""));@@ -130,7 +142,6 @@ export function FillItem(props: FillItemProps) {if (active instanceof HTMLElement && rootRef?.contains(active)) {active.blur();}- world.history.startTransaction("Edit paint opacity");}if (dragStarted) {@@ -147,7 +158,6 @@ export function FillItem(props: FillItemProps) {removeWindowListeners();if (wasDragging) {- world.history.commitTransaction();setIsDraggingSlider(false);resetPointerState();pointerStartTarget = null;@@ -202,7 +212,6 @@ export function FillItem(props: FillItemProps) {};onCleanup(() => {- if (dragStarted) world.history.commitTransaction();removeWindowListeners();setIsDraggingSlider(false);resetPointerState();@@ -310,7 +319,7 @@ export function FillItem(props: FillItemProps) {onFocusIn={(e) => e.stopPropagation()}onClick={props.onClick}>- <PaintItemIcon fill={props.fillEid} />+ <PaintItemIcon fill={props.fill} /></button><Show when={isSolidFill()}>@@ -327,7 +336,7 @@ export function FillItem(props: FillItemProps) {</Show><Show when={!isSolidFill()}>- <FillLabel fill={props.fillEid} />+ <FillLabel fill={props.fill} /></Show></div>@@ -348,7 +357,7 @@ export function FillItem(props: FillItemProps) {class="z-20 min-w-2"onFocusIn={(e) => e.stopPropagation()}>- <Keyframe property="opacity" target={props.fillEid} />+ <Keyframe property="opacity" target={props.fill} /></div></div></div>@@ -357,34 +366,21 @@ export function FillItem(props: FillItemProps) {}type PaintItemIconProps = {- fill: number;+ fill: Entity;};function PaintItemIcon(props: PaintItemIconProps) {- const { world } = useEngine();- const c = world.components;- const assets = world.assets;-- const assetId = useEntityState(c.AssetId, props.fill);- const stopEids = useQuery([c.ColorStop, ChildOf(props.fill), Not(c.Deleted)]);- const color = useEntityState(c.Computed.color, props.fill);- const opacity = useEntityState(c.Computed.opacity, props.fill, 1);- const type = useEntityState(c.Paint, props.fill, PaintType.SOLID);-- const offsets = useEntityStates(c.Computed.stopOffset, stopEids, 0);- const colors = useEntityStates(c.Computed.stopColor, stopEids, 0xFFFFFF);- const opacities = useEntityStates(c.Computed.stopOpacity, stopEids, 1);-- const sortedStops = createMemo(() => stopEids()- .map((_, idx) => ({- offset: offsets()[idx],- color: colors()[idx],- opacity: opacities()[idx],- }))- .toSorted((a, b) => a.offset - b.offset)- );+ const world = useWorld();+ const library = useLibrary();- const asset = createMemo(() => assets.get(assetId() ?? ""));+ const assetId = useTrait(() => props.fill, AssetId);+ const paint = useTrait(() => props.fill, Paint);+ const color = useDerived(() => props.fill.get(Computed)?.color ?? 0xE0E0E0);+ const opacity = useDerived(() => props.fill.get(Computed)?.opacity ?? 1);+ const stops = useDerived(() => readGradientStops(world, props.fill), sameGradientStops);++ const type = () => paint()?.value ?? PaintType.SOLID;+ const asset = createMemo(() => library()?.get(assetId()?.value ?? ""));return (<>@@ -393,18 +389,19 @@ function PaintItemIcon(props: PaintItemIconProps) {asset={asset()!}class="size-full"size={{ width: 50, height: 50 }}+ cache={library()?.cache}/></Show><Show when={type() === PaintType.LINEAR_GRADIENT || type() === PaintType.RADIAL_GRADIENT}><divclass="size-full"- style={{ "background-image": getGradientBackground(sortedStops(), type()) }}+ style={{ "background-image": getGradientBackground(stops(), type()) }}/></Show>- <Show when={color() !== undefined && type() === PaintType.SOLID}>- <OpacitySwatch color={color()!} opacity={opacity()} />+ <Show when={type() === PaintType.SOLID}>+ <OpacitySwatch color={color()} opacity={opacity()} /></Show><Show when={type() === PaintType.HTML || type() === PaintType.SURFACE || type() === PaintType.SHADER}>@@ -416,17 +413,18 @@ function PaintItemIcon(props: PaintItemIconProps) {);}+type FillLabelProps = {- fill: number;+ fill: Entity;};function FillLabel(props: FillLabelProps) {- const { world } = useEngine();- const c = world.components;- const assets = world.assets;+ const library = useLibrary();- const type = useEntityState(c.Paint, props.fill, PaintType.SOLID);- const assetId = useEntityState(c.AssetId, props.fill);+ const paint = useTrait(() => props.fill, Paint);+ const assetId = useTrait(() => props.fill, AssetId);++ const type = () => paint()?.value ?? PaintType.SOLID;const label = createMemo(() => {if (type() === PaintType.SOLID) {@@ -442,8 +440,8 @@ function FillLabel(props: FillLabelProps) {} else if (type() === PaintType.SHADER) {return "Shader";} else {- const asset = assets.get(assetId() ?? "");- return asset?.name ?? "Unknown";+ const asset = library()?.get(assetId()?.value ?? "");+ return asset ? assetName(asset) : "Unknown";}})@@ -454,16 +452,10 @@ function FillLabel(props: FillLabelProps) {)}-type GradientStop = {- offset: number;- color: number;- opacity: number;-};-function getGradientBackground(stops: GradientStop[], style: PaintType) {const parts: string[] = [];for (let i = 0; i < stops.length; i++) {- const color = mergeColorWithOpacity(stops[i].color, stops[i].opacity);+ const color = mergeColorWithOpacity(stops[i]!.color, stops[i]!.opacity);const offset = (i / (stops.length - 1)) * 100;parts.push(`${color} ${offset}%`);}diff --git a/packages/jsx/src/index.ts b/packages/jsx/src/index.tsindex b0190e0e..2d86852b 100644--- a/packages/jsx/src/index.ts+++ b/packages/jsx/src/index.ts@@ -57,6 +57,7 @@ export type {ImageProps,KeyframeProps,KeyframeTrackProps,+ MediaPaintProps,RectProps,SceneProps,SequenceProps,diff --git a/packages/jsx/src/jsx-runtime.ts b/packages/jsx/src/jsx-runtime.tsindex 864ef984..69686c24 100644--- a/packages/jsx/src/jsx-runtime.ts+++ b/packages/jsx/src/jsx-runtime.ts@@ -27,6 +27,7 @@ import type {ColorStopProps,EffectProps,GradientPaintProps,+ MediaPaintProps,GroupProps,HtmlPaintProps,HtmlProps,@@ -89,6 +90,8 @@ export declare namespace JSX {solidPaint: SolidPaintProps & SourceProps;linearGradientPaint: GradientPaintProps & SourceProps;radialGradientPaint: GradientPaintProps & SourceProps;+ imagePaint: MediaPaintProps & SourceProps;+ videoPaint: MediaPaintProps & SourceProps;colorStop: ColorStopProps & SourceProps;stroke: StrokeProps & SourceProps;shadow: ShadowProps & SourceProps;diff --git a/packages/jsx/src/source.ts b/packages/jsx/src/source.tsindex 3cec9346..90cd5cfb 100644--- a/packages/jsx/src/source.ts+++ b/packages/jsx/src/source.ts@@ -103,6 +103,8 @@ export const COMPOSITION_TAGS = ["solidPaint","linearGradientPaint","radialGradientPaint",+ "imagePaint",+ "videoPaint","colorStop","stroke","shadow",diff --git a/packages/jsx/src/types.ts b/packages/jsx/src/types.tsindex d77d8752..67ecccd8 100644--- a/packages/jsx/src/types.ts+++ b/packages/jsx/src/types.ts@@ -513,6 +513,17 @@ export type ColorStopProps = ColorProps & OpacityProps & TrackChildren & {offset: number;};+/**+ * `<imagePaint>` / `<videoPaint>` — an asset painted into the parent+ * geometry's box, a paint child like a solid or a gradient (several stack in+ * document order). The node tags `<image>` / `<video>` are the same media as+ * an element of its own; these fill something else with it, so a rect or a+ * text can be filled with a picture. Which tag it is says what the source is+ * played as, exactly as on the node tags: a frames directory plays as a+ * sequence under either.+ */+export type MediaPaintProps = PaintProps & MediaProps & FitProps & TrackChildren;+export type VideoProps = CommonProps & MediaProps & FitProps & AudioTrackProps & {/** Paint children, stacked over the media paint created by `src`; `<Stroke>`, `<Shadow>`, `<Effect>`, `<Animation>` and `<KeyframeTrack>` children. */children?: SolidJSX.Element;diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.tsindex f343e4d6..a44b2971 100644--- a/packages/reconciler/src/document.ts+++ b/packages/reconciler/src/document.ts@@ -161,6 +161,8 @@ const PAINT_TYPES: Record<string, PaintType> = {radialGradientPaint: PaintType.RADIAL_GRADIENT,shaderPaint: PaintType.SHADER,surfacePaint: PaintType.SURFACE,+ imagePaint: PaintType.IMAGE,+ videoPaint: PaintType.VIDEO,};export const TRANSITION_TYPES: Record<string, TransitionType> = {@@ -483,7 +485,9 @@ export class RuntimeDocument implements ProjectDocument<HostNode> {case 'linearGradientPaint':case 'radialGradientPaint':case 'shaderPaint':- case 'surfacePaint': {+ case 'surfacePaint':+ case 'imagePaint':+ case 'videoPaint': {entity = createEntity(this.world);entity.add(Paint);entity.set(Paint, { value: PAINT_TYPES[name]! });@@ -554,7 +558,7 @@ export class RuntimeDocument implements ProjectDocument<HostNode> {}default:throw new Error(- `<${tag}> is not supported yet (only <stage>, <scene>, <group>, <sequence>, <rect>, <text>, <textRange>, <video>, <image>, <audio>, <surface>, <solidPaint>, <linearGradientPaint>, <radialGradientPaint>, <shaderPaint>, <surfacePaint>, <colorStop>, <stroke>, <shadow>, <effect>, <animation>, <keyframeTrack> and <keyframe>).`,+ `<${tag}> is not supported yet (only <stage>, <scene>, <group>, <sequence>, <rect>, <text>, <textRange>, <video>, <image>, <audio>, <surface>, <solidPaint>, <linearGradientPaint>, <radialGradientPaint>, <shaderPaint>, <surfacePaint>, <imagePaint>, <videoPaint>, <colorStop>, <stroke>, <shadow>, <effect>, <animation>, <keyframeTrack> and <keyframe>).`,);}diff --git a/packages/reconciler/src/elements.ts b/packages/reconciler/src/elements.tsindex 34f56280..76fdb1d4 100644--- a/packages/reconciler/src/elements.ts+++ b/packages/reconciler/src/elements.ts@@ -31,6 +31,7 @@ import type {ImageProps,KeyframeProps,KeyframeTrackProps,+ MediaPaintProps,RectProps,SceneProps,SequenceProps,@@ -95,6 +96,8 @@ export const Captions = hostElement<CaptionsProps>("Captions");export const SolidPaint = hostElement<SolidPaintProps>("SolidPaint");export const LinearGradientPaint = hostElement<GradientPaintProps>("LinearGradientPaint");export const RadialGradientPaint = hostElement<GradientPaintProps>("RadialGradientPaint");+export const ImagePaint = hostElement<MediaPaintProps>("ImagePaint");+export const VideoPaint = hostElement<MediaPaintProps>("VideoPaint");export const ColorStop = hostElement<ColorStopProps>("ColorStop");export const Stroke = hostElement<StrokeProps>("Stroke");export const Shadow = hostElement<ShadowProps>("Shadow");diff --git a/packages/runtime/MIGRATION.md b/packages/runtime/MIGRATION.mdindex 33ae129f..0c7faf61 100644--- a/packages/runtime/MIGRATION.md+++ b/packages/runtime/MIGRATION.md@@ -76,7 +76,7 @@ Done:| Draw tools + toolbar (armed tool is the `Tool` world trait) | `components/canvas/draw-overlay.tsx` || Input, HUD, camera, alignment | `engine/input/`, `engine/hud/`, `engine/align.ts` || Project config (per-scene export settings in package.json) | `engine/project-config.ts` |-| Panels: header, background, scene template, asset info, time, appearance, alignment, export, layout, text, strokes, shadows, effects, animations, transition, masks, audio | `components/sidebar-right/inspector/` |+| Panels: header, background, scene template, asset info, time, appearance, alignment, export, layout, text, strokes, shadows, effects, animations, transition, masks, audio, fills | `components/sidebar-right/inspector/` || Local font families (web fonts and loading are the runtime's) | `engine/fonts.ts` (`getLocalFonts`) |Hosted JSX surface is `COMPOSITION_TAGS` (`packages/jsx/src/source.ts`): the structural tags, the media tags, `<captions>`, the paint family, `<stroke>`, `<shadow>`, `<effect>`, `<animation>`, `<keyframeTrack>`/`<keyframe>`, `<html>`/`<surface>`. `mask` is a `<rect>` prop, not a tag.@@ -95,7 +95,7 @@ Conventions worth repeating when migrating the next panel:- Reordering siblings is a swap through `editor.reparent`, not an index write: `ItemIndex` is the document's to assign (`insertNode` renumbers the whole sibling list), and a move needs an anchor, since `reparent` appends without one and refuses an append into the parent the element already has. So "move later" moves the *next* sibling in front of this one.- A shared control that knows *which* prop it edits keyframes it itself, off an `Entity` (`ColorOpacityPicker`'s `keyframeTarget`, now koota's); only a control that cannot know takes the diamond as a slot (`ControlledTextField`, `ColorOpacityRow`). Moving one of these moves it whole: the bitecs panels still calling it lose their diamonds until they migrate, and its recent-colors palette now queries the koota world for them too (both providers are mounted).-Still on bitecs: transform, caption-settings, fills, interpolation panels; the timeline's keyframe layer; the whole timeline UI.+Still on bitecs: transform, caption-settings, interpolation panels; the timeline's keyframe layer; the whole timeline UI.The stroke panel moved with the model: `StrokeStyle` is the stroke's, not the node's (as `<stroke width join cap miterLimit>` always was), so Weight/Join/Miter sit under each stroke's row instead of once per node. `<stroke>` is a solid paint that takes no paint children, so its picker is the color one alone, with no gradient or asset tab and no `FillPicker` behind it. `cap` still has no control (it shows only on open paths, and there are no icons for it).@@ -111,6 +111,12 @@ The audio panel kept its knob, meter and buttons; what moved is where they writeFixed while the panel moved: the `gain` animation used to write a 0-1 linear ramp into `Computed.volume`, which everything else reads as decibels (`audio-bus.ts` does `10 ** (db / 20)`), so a Volume animation ramped 0 dB to 1 dB, inaudibly, instead of silence to unity. It now ramps the *amplitude* and adds it to the volume as decibels (`amplitudeToDecibels`, -Infinity at silence, which the bus already reads as a gain of zero). Two consequences worth knowing: it reads `Computed.volume` instead of replacing it, unlike the visual presets, so a clip set to -6 dB fades to -6 dB rather than up to unity and an overlapping in and out compose; and a fade sounds even because it is even in amplitude, not in decibels.+The fills panel needed two tags that did not exist. The runtime renders an image or a video **fill** (`renderFills` handles the media paints), but the paint family had no word for one: media was only ever a `<video>`/`<image>` node. So `<imagePaint>` and `<videoPaint>` were added to `COMPOSITION_TAGS`, the intrinsics, `elements.ts` and `PAINT_TYPES`; they mirror the node tags exactly (`src`, `objectFit`, and `bindAsset`'s rule that a frames directory plays as a sequence), which is why they are two tags rather than one `<mediaPaint>` taking its kind from the asset.++What that panel now says that the old one did not: **a fill's kind is its tag**, so changing it is not a property write but a swap. `FillPicker.replaceFill` inserts the new element in front of the next fill (keeping its place in the paint stack) and removes the old one, then hands the caller the entity it ended up with (`onReplace`) — the panel's `picked` signal has to move with it, and the old element's keyframe tracks go with it, which is honest: they drove a paint that no longer exists. The same swap is what linear ↔ radial does, writing the stops and the rotation out onto the new element. Picking another asset for a fill that already is one of the same kind is a plain `src` write, so the fit and the tracks survive.++Two details worth keeping: gradient stops have no `Cache` list of their own (`readGradientStops` queries `ColorStop, ChildOf(fill)` and sorts by offset, since where a stop sits is its `offset` and the file's order says nothing), and the fill picker's asset tab reuses the sidebar's `LazyAssetItem`, so renaming, replacing and deleting an asset work the same from both places. `ScaleModeType.NONE` is not offered: `Fit` has no word for it. The stub "Preview" and "Replace media" toasts of the old asset picker were dropped rather than carried over.+The shadow panel kept the shape it had: a row per `<shadow>` opening a floating inspector, since nothing in the shadow model moved the way the stroke's line style did. The one thing it says that the file does not is what "Add shadow" authors (`opacity` 0.25, `blur` 4, `offsetY` 4): `<shadow>`'s own defaults are all zero, which casts the silhouette back onto itself, so a shadow added from the panel has to spell out where it sits. Every control unsets at the prop's default (0 for the offsets and the blur, 1 for opacity); `color` is required and always written.Two deliberate departures from the old text panel: `letterSpacing` is shown in px (the trait's unit and the prop's; the bitecs panel showed the same number as a percentage), and the font a text is set in — family, weight, size — is written out plainly rather than unset at its default, since a font is what a text *is* and not a modifier of it. `<captions>` has no host yet, so the caption half of the panel is unreachable and was carried over as it stood.