mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-11 18:11:12 +08:00
Draggables Overhaul (#660)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
|
||||
void main() {
|
||||
@@ -20,8 +21,8 @@ class MyGame extends BaseGame
|
||||
|
||||
final Map<int, Rect> _taps = {};
|
||||
|
||||
Offset _start;
|
||||
Offset _end;
|
||||
Vector2 _start;
|
||||
Vector2 _end;
|
||||
Rect _panRect;
|
||||
|
||||
MyGame() {
|
||||
@@ -49,37 +50,31 @@ class MyGame extends BaseGame
|
||||
}
|
||||
|
||||
@override
|
||||
void onReceiveDrag(DragEvent event) {
|
||||
onPanStart(event.initialPosition);
|
||||
|
||||
event
|
||||
..onUpdate = onPanUpdate
|
||||
..onEnd = onPanEnd
|
||||
..onCancel = onPanCancel;
|
||||
}
|
||||
|
||||
void onPanCancel() {
|
||||
void onDragCancel(int pointerId) {
|
||||
_end = null;
|
||||
_start = null;
|
||||
_panRect = null;
|
||||
}
|
||||
|
||||
void onPanStart(Offset position) {
|
||||
@override
|
||||
void onDragStart(int pointerId, Vector2 initialPosition) {
|
||||
_end = null;
|
||||
_start = position;
|
||||
_start = initialPosition;
|
||||
}
|
||||
|
||||
void onPanUpdate(DragUpdateDetails details) {
|
||||
_end = details.localPosition;
|
||||
@override
|
||||
void onDragUpdate(int pointerId, DragUpdateDetails details) {
|
||||
_end = details.localPosition.toVector2();
|
||||
}
|
||||
|
||||
void onPanEnd(DragEndDetails details) {
|
||||
@override
|
||||
void onDragEnd(int pointerId, DragEndDetails details) {
|
||||
if (_start != null && _end != null) {
|
||||
_panRect = Rect.fromLTRB(
|
||||
_start.dx,
|
||||
_start.dy,
|
||||
_end.dx,
|
||||
_end.dy,
|
||||
_start.x,
|
||||
_start.y,
|
||||
_end.x,
|
||||
_end.y,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user