pointerMove method

dynamic pointerMove(
  1. Pointer pointer
)

Implementation

pointerMove(Pointer pointer) {
  // TODO if not when change axis will cause object position change. why???
  if (pointer.x == _pointer0?.x &&
      pointer.y == _pointer0?.y &&
      pointer.button == _pointer0?.button) {
    return;
  }
  _pointer0 = pointer;

  var axis = this.axis;
  var mode = this.mode;
  var object = this.object;
  var space = this.space;

  if (mode == 'scale') {
    space = 'local';
  } else if (axis == 'E' || axis == 'XYZE' || axis == 'XYZ') {
    space = 'world';
  }

  if (object == null ||
      axis == null ||
      this.dragging == false ||
      pointer.button != 1) return;

  _raycaster.setFromCamera(Vector2(pointer.x, pointer.y), this.camera);

  var planeIntersect = intersectObjectWithRay(this._plane, _raycaster, true);

  if (planeIntersect == null || planeIntersect == false) return;

  this.pointEnd.copy(planeIntersect.point).sub(this.worldPositionStart);

  if (mode == 'translate') {
    // Apply translate

    this._offset.copy(this.pointEnd).sub(this.pointStart);

    if (space == 'local' && axis != 'XYZ') {
      this._offset.applyQuaternion(this._worldQuaternionInv);
    }

    if (axis.indexOf('X') == -1) this._offset.x = 0;
    if (axis.indexOf('Y') == -1) this._offset.y = 0;
    if (axis.indexOf('Z') == -1) this._offset.z = 0;

    if (space == 'local' && axis != 'XYZ') {
      this
          ._offset
          .applyQuaternion(this._quaternionStart)
          .divide(this._parentScale);
    } else {
      this
          ._offset
          .applyQuaternion(this._parentQuaternionInv)
          .divide(this._parentScale);
    }

    object.position.copy(this._offset).add(this._positionStart);

    // Apply translation snap

    if (this.translationSnap != null) {
      if (space == 'local') {
        object.position.applyQuaternion(
            _tempQuaternion.copy(this._quaternionStart).invert());

        if (axis.indexOf('X') != -1) {
          object.position.x =
              Math.round(object.position.x / this.translationSnap) *
                  this.translationSnap;
        }

        if (axis.indexOf('Y') != -1) {
          object.position.y =
              Math.round(object.position.y / this.translationSnap) *
                  this.translationSnap;
        }

        if (axis.indexOf('Z') != -1) {
          object.position.z =
              Math.round(object.position.z / this.translationSnap) *
                  this.translationSnap;
        }

        object.position.applyQuaternion(this._quaternionStart);
      }

      if (space == 'world') {
        if (object.parent != null) {
          var _vec =
              _tempVector.setFromMatrixPosition(object.parent.matrixWorld);
          object.position.add(
              _tempVector.setFromMatrixPosition(object.parent.matrixWorld));
        }

        if (axis.indexOf('X') != -1) {
          object.position.x =
              Math.round(object.position.x / this.translationSnap) *
                  this.translationSnap;
        }

        if (axis.indexOf('Y') != -1) {
          object.position.y =
              Math.round(object.position.y / this.translationSnap) *
                  this.translationSnap;
        }

        if (axis.indexOf('Z') != -1) {
          object.position.z =
              Math.round(object.position.z / this.translationSnap) *
                  this.translationSnap;
        }

        if (object.parent != null) {
          object.position.sub(
              _tempVector.setFromMatrixPosition(object.parent.matrixWorld));
        }
      }
    }
  } else if (mode == 'scale') {
    if (axis.indexOf('XYZ') != -1) {
      var d = this.pointEnd.length() / this.pointStart.length();

      if (this.pointEnd.dot(this.pointStart) < 0) d *= -1;

      _tempVector2.set(d, d, d);
    } else {
      _tempVector.copy(this.pointStart);
      _tempVector2.copy(this.pointEnd);

      _tempVector.applyQuaternion(this._worldQuaternionInv);
      _tempVector2.applyQuaternion(this._worldQuaternionInv);

      _tempVector2.divide(_tempVector);

      if (axis.indexOf('X') == -1) {
        _tempVector2.x = 1;
      }

      if (axis.indexOf('Y') == -1) {
        _tempVector2.y = 1;
      }

      if (axis.indexOf('Z') == -1) {
        _tempVector2.z = 1;
      }
    }

    // Apply scale

    object.scale.copy(this._scaleStart).multiply(_tempVector2);

    if (this.scaleSnap != null) {
      if (axis.indexOf('X') != -1) {
        var _x = Math.round(object.scale.x / this.scaleSnap) * this.scaleSnap;

        object.scale.x = _x != 0 ? _x : this.scaleSnap;
      }

      if (axis.indexOf('Y') != -1) {
        var _y = Math.round(object.scale.y / this.scaleSnap) * this.scaleSnap;

        object.scale.y = _y != 0 ? _y : this.scaleSnap;
      }

      if (axis.indexOf('Z') != -1) {
        var _z = Math.round(object.scale.z / this.scaleSnap) * this.scaleSnap;

        object.scale.z = _z != 0 ? _z : this.scaleSnap;
      }
    }
  } else if (mode == 'rotate') {
    this._offset.copy(this.pointEnd).sub(this.pointStart);

    var ROTATION_SPEED = 20 /
        this.worldPosition.distanceTo(
            _tempVector.setFromMatrixPosition(this.camera.matrixWorld));

    if (axis == 'E') {
      this.rotationAxis.copy(this.eye);
      this.rotationAngle = this.pointEnd.angleTo(this.pointStart);

      this._startNorm.copy(this.pointStart).normalize();
      this._endNorm.copy(this.pointEnd).normalize();

      this.rotationAngle *=
          (this._endNorm.cross(this._startNorm).dot(this.eye) < 0 ? 1 : -1);
    } else if (axis == 'XYZE') {
      this.rotationAxis.copy(this._offset).cross(this.eye).normalize();
      this.rotationAngle = this
              ._offset
              .dot(_tempVector.copy(this.rotationAxis).cross(this.eye)) *
          ROTATION_SPEED;
    } else if (axis == 'X' || axis == 'Y' || axis == 'Z') {
      this.rotationAxis.copy(_unit[axis]);

      _tempVector.copy(_unit[axis]);

      if (space == 'local') {
        _tempVector.applyQuaternion(this.worldQuaternion);
      }

      this.rotationAngle =
          this._offset.dot(_tempVector.cross(this.eye).normalize()) *
              ROTATION_SPEED;
    }

    // Apply rotation snap

    if (this.rotationSnap != null)
      this.rotationAngle =
          Math.round(this.rotationAngle / this.rotationSnap) *
              this.rotationSnap;

    // Apply rotate
    if (space == 'local' && axis != 'E' && axis != 'XYZE') {
      object.quaternion.copy(this._quaternionStart);
      object.quaternion
          .multiply(_tempQuaternion.setFromAxisAngle(
              this.rotationAxis, this.rotationAngle))
          .normalize();
    } else {
      this.rotationAxis.applyQuaternion(this._parentQuaternionInv);
      object.quaternion.copy(_tempQuaternion.setFromAxisAngle(
          this.rotationAxis, this.rotationAngle));
      object.quaternion.multiply(this._quaternionStart).normalize();
    }
  }

  this.dispatchEvent(_changeEvent);
  this.dispatchEvent(_objectChangeEvent);
}