Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(native): implement native EventTarget methods #3252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 86 additions & 16 deletions packages/fiber/src/native/Canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import * as React from 'react'
import * as THREE from 'three'
import { View, ViewProps, ViewStyle, LayoutChangeEvent, StyleSheet, PixelRatio } from 'react-native'
import {
View,
type ViewProps,
type ViewStyle,
type GestureResponderHandlers,
type GestureResponderEvent,
PanResponder,
type LayoutChangeEvent,
StyleSheet,
PixelRatio,
} from 'react-native'
import { ExpoWebGLRenderingContext, GLView } from 'expo-gl'
import { useContextBridge, FiberProvider } from 'its-fine'
import { SetBlock, Block, ErrorBoundary, useMutableCallback } from '../core/utils'
Expand Down Expand Up @@ -51,7 +61,7 @@ const CanvasImpl = /*#__PURE__*/ React.forwardRef<View, Props>(

const [{ width, height, top, left }, setSize] = React.useState<Size>({ width: 0, height: 0, top: 0, left: 0 })
const [canvas, setCanvas] = React.useState<HTMLCanvasElement | null>(null)
const [bind, setBind] = React.useState<any>()
const [bind, setBind] = React.useState<GestureResponderHandlers>()
React.useImperativeHandle(forwardedRef, () => viewRef.current)

const handlePointerMissed = useMutableCallback(onPointerMissed)
Expand All @@ -76,21 +86,79 @@ const CanvasImpl = /*#__PURE__*/ React.forwardRef<View, Props>(
// Called on context create or swap
// https://github.com/pmndrs/react-three-fiber/pull/2297
const onContextCreate = React.useCallback((context: ExpoWebGLRenderingContext) => {
const canvasShim = {
const listeners = new Map<string, EventListener[]>()

const canvas = {
width: context.drawingBufferWidth,
height: context.drawingBufferHeight,
style: {},
addEventListener: (() => {}) as any,
removeEventListener: (() => {}) as any,
clientWidth: context.drawingBufferWidth,
clientHeight: context.drawingBufferHeight,
getContext: ((_: any, { antialias = false }) => {
getContext: (_: any, { antialias = false }) => {
setAntialias(antialias)
return context
}) as any,
} as HTMLCanvasElement
},
addEventListener(type: string, listener: EventListener) {
let callbacks = listeners.get(type)
if (!callbacks) {
callbacks = []
listeners.set(type, callbacks)
}

callbacks.push(listener)
},
removeEventListener(type: string, listener: EventListener) {
const callbacks = listeners.get(type)
if (callbacks) {
const index = callbacks.indexOf(listener)
if (index !== -1) callbacks.splice(index, 1)
}
},
dispatchEvent(event: Event) {
Object.assign(event, { target: this })

const callbacks = listeners.get(event.type)
if (callbacks) {
for (const callback of callbacks) {
callback(event)
}
}
},
} as unknown as HTMLCanvasElement

root.current = createRoot<HTMLCanvasElement>(canvasShim)
setCanvas(canvasShim)
root.current = createRoot<HTMLCanvasElement>(canvas)
setCanvas(canvas)

function handleTouch(gestureEvent: GestureResponderEvent, type: string): true {
gestureEvent.persist()

canvas.dispatchEvent(
Object.assign(gestureEvent.nativeEvent, {
type,
offsetX: gestureEvent.nativeEvent.locationX,
offsetY: gestureEvent.nativeEvent.locationY,
}) as unknown as Event,
)

return true
}

const responder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onMoveShouldSetPanResponder: () => true,
onMoveShouldSetPanResponderCapture: () => true,
onPanResponderTerminationRequest: () => true,
onStartShouldSetPanResponderCapture: (e) => handleTouch(e, 'onPointerCapture'),
onPanResponderStart: (e) => handleTouch(e, 'onPointerDown'),
onPanResponderMove: (e) => handleTouch(e, 'onPointerMove'),
onPanResponderEnd: (e, state) => {
handleTouch(e, 'onPointerUp')
if (Math.hypot(state.dx, state.dy) < 20) handleTouch(e, 'onClick')
},
onPanResponderRelease: (e) => handleTouch(e, 'onPointerLeave'),
onPanResponderTerminate: (e) => handleTouch(e, 'onLostPointerCapture'),
onPanResponderReject: (e) => handleTouch(e, 'onLostPointerCapture'),
})
setBind(responder.panHandlers)
}, [])

if (root.current && width > 0 && height > 0) {
Expand All @@ -115,9 +183,6 @@ const CanvasImpl = /*#__PURE__*/ React.forwardRef<View, Props>(
onPointerMissed: (...args) => handlePointerMissed.current?.(...args),
// Overwrite onCreated to apply RN bindings
onCreated: (state: RootState) => {
// Bind events after creation
setBind(state.events.handlers)

// Bind render to RN bridge
const context = state.gl.getContext() as ExpoWebGLRenderingContext
const renderFrame = state.gl.render.bind(state.gl)
Expand Down Expand Up @@ -145,9 +210,14 @@ const CanvasImpl = /*#__PURE__*/ React.forwardRef<View, Props>(
}, [canvas])

return (
<View {...props} ref={viewRef} onLayout={onLayout} style={{ flex: 1, ...style }} {...bind}>
<View {...props} ref={viewRef} onLayout={onLayout} style={{ flex: 1, ...style }}>
Comment on lines -148 to +213
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should allow props to be spread from libraries like https://github.com/TiagoCavalcante/r3f-native-orbitcontrols which also use responder props.

{width > 0 && (
<GLView msaaSamples={antialias ? 4 : 0} onContextCreate={onContextCreate} style={StyleSheet.absoluteFill} />
<GLView
{...bind}
msaaSamples={antialias ? 4 : 0}
onContextCreate={onContextCreate}
style={StyleSheet.absoluteFill}
/>
)}
</View>
)
Expand Down