2024-03-02 08:20:50 +11:00
|
|
|
import { MouseEventHandler, useEffect, useRef, useState } from 'react'
|
|
|
|
import { getNormalisedCoordinates } from '../lib/utils'
|
2023-08-10 16:22:45 -04:00
|
|
|
import Loading from './Loading'
|
2024-03-11 20:26:13 -04:00
|
|
|
import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext'
|
2023-10-11 13:36:54 +11:00
|
|
|
import { useModelingContext } from 'hooks/useModelingContext'
|
2024-06-04 08:32:24 -04:00
|
|
|
import { useNetworkContext } from 'hooks/useNetworkContext'
|
|
|
|
import { NetworkHealthState } from 'hooks/useNetworkStatus'
|
2024-02-14 08:03:20 +11:00
|
|
|
import { ClientSideScene } from 'clientSideScene/ClientSideSceneComp'
|
2024-03-22 10:23:04 +11:00
|
|
|
import { butName } from 'lib/cameraControls'
|
|
|
|
import { sendSelectEventToEngine } from 'lib/selections'
|
2024-07-04 01:40:45 -04:00
|
|
|
import { kclManager } from 'lib/singletons'
|
2023-03-06 20:13:34 +11:00
|
|
|
|
2024-07-02 17:16:27 +10:00
|
|
|
export const Stream = () => {
|
2023-08-10 16:22:45 -04:00
|
|
|
const [isLoading, setIsLoading] = useState(true)
|
2024-07-04 01:40:45 -04:00
|
|
|
const [isFirstRender, setIsFirstRender] = useState(kclManager.isFirstRender)
|
2023-09-06 01:32:53 -04:00
|
|
|
const [clickCoords, setClickCoords] = useState<{ x: number; y: number }>()
|
2023-03-06 20:13:34 +11:00
|
|
|
const videoRef = useRef<HTMLVideoElement>(null)
|
2024-03-11 20:26:13 -04:00
|
|
|
const { settings } = useSettingsAuthContext()
|
2024-07-02 17:16:27 +10:00
|
|
|
const { state, send, context } = useModelingContext()
|
2024-06-04 08:32:24 -04:00
|
|
|
const { overallState } = useNetworkContext()
|
|
|
|
|
|
|
|
const isNetworkOkay =
|
|
|
|
overallState === NetworkHealthState.Ok ||
|
|
|
|
overallState === NetworkHealthState.Weak
|
2023-03-06 20:13:34 +11:00
|
|
|
|
2024-06-04 14:36:34 -04:00
|
|
|
// Linux has a default behavior to paste text on middle mouse up
|
|
|
|
// This adds a listener to block that pasting if the click target
|
|
|
|
// is not a text input, so users can move in the 3D scene with
|
|
|
|
// middle mouse drag with a text input focused without pasting.
|
|
|
|
useEffect(() => {
|
|
|
|
const handlePaste = (e: ClipboardEvent) => {
|
|
|
|
const isHtmlElement = e.target && e.target instanceof HTMLElement
|
|
|
|
const isEditable =
|
|
|
|
(isHtmlElement && !('explicitOriginalTarget' in e)) ||
|
|
|
|
('explicitOriginalTarget' in e &&
|
|
|
|
((e.explicitOriginalTarget as HTMLElement).contentEditable ===
|
|
|
|
'true' ||
|
|
|
|
['INPUT', 'TEXTAREA'].some(
|
|
|
|
(tagName) =>
|
|
|
|
tagName === (e.explicitOriginalTarget as HTMLElement).tagName
|
|
|
|
)))
|
|
|
|
if (!isEditable) {
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
e.stopImmediatePropagation()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
globalThis?.window?.document?.addEventListener('paste', handlePaste, {
|
|
|
|
capture: true,
|
|
|
|
})
|
|
|
|
return () =>
|
|
|
|
globalThis?.window?.document?.removeEventListener('paste', handlePaste, {
|
|
|
|
capture: true,
|
|
|
|
})
|
|
|
|
}, [])
|
|
|
|
|
2024-07-04 01:40:45 -04:00
|
|
|
useEffect(() => {
|
|
|
|
setIsFirstRender(kclManager.isFirstRender)
|
|
|
|
}, [kclManager.isFirstRender])
|
|
|
|
|
2023-03-06 20:13:34 +11:00
|
|
|
useEffect(() => {
|
2023-03-07 15:45:59 +11:00
|
|
|
if (
|
|
|
|
typeof window === 'undefined' ||
|
|
|
|
typeof RTCPeerConnection === 'undefined'
|
|
|
|
)
|
|
|
|
return
|
2023-06-22 16:43:33 +10:00
|
|
|
if (!videoRef.current) return
|
2024-07-02 17:16:27 +10:00
|
|
|
if (!context.store?.mediaStream) return
|
|
|
|
videoRef.current.srcObject = context.store.mediaStream
|
|
|
|
}, [context.store?.mediaStream])
|
2023-03-06 20:13:34 +11:00
|
|
|
|
2024-02-11 12:59:00 +11:00
|
|
|
const handleMouseDown: MouseEventHandler<HTMLDivElement> = (e) => {
|
2024-06-04 08:32:24 -04:00
|
|
|
if (!isNetworkOkay) return
|
2023-06-22 16:43:33 +10:00
|
|
|
if (!videoRef.current) return
|
2024-02-11 12:59:00 +11:00
|
|
|
if (state.matches('Sketch')) return
|
|
|
|
if (state.matches('Sketch no face')) return
|
2024-06-29 10:36:04 -07:00
|
|
|
|
2023-08-09 20:49:10 +10:00
|
|
|
const { x, y } = getNormalisedCoordinates({
|
2023-09-08 10:13:35 -04:00
|
|
|
clientX: e.clientX,
|
|
|
|
clientY: e.clientY,
|
2023-08-09 20:49:10 +10:00
|
|
|
el: videoRef.current,
|
2024-07-02 17:16:27 +10:00
|
|
|
...context.store?.streamDimensions,
|
2023-08-09 20:49:10 +10:00
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
|
2024-07-02 17:16:27 +10:00
|
|
|
send({
|
|
|
|
type: 'Set context',
|
|
|
|
data: {
|
|
|
|
buttonDownInStream: e.button,
|
|
|
|
},
|
|
|
|
})
|
2023-09-06 01:32:53 -04:00
|
|
|
setClickCoords({ x, y })
|
2023-06-22 16:43:33 +10:00
|
|
|
}
|
2023-08-06 21:29:26 -04:00
|
|
|
|
2024-03-22 10:23:04 +11:00
|
|
|
const handleMouseUp: MouseEventHandler<HTMLDivElement> = (e) => {
|
2024-06-04 08:32:24 -04:00
|
|
|
if (!isNetworkOkay) return
|
2023-06-22 16:43:33 +10:00
|
|
|
if (!videoRef.current) return
|
2024-07-02 17:16:27 +10:00
|
|
|
send({
|
|
|
|
type: 'Set context',
|
|
|
|
data: {
|
|
|
|
buttonDownInStream: undefined,
|
|
|
|
},
|
|
|
|
})
|
2024-02-11 12:59:00 +11:00
|
|
|
if (state.matches('Sketch')) return
|
|
|
|
if (state.matches('Sketch no face')) return
|
2023-08-06 21:29:26 -04:00
|
|
|
|
2024-07-02 17:16:27 +10:00
|
|
|
if (!context.store?.didDragInStream && butName(e).left) {
|
|
|
|
sendSelectEventToEngine(
|
|
|
|
e,
|
|
|
|
videoRef.current,
|
|
|
|
context.store?.streamDimensions
|
|
|
|
)
|
2023-10-11 13:36:54 +11:00
|
|
|
}
|
2023-09-14 09:34:37 -04:00
|
|
|
|
2024-07-02 17:16:27 +10:00
|
|
|
send({
|
|
|
|
type: 'Set context',
|
|
|
|
data: {
|
|
|
|
didDragInStream: false,
|
|
|
|
},
|
|
|
|
})
|
2023-09-06 01:32:53 -04:00
|
|
|
setClickCoords(undefined)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleMouseMove: MouseEventHandler<HTMLVideoElement> = (e) => {
|
2024-06-04 08:32:24 -04:00
|
|
|
if (!isNetworkOkay) return
|
2024-02-11 12:59:00 +11:00
|
|
|
if (state.matches('Sketch')) return
|
|
|
|
if (state.matches('Sketch no face')) return
|
2023-09-06 01:32:53 -04:00
|
|
|
if (!clickCoords) return
|
|
|
|
|
|
|
|
const delta =
|
|
|
|
((clickCoords.x - e.clientX) ** 2 + (clickCoords.y - e.clientY) ** 2) **
|
|
|
|
0.5
|
|
|
|
|
2024-07-02 17:16:27 +10:00
|
|
|
if (delta > 5 && !context.store?.didDragInStream) {
|
|
|
|
send({
|
|
|
|
type: 'Set context',
|
|
|
|
data: {
|
|
|
|
didDragInStream: true,
|
|
|
|
},
|
|
|
|
})
|
2023-09-06 01:32:53 -04:00
|
|
|
}
|
2023-06-22 16:43:33 +10:00
|
|
|
}
|
2023-03-06 20:13:34 +11:00
|
|
|
|
|
|
|
return (
|
2024-02-11 12:59:00 +11:00
|
|
|
<div
|
2024-06-18 16:08:41 +10:00
|
|
|
className="absolute inset-0 z-0"
|
2024-06-24 22:39:04 -07:00
|
|
|
id="stream"
|
2024-06-25 13:56:11 -04:00
|
|
|
data-testid="stream"
|
2024-02-11 12:59:00 +11:00
|
|
|
onMouseUp={handleMouseUp}
|
|
|
|
onMouseDown={handleMouseDown}
|
|
|
|
onContextMenu={(e) => e.preventDefault()}
|
|
|
|
onContextMenuCapture={(e) => e.preventDefault()}
|
|
|
|
>
|
2023-06-22 16:43:33 +10:00
|
|
|
<video
|
|
|
|
ref={videoRef}
|
|
|
|
muted
|
|
|
|
autoPlay
|
|
|
|
controls={false}
|
2023-08-10 16:22:45 -04:00
|
|
|
onPlay={() => setIsLoading(false)}
|
2023-09-06 01:32:53 -04:00
|
|
|
onMouseMoveCapture={handleMouseMove}
|
2024-03-23 15:45:55 -07:00
|
|
|
className="w-full cursor-pointer h-full"
|
2023-10-03 15:11:44 -07:00
|
|
|
disablePictureInPicture
|
2024-03-22 10:23:04 +11:00
|
|
|
id="video-stream"
|
2023-06-22 16:43:33 +10:00
|
|
|
/>
|
2024-04-02 10:29:34 -04:00
|
|
|
<ClientSideScene
|
|
|
|
cameraControls={settings.context.modeling.mouseControls.current}
|
|
|
|
/>
|
2024-02-26 21:02:33 +11:00
|
|
|
{!isNetworkOkay && !isLoading && (
|
|
|
|
<div className="text-center absolute inset-0">
|
|
|
|
<Loading>
|
2024-06-04 08:32:24 -04:00
|
|
|
<span data-testid="loading-stream">Stream disconnected...</span>
|
2024-02-26 21:02:33 +11:00
|
|
|
</Loading>
|
|
|
|
</div>
|
|
|
|
)}
|
2024-07-04 01:40:45 -04:00
|
|
|
{(isLoading || isFirstRender) && (
|
|
|
|
<div className="text-center absolute inset-0">
|
2023-11-24 08:59:24 +11:00
|
|
|
<Loading>
|
2024-07-04 01:40:45 -04:00
|
|
|
{!isLoading && isFirstRender ? (
|
|
|
|
<span data-testid="loading-stream">Building scene...</span>
|
|
|
|
) : (
|
|
|
|
<span data-testid="loading-stream">Loading stream...</span>
|
|
|
|
)}
|
2023-11-24 08:59:24 +11:00
|
|
|
</Loading>
|
2023-08-10 16:22:45 -04:00
|
|
|
</div>
|
|
|
|
)}
|
2023-03-06 20:13:34 +11:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|