From 24ac6a536d18246c4be9acef064412ac0ab6b558 Mon Sep 17 00:00:00 2001 From: Jochum van der Ploeg Date: Fri, 6 May 2022 20:12:41 +0200 Subject: [PATCH] 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 --- lib/game/pinball_game.dart | 5 ++- lib/game/view/pinball_game_page.dart | 32 ++++++++++------- test/game/view/pinball_game_page_test.dart | 40 ++++++++++++++++++++++ 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index b4886e4c..503d6261 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -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; diff --git a/lib/game/view/pinball_game_page.dart b/lib/game/view/pinball_game_page.dart index c0d5d1d8..076ed336 100644 --- a/lib/game/view/pinball_game_page.dart +++ b/lib/game/view/pinball_game_page.dart @@ -118,19 +118,27 @@ class PinballGameLoadedView extends StatelessWidget { child: Stack( children: [ Positioned.fill( - child: GameWidget( - 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( + 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(), diff --git a/test/game/view/pinball_game_page_test.dart b/test/game/view/pinball_game_page_test.dart index 5aef07dd..d1ecd72d 100644 --- a/test/game/view/pinball_game_page_test.dart +++ b/test/game/view/pinball_game_page_test.dart @@ -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); + }); }); }