Files
flame/examples/lib/stories/controls/draggables.dart
Lukas Klingsbo 948f277e5d Fix gestures when isHud = true and camera is transformed (#843)
* Fix gestures when isHud = true and camera is transformed

* Use Info instead of Event everywhere
2021-06-09 23:47:59 +02:00

71 lines
1.7 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/gestures.dart';
import 'package:flutter/material.dart' show Colors;
// Note: this component does not consider the possibility of multiple
// simultaneous drags with different pointerIds.
class DraggableSquare extends PositionComponent
with Draggable, HasGameRef<DraggablesGame> {
@override
bool debugMode = true;
DraggableSquare({Vector2? position})
: super(
position: position ?? Vector2.all(100),
size: Vector2.all(100),
);
Vector2? dragDeltaPosition;
bool get isDragging => dragDeltaPosition != null;
@override
void update(double dt) {
super.update(dt);
debugColor = isDragging ? Colors.greenAccent : Colors.purple;
}
@override
bool onDragStart(int pointerId, DragStartInfo info) {
dragDeltaPosition = info.eventPosition.game - position;
return false;
}
@override
bool onDragUpdate(int pointerId, DragUpdateInfo info) {
final dragDeltaPosition = this.dragDeltaPosition;
if (dragDeltaPosition == null) {
return false;
}
position.setFrom(info.eventPosition.game - dragDeltaPosition);
return false;
}
@override
bool onDragEnd(int pointerId, _) {
dragDeltaPosition = null;
return false;
}
@override
bool onDragCancel(int pointerId) {
dragDeltaPosition = null;
return false;
}
}
class DraggablesGame extends BaseGame with HasDraggableComponents {
final double zoom;
DraggablesGame({required this.zoom});
@override
Future<void> onLoad() async {
camera.zoom = zoom;
add(DraggableSquare());
add(DraggableSquare()..y = 350);
}
}