From c9709b3de77548c4b2c166fc00afa4850b04f5c5 Mon Sep 17 00:00:00 2001 From: nateshmbhat Date: Thu, 23 Apr 2020 19:58:58 +0530 Subject: [PATCH 1/3] added hittestbehavior --- example/example.dart | 1 + lib/src/shape_handler.dart | 39 +++++++++++------ lib/src/shapes/arc.dart | 5 ++- lib/src/shapes/circle.dart | 3 +- lib/src/shapes/line.dart | 4 +- lib/src/shapes/oval.dart | 4 +- lib/src/shapes/path.dart | 4 +- lib/src/shapes/point.dart | 4 +- lib/src/shapes/rectangle.dart | 4 +- lib/src/shapes/rounded_rectangle.dart | 4 +- lib/src/shapes/shape.dart | 3 ++ lib/src/touchy_canvas.dart | 61 +++++++++++++++++++++++++-- lib/src/types/types.dart | 4 -- test/matter_test.dart | 3 +- test/shape_handler/shape_handler.dart | 10 +++++ test/shapes/rectangle.dart | 31 ++++++++++++++ 16 files changed, 148 insertions(+), 36 deletions(-) create mode 100644 test/shape_handler/shape_handler.dart diff --git a/example/example.dart b/example/example.dart index a148a38..bc95d04 100644 --- a/example/example.dart +++ b/example/example.dart @@ -4,6 +4,7 @@ import 'package:touchable/touchable.dart'; class MyExampleWidget extends StatelessWidget { @override Widget build(BuildContext context) { + FocusScope.of(context).nextFocus() ; return Flexible( child: FractionallySizedBox( widthFactor: 1, diff --git a/lib/src/shape_handler.dart b/lib/src/shape_handler.dart index c4ede86..841de75 100644 --- a/lib/src/shape_handler.dart +++ b/lib/src/shape_handler.dart @@ -47,27 +47,40 @@ class ShapeHandler { return true; } - Shape _getTopShapeBelowTouchPoint(Offset point) { + List _getTouchedShapes(Offset point) { + var selectedShapes = [] ; for (int i = _shapeStack.length - 1; i >= 0; i--) { - if (_shapeStack[i].isInside(point)) { - if (_isPointInsideClipShapes(_getClipShapesBelowPosition(i), point) == - false) return null; - return _shapeStack[i]; + var shape = _shapeStack[i] ; + if(shape.hitTestBehavior==HitTestBehavior.deferToChild) { + continue ; + } + if (shape.isInside(point)) { + if (_isPointInsideClipShapes(_getClipShapesBelowPosition(i), point) == false) { + if(shape.hitTestBehavior==HitTestBehavior.opaque) { + return selectedShapes; + } + continue ; + } + selectedShapes.add(shape); + if(shape.hitTestBehavior==HitTestBehavior.opaque) { + return selectedShapes ; + } } } - return null; + return selectedShapes; } Future handleGestureEvent(Gesture gesture) async { - var touchPoint = - TouchCanvasUtil.getPointFromGestureDetail(gesture.gestureDetail); + var touchPoint = TouchCanvasUtil.getPointFromGestureDetail(gesture.gestureDetail); if (!_registeredGestures.contains(gesture.gestureType)) return; - var touchedShape = _getTopShapeBelowTouchPoint(touchPoint); - if (touchedShape == null) return; - if (touchedShape.registeredGestures.contains(gesture.gestureType)) { - var callback = touchedShape.getCallbackFromGesture(gesture); - callback(); + var touchedShapes = _getTouchedShapes(touchPoint); + if (touchedShapes.isEmpty) return; + for(var touchedShape in touchedShapes){ + if (touchedShape.registeredGestures.contains(gesture.gestureType)) { + var callback = touchedShape.getCallbackFromGesture(gesture); + callback(); + } } } } diff --git a/lib/src/shapes/arc.dart b/lib/src/shapes/arc.dart index 549e4ad..7e383f1 100644 --- a/lib/src/shapes/arc.dart +++ b/lib/src/shapes/arc.dart @@ -3,6 +3,7 @@ import 'dart:math'; import 'dart:ui'; +import 'package:flutter/src/rendering/proxy_box.dart'; import 'package:touchable/src/shapes/constant.dart'; import 'package:touchable/src/shapes/line.dart'; import 'package:touchable/src/shapes/oval.dart'; @@ -29,8 +30,8 @@ class Arc extends Shape { Offset _arcEndPoint; Arc(this.rect, this.startAngle, this.sweepAngle, this.useCenter, - {Paint paint, Map gestureMap}) - : super(paint: paint, gestureCallbackMap: gestureMap) { + {Paint paint, Map gestureMap, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) + : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap) { _oval = Oval(rect, paint: paint); if (sweepAngle < 0) { diff --git a/lib/src/shapes/circle.dart b/lib/src/shapes/circle.dart index 52ee883..ac491b9 100644 --- a/lib/src/shapes/circle.dart +++ b/lib/src/shapes/circle.dart @@ -15,9 +15,10 @@ class Circle extends Shape { Circle( {@required this.center, @required this.radius, + HitTestBehavior hitTestBehavior, Map gestureMap, Paint paint}) - : super(paint: paint, gestureCallbackMap: gestureMap); + : super(paint: paint, gestureCallbackMap: gestureMap , hitTestBehavior : hitTestBehavior); // (x-a)^2 + (y-b)^2 = r^2 @override diff --git a/lib/src/shapes/line.dart b/lib/src/shapes/line.dart index f3fcd40..98c5cac 100644 --- a/lib/src/shapes/line.dart +++ b/lib/src/shapes/line.dart @@ -13,8 +13,8 @@ class Line extends Shape { final Offset p2; double a, b, c; // Equation ax+by = c - Line(this.p1, this.p2, {Map gestureMap, Paint paint}) - : super(paint: paint, gestureCallbackMap: gestureMap) { + Line(this.p1, this.p2, {Map gestureMap, Paint paint , HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) + : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap) { a = p2.dy - p1.dy; b = p1.dx - p2.dx; c = a * p1.dx + b * p1.dy; diff --git a/lib/src/shapes/oval.dart b/lib/src/shapes/oval.dart index e79d7e2..38a2e7f 100644 --- a/lib/src/shapes/oval.dart +++ b/lib/src/shapes/oval.dart @@ -14,8 +14,8 @@ class Oval extends Shape { double a, b; // x^2/a^2 + y^2/b^2 = 1 - Oval(this.rect, {Map gestureMap, Paint paint}) - : super(paint: paint, gestureCallbackMap: gestureMap) { + Oval(this.rect, {Map gestureMap, Paint paint, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) + : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap) { a = rect.right - rect.center.dx; b = rect.center.dy - rect.top; } diff --git a/lib/src/shapes/path.dart b/lib/src/shapes/path.dart index 284469a..a0d5dbf 100644 --- a/lib/src/shapes/path.dart +++ b/lib/src/shapes/path.dart @@ -9,8 +9,8 @@ import 'package:touchable/src/types/types.dart'; class PathShape extends Shape { final Path path; - PathShape(this.path, {Map gestureMap, Paint paint}) - : super(paint: paint, gestureCallbackMap: gestureMap); + PathShape(this.path, {Map gestureMap, Paint paint, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) + : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap); @override bool isInside(Offset p) { diff --git a/lib/src/shapes/point.dart b/lib/src/shapes/point.dart index 13f3f51..a7c0b37 100644 --- a/lib/src/shapes/point.dart +++ b/lib/src/shapes/point.dart @@ -14,8 +14,8 @@ class Point extends Shape { final List points; Point(this.pointMode, this.points, - {Map gestureMap, Paint paint}) - : super(paint: paint, gestureCallbackMap: gestureMap); + {Map gestureMap, Paint paint, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) + : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap); @override bool isInside(Offset p) { diff --git a/lib/src/shapes/rectangle.dart b/lib/src/shapes/rectangle.dart index 0514169..a7836e6 100644 --- a/lib/src/shapes/rectangle.dart +++ b/lib/src/shapes/rectangle.dart @@ -8,8 +8,8 @@ import 'package:touchable/src/types/types.dart'; class Rectangle extends Shape { final Rect rect; - Rectangle(this.rect, {Map gestureMap, Paint paint}) - : super(paint: paint, gestureCallbackMap: gestureMap); + Rectangle(this.rect, {Map gestureMap, Paint paint, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) + : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap); @override bool isInside(Offset p) { diff --git a/lib/src/shapes/rounded_rectangle.dart b/lib/src/shapes/rounded_rectangle.dart index 4f0c5ce..b3e9dc5 100644 --- a/lib/src/shapes/rounded_rectangle.dart +++ b/lib/src/shapes/rounded_rectangle.dart @@ -9,8 +9,8 @@ class RoundedRectangle extends Shape { final RRect rRect; RoundedRectangle(this.rRect, - {Paint paint, Map gestureMap}) - : super(paint: paint, gestureCallbackMap: gestureMap); + {Paint paint, Map gestureMap, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) + : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap); @override bool isInside(Offset p) { diff --git a/lib/src/shapes/shape.dart b/lib/src/shapes/shape.dart index f3d44e4..b08d842 100644 --- a/lib/src/shapes/shape.dart +++ b/lib/src/shapes/shape.dart @@ -10,6 +10,7 @@ import 'package:touchable/touchable.dart'; abstract class Shape { Paint paint; Map gestureCallbackMap; + HitTestBehavior hitTestBehavior ; Set get registeredGestures => gestureCallbackMap?.keys?.toSet() ?? Set(); @@ -17,6 +18,7 @@ abstract class Shape { Shape({ @required this.paint, @required this.gestureCallbackMap, + this.hitTestBehavior, }) { paint ??= Paint() ..strokeWidth = ShapeConstant.floatPrecision @@ -24,6 +26,7 @@ abstract class Shape { if (paint.strokeWidth == 0) { paint.strokeWidth = ShapeConstant.floatPrecision; } + hitTestBehavior ??= HitTestBehavior.opaque; gestureCallbackMap ??= Map(); } diff --git a/lib/src/touchy_canvas.dart b/lib/src/touchy_canvas.dart index cfd5e19..3736ea6 100644 --- a/lib/src/touchy_canvas.dart +++ b/lib/src/touchy_canvas.dart @@ -55,6 +55,7 @@ class TouchyCanvas { Offset c, double radius, Paint paint, { + HitTestBehavior hitTestBehavior, GestureTapDownCallback onTapDown, GestureTapUpCallback onTapUp, GestureLongPressStartCallback onLongPressStart, @@ -97,7 +98,9 @@ class TouchyCanvas { Offset p1, Offset p2, Paint paint, { + HitTestBehavior hitTestBehavior, GestureTapDownCallback onTapDown, + PaintingStyle paintStyleForTouch, GestureTapUpCallback onTapUp, GestureLongPressStartCallback onLongPressStart, GestureLongPressEndCallback onLongPressEnd, @@ -115,6 +118,8 @@ class TouchyCanvas { _canvas.drawLine(p1, p2, paint); _shapeHandler.addShape(Line(p1, p2, paint: paint, + hitTestBehavior: hitTestBehavior, + gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -136,7 +141,9 @@ class TouchyCanvas { void drawOval( Rect rect, Paint paint, { + HitTestBehavior hitTestBehavior, GestureTapDownCallback onTapDown, + PaintingStyle paintStyleForTouch, GestureTapUpCallback onTapUp, GestureLongPressStartCallback onLongPressStart, GestureLongPressEndCallback onLongPressEnd, @@ -154,6 +161,8 @@ class TouchyCanvas { _canvas.drawOval(rect, paint); _shapeHandler.addShape(Oval(rect, paint: paint, + hitTestBehavior: hitTestBehavior, + gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -181,7 +190,9 @@ class TouchyCanvas { void drawPath( Path path, Paint paint, { + HitTestBehavior hitTestBehavior, GestureTapDownCallback onTapDown, + PaintingStyle paintStyleForTouch, GestureTapUpCallback onTapUp, GestureLongPressStartCallback onLongPressStart, GestureLongPressEndCallback onLongPressEnd, @@ -199,6 +210,8 @@ class TouchyCanvas { _canvas.drawPath(path, paint); _shapeHandler.addShape(PathShape(path, paint: paint, + hitTestBehavior: hitTestBehavior, + gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -221,7 +234,9 @@ class TouchyCanvas { PointMode pointMode, List points, Paint paint, { + HitTestBehavior hitTestBehavior, GestureTapDownCallback onTapDown, + PaintingStyle paintStyleForTouch, GestureTapUpCallback onTapUp, GestureLongPressStartCallback onLongPressStart, GestureLongPressEndCallback onLongPressEnd, @@ -239,6 +254,8 @@ class TouchyCanvas { _canvas.drawPoints(pointMode, points, paint); _shapeHandler.addShape(Point(pointMode, points, paint: paint, + hitTestBehavior: hitTestBehavior, + gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -260,7 +277,9 @@ class TouchyCanvas { void drawRRect( RRect rrect, Paint paint, { + HitTestBehavior hitTestBehavior, GestureTapDownCallback onTapDown, + PaintingStyle paintStyleForTouch, GestureTapUpCallback onTapUp, GestureLongPressStartCallback onLongPressStart, GestureLongPressEndCallback onLongPressEnd, @@ -278,6 +297,8 @@ class TouchyCanvas { _canvas.drawRRect(rrect, paint); _shapeHandler.addShape(RoundedRectangle(rrect, paint: paint, + hitTestBehavior: hitTestBehavior, + gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -300,7 +321,9 @@ class TouchyCanvas { PointMode pointMode, Float32List points, Paint paint, { + HitTestBehavior hitTestBehavior, GestureTapDownCallback onTapDown, + PaintingStyle paintStyleForTouch, GestureTapUpCallback onTapUp, GestureLongPressStartCallback onLongPressStart, GestureLongPressEndCallback onLongPressEnd, @@ -322,6 +345,8 @@ class TouchyCanvas { } _shapeHandler.addShape(Point(pointMode, offsetPoints, paint: paint, + hitTestBehavior: hitTestBehavior, + gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -343,7 +368,9 @@ class TouchyCanvas { void drawRect( Rect rect, Paint paint, { + HitTestBehavior hitTestBehavior, GestureTapDownCallback onTapDown, + PaintingStyle paintStyleForTouch, GestureTapUpCallback onTapUp, GestureLongPressStartCallback onLongPressStart, GestureLongPressEndCallback onLongPressEnd, @@ -361,6 +388,8 @@ class TouchyCanvas { _canvas.drawRect(rect, paint); _shapeHandler.addShape(Rectangle(rect, paint: paint, + hitTestBehavior: hitTestBehavior, + gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -389,7 +418,9 @@ class TouchyCanvas { Image image, Offset p, Paint paint, { + HitTestBehavior hitTestBehavior, GestureTapDownCallback onTapDown, + PaintingStyle paintStyleForTouch, GestureTapUpCallback onTapUp, GestureLongPressStartCallback onLongPressStart, GestureLongPressEndCallback onLongPressEnd, @@ -409,6 +440,8 @@ class TouchyCanvas { Rect.fromLTWH( p.dx, p.dy, image.width.toDouble(), image.height.toDouble()), paint: paint, + hitTestBehavior: hitTestBehavior, + gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -427,12 +460,34 @@ class TouchyCanvas { ))); } - void drawArc(Rect rect, double startAngle, double sweepAngle, bool useCenter, - Paint paint, - {GestureTapDownCallback onTapDown}) { + void drawArc( + Rect rect, + double startAngle, + double sweepAngle, + bool useCenter, + Paint paint, { + HitTestBehavior hitTestBehavior, + GestureTapDownCallback onTapDown, + PaintingStyle paintStyleForTouch, + GestureTapUpCallback onTapUp, + GestureLongPressStartCallback onLongPressStart, + GestureLongPressEndCallback onLongPressEnd, + GestureLongPressMoveUpdateCallback onLongPressMoveUpdate, + GestureForcePressStartCallback onForcePressStart, + GestureForcePressEndCallback onForcePressEnd, + GestureForcePressPeakCallback onForcePressPeak, + GestureForcePressUpdateCallback onForcePressUpdate, + GestureDragStartCallback onPanStart, + GestureDragUpdateCallback onPanUpdate, + GestureDragDownCallback onPanDown, + GestureTapDownCallback onSecondaryTapDown, + GestureTapUpCallback onSecondaryTapUp, + }) { _canvas.drawArc(rect, startAngle, sweepAngle, useCenter, paint); var arc = Arc(rect, startAngle, sweepAngle, useCenter, paint: paint, + hitTestBehavior: hitTestBehavior, + gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: null, diff --git a/lib/src/types/types.dart b/lib/src/types/types.dart index 0dd2a64..e6539b3 100644 --- a/lib/src/types/types.dart +++ b/lib/src/types/types.dart @@ -13,10 +13,6 @@ class Gesture { Gesture(this.gestureType, this.gestureDetail); } -enum TouchHitTestBehavior { - -} - class ClipShapeItem { final ClipShape clipShape; final int position; diff --git a/test/matter_test.dart b/test/matter_test.dart index 57ee7b3..7004028 100644 --- a/test/matter_test.dart +++ b/test/matter_test.dart @@ -1,9 +1,10 @@ -import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'shape_handler/shape_handler.dart'; import 'shapes/shape.dart'; void main() { group('test library', () { testShapes(); + testShapeHandler() ; }); } diff --git a/test/shape_handler/shape_handler.dart b/test/shape_handler/shape_handler.dart new file mode 100644 index 0000000..52a10f7 --- /dev/null +++ b/test/shape_handler/shape_handler.dart @@ -0,0 +1,10 @@ +// Created by nateshmbhat on 23,April,2020 + +import 'package:flutter_test/flutter_test.dart'; + +void testShapeHandler(){ + group('SHape Handler',(){ + + }); +} + diff --git a/test/shapes/rectangle.dart b/test/shapes/rectangle.dart index 1f8c231..63abf3e 100644 --- a/test/shapes/rectangle.dart +++ b/test/shapes/rectangle.dart @@ -52,4 +52,35 @@ void testRectangle() { expect(rect1.isInside(Offset(27.8, 358.9)), true); expect(rect1.isInside(Offset(26.7, 572.2)), true); }); + + test('PaintStyleForTouch override test for Paint : Stroked Rectangle with filled touch : Point lies inside ', () { + var rect = Rectangle( + Rect.fromLTWH( + 0, + 0, + 100, + 200, + ), + paint: Paint() + ..style = PaintingStyle.stroke + ..strokeWidth = 5); + expect(rect.isInside(Offset(80, 205)), false); + expect(rect.isInside(Offset(94, 197)), false); + expect(rect.isInside(Offset(94, 198.5)), true); + expect(rect.isInside(Offset(94, 194)), false); + expect(rect.isInside(Offset(95, 199)), true); + expect(rect.isInside(Offset(0, 0)), true); + + var rect1 = Rectangle(Rect.fromLTWH(20, 300, 100, 300), + paint: Paint() + ..color = Colors.deepOrange + ..style = PaintingStyle.stroke + ..strokeWidth = 50); + + expect(rect1.isInside(Offset(123.4, 564.2)), true); + expect(rect1.isInside(Offset(127.2, 473.5)), true); + expect(rect1.isInside(Offset(135.2, 357.7)), true); + expect(rect1.isInside(Offset(27.8, 358.9)), true); + expect(rect1.isInside(Offset(26.7, 572.2)), true); + }); } From efe03dfcb4598cf92fdf437d34924ee2b1f65cdc Mon Sep 17 00:00:00 2001 From: nateshmbhat Date: Fri, 24 Apr 2020 05:12:34 +0530 Subject: [PATCH 2/3] added tests for Hittest Behavior Signed-off-by: nateshmbhat --- .../hittest_test/defer_to_child.dart | 154 +++++++++++++++++ test/shape_handler/hittest_test/opaque.dart | 151 +++++++++++++++++ .../hittest_test/translucent.dart | 156 ++++++++++++++++++ test/shape_handler/shape_handler.dart | 15 +- 4 files changed, 469 insertions(+), 7 deletions(-) create mode 100644 test/shape_handler/hittest_test/defer_to_child.dart create mode 100644 test/shape_handler/hittest_test/opaque.dart create mode 100644 test/shape_handler/hittest_test/translucent.dart diff --git a/test/shape_handler/hittest_test/defer_to_child.dart b/test/shape_handler/hittest_test/defer_to_child.dart new file mode 100644 index 0000000..7fd5a60 --- /dev/null +++ b/test/shape_handler/hittest_test/defer_to_child.dart @@ -0,0 +1,154 @@ +// Created by nateshmbhat on 24,April,2020 +import 'dart:math'; +import 'dart:ui'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:touchable/src/shape_handler.dart'; +import 'package:touchable/src/shapes/arc.dart'; +import 'package:touchable/src/shapes/circle.dart'; +import 'package:touchable/src/shapes/line.dart'; +import 'package:touchable/src/shapes/oval.dart'; +import 'package:touchable/src/shapes/point.dart'; +import 'package:touchable/src/shapes/rectangle.dart'; +import 'package:touchable/src/shapes/rounded_rectangle.dart'; +import 'package:touchable/src/shapes/util.dart'; +import 'package:touchable/touchable.dart'; + +void testDeferToChild(){ + group('SHape Handler : Defer to child hittest', () { + var shapeHandler = ShapeHandler(); + var resultList = []; + + Map getMap(Function function) { + return TouchCanvasUtil.getGestureCallbackMap( + onTapDown: (detail) { + function(detail); + }, + onTapUp: null, + onLongPressStart: null, + onLongPressEnd: null, + onLongPressMoveUpdate: null, + onForcePressStart: null, + onForcePressEnd: null, + onForcePressPeak: null, + onForcePressUpdate: null, + onPanStart: null, + onPanUpdate: null, + onPanDown: null, + onSecondaryTapDown: null, + onSecondaryTapUp: null); + } + + void addAllShapes() { + shapeHandler.addShape(Circle( + center: Offset(0, 0), + radius: 60, + paint: Paint()..color = Colors.deepOrange, + gestureMap: getMap(() {}))); + + shapeHandler.addShape( + Line(Offset(0, 0), Offset(300, 700), + paint: Paint() + ..color = Colors.black + ..strokeWidth = 200 + ..style = PaintingStyle.stroke, gestureMap: getMap((detail) { + print('line touched'); + resultList.add('black'); + })), + ); + + shapeHandler.addShape(Oval(Rect.fromLTWH(100, 100, 300, 400), + paint: Paint() + ..color = Colors.deepPurple + ..style = PaintingStyle.stroke + ..strokeWidth = 70, gestureMap: getMap((_) { + print('purple oval'); + resultList.add('purple'); + }))); + + shapeHandler.addShape(Rectangle(Rect.fromLTWH(20, 300, 100, 300), + paint: Paint() + ..color = Colors.deepOrange + ..style = PaintingStyle.stroke + ..strokeWidth = 50, gestureMap: getMap((_) { + print('orange circle'); + resultList.add('orange'); + }))); + + var paint = Paint() + ..color = Colors.greenAccent + ..style = PaintingStyle.stroke + ..strokeWidth = 10; + shapeHandler.addShape(Circle( + center: Offset(150, 50), + radius: 60, + paint: paint, + gestureMap: getMap((_) { + print('green circle'); + resultList.add('green'); + }), + )); + + shapeHandler.addShape(Circle( + center: Offset(150, 250), + radius: 70, + paint: Paint()..color = Colors.lightBlueAccent, + hitTestBehavior: HitTestBehavior.deferToChild, + gestureMap: getMap((_) { + print('blue circle'); + resultList.add('blue'); + }))); + ; + +// canvas.drawVertices(vertices, blendMode, paint) + shapeHandler.addShape( + Arc(Rect.fromLTWH(100, 100, 200, 200), -pi, -3 * pi / 2, true, + paint: Paint() + ..strokeWidth = 40 + ..style = PaintingStyle.stroke + ..color = Colors.pink, gestureMap: getMap((_) { + print('pink arc'); + resultList.add('pink'); + })), + ); + + shapeHandler.addShape(RoundedRectangle( + RRect.fromLTRBR(100, 340, 300, 650, Radius.elliptical(100, 150)), + paint: Paint() + ..strokeWidth = 40 + ..color = Colors.grey, hitTestBehavior: HitTestBehavior.deferToChild , gestureMap: getMap((_) { + print('rounded grey rect'); + resultList.add('grey'); + }))); + + shapeHandler.addShape(Point(PointMode.points, [Offset(129.1, 241.9)], + paint: Paint() + ..color = Colors.black + ..strokeWidth = 10)); + } + + addAllShapes(); + void handleGesture(Offset position , List expected){ + resultList = []; + shapeHandler.handleGestureEvent(Gesture(GestureType.onTapDown, + TapDownDetails(localPosition: position))); + expect(resultList, expected); + } + + test('Clicking a shape with (deferToChild) hittest behavior should not call its callback and pass the touch down to the shapes below it. ' ,(){ + handleGesture(Offset(212.2, 171.4) , ['pink']); + handleGesture(Offset(88.0, 356.2) , ['purple']); + handleGesture(Offset(166.1, 242.7) , ['black']); + handleGesture(Offset(249.9, 332.2) , ['black']); + handleGesture(Offset(134.1, 59.8) , ['black']); + handleGesture(Offset(87.2, 349.7) , ['purple']); + handleGesture(Offset(236.6, 426.7) , ['black']); + handleGesture(Offset(214.9, 483.4) , ['purple']); + handleGesture(Offset(213.0, 58.7) , ['green']); + handleGesture(Offset(270,10) , []); + }); + }); +} + + \ No newline at end of file diff --git a/test/shape_handler/hittest_test/opaque.dart b/test/shape_handler/hittest_test/opaque.dart new file mode 100644 index 0000000..6e9079c --- /dev/null +++ b/test/shape_handler/hittest_test/opaque.dart @@ -0,0 +1,151 @@ +// Created by nateshmbhat on 24,April,2020 + +import 'dart:math'; +import 'dart:ui'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:touchable/src/shape_handler.dart'; +import 'package:touchable/src/shapes/arc.dart'; +import 'package:touchable/src/shapes/circle.dart'; +import 'package:touchable/src/shapes/line.dart'; +import 'package:touchable/src/shapes/oval.dart'; +import 'package:touchable/src/shapes/point.dart'; +import 'package:touchable/src/shapes/rectangle.dart'; +import 'package:touchable/src/shapes/rounded_rectangle.dart'; +import 'package:touchable/src/shapes/util.dart'; +import 'package:touchable/touchable.dart'; + + +void testOpaque(){ + group('SHape Handler : opaque hittest', () { + var shapeHandler = ShapeHandler(); + var resultList = []; + + Map getMap(Function function) { + return TouchCanvasUtil.getGestureCallbackMap( + onTapDown: (detail) { + function(detail); + }, + onTapUp: null, + onLongPressStart: null, + onLongPressEnd: null, + onLongPressMoveUpdate: null, + onForcePressStart: null, + onForcePressEnd: null, + onForcePressPeak: null, + onForcePressUpdate: null, + onPanStart: null, + onPanUpdate: null, + onPanDown: null, + onSecondaryTapDown: null, + onSecondaryTapUp: null); + } + + void addAllShapes() { + shapeHandler.addShape(Circle( + center: Offset(0, 0), + radius: 60, + paint: Paint()..color = Colors.deepOrange, + gestureMap: getMap(() {}))); + + shapeHandler.addShape( + Line(Offset(0, 0), Offset(300, 700), + paint: Paint() + ..color = Colors.black + ..strokeWidth = 200 + ..style = PaintingStyle.stroke, gestureMap: getMap((detail) { + print('line touched'); + resultList.add('black'); + })), + ); + + shapeHandler.addShape(Oval(Rect.fromLTWH(100, 100, 300, 400), + paint: Paint() + ..color = Colors.deepPurple + ..style = PaintingStyle.stroke + ..strokeWidth = 70, gestureMap: getMap((_) { + print('purple oval'); + resultList.add('purple'); + }))); + + shapeHandler.addShape(Rectangle(Rect.fromLTWH(20, 300, 100, 300), + paint: Paint() + ..color = Colors.deepOrange + ..style = PaintingStyle.stroke + ..strokeWidth = 50, gestureMap: getMap((_) { + print('orange circle'); + resultList.add('orange'); + }))); + + var paint = Paint() + ..color = Colors.greenAccent + ..style = PaintingStyle.stroke + ..strokeWidth = 10; + shapeHandler.addShape(Circle( + center: Offset(150, 50), + radius: 60, + paint: paint, + gestureMap: getMap((_) { + print('green circle'); + resultList.add('green'); + }), + )); + + shapeHandler.addShape(Circle( + center: Offset(150, 250), + radius: 70, + paint: Paint()..color = Colors.lightBlueAccent, + gestureMap: getMap((_) { + print('blue circle'); + resultList.add('blue'); + }))); + ; + +// canvas.drawVertices(vertices, blendMode, paint) + shapeHandler.addShape( + Arc(Rect.fromLTWH(100, 100, 200, 200), -pi, -3 * pi / 2, true, + paint: Paint() + ..strokeWidth = 40 + ..style = PaintingStyle.stroke + ..color = Colors.pink, gestureMap: getMap((_) { + print('pink arc'); + resultList.add('pink'); + })), + ); + + shapeHandler.addShape(RoundedRectangle( + RRect.fromLTRBR(100, 340, 300, 650, Radius.elliptical(100, 150)), + paint: Paint() + ..strokeWidth = 40 + ..color = Colors.grey, gestureMap: getMap((_) { + print('rounded grey rect'); + resultList.add('grey'); + }))); + + shapeHandler.addShape(Point(PointMode.points, [Offset(129.1, 241.9)], + paint: Paint() + ..color = Colors.black + ..strokeWidth = 10)); + } + + addAllShapes(); + void handleGesture(Offset position , List expected){ + resultList = []; + shapeHandler.handleGestureEvent(Gesture(GestureType.onTapDown, + TapDownDetails(localPosition: position))); + expect(resultList, expected); + } + + test('Test if clicking a certain position , call the appropriate callback functions' ,(){ + handleGesture(Offset(212.2, 171.4) , ['pink']); + handleGesture(Offset(88.0, 356.2) , ['purple']); + handleGesture(Offset(166.1, 242.7) , ['blue']); + handleGesture(Offset(249.9, 332.2) , ['black']); + handleGesture(Offset(134.1, 59.8) , ['black']); + handleGesture(Offset(87.2, 349.7) , ['purple']); + handleGesture(Offset(236.6, 426.7) , ['grey']); + handleGesture(Offset(213.0, 58.7) , ['green']); + }); + }); +} \ No newline at end of file diff --git a/test/shape_handler/hittest_test/translucent.dart b/test/shape_handler/hittest_test/translucent.dart new file mode 100644 index 0000000..c2774db --- /dev/null +++ b/test/shape_handler/hittest_test/translucent.dart @@ -0,0 +1,156 @@ +// Created by nateshmbhat on 24,April,2020 + +import 'dart:math'; +import 'dart:ui'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:touchable/src/shape_handler.dart'; +import 'package:touchable/src/shapes/arc.dart'; +import 'package:touchable/src/shapes/circle.dart'; +import 'package:touchable/src/shapes/line.dart'; +import 'package:touchable/src/shapes/oval.dart'; +import 'package:touchable/src/shapes/point.dart'; +import 'package:touchable/src/shapes/rectangle.dart'; +import 'package:touchable/src/shapes/rounded_rectangle.dart'; +import 'package:touchable/src/shapes/util.dart'; +import 'package:touchable/touchable.dart'; + +void testTranslucent(){ + + group('SHape Handler : Translucent hittest', () { + var shapeHandler = ShapeHandler(); + var resultList = []; + + Map getMap(Function function) { + return TouchCanvasUtil.getGestureCallbackMap( + onTapDown: (detail) { + function(detail); + }, + onTapUp: null, + onLongPressStart: null, + onLongPressEnd: null, + onLongPressMoveUpdate: null, + onForcePressStart: null, + onForcePressEnd: null, + onForcePressPeak: null, + onForcePressUpdate: null, + onPanStart: null, + onPanUpdate: null, + onPanDown: null, + onSecondaryTapDown: null, + onSecondaryTapUp: null); + } + + void addAllShapes() { + shapeHandler.addShape(Circle( + center: Offset(0, 0), + radius: 60, + paint: Paint()..color = Colors.deepOrange, + gestureMap: getMap(() {}))); + + shapeHandler.addShape( + Line(Offset(0, 0), Offset(300, 700), + paint: Paint() + ..color = Colors.black + ..strokeWidth = 200 + ..style = PaintingStyle.stroke, gestureMap: getMap((detail) { + print('line touched'); + resultList.add('black'); + })), + ); + + shapeHandler.addShape(Oval(Rect.fromLTWH(100, 100, 300, 400), + paint: Paint() + ..color = Colors.deepPurple + ..style = PaintingStyle.stroke + ..strokeWidth = 70, gestureMap: getMap((_) { + print('purple oval'); + resultList.add('purple'); + }))); + + shapeHandler.addShape(Rectangle(Rect.fromLTWH(20, 300, 100, 300), + paint: Paint() + ..color = Colors.deepOrange + ..style = PaintingStyle.stroke + ..strokeWidth = 50, gestureMap: getMap((_) { + print('orange circle'); + resultList.add('orange'); + }))); + + var paint = Paint() + ..color = Colors.greenAccent + ..style = PaintingStyle.stroke + ..strokeWidth = 10; + shapeHandler.addShape(Circle( + center: Offset(150, 50), + radius: 60, + paint: paint, + gestureMap: getMap((_) { + print('green circle'); + resultList.add('green'); + }), + )); + + shapeHandler.addShape(Circle( + center: Offset(150, 250), + radius: 70, + paint: Paint()..color = Colors.lightBlueAccent, + hitTestBehavior: HitTestBehavior.translucent, + gestureMap: getMap((_) { + print('blue circle'); + resultList.add('blue'); + }))); + ; + +// canvas.drawVertices(vertices, blendMode, paint) + shapeHandler.addShape( + Arc(Rect.fromLTWH(100, 100, 200, 200), -pi, -3 * pi / 2, true, + paint: Paint() + ..strokeWidth = 40 + ..style = PaintingStyle.stroke + ..color = Colors.pink, gestureMap: getMap((_) { + print('pink arc'); + resultList.add('pink'); + })), + ); + + shapeHandler.addShape(RoundedRectangle( + RRect.fromLTRBR(100, 340, 300, 650, Radius.elliptical(100, 150)), + paint: Paint() + ..strokeWidth = 40 + ..color = Colors.grey, hitTestBehavior: HitTestBehavior.translucent, gestureMap: getMap((_) { + print('rounded grey rect'); + resultList.add('grey'); + }))); + + shapeHandler.addShape(Point(PointMode.points, [Offset(129.1, 241.9)], + paint: Paint() + ..color = Colors.black + ..strokeWidth = 10)); + } + + addAllShapes(); + void handleGesture(Offset position , List expected){ + resultList = []; + shapeHandler.handleGestureEvent(Gesture(GestureType.onTapDown, + TapDownDetails(localPosition: position))); + expect(resultList, expected); + } + + test('Clicking a certain TRANSLUCENT shape calls the callback functions of this shape and others below it' ,(){ + handleGesture(Offset(212.2, 171.4) , ['pink']); + handleGesture(Offset(88.0, 356.2) , ['purple']); + handleGesture(Offset(166.1, 242.7) , ['blue','black']); + handleGesture(Offset(249.9, 332.2) , ['black']); + handleGesture(Offset(134.1, 59.8) , ['black']); + handleGesture(Offset(87.2, 349.7) , ['purple']); + handleGesture(Offset(236.6, 426.7) , ['grey','black']); + handleGesture(Offset(214.9, 483.4) , ['grey','purple']); + handleGesture(Offset(213.0, 58.7) , ['green']); + handleGesture(Offset(270,10) , []); + }); + }); +} + + \ No newline at end of file diff --git a/test/shape_handler/shape_handler.dart b/test/shape_handler/shape_handler.dart index 52a10f7..95ffd88 100644 --- a/test/shape_handler/shape_handler.dart +++ b/test/shape_handler/shape_handler.dart @@ -1,10 +1,11 @@ // Created by nateshmbhat on 23,April,2020 -import 'package:flutter_test/flutter_test.dart'; - -void testShapeHandler(){ - group('SHape Handler',(){ - - }); +import 'hittest_test/defer_to_child.dart'; +import 'hittest_test/opaque.dart'; +import 'hittest_test/translucent.dart'; + +void testShapeHandler() { + testDeferToChild(); + testOpaque(); + testTranslucent(); } - From 8b7cf75111e7a386276ea518dde27b5841da8378 Mon Sep 17 00:00:00 2001 From: nateshmbhat Date: Fri, 24 Apr 2020 08:58:25 +0530 Subject: [PATCH 3/3] updated readme and added documentation url. Formatted with flutter format. Signed-off-by: nateshmbhat --- CHANGELOG.md | 6 ++ README.md | 43 ++++++++----- example/example.dart | 2 +- lib/src/canvas_touch_detector.dart | 6 +- lib/src/shape_handler.dart | 26 ++++---- lib/src/shapes/arc.dart | 12 ++-- lib/src/shapes/circle.dart | 9 +-- lib/src/shapes/clip.dart | 2 - lib/src/shapes/constant.dart | 2 - lib/src/shapes/line.dart | 13 ++-- lib/src/shapes/oval.dart | 13 ++-- lib/src/shapes/path.dart | 13 ++-- lib/src/shapes/point.dart | 12 ++-- lib/src/shapes/rectangle.dart | 13 ++-- lib/src/shapes/rounded_rectangle.dart | 11 +++- lib/src/shapes/shape.dart | 4 +- lib/src/shapes/util.dart | 3 +- lib/src/touchy_canvas.dart | 21 ++---- lib/src/types/types.dart | 2 - lib/touchable.dart | 32 +++++++++- pubspec.yaml | 4 +- test/matter_test.dart | 3 +- .../hittest_test/defer_to_child.dart | 63 +++++++++--------- test/shape_handler/hittest_test/opaque.dart | 57 ++++++++--------- .../hittest_test/translucent.dart | 64 +++++++++---------- test/shape_handler/shape_handler.dart | 2 - test/shapes/arc.dart | 1 - test/shapes/circle.dart | 2 - test/shapes/line.dart | 2 - test/shapes/oval.dart | 2 - test/shapes/point.dart | 2 - test/shapes/rectangle.dart | 6 +- test/shapes/rounded_rectangle.dart | 1 - test/shapes/shape.dart | 2 - 34 files changed, 250 insertions(+), 206 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12dbc14..c5ef8c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [0.1.5] - April 24 2020 +Added support for HitTestBehavior +Minor bug fixes +Added Documentation +Updated docs in code + #### [0.1.2] - April 21 2020 updated readme and example diff --git a/README.md b/README.md index 27e0d52..d7a0531 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,15 @@

+## Index : +- [Why Use Touchable ?](#why-use-touchable) +- [Installation](#installation) +- [Usage](#usage) +- [How it works](#how-touchable-works) +- [Road Map 🗺](#road-map) +- [Links](#links) + + ## Why Use **Touchable** ? - The CustomPainter lets you **only draw** shapes on the canvas. But most would want to let user interact with the drawings. @@ -37,10 +46,10 @@ - With touchable , you get what the normal canvas always missed : **touchability** 😉 - With this , its possible to add all kinds of **gesture callbacks to each drawing** and thus interaction capability to each Shape you draw on the canvas. - Animating individual shapes becomes so much easier than ever before. -- Handles the painting style of your drawing. So , when your paint is `filled ▮` it registers touch on the entire shape else when its `stroke ▯` , it looks for gesture only on the borders. +- Handles the painting style (`filled ▮` , `stroke ▯`) and detects touch accordingly. - Takes the painting **stroke width** also into account. So if your shapes are painted thick , we still got it covered ✓ - -- Handles **clipping** and different **clipping modes**. So, You can have any kind of complex clipping and drawing combinations while getting full interactive capability. +- Supports **clipping** and different **clipping modes**. So, You can have any kind of complex clipping and drawing combinations while getting full interactive capability. +- Supports HitTestBehavior for each shape. - Simple and Easy API. Just wrap your `CustomPaint` with `CanvasTouchDetector` and use the `TouchyCanvas` in your painter. @@ -130,30 +139,30 @@ When user performs any gesture on the screen , based on the location of the gest - [x] sector - [x] Rounded Rectangle (RRect) - [x] Custom Path [only supports opaque hittest] - - [x] Points - - [x] PointMode.point - - [x] PointMode.lines - - [x] PointMode.polygon - - [] Vertices - - [] Traingle - - [] Traingle Strip - - [] Traingle Fan + - [x] Points (PointMode.points , PointMode.lines , PointMode.polygon) + - [ ] Vertices + - [ ] Traingle + - [ ] Traingle Strip + - [ ] Traingle Fan - [x] Support for proper edge detection based on the Paint object properties : - [x] Paint style - [x] Stroke Width - - [] Stroke Cap + - [ ] Stroke Cap - [x] StrokeCap to draw Points - - [ ] StrokeCap for lines with large width + - [ ] `StrokeCap.round` for lines with large width - [x] Support Clipping and clipping modes - [x] ClipRect - [x] intersect mode [Touch detection enabled only inside the clipped region] - [x] difference mode [Touch detection enabled only outside the clipped region] - [x] ClipRRect - [x] ClipPath -- [ ] Allow customizing touch detection behaviour regardless of the Paint applied (give a HitTestBehavior functionality) -- [ ] Support for translation , rotation , scaling and skewing transformations that needs some work vector math +- [x] Support for HitTestBehavior +- [ ] Make the touch detection handling to run in a seperate isolate. +- [ ] Support for translation , rotation , scaling and skewing transformations that needs some vector math ## Links : -+ Pub Dev : https://pub.dev/packages/touchable -+ HomePage : https://github.com/nateshmbhat/touchable \ No newline at end of file ++ [Touchable Docs](https://pub.dev/documentation/touchable/latest/) ++ [Pub Dev](https://pub.dev/packages/touchable) ++ [HomePage](https://github.com/nateshmbhat/touchable) ++ [My Github Page](https://github.com/nateshmbhat) \ No newline at end of file diff --git a/example/example.dart b/example/example.dart index bc95d04..e6c4a82 100644 --- a/example/example.dart +++ b/example/example.dart @@ -4,7 +4,7 @@ import 'package:touchable/touchable.dart'; class MyExampleWidget extends StatelessWidget { @override Widget build(BuildContext context) { - FocusScope.of(context).nextFocus() ; + FocusScope.of(context).nextFocus(); return Flexible( child: FractionallySizedBox( widthFactor: 1, diff --git a/lib/src/canvas_touch_detector.dart b/lib/src/canvas_touch_detector.dart index 20cff62..5230621 100644 --- a/lib/src/canvas_touch_detector.dart +++ b/lib/src/canvas_touch_detector.dart @@ -3,8 +3,10 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:touchable/src/types/types.dart'; -/// Created by nateshmbhat on 04,April,2020 - +///[CanvasTouchDetector] widget detects the gestures on your [CustomPaint] widget. +/// +/// Wrap your [CustomPaint] widget with [CanvasTouchDetector] +/// The [builder] function passes the [BuildContext] and expects a [CustomPaint] object as its return value. class CanvasTouchDetector extends StatefulWidget { final CustomTouchPaintBuilder builder; diff --git a/lib/src/shape_handler.dart b/lib/src/shape_handler.dart index 841de75..4b3de8e 100644 --- a/lib/src/shape_handler.dart +++ b/lib/src/shape_handler.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 12,April,2020 - import 'dart:ui'; import 'package:flutter/cupertino.dart'; @@ -48,22 +46,23 @@ class ShapeHandler { } List _getTouchedShapes(Offset point) { - var selectedShapes = [] ; + var selectedShapes = []; for (int i = _shapeStack.length - 1; i >= 0; i--) { - var shape = _shapeStack[i] ; - if(shape.hitTestBehavior==HitTestBehavior.deferToChild) { - continue ; + var shape = _shapeStack[i]; + if (shape.hitTestBehavior == HitTestBehavior.deferToChild) { + continue; } if (shape.isInside(point)) { - if (_isPointInsideClipShapes(_getClipShapesBelowPosition(i), point) == false) { - if(shape.hitTestBehavior==HitTestBehavior.opaque) { + if (_isPointInsideClipShapes(_getClipShapesBelowPosition(i), point) == + false) { + if (shape.hitTestBehavior == HitTestBehavior.opaque) { return selectedShapes; } - continue ; + continue; } selectedShapes.add(shape); - if(shape.hitTestBehavior==HitTestBehavior.opaque) { - return selectedShapes ; + if (shape.hitTestBehavior == HitTestBehavior.opaque) { + return selectedShapes; } } } @@ -71,12 +70,13 @@ class ShapeHandler { } Future handleGestureEvent(Gesture gesture) async { - var touchPoint = TouchCanvasUtil.getPointFromGestureDetail(gesture.gestureDetail); + var touchPoint = + TouchCanvasUtil.getPointFromGestureDetail(gesture.gestureDetail); if (!_registeredGestures.contains(gesture.gestureType)) return; var touchedShapes = _getTouchedShapes(touchPoint); if (touchedShapes.isEmpty) return; - for(var touchedShape in touchedShapes){ + for (var touchedShape in touchedShapes) { if (touchedShape.registeredGestures.contains(gesture.gestureType)) { var callback = touchedShape.getCallbackFromGesture(gesture); callback(); diff --git a/lib/src/shapes/arc.dart b/lib/src/shapes/arc.dart index 7e383f1..8df94f4 100644 --- a/lib/src/shapes/arc.dart +++ b/lib/src/shapes/arc.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 12,April,2020 - import 'dart:math'; import 'dart:ui'; @@ -30,8 +28,14 @@ class Arc extends Shape { Offset _arcEndPoint; Arc(this.rect, this.startAngle, this.sweepAngle, this.useCenter, - {Paint paint, Map gestureMap, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) - : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap) { + {Paint paint, + Map gestureMap, + HitTestBehavior hitTestBehavior, + PaintingStyle paintStyleForTouch}) + : super( + hitTestBehavior: hitTestBehavior, + paint: paint, + gestureCallbackMap: gestureMap) { _oval = Oval(rect, paint: paint); if (sweepAngle < 0) { diff --git a/lib/src/shapes/circle.dart b/lib/src/shapes/circle.dart index ac491b9..c2e626d 100644 --- a/lib/src/shapes/circle.dart +++ b/lib/src/shapes/circle.dart @@ -6,8 +6,6 @@ import 'package:touchable/src/shapes/constant.dart'; import 'package:touchable/src/shapes/shape.dart'; import 'package:touchable/src/types/types.dart'; -/// Created by nateshmbhat on 04,April,2020 - class Circle extends Shape { final Offset center; final double radius; @@ -15,10 +13,13 @@ class Circle extends Shape { Circle( {@required this.center, @required this.radius, - HitTestBehavior hitTestBehavior, + HitTestBehavior hitTestBehavior, Map gestureMap, Paint paint}) - : super(paint: paint, gestureCallbackMap: gestureMap , hitTestBehavior : hitTestBehavior); + : super( + paint: paint, + gestureCallbackMap: gestureMap, + hitTestBehavior: hitTestBehavior); // (x-a)^2 + (y-b)^2 = r^2 @override diff --git a/lib/src/shapes/clip.dart b/lib/src/shapes/clip.dart index ddd102e..620f1b6 100644 --- a/lib/src/shapes/clip.dart +++ b/lib/src/shapes/clip.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 13,April,2020 - import 'dart:ui'; import 'package:flutter/cupertino.dart'; diff --git a/lib/src/shapes/constant.dart b/lib/src/shapes/constant.dart index 4137022..e92df73 100644 --- a/lib/src/shapes/constant.dart +++ b/lib/src/shapes/constant.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 04,April,2020 - class ShapeConstant { static const floatPrecision = 0.001; static const double infinity = 9999999; diff --git a/lib/src/shapes/line.dart b/lib/src/shapes/line.dart index 98c5cac..b60b47d 100644 --- a/lib/src/shapes/line.dart +++ b/lib/src/shapes/line.dart @@ -6,15 +6,20 @@ import 'package:touchable/src/shapes/shape.dart'; import 'package:touchable/src/shapes/util.dart'; import 'package:touchable/src/types/types.dart'; -/// Created by nateshmbhat on 04,April,2020 - class Line extends Shape { final Offset p1; final Offset p2; double a, b, c; // Equation ax+by = c - Line(this.p1, this.p2, {Map gestureMap, Paint paint , HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) - : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap) { + Line(this.p1, this.p2, + {Map gestureMap, + Paint paint, + HitTestBehavior hitTestBehavior, + PaintingStyle paintStyleForTouch}) + : super( + hitTestBehavior: hitTestBehavior, + paint: paint, + gestureCallbackMap: gestureMap) { a = p2.dy - p1.dy; b = p1.dx - p2.dx; c = a * p1.dx + b * p1.dy; diff --git a/lib/src/shapes/oval.dart b/lib/src/shapes/oval.dart index 38a2e7f..3d8ede0 100644 --- a/lib/src/shapes/oval.dart +++ b/lib/src/shapes/oval.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 11,April,2020 - import 'dart:math'; import 'dart:ui'; @@ -14,8 +12,15 @@ class Oval extends Shape { double a, b; // x^2/a^2 + y^2/b^2 = 1 - Oval(this.rect, {Map gestureMap, Paint paint, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) - : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap) { + Oval(this.rect, + {Map gestureMap, + Paint paint, + HitTestBehavior hitTestBehavior, + PaintingStyle paintStyleForTouch}) + : super( + hitTestBehavior: hitTestBehavior, + paint: paint, + gestureCallbackMap: gestureMap) { a = rect.right - rect.center.dx; b = rect.center.dy - rect.top; } diff --git a/lib/src/shapes/path.dart b/lib/src/shapes/path.dart index a0d5dbf..1ac860e 100644 --- a/lib/src/shapes/path.dart +++ b/lib/src/shapes/path.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 12,April,2020 - import 'dart:ui'; import 'package:flutter/cupertino.dart'; @@ -9,8 +7,15 @@ import 'package:touchable/src/types/types.dart'; class PathShape extends Shape { final Path path; - PathShape(this.path, {Map gestureMap, Paint paint, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) - : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap); + PathShape(this.path, + {Map gestureMap, + Paint paint, + HitTestBehavior hitTestBehavior, + PaintingStyle paintStyleForTouch}) + : super( + hitTestBehavior: hitTestBehavior, + paint: paint, + gestureCallbackMap: gestureMap); @override bool isInside(Offset p) { diff --git a/lib/src/shapes/point.dart b/lib/src/shapes/point.dart index a7c0b37..c68aaa9 100644 --- a/lib/src/shapes/point.dart +++ b/lib/src/shapes/point.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 12,April,2020 - import 'dart:ui'; import 'package:flutter/cupertino.dart'; @@ -14,8 +12,14 @@ class Point extends Shape { final List points; Point(this.pointMode, this.points, - {Map gestureMap, Paint paint, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) - : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap); + {Map gestureMap, + Paint paint, + HitTestBehavior hitTestBehavior, + PaintingStyle paintStyleForTouch}) + : super( + hitTestBehavior: hitTestBehavior, + paint: paint, + gestureCallbackMap: gestureMap); @override bool isInside(Offset p) { diff --git a/lib/src/shapes/rectangle.dart b/lib/src/shapes/rectangle.dart index a7836e6..7e70ebc 100644 --- a/lib/src/shapes/rectangle.dart +++ b/lib/src/shapes/rectangle.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 05,April,2020 - import 'dart:ui'; import 'package:flutter/cupertino.dart'; @@ -8,8 +6,15 @@ import 'package:touchable/src/types/types.dart'; class Rectangle extends Shape { final Rect rect; - Rectangle(this.rect, {Map gestureMap, Paint paint, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) - : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap); + Rectangle(this.rect, + {Map gestureMap, + Paint paint, + HitTestBehavior hitTestBehavior, + PaintingStyle paintStyleForTouch}) + : super( + hitTestBehavior: hitTestBehavior, + paint: paint, + gestureCallbackMap: gestureMap); @override bool isInside(Offset p) { diff --git a/lib/src/shapes/rounded_rectangle.dart b/lib/src/shapes/rounded_rectangle.dart index b3e9dc5..9f2877f 100644 --- a/lib/src/shapes/rounded_rectangle.dart +++ b/lib/src/shapes/rounded_rectangle.dart @@ -1,4 +1,3 @@ -// Created by nateshmbhat on 12,April,2020 import 'dart:ui'; import 'package:flutter/cupertino.dart'; @@ -9,8 +8,14 @@ class RoundedRectangle extends Shape { final RRect rRect; RoundedRectangle(this.rRect, - {Paint paint, Map gestureMap, HitTestBehavior hitTestBehavior, PaintingStyle paintStyleForTouch}) - : super(hitTestBehavior : hitTestBehavior , paint: paint, gestureCallbackMap: gestureMap); + {Paint paint, + Map gestureMap, + HitTestBehavior hitTestBehavior, + PaintingStyle paintStyleForTouch}) + : super( + hitTestBehavior: hitTestBehavior, + paint: paint, + gestureCallbackMap: gestureMap); @override bool isInside(Offset p) { diff --git a/lib/src/shapes/shape.dart b/lib/src/shapes/shape.dart index b08d842..3e0858d 100644 --- a/lib/src/shapes/shape.dart +++ b/lib/src/shapes/shape.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 04,April,2020 - import 'dart:ui'; import 'package:flutter/gestures.dart'; @@ -10,7 +8,7 @@ import 'package:touchable/touchable.dart'; abstract class Shape { Paint paint; Map gestureCallbackMap; - HitTestBehavior hitTestBehavior ; + HitTestBehavior hitTestBehavior; Set get registeredGestures => gestureCallbackMap?.keys?.toSet() ?? Set(); diff --git a/lib/src/shapes/util.dart b/lib/src/shapes/util.dart index 353c392..b41f5ce 100644 --- a/lib/src/shapes/util.dart +++ b/lib/src/shapes/util.dart @@ -1,7 +1,6 @@ -// Created by nateshmbhat on 04,April,2020 - import 'dart:math'; import 'dart:ui'; + import 'package:flutter/material.dart'; import 'package:touchable/src/types/types.dart'; diff --git a/lib/src/touchy_canvas.dart b/lib/src/touchy_canvas.dart index 3736ea6..2ac6911 100644 --- a/lib/src/touchy_canvas.dart +++ b/lib/src/touchy_canvas.dart @@ -1,7 +1,3 @@ -// Created by nateshmbhat on 05,April,2020 -library matter; - -import 'dart:async'; import 'dart:math'; import 'dart:typed_data'; import 'dart:ui'; @@ -20,14 +16,16 @@ import 'package:touchable/src/shapes/point.dart'; import 'package:touchable/src/shapes/rectangle.dart'; import 'package:touchable/src/shapes/rounded_rectangle.dart'; import 'package:touchable/src/shapes/util.dart'; -import 'package:touchable/src/types/types.dart'; class TouchyCanvas { final Canvas _canvas; - StreamController controller; final ShapeHandler _shapeHandler = ShapeHandler(); + ///[TouchyCanvas] helps you add gesture callbacks to the shapes you draw. + /// + /// [context] is the BuildContext that is obtained from the [CanvasTouchDetector] widget's builder function. + /// The parameter [canvas] is the [Canvas] object that you get in your [paint] method inside [CustomPainter] TouchyCanvas(BuildContext context, this._canvas) { var touchController = TouchDetectionController.of(context); touchController.addListener((event) { @@ -119,7 +117,6 @@ class TouchyCanvas { _shapeHandler.addShape(Line(p1, p2, paint: paint, hitTestBehavior: hitTestBehavior, - gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -162,7 +159,6 @@ class TouchyCanvas { _shapeHandler.addShape(Oval(rect, paint: paint, hitTestBehavior: hitTestBehavior, - gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -211,7 +207,6 @@ class TouchyCanvas { _shapeHandler.addShape(PathShape(path, paint: paint, hitTestBehavior: hitTestBehavior, - gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -255,7 +250,6 @@ class TouchyCanvas { _shapeHandler.addShape(Point(pointMode, points, paint: paint, hitTestBehavior: hitTestBehavior, - gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -298,7 +292,6 @@ class TouchyCanvas { _shapeHandler.addShape(RoundedRectangle(rrect, paint: paint, hitTestBehavior: hitTestBehavior, - gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -346,7 +339,6 @@ class TouchyCanvas { _shapeHandler.addShape(Point(pointMode, offsetPoints, paint: paint, hitTestBehavior: hitTestBehavior, - gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -389,7 +381,6 @@ class TouchyCanvas { _shapeHandler.addShape(Rectangle(rect, paint: paint, hitTestBehavior: hitTestBehavior, - gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -441,7 +432,6 @@ class TouchyCanvas { p.dx, p.dy, image.width.toDouble(), image.height.toDouble()), paint: paint, hitTestBehavior: hitTestBehavior, - gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: onTapUp, @@ -487,7 +477,6 @@ class TouchyCanvas { var arc = Arc(rect, startAngle, sweepAngle, useCenter, paint: paint, hitTestBehavior: hitTestBehavior, - gestureMap: TouchCanvasUtil.getGestureCallbackMap( onTapDown: onTapDown, onTapUp: null, @@ -511,7 +500,6 @@ class TouchyCanvas { // _canvas.drawDRRect(outer, inner, paint); // // TODO: implement drawDRRect in SHapeHandler // } - // // // void drawRawAtlas(Image atlas, Float32List rstTransforms, Float32List rects, @@ -529,7 +517,6 @@ class TouchyCanvas { // // TODO: implement drawImageRect // _canvas.drawImageRect(image, src, dst, paint); // } - // // void drawVertices(Vertices vertices, BlendMode blendMode, Paint paint) { // _canvas.drawVertices(vertices, blendMode, paint); diff --git a/lib/src/types/types.dart b/lib/src/types/types.dart index e6539b3..ff2476a 100644 --- a/lib/src/types/types.dart +++ b/lib/src/types/types.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 09,April,2020 - import 'package:flutter/material.dart'; import 'package:touchable/src/shapes/clip.dart'; diff --git a/lib/touchable.dart b/lib/touchable.dart index 1b02cea..3ae3239 100644 --- a/lib/touchable.dart +++ b/lib/touchable.dart @@ -1,3 +1,33 @@ -export 'src/touchy_canvas.dart'; +///Touchable is flutter library to add various gesture callbacks to each Shape you draw on your canvas in your CustomPainter +/// +///When using touchable , the only two classes that you would need to use are : [TouchyCanvas] and [CanvasTouchDetector] +/// +///## Usage : +/// +///Just Wrap your [CustomPaint] widget with [CanvasTouchDetector]. It takes a builder function as argument that expects your [CustomPaint] widget as shown below. +///#### Example : +/// +/// ``` +///CanvasTouchDetector( +/// builder: (context) => +/// CustomPaint( +/// painter: MyPainter(context) +/// ) +///) +///``` +/// +///Inside your [CustomPainter] class's [paint] method , create and use the [TouchyCanvas] object (using the context obtained from the [CanvasTouchDetector] and canvas) to draw any shape with different gesture callbacks. +///####Example : +/// +/// ``` +///var myCanvas = TouchyCanvas(context,canvas); +///myCanvas.drawRect( rect , Paint() , onTapDown: (tapDetail){ +/// //Do stuff here. Probably change your state and animate +///}); +///``` +/// +library touchable; + export 'src/canvas_touch_detector.dart'; +export 'src/touchy_canvas.dart'; export 'src/types/types.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index 00e7bb7..86f6ab8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: touchable -description: Draw Shapes on your canvas and add full interactive capability and gesture callbacks to each object. -version: 0.1.2 +description: Flutter library to add various gesture callbacks to each Shape you draw on your canvas in your CustomPainterFlutter library to add various gesture callbacks to each Shape you draw on your canvas in your CustomPainter +version: 0.1.5 repository: https://github.com/nateshmbhat/touchable homepage: https://github.com/nateshmbhat/touchable diff --git a/test/matter_test.dart b/test/matter_test.dart index 7004028..e6157b7 100644 --- a/test/matter_test.dart +++ b/test/matter_test.dart @@ -1,10 +1,11 @@ import 'package:flutter_test/flutter_test.dart'; + import 'shape_handler/shape_handler.dart'; import 'shapes/shape.dart'; void main() { group('test library', () { testShapes(); - testShapeHandler() ; + testShapeHandler(); }); } diff --git a/test/shape_handler/hittest_test/defer_to_child.dart b/test/shape_handler/hittest_test/defer_to_child.dart index 7fd5a60..1918a72 100644 --- a/test/shape_handler/hittest_test/defer_to_child.dart +++ b/test/shape_handler/hittest_test/defer_to_child.dart @@ -1,4 +1,3 @@ -// Created by nateshmbhat on 24,April,2020 import 'dart:math'; import 'dart:ui'; @@ -15,7 +14,7 @@ import 'package:touchable/src/shapes/rounded_rectangle.dart'; import 'package:touchable/src/shapes/util.dart'; import 'package:touchable/touchable.dart'; -void testDeferToChild(){ +void testDeferToChild() { group('SHape Handler : Defer to child hittest', () { var shapeHandler = ShapeHandler(); var resultList = []; @@ -53,9 +52,9 @@ void testDeferToChild(){ ..color = Colors.black ..strokeWidth = 200 ..style = PaintingStyle.stroke, gestureMap: getMap((detail) { - print('line touched'); - resultList.add('black'); - })), + print('line touched'); + resultList.add('black'); + })), ); shapeHandler.addShape(Oval(Rect.fromLTWH(100, 100, 300, 400), @@ -63,18 +62,18 @@ void testDeferToChild(){ ..color = Colors.deepPurple ..style = PaintingStyle.stroke ..strokeWidth = 70, gestureMap: getMap((_) { - print('purple oval'); - resultList.add('purple'); - }))); + print('purple oval'); + resultList.add('purple'); + }))); shapeHandler.addShape(Rectangle(Rect.fromLTWH(20, 300, 100, 300), paint: Paint() ..color = Colors.deepOrange ..style = PaintingStyle.stroke ..strokeWidth = 50, gestureMap: getMap((_) { - print('orange circle'); - resultList.add('orange'); - }))); + print('orange circle'); + resultList.add('orange'); + }))); var paint = Paint() ..color = Colors.greenAccent @@ -108,16 +107,18 @@ void testDeferToChild(){ ..strokeWidth = 40 ..style = PaintingStyle.stroke ..color = Colors.pink, gestureMap: getMap((_) { - print('pink arc'); - resultList.add('pink'); - })), + print('pink arc'); + resultList.add('pink'); + })), ); shapeHandler.addShape(RoundedRectangle( RRect.fromLTRBR(100, 340, 300, 650, Radius.elliptical(100, 150)), paint: Paint() ..strokeWidth = 40 - ..color = Colors.grey, hitTestBehavior: HitTestBehavior.deferToChild , gestureMap: getMap((_) { + ..color = Colors.grey, + hitTestBehavior: HitTestBehavior.deferToChild, + gestureMap: getMap((_) { print('rounded grey rect'); resultList.add('grey'); }))); @@ -129,26 +130,26 @@ void testDeferToChild(){ } addAllShapes(); - void handleGesture(Offset position , List expected){ + void handleGesture(Offset position, List expected) { resultList = []; - shapeHandler.handleGestureEvent(Gesture(GestureType.onTapDown, - TapDownDetails(localPosition: position))); + shapeHandler.handleGestureEvent(Gesture( + GestureType.onTapDown, TapDownDetails(localPosition: position))); expect(resultList, expected); } - test('Clicking a shape with (deferToChild) hittest behavior should not call its callback and pass the touch down to the shapes below it. ' ,(){ - handleGesture(Offset(212.2, 171.4) , ['pink']); - handleGesture(Offset(88.0, 356.2) , ['purple']); - handleGesture(Offset(166.1, 242.7) , ['black']); - handleGesture(Offset(249.9, 332.2) , ['black']); - handleGesture(Offset(134.1, 59.8) , ['black']); - handleGesture(Offset(87.2, 349.7) , ['purple']); - handleGesture(Offset(236.6, 426.7) , ['black']); - handleGesture(Offset(214.9, 483.4) , ['purple']); - handleGesture(Offset(213.0, 58.7) , ['green']); - handleGesture(Offset(270,10) , []); + test( + 'Clicking a shape with (deferToChild) hittest behavior should not call its callback and pass the touch down to the shapes below it. ', + () { + handleGesture(Offset(212.2, 171.4), ['pink']); + handleGesture(Offset(88.0, 356.2), ['purple']); + handleGesture(Offset(166.1, 242.7), ['black']); + handleGesture(Offset(249.9, 332.2), ['black']); + handleGesture(Offset(134.1, 59.8), ['black']); + handleGesture(Offset(87.2, 349.7), ['purple']); + handleGesture(Offset(236.6, 426.7), ['black']); + handleGesture(Offset(214.9, 483.4), ['purple']); + handleGesture(Offset(213.0, 58.7), ['green']); + handleGesture(Offset(270, 10), []); }); }); } - - \ No newline at end of file diff --git a/test/shape_handler/hittest_test/opaque.dart b/test/shape_handler/hittest_test/opaque.dart index 6e9079c..90e58c7 100644 --- a/test/shape_handler/hittest_test/opaque.dart +++ b/test/shape_handler/hittest_test/opaque.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 24,April,2020 - import 'dart:math'; import 'dart:ui'; @@ -16,8 +14,7 @@ import 'package:touchable/src/shapes/rounded_rectangle.dart'; import 'package:touchable/src/shapes/util.dart'; import 'package:touchable/touchable.dart'; - -void testOpaque(){ +void testOpaque() { group('SHape Handler : opaque hittest', () { var shapeHandler = ShapeHandler(); var resultList = []; @@ -55,9 +52,9 @@ void testOpaque(){ ..color = Colors.black ..strokeWidth = 200 ..style = PaintingStyle.stroke, gestureMap: getMap((detail) { - print('line touched'); - resultList.add('black'); - })), + print('line touched'); + resultList.add('black'); + })), ); shapeHandler.addShape(Oval(Rect.fromLTWH(100, 100, 300, 400), @@ -65,18 +62,18 @@ void testOpaque(){ ..color = Colors.deepPurple ..style = PaintingStyle.stroke ..strokeWidth = 70, gestureMap: getMap((_) { - print('purple oval'); - resultList.add('purple'); - }))); + print('purple oval'); + resultList.add('purple'); + }))); shapeHandler.addShape(Rectangle(Rect.fromLTWH(20, 300, 100, 300), paint: Paint() ..color = Colors.deepOrange ..style = PaintingStyle.stroke ..strokeWidth = 50, gestureMap: getMap((_) { - print('orange circle'); - resultList.add('orange'); - }))); + print('orange circle'); + resultList.add('orange'); + }))); var paint = Paint() ..color = Colors.greenAccent @@ -109,9 +106,9 @@ void testOpaque(){ ..strokeWidth = 40 ..style = PaintingStyle.stroke ..color = Colors.pink, gestureMap: getMap((_) { - print('pink arc'); - resultList.add('pink'); - })), + print('pink arc'); + resultList.add('pink'); + })), ); shapeHandler.addShape(RoundedRectangle( @@ -130,22 +127,24 @@ void testOpaque(){ } addAllShapes(); - void handleGesture(Offset position , List expected){ + void handleGesture(Offset position, List expected) { resultList = []; - shapeHandler.handleGestureEvent(Gesture(GestureType.onTapDown, - TapDownDetails(localPosition: position))); + shapeHandler.handleGestureEvent(Gesture( + GestureType.onTapDown, TapDownDetails(localPosition: position))); expect(resultList, expected); } - test('Test if clicking a certain position , call the appropriate callback functions' ,(){ - handleGesture(Offset(212.2, 171.4) , ['pink']); - handleGesture(Offset(88.0, 356.2) , ['purple']); - handleGesture(Offset(166.1, 242.7) , ['blue']); - handleGesture(Offset(249.9, 332.2) , ['black']); - handleGesture(Offset(134.1, 59.8) , ['black']); - handleGesture(Offset(87.2, 349.7) , ['purple']); - handleGesture(Offset(236.6, 426.7) , ['grey']); - handleGesture(Offset(213.0, 58.7) , ['green']); + test( + 'Test if clicking a certain position , call the appropriate callback functions', + () { + handleGesture(Offset(212.2, 171.4), ['pink']); + handleGesture(Offset(88.0, 356.2), ['purple']); + handleGesture(Offset(166.1, 242.7), ['blue']); + handleGesture(Offset(249.9, 332.2), ['black']); + handleGesture(Offset(134.1, 59.8), ['black']); + handleGesture(Offset(87.2, 349.7), ['purple']); + handleGesture(Offset(236.6, 426.7), ['grey']); + handleGesture(Offset(213.0, 58.7), ['green']); }); }); -} \ No newline at end of file +} diff --git a/test/shape_handler/hittest_test/translucent.dart b/test/shape_handler/hittest_test/translucent.dart index c2774db..c9016e8 100644 --- a/test/shape_handler/hittest_test/translucent.dart +++ b/test/shape_handler/hittest_test/translucent.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 24,April,2020 - import 'dart:math'; import 'dart:ui'; @@ -16,8 +14,7 @@ import 'package:touchable/src/shapes/rounded_rectangle.dart'; import 'package:touchable/src/shapes/util.dart'; import 'package:touchable/touchable.dart'; -void testTranslucent(){ - +void testTranslucent() { group('SHape Handler : Translucent hittest', () { var shapeHandler = ShapeHandler(); var resultList = []; @@ -55,9 +52,9 @@ void testTranslucent(){ ..color = Colors.black ..strokeWidth = 200 ..style = PaintingStyle.stroke, gestureMap: getMap((detail) { - print('line touched'); - resultList.add('black'); - })), + print('line touched'); + resultList.add('black'); + })), ); shapeHandler.addShape(Oval(Rect.fromLTWH(100, 100, 300, 400), @@ -65,18 +62,18 @@ void testTranslucent(){ ..color = Colors.deepPurple ..style = PaintingStyle.stroke ..strokeWidth = 70, gestureMap: getMap((_) { - print('purple oval'); - resultList.add('purple'); - }))); + print('purple oval'); + resultList.add('purple'); + }))); shapeHandler.addShape(Rectangle(Rect.fromLTWH(20, 300, 100, 300), paint: Paint() ..color = Colors.deepOrange ..style = PaintingStyle.stroke ..strokeWidth = 50, gestureMap: getMap((_) { - print('orange circle'); - resultList.add('orange'); - }))); + print('orange circle'); + resultList.add('orange'); + }))); var paint = Paint() ..color = Colors.greenAccent @@ -110,16 +107,17 @@ void testTranslucent(){ ..strokeWidth = 40 ..style = PaintingStyle.stroke ..color = Colors.pink, gestureMap: getMap((_) { - print('pink arc'); - resultList.add('pink'); - })), + print('pink arc'); + resultList.add('pink'); + })), ); shapeHandler.addShape(RoundedRectangle( RRect.fromLTRBR(100, 340, 300, 650, Radius.elliptical(100, 150)), paint: Paint() ..strokeWidth = 40 - ..color = Colors.grey, hitTestBehavior: HitTestBehavior.translucent, gestureMap: getMap((_) { + ..color = Colors.grey, + hitTestBehavior: HitTestBehavior.translucent, gestureMap: getMap((_) { print('rounded grey rect'); resultList.add('grey'); }))); @@ -131,26 +129,26 @@ void testTranslucent(){ } addAllShapes(); - void handleGesture(Offset position , List expected){ + void handleGesture(Offset position, List expected) { resultList = []; - shapeHandler.handleGestureEvent(Gesture(GestureType.onTapDown, - TapDownDetails(localPosition: position))); + shapeHandler.handleGestureEvent(Gesture( + GestureType.onTapDown, TapDownDetails(localPosition: position))); expect(resultList, expected); } - test('Clicking a certain TRANSLUCENT shape calls the callback functions of this shape and others below it' ,(){ - handleGesture(Offset(212.2, 171.4) , ['pink']); - handleGesture(Offset(88.0, 356.2) , ['purple']); - handleGesture(Offset(166.1, 242.7) , ['blue','black']); - handleGesture(Offset(249.9, 332.2) , ['black']); - handleGesture(Offset(134.1, 59.8) , ['black']); - handleGesture(Offset(87.2, 349.7) , ['purple']); - handleGesture(Offset(236.6, 426.7) , ['grey','black']); - handleGesture(Offset(214.9, 483.4) , ['grey','purple']); - handleGesture(Offset(213.0, 58.7) , ['green']); - handleGesture(Offset(270,10) , []); + test( + 'Clicking a certain TRANSLUCENT shape calls the callback functions of this shape and others below it', + () { + handleGesture(Offset(212.2, 171.4), ['pink']); + handleGesture(Offset(88.0, 356.2), ['purple']); + handleGesture(Offset(166.1, 242.7), ['blue', 'black']); + handleGesture(Offset(249.9, 332.2), ['black']); + handleGesture(Offset(134.1, 59.8), ['black']); + handleGesture(Offset(87.2, 349.7), ['purple']); + handleGesture(Offset(236.6, 426.7), ['grey', 'black']); + handleGesture(Offset(214.9, 483.4), ['grey', 'purple']); + handleGesture(Offset(213.0, 58.7), ['green']); + handleGesture(Offset(270, 10), []); }); }); } - - \ No newline at end of file diff --git a/test/shape_handler/shape_handler.dart b/test/shape_handler/shape_handler.dart index 95ffd88..25fcca9 100644 --- a/test/shape_handler/shape_handler.dart +++ b/test/shape_handler/shape_handler.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 23,April,2020 - import 'hittest_test/defer_to_child.dart'; import 'hittest_test/opaque.dart'; import 'hittest_test/translucent.dart'; diff --git a/test/shapes/arc.dart b/test/shapes/arc.dart index 789f853..ff052f0 100644 --- a/test/shapes/arc.dart +++ b/test/shapes/arc.dart @@ -1,4 +1,3 @@ -// Created by nateshmbhat on 16,April,2020 import 'dart:math'; import 'package:flutter/cupertino.dart'; diff --git a/test/shapes/circle.dart b/test/shapes/circle.dart index 4e3c9e4..b501237 100644 --- a/test/shapes/circle.dart +++ b/test/shapes/circle.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 04,April,2020 - import 'package:flutter/cupertino.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:touchable/src/shapes/circle.dart'; diff --git a/test/shapes/line.dart b/test/shapes/line.dart index 201f81f..96382c2 100644 --- a/test/shapes/line.dart +++ b/test/shapes/line.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 04,April,2020 - import 'dart:ui'; import 'package:flutter/material.dart'; diff --git a/test/shapes/oval.dart b/test/shapes/oval.dart index 6c86e51..f7c60c4 100644 --- a/test/shapes/oval.dart +++ b/test/shapes/oval.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 12,April,2020 - import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:touchable/src/shapes/oval.dart'; diff --git a/test/shapes/point.dart b/test/shapes/point.dart index e24f930..8663987 100644 --- a/test/shapes/point.dart +++ b/test/shapes/point.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 12,April,2020 - import 'dart:ui'; import 'package:flutter/cupertino.dart'; diff --git a/test/shapes/rectangle.dart b/test/shapes/rectangle.dart index 63abf3e..37db25e 100644 --- a/test/shapes/rectangle.dart +++ b/test/shapes/rectangle.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 06,April,2020 - import 'dart:ui'; import 'package:flutter/material.dart'; @@ -53,7 +51,9 @@ void testRectangle() { expect(rect1.isInside(Offset(26.7, 572.2)), true); }); - test('PaintStyleForTouch override test for Paint : Stroked Rectangle with filled touch : Point lies inside ', () { + test( + 'PaintStyleForTouch override test for Paint : Stroked Rectangle with filled touch : Point lies inside ', + () { var rect = Rectangle( Rect.fromLTWH( 0, diff --git a/test/shapes/rounded_rectangle.dart b/test/shapes/rounded_rectangle.dart index 27bf314..0bf0551 100644 --- a/test/shapes/rounded_rectangle.dart +++ b/test/shapes/rounded_rectangle.dart @@ -1,4 +1,3 @@ -// Created by nateshmbhat on 18,April,2020 import 'dart:ui'; import 'package:flutter/material.dart'; diff --git a/test/shapes/shape.dart b/test/shapes/shape.dart index b3f33d4..4e3e37b 100644 --- a/test/shapes/shape.dart +++ b/test/shapes/shape.dart @@ -1,5 +1,3 @@ -// Created by nateshmbhat on 04,April,2020 - import 'arc.dart'; import 'circle.dart'; import 'line.dart';