@@ -2,64 +2,76 @@ import 'package:badgemagic/badge_animation/animation_abstract.dart';
2
2
3
3
class AniAnimation extends BadgeAnimation {
4
4
@override
5
- void processAnimation (int badgeHeight, int badgeWidth, int animationIndex,
6
- List <List <bool >> processGrid, List <List <bool >> canvas) {
7
- int newWidth = processGrid[0 ].length;
8
- int newHeight = processGrid.length;
9
- int verticalOffset = (badgeHeight - newHeight) ~ / 2 ;
10
- int displayWidth = newWidth > badgeWidth ? badgeWidth : newWidth;
11
- int horizontalOffset = (badgeWidth - displayWidth) ~ / 2 ;
12
- var totalAnimationLength = badgeWidth;
13
- int frame = animationIndex % totalAnimationLength;
14
- var firstHalf = frame < badgeWidth ~ / 2 ;
15
- var secondHalf = frame >= badgeWidth ~ / 2 ;
16
-
17
- for (int i = 0 ; i < badgeHeight; i++ ) {
18
- for (int j = 0 ; j < badgeWidth; j++ ) {
19
- bool lineShow = false ;
20
- bool bitmapShowcenter = false ;
21
- bool bitmapShowOut = false ;
22
-
23
- int sourceRow = i - verticalOffset;
24
- int sourceCol = j - horizontalOffset;
5
+ void processAnimation (
6
+ int badgeHeight,
7
+ int badgeWidth,
8
+ int animationIndex,
9
+ List <List <bool >> processGrid,
10
+ List <List <bool >> canvas,
11
+ ) {
12
+ if (processGrid.isEmpty ||
13
+ processGrid.any ((row) => row.length != processGrid[0 ].length)) {
14
+ throw ArgumentError (
15
+ 'processGrid must be a non-empty rectangular 2D list' ,
16
+ );
17
+ }
18
+ int newGridHeight = processGrid.length;
19
+ int newGridWidth = processGrid[0 ].length;
25
20
26
- bool isWithinNewGrid = sourceRow >= 0 &&
27
- sourceRow < newHeight &&
28
- sourceCol >= 0 &&
29
- sourceCol < displayWidth;
21
+ if (canvas.length < badgeHeight ||
22
+ canvas.any ((row) => row.length < badgeWidth)) {
23
+ throw ArgumentError (
24
+ 'canvas must have at least $badgeHeight rows and $badgeWidth columns' ,
25
+ );
26
+ }
27
+ int framesCount = (newGridWidth / badgeWidth).ceil ();
28
+ framesCount = framesCount == 0 ? 1 : framesCount;
30
29
31
- int leftCenterCol = badgeWidth ~ / 2 - 1 ;
32
- int rightCenterCol = badgeWidth ~ / 2 ;
30
+ int currentFrame = (animationIndex ~ / badgeWidth) % framesCount;
33
31
34
- int maxDistance = leftCenterCol;
32
+ int startCol = currentFrame * badgeWidth;
33
+ if (startCol + badgeWidth > newGridWidth) {
34
+ startCol = newGridWidth - badgeWidth;
35
+ if (startCol < 0 ) startCol = 0 ;
36
+ }
35
37
36
- int currentAnimationIndex = animationIndex % (maxDistance + 1 );
38
+ List <List <bool >> tempFrame = List .generate (
39
+ badgeHeight,
40
+ (_) => List .filled (badgeWidth, false ),
41
+ );
37
42
38
- int leftColPos = leftCenterCol - currentAnimationIndex;
39
- int rightColPos = rightCenterCol + currentAnimationIndex;
43
+ for (int i = 0 ; i < badgeHeight; i++ ) {
44
+ for (int j = 0 ; j < badgeWidth; j++ ) {
45
+ int gridCol = startCol + j;
46
+ bool isValid = i < newGridHeight && gridCol < newGridWidth;
47
+ tempFrame[i][j] = isValid ? processGrid[i][gridCol] : false ;
48
+ }
49
+ }
40
50
41
- if (leftColPos < 0 ) leftColPos += badgeWidth;
42
- if (rightColPos >= badgeWidth) rightColPos -= badgeWidth ;
51
+ int shiftLeftBy =
52
+ _countLeadingEmptyCols (tempFrame, badgeHeight, badgeWidth);
43
53
44
- if (j == leftColPos || j == rightColPos) {
45
- lineShow = true ;
46
- } else {
47
- lineShow = false ;
48
- }
54
+ for (int i = 0 ; i < badgeHeight; i++ ) {
55
+ for (int j = 0 ; j < badgeWidth; j++ ) {
56
+ int sourceCol = j + shiftLeftBy;
57
+ canvas[i][j] = sourceCol < badgeWidth ? tempFrame[i][sourceCol] : false ;
58
+ }
59
+ }
60
+ }
49
61
50
- if (firstHalf) {
51
- if (isWithinNewGrid && j > leftColPos && j < rightColPos) {
52
- bitmapShowcenter = processGrid[sourceRow][sourceCol];
53
- }
54
- }
55
- if (secondHalf) {
56
- if (isWithinNewGrid && (j < leftColPos || j > rightColPos)) {
57
- bitmapShowOut = processGrid[sourceRow][sourceCol];
58
- }
62
+ int _countLeadingEmptyCols (List <List <bool >> frame, int height, int width) {
63
+ for (int col = 0 ; col < width; col++ ) {
64
+ bool isColEmpty = true ;
65
+ for (int row = 0 ; row < height; row++ ) {
66
+ if (frame[row][col]) {
67
+ isColEmpty = false ;
68
+ break ;
59
69
}
60
-
61
- canvas[i][j] = (lineShow || bitmapShowOut || bitmapShowcenter);
70
+ }
71
+ if (! isColEmpty) {
72
+ return col;
62
73
}
63
74
}
75
+ return 0 ;
64
76
}
65
77
}
0 commit comments