onContact method

void onContact(
  1. Offset position,
  2. double cellDim
)

Implementation

void onContact(Offset position, double cellDim) {
  final rb = gk.currentContext?.findRenderObject() as RenderBox?;
  if (rb == null) return;
  final globalOffset = rb.localToGlobal(Offset.zero);
  final rect = globalOffset & rb.size;
  if (!rect.contains(position)) return;
  position -= globalOffset;
  final (x, y) = (position.dx ~/ cellDim, position.dy ~/ cellDim);
  if (!_isWithinCellBounds(
    position.dx - x * cellDim,
    position.dy - y * cellDim,
    cellDim,
  )) return;
  if (currentPattern.isNotEmpty) {
    final last = currentPattern.last - 1;
    final (lx, ly) = (last % widget.width, last ~/ widget.width);
    final (dx, dy) = ((x - lx).abs(), (y - ly).abs());
    // horizontal, vertical, or diagonal skips are never allowed
    if (dy == 0 && dx > 1) return;
    if (dx == 0 && dy > 1) return;
    if (dy > 1 && dx > 1 && dy == dx) return;
    final max = widget.linkageSettings.maxLinkDistance;
    if (math.max(dy, dx) > max) return;
  }

  final el = y * widget.width + x + 1;
  if (!currentPattern.contains(el)) {
    if (widget.hapticFeedback) {
      HapticFeedback.lightImpact();
    }
    currentPattern.add(el);
    widget.onUpdate?.call(currentPattern);
    cellACs[el - 1].forward();
    setState(() {});
  }
}