All files / Sources/Widgets/Widgets3D/LabelWidget behavior.js

0% Statements 0/77
0% Branches 0/55
0% Functions 0/11
0% Lines 0/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                                                                                                 
import macro from 'vtk.js/Sources/macros';
import { add } from 'vtk.js/Sources/Common/Core/Math';
 
export default function widgetBehavior(publicAPI, model) {
  model.classHierarchy.push('vtkLabelWidgetProp');
  model._isDragging = false;
 
  // --------------------------------------------------------------------------
  // Public methods
  // --------------------------------------------------------------------------
 
  publicAPI.setText = (text) => {
    model.widgetState.getText().setText(text);
    model._interactor.render();
  };
 
  publicAPI.getText = () => model.widgetState.getText().getText();
 
  // --------------------------------------------------------------------------
  // 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 / Place text handle
  // --------------------------------------------------------------------------
 
  publicAPI.handleLeftButtonPress = (e) => {
    if (
      !model.activeState ||
      !model.activeState.getActive() ||
      !model.pickable ||
      ignoreKey(e)
    ) {
      return macro.VOID;
    }
 
    const manipulator =
      model.activeState?.getManipulator?.() ?? model.manipulator;
    const { worldCoords } = manipulator.handleEvent(
      e,
      model._apiSpecificRenderWindow
    );
 
    if (
      model.activeState === model.widgetState.getMoveHandle() &&
      manipulator
    ) {
      // Commit handle to location
      const moveHandle = model.widgetState.getMoveHandle();
      moveHandle.setOrigin(worldCoords);
      model.widgetState.getText().setOrigin(moveHandle.getOrigin());
      publicAPI.loseFocus();
    } else if (model.dragable) {
      model._isDragging = true;
      model._apiSpecificRenderWindow.setCursor('grabbing');
      model._interactor.requestAnimation(publicAPI);
    }
 
    publicAPI.invokeStartInteractionEvent();
    return macro.EVENT_ABORT;
  };
 
  // --------------------------------------------------------------------------
  // Left release: Finish drag
  // --------------------------------------------------------------------------
 
  publicAPI.handleLeftButtonRelease = () => {
    if (
      !model.activeState ||
      !model.activeState.getActive() ||
      !model.pickable
    ) {
      return macro.VOID;
    }
 
    if (model._isDragging) {
      model._apiSpecificRenderWindow.setCursor('pointer');
      model.widgetState.deactivate();
      model._interactor.cancelAnimation(publicAPI);
      model._isDragging = false;
    } else if (model.activeState !== model.widgetState.getMoveHandle()) {
      model.widgetState.deactivate();
    }
 
    if (
      (model.hasFocus && !model.activeState) ||
      (model.activeState && !model.activeState.getActive())
    ) {
      model._widgetManager.enablePicking();
      model._interactor.render();
    }
 
    publicAPI.invokeEndInteractionEvent();
    return macro.EVENT_ABORT;
  };
 
  // --------------------------------------------------------------------------
  // Mouse move: Drag selected handle / Handle follow the mouse
  // --------------------------------------------------------------------------
 
  publicAPI.handleMouseMove = (callData) => {
    const manipulator =
      model.activeState?.getManipulator?.() ?? model.manipulator;
    if (
      manipulator &&
      model.pickable &&
      model.dragable &&
      model.activeState &&
      model.activeState.getActive() &&
      !ignoreKey(callData)
    ) {
      const { worldCoords, worldDelta } = manipulator.handleEvent(
        callData,
        model._apiSpecificRenderWindow
      );
 
      const isHandleMoving =
        model.widgetState.getMoveHandle() === model.activeState ||
        model._isDragging;
 
      if (!isHandleMoving || !worldCoords.length || !worldDelta.length)
        return macro.VOID;
 
      const curOrigin = model.activeState.getOrigin();
      const newOrigin = curOrigin
        ? add(curOrigin, worldDelta, [])
        : worldCoords;
 
      model.activeState.setOrigin(newOrigin);
      model.widgetState.getText().setOrigin(newOrigin);
 
      publicAPI.invokeInteractionEvent();
      return macro.EVENT_ABORT;
    }
 
    return macro.VOID;
  };
 
  // --------------------------------------------------------------------------
  // Focus API
  // --------------------------------------------------------------------------
 
  publicAPI.reset = () => {
    model.widgetState.getMoveHandle().setOrigin(null);
    model.widgetState.getText().setOrigin(null);
    model.widgetState.getText().setText('');
  };
 
  publicAPI.grabFocus = () => {
    if (!model.hasFocus) {
      publicAPI.reset();
 
      model.activeState = model.widgetState.getMoveHandle();
      model.widgetState.getMoveHandle().activate();
      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.activeState = null;
    model.hasFocus = false;
    model._widgetManager.enablePicking();
    model._interactor.render();
  };
}