calculateBoundsByAnimation method

Bounds calculateBoundsByAnimation()

Implementation

core.Bounds calculateBoundsByAnimation() {
  final core.Vector2 offset = core.Vector2();
  final core.Vector2 size = core.Vector2();
  if (animation == null) {
    return core.Bounds(offset, size);
  }

  final core.Animation? skeletonAnimation =
  skeleton.data.findAnimation(animation!);
  if (skeletonAnimation == null) {
    return core.Bounds(offset, size);
  }

  skeleton.state.clearTracks();
  skeleton
    ..setToSetupPose()
    ..updateWorldTransform();
  skeleton.state.setAnimationWith(0, skeletonAnimation, true);

  final double stepTime = skeletonAnimation.duration > 0.0
      ? skeletonAnimation.duration / countStepsForCalculateBounds
      : 0.0;
  double minX = double.maxFinite;
  double maxX = -double.maxFinite;
  double minY = double.maxFinite;
  double maxY = -double.maxFinite;

  for (int i = 0; i < countStepsForCalculateBounds; ++i) {
    skeleton.state
      ..update(stepTime)
      ..apply(skeleton);
    skeleton
      ..updateWorldTransform()
      ..getBounds(offset, size, <double>[]);

    minX = math.min(offset.x, minX);
    maxX = math.max(offset.x + size.x, maxX);
    minY = math.min(offset.y, minY);
    maxY = math.max(offset.y + size.y, maxY);
  }

  offset
    ..x = minX
    ..y = minY;
  size
    ..x = maxX - minX
    ..y = maxY - minY;

  return core.Bounds(offset, size);
}