Skip to content

Implement magnifier that enlarges the area around the touch bubble on panDown #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions example/lib/edge_detection_shape/animated_touch_bubble_part.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ class _AnimatedTouchBubblePartState extends State<AnimatedTouchBubblePart> with
);

_sizeAnimation = Tween<double>(
begin: 0.5,
end: 1.0
begin: 0.5,
end: 1.0
).animate(_controller);

_colorAnimation = ColorTween(
begin: Theme.of(context).accentColor.withOpacity(0.5),
end: Theme.of(context).accentColor.withOpacity(0.0)
begin: Theme.of(context).accentColor.withOpacity(0.5),
end: Theme.of(context).accentColor.withOpacity(0.0)
).animate(
CurvedAnimation(
parent: _controller,
curve: Interval(0.5, 1.0)
)
CurvedAnimation(
parent: _controller,
curve: Interval(0.5, 1.0)
)
);

_controller.repeat();
Expand All @@ -53,31 +53,31 @@ class _AnimatedTouchBubblePartState extends State<AnimatedTouchBubblePart> with
children: [
Center(
child: Container(
width: widget.dragging ? widget.size : widget.size / 2,
height: widget.dragging ? widget.size : widget.size / 2,
width: widget.dragging ? 0 : widget.size / 2,
height: widget.dragging ? 0 : widget.size / 2,
decoration: BoxDecoration(
color: Theme.of(context).accentColor.withOpacity(0.5),
borderRadius: widget.dragging ? BorderRadius.circular(widget.size) : BorderRadius.circular(widget.size / 4)
color: Theme.of(context).accentColor.withOpacity(0.5),
borderRadius: widget.dragging ? BorderRadius.circular(widget.size) : BorderRadius.circular(widget.size / 4)
)
)
),
AnimatedBuilder(
builder: (BuildContext context, Widget child) {
return Center(
child: Container(
width: widget.dragging ? 0 : widget.size * _sizeAnimation.value,
height: widget.dragging ? 0 : widget.size * _sizeAnimation.value,
decoration: BoxDecoration(
border: Border.all(
color: _colorAnimation.value,
width: widget.size / 20
),
borderRadius: widget.dragging ? BorderRadius.zero : BorderRadius.circular(widget.size * _sizeAnimation.value / 2)
)
builder: (BuildContext context, Widget child) {
return Center(
child: Container(
width: widget.dragging ? 0 : widget.size * _sizeAnimation.value,
height: widget.dragging ? 0 : widget.size * _sizeAnimation.value,
decoration: BoxDecoration(
border: Border.all(
color: _colorAnimation.value,
width: widget.size / 20
),
borderRadius: widget.dragging ? BorderRadius.zero : BorderRadius.circular(widget.size * _sizeAnimation.value / 2)
)
);
},
animation: _controller
)
);
},
animation: _controller
)
],
);
Expand Down
127 changes: 88 additions & 39 deletions example/lib/edge_detection_shape/edge_detection_shape.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:simple_edge_detection/edge_detection.dart';

import 'edge_painter.dart';
import 'magnifier.dart';
import 'touch_bubble.dart';

class EdgeDetectionShape extends StatefulWidget {
Expand Down Expand Up @@ -33,10 +34,12 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
double top;
double left;

Offset currentDragPosition;

@override
void didChangeDependencies() {
double shortestSide = min(MediaQuery.of(context).size.width, MediaQuery.of(context).size.height);
edgeDraggerSize = shortestSide / 8;
edgeDraggerSize = shortestSide / 12;
super.didChangeDependencies();
}

Expand All @@ -50,19 +53,23 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {

@override
Widget build(BuildContext context) {
return Stack(
children: [
_getTouchBubbles(),
CustomPaint(
painter: EdgePainter(
points: points,
color: Theme.of(context).accentColor.withOpacity(0.5)
),
)
],
return Magnifier(
visible: currentDragPosition != null,
position: currentDragPosition,
child: Stack(
children: [
_getTouchBubbles(),
CustomPaint(
painter: EdgePainter(
points: points,
color: Theme.of(context).accentColor.withOpacity(0.5)
)
)
],
)
);
}

void _calculateDimensionValues() {
top = 0.0;
left = 0.0;
Expand All @@ -85,17 +92,43 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
);
}

Offset _clampOffset(Offset givenOffset) {
double absoluteX = givenOffset.dx * renderedImageWidth;
double absoluteY = givenOffset.dy * renderedImageHeight;

return Offset(
absoluteX.clamp(0.0, renderedImageWidth) / renderedImageWidth,
absoluteY.clamp(0.0, renderedImageHeight) / renderedImageHeight
);
}

Widget _getTouchBubbles() {
points = [
Offset(left + edgeDetectionResult.topLeft.dx * renderedImageWidth, top + edgeDetectionResult.topLeft.dy * renderedImageHeight),
Offset(left + edgeDetectionResult.topRight.dx * renderedImageWidth, top + edgeDetectionResult.topRight.dy * renderedImageHeight),
Offset(left + edgeDetectionResult.bottomRight.dx * renderedImageWidth, top + (edgeDetectionResult.bottomRight.dy * renderedImageHeight)),
Offset(left + edgeDetectionResult.bottomLeft.dx * renderedImageWidth, top + edgeDetectionResult.bottomLeft.dy * renderedImageHeight),
Offset(left + edgeDetectionResult.topLeft.dx * renderedImageWidth, top + edgeDetectionResult.topLeft.dy * renderedImageHeight),
Offset(
left + edgeDetectionResult.topLeft.dx * renderedImageWidth,
top + edgeDetectionResult.topLeft.dy * renderedImageHeight
),
Offset(
left + edgeDetectionResult.topRight.dx * renderedImageWidth,
top + edgeDetectionResult.topRight.dy * renderedImageHeight
),
Offset(
left + edgeDetectionResult.bottomRight.dx * renderedImageWidth,
top + (edgeDetectionResult.bottomRight.dy * renderedImageHeight)
),
Offset(
left + edgeDetectionResult.bottomLeft.dx * renderedImageWidth,
top + edgeDetectionResult.bottomLeft.dy * renderedImageHeight
),
Offset(
left + edgeDetectionResult.topLeft.dx * renderedImageWidth,
top + edgeDetectionResult.topLeft.dy * renderedImageHeight
),
];

final Function onDragFinished = () {
setState(() {});
currentDragPosition = null;
setState(() {});
};

return Container(
Expand All @@ -108,8 +141,12 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
size: edgeDraggerSize,
onDrag: (position) {
setState(() {
edgeDetectionResult.topLeft += _getNewPositionAfterDrag(
position, renderedImageWidth, renderedImageHeight
currentDragPosition = Offset(points[0].dx, points[0].dy);
Offset newTopLeft = _getNewPositionAfterDrag(
position, renderedImageWidth, renderedImageHeight
);
edgeDetectionResult.topLeft = _clampOffset(
edgeDetectionResult.topLeft + newTopLeft
);
});
},
Expand All @@ -123,9 +160,13 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
size: edgeDraggerSize,
onDrag: (position) {
setState(() {
edgeDetectionResult.topRight += _getNewPositionAfterDrag(
position, renderedImageWidth, renderedImageHeight
Offset newTopRight = _getNewPositionAfterDrag(
position, renderedImageWidth, renderedImageHeight
);
edgeDetectionResult.topRight = _clampOffset(
edgeDetectionResult.topRight + newTopRight
);
currentDragPosition = Offset(points[1].dx, points[1].dy);
});
},
onDragFinished: onDragFinished
Expand All @@ -135,30 +176,38 @@ class _EdgeDetectionShapeState extends State<EdgeDetectionShape> {
),
Positioned(
child: TouchBubble(
size: edgeDraggerSize,
onDrag: (position) {
setState(() {
edgeDetectionResult.bottomRight += _getNewPositionAfterDrag(
size: edgeDraggerSize,
onDrag: (position) {
setState(() {
Offset newBottomRight = _getNewPositionAfterDrag(
position, renderedImageWidth, renderedImageHeight
);
});
},
onDragFinished: onDragFinished
);
edgeDetectionResult.bottomRight = _clampOffset(
edgeDetectionResult.bottomRight + newBottomRight
);
currentDragPosition = Offset(points[2].dx, points[2].dy);
});
},
onDragFinished: onDragFinished
),
left: points[2].dx - (edgeDraggerSize / 2),
top: points[2].dy - (edgeDraggerSize / 2)
),
Positioned(
child: TouchBubble(
size: edgeDraggerSize,
onDrag: (position) {
setState(() {
edgeDetectionResult.bottomLeft += _getNewPositionAfterDrag(
position, renderedImageWidth, renderedImageHeight
);
});
},
onDragFinished: onDragFinished
size: edgeDraggerSize,
onDrag: (position) {
setState(() {
Offset newBottomLeft = _getNewPositionAfterDrag(
position, renderedImageWidth, renderedImageHeight
);
edgeDetectionResult.bottomLeft = _clampOffset(
edgeDetectionResult.bottomLeft + newBottomLeft
);
currentDragPosition = Offset(points[3].dx, points[3].dy);
});
},
onDragFinished: onDragFinished
),
left: points[3].dx - (edgeDraggerSize / 2),
top: points[3].dy - (edgeDraggerSize / 2)
Expand Down
Loading