diff --git a/CHANGELOG.md b/CHANGELOG.md index afd42217c..07afcdb70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Add assertion to make sure Draggables are safe to add - Add utility methods to the Anchor class to make it more "enum like" - Enable user-defined anchors + - Added `toImage` method for the `Sprite` class ## 1.0.0-rc6 - Use `Offset` type directly in `JoystickAction.update` calculations diff --git a/lib/image_composition.dart b/lib/image_composition.dart index ec5abf756..f46a1ddc9 100644 --- a/lib/image_composition.dart +++ b/lib/image_composition.dart @@ -39,6 +39,8 @@ class _Composed { } /// The [ImageComposition] allows for composing multiple images onto a single image. +/// +/// **Note:** Composing images is a heavy async operation and should not be called on each [Game.render]. class ImageComposition { /// The values that will be used to compose the image final List<_Composed> _composes = []; diff --git a/lib/src/extensions/vector2.dart b/lib/src/extensions/vector2.dart index 7c759a6c7..af16e41cb 100644 --- a/lib/src/extensions/vector2.dart +++ b/lib/src/extensions/vector2.dart @@ -15,6 +15,13 @@ extension Vector2Extension on Vector2 { /// Creates a [Point] from the [Vector2] Point toPoint() => Point(x, y); + /// A rectangle constructor operator. + /// + /// Combines two [Vector2]s to form a Rect whose top-left coordinate is the + /// point given by adding this vector, the left-hand-side operand, + /// to the origin, and whose size is the right-hand-side operand. + Rect operator &(Vector2 size) => toPositionedRect(size); + /// Creates a [Rect] starting from [x, y] and having the size of the /// argument [Vector2] Rect toPositionedRect(Vector2 size) => Rect.fromLTWH(x, y, size.x, size.y); diff --git a/lib/src/sprite.dart b/lib/src/sprite.dart index 620d28b7b..fe6ac461c 100644 --- a/lib/src/sprite.dart +++ b/lib/src/sprite.dart @@ -1,5 +1,6 @@ import 'dart:ui'; +import '../image_composition.dart'; import 'anchor.dart'; import 'extensions/offset.dart'; import 'extensions/vector2.dart'; @@ -76,4 +77,13 @@ class Sprite { canvas.drawImageRect(image, src, drawRect, drawPaint); } + + /// Return a new Image based on the [src] of the Sprite. + /// + /// **Note:** This is a heavy async operation and should not be called on each [Game.render]. + Future toImage() async { + final composition = ImageComposition() + ..add(image, Vector2.zero(), source: src); + return composition.compose(); + } }