All files / Sources/Rendering/Core/InteractorObserver index.js

63.79% Statements 37/58
48% Branches 12/25
69.23% Functions 9/13
63.79% Lines 37/58

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 186 187 188      1x                                                 1x                     136x   136x       127x 18x             126x 4914x 2184x                             136x 127x       127x   127x   127x 126x     127x       136x                                               136x 12x       12x           136x 6x       6x         136x 10x   10x                     1x                     136x     136x   136x 136x 136x     136x     136x   136x         136x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkRenderWindowInteractor from 'vtk.js/Sources/Rendering/Core/RenderWindowInteractor';
 
const { vtkErrorMacro, VOID } = macro;
 
// ----------------------------------------------------------------------------
// Global methods
// ----------------------------------------------------------------------------
 
//----------------------------------------------------------------------------
// Description:
// Transform from world to display coordinates.
function computeWorldToDisplay(renderer, x, y, z) {
  const view = renderer.getRenderWindow().getViews()[0];
  return view.worldToDisplay(x, y, z, renderer);
}
 
//----------------------------------------------------------------------------
// Description:
// Transform from display to world coordinates.
function computeDisplayToWorld(renderer, x, y, z) {
  const view = renderer.getRenderWindow().getViews()[0];
  return view.displayToWorld(x, y, z, renderer);
}
 
// ----------------------------------------------------------------------------
// Static API
// ----------------------------------------------------------------------------
export const STATIC = {
  computeWorldToDisplay,
  computeDisplayToWorld,
};
 
// ----------------------------------------------------------------------------
// vtkInteractorObserver methods
// ----------------------------------------------------------------------------
 
function vtkInteractorObserver(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkInteractorObserver');
 
  const superClass = { ...publicAPI };
 
  //----------------------------------------------------------------------------
  function unsubscribeFromEvents() {
    while (model.subscribedEvents.length) {
      model.subscribedEvents.pop().unsubscribe();
    }
  }
 
  //----------------------------------------------------------------------------
  // Check what events we can handle and register callbacks
  function subscribeToEvents() {
    vtkRenderWindowInteractor.handledEvents.forEach((eventName) => {
      if (publicAPI[`handle${eventName}`]) {
        model.subscribedEvents.push(
          model._interactor[`on${eventName}`]((callData) => {
            if (model.processEvents) {
              return publicAPI[`handle${eventName}`](callData);
            }
            return VOID;
          }, model.priority)
        );
      }
    });
  }
 
  //----------------------------------------------------------------------------
  // Public API methods
  //----------------------------------------------------------------------------
  publicAPI.setInteractor = (i) => {
    Iif (i === model._interactor) {
      return;
    }
 
    unsubscribeFromEvents();
 
    model._interactor = i;
 
    if (i && model.enabled) {
      subscribeToEvents();
    }
 
    publicAPI.modified();
  };
 
  //----------------------------------------------------------------------------
  publicAPI.setEnabled = (enable) => {
    if (enable === model.enabled) {
      return;
    }
 
    unsubscribeFromEvents();
 
    if (enable) {
      if (model._interactor) {
        subscribeToEvents();
      } else {
        vtkErrorMacro(`
          The interactor must be set before subscribing to events
        `);
      }
    }
 
    model.enabled = enable;
    publicAPI.modified();
  };
 
  //----------------------------------------------------------------------------
  // Description:
  // Transform from display to world coordinates.
  publicAPI.computeDisplayToWorld = (renderer, x, y, z) => {
    Iif (!renderer) {
      return null;
    }
 
    return model._interactor.getView().displayToWorld(x, y, z, renderer);
  };
 
  //----------------------------------------------------------------------------
  // Description:
  // Transform from world to display coordinates.
  publicAPI.computeWorldToDisplay = (renderer, x, y, z) => {
    Iif (!renderer) {
      return null;
    }
 
    return model._interactor.getView().worldToDisplay(x, y, z, renderer);
  };
 
  //----------------------------------------------------------------------------
 
  publicAPI.setPriority = (priority) => {
    const modified = superClass.setPriority(priority);
 
    Iif (modified && model._interactor) {
      unsubscribeFromEvents();
      subscribeToEvents();
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  enabled: true,
  // _interactor: null,
  priority: 0.0,
  processEvents: true,
  subscribedEvents: [],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Object methods
  macro.obj(publicAPI, model);
 
  macro.event(publicAPI, model, 'InteractionEvent');
  macro.event(publicAPI, model, 'StartInteractionEvent');
  macro.event(publicAPI, model, 'EndInteractionEvent');
 
  // Create get-only macros
  macro.get(publicAPI, model, ['_interactor', 'enabled']);
 
  // Create get-set macros
  macro.setGet(publicAPI, model, ['priority', 'processEvents']);
 
  macro.moveToProtected(publicAPI, model, ['interactor']);
 
  // For more macro methods, see "Sources/macros.js"
 
  // Object specific methods
  vtkInteractorObserver(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkInteractorObserver');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, ...STATIC };