2023-09-15 04:35:48 -07:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { useStore } from 'useStore'
|
2023-09-25 19:49:53 -07:00
|
|
|
import { engineCommandManager } from '../lang/std/engineConnection'
|
2023-10-11 13:36:54 +11:00
|
|
|
import { useModelingContext } from './useModelingContext'
|
2023-10-14 03:47:46 +11:00
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
2023-10-16 15:29:02 +11:00
|
|
|
import { SourceRange } from 'lang/wasm'
|
2023-09-15 04:35:48 -07:00
|
|
|
|
|
|
|
export function useEngineConnectionSubscriptions() {
|
2023-10-11 13:36:54 +11:00
|
|
|
const { setHighlightRange, highlightRange } = useStore((s) => ({
|
2023-09-15 04:35:48 -07:00
|
|
|
setHighlightRange: s.setHighlightRange,
|
|
|
|
highlightRange: s.highlightRange,
|
|
|
|
}))
|
2023-10-14 03:47:46 +11:00
|
|
|
const { send, context } = useModelingContext()
|
2023-10-16 15:29:02 +11:00
|
|
|
|
|
|
|
interface RangeAndId {
|
|
|
|
id: string
|
|
|
|
range: SourceRange
|
|
|
|
}
|
|
|
|
|
2023-09-15 04:35:48 -07:00
|
|
|
useEffect(() => {
|
|
|
|
if (!engineCommandManager) return
|
|
|
|
|
|
|
|
const unSubHover = engineCommandManager.subscribeToUnreliable({
|
|
|
|
event: 'highlight_set_entity',
|
|
|
|
callback: ({ data }) => {
|
|
|
|
if (data?.entity_id) {
|
|
|
|
const sourceRange =
|
2023-10-14 03:47:46 +11:00
|
|
|
engineCommandManager.artifactMap?.[data.entity_id]?.range
|
2023-09-15 04:35:48 -07:00
|
|
|
setHighlightRange(sourceRange)
|
|
|
|
} else if (
|
|
|
|
!highlightRange ||
|
|
|
|
(highlightRange[0] !== 0 && highlightRange[1] !== 0)
|
|
|
|
) {
|
|
|
|
setHighlightRange([0, 0])
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
const unSubClick = engineCommandManager.subscribeTo({
|
|
|
|
event: 'select_with_point',
|
|
|
|
callback: ({ data }) => {
|
|
|
|
if (!data?.entity_id) {
|
2023-10-11 13:36:54 +11:00
|
|
|
send({
|
|
|
|
type: 'Set selection',
|
|
|
|
data: { selectionType: 'singleCodeCursor' },
|
|
|
|
})
|
2023-09-15 04:35:48 -07:00
|
|
|
return
|
|
|
|
}
|
2023-10-14 03:47:46 +11:00
|
|
|
const sourceRange =
|
|
|
|
engineCommandManager.artifactMap[data.entity_id]?.range
|
|
|
|
if (engineCommandManager.artifactMap[data.entity_id]) {
|
|
|
|
send({
|
|
|
|
type: 'Set selection',
|
|
|
|
data: {
|
|
|
|
selectionType: 'singleCodeCursor',
|
|
|
|
selection: { range: sourceRange, type: 'default' },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// selected a vertex
|
|
|
|
engineCommandManager
|
|
|
|
.sendSceneCommand({
|
|
|
|
type: 'modeling_cmd_req',
|
|
|
|
cmd_id: uuidv4(),
|
|
|
|
cmd: {
|
|
|
|
type: 'path_get_curve_uuids_for_vertices',
|
|
|
|
vertex_ids: [data.entity_id],
|
|
|
|
path_id: context.sketchEnginePathId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
const curveIds = res?.data?.data?.curve_ids
|
2023-10-16 15:29:02 +11:00
|
|
|
const ranges: RangeAndId[] = curveIds
|
2023-10-14 03:47:46 +11:00
|
|
|
.map(
|
2023-10-16 15:29:02 +11:00
|
|
|
(id: string): RangeAndId => ({
|
|
|
|
id,
|
|
|
|
range: engineCommandManager.artifactMap[id].range,
|
|
|
|
})
|
2023-10-14 03:47:46 +11:00
|
|
|
)
|
2023-10-16 15:29:02 +11:00
|
|
|
.sort((a: RangeAndId, b: RangeAndId) => a.range[0] - b.range[0])
|
2023-10-14 03:47:46 +11:00
|
|
|
// default to the head of the curve selected
|
2023-10-16 15:29:02 +11:00
|
|
|
const _sourceRange = ranges?.[0].range
|
|
|
|
const artifact = engineCommandManager.artifactMap[ranges?.[0]?.id]
|
|
|
|
if (artifact.type === 'result') {
|
|
|
|
artifact.headVertexId = data.entity_id
|
|
|
|
}
|
2023-10-14 03:47:46 +11:00
|
|
|
send({
|
|
|
|
type: 'Set selection',
|
|
|
|
data: {
|
|
|
|
selectionType: 'singleCodeCursor',
|
2023-10-16 15:29:02 +11:00
|
|
|
// line-end is used to indicate that headVertexId should be sent to the engine as "selected"
|
|
|
|
// not the whole curve
|
2023-10-14 03:47:46 +11:00
|
|
|
selection: { range: _sourceRange, type: 'line-end' },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2023-09-15 04:35:48 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return () => {
|
|
|
|
unSubHover()
|
|
|
|
unSubClick()
|
|
|
|
}
|
2023-10-14 03:47:46 +11:00
|
|
|
}, [
|
|
|
|
engineCommandManager,
|
|
|
|
setHighlightRange,
|
|
|
|
highlightRange,
|
|
|
|
context.sketchEnginePathId,
|
|
|
|
])
|
2023-09-15 04:35:48 -07:00
|
|
|
}
|