InteractorStyle

Introduction

vtkInteractorStyle - provide event-driven interface to the rendering window

vtkInteractorStyle is a base class implementing the majority of motion
control routines and defines an event driven interface to support
vtkRenderWindowInteractor. vtkRenderWindowInteractor implements
platform dependent key/mouse routing and timer control, which forwards
events in a neutral form to vtkInteractorStyle.

vtkInteractorStyle can be subclassed to provide new interaction styles and
a facility to override any of the default mouse/key operations which
currently handle trackball or joystick styles is provided. Note that this
class will fire a variety of events that can be watched using an observer,
such as AnimationEvent, MouseMoveEvent, LeftButtonPressEvent, LeftButtonReleaseEvent,
MiddleButtonPressEvent, MiddleButtonReleaseEvent, RightButtonPressEvent,
RightButtonReleaseEvent, KeyPressEvent, KeyUpEvent,

vtkInteractorStyle subclasses may implement various styles of
interaction. Some common controls are

Keypress r: reset the camera view along the current view

direction. Centers the actors and moves the camera so that all actors are
visible.

Keypress s: modify the representation of all actors so that they are

surfaces.

Keypress w: modify the representation of all actors so that they are

wireframe.

Source

Constants.js
export const States = {
IS_START: 0,
IS_NONE: 0,

IS_ROTATE: 1,
IS_PAN: 2,
IS_SPIN: 3,
IS_DOLLY: 4,

IS_CAMERA_POSE: 11,

IS_WINDOW_LEVEL: 1024,
IS_SLICE: 1025,
};

export default {
States,
};
index.js
import macro from 'vtk.js/Sources/macro';
import vtkInteractorObserver from 'vtk.js/Sources/Rendering/Core/InteractorObserver';
import Constants from 'vtk.js/Sources/Rendering/Core/InteractorStyle/Constants';

const { States } = Constants;

// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------

// Add module-level functions or api that you want to expose statically via
// the next section...

const stateNames = {
Rotate: States.IS_ROTATE,
Pan: States.IS_PAN,
Spin: States.IS_SPIN,
Dolly: States.IS_DOLLY,
CameraPose: States.IS_CAMERA_POSE,
WindowLevel: States.IS_WINDOW_LEVEL,
Slice: States.IS_SLICE,
};

// ----------------------------------------------------------------------------
// vtkInteractorStyle methods
// ----------------------------------------------------------------------------

function vtkInteractorStyle(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkInteractorStyle');

// Public API methods
// create bunch of Start/EndState methods
Object.keys(stateNames).forEach((key) => {
macro.event(publicAPI, model, `Start${key}Event`);
publicAPI[`start${key}`] = () => {
if (model.state !== States.IS_NONE) {
return;
}
model.state = stateNames[key];
model.interactor.requestAnimation(publicAPI);
publicAPI.invokeStartInteractionEvent({ type: 'StartInteractionEvent' });
publicAPI[`invokeStart${key}Event`]({ type: `Start${key}Event` });
};
macro.event(publicAPI, model, `End${key}Event`);
publicAPI[`end${key}`] = () => {
if (model.state !== stateNames[key]) {
return;
}
model.state = States.IS_NONE;
model.interactor.cancelAnimation(publicAPI);
publicAPI.invokeEndInteractionEvent({ type: 'EndInteractionEvent' });
publicAPI[`invokeEnd${key}Event`]({ type: `End${key}Event` });
model.interactor.render();
};
});

//----------------------------------------------------------------------------
publicAPI.handleKeyPress = (callData) => {
const rwi = model.interactor;
let ac = null;
switch (callData.key) {
case 'r':
case 'R':
callData.pokedRenderer.resetCamera();
rwi.render();
break;

case 'w':
case 'W':
ac = callData.pokedRenderer.getActors();
ac.forEach((anActor) => {
anActor.getProperty().setRepresentationToWireframe();
});
rwi.render();
break;

case 's':
case 'S':
ac = callData.pokedRenderer.getActors();
ac.forEach((anActor) => {
anActor.getProperty().setRepresentationToSurface();
});
rwi.render();
break;

case 'v':
case 'V':
ac = callData.pokedRenderer.getActors();
ac.forEach((anActor) => {
anActor.getProperty().setRepresentationToPoints();
});
rwi.render();
break;

default:
break;
}
};
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
state: States.IS_NONE,
handleObservers: 1,
autoAdjustCameraClippingRange: 1,
};

// ----------------------------------------------------------------------------

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

// Inheritance
vtkInteractorObserver.extend(publicAPI, model, initialValues);

// Object specific methods
vtkInteractorStyle(publicAPI, model);
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(extend, 'vtkInteractorStyle');

// ----------------------------------------------------------------------------

export default Object.assign({ newInstance, extend }, Constants);