getFinalDestinationRect method

Rect getFinalDestinationRect()

Implementation

Rect getFinalDestinationRect() {
  _reachCropRectEdge = false;

  if (screenDestinationRect != null) {
    /// scale
    final double scaleDelta = totalScale / preTotalScale;
    if (scaleDelta != 1.0) {
      Offset focalPoint = screenFocalPoint ?? _screenDestinationRect!.center;
      focalPoint = Offset(
        focalPoint.dx
            .clamp(
                _screenDestinationRect!.left, _screenDestinationRect!.right)
            .toDouble(),
        focalPoint.dy
            .clamp(
                _screenDestinationRect!.top, _screenDestinationRect!.bottom)
            .toDouble(),
      );

      _screenDestinationRect = Rect.fromLTWH(
          focalPoint.dx -
              (focalPoint.dx - _screenDestinationRect!.left) * scaleDelta,
          focalPoint.dy -
              (focalPoint.dy - _screenDestinationRect!.top) * scaleDelta,
          _screenDestinationRect!.width * scaleDelta,
          _screenDestinationRect!.height * scaleDelta);
      preTotalScale = totalScale;
      delta = Offset.zero;
    }

    /// move
    else {
      if (_screenDestinationRect != screenCropRect) {
        final bool topSame =
            doubleEqual(_screenDestinationRect!.top, screenCropRect!.top);
        final bool leftSame =
            doubleEqual(_screenDestinationRect!.left, screenCropRect!.left);
        final bool bottomSame = doubleEqual(
            _screenDestinationRect!.bottom, screenCropRect!.bottom);
        final bool rightSame =
            doubleEqual(_screenDestinationRect!.right, screenCropRect!.right);
        if (topSame && bottomSame) {
          delta = Offset(delta.dx, 0.0);
        } else if (leftSame && rightSame) {
          delta = Offset(0.0, delta.dy);
        }

        _screenDestinationRect = _screenDestinationRect!.shift(delta);
      }
      //we have shift offset, we should clear delta.
      delta = Offset.zero;
    }

    _screenDestinationRect =
        computeBoundary(_screenDestinationRect!, screenCropRect!);

    // make sure that crop rect is all in image rect.
    if (screenCropRect != null) {
      Rect rect = screenCropRect!.expandToInclude(_screenDestinationRect!);
      if (rect != _screenDestinationRect) {
        final bool topSame = doubleEqual(rect.top, screenCropRect!.top);
        final bool leftSame = doubleEqual(rect.left, screenCropRect!.left);
        final bool bottomSame =
            doubleEqual(rect.bottom, screenCropRect!.bottom);
        final bool rightSame = doubleEqual(rect.right, screenCropRect!.right);

        // make sure that image rect keep same aspect ratio
        if (topSame && bottomSame) {
          rect = Rect.fromCenter(
              center: rect.center,
              width: rect.height /
                  _screenDestinationRect!.height *
                  _screenDestinationRect!.width,
              height: rect.height);
          _reachCropRectEdge = true;
        } else if (leftSame && rightSame) {
          rect = Rect.fromCenter(
            center: rect.center,
            width: rect.width,
            height: rect.width /
                _screenDestinationRect!.width *
                _screenDestinationRect!.height,
          );
          _reachCropRectEdge = true;
        }
        totalScale =
            totalScale / (rect.width / _screenDestinationRect!.width);
        preTotalScale = totalScale;
        _screenDestinationRect = rect;
      }
    }
  } else {
    _screenDestinationRect = getRectWithScale(_rawDestinationRect!);
    _screenDestinationRect =
        computeBoundary(_screenDestinationRect!, screenCropRect!);
  }
  return _screenDestinationRect!;
}