From 61c83c64fb557d93f09635ea34287e0bc56555ec Mon Sep 17 00:00:00 2001 From: Renan araujo Date: Sat, 14 Dec 2024 22:24:32 +0000 Subject: [PATCH] extension --- packages/flame/lib/src/components/mixins/snapshot.dart | 9 ++++----- packages/flame/lib/src/extensions/canvas.dart | 4 ++-- packages/flame/lib/src/extensions/matrix4.dart | 4 ++++ packages/flame/lib/src/rendering/rotate3d_decorator.dart | 5 ++--- packages/flame/lib/src/rendering/shadow3d_decorator.dart | 5 ++--- .../flame/lib/src/rendering/transform2d_decorator.dart | 5 ++--- packages/flame/lib/src/sprite_batch.dart | 5 ++--- packages/flame_forge2d/lib/body_component.dart | 2 +- 8 files changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/flame/lib/src/components/mixins/snapshot.dart b/packages/flame/lib/src/components/mixins/snapshot.dart index bd20e64612..2f9abbe436 100644 --- a/packages/flame/lib/src/components/mixins/snapshot.dart +++ b/packages/flame/lib/src/components/mixins/snapshot.dart @@ -1,8 +1,7 @@ -import 'dart:typed_data'; import 'dart:ui'; import 'package:flame/components.dart'; -import 'package:vector_math/vector_math.dart'; +import 'package:flame/extensions_matrix4.dart'; /// A mixin that enables caching a component and all its children. If /// [renderSnapshot] is set to `true`, the component and its children will be @@ -49,7 +48,7 @@ mixin Snapshot on PositionComponent { } else { final recorder = PictureRecorder(); final canvas = Canvas(recorder); - canvas.transform(Float64List.sublistView(transform.storage)); + canvas.transform(transform.storage64); canvas.drawPicture(_picture!); final picture = recorder.endRecording(); return picture.toImageSync(width, height); @@ -65,7 +64,7 @@ mixin Snapshot on PositionComponent { final canvas = Canvas(recorder); final matrix = transformMatrix.clone(); matrix.invert(); - canvas.transform(Float64List.sublistView(matrix.storage)); + canvas.transform(matrix.storage64); super.renderTree(canvas); _picture = recorder.endRecording(); return _picture!; @@ -84,7 +83,7 @@ mixin Snapshot on PositionComponent { takeSnapshot(); } canvas.save(); - canvas.transform(Float64List.sublistView(transformMatrix.storage)); + canvas.transform(transformMatrix.storage64); canvas.drawPicture(_picture!); canvas.restore(); } else { diff --git a/packages/flame/lib/src/extensions/canvas.dart b/packages/flame/lib/src/extensions/canvas.dart index 8fc847fac2..fddae21361 100644 --- a/packages/flame/lib/src/extensions/canvas.dart +++ b/packages/flame/lib/src/extensions/canvas.dart @@ -1,6 +1,6 @@ -import 'dart:typed_data'; import 'dart:ui'; +import 'package:flame/extensions_matrix4.dart'; import 'package:flame/palette.dart'; import 'package:flame/src/extensions/vector2.dart'; import 'package:flame/src/game/transform2d.dart'; @@ -60,6 +60,6 @@ extension CanvasExtension on Canvas { /// Use the [Transform2D] object to [transform] the canvas. void transform2D(Transform2D transform2D) { - transform(Float64List.sublistView(transform2D.transformMatrix.storage)); + transform(transform2D.transformMatrix.storage64); } } diff --git a/packages/flame/lib/src/extensions/matrix4.dart b/packages/flame/lib/src/extensions/matrix4.dart index f875cce77e..b4fd95ce00 100644 --- a/packages/flame/lib/src/extensions/matrix4.dart +++ b/packages/flame/lib/src/extensions/matrix4.dart @@ -1,3 +1,5 @@ +import 'dart:typed_data'; + import 'package:vector_math/vector_math.dart'; export 'package:vector_math/vector_math.dart' hide Colors; @@ -78,6 +80,8 @@ extension Matrix4Extension on Matrix4 { return transform2(out); } + Float64List get storage64 => Float64List.sublistView(storage); + /// Create a scaled matrix. /// /// Scale by a [Vector3], [Vector4], or x,y,z diff --git a/packages/flame/lib/src/rendering/rotate3d_decorator.dart b/packages/flame/lib/src/rendering/rotate3d_decorator.dart index 0b6c6195a1..a5c81f0de9 100644 --- a/packages/flame/lib/src/rendering/rotate3d_decorator.dart +++ b/packages/flame/lib/src/rendering/rotate3d_decorator.dart @@ -1,9 +1,8 @@ -import 'dart:typed_data'; import 'dart:ui'; +import 'package:flame/extensions_matrix4.dart'; import 'package:flame/geometry.dart'; import 'package:flame/src/rendering/decorator.dart'; -import 'package:vector_math/vector_math.dart'; /// [Rotate3DDecorator] treats the underlying component as if it was a flat /// sheet of paper, and applies a 3D rotation to it. @@ -62,7 +61,7 @@ class Rotate3DDecorator extends Decorator { ..rotateY(angleY) ..rotateZ(angleZ) ..translate(-center.x, -center.y); - canvas.transform(Float64List.sublistView(matrix.storage)); + canvas.transform(matrix.storage64); draw(canvas); canvas.restore(); } diff --git a/packages/flame/lib/src/rendering/shadow3d_decorator.dart b/packages/flame/lib/src/rendering/shadow3d_decorator.dart index cfe08f03b1..7d01ede9cb 100644 --- a/packages/flame/lib/src/rendering/shadow3d_decorator.dart +++ b/packages/flame/lib/src/rendering/shadow3d_decorator.dart @@ -1,9 +1,8 @@ -import 'dart:typed_data'; import 'dart:ui'; +import 'package:flame/extensions_matrix4.dart'; import 'package:flame/src/palette.dart'; import 'package:flame/src/rendering/decorator.dart'; -import 'package:vector_math/vector_math.dart' show Matrix4, Vector2; /// [Shadow3DDecorator] casts a realistic-looking shadow from the component /// onto the ground. @@ -157,7 +156,7 @@ class Shadow3DDecorator extends Decorator { canvas.saveLayer(null, _paint!); canvas.translate(base.x + _shift, base.y); - canvas.transform(Float64List.sublistView(_transformMatrix!.storage)); + canvas.transform(_transformMatrix!.storage64); draw(canvas); canvas.restore(); draw(canvas); diff --git a/packages/flame/lib/src/rendering/transform2d_decorator.dart b/packages/flame/lib/src/rendering/transform2d_decorator.dart index d4d979812c..9c03cd1853 100644 --- a/packages/flame/lib/src/rendering/transform2d_decorator.dart +++ b/packages/flame/lib/src/rendering/transform2d_decorator.dart @@ -1,6 +1,6 @@ -import 'dart:typed_data'; import 'dart:ui'; +import 'package:flame/extensions_matrix4.dart'; import 'package:flame/src/components/position_component.dart'; import 'package:flame/src/game/transform2d.dart'; import 'package:flame/src/rendering/decorator.dart'; @@ -18,8 +18,7 @@ class Transform2DDecorator extends Decorator { @override void apply(void Function(Canvas) draw, Canvas canvas) { canvas.save(); - canvas.transform( - Float64List.sublistView(transform2d.transformMatrix.storage)); + canvas.transform(transform2d.transformMatrix.storage64); draw(canvas); canvas.restore(); } diff --git a/packages/flame/lib/src/sprite_batch.dart b/packages/flame/lib/src/sprite_batch.dart index 21c8ba23f2..23eacc7a51 100644 --- a/packages/flame/lib/src/sprite_batch.dart +++ b/packages/flame/lib/src/sprite_batch.dart @@ -1,14 +1,13 @@ import 'dart:collection'; import 'dart:math' show pi; -import 'dart:typed_data'; import 'dart:ui'; import 'package:flame/cache.dart'; import 'package:flame/extensions.dart'; +import 'package:flame/extensions_matrix4.dart'; import 'package:flame/flame.dart'; import 'package:flame/game.dart'; import 'package:meta/meta.dart'; -import 'package:vector_math/vector_math.dart'; extension SpriteBatchExtension on Game { /// Utility method to load and cache the image for a [SpriteBatch] based on @@ -433,7 +432,7 @@ class SpriteBatch { canvas ..save() - ..transform(Float64List.sublistView(batchItem.matrix.storage)) + ..transform(batchItem.matrix.storage64) ..drawRect(batchItem.destination, batchItem.paint) ..drawImageRect( atlas, diff --git a/packages/flame_forge2d/lib/body_component.dart b/packages/flame_forge2d/lib/body_component.dart index 85ed8f17e9..6796ac13e0 100644 --- a/packages/flame_forge2d/lib/body_component.dart +++ b/packages/flame_forge2d/lib/body_component.dart @@ -102,7 +102,7 @@ class BodyComponent extends Component _lastAngle = angle; } canvas.save(); - canvas.transform(Float64List.sublistView(matrix.storage)); + canvas.transform(matrix.storage64); super.renderTree(canvas); canvas.restore(); }