Skip to content

Commit

Permalink
feat(polygon): add point menu option
Browse files Browse the repository at this point in the history
  • Loading branch information
floryst committed Oct 6, 2023
1 parent f2a3ef6 commit 71e8a60
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/vtk/PolygonWidget/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ export default function widgetBehavior(publicAPI: any, model: any) {
return macro.VOID;
}

if (checkOverSegment()) {
return macro.VOID;
}

const manipulator =
model.activeState?.getManipulator?.() ?? model.manipulator;
if (model.widgetState.getPlacing() && manipulator) {
Expand All @@ -193,21 +197,6 @@ export default function widgetBehavior(publicAPI: any, model: any) {
return macro.EVENT_ABORT;
}

if (checkOverSegment()) {
// insert point
const insertIndex = model.activeState.getIndex() + 1;
const newHandle = model.widgetState.addHandle({ insertIndex });
const coords = getWorldCoords(event);
if (!coords) throw new Error('No world coords');
newHandle.setOrigin(coords);
// enable dragging immediately
publicAPI.activateHandle({
selectedState: newHandle,
representation: model.representations[0].getActors()[0], // first actor is GlyphMapper for handles
});
overUnselectedHandle = true;
}

if (model.activeState?.getActive() && model.pickable && model.dragable) {
model._isDragging = true;
model._apiSpecificRenderWindow.setCursor('grabbing');
Expand Down Expand Up @@ -361,14 +350,32 @@ export default function widgetBehavior(publicAPI: any, model: any) {
// Right press: Remove last handle / Pop context menu
// --------------------------------------------------------------------------

const makeWidgetActions = () => {
const makeWidgetActions = (eventData: any) => {
const widgetActions: Array<WidgetAction> = [];

const { activeState } = model;

const overSegment = checkOverSegment();
// if hovering on handle and we will still have at least 2 points after removing handle
if (!overSegment && model.widgetState.getHandles().length > 2) {

if (overSegment) {
// Allow inserting ponts when over a segment
widgetActions.push({
name: 'Add Point',
func: () => {
const insertIndex = activeState.getIndex() + 1;
const newHandle = model.widgetState.addHandle({ insertIndex });
const coords = getWorldCoords(eventData);
if (!coords) throw new Error('No world coords');
newHandle.setOrigin(coords);
// enable dragging immediately
publicAPI.activateHandle({
selectedState: newHandle,
representation: model.representations[0].getActors()[0], // first actor is GlyphMapper for handles
});
},
});
} else if (!overSegment && model.widgetState.getHandles().length > 2) {
// if hovering on handle and we will still have at least 2 points after removing handle
widgetActions.push({
name: 'Delete Point',
func: () => {
Expand All @@ -392,7 +399,7 @@ export default function widgetBehavior(publicAPI: any, model: any) {

const eventWithWidgetAction = {
...eventData,
widgetActions: makeWidgetActions(),
widgetActions: makeWidgetActions(eventData),
};

publicAPI.invokeRightClickEvent(eventWithWidgetAction);
Expand Down

0 comments on commit 71e8a60

Please sign in to comment.