Skip to content
Open
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
10 changes: 10 additions & 0 deletions packages/flame/lib/src/events/callbacks/double_tap_callbacks.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:meta/meta.dart';

/// [DoubleTapCallbacks] adds the ability to receive double-tap events in a
/// component.
Expand All @@ -25,8 +26,17 @@ mixin DoubleTapCallbacks on Component {
void onDoubleTapCancel(DoubleTapCancelEvent event) {}

@override
@mustCallSuper
void onMount() {
super.onMount();
DoubleTapDispatcher.addDispatcher(this);
findRootGame()?.adjustPointerEventHandlerCount(1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the extra recursive findRootGame() on every mount/unmount worth the optmization?

}

@override
@mustCallSuper
void onRemove() {
findRootGame()?.adjustPointerEventHandlerCount(-1);
super.onRemove();
}
}
2 changes: 2 additions & 0 deletions packages/flame/lib/src/events/callbacks/drag_callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ mixin DragCallbacks on Component {
hasDrag: true,
hasScale: false,
);
findRootGame()?.adjustPointerEventHandlerCount(1);
}

@override
@mustCallSuper
void onRemove() {
findRootGame()?.adjustPointerEventHandlerCount(-1);
MultiDragScaleDispatcher.removeDispatcher(
this,
hasDrag: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/flame/lib/src/events/callbacks/scale_callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ mixin ScaleCallbacks on Component {
hasDrag: false,
hasScale: true,
);
findRootGame()?.adjustPointerEventHandlerCount(1);
}

@override
@mustCallSuper
void onRemove() {
findRootGame()?.adjustPointerEventHandlerCount(-1);
MultiDragScaleDispatcher.removeDispatcher(
this,
hasDrag: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ mixin SecondaryTapCallbacks on Component {
void onMount() {
super.onMount();
NonPrimaryTapDispatcher.addDispatcher(this);
findRootGame()?.adjustPointerEventHandlerCount(1);
}

@override
@mustCallSuper
void onRemove() {
findRootGame()?.adjustPointerEventHandlerCount(-1);
super.onRemove();
}
}
8 changes: 8 additions & 0 deletions packages/flame/lib/src/events/callbacks/tap_callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ mixin TapCallbacks on Component {
void onMount() {
super.onMount();
MultiTapDispatcher.addDispatcher(this);
findRootGame()?.adjustPointerEventHandlerCount(1);
}

@override
@mustCallSuper
void onRemove() {
findRootGame()?.adjustPointerEventHandlerCount(-1);
super.onRemove();
}
}
17 changes: 17 additions & 0 deletions packages/flame/lib/src/game/flame_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ class FlameGame<W extends World> extends ComponentTreeRoot
point.y < canvasSize.y;
}

/// The number of currently mounted components (in this game or any nested
/// game) that can receive pointer events. Maintained by the event callback
/// mixins ([TapCallbacks], [DragCallbacks], [DoubleTapCallbacks],
/// [ScaleCallbacks], [SecondaryTapCallbacks]) on the root game, so that
/// [containsEventHandlerAt] can skip hit testing entirely for games
/// without any pointer-event handlers.
int _pointerEventHandlerCount = 0;

@internal
void adjustPointerEventHandlerCount(int delta) {
_pointerEventHandlerCount += delta;
assert(_pointerEventHandlerCount >= 0);

@luanpotter luanpotter Aug 7, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might not be very actionable but can we still add a message

}

@override
bool containsEventHandlerAt(Vector2 position) {
// Deprecated game-level detector mixins handle events for the entire
Expand All @@ -262,6 +276,9 @@ class FlameGame<W extends World> extends ComponentTreeRoot
this is MultiTouchDragDetector) {
return true;
}
if (_pointerEventHandlerCount == 0) {
return false;
}
for (final component in super.componentsAtPoint(position)) {
if (component is TapCallbacks ||
component is DragCallbacks ||
Expand Down
Loading