|
| 1 | +import 'package:flutter/material.dart'; |
1 | 2 | import 'package:badgemagic/badge_animation/ani_left.dart';
|
2 | 3 | import 'package:badgemagic/badge_animation/animation_abstract.dart';
|
3 |
| -import 'package:flutter/material.dart'; |
4 | 4 |
|
5 |
| -// Optional: shape enum, doesn't affect freehand |
6 | 5 | enum DrawShape { freehand, square, rectangle, circle, triangle }
|
7 | 6 |
|
8 | 7 | class DrawBadgeProvider extends ChangeNotifier {
|
9 |
| - // 11x44 LED grid, default all false (off) |
| 8 | + final int rows = 11; |
| 9 | + final int cols = 44; |
| 10 | + |
10 | 11 | List<List<bool>> _drawViewGrid =
|
11 |
| - List.generate(11, (i) => List.generate(44, (j) => false)); |
| 12 | + List.generate(11, (_) => List.generate(44, (_) => false)); |
12 | 13 |
|
13 |
| - // Drawing mode (true = draw, false = erase) |
14 |
| - bool isDrawing = true; |
| 14 | + final List<List<bool>> _previewGrid = |
| 15 | + List.generate(11, (_) => List.generate(44, (_) => false)); |
15 | 16 |
|
16 |
| - // Currently selected shape |
| 17 | + bool isDrawing = true; |
17 | 18 | DrawShape _selectedShape = DrawShape.freehand;
|
18 |
| - |
19 |
| - // Animation used in badge (not part of drawing) |
20 | 19 | BadgeAnimation currentAnimation = LeftAnimation();
|
21 | 20 |
|
22 |
| - // Return the current grid |
23 |
| - List<List<bool>> getDrawViewGrid() => _drawViewGrid; |
24 |
| - |
25 |
| - // Return current drawing mode |
26 |
| - bool getIsDrawing() => isDrawing; |
| 21 | + List<List<bool>> getDrawViewGrid() { |
| 22 | + // Merge preview + permanent grid |
| 23 | + final combined = List.generate( |
| 24 | + rows, |
| 25 | + (i) => List.generate(cols, (j) { |
| 26 | + return _drawViewGrid[i][j] || _previewGrid[i][j]; |
| 27 | + })); |
| 28 | + return combined; |
| 29 | + } |
27 | 30 |
|
28 |
| - // Return selected shape |
29 | 31 | DrawShape get selectedShape => _selectedShape;
|
| 32 | + bool getIsDrawing() => isDrawing; |
30 | 33 |
|
31 |
| - // Toggle between drawing and erasing |
32 | 34 | void toggleIsDrawing(bool drawing) {
|
33 | 35 | isDrawing = drawing;
|
34 | 36 | notifyListeners();
|
35 | 37 | }
|
36 | 38 |
|
37 |
| - // Set selected shape |
38 | 39 | void setShape(DrawShape shape) {
|
39 | 40 | _selectedShape = shape;
|
40 | 41 | notifyListeners();
|
41 | 42 | }
|
42 | 43 |
|
43 |
| - // Set a single LED (used by gesture drawing) |
44 |
| - void setDrawViewGrid(int row, int col) { |
45 |
| - // Only allow grid update for freehand drawing |
46 |
| - if (_selectedShape == DrawShape.freehand) { |
47 |
| - if (row >= 0 && |
48 |
| - row < _drawViewGrid.length && |
49 |
| - col >= 0 && |
50 |
| - col < _drawViewGrid[0].length) { |
51 |
| - _drawViewGrid[row][col] = isDrawing; |
52 |
| - notifyListeners(); |
| 44 | + void setCell(int row, int col, bool value, {bool preview = false}) { |
| 45 | + if (row >= 0 && row < rows && col >= 0 && col < cols) { |
| 46 | + if (preview) { |
| 47 | + _previewGrid[row][col] = value; |
| 48 | + } else { |
| 49 | + _drawViewGrid[row][col] = value; |
53 | 50 | }
|
54 | 51 | }
|
55 | 52 | }
|
56 | 53 |
|
57 |
| - // Reset the grid to all OFF |
| 54 | + void clearPreviewGrid() { |
| 55 | + for (int i = 0; i < rows; i++) { |
| 56 | + for (int j = 0; j < cols; j++) { |
| 57 | + _previewGrid[i][j] = false; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + void commitGridUpdate() { |
| 63 | + for (int i = 0; i < rows; i++) { |
| 64 | + for (int j = 0; j < cols; j++) { |
| 65 | + if (_previewGrid[i][j]) { |
| 66 | + _drawViewGrid[i][j] = _previewGrid[i][j]; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + clearPreviewGrid(); |
| 71 | + notifyListeners(); |
| 72 | + } |
| 73 | + |
58 | 74 | void resetDrawViewGrid() {
|
59 |
| - _drawViewGrid = List.generate(11, (i) => List.generate(44, (j) => false)); |
| 75 | + _drawViewGrid = |
| 76 | + List.generate(rows, (_) => List.generate(cols, (_) => false)); |
60 | 77 | notifyListeners();
|
61 | 78 | }
|
62 | 79 |
|
63 |
| - // Load a grid into the draw view |
64 | 80 | void updateDrawViewGrid(List<List<bool>> badgeData) {
|
65 |
| - for (int i = 0; i < _drawViewGrid.length; i++) { |
66 |
| - for (int j = 0; j < _drawViewGrid[0].length; j++) { |
67 |
| - if (j < badgeData[0].length) { |
68 |
| - _drawViewGrid[i][j] = badgeData[i][j]; |
69 |
| - } else { |
70 |
| - _drawViewGrid[i][j] = false; |
71 |
| - } |
| 81 | + for (int i = 0; i < rows; i++) { |
| 82 | + for (int j = 0; j < cols; j++) { |
| 83 | + _drawViewGrid[i][j] = |
| 84 | + (j < badgeData[0].length) ? badgeData[i][j] : false; |
72 | 85 | }
|
73 | 86 | }
|
74 | 87 | notifyListeners();
|
75 | 88 | }
|
| 89 | + |
| 90 | + GridPosition getGridPosition(Offset position, double cellSize) { |
| 91 | + final row = (position.dy / cellSize).floor(); |
| 92 | + final col = (position.dx / cellSize).floor(); |
| 93 | + return GridPosition(row, col); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +class GridPosition { |
| 98 | + final int x; |
| 99 | + final int y; |
| 100 | + GridPosition(this.x, this.y); |
76 | 101 | }
|
0 commit comments