/// Created by Haoyi on 2022/7/6

part of '../visual_feast_engine.dart';

abstract class VisualFeastSprite {
  factory VisualFeastSprite.fromImage(ui.Image image) = _ImageVisualFeastSprite._;

  factory VisualFeastSprite.invalid() = _InvalidVisualFeastSprite._;

  void renderRect(Canvas canvas, Rect dst, {Paint? overridePaint});
}

class _ImageVisualFeastSprite implements VisualFeastSprite {
  final Rect src;
  final ui.Image image;
  static final Paint defaultCommonPaint = Paint()..filterQuality = FilterQuality.high;

  _ImageVisualFeastSprite._(this.image)
      : src = Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble());

  @override
  void renderRect(Canvas canvas, Rect dst, {Paint? overridePaint}) {
    canvas.drawImageRect(image, src, dst, overridePaint ?? defaultCommonPaint);
  }
}

class _InvalidVisualFeastSprite implements VisualFeastSprite {
  _InvalidVisualFeastSprite._();

  @override
  void renderRect(Canvas canvas, Rect dst, {Paint? overridePaint}) {
    Log.e("invalid sprite!");
  }
}
