mirror of https://github.com/flutter/pinball.git
parent
b087d0e418
commit
3b41771bdf
@ -1,41 +0,0 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// {@template component_controller}
|
||||
/// A [ComponentController] is a [Component] in charge of handling the logic
|
||||
/// associated with another [Component].
|
||||
/// {@endtemplate}
|
||||
abstract class ComponentController<T extends Component> extends Component {
|
||||
/// {@macro component_controller}
|
||||
ComponentController(this.component);
|
||||
|
||||
/// The [Component] controlled by this [ComponentController].
|
||||
final T component;
|
||||
|
||||
@override
|
||||
Future<void> addToParent(Component parent) async {
|
||||
assert(
|
||||
parent == component,
|
||||
'ComponentController should be child of $component.',
|
||||
);
|
||||
await super.addToParent(parent);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> add(Component component) {
|
||||
throw Exception('ComponentController cannot add other components.');
|
||||
}
|
||||
}
|
||||
|
||||
/// Mixin that attaches a single [ComponentController] to a [Component].
|
||||
mixin Controls<T extends ComponentController> on Component {
|
||||
/// The [ComponentController] attached to this [Component].
|
||||
late T controller;
|
||||
|
||||
@override
|
||||
@mustCallSuper
|
||||
Future<void> onLoad() async {
|
||||
await super.onLoad();
|
||||
await add(controller);
|
||||
}
|
||||
}
|
@ -1,96 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/src/components/component.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
class TestComponentController extends ComponentController {
|
||||
TestComponentController(Component component) : super(component);
|
||||
}
|
||||
|
||||
class ControlledComponent extends Component
|
||||
with Controls<TestComponentController> {
|
||||
ControlledComponent() : super() {
|
||||
controller = TestComponentController(this);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(FlameGame.new);
|
||||
|
||||
group('ComponentController', () {
|
||||
flameTester.test(
|
||||
'can be instantiated',
|
||||
(game) async {
|
||||
expect(
|
||||
TestComponentController(Component()),
|
||||
isA<ComponentController>(),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'throws AssertionError when not attached to controlled component',
|
||||
(game) async {
|
||||
final component = Component();
|
||||
final controller = TestComponentController(component);
|
||||
|
||||
final anotherComponent = Component();
|
||||
await expectLater(
|
||||
() async => await anotherComponent.add(controller),
|
||||
throwsAssertionError,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'throws Exception when adding a component',
|
||||
(game) async {
|
||||
final component = ControlledComponent();
|
||||
final controller = TestComponentController(component);
|
||||
|
||||
await expectLater(
|
||||
() async => controller.add(Component()),
|
||||
throwsException,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'throws Exception when adding multiple components',
|
||||
(game) async {
|
||||
final component = ControlledComponent();
|
||||
final controller = TestComponentController(component);
|
||||
|
||||
await expectLater(
|
||||
() async => controller.addAll([
|
||||
Component(),
|
||||
Component(),
|
||||
]),
|
||||
throwsException,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('Controls', () {
|
||||
flameTester.test(
|
||||
'can be instantiated',
|
||||
(game) async {
|
||||
expect(ControlledComponent(), isA<Component>());
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test('adds controller', (game) async {
|
||||
final component = ControlledComponent();
|
||||
|
||||
await game.add(component);
|
||||
await game.ready();
|
||||
|
||||
expect(component.contains(component.controller), isTrue);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue