Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/lib/modules/editor/components/AnimationPanel.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<script lang="ts">
// The Animation tab. An animation here is an effect on an image layer, as in a slide deck's
// animation pane: the list names the layers that carry one, the effect section edits the
// selected one, and the timeline plays or scrubs it on its own clock, apart from the simulator's.
import { PanelSection } from "$lib/shared/components/panel-section";
import { Slider } from "$lib/shared/components/slider";
import { Button } from "$lib/shared/components/button";
import { Icon } from "$lib/shared/components/icon";
import { animatedIn, animOf } from "../core/document/proc";
import { editorModel } from "../model";
import { t, sourceName } from "../i18n-inspector";
import Animation from "./props/animation.svelte";

const {
$doc: doc,
$screen: screen,
$selected: selected,
$sim: sim,
select,
simPatched,
} = editorModel;

const layers = $derived(animatedIn($doc?.screens.find((s) => s.kind === $screen)?.layers ?? []));
// the selected layer if it is one of them, else the first
const current = $derived(layers.find((l) => l.id === $selected[0]?.id) ?? layers[0] ?? null);
const anim = $derived(current ? animOf(current) : null);
const scrub = $derived($sim.scrub);

const play = () => simPatched({ wakeAt: Date.now(), scrub: null });
const follow = () => simPatched({ scrub: null });
const at = (ms: number) => simPatched({ scrub: { ms, value: scrub?.value ?? null } });
const valueAt = (value: number) =>
simPatched({ scrub: { ms: scrub?.ms ?? anim?.duration ?? 0, value } });
</script>

<div class="panel type-small" data-dense>
<PanelSection title={$t.animLayers}>
{#if layers.length}
<div class="list">
{#each layers as l (l.id)}
<button
type="button"
class="item type-small"
class:on={l.id === current?.id}
onclick={() => select(l.id)}
>
<Icon name="wb_twilight" size={18} />
<span
>{l.name || (l.kind === "image" ? sourceName($t, l.meta.source) : $t.animation)}</span
>
</button>
{/each}
</div>
{:else}
<p class="type-caption hint-xs">{$t.animNone}</p>
{/if}
</PanelSection>

{#if current && anim}
<PanelSection title={$t.animTimeline}>
<div class="row">
<Button size="small" kind="secondary" onClick={play}>
<Icon name="play_arrow" size={18} />
{$t.animPlay}
</Button>
<Button size="small" kind="ghost" disabled={!scrub} onClick={follow}>
<Icon name="sync" size={18} />
{$t.animFollow}
</Button>
</div>
<span class="type-label muted-label">{$t.animAt}</span>
<Slider
min={0}
max={Math.max(anim.duration, 1)}
step={10}
value={scrub?.ms ?? anim.duration}
onInput={at}
/>
<span class="type-label muted-label">{$t.animValue}</span>
<Slider min={0} max={100} step={1} value={scrub?.value ?? 0} onInput={valueAt} />
</PanelSection>
<PanelSection title={$t.animSettings}>
<Animation layer={current} />
</PanelSection>
{/if}
</div>

<style>
.panel {
--control-height: 1.875rem;

display: flex;
flex-direction: column;
}
.row {
display: flex;
gap: 0.5rem;
}
.list {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.item {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.375rem 0.5rem;
border: 0;
border-radius: var(--border-radius);
background: oklch(from var(--color-text) l c h / 7%);
color: var(--color-text);
text-align: start;
cursor: pointer;

&.on {
outline: 2px solid var(--color-text);
}
}
</style>
6 changes: 6 additions & 0 deletions src/lib/modules/editor/components/SimPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Select } from "$lib/shared/components/select";
import { Switch } from "$lib/shared/components/switch";
import { Icon } from "$lib/shared/components/icon";
import { Button } from "$lib/shared/components/button";
import {
ACCENT_PALETTE,
ID_METRIC,
Expand Down Expand Up @@ -106,6 +107,11 @@
>{$t.slotPlaceholders}</button
>
</div>
<!-- replays the wake sweep of an animated image (0x82, type 2) -->
<Button size="small" kind="secondary" onClick={() => simPatched({ wakeAt: Date.now() })}>
<Icon name="wb_twilight" size={18} />
{$t.wakeScreen}
</Button>
{#if $ids.some(({ id }) => id === 0x99)}
<div>
<span class="type-label muted-label">{$t.theme}</span>
Expand Down
6 changes: 6 additions & 0 deletions src/lib/modules/editor/components/TreePanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
widgetAdded,
numberAdded,
imageAdded,
animatedAdded,
deleteRequested,
duplicateRequested,
copyRequested,
Expand Down Expand Up @@ -295,6 +296,11 @@
<Icon name="image" size={22} />
{$t.emptyImage}
</MenuItem>
<!-- a Watch Pro 2 animated image, from Sun Circle's proven bytes; art comes later -->
<MenuItem onClick={() => animatedAdded()}>
<Icon name="wb_twilight" size={22} />
{$t.animatedLayer}
</MenuItem>
<!-- ten blank frames: the art comes from the inspector, either "regenerate from a font"
or a PNG dropped on a single frame — no ten-file picker any more -->
<MenuItem onClick={() => numberAdded()}>
Expand Down
86 changes: 86 additions & 0 deletions src/lib/modules/editor/components/props/animation.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<script lang="ts">
// The Watch Pro 2's animated image (0x82, type 2). Duration and the two angles are what the
// firmware's parser is known to read as such; the other fields it reads are shown raw, by
// offset, until tests on a watch say what they do.
import { Input } from "$lib/shared/components/input";
import { animOf, withAnimBody, type Anim } from "../../core/document/proc";
import type { Layer } from "../../core/document/doc";
import { t } from "../../i18n-inspector";
import { num, set } from "./patch";

const { layer }: { layer: Layer } = $props();
const anim = $derived(animOf(layer));

const put = (patch: Partial<Anim>) => {
const extra = withAnimBody(layer, patch);

if (extra) set(layer.id, { extra } as Partial<Layer>);
};
// tenths of a degree on file, degrees on screen
const deg = (v: number) => String(v / 10);
const tenths = (s: string) => Math.round(num(s) * 10);

// the s16 fields wrap to what a body byte pair can hold
const s16 = (s: string) => ((num(s) & 0xffff) << 16) >> 16;
const u16 = (s: string) => num(s) & 0xffff;

const RAW: { key: keyof Anim; off: number; signed: boolean }[] = [
{ key: "raw4", off: 4, signed: true },
{ key: "raw6", off: 6, signed: true },
{ key: "raw8", off: 8, signed: true },
{ key: "raw10", off: 10, signed: true },
{ key: "raw16", off: 16, signed: false },
{ key: "raw18", off: 18, signed: false },
];
</script>

{#if anim}
<p class="type-caption hint-xs">{$t.animHint}</p>
<div class="row">
<Input
label={$t.animDuration}
type="number"
min={0}
max={65535}
value={String(anim.duration)}
onChange={(v) => put({ duration: u16(v) })}
/>
</div>
<div class="row">
<Input
label={$t.animStart}
type="number"
min={-360}
max={360}
value={deg(anim.start)}
onChange={(v) => put({ start: Math.max(-3600, Math.min(3600, tenths(v))) })}
/>
<Input
label={$t.animEnd}
type="number"
min={-360}
max={360}
value={deg(anim.end)}
onChange={(v) => put({ end: Math.max(-3600, Math.min(3600, tenths(v))) })}
/>
</div>
<span class="type-label muted-label">{$t.animRaw}</span>
<div class="raw">
{#each RAW as f (f.key)}
<Input
label={`+${f.off}`}
type="number"
value={String(anim[f.key])}
onChange={(v) => put({ [f.key]: f.signed ? s16(v) : u16(v) })}
/>
{/each}
</div>
{/if}

<style>
.raw {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.375rem;
}
</style>
14 changes: 13 additions & 1 deletion src/lib/modules/editor/core/document/edits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// here is keyed by NodeId, which is what makes the layer identity survive an immutable update:
// the old code compared node objects, so any rebuilt parent silently invalidated `sel`.
import { hex, TAG, unhex } from "../format";
import { PANELS, procOf, RINGS, type Proc } from "./proc";
import { PANELS, procOf, RINGS, withAnimBody, animOf, type Anim, type Proc } from "./proc";
import {
framesOf,
isPlaced,
Expand Down Expand Up @@ -387,3 +387,15 @@ export function usedAssets(doc: Doc) {
doc.screens.forEach((s) => walk(s.layers));
return out;
}

/** Change fields of a type 2 (animated) layer's 0x5c body; the rest of the body stays as found. */
export function withAnim(doc: Doc, id: NodeId, patch: Partial<Anim>): Doc {
const l = findLayer(doc, id);

if (!l || !animOf(l)) return doc;
const extra = withAnimBody(l, patch);

return !extra || extra === (l as { extra?: unknown }).extra
? doc
: patchLayer(doc, id, { extra } as Partial<Layer>);
}
16 changes: 16 additions & 0 deletions src/lib/modules/editor/core/document/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
type WidgetMeta,
} from "./doc";
import { TAG, hex, unhex, type Resource } from "../format";
import { ANIM_SEED } from "./proc";

const META0: WidgetMeta = {
w: 0,
Expand Down Expand Up @@ -51,6 +52,21 @@ export const newImage = (frames: readonly ImageId[]): ImageLayer => ({
frames,
});

/** The animated image (0x82, type 2), seeded with Sun Circle's exact 0x5c body: a full-screen
* 466x466 layer that sweeps in on wake and stops where daylight % puts it. Starting from bytes a
* Watch Pro 2 is known to play means a new layer is as safe as the stock one; edit from there. */
export const newAnimated = (frames: readonly ImageId[]): ImageLayer => ({
...base(),
tag: 0x82,
kind: "image",
x: 0,
y: 0,
meta: metaWith({ w: 466, h: 466, source: 0x82, max: 0x3c }),
refType: 1,
frames,
extra: [{ tag: TAG.proc, hex: ANIM_SEED }],
});

/** One frame per battery percent, 0..100: the image widget draws frames[value % count]. */
export const PROGRESS_STEPS = 101;

Expand Down
Loading