mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-13 11:20:19 +08:00
Draggables Overhaul (#660)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/gestures.dart';
|
||||
import 'package:flutter/material.dart' hide Draggable;
|
||||
import 'package:flame/game.dart';
|
||||
|
||||
@@ -32,20 +31,28 @@ class DraggableSquare extends PositionComponent with Draggable {
|
||||
debugColor = _isDragging ? Colors.greenAccent : Colors.purple;
|
||||
}
|
||||
|
||||
Vector2 initialPosition;
|
||||
Vector2 dragDeltaPosition;
|
||||
|
||||
@override
|
||||
bool onReceiveDrag(DragEvent event) {
|
||||
event.onUpdate = (DragUpdateDetails details) {
|
||||
if (!_isDragging) {
|
||||
_isDragging = true;
|
||||
dragDeltaPosition =
|
||||
event.initialPosition.toVector2() - position.clone();
|
||||
}
|
||||
position = details.localPosition.toVector2() - dragDeltaPosition;
|
||||
};
|
||||
event.onEnd = (DragEndDetails details) {
|
||||
_isDragging = false;
|
||||
};
|
||||
bool onDragStart(int pointerId, Vector2 startPosition) {
|
||||
initialPosition = startPosition;
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
bool onDragUpdate(int pointerId, DragUpdateDetails details) {
|
||||
if (!_isDragging) {
|
||||
_isDragging = true;
|
||||
dragDeltaPosition = initialPosition - position;
|
||||
}
|
||||
position = details.localPosition.toVector2() - dragDeltaPosition;
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
bool onDragEnd(int pointerId, DragEndDetails details) {
|
||||
_isDragging = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user