getSnapTarget method

Offset getSnapTarget()

Implementation

Offset getSnapTarget() {
  if (snapTargets == null)
    return deltaNotifier.value;
  else {
    Map<Offset, double> map = Map<Offset, double>();
    snapTargets!.forEach((SnapTarget snapTarget) {
      if (Pivot.isClosestHorizontal(snapTarget.viewPivot) || Pivot.isClosestAny(snapTarget.viewPivot)) {
        Offset left = Offset(0 - viewOrigin!.dx + boundOrigin!.dx, deltaNotifier.value.dy.clamp(maxTop, maxBottom));
        map[left] = Point(deltaNotifier.value.dx, deltaNotifier.value.dy).distanceTo(Point(left.dx, left.dy));
        if (_debugLevel > 1) {
          print("--------------------");
          print("Left");
          print(snapTarget.boundPivot);
          print(snapTarget.viewPivot);
          print(boundWidth);
          print(boundHeight);
          print(boundOrigin);
          print(viewWidth);
          print(viewHeight);
          print(viewOrigin);
          print(left);
          print("--------------------");
        }
        Offset right = Offset(boundWidth + -viewOrigin!.dx + boundOrigin!.dx - viewWidth, deltaNotifier.value.dy.clamp(maxTop, maxBottom));
        map[right] = Point(deltaNotifier.value.dx, deltaNotifier.value.dy).distanceTo(Point(right.dx, right.dy));
        if (_debugLevel > 1) {
          print("--------------------");
          print("Right");
          print(snapTarget.boundPivot);
          print(snapTarget.viewPivot);
          print(boundWidth);
          print(boundHeight);
          print(boundOrigin);
          print(viewWidth);
          print(viewHeight);
          print(viewOrigin);
          print(right);
          print("--------------------");
        }
      }
      if (Pivot.isClosestVertical(snapTarget.viewPivot) || Pivot.isClosestAny(snapTarget.viewPivot)) {
        Offset top = Offset(deltaNotifier.value.dx.clamp(maxLeft, maxRight), 0 - viewOrigin!.dy + boundOrigin!.dy);
        map[top] = Point(deltaNotifier.value.dx, deltaNotifier.value.dy).distanceTo(Point(top.dx, top.dy));
        if (_debugLevel > 1) {
          print("--------------------");
          print("Top");
          print(snapTarget.boundPivot);
          print(snapTarget.viewPivot);
          print(boundWidth);
          print(boundHeight);
          print(boundOrigin);
          print(viewWidth);
          print(viewHeight);
          print(viewOrigin);
          print(top);
          print("--------------------");
        }
        Offset bottom = Offset(deltaNotifier.value.dx.clamp(maxLeft, maxRight), boundHeight + -viewOrigin!.dy + boundOrigin!.dy - viewHeight);
        map[bottom] = Point(deltaNotifier.value.dx, deltaNotifier.value.dy).distanceTo(Point(bottom.dx, bottom.dy));
        if (_debugLevel > 1) {
          print("--------------------");
          print("Bottom");
          print(snapTarget.boundPivot);
          print(snapTarget.viewPivot);
          print(boundWidth);
          print(boundHeight);
          print(boundOrigin);
          print(viewWidth);
          print(viewHeight);
          print(viewOrigin);
          print(bottom);
          print("--------------------");
        }
      }
      if (!Pivot.isClosestHorizontal(snapTarget.viewPivot) && !Pivot.isClosestVertical(snapTarget.viewPivot) && !Pivot.isClosestAny(snapTarget.viewPivot)) {
        Offset offset = Offset(boundWidth * snapTarget.boundPivot.dx + boundOrigin!.dx - viewWidth * snapTarget.viewPivot.dx - viewOrigin!.dx, boundHeight * snapTarget.boundPivot.dy + boundOrigin!.dy - viewHeight * snapTarget.viewPivot.dy - viewOrigin!.dy);
        if (_debugLevel > 1) {
          print("--------------------");
          print(snapTarget.boundPivot);
          print(snapTarget.viewPivot);
          print(boundWidth);
          print(boundHeight);
          print(boundOrigin);
          print(viewWidth);
          print(viewHeight);
          print(viewOrigin);
          print(offset);
          print("--------------------");
        }
        map[offset] = Point(deltaNotifier.value.dx, deltaNotifier.value.dy).distanceTo(Point(offset.dx, offset.dy));
      }
    });

    print(map.values.reduce(min));
    if (map.values.reduce(min) > minSnapDistance) {
      return deltaNotifier.value;
    }
    return map.keys.firstWhere((Offset offset) {
      return map[offset] == map.values.reduce(min);
    });
  }
}