-
-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added ability to control overlays via the RouterComponent (#1840)
Class _ActiveOverlays renamed into OverlayManager and move into its own file. The class is still marked as internal. In GameWidget.controlled constructor the properties initialActiveOverlays and mouseCursor were missing, now added. Created class OverlayRoute for encapsulating an overlay-handling logic within the RouterComponent. New overlay builders can now be added to the game at runtime. The OverlaysManager._activeOverlays property is now a list instead of a set, so that the overlays would be displayed in the specific order requested by the user.
- Loading branch information
Showing
10 changed files
with
494 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import 'package:flame/src/components/component.dart'; | ||
import 'package:flame/src/components/route.dart'; | ||
import 'package:flame/src/game/game.dart'; | ||
import 'package:flutter/widgets.dart' hide Route; | ||
import 'package:meta/meta.dart'; | ||
|
||
/// [OverlayRoute] is a class that allows adding/removing game overlays as if | ||
/// they were ordinary [Route]s. | ||
/// | ||
/// There are several differences between an [OverlayRoute] and the regular | ||
/// [Route]s: | ||
/// - the overlays are always rendered on top of the game canvas, so if you push | ||
/// a regular route on top of an overlay route, the overlay route would still | ||
/// be displayed on top. | ||
/// - the `builder` of an overlay route produces a widget instead of a | ||
/// component. | ||
class OverlayRoute extends Route { | ||
/// An overlay route that uses the specified [builder]. This builder will be | ||
/// registered with the Game's map of overlay builders when this route is | ||
/// first activated. | ||
OverlayRoute(OverlayBuilder builder, {super.transparent = true}) | ||
: _builder = builder, | ||
super(null); | ||
|
||
/// An overlay route that corresponds to an overlay that was already declared | ||
/// within GameWidget's `overlayBuilderMap`. | ||
OverlayRoute.existing({super.transparent = true}) | ||
: _builder = null, | ||
super(null); | ||
|
||
final OverlayBuilder? _builder; | ||
|
||
@internal | ||
Game get game => findGame()!; | ||
|
||
@override | ||
Component build() { | ||
if (_builder != null) { | ||
game.overlays.addEntry(name, _builder!); | ||
} | ||
return Component(); | ||
} | ||
|
||
@mustCallSuper | ||
@override | ||
void onPush(Route? previousRoute) { | ||
final didAdd = game.overlays.add(name); | ||
assert(didAdd, 'An overlay $name was already added before'); | ||
} | ||
|
||
@mustCallSuper | ||
@override | ||
void onPop(Route nextRoute) { | ||
final didRemove = game.overlays.remove(name); | ||
assert(didRemove, 'An overlay $name was already removed'); | ||
} | ||
} | ||
|
||
typedef OverlayBuilder = Widget Function(BuildContext context, Game game); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.