diff --git a/Sources/Widgets/Widgets3D/SeedWidget/behavior.js b/Sources/Widgets/Widgets3D/SeedWidget/behavior.js index b23f5197391..b917c6f4460 100644 --- a/Sources/Widgets/Widgets3D/SeedWidget/behavior.js +++ b/Sources/Widgets/Widgets3D/SeedWidget/behavior.js @@ -22,11 +22,23 @@ export default function widgetBehavior(publicAPI, model) { .worldCoords; } - // Update the handle's center. Example: - // handle.setCenter([1,2,3]); publicAPI.setCenter = (newCenter) => { moveHandle.setOrigin(newCenter); + model._interactor.render(); + }; + + publicAPI.startInteract = () => { model._widgetManager.enablePicking(); + publicAPI.grabFocus(); + model._apiSpecificRenderWindow.setCursor('grabbing'); + publicAPI.invokeStartInteractionEvent(); + }; + + publicAPI.endInteract = () => { + publicAPI.loseFocus(); + model._apiSpecificRenderWindow.setCursor('pointer'); + model.widgetState.deactivate(); + publicAPI.invokeEndInteractionEvent(); }; publicAPI.handleLeftButtonPress = (e) => { @@ -38,12 +50,10 @@ export default function widgetBehavior(publicAPI, model) { if (model.activeState === moveHandle) { if (!moveHandle.getOrigin() && worldCoords) { - moveHandle.setOrigin(worldCoords); + publicAPI.setCenter(worldCoords); } } - model._isDragging = true; - model._apiSpecificRenderWindow.setCursor('grabbing'); - publicAPI.invokeStartInteractionEvent(); + publicAPI.startInteract(); return macro.EVENT_ABORT; }; @@ -53,13 +63,8 @@ export default function widgetBehavior(publicAPI, model) { return macro.VOID; } if (isPlaced()) { - model._widgetManager.enablePicking(); - model._apiSpecificRenderWindow.setCursor('pointer'); - model._isDragging = false; - model.activeState = null; - model.widgetState.deactivate(); + publicAPI.endInteract(); } - publicAPI.invokeEndInteractionEvent(); return macro.EVENT_ABORT; }; @@ -78,7 +83,6 @@ export default function widgetBehavior(publicAPI, model) { moveHandle.setVisible(true); model._isDragging = true; model.activeState = moveHandle; - model._interactor.render(); }; publicAPI.loseFocus = () => { diff --git a/Sources/Widgets/Widgets3D/SeedWidget/example/index.js b/Sources/Widgets/Widgets3D/SeedWidget/example/index.js index 99cbb366053..59d778cf5aa 100644 --- a/Sources/Widgets/Widgets3D/SeedWidget/example/index.js +++ b/Sources/Widgets/Widgets3D/SeedWidget/example/index.js @@ -58,8 +58,10 @@ renderer.resetCamera(); fullScreenRenderer.addController(controlPanel); -document.querySelector('#addWidget').addEventListener('click', () => { - widgetManager.releaseFocus(widget); +function createWidget(eventOrCenter = null) { + if (widgetHandle) { + widgetHandle.endInteract(); + } widget = vtkSeedWidget.newInstance(); // Important: set the manipulator of the widget to constrain movement to the actor @@ -77,8 +79,14 @@ document.querySelector('#addWidget').addEventListener('click', () => { // Start placement interaction widget.placeWidget(cone.getOutputData().getBounds()); widgetHandle = widgetManager.addWidget(widget); - widgetManager.grabFocus(widget); -}); + if (Array.isArray(eventOrCenter)) { + widgetHandle.setCenter(eventOrCenter); + } else { + widgetHandle.startInteract(); + } +} + +document.querySelector('#addWidget').addEventListener('click', createWidget); document.querySelector('#removeWidget').addEventListener('click', () => { const widgets = widgetManager.getWidgets(); @@ -86,6 +94,8 @@ document.querySelector('#removeWidget').addEventListener('click', () => { widgetManager.removeWidget(widgets[widgets.length - 1]); }); +createWidget([0.5, 0, 0]); + // ----------------------------------------------------------- // globals // ----------------------------------------------------------- diff --git a/Sources/Widgets/Widgets3D/SeedWidget/index.d.ts b/Sources/Widgets/Widgets3D/SeedWidget/index.d.ts index e73ddc017a1..411595108d8 100644 --- a/Sources/Widgets/Widgets3D/SeedWidget/index.d.ts +++ b/Sources/Widgets/Widgets3D/SeedWidget/index.d.ts @@ -20,9 +20,26 @@ export interface vtkSeedWidgetState { getMoveHandle(): ISeedWidgetHandleState; } -// The type of object returned by vtkWidgetManager.addWidget() +// Object returned by vtkWidgetManager.addWidget(). +// One instance per view. export interface vtkSeedWidgetHandle { + /** + * Place the seed position. + * @param center Vector3 3D position + */ setCenter(center: Vector3): void; + + /** + * Turn the seed widget as interactive. + * @see vtkSeedWidgetHandle.endInteract + */ + startInteract(): void; + + /** + * Stop the seed widget to be interactive. + * @see vtkSeedWidgetHandle.endInteract + */ + endInteract(): void; } export interface vtkSeedWidget {