Skip to content

Commit

Permalink
Show a dialog to confirm closing current game
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdelaal86 committed Oct 12, 2024
1 parent bb929c2 commit 9676435
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 33 deletions.
4 changes: 2 additions & 2 deletions flutter/lib/components/buttons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RoundedButton extends AdvancedButtonComponent {

@override
Future<void> onLoad() async {
super.onLoad();
await super.onLoad();
defaultLabel = createLabel(icon, text);
defaultSkin = RoundedRectComponent()..setColor(defaultColor);
hoverSkin = RoundedRectComponent()..setColor(hoverColor);
Expand Down Expand Up @@ -79,7 +79,7 @@ class ToggleButton extends ToggleButtonComponent {

@override
Future<void> onLoad() async {
super.onLoad();
await super.onLoad();
defaultLabel = defaultSelectedLabel = RoundedButton.createLabel(icon, text);
defaultSkin = RoundedRectComponent()..setColor(defaultColor);
hoverSkin = RoundedRectComponent()..setColor(hoverColor);
Expand Down
43 changes: 43 additions & 0 deletions flutter/lib/components/dialogs.dart
Original file line number Diff line number Diff line change
@@ -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<bool> with HasGameReference<DjambiGame> {
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),
),
],
);
}
}
4 changes: 2 additions & 2 deletions flutter/lib/components/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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",
));
Expand Down
9 changes: 7 additions & 2 deletions flutter/lib/components/header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import 'game.dart';

class Header extends PositionComponent with HasGameReference<DjambiGame> {
final String title;

final VoidCallback? onBackTapUp;

late final TextComponent _text;

Header({
this.title = "Djambi",
this.onBackTapUp,
});

@override
Expand All @@ -25,7 +27,10 @@ class Header extends PositionComponent with HasGameReference<DjambiGame> {
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(),
},
),
]);
}
Expand Down
43 changes: 18 additions & 25 deletions flutter/lib/components/pages/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,16 @@ class OptionsPage extends PositionComponent with HasGameReference<DjambiGame> {

@override
Future<void> 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);
Expand Down Expand Up @@ -93,48 +86,48 @@ class OptionsPage extends PositionComponent with HasGameReference<DjambiGame> {
_playButton.position = Vector2(size.x / 2, size.y - _playButton.height - hrSep);
}

void _createTurnDirection() {
List<Component> _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<Component> _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<Component> _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;
Expand Down
14 changes: 12 additions & 2 deletions flutter/lib/components/pages/play.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<DjambiGame> {
// @override
// bool get debugMode => true;

Expand All @@ -30,7 +32,7 @@ class PlayPage extends PositionComponent {
onManoeuvreCompleted,
);
await addAll([
Header(),
Header(onBackTapUp: onBackTapUp),
_board = Board(
_gameState, _boardTheme, _pieceTheme,
anchor: Anchor.center,
Expand Down Expand Up @@ -67,4 +69,12 @@ class PlayPage extends PositionComponent {
_gameState.aiAct(2);
}
}

Future<void> 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");
}
}
}

0 comments on commit 9676435

Please sign in to comment.