calculateMovement method

dynamic calculateMovement({
  1. required BuildContext context,
  2. required ScaleUpdateDetails detail,
  3. required Layer activeLayer,
  4. required bool configEnabledHitVibration,
  5. required GlobalKey<State<StatefulWidget>> removeAreaKey,
  6. required dynamic onHoveredRemoveChanged(
    1. bool
    ),
})

Calculates movement of a layer based on user interactions, considering various conditions such as hit areas and screen boundaries.

Implementation

calculateMovement({
  required BuildContext context,
  required ScaleUpdateDetails detail,
  required Layer activeLayer,
  required bool configEnabledHitVibration,
  required GlobalKey removeAreaKey,
  required Function(bool) onHoveredRemoveChanged,
}) {
  if (_activeScale) return;

  RenderBox? box =
      removeAreaKey.currentContext?.findRenderObject() as RenderBox?;
  if (box != null) {
    Offset position = box.localToGlobal(Offset.zero);
    bool hit = Rect.fromLTWH(
      position.dx,
      position.dy,
      box.size.width,
      box.size.height,
    ).contains(detail.focalPoint);
    if (hoverRemoveBtn != hit) {
      hoverRemoveBtn = hit;
      onHoveredRemoveChanged.call(hoverRemoveBtn);
    }
  }

  activeLayer.offset = Offset(
    activeLayer.offset.dx + detail.focalPointDelta.dx,
    activeLayer.offset.dy + detail.focalPointDelta.dy,
  );

  bool vibarate = false;
  double posX = activeLayer.offset.dx;
  double posY = activeLayer.offset.dy;

  bool hitAreaX = detail.focalPoint.dx >= snapStartPosX - hitSpan &&
      detail.focalPoint.dx <= snapStartPosX + hitSpan;
  bool hitAreaY = detail.focalPoint.dy >= snapStartPosY - hitSpan &&
      detail.focalPoint.dy <= snapStartPosY + hitSpan;

  bool helperGoNearLineLeft =
      posX >= 0 && lastPositionX == LayerLastPosition.left;
  bool helperGoNearLineRight =
      posX <= 0 && lastPositionX == LayerLastPosition.right;
  bool helperGoNearLineTop =
      posY >= 0 && lastPositionY == LayerLastPosition.top;
  bool helperGoNearLineBottom =
      posY <= 0 && lastPositionY == LayerLastPosition.bottom;

  /// Calc vertical helper line
  if ((!showVerticalHelperLine &&
          (helperGoNearLineLeft || helperGoNearLineRight)) ||
      (showVerticalHelperLine && hitAreaX)) {
    if (!showVerticalHelperLine) {
      vibarate = true;
      snapStartPosX = detail.focalPoint.dx;
    }
    showVerticalHelperLine = true;
    activeLayer.offset = Offset(0, activeLayer.offset.dy);
    lastPositionX = LayerLastPosition.center;
  } else {
    showVerticalHelperLine = false;
    lastPositionX =
        posX <= 0 ? LayerLastPosition.left : LayerLastPosition.right;
  }

  /// Calc horizontal helper line
  if ((!showHorizontalHelperLine &&
          (helperGoNearLineTop || helperGoNearLineBottom)) ||
      (showHorizontalHelperLine && hitAreaY)) {
    if (!showHorizontalHelperLine) {
      vibarate = true;
      snapStartPosY = detail.focalPoint.dy;
    }
    showHorizontalHelperLine = true;
    activeLayer.offset = Offset(activeLayer.offset.dx, 0);
    lastPositionY = LayerLastPosition.center;
  } else {
    showHorizontalHelperLine = false;
    lastPositionY =
        posY <= 0 ? LayerLastPosition.top : LayerLastPosition.bottom;
  }

  if (configEnabledHitVibration && vibarate) {
    _lineHitVibrate();
  }
}