fix: debugMode should be inherited from parent when mounted (#1469)

This commit is contained in:
Lukas Klingsbo
2022-03-19 16:13:50 +01:00
committed by GitHub
parent 6434829b45
commit e894d20133
6 changed files with 42 additions and 23 deletions

View File

@ -42,9 +42,8 @@ class MultipleShapesExample extends FlameGame
var totalAdded = 1; var totalAdded = 1;
while (totalAdded < 100) { while (totalAdded < 100) {
lastToAdd = nextRandomCollidable(lastToAdd, screenHitbox); lastToAdd = nextRandomCollidable(lastToAdd, screenHitbox);
final lastBottomRight = final lastBottomRight = lastToAdd.toAbsoluteRect().bottomRight;
lastToAdd.toAbsoluteRect().bottomRight.toVector2(); if (lastBottomRight.dx < size.x && lastBottomRight.dy < size.y) {
if (lastBottomRight.x < size.x && lastBottomRight.y < size.y) {
add(lastToAdd); add(lastToAdd);
totalAdded++; totalAdded++;
} else { } else {
@ -143,7 +142,7 @@ abstract class MyCollidable extends PositionComponent
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
if (isDragged) { if (isDragged) {
final localCenter = (scaledSize / 2).toOffset(); final localCenter = scaledSize.toOffset() / 2;
canvas.drawCircle(localCenter, 5, _dragIndicatorPaint); canvas.drawCircle(localCenter, 5, _dragIndicatorPaint);
} }
} }

View File

@ -1,5 +1,3 @@
import 'dart:ui';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import '../../../collisions.dart'; import '../../../collisions.dart';
@ -14,9 +12,6 @@ mixin ShapeHitbox on ShapeComponent implements Hitbox<ShapeHitbox> {
@override @override
CollisionType collisionType = CollisionType.active; CollisionType collisionType = CollisionType.active;
@override
Paint get paint => debugPaint;
/// Whether the hitbox is allowed to collide with another hitbox that is /// Whether the hitbox is allowed to collide with another hitbox that is
/// added to the same parent. /// added to the same parent.
bool allowSiblingCollision = false; bool allowSiblingCollision = false;
@ -48,10 +43,7 @@ mixin ShapeHitbox on ShapeComponent implements Hitbox<ShapeHitbox> {
final Matrix3 _rotationMatrix = Matrix3.zero(); final Matrix3 _rotationMatrix = Matrix3.zero();
@override @override
bool get renderShape => _renderShape || debugMode; bool renderShape = false;
@override
set renderShape(bool shouldRender) => _renderShape = shouldRender;
bool _renderShape = false;
@protected @protected
late PositionComponent hitboxParent; late PositionComponent hitboxParent;

View File

@ -390,7 +390,6 @@ class Component {
_state == LifecycleState.removed, _state == LifecycleState.removed,
); );
_parent = parent; _parent = parent;
debugMode |= parent.debugMode;
parent.lifecycle._children.add(this); parent.lifecycle._children.add(this);
if (!isLoaded) { if (!isLoaded) {
@ -455,6 +454,7 @@ class Component {
} }
_mountCompleter?.complete(); _mountCompleter?.complete();
_mountCompleter = null; _mountCompleter = null;
debugMode |= _parent!.debugMode;
onMount(); onMount();
_state = LifecycleState.mounted; _state = LifecycleState.mounted;
if (!existingChild) { if (!existingChild) {

View File

@ -57,8 +57,6 @@ class CircleComponent extends ShapeComponent {
return min(_scaledSize.x, _scaledSize.y) / 2; 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 @override
void render(Canvas canvas) { void render(Canvas canvas) {
if (renderShape) { if (renderShape) {
@ -66,6 +64,12 @@ class CircleComponent extends ShapeComponent {
} }
} }
@override
void renderDebugMode(Canvas canvas) {
super.renderDebugMode(canvas);
canvas.drawCircle((size / 2).toOffset(), radius, debugPaint);
}
/// Checks whether the represented circle contains the [point]. /// Checks whether the represented circle contains the [point].
@override @override
bool containsPoint(Vector2 point) { bool containsPoint(Vector2 point) {

View File

@ -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]. /// Checks whether the polygon contains the [point].
/// Note: The polygon needs to be convex for this to work. /// Note: The polygon needs to be convex for this to work.
@override @override

View File

@ -39,7 +39,7 @@ class _MyTap extends PositionComponent with Tappable {
} }
} }
class _MyAsyncChild extends _MyTap { class _MyAsyncChild extends PositionComponent {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
@ -52,7 +52,7 @@ void main() {
final withTappables = FlameTester(() => _HasTappablesGame()); final withTappables = FlameTester(() => _HasTappablesGame());
group('Composability', () { group('Composability', () {
withTappables.test( testWithFlameGame(
'child is not added until the component is prepared', 'child is not added until the component is prepared',
(game) async { (game) async {
final child = Component(); final child = Component();
@ -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 child = Component();
final wrapper = Component(); final wrapper = Component();
await game.ensureAdd(wrapper); await game.ensureAdd(wrapper);
@ -88,7 +88,7 @@ void main() {
expect(wrapper.contains(child), false); expect(wrapper.contains(child), false);
}); });
withTappables.test( testWithFlameGame(
'when child is async loading, the child is added to the component after ' 'when child is async loading, the child is added to the component after '
'loading', 'loading',
(game) async { (game) async {
@ -119,8 +119,8 @@ void main() {
expect(child.tapped, true); expect(child.tapped, true);
}); });
withTappables.test('add multiple children with addAll', (game) async { testWithFlameGame('add multiple children with addAll', (game) async {
final children = List.generate(10, (_) => _MyTap()); final children = List.generate(10, (_) => _MyAsyncChild());
final wrapper = Component(); final wrapper = Component();
await wrapper.addAll(children); await wrapper.addAll(children);
@ -191,7 +191,7 @@ void main() {
expect(child.rendered, true); 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 child = Component();
final wrapper = Component(); final wrapper = Component();
wrapper.debugMode = true; wrapper.debugMode = true;
@ -203,5 +203,23 @@ void main() {
wrapper.debugMode = false; wrapper.debugMode = false;
expect(child.debugMode, true); 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);
},
);
}); });
} }