2021-12-02 19:13:06 +00:00
|
|
|
import { useState, useEffect, useRef, useMemo, MouseEvent } from 'react';
|
2021-11-29 17:44:31 +00:00
|
|
|
import { MediaSet } from './generated/media_set';
|
|
|
|
import { Frames, VideoPosition } from './App';
|
2021-10-08 14:38:35 +00:00
|
|
|
import { WaveformCanvas } from './WaveformCanvas';
|
2021-11-29 17:44:31 +00:00
|
|
|
import { Observable } from 'rxjs';
|
2021-10-08 14:38:35 +00:00
|
|
|
|
2021-11-25 18:02:37 +00:00
|
|
|
export interface Selection {
|
|
|
|
start: number;
|
|
|
|
end: number;
|
|
|
|
}
|
|
|
|
|
2021-10-08 14:38:35 +00:00
|
|
|
interface Props {
|
2021-11-29 17:44:31 +00:00
|
|
|
peaks: Observable<number[]>;
|
2021-10-08 14:38:35 +00:00
|
|
|
mediaSet: MediaSet;
|
|
|
|
height: number;
|
|
|
|
offsetPixels: number;
|
2021-11-25 18:02:37 +00:00
|
|
|
position: VideoPosition;
|
2021-11-30 19:41:34 +00:00
|
|
|
viewport: Frames;
|
2021-10-08 14:38:35 +00:00
|
|
|
onSelectionChange: (selection: Frames) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Mode {
|
|
|
|
Normal,
|
|
|
|
Selecting,
|
|
|
|
Dragging,
|
2021-11-25 18:02:37 +00:00
|
|
|
ResizingStart,
|
|
|
|
ResizingEnd,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum HoverState {
|
|
|
|
Normal,
|
|
|
|
OverSelectionStart,
|
|
|
|
OverSelectionEnd,
|
|
|
|
OverSelection,
|
2021-10-08 14:38:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:44:31 +00:00
|
|
|
export const CanvasLogicalWidth = 2_000;
|
|
|
|
export const CanvasLogicalHeight = 500;
|
2021-10-08 14:38:35 +00:00
|
|
|
|
|
|
|
const emptySelection = { start: 0, end: 0 };
|
|
|
|
|
|
|
|
export const Overview: React.FC<Props> = ({
|
2021-11-29 17:44:31 +00:00
|
|
|
peaks,
|
2021-10-08 14:38:35 +00:00
|
|
|
mediaSet,
|
|
|
|
height,
|
|
|
|
offsetPixels,
|
|
|
|
position,
|
2021-11-30 19:41:34 +00:00
|
|
|
viewport,
|
2021-10-08 14:38:35 +00:00
|
|
|
onSelectionChange,
|
|
|
|
}: Props) => {
|
|
|
|
const hudCanvasRef = useRef<HTMLCanvasElement>(null);
|
|
|
|
const [mode, setMode] = useState(Mode.Normal);
|
2021-11-25 18:02:37 +00:00
|
|
|
const [hoverState, setHoverState] = useState(HoverState.Normal);
|
2021-11-30 19:41:34 +00:00
|
|
|
const [cursor, setCursor] = useState('auto');
|
|
|
|
|
|
|
|
// selection and newSelection relate to canvas logical pixels:
|
|
|
|
const [selection, setSelection] = useState({ ...emptySelection });
|
2021-11-25 18:02:37 +00:00
|
|
|
const [newSelection, setNewSelection] = useState({
|
|
|
|
...emptySelection,
|
|
|
|
});
|
|
|
|
|
|
|
|
const moveOffsetX = useRef(0);
|
2021-10-08 14:38:35 +00:00
|
|
|
|
2021-11-30 19:41:34 +00:00
|
|
|
// side effects
|
2021-10-08 14:38:35 +00:00
|
|
|
|
2021-11-23 13:06:50 +00:00
|
|
|
// handle global mouse up.
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('mouseup', handleMouseUp);
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('mouseup', handleMouseUp);
|
|
|
|
};
|
|
|
|
}, [mode, newSelection]);
|
|
|
|
|
2021-11-30 19:41:34 +00:00
|
|
|
// set selection state on viewport change
|
2021-11-25 18:02:37 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (mediaSet == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-30 19:41:34 +00:00
|
|
|
setSelection({
|
|
|
|
start: Math.round(
|
|
|
|
(viewport.start / mediaSet.audioFrames) * CanvasLogicalWidth
|
|
|
|
),
|
|
|
|
end: Math.round(
|
|
|
|
(viewport.end / mediaSet.audioFrames) * CanvasLogicalWidth
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}, [mediaSet, viewport]);
|
2021-11-25 18:02:37 +00:00
|
|
|
|
2021-10-08 14:38:35 +00:00
|
|
|
// load peaks on mediaset change
|
|
|
|
useEffect(() => {
|
|
|
|
(async function () {
|
|
|
|
if (mediaSet == null) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-06 20:52:47 +00:00
|
|
|
const canvas = hudCanvasRef.current;
|
|
|
|
if (canvas == null) {
|
|
|
|
console.error('no hud canvas ref available');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
if (ctx == null) {
|
|
|
|
console.error('no hud 2d context available');
|
|
|
|
return;
|
|
|
|
}
|
2021-10-08 14:38:35 +00:00
|
|
|
})();
|
|
|
|
}, [mediaSet]);
|
|
|
|
|
2021-11-06 20:52:47 +00:00
|
|
|
// draw the overview HUD
|
2021-10-08 14:38:35 +00:00
|
|
|
useEffect(() => {
|
2021-11-25 18:02:37 +00:00
|
|
|
requestAnimationFrame(() => {
|
2021-10-08 14:38:35 +00:00
|
|
|
const canvas = hudCanvasRef.current;
|
|
|
|
if (canvas == null) {
|
|
|
|
console.error('no hud canvas ref available');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
if (ctx == null) {
|
|
|
|
console.error('no hud 2d context available');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
2021-11-25 18:02:37 +00:00
|
|
|
// draw selection
|
|
|
|
|
|
|
|
let currentSelection: Selection;
|
|
|
|
if (
|
|
|
|
mode == Mode.Selecting ||
|
|
|
|
mode == Mode.Dragging ||
|
|
|
|
mode == Mode.ResizingStart ||
|
|
|
|
mode == Mode.ResizingEnd
|
|
|
|
) {
|
2021-10-08 14:38:35 +00:00
|
|
|
currentSelection = newSelection;
|
|
|
|
} else {
|
|
|
|
currentSelection = selection;
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:02:37 +00:00
|
|
|
ctx.beginPath();
|
2021-10-08 14:38:35 +00:00
|
|
|
ctx.strokeStyle = 'red';
|
2021-11-25 18:02:37 +00:00
|
|
|
ctx.lineWidth = 4;
|
|
|
|
const alpha = hoverState == HoverState.OverSelection ? '0.15' : '0.13';
|
|
|
|
ctx.fillStyle = `rgba(255, 255, 255, ${alpha})`;
|
2021-11-30 19:41:34 +00:00
|
|
|
ctx.rect(
|
|
|
|
currentSelection.start,
|
|
|
|
2,
|
|
|
|
currentSelection.end - currentSelection.start,
|
|
|
|
canvas.height - 10
|
|
|
|
);
|
2021-11-25 18:02:37 +00:00
|
|
|
ctx.fill();
|
|
|
|
ctx.stroke();
|
|
|
|
|
|
|
|
// draw position marker
|
|
|
|
|
|
|
|
const markerX = canvas.width * (position.percent / 100);
|
2021-10-08 14:38:35 +00:00
|
|
|
ctx.beginPath();
|
2021-11-25 18:02:37 +00:00
|
|
|
ctx.moveTo(markerX, 0);
|
2021-10-08 14:38:35 +00:00
|
|
|
ctx.lineWidth = 4;
|
2021-11-25 18:02:37 +00:00
|
|
|
ctx.lineTo(markerX, canvas.height - 4);
|
2021-10-08 14:38:35 +00:00
|
|
|
ctx.stroke();
|
2021-11-25 18:02:37 +00:00
|
|
|
});
|
2021-11-30 19:41:34 +00:00
|
|
|
}, [mediaSet, selection, newSelection, position]);
|
2021-10-08 14:38:35 +00:00
|
|
|
|
|
|
|
// handlers
|
|
|
|
|
2021-11-30 19:41:34 +00:00
|
|
|
const hoverOffset = 10;
|
|
|
|
|
|
|
|
const isHoveringSelectionStart = (x: number): boolean => {
|
|
|
|
return (
|
|
|
|
x > selection.start - hoverOffset && x < selection.start + hoverOffset
|
|
|
|
);
|
2021-11-25 18:02:37 +00:00
|
|
|
};
|
|
|
|
|
2021-11-30 19:41:34 +00:00
|
|
|
const isHoveringSelectionEnd = (x: number): boolean => {
|
|
|
|
return x > selection.end - hoverOffset && x < selection.end + hoverOffset;
|
2021-11-25 18:02:37 +00:00
|
|
|
};
|
|
|
|
|
2021-11-30 19:41:34 +00:00
|
|
|
const isHoveringSelection = (x: number): boolean => {
|
|
|
|
return x >= selection.start && x <= selection.end;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getCanvasX = (evt: MouseEvent<HTMLCanvasElement>): number => {
|
|
|
|
const rect = evt.currentTarget.getBoundingClientRect();
|
|
|
|
const x = Math.round(
|
|
|
|
((evt.clientX - rect.left) / rect.width) * CanvasLogicalWidth
|
|
|
|
);
|
|
|
|
return constrainXToCanvas(x);
|
|
|
|
};
|
|
|
|
|
|
|
|
const constrainXToCanvas = (x: number): number => {
|
|
|
|
if (x < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (x > CanvasLogicalWidth) {
|
|
|
|
return CanvasLogicalWidth;
|
|
|
|
}
|
|
|
|
return x;
|
2021-11-25 18:02:37 +00:00
|
|
|
};
|
|
|
|
|
2021-10-08 14:38:35 +00:00
|
|
|
const handleMouseDown = (evt: MouseEvent<HTMLCanvasElement>) => {
|
|
|
|
if (mode != Mode.Normal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-30 19:41:34 +00:00
|
|
|
const x = getCanvasX(evt);
|
2021-10-08 14:38:35 +00:00
|
|
|
|
2021-11-30 19:41:34 +00:00
|
|
|
if (isHoveringSelectionStart(x)) {
|
2021-11-25 18:02:37 +00:00
|
|
|
setMode(Mode.ResizingStart);
|
2021-11-30 19:41:34 +00:00
|
|
|
moveOffsetX.current = x;
|
2021-11-25 18:02:37 +00:00
|
|
|
return;
|
2021-11-30 19:41:34 +00:00
|
|
|
} else if (isHoveringSelectionEnd(x)) {
|
2021-11-25 18:02:37 +00:00
|
|
|
setMode(Mode.ResizingEnd);
|
2021-11-30 19:41:34 +00:00
|
|
|
moveOffsetX.current = x;
|
2021-11-25 18:02:37 +00:00
|
|
|
return;
|
2021-11-30 19:41:34 +00:00
|
|
|
} else if (isHoveringSelection(x)) {
|
2021-10-08 14:38:35 +00:00
|
|
|
setMode(Mode.Dragging);
|
2021-11-25 18:02:37 +00:00
|
|
|
setCursor('pointer');
|
2021-11-30 19:41:34 +00:00
|
|
|
moveOffsetX.current = x;
|
2021-10-08 14:38:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setMode(Mode.Selecting);
|
2021-11-25 18:02:37 +00:00
|
|
|
setCursor('col-resize');
|
2021-11-30 19:41:34 +00:00
|
|
|
moveOffsetX.current = x;
|
|
|
|
setNewSelection({ start: x, end: x });
|
2021-10-08 14:38:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseMove = (evt: MouseEvent<HTMLCanvasElement>) => {
|
2021-11-30 19:41:34 +00:00
|
|
|
const x = getCanvasX(evt);
|
2021-10-08 14:38:35 +00:00
|
|
|
|
2021-11-25 18:02:37 +00:00
|
|
|
switch (mode) {
|
|
|
|
case Mode.Normal: {
|
|
|
|
if (isHoveringSelectionStart(x)) {
|
|
|
|
setHoverState(HoverState.OverSelectionStart);
|
|
|
|
setCursor('col-resize');
|
|
|
|
} else if (isHoveringSelectionEnd(x)) {
|
|
|
|
setHoverState(HoverState.OverSelectionEnd);
|
|
|
|
setCursor('col-resize');
|
|
|
|
} else if (isHoveringSelection(x)) {
|
|
|
|
setHoverState(HoverState.OverSelection);
|
|
|
|
setCursor('pointer');
|
|
|
|
} else {
|
|
|
|
setCursor('auto');
|
|
|
|
}
|
|
|
|
break;
|
2021-10-08 14:38:35 +00:00
|
|
|
}
|
2021-11-25 18:02:37 +00:00
|
|
|
case Mode.ResizingStart: {
|
|
|
|
const diff = x - moveOffsetX.current;
|
2021-11-30 19:41:34 +00:00
|
|
|
const start = constrainXToCanvas(selection.start + diff);
|
2021-10-08 14:38:35 +00:00
|
|
|
|
2021-11-25 18:02:37 +00:00
|
|
|
if (start > selection.end) {
|
|
|
|
setNewSelection({ start: selection.end, end: start });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
setNewSelection({ ...newSelection, start: start });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Mode.ResizingEnd: {
|
|
|
|
const diff = x - moveOffsetX.current;
|
2021-11-30 19:41:34 +00:00
|
|
|
const start = constrainXToCanvas(selection.end + diff);
|
2021-11-25 18:02:37 +00:00
|
|
|
|
|
|
|
if (start < selection.start) {
|
|
|
|
setNewSelection({ start: Math.max(0, start), end: selection.start });
|
|
|
|
break;
|
|
|
|
}
|
2021-10-08 14:38:35 +00:00
|
|
|
|
2021-11-25 18:02:37 +00:00
|
|
|
setNewSelection({ ...newSelection, end: start });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Mode.Dragging: {
|
|
|
|
const diff = x - moveOffsetX.current;
|
|
|
|
const width = selection.end - selection.start;
|
|
|
|
let start = Math.max(0, selection.start + diff);
|
|
|
|
let end = start + width;
|
2021-11-30 19:41:34 +00:00
|
|
|
if (end > CanvasLogicalWidth) {
|
|
|
|
end = CanvasLogicalWidth;
|
2021-11-25 18:02:37 +00:00
|
|
|
start = end - width;
|
|
|
|
}
|
|
|
|
|
|
|
|
setNewSelection({ start: start, end: end });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Mode.Selecting: {
|
|
|
|
if (x < moveOffsetX.current) {
|
|
|
|
setNewSelection({
|
|
|
|
start: x,
|
|
|
|
end: moveOffsetX.current,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setNewSelection({ start: moveOffsetX.current, end: x });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 14:38:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseUp = () => {
|
|
|
|
if (mode == Mode.Normal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setMode(Mode.Normal);
|
2021-11-25 18:02:37 +00:00
|
|
|
setCursor('auto');
|
|
|
|
|
2021-11-30 19:41:34 +00:00
|
|
|
const start = Math.round(
|
|
|
|
(newSelection.start / CanvasLogicalWidth) * mediaSet.audioFrames
|
|
|
|
);
|
|
|
|
const end = Math.round(
|
|
|
|
(newSelection.end / CanvasLogicalWidth) * mediaSet.audioFrames
|
|
|
|
);
|
|
|
|
onSelectionChange({ start, end });
|
2021-11-25 18:02:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseLeave = (_evt: MouseEvent<HTMLCanvasElement>) => {
|
|
|
|
setHoverState(HoverState.Normal);
|
2021-10-08 14:38:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// render component
|
|
|
|
|
|
|
|
const containerStyles = {
|
|
|
|
flexGrow: 0,
|
|
|
|
position: 'relative',
|
|
|
|
margin: `0 ${offsetPixels}px`,
|
|
|
|
height: `${height}px`,
|
|
|
|
} as React.CSSProperties;
|
|
|
|
|
|
|
|
const hudCanvasStyles = {
|
|
|
|
position: 'absolute',
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
display: 'block',
|
|
|
|
zIndex: 2,
|
2021-11-25 18:02:37 +00:00
|
|
|
cursor: cursor,
|
2021-10-08 14:38:35 +00:00
|
|
|
} as React.CSSProperties;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div style={containerStyles}>
|
2021-12-02 19:13:06 +00:00
|
|
|
{useMemo(
|
|
|
|
() => (
|
|
|
|
<WaveformCanvas
|
|
|
|
peaks={peaks}
|
|
|
|
channels={mediaSet.audioChannels}
|
|
|
|
width={CanvasLogicalWidth}
|
|
|
|
height={CanvasLogicalHeight}
|
|
|
|
strokeStyle="black"
|
|
|
|
fillStyle="#003300"
|
|
|
|
zIndex={1}
|
|
|
|
alpha={1}
|
|
|
|
></WaveformCanvas>
|
|
|
|
),
|
|
|
|
[peaks, mediaSet]
|
|
|
|
)}
|
2021-10-08 14:38:35 +00:00
|
|
|
<canvas
|
|
|
|
ref={hudCanvasRef}
|
|
|
|
width={CanvasLogicalWidth}
|
|
|
|
height={CanvasLogicalHeight}
|
|
|
|
style={hudCanvasStyles}
|
|
|
|
onMouseDown={handleMouseDown}
|
|
|
|
onMouseMove={handleMouseMove}
|
2021-11-25 18:02:37 +00:00
|
|
|
onMouseLeave={handleMouseLeave}
|
2021-10-08 14:38:35 +00:00
|
|
|
></canvas>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|