seperateSprites method

void seperateSprites()

Implementation

void seperateSprites() async{
  if(objectTappedOn.length == 1){
    int loc = objectTappedOn[0].animation;
    origionalPos = sprites[loc].position;
    Image img = sprites[loc].sprite;
    ByteData? byteData = await img.toByteData();
    Uint8List pixels = byteData!.buffer.asUint8List();
    //Color bgColor = _getBackGroundColor(pixels);
    List<Rectangle> startingRect = [];

    bool start = false;
    int iStart = 0;

    for(int i = 0; i < img.height;i++){
      bool allZeros = true;
      for (int j = 0; j < img.width;j++){
        Color color = Color.fromARGB(pixels[j*4+i*img.width*4], pixels[(j*4+i*img.width*4)+1], pixels[(j*4+i*img.width*4)+2], pixels[(j*4+i*img.width*4)+3]);
        if(color != const Color.fromARGB(0,0,0,0)){
          allZeros = false;
          if(!start){
            start = true;
            iStart = i;
          }
          break;
        }
      }

      if((allZeros && start) || (i == img.height-1 && start)){
        start = false;
        startingRect.add(Rectangle(x: 0,y: iStart,height: (i-iStart),width: img.width));
      }
    }

    start = false;

    for(int k = 0; k < startingRect.length; k++){
      int height = startingRect[k].height;
      int width = startingRect[k].width;
      int x = 0;

      for(int i = 0; i < width;i++){
        bool allZeros = true;

        for (int j = startingRect[k].y; j < startingRect[k].y+height; j++){
          Color color = Color.fromARGB(pixels[j*width*4+i*4], pixels[(j*width*4+i*4)+1], pixels[(j*width*4+i*4)+2], pixels[(j*width*4+i*4)+3]);

          if(color != const Color.fromARGB(0,0,0,0)){
            allZeros = false;
            if(!start){
              start = true;
              x = i;
            }
            break;
          }
        }

        if((allZeros && start) || (i == width-1 && start)){
          start = false;
          Rect rects = Rect.fromLTWH(x.toDouble(), startingRect[k].y.toDouble(), i-x.toDouble(),height.toDouble());
          x = i;
          Offset off = Converter.toOffset(camera.position, sprites[loc].position);
          Offset offset = Offset(rects.left,rects.top)+off;

          await _generateImage(Size(rects.width,rects.height), [loc], -offset).then((value){
            Vector3 newPosition = Converter.toVector3(camera.position, offset);
            sprites.add(
              SpriteImage(
                sprite: value,
                name: i.toString(),
                position: newPosition,
                section: rects
              )
            );
          });
        }
      }
    }

    removeSprite([loc]);
    updateTapLocation(const Offset(0,0));
  }
  update();
}