import { ref, watch } from 'vue'; import { InteractionType } from '@3cr/types-ts'; import { useDataOverlayStore } from '@/stores/data-overlay.store'; import { useMcadStore } from '@/stores/mcad.store'; import { storeToRefs } from 'pinia'; import { DataOverlay, DataOverlayEvent, McadObjectInteraction, } from '@3cr/viewer-types-ts'; import { useEventListener } from '@vueuse/core'; /** * Displays an information modal when clicking on an annotation, measurement, or MCAD object. */ export function useDataOverlayTool() { const { annotationEvent, measurementEvent } = storeToRefs( useDataOverlayStore(), ); const { mcadEvent } = storeToRefs(useMcadStore()); const _flag = ref(false); const _focused = ref(null); const isActive = ref(false); const selected = ref(null); useEventListener(document.body, 'mousedown', onMouseDown); useEventListener(document.body, 'mousemove', onMouseMove); useEventListener(document.body, 'mouseup', onMouseUp); watch(annotationEvent, onInteraction); watch(measurementEvent, onInteraction); watch(mcadEvent, onMcadInteraction); function onMouseDown(event: MouseEvent): void { if (isActive.value && event.button === 0) { _flag.value = true; } } function onMouseMove(): void { if (isActive.value) { _flag.value = false; } } function onMouseUp(event: MouseEvent): void { if (isActive.value && event.button === 0 && _flag.value) { selected.value = _focused.value; } } function onInteraction(event: DataOverlayEvent | null): void { if (isActive.value) { if (event?.Data.Interaction === InteractionType.HOVER_START) { _focused.value = event.Data.DataOverlay; } else if (event?.Data.Interaction === InteractionType.HOVER_END) { _focused.value = null; } } } function onMcadInteraction(event: McadObjectInteraction | null): void { if (isActive.value) { if (event?.Interaction === InteractionType.HOVER_START) { _focused.value = event.Mcad; } else if (event?.Interaction === InteractionType.HOVER_END) { _focused.value = null; } } } return { isActive, selected }; }