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 =
            _screenDestinationRect!.topIsSame(screenCropRect!);
        final bool leftSame =
            _screenDestinationRect!.leftIsSame(screenCropRect!);
        final bool bottomSame =
            _screenDestinationRect!.bottomIsSame(screenCropRect!);
        final bool rightSame =
            _screenDestinationRect!.rightIsSame(screenCropRect!);

        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 = rect.topIsSame(screenCropRect!);
        final bool leftSame = rect.leftIsSame(screenCropRect!);
        final bool bottomSame = rect.bottomIsSame(screenCropRect!);
        final bool rightSame = rect.rightIsSame(screenCropRect!);

        // 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);
        // init totalScale
        if (_rawDestinationRect!.isSame(_rawDestinationRect!)) {
          totalScale = 1.0;
        }
        preTotalScale = totalScale;
        _screenDestinationRect = rect;
      }
    }
  } else {
    _screenDestinationRect = getRectWithScale(_rawDestinationRect!);
    _screenDestinationRect =
        computeBoundary(_screenDestinationRect!, screenCropRect!);
  }
  return _screenDestinationRect!;
}