All files / Sources/Interaction/Manipulators/MouseRangeManipulator index.js

55.55% Statements 65/117
43.39% Branches 23/53
41.66% Functions 10/24
58.55% Lines 65/111

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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                  5x       5x                       7x           7x           7x   7x         7x   7x 7x 7x   7x   5x 5x 2x               2x                         5x                 1x 1x                 1x 1x       5x                 1x 1x                 1x 1x       5x                 3x         3x 3x 3x                 3x 3x       5x 1x 1x 1x 1x         5x 1x 1x 1x 1x         5x 2x 1x 1x 1x         5x             5x                         5x                             5x                             5x                                   5x                                                                         5x 7x     7x 7x     5x                 1x                 5x     5x 5x     5x     5x         1x                
import macro, { vtkWarningMacro } from 'vtk.js/Sources/macros';
import vtkCompositeMouseManipulator from 'vtk.js/Sources/Interaction/Manipulators/CompositeMouseManipulator';
 
// ----------------------------------------------------------------------------
// vtkMouseRangeManipulator methods
// ----------------------------------------------------------------------------
 
function vtkMouseRangeManipulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkMouseRangeManipulator');
 
  // Keep track of delta that is below the value
  // of one step to progressively increment it
  const incrementalDelta = new Map();
 
  // Internal methods
  //-------------------------------------------------------------------------
  function scaleDeltaToRange(listener, normalizedDelta) {
    return (
      normalizedDelta * ((listener.max - listener.min) / (listener.step + 1))
    );
  }
 
  //-------------------------------------------------------------------------
  function processDelta(listener, delta) {
    const oldValue = listener.getValue();
 
    // if exponential scroll is enabled, we raise our scale to the
    //  exponent of the net delta of the interaction. The further away
    // the user's cursor is from the start of the interaction, the more
    // their movements will effect the value.
    let scalingFactor = listener.exponentialScroll
      ? listener.scale ** Math.log2(Math.abs(model.interactionNetDelta) + 2)
      : listener.scale;
 
    // Preserve the sign of scale (which can be used to invert the scrolling direction)
    // after the power operation above (in case of exponentialScroll).
    scalingFactor = Math.abs(scalingFactor) * Math.sign(listener.scale);
 
    const newDelta = delta * scalingFactor + incrementalDelta.get(listener);
 
    // Compute new value based on step
    // In the following line, Math.abs is required so that the floor function
    // consistently rounds to the lowest absolute integer.
    const stepsToDifference = Math.floor(Math.abs(newDelta / listener.step));
    let value =
      oldValue + listener.step * stepsToDifference * Math.sign(newDelta);
    value = Math.max(value, listener.min);
    value = Math.min(value, listener.max);
 
    if (value !== oldValue) {
      // Update value
      listener.setValue(value);
      incrementalDelta.set(listener, 0);
    } else Iif (
      (value === listener.min && newDelta < 0) ||
      (value === listener.max && newDelta > 0)
    ) {
      // Do not allow incremental delta to go past range
      incrementalDelta.set(listener, 0);
    } else {
      // Store delta for the next iteration
      incrementalDelta.set(listener, newDelta);
    }
  }
 
  // Public API methods
 
  // min:number = minimum allowable value
  // max:number = maximum allowable value
  // step:number = value per step -- smaller = more steps over a given distance, larger = fewer steps over a given distance
  // getValue:fn = function that returns current value
  // setValue:fn = function to set value
  // scale:number = scale value is applied to mouse event to allow users accelerate or decelerate delta without emitting more events
  //-------------------------------------------------------------------------
  publicAPI.setHorizontalListener = (
    min,
    max,
    step,
    getValue,
    setValue,
    scale = 1,
    exponentialScroll = false
  ) => {
    const getFn = Number.isFinite(getValue) ? () => getValue : getValue;
    model.horizontalListener = {
      min,
      max,
      step,
      getValue: getFn,
      setValue,
      scale,
      exponentialScroll,
    };
    incrementalDelta.set(model.horizontalListener, 0);
    publicAPI.modified();
  };
 
  //-------------------------------------------------------------------------
  publicAPI.setVerticalListener = (
    min,
    max,
    step,
    getValue,
    setValue,
    scale = 1,
    exponentialScroll = false
  ) => {
    const getFn = Number.isFinite(getValue) ? () => getValue : getValue;
    model.verticalListener = {
      min,
      max,
      step,
      getValue: getFn,
      setValue,
      scale,
      exponentialScroll,
    };
    incrementalDelta.set(model.verticalListener, 0);
    publicAPI.modified();
  };
 
  //-------------------------------------------------------------------------
  publicAPI.setScrollListener = (
    min,
    max,
    step,
    getValue,
    setValue,
    scale = 1,
    exponentialScroll = false
  ) => {
    Iif (step < 0) {
      vtkWarningMacro(
        'Value of step cannot be negative. If you want to invert the scrolling direction, use a negative scale value instead.'
      );
    }
    const stepSize = Math.abs(step);
    const getFn = Number.isFinite(getValue) ? () => getValue : getValue;
    model.scrollListener = {
      min,
      max,
      step: stepSize,
      getValue: getFn,
      setValue,
      scale,
      exponentialScroll,
    };
    incrementalDelta.set(model.scrollListener, 0);
    publicAPI.modified();
  };
 
  //-------------------------------------------------------------------------
  publicAPI.removeHorizontalListener = () => {
    if (model.horizontalListener) {
      incrementalDelta.delete(model.horizontalListener);
      delete model.horizontalListener;
      publicAPI.modified();
    }
  };
 
  //-------------------------------------------------------------------------
  publicAPI.removeVerticalListener = () => {
    if (model.verticalListener) {
      incrementalDelta.delete(model.verticalListener);
      delete model.verticalListener;
      publicAPI.modified();
    }
  };
 
  //-------------------------------------------------------------------------
  publicAPI.removeScrollListener = () => {
    if (model.scrollListener) {
      incrementalDelta.delete(model.scrollListener);
      delete model.scrollListener;
      publicAPI.modified();
    }
  };
 
  //-------------------------------------------------------------------------
  publicAPI.removeAllListeners = () => {
    publicAPI.removeHorizontalListener();
    publicAPI.removeVerticalListener();
    publicAPI.removeScrollListener();
  };
 
  //-------------------------------------------------------------------------
  publicAPI.onButtonDown = (interactor, renderer, position) => {
    model.previousPosition = position;
    model.interactionNetDelta = 0;
    const glRenderWindow = interactor.getView();
    // Ratio is the dom size vs renderwindow size
    const ratio =
      glRenderWindow.getContainerSize()[0] / glRenderWindow.getSize()[0];
    // Get proper pixel range used by viewport in rw size space
    const size = glRenderWindow.getViewportSize(renderer);
    // rescale size to match mouse event position
    model.containerSize = size.map((v) => v * ratio);
  };
 
  publicAPI.onButtonUp = (interactor) => {
    interactor.exitPointerLock();
  };
 
  //--------------------------------------------------------------------------
 
  // TODO: at some point, this should perhaps be done in
  // RenderWindowInteractor instead of here.
  // We need to hook into mousemove directly for two reasons:
  // 1. We need to keep receiving mouse move events after the mouse button
  //    is released. This is currently not possible with
  //    vtkInteractorStyleManipulator.
  // 2. Since the mouse is stationary in pointer lock mode, we need the
  //    event.movementX and event.movementY info, which are not currently
  //    passed via interactor.onMouseMove.
  publicAPI.startPointerLockEvent = (interactor, renderer) => {
    const handlePointerLockMove = (event) => {
      publicAPI.onPointerLockMove(interactor, renderer, event);
    };
 
    document.addEventListener('mousemove', handlePointerLockMove);
 
    let subscription = null;
    const endInteraction = () => {
      document.removeEventListener('mousemove', handlePointerLockMove);
      subscription?.unsubscribe();
    };
    subscription = interactor?.onEndPointerLock(endInteraction);
  };
 
  publicAPI.onPointerLockMove = (interactor, renderer, event) => {
    // There is a slight delay between the `onEndPointerLock` call
    // and the last `onMouseMove` event, we must make sure the pointer
    // is still locked before we run this logic otherwise we may
    // get a `onMouseMove` call after the pointer has been unlocked.
    if (!interactor.isPointerLocked()) return;
 
    // previousPosition could be undefined if for some reason the
    // `startPointerLockEvent` method is called before the `onButtonDown` one.
    if (model.previousPosition == null) return;
 
    model.previousPosition.x += event.movementX;
    model.previousPosition.y += event.movementY;
 
    publicAPI.onMouseMove(interactor, renderer, model.previousPosition);
  };
 
  //-------------------------------------------------------------------------
  publicAPI.onMouseMove = (interactor, renderer, position) => {
    if (!model.verticalListener && !model.horizontalListener) {
      return;
    }
 
    // We only want to initialize the pointer lock listener
    // after the user starts moving their mouse, this way
    // we don't interfere with other events such as doubleClick,
    // for this reason we don't call this from `onButtonDown`
    if (model.usePointerLock && !interactor.isPointerLocked()) {
      interactor.requestPointerLock();
      publicAPI.startPointerLockEvent(interactor, renderer);
    }
 
    if (!position) {
      return;
    }
 
    if (model.horizontalListener) {
      const dxNorm =
        (position.x - model.previousPosition.x) / model.containerSize[0];
      const dx = scaleDeltaToRange(model.horizontalListener, dxNorm);
      model.interactionNetDelta += dx;
      processDelta(model.horizontalListener, dx);
    }
    if (model.verticalListener) {
      const dyNorm =
        (position.y - model.previousPosition.y) / model.containerSize[1];
      const dy = scaleDeltaToRange(model.verticalListener, dyNorm);
      model.interactionNetDelta += dy;
      processDelta(model.verticalListener, dy);
    }
 
    model.previousPosition = position;
  };
 
  //-------------------------------------------------------------------------
  publicAPI.onScroll = (interactor, renderer, delta) => {
    Iif (!model.scrollListener || !delta) {
      return;
    }
    model.interactionNetDelta += delta * model.scrollListener.step;
    processDelta(model.scrollListener, delta * model.scrollListener.step);
  };
 
  publicAPI.onStartScroll = () => {
    model.interactionNetDelta = 0;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  horizontalListener: null,
  verticalListener: null,
  scrollListener: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  macro.obj(publicAPI, model);
  vtkCompositeMouseManipulator.extend(publicAPI, model, initialValues);
 
  // Create get-set macros
  macro.setGet(publicAPI, model, ['usePointerLock']);
 
  // Object specific methods
  vtkMouseRangeManipulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkMouseRangeManipulator'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };