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: Add ability to add/remove multiple overlays at once #1657

Merged
merged 7 commits into from
May 27, 2022
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
2 changes: 1 addition & 1 deletion packages/flame/lib/src/game/game_widget/game_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class GameWidget<T extends Game> extends StatefulWidget {
game.mouseCursor = mouseCursor;
}
if (initialActiveOverlays != null) {
initialActiveOverlays.forEach(game.overlays.add);
game.overlays.addAll(initialActiveOverlays);
}
}

Expand Down
22 changes: 22 additions & 0 deletions packages/flame/lib/src/game/mixins/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ class _ActiveOverlays {
return setChanged;
}

/// Marks [overlayNames] to be rendered.
void addAll(Iterable<String> overlayNames) {
final overlayCountBeforeAdded = _activeOverlays.length;
_activeOverlays.addAll(overlayNames);

final overlayCountAfterAdded = _activeOverlays.length;
if (overlayCountBeforeAdded != overlayCountAfterAdded) {
_game?._refreshWidget();
}
}

/// Hides the [overlayName].
bool remove(String overlayName) {
final hasRemoved = _activeOverlays.remove(overlayName);
Expand All @@ -334,6 +345,17 @@ class _ActiveOverlays {
return hasRemoved;
}

/// Hides multiple overlays specified in [overlayNames].
void removeAll(Iterable<String> overlayNames) {
final overlayCountBeforeRemoved = _activeOverlays.length;
_activeOverlays.removeAll(overlayNames);

final overlayCountAfterRemoved = _activeOverlays.length;
if (overlayCountBeforeRemoved != overlayCountAfterRemoved) {
_game?._refreshWidget();
}
}

/// The names of all currently active overlays.
Set<String> get value => _activeOverlays;

Expand Down
21 changes: 21 additions & 0 deletions packages/flame/test/game/active_overlays_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ void main() {
});
});

group('addAll', () {
test('can add multiple overlays at once', () {
final overlays = FlameGame().overlays;
overlays.addAll(['test', 'test2']);
expect(overlays.isActive('test'), true);
expect(overlays.isActive('test2'), true);
});
});

group('removeAll', () {
test('can remove multiple overlays at once', () {
final overlays = FlameGame().overlays;
overlays.addAll(['test', 'test2']);

overlays.removeAll(['test', 'test2']);

expect(overlays.isActive('test'), false);
expect(overlays.isActive('test2'), false);
});
});

group('remove', () {
test('can remove an overlay', () {
final overlays = FlameGame().overlays;
Expand Down