From 685e1d9529df90f203e7827950ed5d9261b2ce42 Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Sun, 26 Nov 2023 12:39:29 -0500 Subject: [PATCH] feat: Make Viewfinder and Viewport comply with CoordinateTransform interface (#2872) Make `Viewfinder` and `Viewport` comply with `CoordinateTransform` interface; they already perform the coordinate transform operation but didn't technically implement then interface. --- packages/flame/lib/src/camera/viewfinder.dart | 21 +++++++++++++++---- packages/flame/lib/src/camera/viewport.dart | 20 ++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/flame/lib/src/camera/viewfinder.dart b/packages/flame/lib/src/camera/viewfinder.dart index cf7dc1db891..33563c73a01 100644 --- a/packages/flame/lib/src/camera/viewfinder.dart +++ b/packages/flame/lib/src/camera/viewfinder.dart @@ -1,9 +1,7 @@ import 'dart:math'; +import 'package:flame/components.dart'; import 'package:flame/extensions.dart'; -import 'package:flame/src/anchor.dart'; -import 'package:flame/src/camera/camera_component.dart'; -import 'package:flame/src/components/core/component.dart'; import 'package:flame/src/effects/provider_interfaces.dart'; import 'package:flame/src/game/transform2d.dart'; import 'package:meta/meta.dart'; @@ -18,7 +16,12 @@ import 'package:meta/meta.dart'; /// If you add children to the [Viewfinder] they will appear like HUDs i.e. /// statically in front of the world. class Viewfinder extends Component - implements AnchorProvider, AngleProvider, PositionProvider, ScaleProvider { + implements + AnchorProvider, + AngleProvider, + PositionProvider, + ScaleProvider, + CoordinateTransform { /// Transform matrix used by the viewfinder. final Transform2D transform = Transform2D(); @@ -180,6 +183,16 @@ class Viewfinder extends Component } } + @override + Vector2 parentToLocal(Vector2 point) { + return globalToLocal(point); + } + + @override + Vector2 localToParent(Vector2 point) { + return localToGlobal(point); + } + @override void onGameResize(Vector2 size) { _updateZoom(); diff --git a/packages/flame/lib/src/camera/viewport.dart b/packages/flame/lib/src/camera/viewport.dart index da1fb48feee..96d89a23be0 100644 --- a/packages/flame/lib/src/camera/viewport.dart +++ b/packages/flame/lib/src/camera/viewport.dart @@ -1,9 +1,7 @@ +import 'package:flame/components.dart'; import 'package:flame/extensions.dart'; import 'package:flame/game.dart'; -import 'package:flame/src/anchor.dart'; -import 'package:flame/src/camera/camera_component.dart'; import 'package:flame/src/camera/viewports/fixed_resolution_viewport.dart'; -import 'package:flame/src/components/core/component.dart'; import 'package:flame/src/effects/provider_interfaces.dart'; import 'package:meta/meta.dart'; @@ -21,7 +19,11 @@ import 'package:meta/meta.dart'; /// A viewport establishes its own local coordinate system, with the origin at /// the top left corner of the viewport's bounding box. abstract class Viewport extends Component - implements AnchorProvider, PositionProvider, SizeProvider { + implements + AnchorProvider, + PositionProvider, + SizeProvider, + CoordinateTransform { Viewport({super.children}); final Vector2 _size = Vector2.zero(); @@ -148,6 +150,16 @@ abstract class Viewport extends Component return (output?..setValues(x, y)) ?? Vector2(x, y); } + @override + Vector2 parentToLocal(Vector2 point) { + return globalToLocal(point); + } + + @override + Vector2 localToParent(Vector2 point) { + return localToGlobal(point); + } + void transformCanvas(Canvas canvas) { canvas.transform2D(transform); }