Skip to content

Commit cac7b13

Browse files
ctrlVnthpdang
andauthored
fix: snowflake, down, up and picture transitions same of the badge (#1751)
Co-authored-by: Hong Phuc Dang <dhppat@gmail.com>
1 parent d81edd2 commit cac7b13

4 files changed

Lines changed: 163 additions & 45 deletions

File tree

lib/badge_animation/ani_down.dart

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,55 @@ class DownAnimation extends BadgeAnimation {
66
List<List<bool>> processGrid, List<List<bool>> canvas) {
77
int newWidth = processGrid[0].length;
88
int newHeight = processGrid.length;
9-
int animationValue = animationIndex ~/ ((newWidth / badgeHeight));
9+
10+
bool isTextTooLong = newWidth > badgeWidth;
11+
12+
int totalPages = 1;
13+
int currentPage = 0;
14+
int startColOffset = 0;
15+
int horizontalOffset = 0;
16+
17+
int holdDuration = 15;
18+
19+
int singlePageDuration = (badgeHeight * 2) + holdDuration;
20+
21+
if (isTextTooLong) {
22+
totalPages = (newWidth / badgeWidth).ceil();
23+
if (totalPages == 0) totalPages = 1;
24+
currentPage = (animationIndex ~/ singlePageDuration) % totalPages;
25+
startColOffset = currentPage * badgeWidth;
26+
} else {
27+
horizontalOffset = (badgeWidth - newWidth) ~/ 2;
28+
}
29+
30+
int localFrame = animationIndex % singlePageDuration;
31+
int verticalScrollOffset;
32+
33+
if (localFrame < badgeHeight) {
34+
verticalScrollOffset = badgeHeight - localFrame;
35+
} else if (localFrame >= badgeHeight &&
36+
localFrame < (badgeHeight + holdDuration)) {
37+
verticalScrollOffset = 0;
38+
} else {
39+
verticalScrollOffset = -(localFrame - badgeHeight - holdDuration);
40+
}
41+
1042
for (int i = 0; i < badgeHeight; i++) {
43+
int sourceRow = i + verticalScrollOffset;
44+
1145
for (int j = 0; j < badgeWidth; j++) {
12-
if (j < badgeWidth && i < badgeHeight) {
13-
bool upCondition = (i >= 0 &&
14-
i < newHeight &&
15-
j >= 0 &&
16-
j < newWidth &&
17-
processGrid[(i - animationValue + newHeight) % newHeight][j]);
18-
19-
canvas[i][j] = upCondition;
46+
int sourceCol =
47+
isTextTooLong ? (startColOffset + j) : (j - horizontalOffset);
48+
49+
bool isWithinGrid = sourceRow >= 0 &&
50+
sourceRow < newHeight &&
51+
sourceCol >= 0 &&
52+
sourceCol < newWidth;
53+
54+
if (isWithinGrid) {
55+
canvas[i][j] = processGrid[sourceRow][sourceCol];
56+
} else {
57+
canvas[i][j] = false;
2058
}
2159
}
2260
}

lib/badge_animation/ani_picture.dart

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,56 @@ class PictureAnimation extends BadgeAnimation {
77
int newWidth = processGrid[0].length;
88
int newHeight = processGrid.length;
99
int verticalOffset = (badgeHeight - newHeight) ~/ 2;
10-
int displayWidth = newWidth > badgeWidth ? badgeWidth : newWidth;
11-
int horizontalOffset = (badgeWidth - displayWidth) ~/ 2;
10+
11+
bool isTextTooLong = newWidth > badgeWidth;
12+
13+
int totalPages = 1;
14+
int currentPage = 0;
15+
int startColOffset = 0;
16+
int horizontalOffset = 0;
17+
1218
var totalAnimationLength = badgeWidth;
19+
20+
if (isTextTooLong) {
21+
totalPages = (newWidth / badgeWidth).ceil();
22+
if (totalPages == 0) totalPages = 1;
23+
24+
currentPage = (animationIndex ~/ totalAnimationLength) % totalPages;
25+
startColOffset = currentPage * badgeWidth;
26+
} else {
27+
int displayWidth = newWidth > badgeWidth ? badgeWidth : newWidth;
28+
horizontalOffset = (badgeWidth - displayWidth) ~/ 2;
29+
}
30+
1331
int frame = animationIndex % totalAnimationLength;
1432
var firstHalf = frame < badgeWidth ~/ 2;
1533
var secondHalf = frame >= badgeWidth ~/ 2;
1634

35+
int leftCenterCol = badgeWidth ~/ 2 - 1;
36+
int rightCenterCol = badgeWidth ~/ 2;
37+
int maxDistance = leftCenterCol;
38+
int currentAnimationIndex = animationIndex % (maxDistance + 1);
39+
40+
int leftColPos = leftCenterCol - currentAnimationIndex;
41+
int rightColPos = rightCenterCol + currentAnimationIndex;
42+
43+
if (leftColPos < 0) leftColPos += badgeWidth;
44+
if (rightColPos >= badgeWidth) rightColPos -= badgeWidth;
45+
1746
for (int i = 0; i < badgeHeight; i++) {
1847
for (int j = 0; j < badgeWidth; j++) {
1948
bool lineShow = false;
2049
bool bitmapShowcenter = false;
2150
bool bitmapShowOut = false;
2251

2352
int sourceRow = i - verticalOffset;
24-
int sourceCol = j - horizontalOffset;
53+
int sourceCol =
54+
isTextTooLong ? (startColOffset + j) : (j - horizontalOffset);
2555

2656
bool isWithinNewGrid = sourceRow >= 0 &&
2757
sourceRow < newHeight &&
2858
sourceCol >= 0 &&
29-
sourceCol < displayWidth;
30-
31-
int leftCenterCol = badgeWidth ~/ 2 - 1;
32-
int rightCenterCol = badgeWidth ~/ 2;
33-
34-
int maxDistance = leftCenterCol;
35-
36-
int currentAnimationIndex = animationIndex % (maxDistance + 1);
37-
38-
int leftColPos = leftCenterCol - currentAnimationIndex;
39-
int rightColPos = rightCenterCol + currentAnimationIndex;
40-
41-
if (leftColPos < 0) leftColPos += badgeWidth;
42-
if (rightColPos >= badgeWidth) rightColPos -= badgeWidth;
59+
sourceCol < (isTextTooLong ? newWidth : newWidth);
4360

4461
if (j == leftColPos || j == rightColPos) {
4562
lineShow = true;

lib/badge_animation/ani_snowflake.dart

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,46 @@ class SnowFlakeAnimation extends BadgeAnimation {
55
void processAnimation(int badgeHeight, int badgeWidth, int animationIndex,
66
List<List<bool>> processGrid, List<List<bool>> canvas) {
77
int newWidth = processGrid[0].length;
8-
int totalAnimationLength = badgeHeight * 16;
9-
int frame = animationIndex % totalAnimationLength;
108

11-
int horizontalOffset = (badgeWidth - newWidth) ~/ 2;
9+
bool isTextTooLong = newWidth > badgeWidth;
1210

13-
bool phase1 = frame < badgeHeight * 4;
14-
bool phase2 = frame >= badgeHeight * 4 && frame < badgeHeight * 8;
11+
int totalPages = 1;
12+
int currentPage = 0;
13+
int startColOffset = 0;
14+
int horizontalOffset = 0;
15+
16+
if (isTextTooLong) {
17+
totalPages = (newWidth / badgeWidth).ceil();
18+
if (totalPages == 0) totalPages = 1;
19+
20+
int singlePageDuration = badgeHeight * 8;
21+
currentPage = (animationIndex ~/ singlePageDuration) % totalPages;
22+
startColOffset = currentPage * badgeWidth;
23+
} else {
24+
horizontalOffset = (badgeWidth - newWidth) ~/ 2;
25+
}
26+
27+
int singlePageDuration = badgeHeight * 8;
28+
int localFrame = isTextTooLong
29+
? (animationIndex % singlePageDuration)
30+
: (animationIndex % (badgeHeight * 16));
31+
32+
bool phase1 = localFrame < badgeHeight * 4;
33+
bool phase2 = localFrame >= badgeHeight * 4 && localFrame < badgeHeight * 8;
1534

1635
if (phase1) {
1736
for (int row = badgeHeight - 1; row >= 0; row--) {
18-
int fallPosition = frame - (badgeHeight - 1 - row) * 2;
37+
int fallPosition = localFrame - (badgeHeight - 1 - row) * 2;
1938
int stoppingPosition = row;
2039
fallPosition =
2140
fallPosition >= stoppingPosition ? stoppingPosition : fallPosition;
2241

2342
if (fallPosition >= 0 && fallPosition < badgeHeight) {
2443
for (int col = 0; col < badgeWidth; col++) {
25-
int sourceCol = col - horizontalOffset;
44+
int sourceCol = isTextTooLong
45+
? (startColOffset + col)
46+
: (col - horizontalOffset);
47+
2648
bool isWithinNewGrid = sourceCol >= 0 && sourceCol < newWidth;
2749
if (isWithinNewGrid) {
2850
canvas[fallPosition][col] = processGrid[row][sourceCol];
@@ -34,11 +56,13 @@ class SnowFlakeAnimation extends BadgeAnimation {
3456
for (int row = badgeHeight - 1; row >= 0; row--) {
3557
int fallOutStartFrame = (badgeHeight - 1 - row) * 2;
3658
int fallOutPosition =
37-
row + (frame - badgeHeight * 4 - fallOutStartFrame);
59+
row + (localFrame - badgeHeight * 4 - fallOutStartFrame);
3860

3961
if (fallOutPosition < row) {
4062
for (int col = 0; col < badgeWidth; col++) {
41-
int sourceCol = col - horizontalOffset;
63+
int sourceCol = isTextTooLong
64+
? (startColOffset + col)
65+
: (col - horizontalOffset);
4266
bool isWithinNewGrid = sourceCol >= 0 && sourceCol < newWidth;
4367
if (isWithinNewGrid) {
4468
canvas[row][col] = processGrid[row][sourceCol];
@@ -52,7 +76,9 @@ class SnowFlakeAnimation extends BadgeAnimation {
5276
}
5377

5478
for (int col = 0; col < badgeWidth; col++) {
55-
int sourceCol = col - horizontalOffset;
79+
int sourceCol = isTextTooLong
80+
? (startColOffset + col)
81+
: (col - horizontalOffset);
5682
bool isWithinNewGrid = sourceCol >= 0 && sourceCol < newWidth;
5783
if (isWithinNewGrid && fallOutPosition < badgeHeight) {
5884
canvas[fallOutPosition][col] = processGrid[row][sourceCol];

lib/badge_animation/ani_up.dart

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,55 @@ class UpAnimation extends BadgeAnimation {
66
List<List<bool>> processGrid, List<List<bool>> canvas) {
77
int newWidth = processGrid[0].length;
88
int newHeight = processGrid.length;
9-
int animationValue = animationIndex ~/ ((newWidth / badgeHeight));
9+
10+
bool isTextTooLong = newWidth > badgeWidth;
11+
12+
int totalPages = 1;
13+
int currentPage = 0;
14+
int startColOffset = 0;
15+
int horizontalOffset = 0;
16+
17+
int holdDuration = 15;
18+
19+
int singlePageDuration = (badgeHeight * 2) + holdDuration;
20+
21+
if (isTextTooLong) {
22+
totalPages = (newWidth / badgeWidth).ceil();
23+
if (totalPages == 0) totalPages = 1;
24+
currentPage = (animationIndex ~/ singlePageDuration) % totalPages;
25+
startColOffset = currentPage * badgeWidth;
26+
} else {
27+
horizontalOffset = (badgeWidth - newWidth) ~/ 2;
28+
}
29+
30+
int localFrame = animationIndex % singlePageDuration;
31+
int verticalScrollOffset;
32+
33+
if (localFrame < badgeHeight) {
34+
verticalScrollOffset = badgeHeight - localFrame;
35+
} else if (localFrame >= badgeHeight &&
36+
localFrame < (badgeHeight + holdDuration)) {
37+
verticalScrollOffset = 0;
38+
} else {
39+
verticalScrollOffset = -(localFrame - badgeHeight - holdDuration);
40+
}
1041

1142
for (int i = 0; i < badgeHeight; i++) {
43+
int sourceRow = i - verticalScrollOffset;
44+
1245
for (int j = 0; j < badgeWidth; j++) {
13-
if (j < badgeWidth && i < badgeHeight) {
14-
bool upCondition = (i >= 0 &&
15-
i < newHeight &&
16-
j >= 0 &&
17-
j < newWidth &&
18-
processGrid[(i + animationValue + newHeight) % newHeight][j]);
19-
20-
canvas[i][j] = upCondition;
46+
int sourceCol =
47+
isTextTooLong ? (startColOffset + j) : (j - horizontalOffset);
48+
49+
bool isWithinGrid = sourceRow >= 0 &&
50+
sourceRow < newHeight &&
51+
sourceCol >= 0 &&
52+
sourceCol < newWidth;
53+
54+
if (isWithinGrid) {
55+
canvas[i][j] = processGrid[sourceRow][sourceCol];
56+
} else {
57+
canvas[i][j] = false;
2158
}
2259
}
2360
}

0 commit comments

Comments
 (0)