mirror of https://github.com/flutter/pinball.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
66 lines
1.7 KiB
3 years ago
|
import 'package:flame/components.dart';
|
||
|
import 'package:flutter/services.dart';
|
||
3 years ago
|
import 'package:pinball/flame/flame.dart';
|
||
3 years ago
|
import 'package:pinball_components/pinball_components.dart';
|
||
|
|
||
3 years ago
|
/// {@template controlled_flipper}
|
||
|
/// A [Flipper] with a [FlipperController] attached.
|
||
|
/// {@endtemplate}
|
||
|
class ControlledFlipper extends Flipper with Controls<FlipperController> {
|
||
|
/// {@macro controlled_flipper}
|
||
|
ControlledFlipper({
|
||
|
required BoardSide side,
|
||
|
}) : super(side: side) {
|
||
|
controller = FlipperController(this);
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/// {@template flipper_controller}
|
||
3 years ago
|
/// A [ComponentController] that controls a [Flipper]s movement.
|
||
3 years ago
|
/// {@endtemplate}
|
||
3 years ago
|
class FlipperController extends ComponentController<Flipper>
|
||
|
with KeyboardHandler {
|
||
3 years ago
|
/// {@macro flipper_controller}
|
||
3 years ago
|
FlipperController(Flipper flipper)
|
||
|
: _keys = flipper.side.flipperKeys,
|
||
|
super(flipper);
|
||
3 years ago
|
|
||
|
/// The [LogicalKeyboardKey]s that will control the [Flipper].
|
||
|
///
|
||
|
/// [onKeyEvent] method listens to when one of these keys is pressed.
|
||
|
final List<LogicalKeyboardKey> _keys;
|
||
|
|
||
|
@override
|
||
|
bool onKeyEvent(
|
||
|
RawKeyEvent event,
|
||
|
Set<LogicalKeyboardKey> keysPressed,
|
||
|
) {
|
||
|
if (!_keys.contains(event.logicalKey)) return true;
|
||
|
|
||
|
if (event is RawKeyDownEvent) {
|
||
3 years ago
|
component.moveUp();
|
||
3 years ago
|
} else if (event is RawKeyUpEvent) {
|
||
3 years ago
|
component.moveDown();
|
||
3 years ago
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
extension on BoardSide {
|
||
|
List<LogicalKeyboardKey> get flipperKeys {
|
||
|
switch (this) {
|
||
|
case BoardSide.left:
|
||
|
return [
|
||
|
LogicalKeyboardKey.arrowLeft,
|
||
|
LogicalKeyboardKey.keyA,
|
||
|
];
|
||
|
case BoardSide.right:
|
||
|
return [
|
||
|
LogicalKeyboardKey.arrowRight,
|
||
|
LogicalKeyboardKey.keyD,
|
||
|
];
|
||
|
}
|
||
|
}
|
||
|
}
|