Skip to content

Commit

Permalink
don't use CanvasMousePositionRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
ruggi committed Jul 2, 2024
1 parent 10ec4b8 commit c6f67c9
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions editor/src/components/canvas/controls/grid-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
import { toFirst } from '../../../core/shared/optics/optic-utilities'
import type { Optic } from '../../../core/shared/optics/optics'
import { assertNever } from '../../../core/shared/utils'
import { CanvasMousePositionRaw } from '../../../utils/global-positions'
import { Modifier } from '../../../utils/modifiers'
import { when } from '../../../utils/react-conditionals'
import { useDispatch } from '../../editor/store/dispatch-context'
Expand Down Expand Up @@ -124,7 +123,9 @@ export const GridControls = controlForStrategyMemoized(() => {
'GridControls activelyDraggingOrResizingCell',
)

const { hoveringCell, hoveringStart } = useHoveringCell(activelyDraggingOrResizingCell)
const { hoveringCell, hoveringStart, mouseCanvasPosition } = useMouseMove(
activelyDraggingOrResizingCell,
)
useSnapAnimation(hoveringCell, controls)

const dragging = useEditorState(
Expand Down Expand Up @@ -303,7 +304,7 @@ export const GridControls = controlForStrategyMemoized(() => {
return features.Grid.opacityBaseline
} else if (features.Grid.dragLockedToCenter) {
return Math.min(
(0.2 * distance(getRectCenter(shadow.globalFrame), CanvasMousePositionRaw!)) /
(0.2 * distance(getRectCenter(shadow.globalFrame), mouseCanvasPosition)) /
Math.min(shadow.globalFrame.height, shadow.globalFrame.width) +
0.05,
features.Grid.opacityBaseline,
Expand All @@ -316,14 +317,14 @@ export const GridControls = controlForStrategyMemoized(() => {
interactionData.dragStart,
pointDifference(initialShadowFrame, shadow.globalFrame),
),
CanvasMousePositionRaw!,
mouseCanvasPosition,
)) /
Math.min(shadow.globalFrame.height, shadow.globalFrame.width) +
0.05,
features.Grid.opacityBaseline,
)
}
}, [features, shadow, initialShadowFrame, interactionData])
}, [features, shadow, initialShadowFrame, interactionData, mouseCanvasPosition])

// NOTE: this stuff is meant to be temporary, until we settle on the set of interaction pieces we like.
// After that, we should get rid of this.
Expand Down Expand Up @@ -352,9 +353,7 @@ export const GridControls = controlForStrategyMemoized(() => {
shadow.globalFrame[dimension] / 2
)
} else if (features.Grid.dragMagnetic) {
return (
shadow.globalFrame[axis] + (CanvasMousePositionRaw![axis] - hoveringStart.point[axis])
)
return shadow.globalFrame[axis] + (mouseCanvasPosition[axis] - hoveringStart.point[axis])
} else if (features.Grid.dragRatio) {
return (
shadow.globalFrame[axis] +
Expand All @@ -369,7 +368,7 @@ export const GridControls = controlForStrategyMemoized(() => {
}

return { x: getCoord('x', 'width'), y: getCoord('y', 'height') }
}, [features, initialShadowFrame, interactionData, shadow, hoveringStart])
}, [features, initialShadowFrame, interactionData, shadow, hoveringStart, mouseCanvasPosition])

if (grids.length === 0) {
return null
Expand Down Expand Up @@ -736,12 +735,27 @@ function useSnapAnimation(hoveringCell: string | null, controls: AnimationContro
}, [hoveringCell, controls, features.Grid.animateSnap])
}

function useHoveringCell(activelyDraggingOrResizingCell: string | null) {
function useMouseMove(activelyDraggingOrResizingCell: string | null) {
const [hoveringCell, setHoveringCell] = React.useState<string | null>(null)
const [hoveringStart, setHoveringStart] = React.useState<{
id: string
point: CanvasPoint
} | null>(null)
const [mouseCanvasPosition, setMouseCanvasPosition] = React.useState<CanvasPoint>(
canvasPoint({ x: 0, y: 0 }),
)

const canvasScale = useEditorState(
Substores.canvasOffset,
(store) => store.editor.canvas.scale,
'useHoveringCell canvasScale',
)

const canvasOffset = useEditorState(
Substores.canvasOffset,
(store) => store.editor.canvas.roundedCanvasOffset,
'useHoveringCell canvasOffset',
)

React.useEffect(() => {
function handleMouseMove(e: MouseEvent) {
Expand All @@ -761,9 +775,16 @@ function useHoveringCell(activelyDraggingOrResizingCell: string | null) {
TargetGridCell.current.column = column == null ? 0 : parseInt(column)
setHoveringCell(cellUnderMouse.id)

const newMouseCanvasPosition = windowToCanvasCoordinates(
canvasScale,
canvasOffset,
windowPoint({ x: e.clientX, y: e.clientY }),
).canvasPositionRaw
setMouseCanvasPosition(newMouseCanvasPosition)

setHoveringStart((start) => {
if (start == null || start.id !== cellUnderMouse.id) {
return { id: cellUnderMouse.id, point: canvasPoint(CanvasMousePositionRaw!) }
return { id: cellUnderMouse.id, point: canvasPoint(newMouseCanvasPosition) }
}
return start
})
Expand All @@ -773,7 +794,7 @@ function useHoveringCell(activelyDraggingOrResizingCell: string | null) {
return function () {
window.removeEventListener('mousemove', handleMouseMove)
}
}, [activelyDraggingOrResizingCell])
}, [activelyDraggingOrResizingCell, canvasOffset, canvasScale])

return { hoveringCell, hoveringStart }
return { hoveringCell, hoveringStart, mouseCanvasPosition }
}

0 comments on commit c6f67c9

Please sign in to comment.