2024-07-17 00:26:14 -04:00
|
|
|
import { useLayoutEffect, useEffect, useRef } from 'react'
|
2024-03-22 16:55:30 +11:00
|
|
|
import { engineCommandManager, kclManager } from 'lib/singletons'
|
2023-09-25 19:49:53 -07:00
|
|
|
import { deferExecution } from 'lib/utils'
|
2024-03-22 09:35:07 -04:00
|
|
|
import { Themes } from 'lib/theme'
|
2024-06-30 19:21:24 -07:00
|
|
|
import { makeDefaultPlanes, modifyGrid } from 'lang/wasm'
|
2024-07-02 17:16:27 +10:00
|
|
|
import { useModelingContext } from './useModelingContext'
|
2024-08-07 03:11:57 -04:00
|
|
|
import { useNetworkContext } from 'hooks/useNetworkContext'
|
2024-07-12 10:57:27 +10:00
|
|
|
import { useAppState, useAppStream } from 'AppState'
|
2024-08-07 03:11:57 -04:00
|
|
|
import { SettingsViaQueryString } from 'lib/settings/settingsTypes'
|
|
|
|
import {
|
|
|
|
EngineConnectionStateType,
|
|
|
|
DisconnectingType,
|
|
|
|
} from 'lang/std/engineConnection'
|
2023-09-14 10:47:55 +10:00
|
|
|
|
|
|
|
export function useSetupEngineManager(
|
|
|
|
streamRef: React.RefObject<HTMLDivElement>,
|
2024-08-07 03:11:57 -04:00
|
|
|
modelingSend: ReturnType<typeof useModelingContext>['send'],
|
|
|
|
modelingContext: ReturnType<typeof useModelingContext>['context'],
|
2024-04-19 00:58:32 -04:00
|
|
|
settings = {
|
2024-04-25 15:51:33 -04:00
|
|
|
pool: null,
|
2024-04-19 00:58:32 -04:00
|
|
|
theme: Themes.System,
|
|
|
|
highlightEdges: true,
|
2024-04-24 13:59:25 -07:00
|
|
|
enableSSAO: true,
|
2024-07-01 15:31:42 -04:00
|
|
|
showScaleGrid: false,
|
2024-08-07 03:11:57 -04:00
|
|
|
} as SettingsViaQueryString,
|
|
|
|
token?: string
|
2023-09-14 10:47:55 +10:00
|
|
|
) {
|
2024-08-07 03:11:57 -04:00
|
|
|
const networkContext = useNetworkContext()
|
|
|
|
const { pingPongHealth, immediateState } = networkContext
|
2024-07-03 09:22:46 +10:00
|
|
|
const { setAppState } = useAppState()
|
2024-07-12 10:57:27 +10:00
|
|
|
const { setMediaStream } = useAppStream()
|
2023-09-14 10:47:55 +10:00
|
|
|
|
2023-09-25 19:49:53 -07:00
|
|
|
const hasSetNonZeroDimensions = useRef<boolean>(false)
|
2023-09-14 10:47:55 +10:00
|
|
|
|
2024-04-25 15:51:33 -04:00
|
|
|
if (settings.pool) {
|
|
|
|
// override the pool param (?pool=) to request a specific engine instance
|
|
|
|
// from a particular pool.
|
2024-08-07 03:11:57 -04:00
|
|
|
engineCommandManager.settings.pool = settings.pool
|
2024-04-25 15:51:33 -04:00
|
|
|
}
|
|
|
|
|
2024-08-07 03:11:57 -04:00
|
|
|
const startEngineInstance = () => {
|
2023-09-25 19:49:53 -07:00
|
|
|
// Load the engine command manager once with the initial width and height,
|
|
|
|
// then we do not want to reload it.
|
|
|
|
const { width: quadWidth, height: quadHeight } = getDimensions(
|
2024-07-12 16:42:23 -04:00
|
|
|
streamRef?.current?.offsetWidth ?? 0,
|
|
|
|
streamRef?.current?.offsetHeight ?? 0
|
2023-09-25 19:49:53 -07:00
|
|
|
)
|
2024-07-12 16:42:23 -04:00
|
|
|
engineCommandManager.start({
|
|
|
|
setMediaStream: (mediaStream) => setMediaStream(mediaStream),
|
|
|
|
setIsStreamReady: (isStreamReady) => setAppState({ isStreamReady }),
|
|
|
|
width: quadWidth,
|
|
|
|
height: quadHeight,
|
|
|
|
token,
|
|
|
|
settings,
|
|
|
|
makeDefaultPlanes: () => {
|
|
|
|
return makeDefaultPlanes(kclManager.engineCommandManager)
|
|
|
|
},
|
|
|
|
modifyGrid: (hidden: boolean) => {
|
|
|
|
return modifyGrid(kclManager.engineCommandManager, hidden)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
hasSetNonZeroDimensions.current = true
|
2024-06-04 08:32:24 -04:00
|
|
|
}
|
|
|
|
|
2024-07-12 16:42:23 -04:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
const { width: quadWidth, height: quadHeight } = getDimensions(
|
|
|
|
streamRef?.current?.offsetWidth ?? 0,
|
|
|
|
streamRef?.current?.offsetHeight ?? 0
|
|
|
|
)
|
|
|
|
if (!hasSetNonZeroDimensions.current && quadHeight && quadWidth) {
|
|
|
|
startEngineInstance()
|
|
|
|
}
|
|
|
|
}, [
|
2024-06-04 08:32:24 -04:00
|
|
|
streamRef?.current?.offsetWidth,
|
|
|
|
streamRef?.current?.offsetHeight,
|
2024-08-07 03:11:57 -04:00
|
|
|
modelingSend,
|
2024-06-04 08:32:24 -04:00
|
|
|
])
|
2023-09-25 19:49:53 -07:00
|
|
|
|
2024-08-07 03:11:57 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (pingPongHealth === 'TIMEOUT') {
|
|
|
|
engineCommandManager.tearDown()
|
|
|
|
}
|
|
|
|
}, [pingPongHealth])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const intervalId = setInterval(() => {
|
|
|
|
if (immediateState.type === EngineConnectionStateType.Disconnected) {
|
|
|
|
engineCommandManager.engineConnection = undefined
|
|
|
|
startEngineInstance()
|
|
|
|
}
|
|
|
|
}, 3000)
|
|
|
|
return () => {
|
|
|
|
clearInterval(intervalId)
|
|
|
|
}
|
|
|
|
}, [immediateState])
|
|
|
|
|
2023-09-25 19:49:53 -07:00
|
|
|
useEffect(() => {
|
2024-08-16 07:15:42 -04:00
|
|
|
engineCommandManager.settings = settings
|
2024-08-07 20:44:33 -04:00
|
|
|
|
2023-09-25 19:49:53 -07:00
|
|
|
const handleResize = deferExecution(() => {
|
|
|
|
const { width, height } = getDimensions(
|
2024-07-12 16:42:23 -04:00
|
|
|
streamRef?.current?.offsetWidth ?? 0,
|
|
|
|
streamRef?.current?.offsetHeight ?? 0
|
2023-09-25 19:49:53 -07:00
|
|
|
)
|
2024-09-12 22:06:50 -04:00
|
|
|
engineCommandManager.handleResize({
|
|
|
|
streamWidth: width,
|
|
|
|
streamHeight: height,
|
|
|
|
})
|
2023-09-25 19:49:53 -07:00
|
|
|
}, 500)
|
|
|
|
|
2024-06-04 08:32:24 -04:00
|
|
|
const onOnline = () => {
|
2024-08-07 03:11:57 -04:00
|
|
|
startEngineInstance()
|
2024-07-12 16:42:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const onVisibilityChange = () => {
|
|
|
|
if (window.document.visibilityState === 'visible') {
|
|
|
|
if (
|
|
|
|
!engineCommandManager.engineConnection?.isReady() &&
|
|
|
|
!engineCommandManager.engineConnection?.isConnecting()
|
|
|
|
) {
|
|
|
|
startEngineInstance()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.document.addEventListener('visibilitychange', onVisibilityChange)
|
|
|
|
|
|
|
|
const onAnyInput = () => {
|
2024-08-07 03:11:57 -04:00
|
|
|
const isEngineNotReadyOrConnecting =
|
2024-07-12 16:42:23 -04:00
|
|
|
!engineCommandManager.engineConnection?.isReady() &&
|
|
|
|
!engineCommandManager.engineConnection?.isConnecting()
|
2024-08-07 03:11:57 -04:00
|
|
|
|
|
|
|
const conn = engineCommandManager.engineConnection
|
|
|
|
|
|
|
|
const isStreamPaused =
|
|
|
|
conn?.state.type === EngineConnectionStateType.Disconnecting &&
|
|
|
|
conn?.state.value.type === DisconnectingType.Pause
|
|
|
|
|
|
|
|
if (isEngineNotReadyOrConnecting || isStreamPaused) {
|
|
|
|
engineCommandManager.engineConnection = undefined
|
2024-07-12 16:42:23 -04:00
|
|
|
startEngineInstance()
|
|
|
|
}
|
2024-06-04 08:32:24 -04:00
|
|
|
}
|
2024-07-12 16:42:23 -04:00
|
|
|
window.document.addEventListener('keydown', onAnyInput)
|
|
|
|
window.document.addEventListener('mousemove', onAnyInput)
|
|
|
|
window.document.addEventListener('mousedown', onAnyInput)
|
|
|
|
window.document.addEventListener('scroll', onAnyInput)
|
|
|
|
window.document.addEventListener('touchstart', onAnyInput)
|
2024-06-04 08:32:24 -04:00
|
|
|
|
|
|
|
const onOffline = () => {
|
|
|
|
engineCommandManager.tearDown()
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('online', onOnline)
|
|
|
|
window.addEventListener('offline', onOffline)
|
2023-09-25 19:49:53 -07:00
|
|
|
window.addEventListener('resize', handleResize)
|
2023-09-14 10:47:55 +10:00
|
|
|
return () => {
|
2024-07-12 16:42:23 -04:00
|
|
|
window.document.removeEventListener(
|
|
|
|
'visibilitychange',
|
|
|
|
onVisibilityChange
|
|
|
|
)
|
|
|
|
window.document.removeEventListener('keydown', onAnyInput)
|
|
|
|
window.document.removeEventListener('mousemove', onAnyInput)
|
|
|
|
window.document.removeEventListener('mousedown', onAnyInput)
|
|
|
|
window.document.removeEventListener('scroll', onAnyInput)
|
|
|
|
window.document.removeEventListener('touchstart', onAnyInput)
|
2024-06-04 08:32:24 -04:00
|
|
|
window.removeEventListener('online', onOnline)
|
|
|
|
window.removeEventListener('offline', onOffline)
|
2023-09-25 19:49:53 -07:00
|
|
|
window.removeEventListener('resize', handleResize)
|
2023-09-14 10:47:55 +10:00
|
|
|
}
|
2024-07-12 16:42:23 -04:00
|
|
|
|
|
|
|
// Engine relies on many settings so we should rebind events when it changes
|
|
|
|
// We have to list out the ones we care about because the settings object holds
|
|
|
|
// non-settings too...
|
2024-08-16 07:15:42 -04:00
|
|
|
}, [...Object.values(settings)])
|
2023-09-25 19:49:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function getDimensions(streamWidth?: number, streamHeight?: number) {
|
2024-08-01 22:30:08 +10:00
|
|
|
const factorOf = 4
|
2023-11-13 19:32:07 -05:00
|
|
|
const maxResolution = 2000
|
2023-09-25 19:49:53 -07:00
|
|
|
const width = streamWidth ? streamWidth : 0
|
|
|
|
const height = streamHeight ? streamHeight : 0
|
2023-11-13 19:32:07 -05:00
|
|
|
const ratio = Math.min(
|
|
|
|
Math.min(maxResolution / width, maxResolution / height),
|
|
|
|
1.0
|
|
|
|
)
|
2024-08-01 22:30:08 +10:00
|
|
|
const quadWidth = Math.round((width * ratio) / factorOf) * factorOf
|
|
|
|
const quadHeight = Math.round((height * ratio) / factorOf) * factorOf
|
2023-09-25 19:49:53 -07:00
|
|
|
return { width: quadWidth, height: quadHeight }
|
2023-09-14 10:47:55 +10:00
|
|
|
}
|