|
| 1 | +import 'package:badgemagic/badge_animation/animation_abstract.dart'; |
| 2 | +import 'dart:math'; |
| 3 | + |
| 4 | +class BrokenHeartsAnimation extends BadgeAnimation { |
| 5 | + /// **Now a 9×9 heart** (instead of 7×7), so it fills more of the 11×44 badge. |
| 6 | + static const List<List<int>> heartShape = [ |
| 7 | + [0, 0, 1, 1, 0, 1, 1, 0, 0], |
| 8 | + [0, 1, 1, 1, 1, 1, 1, 1, 0], |
| 9 | + [1, 1, 1, 1, 1, 1, 1, 1, 1], |
| 10 | + [1, 1, 1, 1, 1, 1, 1, 1, 1], |
| 11 | + [0, 1, 1, 1, 1, 1, 1, 1, 0], |
| 12 | + [0, 0, 1, 1, 1, 1, 1, 0, 0], |
| 13 | + [0, 0, 0, 1, 1, 1, 0, 0, 0], |
| 14 | + [0, 0, 0, 0, 1, 0, 0, 0, 0], |
| 15 | + [ |
| 16 | + 0, |
| 17 | + 0, |
| 18 | + 0, |
| 19 | + 0, |
| 20 | + 0, |
| 21 | + 0, |
| 22 | + 0, |
| 23 | + 0, |
| 24 | + 0 |
| 25 | + ], // tip row (optional—it ensures the very bottom pixel sits one row above the badge bottom) |
| 26 | + ]; |
| 27 | + |
| 28 | + final List<List<Point<int>>> _clustersLeft = []; |
| 29 | + final List<List<Point<int>>> _clustersRight = []; |
| 30 | + bool _initialized = false; |
| 31 | + final Random _rng = Random(12345); |
| 32 | + |
| 33 | + void _initializeClusters(int badgeH, int badgeW) { |
| 34 | + if (_initialized) return; |
| 35 | + _initialized = true; |
| 36 | + |
| 37 | + final int heartW = heartShape[0].length; |
| 38 | + final int heartH = heartShape.length; |
| 39 | + final int leftCx = badgeW ~/ 4 - heartW ~/ 2; |
| 40 | + final int topY = badgeH ~/ 2 - heartH ~/ 2; |
| 41 | + |
| 42 | + // collect all solid pixels of left heart |
| 43 | + final pixels = <Point<int>>[]; |
| 44 | + for (int y = 0; y < heartH; y++) { |
| 45 | + for (int x = 0; x < heartW; x++) { |
| 46 | + if (heartShape[y][x] == 1) { |
| 47 | + pixels.add(Point(leftCx + x, topY + y)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // carve into random clusters of size 1–4 |
| 53 | + while (pixels.isNotEmpty) { |
| 54 | + int size = _rng.nextInt(min(4, pixels.length)) + 1; |
| 55 | + final clusterL = <Point<int>>[]; |
| 56 | + for (int i = 0; i < size; i++) { |
| 57 | + clusterL.add(pixels.removeAt(_rng.nextInt(pixels.length))); |
| 58 | + } |
| 59 | + _clustersLeft.add(clusterL); |
| 60 | + _clustersRight |
| 61 | + .add(clusterL.map((pt) => Point(pt.x + badgeW ~/ 2, pt.y)).toList()); |
| 62 | + } |
| 63 | + |
| 64 | + // sort so bottom-most clusters fall first |
| 65 | + final paired = List.generate( |
| 66 | + _clustersLeft.length, |
| 67 | + (i) => MapEntry(_clustersLeft[i], _clustersRight[i]), |
| 68 | + ); |
| 69 | + paired.sort((a, b) { |
| 70 | + double ya = a.key.map((p) => p.y).reduce((u, v) => u + v) / a.key.length; |
| 71 | + double yb = b.key.map((p) => p.y).reduce((u, v) => u + v) / b.key.length; |
| 72 | + return yb.compareTo(ya); // descending: larger Y first |
| 73 | + }); |
| 74 | + _clustersLeft |
| 75 | + ..clear() |
| 76 | + ..addAll(paired.map((e) => e.key)); |
| 77 | + _clustersRight |
| 78 | + ..clear() |
| 79 | + ..addAll(paired.map((e) => e.value)); |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + void processAnimation( |
| 84 | + int badgeHeight, |
| 85 | + int badgeWidth, |
| 86 | + int animationIndex, |
| 87 | + List<List<bool>> processGrid, |
| 88 | + List<List<bool>> canvas, |
| 89 | + ) { |
| 90 | + _initializeClusters(badgeHeight, badgeWidth); |
| 91 | + |
| 92 | + // clear |
| 93 | + for (int y = 0; y < badgeHeight; y++) { |
| 94 | + for (int x = 0; x < badgeWidth; x++) { |
| 95 | + canvas[y][x] = false; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + final int N = _clustersLeft.length; |
| 100 | + final int cycle = N + badgeHeight; |
| 101 | + final int frame = animationIndex % cycle; |
| 102 | + |
| 103 | + // draw each cluster either “attached” or “falling” |
| 104 | + for (int i = 0; i < N; i++) { |
| 105 | + final bool isFalling = frame >= i; |
| 106 | + final int dy = frame - i; |
| 107 | + for (var pt in _clustersLeft[i]) { |
| 108 | + final int y = isFalling ? pt.y + dy : pt.y; |
| 109 | + if (y >= 0 && y < badgeHeight) canvas[y][pt.x] = true; |
| 110 | + } |
| 111 | + for (var pt in _clustersRight[i]) { |
| 112 | + final int y = isFalling ? pt.y + dy : pt.y; |
| 113 | + if (y >= 0 && y < badgeHeight) canvas[y][pt.x] = true; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments