addTransform method

void addTransform({
  1. required Rect source,
  2. RSTransform? transform,
  3. bool flip = false,
  4. Color? color,
})

Add a new batch item using a RSTransform.

The source parameter is the source location on the atlas.

You can position, rotate, scale and flip it on the canvas using the transform and flip parameters.

The color parameter allows you to render a color behind the batch item, as a background color.

The add method may be a simpler way to add a batch item to the batch. However, if there is a way to factor out the computations of the sine and cosine of the rotation so that they can be reused over multiple calls to this constructor, it may be more efficient to directly use this method instead.

Implementation

void addTransform({
  required Rect source,
  RSTransform? transform,
  bool flip = false,
  Color? color,
}) {
  final batchItem = BatchItem(
    source: source,
    transform: transform ??= defaultTransform ?? RSTransform(1, 0, 0, 0),
    flip: flip,
    color: color ?? defaultColor,
  );

  if (flip && useAtlas && _flippedAtlasStatus.isNone) {
    _makeFlippedAtlas();
  }

  _batchItems.add(batchItem);
  _sources.add(
    flip
        ? Rect.fromLTWH(
            // The atlas is twice as wide when the flipped atlas is generated.
            (atlas.width * (_flippedAtlasStatus.isGenerated ? 1 : 2)) -
                source.right,
            source.top,
            source.width,
            source.height,
          )
        : batchItem.source,
  );
  _transforms.add(batchItem.transform);
  if (color != null) {
    _colors.add(color);
  }
}