rig-review 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ import type {JobRigPresentation} from './viewport-capture.ts';
4
4
 
5
5
  type DrawSource = Pick<JobRigCapture['binding'], 'publicationId' | 'instanceKey' | 'sourceHash' | 'pageId'>;
6
6
  export interface JobRigDrawState {
7
- player: Pick<RigPlayer, 'generation' | 'current' | 'time' | 'slots'> & {readonly renderer: object};
7
+ player: Pick<RigPlayer, 'generation' | 'current' | 'time' | 'slots' | 'entry'> & {readonly renderer: object};
8
8
  frameGeneration: number | undefined;
9
9
  presentation: JobRigPresentation | undefined;
10
10
  source: DrawSource;
@@ -21,6 +21,8 @@ export interface JobRigDraw {
21
21
  readonly drawGeneration: number;
22
22
  readonly animation: string | null;
23
23
  readonly time: number;
24
+ readonly trackTime: number;
25
+ readonly loop: boolean;
24
26
  readonly playing: boolean;
25
27
  readonly source: DrawSource;
26
28
  readonly layout: string;
@@ -55,20 +57,63 @@ export function jobRigPixelPlacement(pane: Pick<DOMRect, 'left' | 'top'>,
55
57
  };
56
58
  }
57
59
 
60
+ function readPlayerTiming(state: JobRigDrawState): {animation: string | null; time: number; trackTime: number; loop: boolean} {
61
+ if (!Number.isFinite(state.player.time) || state.player.time < 0 || typeof state.playing !== 'boolean' ||
62
+ typeof state.player.entry !== 'function') {
63
+ throw new Error('JOB_STILL_CAPTURE_FAILED');
64
+ }
65
+ const entry = state.player.entry(0);
66
+ if (state.player.current === null) {
67
+ if (state.playing || state.player.time !== 0 ||
68
+ (entry !== null && (entry.animation !== null || entry.trackTime !== 0 || entry.animationTime !== 0))) {
69
+ throw new Error('JOB_STILL_CAPTURE_FAILED');
70
+ }
71
+ return {animation: null, time: 0, trackTime: 0, loop: false};
72
+ }
73
+ if (typeof state.player.current !== 'string' || !state.player.current || !entry ||
74
+ typeof entry.animation !== 'string' || entry.animation !== state.player.current ||
75
+ typeof entry.loop !== 'boolean' || !Number.isFinite(entry.trackTime) || entry.trackTime < 0 ||
76
+ entry.trackTime !== state.player.time || !Number.isFinite(entry.duration) || entry.duration < 0 ||
77
+ !Number.isFinite(entry.animationTime) || entry.animationTime < 0 || entry.animationTime > entry.duration) {
78
+ throw new Error('JOB_STILL_CAPTURE_FAILED');
79
+ }
80
+ return {animation: entry.animation, time: entry.animationTime, trackTime: entry.trackTime, loop: entry.loop};
81
+ }
82
+
83
+ function matchesDrawState(draw: JobRigDraw, state: JobRigDrawState): boolean {
84
+ if (draw.presentation !== state.presentation || draw.player !== state.player || draw.renderer !== state.player.renderer ||
85
+ draw.playerGeneration !== state.player.generation || state.frameGeneration !== draw.playerGeneration ||
86
+ draw.animation !== state.player.current || draw.trackTime !== state.player.time ||
87
+ draw.playing !== state.playing || draw.layout !== state.layout || sourceKey(draw.source) !== sourceKey(state.source)) {
88
+ return false;
89
+ }
90
+ const entry = state.player.entry(0);
91
+ if (draw.animation === null) {
92
+ return !draw.playing && !state.playing && draw.time === 0 && draw.trackTime === 0 && draw.loop === false && state.player.time === 0 &&
93
+ (entry === null || (entry.animation === null && entry.trackTime === 0 && entry.animationTime === 0));
94
+ }
95
+ return entry !== null && entry.animation === draw.animation && entry.loop === draw.loop &&
96
+ entry.trackTime === draw.trackTime && entry.animationTime === draw.time &&
97
+ Number.isFinite(entry.duration) && entry.duration >= 0 && draw.time >= 0 && draw.time <= entry.duration;
98
+ }
99
+
58
100
  /** Called after the public draw/present and viewport-render boundaries succeed. */
59
101
  export function recordJobRigDraw(state: JobRigDrawState, drawGeneration: number): JobRigDraw {
60
102
  if (!state.presentation || state.frameGeneration !== state.player.generation || !Number.isSafeInteger(drawGeneration) || drawGeneration < 1 ||
61
- !Number.isSafeInteger(state.player.generation) || state.player.generation < 0 || !Number.isFinite(state.player.time) || state.player.time < 0) {
103
+ !Number.isSafeInteger(state.player.generation) || state.player.generation < 0) {
62
104
  throw new Error('JOB_STILL_CAPTURE_FAILED');
63
105
  }
106
+ const timing = readPlayerTiming(state);
64
107
  return Object.freeze({
65
108
  player: state.player,
66
109
  renderer: state.player.renderer,
67
110
  presentation: state.presentation,
68
111
  playerGeneration: state.player.generation,
69
112
  drawGeneration,
70
- animation: state.player.current,
71
- time: state.player.time,
113
+ animation: timing.animation,
114
+ time: timing.time,
115
+ trackTime: timing.trackTime,
116
+ loop: timing.loop,
72
117
  playing: state.playing,
73
118
  source: Object.freeze({...state.source}),
74
119
  layout: state.layout,
@@ -80,16 +125,13 @@ export function freezeJobRigCapture(draw: JobRigDraw | undefined, state: JobRigD
80
125
  copy: () => JobRigPixels): FrozenJobRigCapture {
81
126
  if (!state.defaultCamera) throw new Error('JOB_STILL_CAMERA_NOT_FIXED');
82
127
  if (!state.wholeRig || state.player.slots !== null) throw new Error('JOB_STILL_VIEW_UNSUPPORTED');
83
- if (!draw) throw new Error('JOB_STILL_CAPTURE_FAILED');
84
- if (draw.presentation !== state.presentation || draw.player !== state.player || draw.renderer !== state.player.renderer || draw.playerGeneration !== state.player.generation ||
85
- state.frameGeneration !== draw.playerGeneration || draw.animation !== state.player.current ||
86
- draw.time !== state.player.time || draw.layout !== state.layout || sourceKey(draw.source) !== sourceKey(state.source)) {
128
+ if (!draw || typeof state.player.entry !== 'function') throw new Error('JOB_STILL_CAPTURE_FAILED');
129
+ if (draw.animation !== null && !draw.animation) throw new Error('JOB_STILL_CAPTURE_FAILED');
130
+ if (!matchesDrawState(draw, state)) {
87
131
  throw new Error('JOB_STILL_BINDING_MISMATCH');
88
132
  }
89
- if (draw.animation !== null && !draw.animation) throw new Error('JOB_STILL_CAPTURE_FAILED');
90
133
  const pixels = copy();
91
- if (state.presentation !== draw.presentation || state.player.renderer !== draw.renderer || state.player.generation !== draw.playerGeneration || state.player.current !== draw.animation ||
92
- state.player.time !== draw.time || draw.layout !== state.layout || sourceKey(draw.source) !== sourceKey(state.source)) {
134
+ if (!matchesDrawState(draw, state)) {
93
135
  throw new Error('JOB_STILL_BINDING_MISMATCH');
94
136
  }
95
137
  if (!Number.isSafeInteger(pixels.width) || !Number.isSafeInteger(pixels.height) || pixels.width < 1 || pixels.height < 1 ||
@@ -6,6 +6,7 @@ import {t} from './i18n.ts';
6
6
  import {JOB_HUB_READY, jobHubMessageMatches, type JobHubBinding} from './job-hub.ts';
7
7
  import {recordJobRigDraw, freezeJobRigCapture, copyJobRigViewport, createJobRigSubmission, jobRigPixelPlacement,
8
8
  type JobRigDraw, type JobRigDrawState} from './job-rig-capture.ts';
9
+ import {advancePlayback} from './playback-clock.ts';
9
10
  import {presentJobRigViewport, type JobRigPresentation} from './viewport-capture.ts';
10
11
  import {CanvasRenderer} from '@rig-c/play';
11
12
 
@@ -987,6 +988,11 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
987
988
  let currentElement: HTMLElement | null = null;
988
989
  let animation = '';
989
990
  let time = 0;
991
+ let playing = false;
992
+ let loop = true;
993
+ let playbackCarry = 0;
994
+ let rafHandle = 0;
995
+ let lastFrameTime = 0;
990
996
  let selectedPart = '';
991
997
  let partMode: 'context' | 'solo' = 'context';
992
998
  let selectedReference = '';
@@ -1070,7 +1076,7 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1070
1076
  ]),
1071
1077
  defaultCamera: plain && camera.zoom === 1 && camera.center.x === frame.center.x && camera.center.y === frame.center.y,
1072
1078
  wholeRig: !selectedPart && !owner.inspection?.value() && player.slots === null,
1073
- playing: false};
1079
+ playing};
1074
1080
  }
1075
1081
  function rememberDraw(): void {
1076
1082
  const owner = current;
@@ -1103,6 +1109,95 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1103
1109
  owner.player?.draw();
1104
1110
  owner.inspection?.present('single');
1105
1111
  }
1112
+ function syncTransportControls(): void {
1113
+ const playBtn = workspace.querySelector<HTMLButtonElement>('#job-play');
1114
+ if (playBtn) {
1115
+ const clip = current?.assets?.animations.find(item => item.name === animation);
1116
+ playBtn.disabled = !animation || !clip || !(clip.duration > 0) || !current?.player;
1117
+ const key = playing ? 'Pause' : 'Play';
1118
+ playBtn.dataset.jobText = key;
1119
+ playBtn.textContent = t(key);
1120
+ playBtn.setAttribute('aria-pressed', String(playing));
1121
+ }
1122
+ const loopInput = workspace.querySelector<HTMLInputElement>('#job-loop');
1123
+ if (loopInput) loopInput.checked = loop;
1124
+ }
1125
+ function stopPlayback(targetPlayer = current?.player): void {
1126
+ if (rafHandle) {
1127
+ cancelAnimationFrame(rafHandle);
1128
+ rafHandle = 0;
1129
+ }
1130
+ playing = false;
1131
+ playbackCarry = 0;
1132
+ lastFrameTime = 0;
1133
+ targetPlayer?.pause();
1134
+ syncTransportControls();
1135
+ }
1136
+ function presentCurrentFrame(owner: JobPreparedInspection): void {
1137
+ if (!owner.player) return;
1138
+ drawInspection(owner);
1139
+ owner.inspection?.select(selectedPart ? { side: 'single', slot: selectedPart, mode: partMode } : null);
1140
+ setText('job-part-observation', selectedPart ? JSON.stringify(owner.inspection?.observation(), null, 2) : '');
1141
+ viewport.fitCamera();
1142
+ }
1143
+ function armPlayback(expectedOwner: JobPreparedInspection, expectedPlayer: RigPlayer): void {
1144
+ if (rafHandle) {
1145
+ cancelAnimationFrame(rafHandle);
1146
+ rafHandle = 0;
1147
+ }
1148
+ const handle = requestAnimationFrame(now => stepPlayback(handle, expectedOwner, expectedPlayer, now));
1149
+ rafHandle = handle;
1150
+ }
1151
+ function playbackTimeLabel(duration: number, trackTime: number): string {
1152
+ // A one-second 1/60 replay leaves six epsilons after modulo. Limit display snapping
1153
+ // to eight duration-scaled epsilons. The public time and captured frame stay raw.
1154
+ if (trackTime < duration) return String(time);
1155
+ const residue = 8 * Number.EPSILON * duration;
1156
+ if (time <= residue) return '0';
1157
+ if (duration - time <= residue) return String(duration);
1158
+ return String(time);
1159
+ }
1160
+ function stepPlayback(handle: number, expectedOwner: JobPreparedInspection, expectedPlayer: RigPlayer, now: number): void {
1161
+ if (current !== expectedOwner || current?.player !== expectedPlayer) {
1162
+ expectedPlayer.pause();
1163
+ return;
1164
+ }
1165
+ if (handle !== rafHandle) return;
1166
+ rafHandle = 0;
1167
+ const clip = expectedOwner.assets?.animations.find(item => item.name === animation);
1168
+ if (!playing || !hubVisible || !animation || !clip || !(clip.duration > 0)) {
1169
+ stopPlayback(expectedPlayer);
1170
+ return;
1171
+ }
1172
+ const dt = lastFrameTime > 0 ? Math.min(Math.max(0, (now - lastFrameTime) / 1000), 0.1) : 0;
1173
+ lastFrameTime = now;
1174
+ const next = advancePlayback({ time, duration: clip.duration, carry: playbackCarry, elapsed: dt, repeat: loop });
1175
+ for (const step of next.steps) expectedPlayer.advance(step);
1176
+ const activeEntry = expectedPlayer.entry(0);
1177
+ if (!activeEntry || activeEntry.animation !== animation || activeEntry.loop !== loop ||
1178
+ !Number.isFinite(activeEntry.animationTime) || activeEntry.animationTime < 0 ||
1179
+ activeEntry.animationTime > activeEntry.duration) {
1180
+ stopPlayback(expectedPlayer);
1181
+ return;
1182
+ }
1183
+ playbackCarry = next.carry;
1184
+ if (next.ended) {
1185
+ playing = false;
1186
+ playbackCarry = 0;
1187
+ lastFrameTime = 0;
1188
+ expectedPlayer.pause();
1189
+ time = clip.duration;
1190
+ } else {
1191
+ time = activeEntry.animationTime;
1192
+ }
1193
+ const clock = workspace.querySelector<HTMLInputElement>('#job-time');
1194
+ if (clock) clock.value = playbackTimeLabel(clip.duration, activeEntry.trackTime);
1195
+ syncTransportControls();
1196
+ presentCurrentFrame(expectedOwner);
1197
+ if (playing && current === expectedOwner && current.player === expectedPlayer && hubVisible) {
1198
+ armPlayback(expectedOwner, expectedPlayer);
1199
+ }
1200
+ }
1106
1201
  function copyRigVerdict(ask: JobPendingAsk, owner: JobPreparedInspection) {
1107
1202
  if (owner !== current || ask.publicationId !== owner.display.publication.publicationId) {
1108
1203
  throw new Error('JOB_STILL_BINDING_MISMATCH');
@@ -1529,12 +1624,83 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1529
1624
  option.textContent = clip.name;
1530
1625
  clips.append(option);
1531
1626
  }
1532
- clips.onchange = () => { animation = clips.value; time = 0; pose(); };
1627
+ clips.onchange = () => { stopPlayback(); animation = clips.value; time = 0; pose(); };
1628
+ const playButton = node('button', 'Play');
1629
+ playButton.id = 'job-play';
1630
+ playButton.type = 'button';
1631
+ playButton.setAttribute('aria-pressed', 'false');
1632
+ playButton.disabled = true;
1633
+ playButton.onclick = () => {
1634
+ const clip = current?.assets?.animations.find(item => item.name === animation);
1635
+ if (!current?.player || !animation || !clip || !(clip.duration > 0)) {
1636
+ stopPlayback();
1637
+ return;
1638
+ }
1639
+ if (playing) {
1640
+ stopPlayback();
1641
+ presentCurrentFrame(current);
1642
+ return;
1643
+ }
1644
+ if (current.player.composed) current.setupPose(false);
1645
+ if (!loop && time >= clip.duration) {
1646
+ time = 0;
1647
+ current.player.play(animation, { loop });
1648
+ } else if (current.player.current !== animation) {
1649
+ current.player.play(animation, { loop });
1650
+ if (time > 0) current.player.seek(time);
1651
+ } else {
1652
+ current.player.resume();
1653
+ }
1654
+ playing = true;
1655
+ playbackCarry = 0;
1656
+ lastFrameTime = 0;
1657
+ const clockInput = workspace.querySelector<HTMLInputElement>('#job-time');
1658
+ if (clockInput) clockInput.value = playbackTimeLabel(clip.duration, current.player.time);
1659
+ syncTransportControls();
1660
+ presentCurrentFrame(current);
1661
+ if (hubVisible) armPlayback(current, current.player);
1662
+ };
1663
+ const loopInput = node('input');
1664
+ loopInput.id = 'job-loop';
1665
+ loopInput.type = 'checkbox';
1666
+ loopInput.checked = loop;
1667
+ loopInput.onchange = () => {
1668
+ loop = loopInput.checked;
1669
+ playbackCarry = 0;
1670
+ lastFrameTime = 0;
1671
+ const clip = current?.assets?.animations.find(item => item.name === animation);
1672
+ if (current?.player && animation && clip) {
1673
+ if (current.player.composed) current.setupPose(false);
1674
+ current.player.play(animation, { loop });
1675
+ if (time > 0) current.player.seek(time);
1676
+ const activeEntry = current.player.entry(0);
1677
+ if (!activeEntry || activeEntry.animation !== animation || activeEntry.loop !== loop ||
1678
+ !Number.isFinite(activeEntry.animationTime) || activeEntry.animationTime < 0 ||
1679
+ activeEntry.animationTime > activeEntry.duration) {
1680
+ stopPlayback(current.player);
1681
+ return;
1682
+ }
1683
+ time = activeEntry.animationTime;
1684
+ const clockInput = workspace.querySelector<HTMLInputElement>('#job-time');
1685
+ if (clockInput) clockInput.value = playbackTimeLabel(clip.duration, activeEntry.trackTime);
1686
+ if (!playing) current.player.pause();
1687
+ syncTransportControls();
1688
+ presentCurrentFrame(current);
1689
+ if (playing && hubVisible) armPlayback(current, current.player);
1690
+ } else {
1691
+ syncTransportControls();
1692
+ }
1693
+ };
1533
1694
  const clock = node('input');
1534
1695
  clock.id = 'job-time';
1535
1696
  clock.type = 'number';
1536
1697
  clock.min = '0';
1537
1698
  clock.step = '0.001';
1699
+ clock.oninput = () => {
1700
+ if (!playing) return;
1701
+ stopPlayback();
1702
+ if (current) presentCurrentFrame(current);
1703
+ };
1538
1704
  clock.onchange = () => {
1539
1705
  const value = Number(clock.value);
1540
1706
  const duration = current?.assets?.animations.find(clip => clip.name === animation)?.duration;
@@ -1543,13 +1709,14 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1543
1709
  notice.textContent = t('KEY_TIME_OUT_OF_DOMAIN');
1544
1710
  return;
1545
1711
  }
1712
+ stopPlayback();
1546
1713
  time = value;
1547
1714
  pose();
1548
1715
  };
1549
1716
  const panels = selectValues(['1', '2', '3', '4'], ['1 Parts + mesh', '2 Interview', '3 Rig + animation', '4 Direction']);
1550
1717
  panels.id = 'job-panel';
1551
1718
  panels.onchange = () => { panel = panels.value; showPanel(); };
1552
- transport.append(clips, inputLabel('job.time', clock), panels);
1719
+ transport.append(clips, playButton, inputLabel('Repeat', loopInput), inputLabel('job.time', clock), panels);
1553
1720
  const numeric = node('pre');
1554
1721
  numeric.id = 'view-coordinates';
1555
1722
  numeric.hidden = true;
@@ -1666,15 +1833,28 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1666
1833
  if (current.player) {
1667
1834
  drawn.delete(current);
1668
1835
  current.rigPresentation = undefined;
1669
- if (animation && current.player.current !== animation)
1670
- current.player.play(animation, { loop: true });
1671
- if (animation)
1836
+ if (animation && (current.player.current !== animation || current.player.entry(0)?.loop !== loop))
1837
+ current.player.play(animation, { loop });
1838
+ if (animation) {
1839
+ const seekAtLoopEnd = loop && time === clip?.duration;
1672
1840
  current.player.seek(time);
1841
+ const activeEntry = current.player.entry(0);
1842
+ if (!activeEntry || activeEntry.animation !== animation || activeEntry.loop !== loop ||
1843
+ !Number.isFinite(activeEntry.animationTime) || activeEntry.animationTime < 0 ||
1844
+ activeEntry.animationTime > activeEntry.duration) {
1845
+ current.player.pause();
1846
+ notice.textContent = t('JOB_VISUAL_UNAVAILABLE');
1847
+ return;
1848
+ }
1849
+ time = activeEntry.animationTime;
1850
+ clock.value = seekAtLoopEnd ? '0' : String(time);
1851
+ }
1673
1852
  current.player.pause();
1674
1853
  drawInspection(current);
1675
1854
  current.inspection?.select(selectedPart ? { side: 'single', slot: selectedPart, mode: partMode } : null);
1676
1855
  setText('job-part-observation', selectedPart ? JSON.stringify(current.inspection?.observation(), null, 2) : '');
1677
1856
  }
1857
+ syncTransportControls();
1678
1858
  if (seekReference) {
1679
1859
  const value = reference();
1680
1860
  const owner = current;
@@ -1693,7 +1873,7 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1693
1873
  }
1694
1874
  function pauseHiddenHubFrame(): void {
1695
1875
  if (hubVisible) return;
1696
- current?.player?.pause();
1876
+ stopPlayback();
1697
1877
  for (const video of workspace.querySelectorAll('video')) video.pause();
1698
1878
  }
1699
1879
  function announceHubFrame(): void {
@@ -1723,6 +1903,7 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1723
1903
  for (const entry of nextForms.values()) formHost.append(entry.form);
1724
1904
  referenceSelection.cancel();
1725
1905
  const previous = current;
1906
+ stopPlayback(previous?.player);
1726
1907
  const previousElement = currentElement;
1727
1908
  const previousForms = new Map(forms);
1728
1909
  const navigation = { animation, time, selectedPart, partMode, selectedReference, panel,
@@ -1742,6 +1923,7 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1742
1923
  let nextUnbind: (() => void) | null = null;
1743
1924
  let nextResize: ResizeObserver | null = null;
1744
1925
  const rollback = () => {
1926
+ stopPlayback(prepared.player);
1745
1927
  nextUnbind?.();
1746
1928
  nextResize?.disconnect();
1747
1929
  current = previous;
@@ -1759,6 +1941,7 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1759
1941
  Object.assign(viewportState, navigation.camera);
1760
1942
  if (current) {
1761
1943
  el<HTMLSelectElement>('job-reference').value = selectedReference;
1944
+ syncTransportControls();
1762
1945
  viewport.fitCamera();
1763
1946
  }
1764
1947
  };
@@ -1840,6 +2023,11 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1840
2023
  ? t('job.updated', [state.previousLabel, state.displayed.publication.label]) : state.displayed.publication.label;
1841
2024
  if (compatibilityNotice)
1842
2025
  notice.textContent += ' · ' + t('job.incompatible');
2026
+ if (state.closed) {
2027
+ const wasPlaying = playing;
2028
+ stopPlayback();
2029
+ if (wasPlaying && current) presentCurrentFrame(current);
2030
+ }
1843
2031
  updateForms(state);
1844
2032
  if (current)
1845
2033
  showPanel();
@@ -1909,6 +2097,7 @@ export async function mountJobSession(host: HTMLElement): Promise<() => void> {
1909
2097
  };
1910
2098
  resolutionChanged();
1911
2099
  const stop = () => {
2100
+ stopPlayback();
1912
2101
  resolution?.removeEventListener('change', resolutionChanged);
1913
2102
  globalThis.removeEventListener('message', hubVisibility);
1914
2103
  saveDraft();