Skip to content

Commit

Permalink
fix!: Migrate from RawKeyEvent to KeyEvent (#3002)
Browse files Browse the repository at this point in the history
First pass at migrating from `RawKeyEvent` and `RawKeyboard`
to `KeyEvent` and `HardwareKeyboard`.

Context:
https://docs.flutter.dev/release/breaking-changes/key-event-migration
  • Loading branch information
domesticmouse authored Feb 15, 2024
1 parent 20d368c commit 330862c
Show file tree
Hide file tree
Showing 53 changed files with 90 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:


env:
FLUTTER_MIN_VERSION: '3.16.0'
FLUTTER_MIN_VERSION: '3.19.0'

jobs:
# BEGIN LINTING STAGE
Expand Down
2 changes: 1 addition & 1 deletion doc/flame/examples/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ publish_to: none

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.13.0"
flutter: ^3.19.0

dependencies:
flame: ^1.15.0
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorials/platformer/app/lib/actors/ember.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class EmberPlayer extends SpriteAnimationComponent
}

@override
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
bool onKeyEvent(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
horizontalDirection = 0;
horizontalDirection += (keysPressed.contains(LogicalKeyboardKey.keyA) ||
keysPressed.contains(LogicalKeyboardKey.arrowLeft))
Expand Down
1 change: 1 addition & 0 deletions doc/tutorials/platformer/app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version: 1.0.0+1

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ^3.19.0

dependencies:
flame: ^1.15.0
Expand Down
2 changes: 1 addition & 1 deletion examples/games/padracing/lib/padracing_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class PadRacingGame extends Forge2DGame with KeyboardEvents {

@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
super.onKeyEvent(event, keysPressed);
Expand Down
2 changes: 1 addition & 1 deletion examples/games/rogue_shooter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version: 0.1.0

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.13.0"
flutter: ^3.19.0

dependencies:
flame: ^1.15.0
Expand Down
2 changes: 1 addition & 1 deletion examples/games/trex/lib/trex_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class TRexGame extends FlameGame

@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
if (keysPressed.contains(LogicalKeyboardKey.enter) ||
Expand Down
2 changes: 1 addition & 1 deletion examples/games/trex/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version: 0.1.0

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.13.0"
flutter: ^3.19.0

dependencies:
collection: ^1.16.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ class Player extends PositionComponent with KeyboardHandler {
}

@override
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
final isKeyDown = event is RawKeyDownEvent;
bool onKeyEvent(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
final isKeyDown = event is KeyDownEvent;
final keyLeft = (event.logicalKey == LogicalKeyboardKey.arrowLeft) ||
(event.logicalKey == LogicalKeyboardKey.keyA);
final keyRight = (event.logicalKey == LogicalKeyboardKey.arrowRight) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ class CoordinateSystemsExample extends FlameGame
/// Camera controls.
@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
final isKeyDown = event is RawKeyDownEvent;
final isKeyDown = event is KeyDownEvent;

if (event.logicalKey == LogicalKeyboardKey.keyA) {
cameraVelocity.x = isKeyDown ? -1 : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class MovableEmber extends Ember<FollowComponentExample>
}

@override
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
final isKeyDown = event is RawKeyDownEvent;
bool onKeyEvent(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
final isKeyDown = event is KeyDownEvent;

final bool handled;
if (event.logicalKey == LogicalKeyboardKey.keyA) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Press T button to toggle player to collide with other objects.

@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
for (final key in keysPressed) {
Expand Down
6 changes: 3 additions & 3 deletions examples/lib/stories/input/keyboard_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class KeyboardExample extends FlameGame with KeyboardEvents {

@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
final isKeyDown = event is RawKeyDownEvent;
final isKeyDown = event is KeyDownEvent;

// Avoiding repeat event as we are interested only in
// key up and key down event.
if (!event.repeat) {
if (key is! KeyRepeatEvent) {
if (event.logicalKey == LogicalKeyboardKey.keyA) {
_direction.x += isKeyDown ? -1 : 1;
} else if (event.logicalKey == LogicalKeyboardKey.keyD) {
Expand Down
4 changes: 2 additions & 2 deletions examples/lib/stories/system/without_flame_game_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class NoFlameGameExample extends Game with KeyboardEvents {

@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
final isKeyDown = event is RawKeyDownEvent;
final isKeyDown = event is KeyDownEvent;

if (event.logicalKey == LogicalKeyboardKey.keyA) {
velocity.x = isKeyDown ? -1 : 0;
Expand Down
2 changes: 1 addition & 1 deletion examples/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version: 0.1.0

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.13.0"
flutter: ^3.19.0

dependencies:
dashbook: ^0.1.12
Expand Down
1 change: 1 addition & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ command:
bootstrap:
environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ^3.19.0
dependencies:
meta: ^1.9.1
vector_math: ^2.1.4
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ publish_to: 'none'

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.13.0"
flutter: ^3.19.0

dependencies:
flame: ^1.15.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class KeyboardListenerComponent extends Component with KeyboardHandler {
final Map<LogicalKeyboardKey, KeyHandlerCallback> _keyDown;

@override
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
final isUp = event is RawKeyUpEvent;
bool onKeyEvent(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
final isUp = event is KeyUpEvent;

final handlers = isUp ? _keyUp : _keyDown;
final handler = handlers[event.logicalKey];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:flutter/services.dart';
/// Must be used in components that can only be added to games that are mixed
/// with [HasKeyboardHandlerComponents].
mixin KeyboardHandler on Component {
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
bool onKeyEvent(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
return true;
}
}
9 changes: 6 additions & 3 deletions packages/flame/lib/src/game/game_widget/game_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,18 @@ class GameWidgetState<T extends Game> extends State<GameWidget<T>> {
}
}

KeyEventResult _handleKeyEvent(FocusNode focusNode, RawKeyEvent event) {
KeyEventResult _handleKeyEvent(FocusNode focusNode, KeyEvent event) {
final game = currentGame;

if (!_focusNode.hasPrimaryFocus) {
return KeyEventResult.ignored;
}

if (game is KeyboardEvents) {
return game.onKeyEvent(event, RawKeyboard.instance.keysPressed);
return game.onKeyEvent(
event,
HardwareKeyboard.instance.logicalKeysPressed,
);
}
return KeyEventResult.handled;
}
Expand Down Expand Up @@ -361,7 +364,7 @@ class GameWidgetState<T extends Game> extends State<GameWidget<T>> {
focusNode: _focusNode,
autofocus: widget.autofocus,
descendantsAreFocusable: true,
onKey: _handleKeyEvent,
onKeyEvent: _handleKeyEvent,
child: MouseRegion(
cursor: currentGame.mouseCursor,
child: Directionality(
Expand Down
4 changes: 2 additions & 2 deletions packages/flame/lib/src/game/mixins/keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mixin HasKeyboardHandlerComponents<W extends World> on FlameGame<W>
@override
@mustCallSuper
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
final blockedPropagation = !propagateToChildren<KeyboardHandler>(
Expand All @@ -34,7 +34,7 @@ mixin HasKeyboardHandlerComponents<W extends World> on FlameGame<W>
/// Override [onKeyEvent] to customize the keyboard handling behavior.
mixin KeyboardEvents on Game {
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
assert(
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ funding:

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.13.0"
flutter: ^3.19.0

dependencies:
collection: ^1.17.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ abstract class _KeyCallStub {

class KeyCallStub extends Mock implements _KeyCallStub {}

class MockRawKeyUpEvent extends Mock implements RawKeyUpEvent {
class MockKeyUpEvent extends Mock implements KeyUpEvent {
@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return super.toString();
}
}

RawKeyUpEvent _mockKeyUp(LogicalKeyboardKey key) {
final event = MockRawKeyUpEvent();
KeyUpEvent _mockKeyUp(LogicalKeyboardKey key) {
final event = MockKeyUpEvent();
when(() => event.logicalKey).thenReturn(key);
return event;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class _KeyboardEventsGame extends FlameGame with KeyboardEvents {

@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
this.keysPressed.add(event.character ?? 'none');
Expand All @@ -25,7 +25,7 @@ class _KeyboardHandlerComponent extends Component with KeyboardHandler {
final List<String> keysPressed = [];

@override
bool onKeyEvent(RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
bool onKeyEvent(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
this.keysPressed.add(event.character ?? 'none');
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/flame/test/game/game_widget/game_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class _GameWithKeyboardEvents extends FlameGame with KeyboardEvents {

@override
KeyEventResult onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
keyEvents.add(event.logicalKey);
Expand Down Expand Up @@ -443,7 +443,7 @@ void main() {
testWidgets('overlay handles keys', (tester) async {
final overlayKeyEvents = <LogicalKeyboardKey>[];
final overlayFocusNode = FocusNode(
onKey: (_, keyEvent) {
onKeyEvent: (_, keyEvent) {
overlayKeyEvents.add(keyEvent.logicalKey);
return KeyEventResult.ignored;
},
Expand Down Expand Up @@ -475,7 +475,7 @@ void main() {
await simulateKeyDownEvent(LogicalKeyboardKey.keyA);
await tester.pump();

expect(game.keyEvents, <RawKeyEvent>[]);
expect(game.keyEvents, <KeyEvent>[]);
expect(overlayKeyEvents, [LogicalKeyboardKey.keyA]);
});
});
Expand Down
20 changes: 10 additions & 10 deletions packages/flame/test/game/mixins/keyboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@ import 'package:flame/input.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class _ValidGame extends FlameGame with KeyboardEvents {}

class _InvalidGame extends FlameGame
with HasKeyboardHandlerComponents, KeyboardEvents {}

class _MockRawKeyEventData extends Mock implements RawKeyEventData {
@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.debug}) {
return super.toString();
}
}

void main() {
group('Keyboard events', () {
test(
'game with KeyboardEvents can handle key events',
() {
final validGame = _ValidGame();
final event = RawKeyDownEvent(data: _MockRawKeyEventData());
const event = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.arrowUp,
logicalKey: LogicalKeyboardKey.arrowUp,
timeStamp: Duration.zero,
);

// Should just work with the default implementation
expect(
Expand All @@ -37,7 +33,11 @@ void main() {
'cannot mix KeyboardEvent and HasKeyboardHandlerComponents together',
() {
final invalidGame = _InvalidGame();
final event = RawKeyDownEvent(data: _MockRawKeyEventData());
const event = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.arrowUp,
logicalKey: LogicalKeyboardKey.arrowUp,
timeStamp: Duration.zero,
);

// Should throw an assertion error
expect(
Expand Down
2 changes: 1 addition & 1 deletion packages/flame_audio/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ funding:

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.13.0"
flutter: ^3.19.0

dependencies:
audioplayers: ^5.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class PlayerComponent extends SpriteAnimationComponent

@override
bool onKeyEvent(
RawKeyEvent event,
KeyEvent event,
Set<LogicalKeyboardKey> keysPressed,
) {
if (keysPressed.contains(LogicalKeyboardKey.tab)) {
Expand Down
1 change: 1 addition & 0 deletions packages/flame_bloc/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version: 1.0.0+1

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ^3.19.0

dependencies:
equatable: ^2.0.5
Expand Down
2 changes: 1 addition & 1 deletion packages/flame_bloc/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ funding:

environment:
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"
flutter: ^3.19.0

dependencies:
bloc: ^8.1.1
Expand Down
Loading

0 comments on commit 330862c

Please sign in to comment.