2022-01-14 11:24:59 +00:00
|
|
|
import { useState, useEffect, useRef, MouseEvent } from 'react';
|
2021-12-11 16:25:43 +00:00
|
|
|
|
|
|
|
interface Styles {
|
|
|
|
borderLineWidth: number;
|
|
|
|
borderStrokeStyle: string;
|
|
|
|
positionLineWidth: number;
|
|
|
|
positionStrokeStyle: string;
|
|
|
|
}
|
2021-12-06 22:52:24 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
2021-12-12 10:04:23 +00:00
|
|
|
emptySelectionAction: EmptySelectionAction;
|
2021-12-11 16:25:43 +00:00
|
|
|
styles: Styles;
|
|
|
|
position: number | null;
|
2021-12-06 22:52:24 +00:00
|
|
|
selection: Selection;
|
|
|
|
onSelectionChange: (selection: Selection) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Mode {
|
|
|
|
Normal,
|
|
|
|
Selecting,
|
|
|
|
Dragging,
|
|
|
|
ResizingStart,
|
|
|
|
ResizingEnd,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum HoverState {
|
|
|
|
Normal,
|
|
|
|
OverSelectionStart,
|
|
|
|
OverSelectionEnd,
|
|
|
|
OverSelection,
|
|
|
|
}
|
|
|
|
|
2021-12-12 10:04:23 +00:00
|
|
|
export enum EmptySelectionAction {
|
|
|
|
SelectNothing,
|
|
|
|
SelectPrevious,
|
|
|
|
}
|
|
|
|
|
2021-12-06 22:52:24 +00:00
|
|
|
export interface Selection {
|
|
|
|
start: number;
|
|
|
|
end: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const emptySelection: Selection = { start: 0, end: 0 };
|
|
|
|
|
|
|
|
export const HudCanvas: React.FC<Props> = ({
|
|
|
|
width,
|
|
|
|
height,
|
2021-12-12 10:04:23 +00:00
|
|
|
emptySelectionAction,
|
2021-12-11 16:25:43 +00:00
|
|
|
styles: {
|
|
|
|
borderLineWidth,
|
|
|
|
borderStrokeStyle,
|
|
|
|
positionLineWidth,
|
|
|
|
positionStrokeStyle,
|
|
|
|
},
|
2021-12-06 22:52:24 +00:00
|
|
|
position,
|
|
|
|
selection,
|
|
|
|
onSelectionChange,
|
|
|
|
}: Props) => {
|
|
|
|
// selection and newSelection are in canvas logical pixels:
|
|
|
|
const [newSelection, setNewSelection] = useState({
|
|
|
|
...emptySelection,
|
|
|
|
});
|
|
|
|
const [mode, setMode] = useState(Mode.Normal);
|
|
|
|
const [hoverState, setHoverState] = useState(HoverState.Normal);
|
2022-01-14 11:54:54 +00:00
|
|
|
const [cursor, setCursor] = useState('cursor-auto');
|
2021-12-06 22:52:24 +00:00
|
|
|
|
|
|
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
|
|
const moveOffsetX = useRef(0);
|
|
|
|
|
|
|
|
// side effects
|
|
|
|
|
|
|
|
// handle global mouse up
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('mouseup', handleMouseUp);
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('mouseup', handleMouseUp);
|
|
|
|
};
|
|
|
|
}, [mode, newSelection]);
|
|
|
|
|
|
|
|
// draw the overview HUD
|
|
|
|
useEffect(() => {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
const canvas = canvasRef.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);
|
|
|
|
|
|
|
|
// draw selection
|
|
|
|
|
|
|
|
let currentSelection: Selection;
|
|
|
|
if (
|
|
|
|
mode == Mode.Selecting ||
|
|
|
|
mode == Mode.Dragging ||
|
|
|
|
mode == Mode.ResizingStart ||
|
|
|
|
mode == Mode.ResizingEnd
|
|
|
|
) {
|
|
|
|
currentSelection = newSelection;
|
|
|
|
} else {
|
|
|
|
currentSelection = selection;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.beginPath();
|
2021-12-11 16:25:43 +00:00
|
|
|
ctx.strokeStyle = borderStrokeStyle;
|
|
|
|
ctx.lineWidth = borderLineWidth;
|
2021-12-06 22:52:24 +00:00
|
|
|
const alpha = hoverState == HoverState.OverSelection ? '0.15' : '0.13';
|
|
|
|
ctx.fillStyle = `rgba(255, 255, 255, ${alpha})`;
|
|
|
|
ctx.rect(
|
|
|
|
currentSelection.start,
|
2021-12-11 16:25:43 +00:00
|
|
|
borderLineWidth,
|
2021-12-06 22:52:24 +00:00
|
|
|
currentSelection.end - currentSelection.start,
|
2021-12-11 16:25:43 +00:00
|
|
|
canvas.height - borderLineWidth * 2
|
2021-12-06 22:52:24 +00:00
|
|
|
);
|
|
|
|
ctx.fill();
|
|
|
|
ctx.stroke();
|
|
|
|
|
|
|
|
// draw position marker
|
|
|
|
|
2021-12-11 16:25:43 +00:00
|
|
|
if (position == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-06 22:52:24 +00:00
|
|
|
ctx.beginPath();
|
2021-12-11 16:25:43 +00:00
|
|
|
ctx.strokeStyle = positionStrokeStyle;
|
|
|
|
ctx.lineWidth = positionLineWidth;
|
|
|
|
ctx.moveTo(position, 0);
|
2021-12-06 22:52:24 +00:00
|
|
|
ctx.lineWidth = 4;
|
2021-12-11 16:25:43 +00:00
|
|
|
ctx.lineTo(position, canvas.height - 4);
|
2021-12-06 22:52:24 +00:00
|
|
|
ctx.stroke();
|
|
|
|
});
|
|
|
|
}, [selection, newSelection, position]);
|
|
|
|
|
|
|
|
// handlers
|
|
|
|
|
|
|
|
const hoverOffset = 10;
|
|
|
|
|
|
|
|
const isHoveringSelectionStart = (x: number): boolean => {
|
|
|
|
return (
|
|
|
|
x > selection.start - hoverOffset && x < selection.start + hoverOffset
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const isHoveringSelectionEnd = (x: number): boolean => {
|
|
|
|
return x > selection.end - hoverOffset && x < selection.end + hoverOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
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) * width);
|
|
|
|
return constrainXToCanvas(x);
|
|
|
|
};
|
|
|
|
|
|
|
|
const constrainXToCanvas = (x: number): number => {
|
|
|
|
if (x < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (x > width) {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
};
|
|
|
|
|
2022-01-14 11:24:59 +00:00
|
|
|
const handleMouseDown = (evt: MouseEvent<HTMLCanvasElement>) => {
|
|
|
|
if (mode != Mode.Normal) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-06 22:52:24 +00:00
|
|
|
|
2022-01-14 11:24:59 +00:00
|
|
|
const x = getCanvasX(evt);
|
|
|
|
|
|
|
|
if (isHoveringSelectionStart(x)) {
|
|
|
|
setMode(Mode.ResizingStart);
|
|
|
|
moveOffsetX.current = x;
|
|
|
|
} else if (isHoveringSelectionEnd(x)) {
|
|
|
|
setMode(Mode.ResizingEnd);
|
|
|
|
moveOffsetX.current = x;
|
|
|
|
} else if (isHoveringSelection(x)) {
|
|
|
|
setMode(Mode.Dragging);
|
2022-01-14 11:54:54 +00:00
|
|
|
setCursor('cursor-pointer');
|
2022-01-14 11:24:59 +00:00
|
|
|
moveOffsetX.current = x;
|
|
|
|
} else {
|
|
|
|
setMode(Mode.Selecting);
|
2022-01-14 11:54:54 +00:00
|
|
|
setCursor('cursor-col-resize');
|
2022-01-14 11:24:59 +00:00
|
|
|
moveOffsetX.current = x;
|
|
|
|
setNewSelection({ start: x, end: x });
|
|
|
|
}
|
|
|
|
};
|
2021-12-06 22:52:24 +00:00
|
|
|
|
2022-01-14 11:24:59 +00:00
|
|
|
const handleMouseMove = (evt: MouseEvent<HTMLCanvasElement>) => {
|
|
|
|
const x = getCanvasX(evt);
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case Mode.Normal: {
|
|
|
|
if (isHoveringSelectionStart(x)) {
|
|
|
|
setHoverState(HoverState.OverSelectionStart);
|
2022-01-14 11:54:54 +00:00
|
|
|
setCursor('cursor-col-resize');
|
2022-01-14 11:24:59 +00:00
|
|
|
} else if (isHoveringSelectionEnd(x)) {
|
|
|
|
setHoverState(HoverState.OverSelectionEnd);
|
2022-01-14 11:54:54 +00:00
|
|
|
setCursor('cursor-col-resize');
|
2022-01-14 11:24:59 +00:00
|
|
|
} else if (isHoveringSelection(x)) {
|
|
|
|
setHoverState(HoverState.OverSelection);
|
2022-01-14 11:54:54 +00:00
|
|
|
setCursor('cursor-pointer');
|
2022-01-14 11:24:59 +00:00
|
|
|
} else {
|
2022-01-14 11:54:54 +00:00
|
|
|
setCursor('cursor-auto');
|
2021-12-06 22:52:24 +00:00
|
|
|
}
|
2022-01-14 11:24:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Mode.ResizingStart: {
|
|
|
|
const diff = x - moveOffsetX.current;
|
|
|
|
const start = constrainXToCanvas(selection.start + diff);
|
2021-12-06 22:52:24 +00:00
|
|
|
|
2022-01-14 11:24:59 +00:00
|
|
|
if (start > selection.end) {
|
|
|
|
setNewSelection({ start: selection.end, end: start });
|
2021-12-06 22:52:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:24:59 +00:00
|
|
|
setNewSelection({ ...selection, start: start });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Mode.ResizingEnd: {
|
|
|
|
const diff = x - moveOffsetX.current;
|
|
|
|
const end = constrainXToCanvas(selection.end + diff);
|
2021-12-06 22:52:24 +00:00
|
|
|
|
2022-01-14 11:24:59 +00:00
|
|
|
if (end < selection.start) {
|
|
|
|
setNewSelection({ start: Math.max(0, end), end: selection.start });
|
2022-01-13 06:59:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-01-14 11:24:59 +00:00
|
|
|
|
|
|
|
setNewSelection({ ...selection, end: end });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Mode.Dragging: {
|
|
|
|
const diff = x - moveOffsetX.current;
|
|
|
|
const selectionWidth = selection.end - selection.start;
|
|
|
|
let start = Math.max(0, selection.start + diff);
|
|
|
|
let end = start + selectionWidth;
|
|
|
|
if (end > width) {
|
|
|
|
end = width;
|
|
|
|
start = end - selectionWidth;
|
2022-01-13 06:59:48 +00:00
|
|
|
}
|
2022-01-14 11:24:59 +00:00
|
|
|
|
|
|
|
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 });
|
2021-12-06 22:52:24 +00:00
|
|
|
}
|
2022-01-14 11:24:59 +00:00
|
|
|
break;
|
2021-12-06 22:52:24 +00:00
|
|
|
}
|
2022-01-14 11:24:59 +00:00
|
|
|
}
|
|
|
|
};
|
2021-12-06 22:52:24 +00:00
|
|
|
|
2022-01-14 11:24:59 +00:00
|
|
|
const handleMouseUp = () => {
|
2021-12-06 22:52:24 +00:00
|
|
|
if (mode == Mode.Normal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setMode(Mode.Normal);
|
2022-01-14 11:54:54 +00:00
|
|
|
setCursor('cursor-auto');
|
2021-12-06 22:52:24 +00:00
|
|
|
|
|
|
|
if (newSelection.start == newSelection.end) {
|
2021-12-12 10:04:23 +00:00
|
|
|
handleEmptySelectionAction();
|
2021-12-06 22:52:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelectionChange({ ...newSelection });
|
2022-01-14 11:24:59 +00:00
|
|
|
};
|
2021-12-06 22:52:24 +00:00
|
|
|
|
2022-01-14 11:24:59 +00:00
|
|
|
const handleEmptySelectionAction = () => {
|
2021-12-12 10:04:23 +00:00
|
|
|
switch (emptySelectionAction) {
|
|
|
|
case EmptySelectionAction.SelectPrevious:
|
|
|
|
setNewSelection({ ...selection });
|
|
|
|
break;
|
|
|
|
case EmptySelectionAction.SelectNothing:
|
|
|
|
onSelectionChange({ start: 0, end: 0 });
|
|
|
|
break;
|
|
|
|
}
|
2022-01-14 11:24:59 +00:00
|
|
|
};
|
2021-12-12 10:04:23 +00:00
|
|
|
|
2021-12-06 22:52:24 +00:00
|
|
|
const handleMouseLeave = (_evt: MouseEvent<HTMLCanvasElement>) => {
|
|
|
|
setHoverState(HoverState.Normal);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<canvas
|
|
|
|
ref={canvasRef}
|
2022-01-14 11:54:54 +00:00
|
|
|
className={`block absolute w-full h-full ${cursor} z-20`}
|
2021-12-06 22:52:24 +00:00
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
onMouseDown={handleMouseDown}
|
|
|
|
onMouseMove={handleMouseMove}
|
|
|
|
onMouseLeave={handleMouseLeave}
|
|
|
|
></canvas>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|