mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 11:43:19 +08:00
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.
68 lines
1.6 KiB
Dart
68 lines
1.6 KiB
Dart
import 'package:examples/commons/ember.dart';
|
|
import 'package:flame/experimental.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:flutter/material.dart' show Colors;
|
|
|
|
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
|
|
changing.
|
|
''';
|
|
|
|
final double zoom;
|
|
late final DraggableEmber square;
|
|
|
|
DraggablesExample({required this.zoom});
|
|
|
|
@override
|
|
Future<void> onLoad() async {
|
|
camera.zoom = zoom;
|
|
add(square = DraggableEmber());
|
|
add(DraggableEmber()..y = 350);
|
|
}
|
|
}
|
|
|
|
// Note: this component does not consider the possibility of multiple
|
|
// simultaneous drags with different pointerIds.
|
|
class DraggableEmber extends Ember with DragCallbacks {
|
|
@override
|
|
bool debugMode = true;
|
|
bool isDragged = false;
|
|
|
|
DraggableEmber({Vector2? position})
|
|
: super(
|
|
position: position ?? Vector2.all(100),
|
|
size: Vector2.all(100),
|
|
);
|
|
|
|
@override
|
|
void update(double dt) {
|
|
super.update(dt);
|
|
debugColor = isDragged && parent is DraggablesExample
|
|
? Colors.greenAccent
|
|
: Colors.purple;
|
|
}
|
|
|
|
@override
|
|
void onDragStart(_) {
|
|
isDragged = true;
|
|
}
|
|
|
|
@override
|
|
void onDragUpdate(DragUpdateEvent info) {
|
|
if (parent is! DraggablesExample) {
|
|
info.continuePropagation = true;
|
|
return;
|
|
}
|
|
|
|
position.add(info.localPosition);
|
|
info.continuePropagation = false;
|
|
}
|
|
|
|
@override
|
|
void onDragEnd(_) {
|
|
isDragged = false;
|
|
}
|
|
}
|