LCOV - code coverage report
Current view: top level - src - mini_sprite.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 35 35 100.0 %
Date: 2022-08-13 17:52:58 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:equatable/equatable.dart';
       2             : 
       3             : /// {@template mini_sprite}
       4             : /// A class used to manipulate a matrix of pixels.
       5             : /// first dimension of the [pixels] array is the y coordinate,
       6             : /// second is the x coordinate.
       7             : /// {@endtemplate}
       8             : class MiniSprite extends Equatable {
       9             :   /// {@macro mini_sprite}
      10           3 :   const MiniSprite(this.pixels);
      11             : 
      12             :   /// {@macro mini_sprite}
      13             :   ///
      14             :   /// Creates an empty sprite with the given width and height.
      15           1 :   MiniSprite.empty(int width, int height)
      16             :       : pixels =
      17           4 :             List.generate(height, (_) => List.generate(width, (_) => false));
      18             : 
      19             :   /// {@macro mini_sprite}
      20             :   ///
      21             :   /// Returns a [MiniSprite] from the serialized data.
      22           2 :   factory MiniSprite.fromDataString(String value) {
      23           2 :     final blocks = value.split(';');
      24             : 
      25           4 :     final size = blocks.removeAt(0).split(',');
      26           4 :     final height = int.parse(size[0]);
      27           4 :     final width = int.parse(size[1]);
      28             : 
      29           4 :     final flatten = blocks.map((rawBlock) {
      30           2 :       final blockSplit = rawBlock.split(',');
      31             : 
      32           4 :       final count = int.parse(blockSplit[0]);
      33           6 :       final value = int.parse(blockSplit[1]) == 1;
      34             : 
      35           2 :       return List.filled(count, value);
      36           6 :     }).fold<List<bool>>(List<bool>.empty(), (value, list) {
      37           2 :       return [
      38             :         ...value,
      39           2 :         ...list,
      40             :       ];
      41             :     });
      42             : 
      43           2 :     final pixels = List.generate(
      44             :       height,
      45           4 :       (_) => List.generate(
      46             :         width,
      47           2 :         (_) {
      48           2 :           return flatten.removeAt(0);
      49             :         },
      50             :       ),
      51             :     );
      52             : 
      53           2 :     return MiniSprite(pixels);
      54             :   }
      55             : 
      56             :   /// The matrix of pixels.
      57             :   final List<List<bool>> pixels;
      58             : 
      59             :   /// Returns this as a data string.
      60           2 :   String toDataString() {
      61          12 :     final dimensions = '${pixels.length},${pixels[0].length}';
      62             : 
      63             :     var counter = 0;
      64             :     bool? last;
      65             : 
      66           2 :     final blocks = <String>[];
      67             : 
      68           8 :     for (var y = 0; y < pixels.length; y++) {
      69          10 :       for (var x = 0; x < pixels[y].length; x++) {
      70             :         if (last == null) {
      71           6 :           last = pixels[y][x];
      72             :           counter = 1;
      73             :         } else {
      74           8 :           if (last == pixels[y][x]) {
      75           2 :             counter++;
      76             :           } else {
      77           2 :             blocks.add('$counter,${last ? 1 : 0}');
      78           3 :             last = pixels[y][x];
      79             :             counter = 1;
      80             :           }
      81             :         }
      82             :       }
      83             :     }
      84             :     if (last != null) {
      85           4 :       blocks.add('$counter,${last ? 1 : 0}');
      86             :     }
      87             : 
      88           4 :     return '$dimensions;${blocks.join(';')}';
      89             :   }
      90             : 
      91           2 :   @override
      92           4 :   List<Object> get props => [pixels];
      93             : }

Generated by: LCOV version 1.15