creek
The AI Image Editor of 2030git clone https://github.com/maydayv7/creek
lib/ui/widgets/selection_overlay_painter.dart (2532B)
1import 'package:flutter/material.dart';
2import 'package:creekui/data/models/canvas_models.dart';
3import 'package:creekui/ui/styles/variables.dart';
4
5class SelectionOverlayPainter extends CustomPainter {
6 final Rect rect;
7 final bool isResizing;
8 final DragHandle activeHandle;
9
10 SelectionOverlayPainter({
11 required this.rect,
12 this.isResizing = false,
13 this.activeHandle = DragHandle.none,
14 });
15
16 @override
17 void paint(Canvas canvas, Size size) {
18 // 1. Dim the rest of image
19 final backgroundPath =
20 Path()..addRect(Rect.fromLTWH(0, 0, size.width, size.height));
21 final selectionPath = Path()..addRect(rect);
22 final dimmedPath = Path.combine(
23 PathOperation.difference,
24 backgroundPath,
25 selectionPath,
26 );
27
28 final dimPaint =
29 Paint()
30 ..color = Colors.black.withOpacity(0.5)
31 ..style = PaintingStyle.fill;
32
33 canvas.drawPath(dimmedPath, dimPaint);
34
35 // 2. Draw Selection Border
36 final paint =
37 Paint()
38 ..color = Variables.selectionBorder
39 ..style = PaintingStyle.stroke
40 ..strokeWidth = 2.0;
41
42 // Use a dash effect for the selection box
43 _drawDashedRect(canvas, rect, paint);
44
45 // 3. If Resizing, Draw Handles
46 if (isResizing) {
47 final handlePaint =
48 Paint()
49 ..color = Colors.white
50 ..style = PaintingStyle.fill;
51
52 final handleBorderPaint =
53 Paint()
54 ..color = Variables.selectionBorder
55 ..style = PaintingStyle.stroke
56 ..strokeWidth = 2.0;
57
58 final double handleRadius = 6.0;
59
60 final handles = [
61 rect.topLeft,
62 rect.topRight,
63 rect.bottomLeft,
64 rect.bottomRight,
65 ];
66
67 for (final handle in handles) {
68 canvas.drawCircle(handle, handleRadius, handlePaint);
69 canvas.drawCircle(handle, handleRadius, handleBorderPaint);
70 }
71
72 // 4. Draw Center Handle (Move)
73 final centerPaint =
74 Paint()
75 ..color = Variables.accentMagic.withOpacity(0.5)
76 ..style = PaintingStyle.fill;
77
78 canvas.drawCircle(rect.center, 8.0, centerPaint);
79 }
80 }
81
82 void _drawDashedRect(Canvas canvas, Rect rect, Paint paint) {
83 final path = Path()..addRect(rect);
84 canvas.drawPath(path, paint);
85 }
86
87 @override
88 bool shouldRepaint(covariant SelectionOverlayPainter oldDelegate) {
89 return oldDelegate.rect != rect ||
90 oldDelegate.isResizing != isResizing ||
91 oldDelegate.activeHandle != activeHandle;
92 }
93}