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/367/head
Jochum van der Ploeg 3 years ago committed by GitHub
parent 0502d97400
commit 24ac6a536d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -118,8 +118,15 @@ class PinballGameLoadedView extends StatelessWidget {
child: Stack( child: Stack(
children: [ children: [
Positioned.fill( Positioned.fill(
child: MouseRegion(
onHover: (_) {
if (!game.focusNode.hasFocus) {
game.focusNode.requestFocus();
}
},
child: GameWidget<PinballGame>( child: GameWidget<PinballGame>(
game: game, game: game,
focusNode: game.focusNode,
initialActiveOverlays: const [PinballGame.playButtonOverlay], initialActiveOverlays: const [PinballGame.playButtonOverlay],
overlayBuilderMap: { overlayBuilderMap: {
PinballGame.playButtonOverlay: (context, game) { PinballGame.playButtonOverlay: (context, game) {
@ -133,6 +140,7 @@ class PinballGameLoadedView extends StatelessWidget {
}, },
), ),
), ),
),
const _PositionedGameHud(), const _PositionedGameHud(),
], ],
), ),

@ -1,5 +1,7 @@
// ignore_for_file: prefer_const_constructors // ignore_for_file: prefer_const_constructors
import 'dart:ui';
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -291,5 +293,43 @@ void main() {
findsNothing, 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