Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve navigation between all the pages #74

Merged
merged 1 commit into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions lib/end_game/view/end_game_page.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:gamepads/gamepads.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:lightrunners/game/game.dart';
import 'package:lightrunners/title/view/title_page.dart';
import 'package:lightrunners/ui/ui.dart';
import 'package:lightrunners/utils/gamepad_navigator.dart';
import 'package:lightrunners/widgets/widgets.dart';

class EndGamePage extends StatelessWidget {
class EndGamePage extends StatefulWidget {
const EndGamePage({
required this.scores,
super.key,
Expand All @@ -14,15 +19,40 @@ class EndGamePage extends StatelessWidget {

static Route<void> route(Map<Color, int> scores) {
return MaterialPageRoute<void>(
maintainState: false,
builder: (_) => EndGamePage(scores: scores),
);
}

@override
State<EndGamePage> createState() => _EndGamePageState();
}

class _EndGamePageState extends State<EndGamePage> {
late StreamSubscription<GamepadEvent> _gamepadSubscription;
late GamepadNavigator _gamepadNavigator;

@override
void initState() {
super.initState();
_gamepadNavigator = GamepadNavigator(
onAny: () => Navigator.of(context).pushReplacement(TitlePage.route()),
);
_gamepadSubscription = Gamepads.events.listen(_gamepadNavigator.handle);
}

@override
void dispose() {
_gamepadSubscription.cancel();

super.dispose();
}

@override
Widget build(BuildContext context) {
final fontFamily = GoogleFonts.bungee().fontFamily;

final scores = this.scores.entries.toList()
final scores = widget.scores.entries.toList()
..sort((a, b) => b.value.compareTo(a.value));

const baseSize = 64;
Expand Down Expand Up @@ -100,7 +130,8 @@ class EndGamePage extends StatelessWidget {
),
OpacityBlinker(
child: TextButton(
onPressed: () => Navigator.pop(context),
onPressed: () =>
Navigator.of(context).pushReplacement(TitlePage.route()),
child: Text(
'Press any button to continue',
style: TextStyle(
Expand Down
1 change: 1 addition & 0 deletions lib/game/view/game_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GamePage extends StatefulWidget {

static Route<void> route({required List<String?> players}) {
return MaterialPageRoute<void>(
maintainState: false,
builder: (_) => GamePage(players: players),
);
}
Expand Down
15 changes: 10 additions & 5 deletions lib/lobby/view/lobby_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import 'package:flutter/services.dart';
import 'package:gamepads/gamepads.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:lightrunners/game/view/game_page.dart';
import 'package:lightrunners/title/view/title_page.dart';
import 'package:lightrunners/ui/palette.dart';
import 'package:lightrunners/utils/gamepad_map.dart';
import 'package:lightrunners/widgets/screen_scaffold.dart';

class LobbyPage extends StatefulWidget {
const LobbyPage({super.key});

static Route<void> route() {
return MaterialPageRoute<void>(
maintainState: false,
builder: (_) => const LobbyPage(),
);
}
Expand All @@ -34,10 +37,11 @@ class _LobbyPageState extends State<LobbyPage> {

_gamepadSubscription = Gamepads.events.listen((GamepadEvent event) {
setState(() {
if ((event.key == '7' || event.key == 'line.horizontal.3.circle') &&
event.value == 1.0) {
// The start key was pressed
Navigator.of(context).push(GamePage.route(players: _players));
if (startButton.matches(event)) {
Navigator.of(context)
.pushReplacement(GamePage.route(players: _players));
} else if (selectButton.matches(event)) {
Navigator.of(context).pushReplacement(TitlePage.route());
} else if (!_players.contains(event.gamepadId) && event.key == '0') {
_players.add(event.gamepadId);
}
Expand Down Expand Up @@ -70,7 +74,8 @@ class _LobbyPageState extends State<LobbyPage> {
});
} else if (event.isKeyPressed(LogicalKeyboardKey.enter)) {
// The start key was pressed
Navigator.of(context).push(GamePage.route(players: _players));
Navigator.of(context)
.pushReplacement(GamePage.route(players: _players));
}
},
child: ScreenScaffold(
Expand Down
11 changes: 9 additions & 2 deletions lib/title/view/title_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import 'package:lightrunners/widgets/controller_menu.dart';
import 'package:lightrunners/widgets/screen_scaffold.dart';

class TitlePage extends StatelessWidget {
static Route<void> route() {
return MaterialPageRoute<void>(
maintainState: false,
builder: (_) => const TitlePage(),
);
}

const TitlePage({super.key});

@override
Expand All @@ -26,13 +33,13 @@ class TitlePage extends StatelessWidget {
(
name: 'Start',
onPressed: () {
Navigator.of(context).push(LobbyPage.route());
Navigator.of(context).pushReplacement(LobbyPage.route());
},
),
(
name: 'LeaderBoard',
onPressed: () {
Navigator.of(context).push(
Navigator.of(context).pushReplacement(
EndGamePage.route(
{
GamePalette.blue: 100,
Expand Down
27 changes: 21 additions & 6 deletions lib/utils/gamepad_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ class GamepadAnalogAxis implements GamepadKey {
}

class GamepadButtonKey extends GamepadKey {
final String key;
final String linuxKeyName;
final String macosKeyName;

const GamepadButtonKey({required this.key});
const GamepadButtonKey({
required this.linuxKeyName,
required this.macosKeyName,
});

@override
bool matches(GamepadEvent event) {
return event.key == key &&
event.value == 1.0 &&
event.type == KeyType.button;
final isKey = event.key == linuxKeyName || event.key == macosKeyName;
return isKey && event.value == 1.0 && event.type == KeyType.button;
}
}

Expand Down Expand Up @@ -81,5 +84,17 @@ const rightYAxis = GamepadAnalogAxis(
macosKeyName: 'r.joystick - yAxis',
);

const GamepadKey aButton = GamepadButtonKey(key: '0');
const GamepadKey aButton = GamepadButtonKey(
linuxKeyName: '0',
macosKeyName: '???',
);
const GamepadKey startButton = GamepadButtonKey(
linuxKeyName: '7',
macosKeyName: 'line.horizontal.3.circle',
);
const GamepadKey selectButton = GamepadButtonKey(
linuxKeyName: '6',
macosKeyName: '???',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erickzanardo could you please fill up later please

);

const GamepadKey r1Bumper = GamepadBumperKey(key: '5');
6 changes: 6 additions & 0 deletions lib/utils/gamepad_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ class GamepadNavigator {
final Function(int)? xAxisHandler;
final Function(int)? yAxisHandler;
final Function()? onAction;
final Function()? onAny;

GamepadNavigator({
this.xAxisHandler,
this.yAxisHandler,
this.onAction,
this.onAny,
});

int _getValue(GamepadEvent event) {
return GamepadAnalogAxis.normalizedIntensity(event).sign.toInt();
}

void handle(GamepadEvent event) {
if (event.value == 1.0) {
onAny?.call();
}

if (leftXAxis.matches(event)) {
final value = _getValue(event);
if (value != 0) {
Expand Down