UNPKG

@thewtex/vtk.js-esm

Version:

Visualization Toolkit for the Web

314 lines (250 loc) 12.5 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import macro from '../../macro.js'; import vtkInteractorStyle from '../../Rendering/Core/InteractorStyle.js'; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } // Event Types // ---------------------------------------------------------------------------- var START_INTERACTION_EVENT = { type: 'StartInteractionEvent' }; var INTERACTION_EVENT = { type: 'InteractionEvent' }; var END_INTERACTION_EVENT = { type: 'EndInteractionEvent' }; // ---------------------------------------------------------------------------- // vtkInteractorStyleRemoteMouse methods // ---------------------------------------------------------------------------- function vtkInteractorStyleRemoteMouse(publicAPI, model) { // Set our className model.classHierarchy.push('vtkInteractorStyleRemoteMouse'); function createRemoteEvent(callData) { // Fields: // - action: [down, up] // - x, y (Normalized) // Flags: // - buttonLeft, buttonMiddle, buttonRight // - shiftKey, ctrlKey, altKey, metaKey var buttonLeft = model.buttonLeft, buttonMiddle = model.buttonMiddle, buttonRight = model.buttonRight; var shiftKey = callData.shiftKey ? 1 : 0; var ctrlKey = callData.controlKey ? 1 : 0; var altKey = callData.altKey ? 1 : 0; var metaKey = callData.metaKey ? 1 : 0; // Might be platform specific var action = buttonLeft || buttonMiddle || buttonRight ? 'down' : 'up'; // Fixme x / y var _model$interactor$get = model.interactor.getView().getSizeByReference(), _model$interactor$get2 = _slicedToArray(_model$interactor$get, 2), width = _model$interactor$get2[0], height = _model$interactor$get2[1]; var _callData$position = callData.position, x = _callData$position.x, y = _callData$position.y; x /= width; y /= height; return _objectSpread({ action: action, x: x, y: y, buttonLeft: buttonLeft, buttonMiddle: buttonMiddle, buttonRight: buttonRight, shiftKey: shiftKey, altKey: altKey, ctrlKey: ctrlKey, metaKey: metaKey }, model.remoteEventAddOn); } //------------------------------------------------------------------------- // Mouse //------------------------------------------------------------------------- publicAPI.handleLeftButtonPress = function (callData) { model.previousPosition = callData.position; model.buttonLeft = 1; publicAPI.onButtonDown(1, callData); }; //------------------------------------------------------------------------- publicAPI.handleMiddleButtonPress = function (callData) { model.previousPosition = callData.position; model.buttonMiddle = 1; publicAPI.onButtonDown(2, callData); }; //------------------------------------------------------------------------- publicAPI.handleRightButtonPress = function (callData) { model.previousPosition = callData.position; model.buttonRight = 1; publicAPI.onButtonDown(3, callData); }; //------------------------------------------------------------------------- publicAPI.handleLeftButtonRelease = function (callData) { model.buttonLeft = 0; publicAPI.onButtonUp(1, callData); }; //------------------------------------------------------------------------- publicAPI.handleMiddleButtonRelease = function (callData) { model.buttonMiddle = 0; publicAPI.onButtonUp(2, callData); }; //------------------------------------------------------------------------- publicAPI.handleRightButtonRelease = function (callData) { model.buttonRight = 0; publicAPI.onButtonUp(3, callData); }; //------------------------------------------------------------------------- publicAPI.onButtonDown = function (button, callData) { model.interactor.requestAnimation(publicAPI.onButtonDown); publicAPI.invokeStartInteractionEvent(START_INTERACTION_EVENT); publicAPI.invokeRemoteMouseEvent(createRemoteEvent(callData)); }; //------------------------------------------------------------------------- publicAPI.onButtonUp = function (button, callData) { publicAPI.invokeRemoteMouseEvent(createRemoteEvent(callData)); publicAPI.invokeEndInteractionEvent(END_INTERACTION_EVENT); model.interactor.cancelAnimation(publicAPI.onButtonDown); }; //------------------------------------------------------------------------- publicAPI.handleStartMouseWheel = function (callData) { var spinY = callData.spinY, altKey = callData.altKey, controlKey = callData.controlKey, shiftKey = callData.shiftKey; model.interactor.requestAnimation(publicAPI.handleStartMouseWheel); publicAPI.invokeStartInteractionEvent(START_INTERACTION_EVENT); publicAPI.invokeRemoteWheelEvent(_objectSpread({ type: 'StartMouseWheel', spinY: spinY, altKey: altKey, controlKey: controlKey, shiftKey: shiftKey }, model.remoteEventAddOn)); }; //------------------------------------------------------------------------- publicAPI.handleMouseWheel = function (callData) { var spinY = callData.spinY, altKey = callData.altKey, controlKey = callData.controlKey, shiftKey = callData.shiftKey; publicAPI.invokeRemoteWheelEvent(_objectSpread({ type: 'MouseWheel', spinY: spinY, altKey: altKey, controlKey: controlKey, shiftKey: shiftKey }, model.remoteEventAddOn)); publicAPI.invokeInteractionEvent(INTERACTION_EVENT); }; //------------------------------------------------------------------------- publicAPI.handleEndMouseWheel = function () { publicAPI.invokeRemoteWheelEvent(_objectSpread({ type: 'EndMouseWheel' }, model.remoteEventAddOn)); model.interactor.cancelAnimation(publicAPI.handleStartMouseWheel); publicAPI.invokeEndInteractionEvent(END_INTERACTION_EVENT); }; //------------------------------------------------------------------------- publicAPI.handleMouseMove = function (callData) { var ts = Date.now(); var needToSend = model.throttleDelay < ts - model.lastThrottleTime; if (needToSend && (model.sendMouseMove || model.buttonLeft || model.buttonMiddle || model.buttonRight)) { model.lastThrottleTime = ts; publicAPI.invokeRemoteMouseEvent(createRemoteEvent(callData)); } publicAPI.invokeInteractionEvent(INTERACTION_EVENT); }; // Override default handler publicAPI.handleKeyPress = function () {}; //------------------------------------------------------------------------- // Gesture //------------------------------------------------------------------------- publicAPI.handleStartPinch = function (callData) { publicAPI.startDolly(); var scale = callData.scale; model.interactor.requestAnimation(publicAPI.handleStartPinch); publicAPI.invokeStartInteractionEvent(START_INTERACTION_EVENT); publicAPI.invokeRemoteGestureEvent(_objectSpread({ type: 'StartPinch', scale: scale }, model.remoteEventAddOn)); }; //---------------------------------------------------------------------------- publicAPI.handlePinch = function (callData) { var scale = callData.scale; publicAPI.invokeRemoteGestureEvent(_objectSpread({ type: 'Pinch', scale: scale }, model.remoteEventAddOn)); }; //-------------------------------------------------------------------------- publicAPI.handleEndPinch = function () { publicAPI.endDolly(); publicAPI.invokeRemoteGestureEvent(_objectSpread({ type: 'EndPinch' }, model.remoteEventAddOn)); model.interactor.cancelAnimation(publicAPI.handleStartPinch); publicAPI.invokeEndInteractionEvent(END_INTERACTION_EVENT); }; //---------------------------------------------------------------------------- publicAPI.handleStartRotate = function (callData) { publicAPI.startRotate(); var rotation = callData.rotation; model.interactor.requestAnimation(publicAPI.handleStartRotate); publicAPI.invokeStartInteractionEvent(START_INTERACTION_EVENT); publicAPI.invokeRemoteGestureEvent(_objectSpread({ type: 'StartRotate', rotation: rotation }, model.remoteEventAddOn)); }; //---------------------------------------------------------------------------- publicAPI.handleRotate = function (callData) { var rotation = callData.rotation; publicAPI.invokeRemoteGestureEvent(_objectSpread({ type: 'Rotate', rotation: rotation }, model.remoteEventAddOn)); }; //-------------------------------------------------------------------------- publicAPI.handleEndRotate = function () { publicAPI.endRotate(); publicAPI.invokeRemoteGestureEvent(_objectSpread({ type: 'EndRotate' }, model.remoteEventAddOn)); model.interactor.cancelAnimation(publicAPI.handleStartRotate); publicAPI.invokeEndInteractionEvent(END_INTERACTION_EVENT); }; //---------------------------------------------------------------------------- publicAPI.handleStartPan = function (callData) { publicAPI.startPan(); var translation = callData.translation; model.interactor.requestAnimation(publicAPI.handleStartPan); publicAPI.invokeStartInteractionEvent(START_INTERACTION_EVENT); publicAPI.invokeRemoteGestureEvent(_objectSpread({ type: 'StartPan', translation: translation }, model.remoteEventAddOn)); }; //---------------------------------------------------------------------------- publicAPI.handlePan = function (callData) { var translation = callData.translation; publicAPI.invokeRemoteGestureEvent(_objectSpread({ type: 'Pan', translation: translation }, model.remoteEventAddOn)); }; //-------------------------------------------------------------------------- publicAPI.handleEndPan = function () { publicAPI.endPan(); publicAPI.invokeRemoteGestureEvent(_objectSpread({ type: 'EndPan' }, model.remoteEventAddOn)); model.interactor.cancelAnimation(publicAPI.handleStartPan); publicAPI.invokeEndInteractionEvent(END_INTERACTION_EVENT); }; } // ---------------------------------------------------------------------------- // Object factory // ---------------------------------------------------------------------------- var DEFAULT_VALUES = { buttonLeft: 0, buttonMiddle: 0, buttonRight: 0, sendMouseMove: false, throttleDelay: 33.3, // 33.3 millisecond <=> 30 events/second lastThrottleTime: 0 // remoteEventAddOn: null, }; // ---------------------------------------------------------------------------- function extend(publicAPI, model) { var initialValues = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; Object.assign(model, DEFAULT_VALUES, initialValues); // Inheritance vtkInteractorStyle.extend(publicAPI, model, initialValues); macro.setGet(publicAPI, model, ['sendMouseMove', 'remoteEventAddOn', 'throttleDelay']); macro.event(publicAPI, model, 'RemoteMouseEvent'); macro.event(publicAPI, model, 'RemoteWheelEvent'); macro.event(publicAPI, model, 'RemoteGestureEvent'); // Object specific methods vtkInteractorStyleRemoteMouse(publicAPI, model); } // ---------------------------------------------------------------------------- var newInstance = macro.newInstance(extend, 'vtkInteractorStyleRemoteMouse'); // ---------------------------------------------------------------------------- var vtkInteractorStyleRemoteMouse$1 = { newInstance: newInstance, extend: extend }; export default vtkInteractorStyleRemoteMouse$1; export { extend, newInstance };