style: refactored level into smaller methods

This commit is contained in:
Orangerot 2025-01-13 15:16:08 +01:00
parent 7bf1ead030
commit d52f60ef92

View file

@ -72,13 +72,61 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
_position = value; _position = value;
}), }),
); );
_durationSubscription = player.onDurationChanged.listen((duration) {
// listen for new values from player
_durationSubscription =
player.onDurationChanged.listen((Duration duration) {
setState(() => _duration = duration); setState(() => _duration = duration);
}); });
_positionSubscription =
player.onPositionChanged.listen((Duration position) {
setState(() => _position = position);
for (final note in notes) {
_noteHitCheck(note, position);
}
});
player.onPlayerComplete.listen((void _) {
Route route = MaterialPageRoute(
builder: (context) => GameOverStats(
simfile: widget.simfile,
notes: notes,
));
Navigator.pushReplacement(context, route);
});
// listen for esense button and pause/resume
if (ESenseInput.instance.connected) { if (ESenseInput.instance.connected) {
_buttonSubscription = ESenseInput.instance.buttonEvents().listen((event) { _buttonSubscription = ESenseInput.instance.buttonEvents().listen((event) {
if (!event.pressed) { if (!event.pressed) {
_pauseResume();
}
});
}
widget.simfile.chartSimplest?.beats.forEach((time, noteData) {
int arrowIndex = noteData.indexOf('1');
if (arrowIndex < 0 || arrowIndex > 3) {
return;
}
notes.add(Note(time: time, direction: ArrowDirection.values[arrowIndex]));
});
player.play(DeviceFileSource(widget.simfile.audioPath!));
}
@override
void dispose() {
_animationController.dispose();
_durationSubscription?.cancel();
_positionSubscription?.cancel();
_buttonSubscription?.cancel();
player.dispose();
super.dispose();
}
void _pauseResume() {
if (_isPlaying) { if (_isPlaying) {
player.pause(); player.pause();
setState(() { setState(() {
@ -91,34 +139,11 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
}); });
} }
} }
});
}
_positionSubscription = player.onPositionChanged.listen( void _noteHitCheck(Note note, Duration time) {
(p) => setState(() => _position = p), note.position = note.time - time.inMilliseconds / 60000.0;
);
player.onDurationChanged.listen((Duration d) {
// print('Max duration: $d');
setState(() => _duration = d);
});
player.onPlayerComplete.listen((void _) {
Route route = MaterialPageRoute(
builder: (context) => GameOverStats(
simfile: widget.simfile,
notes: notes,
));
Navigator.pushReplacement(context, route);
});
player.onPositionChanged.listen((Duration p) {
// print('Current position: $p');
setState(() => _position = p);
for (final note in notes) {
note.position = note.time - p.inMilliseconds / 60000.0;
if (note.wasHit != null) { if (note.wasHit != null) {
continue; return;
} }
if (note.position.abs() < 0.5 * 1.0 / 60.0) { if (note.position.abs() < 0.5 * 1.0 / 60.0) {
InputDirection esenseDirection = InputDirection esenseDirection =
@ -163,25 +188,8 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
}); });
} }
} }
});
widget.simfile.chartSimplest!.beats.forEach((time, noteData) { void _keyboardHandler(event) {
int arrowIndex = noteData.indexOf('1');
if (arrowIndex < 0 || arrowIndex > 3) {
return;
}
notes.add(Note(time: time, direction: ArrowDirection.values[arrowIndex]));
});
player.play(DeviceFileSource(widget.simfile.audioPath!));
}
@override
Widget build(BuildContext context) {
return KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (event) {
bool isDown = false; bool isDown = false;
if (event is KeyDownEvent) { if (event is KeyDownEvent) {
isDown = true; isDown = true;
@ -204,24 +212,19 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
inputDirection.right = isDown; inputDirection.right = isDown;
break; break;
} }
}, }
@override
Widget build(BuildContext context) {
return KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: _keyboardHandler,
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(
leading: IconButton( leading: IconButton(
icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow), icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
onPressed: () { onPressed: _pauseResume,
if (_isPlaying) {
player.pause();
setState(() {
_isPlaying = false;
});
} else {
player.resume();
setState(() {
_isPlaying = true;
});
}
},
), ),
title: Text(widget.simfile.tags['TITLE']!), title: Text(widget.simfile.tags['TITLE']!),
actions: [ actions: [
@ -273,14 +276,4 @@ class _LevelState extends State<Level> with SingleTickerProviderStateMixin {
])), ])),
); );
} }
@override
void dispose() {
_animationController.dispose();
_durationSubscription?.cancel();
_positionSubscription?.cancel();
_buttonSubscription?.cancel();
player.dispose();
super.dispose();
}
} }