|
| 1 | +import macro from '@kitware/vtk.js/macro'; |
| 2 | +import vtkMouseRangeManipulator from '@kitware/vtk.js/Interaction/Manipulators/MouseRangeManipulator'; |
| 3 | + |
| 4 | +function vtkGatedMouseRangeManipulator(publicAPI, model) { |
| 5 | + model.classHierarchy.push('vtkGatedMouseRangeManipulator'); |
| 6 | + |
| 7 | + model.gateEnabled = true; |
| 8 | + |
| 9 | + model.mouseMoveListener = null; |
| 10 | + model.viewContainer = null; |
| 11 | + model.newInteraction = true; |
| 12 | + |
| 13 | + const superOnMouseMove = publicAPI.onMouseMove; |
| 14 | + |
| 15 | + publicAPI.onMouseMove = (interactor, renderer, position) => { |
| 16 | + if (!model.gateEnabled) { |
| 17 | + return; |
| 18 | + } |
| 19 | + superOnMouseMove(interactor, renderer, position); |
| 20 | + }; |
| 21 | + |
| 22 | + publicAPI.setGateEnabled = (enabled) => { |
| 23 | + model.newInteraction = !model.gateEnabled && enabled; |
| 24 | + model.gateEnabled = enabled; |
| 25 | + publicAPI.modified(); |
| 26 | + }; |
| 27 | + |
| 28 | + // Define the mouse move listener function outside initializeGlobalMouseMove |
| 29 | + const createMouseMoveListener = (interactor, renderer) => { |
| 30 | + return (event) => { |
| 31 | + // Proceed if the mouse is inside the container. |
| 32 | + if (!model.viewContainer.contains(event.target)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + // Map the event client coordinates to the container's coordinate system. |
| 36 | + const rect = model.viewContainer.getBoundingClientRect(); |
| 37 | + const position = { |
| 38 | + x: event.clientX - rect.left, |
| 39 | + y: event.clientY - rect.top, |
| 40 | + }; |
| 41 | + |
| 42 | + if (model.newInteraction) { |
| 43 | + model.newInteraction = false; |
| 44 | + publicAPI.onButtonDown(interactor, renderer, position); |
| 45 | + } |
| 46 | + publicAPI.onMouseMove(interactor, renderer, position); |
| 47 | + }; |
| 48 | + }; |
| 49 | + |
| 50 | + // Initializes a global mousemove listener on the view container. |
| 51 | + // Only events occurring over the container will trigger onMouseMove. |
| 52 | + publicAPI.initializeGlobalMouseMove = (interactor, renderer, container) => { |
| 53 | + model.viewContainer = container; |
| 54 | + if (!model.mouseMoveListener) { |
| 55 | + model.mouseMoveListener = createMouseMoveListener(interactor, renderer); |
| 56 | + model.viewContainer.addEventListener( |
| 57 | + 'mousemove', |
| 58 | + model.mouseMoveListener |
| 59 | + ); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + publicAPI.delete = macro.chain(publicAPI.delete, () => { |
| 64 | + if (model.mouseMoveListener && model.viewContainer) { |
| 65 | + model.viewContainer.removeEventListener( |
| 66 | + 'mousemove', |
| 67 | + model.mouseMoveListener |
| 68 | + ); |
| 69 | + model.mouseMoveListener = null; |
| 70 | + } |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +function extend(publicAPI, model, initialValues = {}) { |
| 75 | + vtkMouseRangeManipulator.extend(publicAPI, model, initialValues); |
| 76 | + Object.assign(model, { |
| 77 | + gateEnabled: true, |
| 78 | + mouseMoveListener: null, |
| 79 | + viewContainer: null, |
| 80 | + }); |
| 81 | + vtkGatedMouseRangeManipulator(publicAPI, model); |
| 82 | +} |
| 83 | + |
| 84 | +export const newInstance = macro.newInstance( |
| 85 | + extend, |
| 86 | + 'vtkGatedMouseRangeManipulator' |
| 87 | +); |
| 88 | + |
| 89 | +export default { newInstance, extend }; |
0 commit comments