Skip to content

Commit

Permalink
fix: Use root game for gestures (#2756)
Browse files Browse the repository at this point in the history
Since registering detectors won't work on a non-root game we have to register the dispatchers on the root game.
  • Loading branch information
spydon committed Sep 21, 2023
1 parent 521e56b commit f5d0cb3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
11 changes: 11 additions & 0 deletions packages/flame/lib/src/components/core/component.dart
Expand Up @@ -381,6 +381,8 @@ class Component {

@internal
static Game? staticGameInstance;

/// Fetches the nearest [FlameGame] ancestor to the component.
FlameGame? findGame() {
assert(
staticGameInstance is FlameGame || staticGameInstance == null,
Expand All @@ -393,6 +395,15 @@ class Component {
((this is FlameGame) ? (this as FlameGame) : _parent?.findGame());
}

/// Fetches the root [FlameGame] ancestor to the component.
FlameGame? findRootGame() {
var game = findGame();
while (game?.parent != null) {
game = game!.parent!.findGame();
}
return game;
}

/// Whether the children list contains the given component.
///
/// This method uses reference equality.
Expand Down
Expand Up @@ -28,7 +28,7 @@ mixin DoubleTapCallbacks on Component {
@override
void onMount() {
super.onMount();
final game = findGame()!;
final game = findRootGame()!;
if (game.findByKey(const DoubleTapDispatcherKey()) == null) {
final dispatcher = DoubleTapDispatcher();
game.registerKey(const DoubleTapDispatcherKey(), dispatcher);
Expand Down
Expand Up @@ -65,7 +65,7 @@ mixin DragCallbacks on Component {
@mustCallSuper
void onMount() {
super.onMount();
final game = findGame()!;
final game = findRootGame()!;
if (game.findByKey(const MultiDragDispatcherKey()) == null) {
final dispatcher = MultiDragDispatcher();
game.registerKey(const MultiDragDispatcherKey(), dispatcher);
Expand Down
Expand Up @@ -18,7 +18,7 @@ mixin PointerMoveCallbacks on Component {
}

static void onMountHandler(PointerMoveCallbacks instance) {
final game = instance.findGame()!;
final game = instance.findRootGame()!;
const key = MouseMoveDispatcherKey();
if (game.findByKey(key) == null) {
final dispatcher = PointerMoveDispatcher();
Expand Down
Expand Up @@ -22,7 +22,7 @@ mixin TapCallbacks on Component {
@mustCallSuper
void onMount() {
super.onMount();
final game = findGame()!;
final game = findRootGame()!;
if (game.findByKey(const MultiTapDispatcherKey()) == null) {
final dispatcher = MultiTapDispatcher();
game.registerKey(const MultiTapDispatcherKey(), dispatcher);
Expand Down
22 changes: 22 additions & 0 deletions packages/flame/test/components/component_test.dart
Expand Up @@ -1121,6 +1121,28 @@ void main() {
});
});

group('findRootGame()', () {
testWithFlameGame('finds root game in nested game structure',
(game) async {
final component = Component();
await game.ensureAdd(
FlameGame(
children: [
Component(children: [component]),
],
),
);
expect(component.findRootGame(), game);
});

testWithFlameGame('finds root game in non-nested game structure',
(game) async {
final component = Component();
await game.ensureAdd(component);
expect(component.findRootGame(), game);
});
});

group('miscellaneous', () {
testWithFlameGame('childrenFactory', (game) async {
final component0 = Component();
Expand Down

0 comments on commit f5d0cb3

Please sign in to comment.