Skip to content

Commit

Permalink
fix: Make debugCoordinatesPrecision into a variable instead of a ge…
Browse files Browse the repository at this point in the history
…tter (#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.
  • Loading branch information
spydon authored Sep 8, 2023
1 parent b77c237 commit 9918c05
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/flame/lib/src/components/core/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
41 changes: 35 additions & 6 deletions packages/flame/test/components/position_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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', () {
Expand Down Expand Up @@ -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;
}

0 comments on commit 9918c05

Please sign in to comment.