Skip to content

Commit

Permalink
feat: allow scaling arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-anders committed Jul 3, 2024
1 parent 1f4e26c commit 0ce50dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/src/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,15 @@ class Arrow implements Shape {
final Color color;
final SquareId orig;
final SquareId dest;
/// Width of the arrow and size of its tip will be scaled by this factor.
/// If 1.0, the width will be squareSize/4
final double scale;

const Arrow({
required this.color,
required this.orig,
required this.dest,
this.scale = 1.0,
});

@override
Expand Down
10 changes: 7 additions & 3 deletions lib/src/widgets/shape.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ShapeWidget extends StatelessWidget {
color: final color,
orig: final orig,
dest: final dest,
scale: final scale,
) =>
SizedBox.square(
dimension: boardSize,
Expand All @@ -34,6 +35,7 @@ class ShapeWidget extends StatelessWidget {
orientation,
Coord.fromSquareId(orig),
Coord.fromSquareId(dest),
scale,
),
),
),
Expand Down Expand Up @@ -62,17 +64,19 @@ class ShapeWidget extends StatelessWidget {
}

class _ArrowPainter extends CustomPainter {
_ArrowPainter(this.color, this.orientation, this.fromCoord, this.toCoord);
_ArrowPainter(
this.color, this.orientation, this.fromCoord, this.toCoord, this.scale);

final Color color;
final Side orientation;
final Coord fromCoord;
final Coord toCoord;
final double scale;

@override
void paint(Canvas canvas, Size size) {
final squareSize = size.width / 8;
final lineWidth = squareSize / 4;
final lineWidth = scale * squareSize / 4;
final paint = Paint()
..strokeWidth = lineWidth
..color = color
Expand All @@ -87,7 +91,7 @@ class _ArrowPainter extends CustomPainter {
math.atan2(toOffset.dy - fromOffset.dy, toOffset.dx - fromOffset.dx);
final fromMarginOffset =
Offset(margin * math.cos(angle), margin * math.sin(angle));
final arrowSize = squareSize * 0.48;
final arrowSize = scale * squareSize * 0.48;
const arrowAngle = math.pi / 5;

final from = fromOffset + shift + fromMarginOffset;
Expand Down

0 comments on commit 0ce50dd

Please sign in to comment.