mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 19:12:31 +08:00
feat!: The HasTappableComponents mixin is no longer needed (#2450)
This PR is the second in a series of refactors that aim to simplify event handling in Flame. The approach is as follows:
Added the MultiTapDispatcher component, which contains the logic that used to be within the HasTappableComponents mixin. This component is internal; it mounts to a FlameGame directly, and ensures that it is a singleton.
Whenever any TapCallbacks component is added to a game, it automatically adds the MultiTapDispatcher component (unless there is already one), which in turn registers a tap gesture detector with GestureDetectorBuilder and rebuilds the game widget.
The end result is that now in order to make a component tappable you only need to add the TapCallbacks mixin to that component, everything else will be handled by the framework.
Consequently, the HasTappableComponents mixin is now empty and marked as deprecated.
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
import 'package:examples/commons/ember.dart';
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/experimental.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flutter/material.dart' show Colors;
|
||||
|
||||
class DraggablesExample extends FlameGame with HasDraggables {
|
||||
class DraggablesExample extends FlameGame {
|
||||
static const String description = '''
|
||||
In this example we show you can use the `Draggable` mixin on
|
||||
`PositionComponent`s. Drag around the Embers and see their position
|
||||
@ -26,9 +25,10 @@ class DraggablesExample extends FlameGame with HasDraggables {
|
||||
|
||||
// Note: this component does not consider the possibility of multiple
|
||||
// simultaneous drags with different pointerIds.
|
||||
class DraggableEmber extends Ember with Draggable {
|
||||
class DraggableEmber extends Ember with DragCallbacks {
|
||||
@override
|
||||
bool debugMode = true;
|
||||
bool isDragged = false;
|
||||
|
||||
DraggableEmber({Vector2? position})
|
||||
: super(
|
||||
@ -45,12 +45,23 @@ class DraggableEmber extends Ember with Draggable {
|
||||
}
|
||||
|
||||
@override
|
||||
bool onDragUpdate(DragUpdateInfo info) {
|
||||
void onDragStart(_) {
|
||||
isDragged = true;
|
||||
}
|
||||
|
||||
@override
|
||||
void onDragUpdate(DragUpdateEvent info) {
|
||||
if (parent is! DraggablesExample) {
|
||||
return true;
|
||||
info.continuePropagation = true;
|
||||
return;
|
||||
}
|
||||
|
||||
position.add(info.delta.game);
|
||||
return false;
|
||||
position.add(info.localPosition);
|
||||
info.continuePropagation = false;
|
||||
}
|
||||
|
||||
@override
|
||||
void onDragEnd(_) {
|
||||
isDragged = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user