mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 12:28:03 +08:00
fix: Use root game for gestures (#2756)
Since registering detectors won't work on a non-root game we have to register the dispatchers on the root game.
This commit is contained in:
@ -381,6 +381,8 @@ class Component {
|
|||||||
|
|
||||||
@internal
|
@internal
|
||||||
static Game? staticGameInstance;
|
static Game? staticGameInstance;
|
||||||
|
|
||||||
|
/// Fetches the nearest [FlameGame] ancestor to the component.
|
||||||
FlameGame? findGame() {
|
FlameGame? findGame() {
|
||||||
assert(
|
assert(
|
||||||
staticGameInstance is FlameGame || staticGameInstance == null,
|
staticGameInstance is FlameGame || staticGameInstance == null,
|
||||||
@ -393,6 +395,15 @@ class Component {
|
|||||||
((this is FlameGame) ? (this as FlameGame) : _parent?.findGame());
|
((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.
|
/// Whether the children list contains the given component.
|
||||||
///
|
///
|
||||||
/// This method uses reference equality.
|
/// This method uses reference equality.
|
||||||
|
|||||||
@ -28,7 +28,7 @@ mixin DoubleTapCallbacks on Component {
|
|||||||
@override
|
@override
|
||||||
void onMount() {
|
void onMount() {
|
||||||
super.onMount();
|
super.onMount();
|
||||||
final game = findGame()!;
|
final game = findRootGame()!;
|
||||||
if (game.findByKey(const DoubleTapDispatcherKey()) == null) {
|
if (game.findByKey(const DoubleTapDispatcherKey()) == null) {
|
||||||
final dispatcher = DoubleTapDispatcher();
|
final dispatcher = DoubleTapDispatcher();
|
||||||
game.registerKey(const DoubleTapDispatcherKey(), dispatcher);
|
game.registerKey(const DoubleTapDispatcherKey(), dispatcher);
|
||||||
|
|||||||
@ -65,7 +65,7 @@ mixin DragCallbacks on Component {
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
void onMount() {
|
void onMount() {
|
||||||
super.onMount();
|
super.onMount();
|
||||||
final game = findGame()!;
|
final game = findRootGame()!;
|
||||||
if (game.findByKey(const MultiDragDispatcherKey()) == null) {
|
if (game.findByKey(const MultiDragDispatcherKey()) == null) {
|
||||||
final dispatcher = MultiDragDispatcher();
|
final dispatcher = MultiDragDispatcher();
|
||||||
game.registerKey(const MultiDragDispatcherKey(), dispatcher);
|
game.registerKey(const MultiDragDispatcherKey(), dispatcher);
|
||||||
|
|||||||
@ -18,7 +18,7 @@ mixin PointerMoveCallbacks on Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void onMountHandler(PointerMoveCallbacks instance) {
|
static void onMountHandler(PointerMoveCallbacks instance) {
|
||||||
final game = instance.findGame()!;
|
final game = instance.findRootGame()!;
|
||||||
const key = MouseMoveDispatcherKey();
|
const key = MouseMoveDispatcherKey();
|
||||||
if (game.findByKey(key) == null) {
|
if (game.findByKey(key) == null) {
|
||||||
final dispatcher = PointerMoveDispatcher();
|
final dispatcher = PointerMoveDispatcher();
|
||||||
|
|||||||
@ -22,7 +22,7 @@ mixin TapCallbacks on Component {
|
|||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
void onMount() {
|
void onMount() {
|
||||||
super.onMount();
|
super.onMount();
|
||||||
final game = findGame()!;
|
final game = findRootGame()!;
|
||||||
if (game.findByKey(const MultiTapDispatcherKey()) == null) {
|
if (game.findByKey(const MultiTapDispatcherKey()) == null) {
|
||||||
final dispatcher = MultiTapDispatcher();
|
final dispatcher = MultiTapDispatcher();
|
||||||
game.registerKey(const MultiTapDispatcherKey(), dispatcher);
|
game.registerKey(const MultiTapDispatcherKey(), dispatcher);
|
||||||
|
|||||||
@ -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', () {
|
group('miscellaneous', () {
|
||||||
testWithFlameGame('childrenFactory', (game) async {
|
testWithFlameGame('childrenFactory', (game) async {
|
||||||
final component0 = Component();
|
final component0 = Component();
|
||||||
|
|||||||
Reference in New Issue
Block a user