@@ -2,76 +2,64 @@ import 'package:badgemagic/badge_animation/animation_abstract.dart';
2
2
3
3
class SnowFlakeAnimation extends BadgeAnimation {
4
4
@override
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;
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 ;
20
16
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;
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 ;
29
22
30
- int currentFrame = (animationIndex ~ / badgeWidth) % framesCount;
23
+ int sourceRow = i - verticalOffset;
24
+ int sourceCol = j - horizontalOffset;
31
25
32
- int startCol = currentFrame * badgeWidth;
33
- if (startCol + badgeWidth > newGridWidth) {
34
- startCol = newGridWidth - badgeWidth;
35
- if (startCol < 0 ) startCol = 0 ;
36
- }
26
+ bool isWithinNewGrid = sourceRow >= 0 &&
27
+ sourceRow < newHeight &&
28
+ sourceCol >= 0 &&
29
+ sourceCol < displayWidth;
37
30
38
- List <List <bool >> tempFrame = List .generate (
39
- badgeHeight,
40
- (_) => List .filled (badgeWidth, false ),
41
- );
31
+ int leftCenterCol = badgeWidth ~ / 2 - 1 ;
32
+ int rightCenterCol = badgeWidth ~ / 2 ;
42
33
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
- }
34
+ int maxDistance = leftCenterCol;
50
35
51
- int shiftLeftBy =
52
- _countLeadingEmptyCols (tempFrame, badgeHeight, badgeWidth);
36
+ int currentAnimationIndex = animationIndex % (maxDistance + 1 );
53
37
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
- }
38
+ int leftColPos = leftCenterCol - currentAnimationIndex;
39
+ int rightColPos = rightCenterCol + currentAnimationIndex;
40
+
41
+ if (leftColPos < 0 ) leftColPos += badgeWidth;
42
+ if (rightColPos >= badgeWidth) rightColPos -= badgeWidth;
61
43
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 ;
44
+ if (j == leftColPos || j == rightColPos) {
45
+ lineShow = true ;
46
+ } else {
47
+ lineShow = false ;
69
48
}
70
- }
71
- if (! isColEmpty) {
72
- return col;
49
+
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
+ }
59
+ }
60
+
61
+ canvas[i][j] = (lineShow || bitmapShowOut || bitmapShowcenter);
73
62
}
74
63
}
75
- return 0 ;
76
64
}
77
65
}
0 commit comments