From 9918c0515ae88c2f1bfb7423a2993c983dec16c2 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Fri, 8 Sep 2023 15:49:09 +0200 Subject: [PATCH] fix: Make `debugCoordinatesPrecision` into a variable instead of a getter (#2713) Since you might want to set `debugCoordinatesPrecision` without having to override it, it is better to have it as a variable than a getter. --- .../lib/src/components/core/component.dart | 2 +- .../components/position_component_test.dart | 41 ++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/flame/lib/src/components/core/component.dart b/packages/flame/lib/src/components/core/component.dart index 032439c748c..92c4850cf2b 100644 --- a/packages/flame/lib/src/components/core/component.dart +++ b/packages/flame/lib/src/components/core/component.dart @@ -930,7 +930,7 @@ class Component { /// How many decimal digits to print when displaying coordinates in the /// debug mode. Setting this to null will suppress all coordinates from /// the output. - int? get debugCoordinatesPrecision => 0; + int? debugCoordinatesPrecision = 0; /// A key that can be used to identify this component in the tree. /// diff --git a/packages/flame/test/components/position_component_test.dart b/packages/flame/test/components/position_component_test.dart index 1f2ff896503..a9324b7b7d5 100644 --- a/packages/flame/test/components/position_component_test.dart +++ b/packages/flame/test/components/position_component_test.dart @@ -919,7 +919,7 @@ void main() { ..position = Vector2(23, 17) ..size = Vector2.all(10) ..anchor = Anchor.center - ..precision = null; + ..debugCoordinatesPrecision = null; final canvas = MockCanvas(); component.renderTree(canvas); expect( @@ -932,6 +932,40 @@ void main() { ..translate(0, 0), // canvas.restore ); }); + + test('render without coordinates and then render with coordinates', () { + final component = _MyDebugComponent() + ..position = Vector2(23, 17) + ..size = Vector2.all(10) + ..anchor = Anchor.center + ..debugCoordinatesPrecision = null; + final withoutCoordinatesCanvas = MockCanvas(); + component.renderTree(withoutCoordinatesCanvas); + expect( + withoutCoordinatesCanvas, + MockCanvas() + ..translate(18, 12) + ..drawRect(const Rect.fromLTWH(0, 0, 10, 10)) + ..drawLine(const Offset(5, 3), const Offset(5, 7)) + ..drawLine(const Offset(3, 5), const Offset(7, 5)) + ..translate(0, 0), // canvas.restore + ); + + component.debugCoordinatesPrecision = 0; + final withCoordinatesCanvas = MockCanvas(); + component.renderTree(withCoordinatesCanvas); + expect( + withCoordinatesCanvas, + MockCanvas() + ..translate(18, 12) + ..drawRect(const Rect.fromLTWH(0, 0, 10, 10)) + ..drawLine(const Offset(5, 3), const Offset(5, 7)) + ..drawLine(const Offset(3, 5), const Offset(7, 5)) + ..drawParagraph(null, const Offset(-30, -15)) + ..drawParagraph(null, const Offset(-20, 10)) + ..translate(0, 0), // canvas.restore + ); + }); }); group('Bounding rectangle', () { @@ -1003,11 +1037,6 @@ void main() { class _MyHitboxComponent extends PositionComponent with GestureHitboxes {} class _MyDebugComponent extends PositionComponent { - int? precision = 0; - @override bool get debugMode => true; - - @override - int? get debugCoordinatesPrecision => precision; }