From 4f443af8fa4c243c8dcf1d6437c785260bca96e8 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Mon, 17 Jan 2022 18:58:50 +0100 Subject: [PATCH] HudCanvas: draw hover position --- frontend/src/HudCanvas.tsx | 25 ++++++++++++++++++++++--- frontend/src/Overview.tsx | 1 + frontend/src/Waveform.tsx | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/frontend/src/HudCanvas.tsx b/frontend/src/HudCanvas.tsx index 2835d48..844687f 100644 --- a/frontend/src/HudCanvas.tsx +++ b/frontend/src/HudCanvas.tsx @@ -5,6 +5,7 @@ interface Styles { borderStrokeStyle: string; positionLineWidth: number; positionStrokeStyle: string; + hoverPositionStrokeStyle: string; } interface Props { @@ -53,6 +54,7 @@ export const HudCanvas: React.FC = ({ borderStrokeStyle, positionLineWidth, positionStrokeStyle, + hoverPositionStrokeStyle, }, position, selection, @@ -62,6 +64,7 @@ export const HudCanvas: React.FC = ({ const [newSelection, setNewSelection] = useState({ ...emptySelection, }); + const [hoverPosition, setHoverPosition] = useState(null); const [mode, setMode] = useState(Mode.Normal); const [hoverState, setHoverState] = useState(HoverState.Normal); const [cursor, setCursor] = useState('cursor-auto'); @@ -126,6 +129,20 @@ export const HudCanvas: React.FC = ({ ctx.fill(); ctx.stroke(); + // draw hover position + if ( + hoverPosition != null && + (hoverPosition < currentSelection.start || + hoverPosition > currentSelection.end) + ) { + ctx.beginPath(); + ctx.strokeStyle = hoverPositionStrokeStyle; + ctx.lineWidth = 2; + ctx.moveTo(hoverPosition, 0); + ctx.lineTo(hoverPosition, canvas.height); + ctx.stroke(); + } + // draw position marker if (position == null) { @@ -140,7 +157,7 @@ export const HudCanvas: React.FC = ({ ctx.lineTo(position, canvas.height); ctx.stroke(); }); - }, [selection, newSelection, position]); + }, [selection, newSelection, position, hoverPosition]); // handlers @@ -191,7 +208,7 @@ export const HudCanvas: React.FC = ({ moveOffsetX.current = x; } else if (isHoveringSelection(x)) { setMode(Mode.Dragging); - setCursor('cursor-pointer'); + setCursor('cursor-move'); moveOffsetX.current = x; } else { setMode(Mode.Selecting); @@ -203,6 +220,7 @@ export const HudCanvas: React.FC = ({ const handleMouseMove = (evt: MouseEvent) => { const x = getCanvasX(evt); + setHoverPosition(x); switch (mode) { case Mode.Normal: { @@ -214,7 +232,7 @@ export const HudCanvas: React.FC = ({ setCursor('cursor-col-resize'); } else if (isHoveringSelection(x)) { setHoverState(HoverState.OverSelection); - setCursor('cursor-pointer'); + setCursor('cursor-move'); } else { setCursor('cursor-auto'); } @@ -299,6 +317,7 @@ export const HudCanvas: React.FC = ({ const handleMouseLeave = (_evt: MouseEvent) => { setHoverState(HoverState.Normal); + setHoverPosition(0); }; return ( diff --git a/frontend/src/Overview.tsx b/frontend/src/Overview.tsx index 1e578ad..6c4df41 100644 --- a/frontend/src/Overview.tsx +++ b/frontend/src/Overview.tsx @@ -73,6 +73,7 @@ export const Overview: React.FC = ({ borderStrokeStyle: 'red', positionLineWidth: 4, positionStrokeStyle: 'red', + hoverPositionStrokeStyle: 'transparent', }; return ( diff --git a/frontend/src/Waveform.tsx b/frontend/src/Waveform.tsx index 00d60a2..41c5178 100644 --- a/frontend/src/Waveform.tsx +++ b/frontend/src/Waveform.tsx @@ -120,6 +120,7 @@ export const Waveform: React.FC = ({ borderStrokeStyle: 'transparent', positionLineWidth: 6, positionStrokeStyle: 'red', + hoverPositionStrokeStyle: '#666666', }; return (