recalculate bounding rect of stroke when adding point

add eraser prototype functionality
This commit is contained in:
lukas-heiligenbrunner 2022-10-28 23:59:12 +02:00
parent c5bce47222
commit 57cff08218
2 changed files with 92 additions and 56 deletions

View File

@ -3,6 +3,11 @@ import 'dart:ui';
class Stroke { class Stroke {
List<Point> points = <Point>[]; List<Point> points = <Point>[];
double _minx = double.infinity;
double _maxx = double.negativeInfinity;
double _miny = double.infinity;
double _maxy = double.negativeInfinity;
@override @override
String toString() { String toString() {
return 'Stroke{points: $points}'; return 'Stroke{points: $points}';
@ -10,6 +15,16 @@ class Stroke {
void addPoint(Point point) { void addPoint(Point point) {
points.add(point); points.add(point);
// update bounding rect
if (point.point.dx < _minx) _minx = point.point.dx;
if (point.point.dx > _maxx) _maxx = point.point.dx;
if (point.point.dy < _miny) _miny = point.point.dy;
if (point.point.dy > _maxy) _maxy = point.point.dy;
}
Rect getBoundingRect() {
return Rect.fromPoints(Offset(_minx, _miny), Offset(_maxx, _maxy));
} }
Stroke.fromPoints(this.points); Stroke.fromPoints(this.points);

View File

@ -111,6 +111,27 @@ class _DrawingPageState extends State<DrawingPage> {
return; return;
} }
// todo outsource this eraser pen
if (eraseractive) {
// todo dynamic eraser size
final eraserrect = Rect.fromCircle(center: pos, radius: 3);
for (final stroke in _strokes) {
// check if delete action was within bounding rect of stroke
if (stroke.getBoundingRect().contains(pos)) {
// check if eraser hit an point within its range
for (final pt in stroke.points) {
if (eraserrect.contains(pt.point)) {
setState(() {
_strokes = List.from(_strokes)..remove(stroke);
});
return;
}
}
}
}
return;
}
if (allowDrawWithFinger || event.kind != PointerDeviceKind.touch) { if (allowDrawWithFinger || event.kind != PointerDeviceKind.touch) {
final pts = _strokes.last.points; final pts = _strokes.last.points;
@ -134,8 +155,7 @@ class _DrawingPageState extends State<DrawingPage> {
Widget _buildCanvas() { Widget _buildCanvas() {
final size = MediaQuery.of(context).size; final size = MediaQuery.of(context).size;
return Scaffold( return Listener(
body: Listener(
behavior: HitTestBehavior.opaque, behavior: HitTestBehavior.opaque,
onPointerMove: (e) => _onPointerMove(e, size), onPointerMove: (e) => _onPointerMove(e, size),
onPointerSignal: (event) { onPointerSignal: (event) {
@ -162,6 +182,8 @@ class _DrawingPageState extends State<DrawingPage> {
} }
}, },
onPointerUp: (event) { onPointerUp: (event) {
if (eraseractive) return;
if (allowDrawWithFinger || event.kind != PointerDeviceKind.touch) { if (allowDrawWithFinger || event.kind != PointerDeviceKind.touch) {
if (_strokes.last.points.length <= 1) { if (_strokes.last.points.length <= 1) {
// if the line consists only of one point (point) add endpoint as the same to allow drawing a line // if the line consists only of one point (point) add endpoint as the same to allow drawing a line
@ -200,7 +222,6 @@ class _DrawingPageState extends State<DrawingPage> {
size: Size.infinite, size: Size.infinite,
), ),
), ),
),
); );
} }
} }