-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #379 from floryst/refactor-widgets
Refactor widgets
- Loading branch information
Showing
13 changed files
with
105 additions
and
619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,185 +1,8 @@ | ||
import macro from 'vtk.js/Sources/macro'; | ||
import superWidgetBehavior from 'vtk.js/Sources/Widgets/Widgets3D/AngleWidget/behavior'; | ||
|
||
const MAX_POINTS = 3; | ||
import widgetManipulatorWatcher from 'paraview-glance/src/vtk/widgetManipulatorWatcher'; | ||
|
||
export default function widgetBehavior(publicAPI, model) { | ||
model.classHierarchy.push('vtkAngleWidgetProp'); | ||
let isDragging = null; | ||
let cameraSub = null; | ||
|
||
// -------------------------------------------------------------------------- | ||
// Display 2D | ||
// -------------------------------------------------------------------------- | ||
|
||
publicAPI.setDisplayCallback = (callback) => | ||
model.representations[0].setDisplayCallback(callback); | ||
|
||
// -------------------------------------------------------------------------- | ||
// Interactor events | ||
// -------------------------------------------------------------------------- | ||
|
||
function ignoreKey(e) { | ||
return e.altKey || e.controlKey || e.shiftKey; | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Left press: Select handle to drag | ||
// -------------------------------------------------------------------------- | ||
|
||
publicAPI.handleLeftButtonPress = (e) => { | ||
if ( | ||
!model.activeState || | ||
!model.activeState.getActive() || | ||
!model.pickable || | ||
ignoreKey(e) | ||
) { | ||
return macro.VOID; | ||
} | ||
|
||
if ( | ||
model.activeState === model.widgetState.getMoveHandle() && | ||
model.widgetState.getHandleList().length < MAX_POINTS | ||
) { | ||
// Commit handle to location | ||
const moveHandle = model.widgetState.getMoveHandle(); | ||
const newHandle = model.widgetState.addHandle(); | ||
newHandle.setOrigin(...moveHandle.getOrigin()); | ||
newHandle.setColor(moveHandle.getColor()); | ||
newHandle.setScale1(moveHandle.getScale1()); | ||
} else { | ||
isDragging = true; | ||
model.openGLRenderWindow.setCursor('grabbing'); | ||
model.interactor.requestAnimation(publicAPI); | ||
} | ||
|
||
publicAPI.invokeStartInteractionEvent(); | ||
return macro.EVENT_ABORT; | ||
}; | ||
|
||
// -------------------------------------------------------------------------- | ||
// Mouse move: Drag selected handle / Handle follow the mouse | ||
// -------------------------------------------------------------------------- | ||
|
||
publicAPI.handleMouseMove = (callData) => { | ||
if ( | ||
model.hasFocus && | ||
model.widgetState.getHandleList().length === MAX_POINTS | ||
) { | ||
model.widgetManager.releaseFocus(); | ||
return macro.VOID; | ||
} | ||
|
||
if ( | ||
model.pickable && | ||
model.manipulator && | ||
model.activeState && | ||
model.activeState.getActive() && | ||
!ignoreKey(callData) | ||
) { | ||
const worldCoords = model.manipulator.handleEvent( | ||
callData, | ||
model.openGLRenderWindow | ||
); | ||
if ( | ||
worldCoords.length && | ||
(model.activeState === model.widgetState.getMoveHandle() || isDragging) | ||
) { | ||
model.activeState.setOrigin(worldCoords); | ||
publicAPI.invokeInteractionEvent(); | ||
return macro.EVENT_ABORT; | ||
} | ||
} | ||
|
||
return macro.VOID; | ||
}; | ||
|
||
// -------------------------------------------------------------------------- | ||
// Left release: Finish drag / Create new handle | ||
// -------------------------------------------------------------------------- | ||
|
||
publicAPI.handleLeftButtonRelease = () => { | ||
if (isDragging && model.pickable) { | ||
model.openGLRenderWindow.setCursor('pointer'); | ||
model.widgetState.deactivate(); | ||
model.interactor.cancelAnimation(publicAPI); | ||
publicAPI.invokeEndInteractionEvent(); | ||
} else if (model.activeState !== model.widgetState.getMoveHandle()) { | ||
model.widgetState.deactivate(); | ||
} | ||
|
||
if ( | ||
(model.hasFocus && !model.activeState) || | ||
(model.activeState && !model.activeState.getActive()) | ||
) { | ||
publicAPI.invokeEndInteractionEvent(); | ||
} | ||
|
||
isDragging = false; | ||
}; | ||
|
||
// -------------------------------------------------------------------------- | ||
// Focus API - modeHandle follow mouse when widget has focus | ||
// -------------------------------------------------------------------------- | ||
|
||
publicAPI.grabFocus = () => { | ||
if ( | ||
!model.hasFocus && | ||
model.widgetState.getHandleList().length < MAX_POINTS | ||
) { | ||
model.activeState = model.widgetState.getMoveHandle(); | ||
model.activeState.activate(); | ||
model.activeState.setVisible(true); | ||
model.interactor.requestAnimation(publicAPI); | ||
publicAPI.invokeStartInteractionEvent(); | ||
} | ||
model.hasFocus = true; | ||
}; | ||
|
||
// -------------------------------------------------------------------------- | ||
|
||
publicAPI.loseFocus = () => { | ||
if (model.hasFocus) { | ||
model.interactor.cancelAnimation(publicAPI); | ||
publicAPI.invokeEndInteractionEvent(); | ||
} | ||
model.widgetState.deactivate(); | ||
model.widgetState.getMoveHandle().deactivate(); | ||
model.widgetState.getMoveHandle().setVisible(false); | ||
model.activeState = null; | ||
model.hasFocus = false; | ||
model.widgetManager.enablePicking(); | ||
model.interactor.render(); | ||
}; | ||
|
||
// -------------------------------------------------------------------------- | ||
|
||
publicAPI.delete = macro.chain(publicAPI.delete, () => { | ||
if (cameraSub) { | ||
cameraSub.unsubscribe(); | ||
} | ||
}); | ||
|
||
// -------------------------------------------------------------------------- | ||
// init | ||
// -------------------------------------------------------------------------- | ||
|
||
const setHandleScaleFromCamera = () => { | ||
if (model.camera) { | ||
let scale; | ||
if (model.camera.getParallelProjection()) { | ||
scale = model.camera.getParallelScale() / 1.25; | ||
} else { | ||
scale = model.camera.getDistance() / 6; | ||
} | ||
|
||
publicAPI.setHandleScale(scale); | ||
} | ||
}; | ||
|
||
// listen to camera so we can scale the handles to the screen | ||
cameraSub = model.camera.onModified(setHandleScaleFromCamera); | ||
// since forwarded/linked set/get methods aren't created until | ||
// after this behavior function finishes, this is a hack to invoke | ||
// initial handle scale. | ||
setTimeout(setHandleScaleFromCamera, 0); | ||
superWidgetBehavior(publicAPI, model); | ||
widgetManipulatorWatcher(publicAPI, model); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.