import { useEffect, useMemo, useState } from "react";
import { StyleSheet } from "react-native";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
import Animated, {
cancelAnimation,
runOnJS,
useAnimatedReaction,
useAnimatedStyle,
useSharedValue,
withDecay,
withTiming,
} from "react-native-reanimated";
import type { SharedValue } from "react-native-reanimated";
import { View } from "tamagui";
import { GraphDots } from "./GraphDots";
import type { GraphViewportProps } from "./GraphViewport";
import {
clampOffset,
clampView,
dotPhase,
dotPitch,
graphZoom,
toggledView,
zoomAt,
} from "./viewMath";
import type { Extent, GraphDots as GraphDotsSpec } from "./viewMath";
export type { GraphViewportProps } from "./GraphViewport";
export function GraphViewport({ content, viewport, dots, frozen, children }: GraphViewportProps) {
const start = clampView({ scale: 1, x: 0, y: 0 }, content, viewport);
const scale = useSharedValue(start.scale);
const x = useSharedValue(start.x);
const y = useSharedValue(start.y);
const pinching = useSharedValue(0);
const focalX = useSharedValue(0);
const focalY = useSharedValue(0);
const held = useSharedValue(frozen ? 1 : 0);
const { width: cw, height: ch } = content;
const { width: vw, height: vh } = viewport;
useEffect(() => {
const next = clampView(
{ scale: scale.value, x: x.value, y: y.value },
{ width: cw, height: ch },
{ width: vw, height: vh },
);
scale.value = next.scale;
x.value = next.x;
y.value = next.y;
}, [cw, ch, vw, vh, scale, x, y]);
useEffect(() => {
held.value = frozen ? 1 : 0;
}, [frozen, held]);
const gesture = useMemo(() => {
const size = { width: cw, height: ch };
const view = { width: vw, height: vh };
const pan = Gesture.Pan()
.maxPointers(1)
.onStart(() => {
"worklet";
if (held.value) return;
cancelAnimation(x);
cancelAnimation(y);
})
.onChange((event) => {
"worklet";
if (pinching.value || held.value) return;
x.value = clampOffset(x.value + event.changeX, cw * scale.value, vw);
y.value = clampOffset(y.value + event.changeY, ch * scale.value, vh);
})
.onEnd((event) => {
"worklet";
if (pinching.value || held.value) return;
const wide = cw * scale.value;
const tall = ch * scale.value;
x.value = withDecay({
velocity: event.velocityX,
clamp: [clampOffset(-Infinity, wide, vw), clampOffset(Infinity, wide, vw)],
});
y.value = withDecay({
velocity: event.velocityY,
clamp: [clampOffset(-Infinity, tall, vh), clampOffset(Infinity, tall, vh)],
});
});
const pinch = Gesture.Pinch()
.onStart((event) => {
"worklet";
pinching.value = 1;
if (held.value) return;
cancelAnimation(x);
cancelAnimation(y);
cancelAnimation(scale);
focalX.value = event.focalX;
focalY.value = event.focalY;
})
.onChange((event) => {
"worklet";
if (held.value) return;
const next = zoomAt(
{
scale: scale.value,
x: x.value + event.focalX - focalX.value,
y: y.value + event.focalY - focalY.value,
},
scale.value * event.scaleChange,
{ x: event.focalX, y: event.focalY },
size,
view,
);
focalX.value = event.focalX;
focalY.value = event.focalY;
scale.value = next.scale;
x.value = next.x;
y.value = next.y;
})
.onFinalize(() => {
"worklet";
pinching.value = 0;
});
const reset = Gesture.Tap()
.numberOfTaps(2)
.onEnd((event, success) => {
"worklet";
if (!success || held.value) return;
const next = toggledView(
{ scale: scale.value, x: x.value, y: y.value },
{ x: event.x, y: event.y },
size,
view,
);
scale.value = withTiming(next.scale, { duration: graphZoom.settleMs });
x.value = withTiming(next.x, { duration: graphZoom.settleMs });
y.value = withTiming(next.y, { duration: graphZoom.settleMs });
});
return Gesture.Race(reset, Gesture.Simultaneous(pinch, pan));
}, [cw, ch, vw, vh, scale, x, y, pinching, focalX, focalY, held]);
const placed = useAnimatedStyle(() => ({
transform: [
{ translateX: x.value - ((1 - scale.value) * cw) / 2 },
{ translateY: y.value - ((1 - scale.value) * ch) / 2 },
{ scale: scale.value },
],
}));
return (
{dots ? : null}
{children}
);
}
function DotLayer({
viewport,
dots,
scale,
x,
y,
}: {
viewport: Extent;
dots: GraphDotsSpec;
scale: SharedValue;
x: SharedValue;
y: SharedValue;
}) {
const [drawn, setDrawn] = useState(() => scale.value);
const spaced = dotPitch(dots.pitch, drawn);
const step = spaced * drawn;
const width = viewport.width / 0.85 + step * 2;
const height = viewport.height / 0.85 + step * 2;
const pitch = dots.pitch;
useAnimatedReaction(
() => scale.value,
(now) => {
if (Math.abs(now / drawn - 1) > 0.1 || dotPitch(pitch, now) !== spaced)
runOnJS(setDrawn)(now);
},
[drawn, spaced, pitch],
);
const placed = useAnimatedStyle(() => {
const ratio = scale.value / drawn;
const shift = (axis: number, offset: number) =>
dotPhase(offset, scale.value, pitch, spaced) - (ratio * step) / 2 - (axis / 2) * (1 - ratio);
return {
transform: [
{ translateX: shift(width, x.value) },
{ translateY: shift(height, y.value) },
{ scale: ratio },
],
};
});
return (
);
}
const styles = StyleSheet.create({
viewport: {
flex: 1,
overflow: "hidden",
},
content: {
position: "absolute",
left: 0,
top: 0,
},
dots: {
position: "absolute",
left: 0,
top: 0,
},
});