diff --git a/flutter/lib/components/buttons.dart b/flutter/lib/components/buttons.dart index dc57524..fc80a8d 100644 --- a/flutter/lib/components/buttons.dart +++ b/flutter/lib/components/buttons.dart @@ -44,7 +44,7 @@ class RoundedButton extends AdvancedButtonComponent { @override Future onLoad() async { - super.onLoad(); + await super.onLoad(); defaultLabel = createLabel(icon, text); defaultSkin = RoundedRectComponent()..setColor(defaultColor); hoverSkin = RoundedRectComponent()..setColor(hoverColor); @@ -79,7 +79,7 @@ class ToggleButton extends ToggleButtonComponent { @override Future onLoad() async { - super.onLoad(); + await super.onLoad(); defaultLabel = defaultSelectedLabel = RoundedButton.createLabel(icon, text); defaultSkin = RoundedRectComponent()..setColor(defaultColor); hoverSkin = RoundedRectComponent()..setColor(hoverColor); diff --git a/flutter/lib/components/dialogs.dart b/flutter/lib/components/dialogs.dart new file mode 100644 index 0000000..64c1778 --- /dev/null +++ b/flutter/lib/components/dialogs.dart @@ -0,0 +1,43 @@ +import 'dart:ui'; + +import 'package:flame/components.dart'; +import 'package:flame/game.dart'; + +import 'buttons.dart'; +import 'game.dart'; + +class ConfirmDialog extends ValueRoute with HasGameReference { + ConfirmDialog(this.text) : super(value: false); + final String text; + + @override + Component build() { + return RectangleComponent( + size: Vector2(600, 300), + position: game.size / 2, + anchor: Anchor.center, + paint: Paint()..color = const Color(0x44ff0000), + children: [ + TextBoxComponent( + text: text, + anchor: Anchor.center, + align: Anchor.center, + size: Vector2(550, 150), + position: Vector2(300, 100), + ), + RoundedButton( + text: 'Yes', + size: Vector2(250, 75), + onReleased: () => completeWith(true), + position: Vector2(150, 200), + ), + RoundedButton( + text: 'No', + size: Vector2(250, 75), + onReleased: () => completeWith(false), + position: Vector2(450, 200), + ), + ], + ); + } +} diff --git a/flutter/lib/components/game.dart b/flutter/lib/components/game.dart index 40218f2..dd0417a 100644 --- a/flutter/lib/components/game.dart +++ b/flutter/lib/components/game.dart @@ -12,8 +12,8 @@ class DjambiGame extends FlameGame { await add(router = RouterComponent( routes: { "home": Route(HomePage.new), - "options": Route(OptionsPage.new), - "play": Route(PlayPage.new), + "options": Route(OptionsPage.new, maintainState: false), + "play": Route(PlayPage.new, maintainState: false), }, initialRoute: "home", )); diff --git a/flutter/lib/components/header.dart b/flutter/lib/components/header.dart index ccce39f..5999d72 100644 --- a/flutter/lib/components/header.dart +++ b/flutter/lib/components/header.dart @@ -6,11 +6,13 @@ import 'game.dart'; class Header extends PositionComponent with HasGameReference { final String title; - + final VoidCallback? onBackTapUp; + late final TextComponent _text; Header({ this.title = "Djambi", + this.onBackTapUp, }); @override @@ -25,7 +27,10 @@ class Header extends PositionComponent with HasGameReference { icon: Icons.arrow_back, position: Vector2(50, 50), size: RoundedButton.defaultSize, - onReleased: () => game.router.pop(), + onReleased: () => switch(onBackTapUp) { + null => game.router.pop(), + final fun => fun.call(), + }, ), ]); } diff --git a/flutter/lib/components/pages/options.dart b/flutter/lib/components/pages/options.dart index a85c291..0361602 100644 --- a/flutter/lib/components/pages/options.dart +++ b/flutter/lib/components/pages/options.dart @@ -28,23 +28,16 @@ class OptionsPage extends PositionComponent with HasGameReference { @override Future onLoad() async { - _createTurnDirection(); - _createStartIdeology(); - _createHumanPlayers(); await addAll([ Header(), + ..._createTurnDirection(), + ..._createStartIdeology(), + ..._createHumanPlayers(), _playButton = RoundedButton( text: "Play", size: Vector2(300, 75), onReleased: () => game.router.pushNamed("play"), ), - _turnDirText, - _turnDirClockwise, - _turnDirAnticlockwise, - _startIdeologyText, - ..._startIdeologyButtons, - _humanPlayersText, - ..._humanPlayersButtons, ]); _setTurnDirection(_settings.turnDirection); _setStartIdeology(_settings.startIdeology); @@ -93,48 +86,48 @@ class OptionsPage extends PositionComponent with HasGameReference { _playButton.position = Vector2(size.x / 2, size.y - _playButton.height - hrSep); } - void _createTurnDirection() { + List _createTurnDirection() => [ _turnDirText = TextComponent( text: "Turn Direction:", anchor: Anchor.centerRight, - ); + ), _turnDirClockwise = OptionButton( icon: Icons.rotate_right, size: RoundedButton.defaultSize, onSelect: () => _setTurnDirection(TurnDirection.clockwise), - ); + ), _turnDirAnticlockwise = OptionButton( icon: Icons.rotate_left, size: RoundedButton.defaultSize, onSelect: () => _setTurnDirection(TurnDirection.anticlockwise), - ); - } + ), + ]; - void _createStartIdeology() { + List _createStartIdeology() => [ _startIdeologyText = TextComponent( text: "Start Player:", anchor: Anchor.centerRight, - ); - _startIdeologyButtons = Ideology.values.map((e) => OptionButton( + ), + ..._startIdeologyButtons = Ideology.values.map((e) => OptionButton( text: e.name[0].toUpperCase(), size: RoundedButton.defaultSize, onSelect: () => _setStartIdeology(e), - )).toList(); - } + )).toList(), + ]; - void _createHumanPlayers() { + List _createHumanPlayers() => [ _humanPlayersText = TextComponent( text: "Human Players:", anchor: Anchor.centerRight, - ); - _humanPlayersButtons = Ideology.values.map((e) => ToggleButton( + ), + ..._humanPlayersButtons = Ideology.values.map((e) => ToggleButton( text: e.name[0].toUpperCase(), size: RoundedButton.defaultSize, onSelectedChanged: (value) { _settings.players[e] = value ? PlayerType.human : PlayerType.aiMaxN; }, - )).toList(); - } + )).toList(), + ]; void _setTurnDirection(TurnDirection direction) { _settings.turnDirection = direction; diff --git a/flutter/lib/components/pages/play.dart b/flutter/lib/components/pages/play.dart index d1f70f7..6af6c24 100644 --- a/flutter/lib/components/pages/play.dart +++ b/flutter/lib/components/pages/play.dart @@ -7,10 +7,12 @@ import '../../views/board.dart'; import '../../views/dimensions.dart'; import '../../views/state.dart'; import '../buttons.dart'; +import '../dialogs.dart'; +import '../game.dart'; import '../header.dart'; import '../settings.dart'; -class PlayPage extends PositionComponent { +class PlayPage extends PositionComponent with HasGameReference { // @override // bool get debugMode => true; @@ -30,7 +32,7 @@ class PlayPage extends PositionComponent { onManoeuvreCompleted, ); await addAll([ - Header(), + Header(onBackTapUp: onBackTapUp), _board = Board( _gameState, _boardTheme, _pieceTheme, anchor: Anchor.center, @@ -67,4 +69,12 @@ class PlayPage extends PositionComponent { _gameState.aiAct(2); } } + + Future onBackTapUp() async { + const msg = "Are you sure?\nThe match state will not be saved!"; + final result = await game.router.pushAndWait(ConfirmDialog(msg)); + if (result) { + game.router.popUntilNamed("home"); + } + } }