-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathbehavior.ts
More file actions
256 lines (214 loc) · 7.2 KB
/
behavior.ts
File metadata and controls
256 lines (214 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import macro from '@kitware/vtk.js/macro';
import type { Vector3 } from '@kitware/vtk.js/types';
import { computeWorldCoords } from '@/src/vtk/ToolWidgetUtils/utils';
export enum InteractionState {
PlacingFirst = 'PlacingFirst',
PlacingSecond = 'PlacingSecond',
Select = 'Select',
Dragging = 'Dragging',
}
export function shouldIgnoreEvent(e: any) {
return e.altKey || e.controlKey || e.shiftKey;
}
export default function widgetBehavior(publicAPI: any, model: any) {
model.classHierarchy.push('vtkRulerWidgetProp');
const anotherWidgetHasFocus = () =>
model._widgetManager
.getWidgets()
.some((w: any) => w !== publicAPI && w.hasFocus());
model.interactionState = InteractionState.Select;
let draggingState: any = null;
macro.setGet(publicAPI, model, ['interactionState']);
// support setting per-view widget manipulators
macro.setGet(publicAPI, model, ['manipulator']);
// support forwarding events
macro.event(publicAPI, model, 'RightClickEvent');
macro.event(publicAPI, model, 'PlacedEvent');
macro.event(publicAPI, model, 'HoverEvent');
publicAPI.deactivateAllHandles = () => {
model.widgetState.deactivate();
model.activeState = null;
};
publicAPI.setFirstPoint = (coord: Vector3) => {
const point = model.widgetState.getFirstPoint();
point.setOrigin(coord);
};
publicAPI.setSecondPoint = (coord: Vector3) => {
const point = model.widgetState.getSecondPoint();
point.setOrigin(coord);
};
const originalSetInteractionState = publicAPI.setInteractionState;
publicAPI.setInteractionState = (state: InteractionState) => {
const changed = originalSetInteractionState(state);
if (changed && state === InteractionState.PlacingFirst) {
model.widgetState.setIsPlaced(false);
model.widgetState.getFirstPoint().setVisible(false);
model.widgetState.getSecondPoint().setVisible(false);
}
return changed;
};
publicAPI.resetInteractions = () => {
model._interactor.cancelAnimation(publicAPI, true);
};
// Check if mouse is over line segment between handles
const checkOverSegment = () => {
const selections = model._widgetManager.getSelections();
const overSegment =
selections[0]?.getProperties().prop ===
model.representations[1].getActors()[0]; // line representation is second representation
return overSegment;
};
// Check if mouse is over fill representation (for hover but not interaction)
const checkOverFill = () => {
const selections = model._widgetManager.getSelections();
return (
model.representations[2] &&
selections?.[0]?.getProperties().prop ===
model.representations[2].getActors()[0]
);
};
const getWorldCoords = computeWorldCoords(model);
/**
* Places or drags a point.
*/
publicAPI.handleLeftButtonPress = (eventData: any) => {
if (!model.manipulator || shouldIgnoreEvent(eventData)) {
return macro.VOID;
}
// Ignore clicks on fill - let them pass through
if (checkOverFill()) {
return macro.VOID;
}
// turns off hover while dragging
publicAPI.invokeHoverEvent({
...eventData,
hovering: false,
});
const intState = publicAPI.getInteractionState();
// If not placing and another widget is active, don't consume event.
const activeWidget = model._widgetManager.getActiveWidget();
if (
intState === InteractionState.Select &&
activeWidget &&
activeWidget !== publicAPI
) {
return macro.VOID;
}
const worldCoords = getWorldCoords(eventData);
if (!worldCoords?.length) {
return macro.VOID;
}
if (intState === InteractionState.PlacingFirst) {
publicAPI.setFirstPoint(worldCoords);
publicAPI.setSecondPoint(worldCoords);
model.widgetState.getFirstPoint().setVisible(true);
model.widgetState.getSecondPoint().setVisible(true);
model._interactor.requestAnimation(publicAPI);
publicAPI.invokeStartInteractionEvent();
publicAPI.setInteractionState(InteractionState.PlacingSecond);
return macro.EVENT_ABORT;
}
if (intState === InteractionState.PlacingSecond) {
publicAPI.setSecondPoint(worldCoords);
model.widgetState.setIsPlaced(true);
publicAPI.setInteractionState(InteractionState.Select);
publicAPI.invokeEndInteractionEvent();
publicAPI.invokePlacedEvent();
model._interactor.cancelAnimation(publicAPI);
return macro.EVENT_ABORT;
}
// dragging
if (
model.activeState?.getActive() &&
model.activeState?.setOrigin &&
model.pickable &&
!checkOverSegment()
) {
draggingState = model.activeState;
publicAPI.setInteractionState(InteractionState.Dragging);
model._apiSpecificRenderWindow.setCursor('grabbing');
model._interactor.requestAnimation(publicAPI);
publicAPI.invokeStartInteractionEvent();
return macro.EVENT_ABORT;
}
return macro.VOID;
};
/**
* Moves a point around.
*/
publicAPI.handleMouseMove = (eventData: any) => {
const worldCoords = getWorldCoords(eventData);
if (!worldCoords?.length) {
return macro.VOID;
}
const intState = publicAPI.getInteractionState();
if (intState === InteractionState.PlacingSecond) {
model.widgetState.getSecondPoint().setOrigin(worldCoords);
// show second point during placement
model.widgetState.getSecondPoint().setVisible(true);
publicAPI.invokeInteractionEvent();
return macro.EVENT_ABORT;
}
if (
publicAPI.getInteractionState() === InteractionState.Dragging &&
draggingState
) {
draggingState.setOrigin(worldCoords);
publicAPI.invokeInteractionEvent();
return macro.EVENT_ABORT;
}
if (anotherWidgetHasFocus()) {
publicAPI.invokeHoverEvent({
...eventData,
hovering: false,
});
return macro.VOID;
}
publicAPI.invokeHoverEvent({
...eventData,
hovering: !!model.activeState || checkOverFill(),
});
return macro.VOID;
};
/**
* Finishes dragging
*/
publicAPI.handleLeftButtonRelease = (eventData: any) => {
if (draggingState) {
const worldCoords = getWorldCoords(eventData);
if (worldCoords?.length) {
draggingState.setOrigin(worldCoords);
}
draggingState = null;
publicAPI.setInteractionState(InteractionState.Select);
model._apiSpecificRenderWindow.setCursor('pointer');
model.widgetState.deactivate();
model._interactor.cancelAnimation(publicAPI);
publicAPI.invokeEndInteractionEvent();
model._widgetManager.enablePicking();
}
};
publicAPI.handleRightButtonPress = (eventData: any) => {
if (
shouldIgnoreEvent(eventData) ||
publicAPI.getInteractionState() !== InteractionState.Select ||
!model.activeState
) {
return macro.VOID;
}
if (anotherWidgetHasFocus()) {
return macro.VOID;
}
publicAPI.invokeRightClickEvent(eventData);
return macro.EVENT_ABORT;
};
publicAPI.grabFocus = () => {
throw new Error('grabFocus is not implemented');
};
publicAPI.loseFocus = () => {
throw new Error('loseFocus is not implemented');
};
publicAPI.delete = macro.chain(() => {
publicAPI.resetInteractions();
}, publicAPI.delete);
}