fix: keep focus on game (#311)

* fix: keep focus on game

* fix: keep focus on game

* fix: keep focus on game

* fix: keep focus on game

Co-authored-by: Tom Arra <tarra3@gmail.com>
pull/358/head
Jochum van der Ploeg 2 years ago committed by alestiago
parent be3a3b053d
commit 3ff0e5ef35

@ -24,7 +24,8 @@ class PinballGame extends PinballForge2DGame
required GameBloc gameBloc,
required AppLocalizations l10n,
required PinballPlayer player,
}) : _gameBloc = gameBloc,
}) : focusNode = FocusNode(),
_gameBloc = gameBloc,
_player = player,
_characterTheme = characterTheme,
_l10n = l10n,
@ -40,6 +41,8 @@ class PinballGame extends PinballForge2DGame
@override
Color backgroundColor() => Colors.transparent;
final FocusNode focusNode;
final CharacterTheme _characterTheme;
final PinballPlayer _player;

@ -118,19 +118,27 @@ class PinballGameLoadedView extends StatelessWidget {
child: Stack(
children: [
Positioned.fill(
child: GameWidget<PinballGame>(
game: game,
initialActiveOverlays: const [PinballGame.playButtonOverlay],
overlayBuilderMap: {
PinballGame.playButtonOverlay: (context, game) {
return const Positioned(
bottom: 20,
right: 0,
left: 0,
child: PlayButtonOverlay(),
);
},
child: MouseRegion(
onHover: (_) {
if (!game.focusNode.hasFocus) {
game.focusNode.requestFocus();
}
},
child: GameWidget<PinballGame>(
game: game,
focusNode: game.focusNode,
initialActiveOverlays: const [PinballGame.playButtonOverlay],
overlayBuilderMap: {
PinballGame.playButtonOverlay: (context, game) {
return const Positioned(
bottom: 20,
right: 0,
left: 0,
child: PlayButtonOverlay(),
);
},
},
),
),
),
const _PositionedGameHud(),

@ -1,5 +1,7 @@
// ignore_for_file: prefer_const_constructors
import 'dart:ui';
import 'package:bloc_test/bloc_test.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
@ -291,5 +293,43 @@ void main() {
findsNothing,
);
});
testWidgets('keep focus on game when mouse hovers over it', (tester) async {
final startGameState = StartGameState.initial().copyWith(
status: StartGameStatus.play,
);
final gameState = GameState.initial().copyWith(
status: GameStatus.gameOver,
);
whenListen(
startGameBloc,
Stream.value(startGameState),
initialState: startGameState,
);
whenListen(
gameBloc,
Stream.value(gameState),
initialState: gameState,
);
await tester.pumpApp(
PinballGameView(game: game),
gameBloc: gameBloc,
startGameBloc: startGameBloc,
);
game.focusNode.unfocus();
await tester.pump();
expect(game.focusNode.hasFocus, isFalse);
final gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer(location: Offset.zero);
addTearDown(gesture.removePointer);
await gesture.moveTo((game.size / 2).toOffset());
await tester.pump();
expect(game.focusNode.hasFocus, isTrue);
});
});
}

Loading…
Cancel
Save