Skip to content

Commit

Permalink
fix: debugMode should be inherited from parent when mounted (#1469)
Browse files Browse the repository at this point in the history
  • Loading branch information
spydon committed Mar 19, 2022
1 parent 6434829 commit e894d20
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ class MultipleShapesExample extends FlameGame
var totalAdded = 1;
while (totalAdded < 100) {
lastToAdd = nextRandomCollidable(lastToAdd, screenHitbox);
final lastBottomRight =
lastToAdd.toAbsoluteRect().bottomRight.toVector2();
if (lastBottomRight.x < size.x && lastBottomRight.y < size.y) {
final lastBottomRight = lastToAdd.toAbsoluteRect().bottomRight;
if (lastBottomRight.dx < size.x && lastBottomRight.dy < size.y) {
add(lastToAdd);
totalAdded++;
} else {
Expand Down Expand Up @@ -143,7 +142,7 @@ abstract class MyCollidable extends PositionComponent
@override
void render(Canvas canvas) {
if (isDragged) {
final localCenter = (scaledSize / 2).toOffset();
final localCenter = scaledSize.toOffset() / 2;
canvas.drawCircle(localCenter, 5, _dragIndicatorPaint);
}
}
Expand Down
10 changes: 1 addition & 9 deletions packages/flame/lib/src/collisions/hitboxes/shape_hitbox.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:ui';

import 'package:meta/meta.dart';

import '../../../collisions.dart';
Expand All @@ -14,9 +12,6 @@ mixin ShapeHitbox on ShapeComponent implements Hitbox<ShapeHitbox> {
@override
CollisionType collisionType = CollisionType.active;

@override
Paint get paint => debugPaint;

/// Whether the hitbox is allowed to collide with another hitbox that is
/// added to the same parent.
bool allowSiblingCollision = false;
Expand Down Expand Up @@ -48,10 +43,7 @@ mixin ShapeHitbox on ShapeComponent implements Hitbox<ShapeHitbox> {
final Matrix3 _rotationMatrix = Matrix3.zero();

@override
bool get renderShape => _renderShape || debugMode;
@override
set renderShape(bool shouldRender) => _renderShape = shouldRender;
bool _renderShape = false;
bool renderShape = false;

@protected
late PositionComponent hitboxParent;
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/components/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ class Component {
_state == LifecycleState.removed,
);
_parent = parent;
debugMode |= parent.debugMode;
parent.lifecycle._children.add(this);

if (!isLoaded) {
Expand Down Expand Up @@ -455,6 +454,7 @@ class Component {
}
_mountCompleter?.complete();
_mountCompleter = null;
debugMode |= _parent!.debugMode;
onMount();
_state = LifecycleState.mounted;
if (!existingChild) {
Expand Down
8 changes: 6 additions & 2 deletions packages/flame/lib/src/geometry/circle_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ class CircleComponent extends ShapeComponent {
return min(_scaledSize.x, _scaledSize.y) / 2;
}

/// This render method doesn't rotate the canvas according to angle since a
/// circle will look the same rotated as not rotated.
@override
void render(Canvas canvas) {
if (renderShape) {
canvas.drawCircle((size / 2).toOffset(), radius, paint);
}
}

@override
void renderDebugMode(Canvas canvas) {
super.renderDebugMode(canvas);
canvas.drawCircle((size / 2).toOffset(), radius, debugPaint);
}

/// Checks whether the represented circle contains the [point].
@override
bool containsPoint(Vector2 point) {
Expand Down
6 changes: 6 additions & 0 deletions packages/flame/lib/src/geometry/polygon_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ class PolygonComponent extends ShapeComponent {
}
}

@override
void renderDebugMode(Canvas canvas) {
super.renderDebugMode(canvas);
canvas.drawPath(_path, debugPaint);
}

/// Checks whether the polygon contains the [point].
/// Note: The polygon needs to be convex for this to work.
@override
Expand Down
32 changes: 25 additions & 7 deletions packages/flame/test/components/composed_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class _MyTap extends PositionComponent with Tappable {
}
}

class _MyAsyncChild extends _MyTap {
class _MyAsyncChild extends PositionComponent {
@override
Future<void> onLoad() async {
await super.onLoad();
Expand All @@ -52,7 +52,7 @@ void main() {
final withTappables = FlameTester(() => _HasTappablesGame());

group('Composability', () {
withTappables.test(
testWithFlameGame(
'child is not added until the component is prepared',
(game) async {
final child = Component();
Expand All @@ -71,7 +71,7 @@ void main() {
},
);

withTappables.test('removes the child from the component', (game) async {
testWithFlameGame('removes the child from the component', (game) async {
final child = Component();
final wrapper = Component();
await game.ensureAdd(wrapper);
Expand All @@ -88,7 +88,7 @@ void main() {
expect(wrapper.contains(child), false);
});

withTappables.test(
testWithFlameGame(
'when child is async loading, the child is added to the component after '
'loading',
(game) async {
Expand Down Expand Up @@ -119,8 +119,8 @@ void main() {
expect(child.tapped, true);
});

withTappables.test('add multiple children with addAll', (game) async {
final children = List.generate(10, (_) => _MyTap());
testWithFlameGame('add multiple children with addAll', (game) async {
final children = List.generate(10, (_) => _MyAsyncChild());
final wrapper = Component();
await wrapper.addAll(children);

Expand Down Expand Up @@ -191,7 +191,7 @@ void main() {
expect(child.rendered, true);
});

withTappables.test('initially same debugMode as parent', (game) async {
testWithFlameGame('initially same debugMode as parent', (game) async {
final child = Component();
final wrapper = Component();
wrapper.debugMode = true;
Expand All @@ -203,5 +203,23 @@ void main() {
wrapper.debugMode = false;
expect(child.debugMode, true);
});

testWithFlameGame(
'debugMode propagates to descendants onMount',
(game) async {
final child = Component();
final parent = Component();
final grandParent = Component();
parent.add(child);
grandParent.add(parent);
grandParent.debugMode = true;

await game.ensureAdd(grandParent);

expect(child.debugMode, true);
expect(parent.debugMode, true);
expect(grandParent.debugMode, true);
},
);
});
}

0 comments on commit e894d20

Please sign in to comment.