Skip to content

Commit

Permalink
Implement pausing (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecreatorormaybenot authored Dec 14, 2024
1 parent c216d5b commit 02bc3a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
19 changes: 19 additions & 0 deletions fireworks/lib/src/foundation/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class FireworkController implements Listenable {
double _globalHue = 42;

late final Ticker _ticker;
var _paused = false;

/// Starts the firework show.
///
Expand All @@ -74,6 +75,22 @@ class FireworkController implements Listenable {
_ticker = vsync.createTicker(_update)..start();
}

/// Pauses the firework show.
///
/// Can only be called while the firework show is running.
void pause() {
assert(!_paused);
_paused = true;
}

/// Resumes the firework show.
///
/// Can only be called while the firework show is paused.
void resume() {
assert(_paused);
_paused = false;
}

final List<VoidCallback> _listeners;

@override
Expand Down Expand Up @@ -127,6 +144,8 @@ class FireworkController implements Listenable {
Duration _lastRocketSpawn = Duration.zero;

void _update(Duration elapsedDuration) {
if (_paused) return;

if (windowSize == Size.zero) {
// We need to wait until we have the size.
return;
Expand Down
12 changes: 12 additions & 0 deletions fireworks_demo/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class _FireworksState extends State<_Fireworks>
);

var _showInfoOverlay = false;
var _isPaused = false;

@override
void dispose() {
Expand All @@ -75,6 +76,16 @@ class _FireworksState extends State<_Fireworks>
_showInfoOverlay = !_showInfoOverlay;
});
},
onSecondaryTap: () {
setState(() {
_isPaused = !_isPaused;
if (!_isPaused) {
_controller.pause();
} else {
_controller.resume();
}
});
},
child: Stack(
children: [
Fireworks(
Expand Down Expand Up @@ -151,6 +162,7 @@ class _FireworksState extends State<_Fireworks>
Text(
'Hover with your mouse to launch fireworks to '
'your mouse :)\n'
'Right click to pause/resume the animation.\n'
'Or just lean back and enjoy the show (:\n\n'
'Click again to close this.',
textAlign: TextAlign.center,
Expand Down
14 changes: 0 additions & 14 deletions fireworks_demo/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
collection:
dependency: transitive
description:
Expand Down Expand Up @@ -89,13 +82,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.4"
material_color_utilities:
dependency: transitive
description:
Expand Down

0 comments on commit 02bc3a9

Please sign in to comment.