fromGdxPacker static method

Future<SpriteBatch> fromGdxPacker(
  1. String path, {
  2. bool flippable = false,
  3. bool maskable = false,
})

Implementation

static Future<SpriteBatch> fromGdxPacker(String path,
    {bool flippable = false, bool maskable = false}) async {
  TexArea getTexArea(String boundsStr, int pageIndex) {
    final split = boundsStr.split(',');
    return TexArea(
      rect: Rect.fromLTWH(
        double.parse(split[0]),
        double.parse(split[1]),
        double.parse(split[2]),
        double.parse(split[3]),
      ),
      pageIndex: pageIndex,
    );
  }

  final atlas = await rootBundle.loadString(path);

  final pages = <Image>[];
  final textures = <String, TexArea>{};
  final framesMap = <String, SplayTreeMap<int, TexArea>>{};

  final lines = atlas.split(RegExp(r'\r?\n')).map((e) => e.trim()).toList();

  int i = 0;
  while (i < lines.length) {
    final line = lines[i];

    if (_isImageFile(line)) {
      final pageIndex = pages.length;

      final pathSplit = path.split("/");
      pathSplit.removeLast();
      pathSplit.add(line);
      final image = await ImageUtils.loadImageFromAssets(pathSplit.join("/"));
      assert(image != null, "Batch image could not be loaded '${pathSplit.join("/")}' !");
      final newImage = await transformSheetImage(
        image!,
        flippable: flippable,
        maskable: maskable,
        disposeOriginal: true,
      );
      pages.add(newImage);

      i++;

      while (i < lines.length && lines[i].contains(":")) {
        i++;
      }

      while (i < lines.length && !_isImageFile(lines[i]) && lines[i] != "") {
        final name = lines[i];
        i++;

        if (name.contains(":")) continue;

        final tempValues = <String, String>{};
        while (i < lines.length && lines[i].contains(":")) {
          final splitted = lines[i].split(":");
          tempValues[splitted[0]] = splitted[1];
          i++;
        }

        if (tempValues.containsKey("index")) {
          if (!framesMap.containsKey(name)) framesMap[name] = SplayTreeMap<int, TexArea>();
          final idx = int.parse(tempValues["index"]!);
          framesMap[name]![idx] = getTexArea(tempValues["bounds"]!, pageIndex);
        } else {
          textures[name] = getTexArea(tempValues["bounds"]!, pageIndex);
        }
      }
    } else {
      i++;
    }
  }

  assert(pages.isNotEmpty, "No atlas pages found!");

  return SpriteBatch._(
    pages: pages,
    textures: textures,
    frames: framesMap.map((key, value) => MapEntry(key, value.values.toList())),
  );
}