Skip to content

Commit e89af2c

Browse files
committed
fix(widgets): fix behavior of EndInteractionEvent
This commit removes the boolean parameter of `invokeEndInteractionEvent()` and adds a `resetAfterPointPlacement` property to the SplineWidget, to make it behave like the ShapeWidget.
1 parent 974d8ca commit e89af2c

File tree

6 files changed

+73
-47
lines changed

6 files changed

+73
-47
lines changed

Sources/Widgets/Widgets3D/PaintWidget/behavior.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import macro from 'vtk.js/Sources/macros';
22
import { vec3 } from 'gl-matrix';
33

44
export default function widgetBehavior(publicAPI, model) {
5+
model.painting = model.factory.getPainting();
6+
57
publicAPI.handleLeftButtonPress = (callData) => {
68
if (!model.activeState || !model.activeState.getActive()) {
79
return macro.VOID;

Sources/Widgets/Widgets3D/PaintWidget/example/index.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ widgets.circleWidget = vtkEllipseWidget.newInstance({
9999
},
100100
},
101101
});
102-
widgets.splineWidget = vtkSplineWidget.newInstance();
102+
widgets.splineWidget = vtkSplineWidget.newInstance({
103+
resetAfterPointPlacement: true,
104+
});
103105
widgets.polygonWidget = vtkSplineWidget.newInstance({
106+
resetAfterPointPlacement: true,
104107
resolution: 1,
105108
});
106109

@@ -243,18 +246,16 @@ reader
243246
widgets.circleWidget.getWidgetState().getText().setText(text);
244247
});
245248

246-
scene.splineHandle
247-
.getWidgetState()
248-
.getMoveHandle()
249-
.setScale1(2 * Math.max(...image.data.getSpacing()));
249+
scene.splineHandle.setHandleSizeInPixels(
250+
2 * Math.max(...image.data.getSpacing())
251+
);
250252
scene.splineHandle.setFreehandMinDistance(
251253
4 * Math.max(...image.data.getSpacing())
252254
);
253255

254-
scene.polygonHandle
255-
.getWidgetState()
256-
.getMoveHandle()
257-
.setScale1(2 * Math.max(...image.data.getSpacing()));
256+
scene.polygonHandle.setHandleSizeInPixels(
257+
2 * Math.max(...image.data.getSpacing())
258+
);
258259
scene.polygonHandle.setFreehandMinDistance(
259260
4 * Math.max(...image.data.getSpacing())
260261
);
@@ -453,18 +454,14 @@ scene.splineHandle.onEndInteractionEvent(() => {
453454
const points = scene.splineHandle.getPoints();
454455
painter.paintPolygon(points);
455456

456-
scene.splineHandle.reset();
457457
scene.splineHandle.updateRepresentationForRender();
458-
scene.widgetManager.grabFocus(widgets.splineWidget);
459458
});
460459
initializeHandle(scene.splineHandle);
461460

462461
scene.polygonHandle.onEndInteractionEvent(() => {
463462
const points = scene.polygonHandle.getPoints();
464463
painter.paintPolygon(points);
465464

466-
scene.polygonHandle.reset();
467465
scene.polygonHandle.updateRepresentationForRender();
468-
scene.widgetManager.grabFocus(widgets.polygonWidget);
469466
});
470467
initializeHandle(scene.polygonHandle);

Sources/Widgets/Widgets3D/ShapeWidget/behavior.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export default function widgetBehavior(publicAPI, model) {
118118
model.activeState = model.point2Handle;
119119

120120
model.point2Handle.setVisible(true);
121+
model.widgetState.getText().setVisible(true);
121122

122123
publicAPI.updateShapeBounds();
123124

@@ -131,12 +132,6 @@ export default function widgetBehavior(publicAPI, model) {
131132
model.point2Handle.setOrigin(model.point2);
132133

133134
publicAPI.updateShapeBounds();
134-
135-
if (publicAPI.getResetAfterPointPlacement()) {
136-
publicAPI.reset();
137-
} else {
138-
publicAPI.loseFocus();
139-
}
140135
}
141136
};
142137

@@ -526,7 +521,13 @@ export default function widgetBehavior(publicAPI, model) {
526521
} else {
527522
publicAPI.placePoint2(model.point2Handle.getOrigin());
528523
publicAPI.invokeInteractionEvent();
529-
publicAPI.invokeEndInteractionEvent(true);
524+
publicAPI.invokeEndInteractionEvent();
525+
526+
if (publicAPI.getResetAfterPointPlacement()) {
527+
publicAPI.reset();
528+
} else {
529+
publicAPI.loseFocus();
530+
}
530531
}
531532

532533
return macro.EVENT_ABORT;
@@ -558,7 +559,7 @@ export default function widgetBehavior(publicAPI, model) {
558559
model.apiSpecificRenderWindow.setCursor('pointer');
559560
model.widgetState.deactivate();
560561
model.interactor.cancelAnimation(publicAPI);
561-
publicAPI.invokeEndInteractionEvent(true);
562+
publicAPI.invokeEndInteractionEvent();
562563

563564
return macro.EVENT_ABORT;
564565
}
@@ -578,16 +579,15 @@ export default function widgetBehavior(publicAPI, model) {
578579
}
579580

580581
if (model.point1) {
581-
model.point2 = model.point2Handle.getOrigin();
582-
publicAPI.updateShapeBounds();
582+
publicAPI.placePoint2(model.point2Handle.getOrigin());
583583

584584
if (publicAPI.isDraggingEnabled()) {
585585
const distance = vec3.squaredDistance(model.point1, model.point2);
586586
const maxDistance = 100;
587587

588588
if (distance > maxDistance || publicAPI.isDraggingForced()) {
589589
publicAPI.invokeInteractionEvent();
590-
publicAPI.invokeEndInteractionEvent(true);
590+
publicAPI.invokeEndInteractionEvent();
591591

592592
if (publicAPI.getResetAfterPointPlacement()) {
593593
publicAPI.reset();
@@ -608,9 +608,9 @@ export default function widgetBehavior(publicAPI, model) {
608608
publicAPI.handleKeyDown = ({ key }) => {
609609
if (key === 'Escape') {
610610
if (model.hasFocus) {
611-
publicAPI.invokeEndInteractionEvent(false);
612611
publicAPI.reset();
613612
publicAPI.loseFocus();
613+
publicAPI.invokeEndInteractionEvent();
614614
}
615615
} else {
616616
model.keysDown[key] = true;

Sources/Widgets/Widgets3D/SplineWidget/behavior.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ export default function widgetBehavior(publicAPI, model) {
1212
// --------------------------------------------------------------------------
1313

1414
const updateHandlesSize = () => {
15-
if (model.handleSizeInPixels != null) {
16-
const scale =
17-
model.handleSizeInPixels *
18-
vec3.distance(
19-
model.apiSpecificRenderWindow.displayToWorld(0, 0, 0, model.renderer),
20-
model.apiSpecificRenderWindow.displayToWorld(1, 0, 0, model.renderer)
21-
);
15+
if (publicAPI.getHandleSizeInPixels() != null) {
16+
const scale = publicAPI.getHandleSizeInPixels();
2217

2318
model.moveHandle.setScale1(scale);
2419
model.widgetState.getHandleList().forEach((handle) => {
@@ -39,7 +34,7 @@ export default function widgetBehavior(publicAPI, model) {
3934
model.moveHandle.getOrigin(),
4035
model.lastHandle.getOrigin()
4136
) >
42-
model.freehandMinDistance * model.freehandMinDistance
37+
publicAPI.getFreehandMinDistance() * publicAPI.getFreehandMinDistance()
4338
) {
4439
model.lastHandle = model.widgetState.addHandle();
4540
model.lastHandle.setVisible(false);
@@ -60,6 +55,13 @@ export default function widgetBehavior(publicAPI, model) {
6055
const getHoveredHandle = () => {
6156
const handles = model.widgetState.getHandleList();
6257

58+
const scale =
59+
model.moveHandle.getScale1() *
60+
vec3.distance(
61+
model.apiSpecificRenderWindow.displayToWorld(0, 0, 0, model.renderer),
62+
model.apiSpecificRenderWindow.displayToWorld(1, 0, 0, model.renderer)
63+
);
64+
6365
return handles.reduce(
6466
({ closestHandle, closestDistance }, handle) => {
6567
const distance = vec3.squaredDistance(
@@ -81,8 +83,7 @@ export default function widgetBehavior(publicAPI, model) {
8183
},
8284
{
8385
closestHandle: null,
84-
closestDistance:
85-
model.moveHandle.getScale1() * model.moveHandle.getScale1(),
86+
closestDistance: scale * scale,
8687
}
8788
).closestHandle;
8889
};
@@ -98,20 +99,33 @@ export default function widgetBehavior(publicAPI, model) {
9899
// Public methods
99100
// --------------------------------------------------------------------------
100101

101-
macro.setGet(publicAPI, model, [
102-
'freehandMinDistance',
103-
'allowFreehand',
104-
'resolution',
105-
'defaultCursor',
106-
'handleSizeInPixels',
107-
]);
102+
publicAPI.setResetAfterPointPlacement =
103+
model.factory.setResetAfterPointPlacement;
104+
publicAPI.getResetAfterPointPlacement =
105+
model.factory.getResetAfterPointPlacement;
106+
publicAPI.setResetAfterPointPlacement(
107+
publicAPI.getResetAfterPointPlacement()
108+
);
109+
110+
publicAPI.setFreehandMinDistance = model.factory.setFreehandMinDistance;
111+
publicAPI.getFreehandMinDistance = model.factory.getFreehandMinDistance;
112+
publicAPI.setFreehandMinDistance(publicAPI.getFreehandMinDistance());
113+
114+
publicAPI.setAllowFreehand = model.factory.setAllowFreehand;
115+
publicAPI.getAllowFreehand = model.factory.getAllowFreehand;
116+
publicAPI.setAllowFreehand(publicAPI.getAllowFreehand());
117+
118+
publicAPI.setDefaultCursor = model.factory.setDefaultCursor;
119+
publicAPI.getDefaultCursor = model.factory.getDefaultCursor;
120+
publicAPI.setDefaultCursor(publicAPI.getDefaultCursor());
108121

109122
// --------------------------------------------------------------------------
110123

111124
publicAPI.setHandleSizeInPixels = (size) => {
112125
model.factory.setHandleSizeInPixels(size);
113126
updateHandlesSize();
114127
};
128+
publicAPI.getHandleSizeInPixels = model.factory.getHandleSizeInPixels;
115129
publicAPI.setHandleSizeInPixels(model.factory.getHandleSizeInPixels()); // set initial value
116130

117131
// --------------------------------------------------------------------------
@@ -202,7 +216,7 @@ export default function widgetBehavior(publicAPI, model) {
202216
}
203217
}
204218

205-
model.freeHand = model.allowFreehand && !model.isDragging;
219+
model.freeHand = publicAPI.getAllowFreehand() && !model.isDragging;
206220
} else {
207221
model.isDragging = true;
208222
model.apiSpecificRenderWindow.setCursor('grabbing');
@@ -244,7 +258,13 @@ export default function widgetBehavior(publicAPI, model) {
244258
model.moveHandle.getScale1() * model.moveHandle.getScale1()
245259
) {
246260
model.lastHandle.setVisible(true);
247-
publicAPI.loseFocus();
261+
publicAPI.invokeEndInteractionEvent();
262+
263+
if (publicAPI.getResetAfterPointPlacement()) {
264+
publicAPI.reset();
265+
} else {
266+
publicAPI.loseFocus();
267+
}
248268
}
249269
}
250270

@@ -325,11 +345,18 @@ export default function widgetBehavior(publicAPI, model) {
325345

326346
if (key === 'Enter') {
327347
if (model.widgetState.getHandleList().length > 0) {
328-
publicAPI.loseFocus();
348+
publicAPI.invokeEndInteractionEvent();
349+
350+
if (publicAPI.getResetAfterPointPlacement()) {
351+
publicAPI.reset();
352+
} else {
353+
publicAPI.loseFocus();
354+
}
329355
}
330356
} else if (key === 'Escape') {
331357
publicAPI.reset();
332358
publicAPI.loseFocus();
359+
publicAPI.invokeEndInteractionEvent();
333360
} else if (key === 'Delete' || key === 'Backspace') {
334361
if (model.lastHandle) {
335362
model.widgetState.removeHandle(model.lastHandle);
@@ -367,7 +394,6 @@ export default function widgetBehavior(publicAPI, model) {
367394
publicAPI.loseFocus = () => {
368395
if (model.hasFocus) {
369396
model.interactor.cancelAnimation(publicAPI);
370-
publicAPI.invokeEndInteractionEvent();
371397
}
372398

373399
model.widgetState.deactivate();

Sources/Widgets/Widgets3D/SplineWidget/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const DEFAULT_VALUES = {
6666
resolution: 32, // propagates to SplineContextRepresentation
6767
defaultCursor: 'pointer',
6868
handleSizeInPixels: 10, // propagates to SplineContextRepresentation
69+
resetAfterPointPlacement: false,
6970
};
7071

7172
// ----------------------------------------------------------------------------
@@ -81,6 +82,7 @@ export function extend(publicAPI, model, initialValues = {}) {
8182
'resolution',
8283
'defaultCursor',
8384
'handleSizeInPixels',
85+
'resetAfterPointPlacement',
8486
]);
8587

8688
vtkSplineWidget(publicAPI, model);

Sources/Widgets/Widgets3D/SplineWidget/state.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export default function generateState() {
2424
name: 'handle',
2525
initialValues: {
2626
scale1: 0.05,
27-
visible: true,
2827
},
2928
})
3029
.build();

0 commit comments

Comments
 (0)