drawTexture method
Draws the texture to a Canvas at a specified position
and with the
specified paint
.
Implementation
void drawTexture(Canvas canvas, Offset position, Paint paint) {
// Get drawing position
double x = position.dx;
double y = position.dy;
// Draw the texture
if (rotated) {
// Account for position
bool translate = (x != 0 || y != 0);
if (translate) {
canvas.translate(x, y);
}
// Calculate the rotated frame and spriteSourceSize
Size originalFrameSize = frame.size;
Rect rotatedFrame = frame.topLeft &
Size(originalFrameSize.height, originalFrameSize.width);
Offset rotatedSpriteSourcePoint = Offset(
-spriteSourceSize.top -
(spriteSourceSize.bottom - spriteSourceSize.top),
spriteSourceSize.left);
Rect rotatedSpriteSourceSize = rotatedSpriteSourcePoint &
Size(originalFrameSize.height, originalFrameSize.width);
// Draw the rotated sprite
canvas.rotate(-math.pi / 2.0);
canvas.drawImageRect(image, rotatedFrame, rotatedSpriteSourceSize, paint);
canvas.rotate(math.pi / 2.0);
// Translate back
if (translate) {
canvas.translate(-x, -y);
}
} else {
// Draw the sprite
Rect dstRect = Rect.fromLTWH(
x + spriteSourceSize.left,
y + spriteSourceSize.top,
spriteSourceSize.width,
spriteSourceSize.height);
canvas.drawImageRect(image, frame, dstRect, paint);
}
}